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
@@ -0,0 +1,44 @@
# 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
MAX_APDU=50
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/bacdevobjpropref.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacapp.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacreal.c
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bactext.c
${SRC_DIR}/bacnet/basic/sys/bigend.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/lighting.c
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
+130
View File
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2020 Legrand North America, LLC.
*
* SPDX-License-Identifier: MIT
*/
/* @file
* @brief test BACnet integer encode/decode APIs
*/
#include <ztest.h>
#include <bacnet/bacdcode.h>
#include <bacnet/bacdevobjpropref.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test
*/
static void testDevObjPropRef(
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *inData)
{
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE outData;
uint8_t buffer[MAX_APDU] = { 0 };
int inLen = 0;
int outLen = 0;
/* encode */
inLen = bacapp_encode_device_obj_property_ref(buffer, inData);
/* add a closing tag at the end of the buffer to verify proper handling
when that is encountered in real packets */
encode_closing_tag(&buffer[inLen], 3);
/* decode */
outLen = bacapp_decode_device_obj_property_ref(buffer, &outData);
zassert_equal(outLen, inLen, NULL);
zassert_equal(
inData->objectIdentifier.instance, outData.objectIdentifier.instance, NULL);
zassert_equal(
inData->objectIdentifier.type, outData.objectIdentifier.type, NULL);
zassert_equal(inData->propertyIdentifier, outData.propertyIdentifier, NULL);
if (inData->arrayIndex != BACNET_ARRAY_ALL) {
zassert_equal(inData->arrayIndex, outData.arrayIndex, NULL);
} else {
zassert_equal(outData.arrayIndex, BACNET_ARRAY_ALL, NULL);
}
if (inData->deviceIdentifier.type == OBJECT_DEVICE) {
zassert_equal(
inData->deviceIdentifier.instance,
outData.deviceIdentifier.instance, NULL);
zassert_equal(
inData->deviceIdentifier.type, outData.deviceIdentifier.type, NULL);
} else {
zassert_equal(outData.deviceIdentifier.instance, BACNET_NO_DEV_ID, NULL);
zassert_equal(outData.deviceIdentifier.type, BACNET_NO_DEV_TYPE, NULL);
}
}
static void testDevIdPropRef(void)
{
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE inData;
/* everything encoded */
inData.objectIdentifier.instance = 0x1234;
inData.objectIdentifier.type = 15;
inData.propertyIdentifier = 25;
inData.arrayIndex = 0x5678;
inData.deviceIdentifier.instance = 0x4343;
inData.deviceIdentifier.type = OBJECT_DEVICE;
testDevObjPropRef(&inData);
/* optional array */
inData.objectIdentifier.instance = 0x1234;
inData.objectIdentifier.type = 15;
inData.propertyIdentifier = 25;
inData.arrayIndex = BACNET_ARRAY_ALL;
inData.deviceIdentifier.instance = 0x4343;
inData.deviceIdentifier.type = OBJECT_DEVICE;
testDevObjPropRef(&inData);
/* optional device ID */
inData.objectIdentifier.instance = 0x1234;
inData.objectIdentifier.type = 15;
inData.propertyIdentifier = 25;
inData.arrayIndex = 1;
inData.deviceIdentifier.instance = 0;
inData.deviceIdentifier.type = BACNET_NO_DEV_TYPE;
testDevObjPropRef(&inData);
/* optional array + optional device ID */
inData.objectIdentifier.instance = 0x1234;
inData.objectIdentifier.type = 15;
inData.propertyIdentifier = 25;
inData.arrayIndex = BACNET_ARRAY_ALL;
inData.deviceIdentifier.instance = 0;
inData.deviceIdentifier.type = BACNET_NO_DEV_TYPE;
testDevObjPropRef(&inData);
}
static void testDevIdRef(void)
{
BACNET_DEVICE_OBJECT_REFERENCE inData;
BACNET_DEVICE_OBJECT_REFERENCE outData;
uint8_t buffer[MAX_APDU];
int inLen;
int outLen;
inData.deviceIdentifier.instance = 0x4343;
inData.deviceIdentifier.type = OBJECT_DEVICE;
inLen = bacapp_encode_device_obj_ref(buffer, &inData);
outLen = bacapp_decode_device_obj_ref(buffer, &outData);
zassert_equal(outLen, inLen, NULL);
zassert_equal(
inData.deviceIdentifier.instance, outData.deviceIdentifier.instance, NULL);
zassert_equal(
inData.deviceIdentifier.type, outData.deviceIdentifier.type, NULL);
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(bacdevobjpropref_tests,
ztest_unit_test(testDevIdPropRef),
ztest_unit_test(testDevIdRef)
);
ztest_run_test_suite(bacdevobjpropref_tests);
}