Feature/zephyr ztest (#118)

* Leverage (older) embedded unit tests into external unit tests build upon copy of Zephyr's ztest library and CMake.

* Expand top-level CMake build to run external unit tests.

* Expand Zephyr module extension to run external unit tests via west or sanitycheck.

Co-authored-by: Gregory Shue <gregory.shue@legrand.us>
This commit is contained in:
Greg Shue
2020-09-16 05:33:34 -07:00
committed by GitHub
parent a7b2e94cb7
commit 19869dccdb
399 changed files with 21885 additions and 5 deletions
+33
View File
@@ -0,0 +1,33 @@
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(test_${basename}
VERSION 1.0.0
LANGUAGES C)
string(REGEX REPLACE "/bacnet/[a-zA-Z_/-]*$" "" TST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(ZTST_DIR "${TST_DIR}/ztest/src")
string(REGEX REPLACE "/test$" "/src" SRC_DIR ${TST_DIR})
add_compile_definitions(
BIG_ENDIAN=0
CONFIG_ZTEST=1
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/datalink/crc.c
# Support files and stubs (pathname alphabetical)
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
+133
View File
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2020 Legrand North America, LLC.
*
* SPDX-License-Identifier: MIT
*/
/* @file
* @brief test BACnet integer encode/decode APIs
*/
#include <ztest.h>
#include <bacnet/datalink/crc.h>
#include <bacnet/bytes.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test CRC8 from Annex G 1.0 of BACnet Standard
*/
static void testCRC8(void)
{
uint8_t crc = 0xff; /* accumulates the crc value */
uint8_t frame_crc; /* appended to the end of the frame */
crc = CRC_Calc_Header(0x00, crc);
zassert_equal(crc, 0x55, NULL);
crc = CRC_Calc_Header(0x10, crc);
zassert_equal(crc, 0xC2, NULL);
crc = CRC_Calc_Header(0x05, crc);
zassert_equal(crc, 0xBC, NULL);
crc = CRC_Calc_Header(0x00, crc);
zassert_equal(crc, 0x95, NULL);
crc = CRC_Calc_Header(0x00, crc);
zassert_equal(crc, 0x73, NULL);
/* send the ones complement of the CRC in place of */
/* the CRC, and the resulting CRC will always equal 0x55. */
frame_crc = ~crc;
zassert_equal(frame_crc, 0x8C, NULL);
/* use the ones complement value and the next to last CRC value */
crc = CRC_Calc_Header(frame_crc, crc);
zassert_equal(crc, 0x55, NULL);
}
/**
* @brief Test CRC8 from Annex G 2.0 of BACnet Standard
*/
static void testCRC16(void)
{
uint16_t crc = 0xffff;
uint16_t data_crc;
crc = CRC_Calc_Data(0x01, crc);
zassert_equal(crc, 0x1E0E, NULL);
crc = CRC_Calc_Data(0x22, crc);
zassert_equal(crc, 0xEB70, NULL);
crc = CRC_Calc_Data(0x30, crc);
zassert_equal(crc, 0x42EF, NULL);
/* send the ones complement of the CRC in place of */
/* the CRC, and the resulting CRC will always equal 0xF0B8. */
data_crc = ~crc;
zassert_equal(data_crc, 0xBD10, NULL);
crc = CRC_Calc_Data(LO_BYTE(data_crc), crc);
zassert_equal(crc, 0x0F3A, NULL);
crc = CRC_Calc_Data(HI_BYTE(data_crc), crc);
zassert_equal(crc, 0xF0B8, NULL);
}
/**
* @brief "Test" to create/log generated CRC8 table
*/
static void testCRC8CreateTable(void)
{
uint8_t crc = 0xff; /* accumulates the crc value */
int i;
printf("static const uint8_t HeaderCRC[256] =\n");
printf("{\n");
printf(" ");
for (i = 0; i < 256; i++) {
crc = CRC_Calc_Header(i, 0);
printf("0x%02x, ", crc);
if (!((i + 1) % 8)) {
printf("\n");
if (i != 255) {
printf(" ");
}
}
}
printf("};\n");
}
/**
* @brief "Test" to create/log generated CRC16 table
*/
static void testCRC16CreateTable(void)
{
uint16_t crc;
int i;
printf("static const uint16_t DataCRC[256] =\n");
printf("{\n");
printf(" ");
for (i = 0; i < 256; i++) {
crc = CRC_Calc_Data(i, 0);
printf("0x%04x, ", crc);
if (!((i + 1) % 8)) {
printf("\n");
if (i != 255) {
printf(" ");
}
}
}
printf("};\n");
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(crc_tests,
ztest_unit_test(testCRC8),
ztest_unit_test(testCRC16),
ztest_unit_test(testCRC8CreateTable),
ztest_unit_test(testCRC16CreateTable)
);
ztest_run_test_suite(crc_tests);
}