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:
@@ -0,0 +1,38 @@
|
||||
# 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/whois.c
|
||||
# Support files and stubs (pathname alphabetical)
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
${ZTST_DIR}/ztest.c
|
||||
)
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Legrand North America, LLC.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
/* @file
|
||||
* @brief test BACnet integer encode/decode APIs
|
||||
*/
|
||||
|
||||
#include <ztest.h>
|
||||
#include <bacnet/bacdef.h>
|
||||
#include <bacnet/whois.h>
|
||||
|
||||
/**
|
||||
* @addtogroup bacnet_tests
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Test
|
||||
*/
|
||||
static int whois_decode_apdu(
|
||||
uint8_t *apdu, unsigned apdu_len, int32_t *pLow_limit, int32_t *pHigh_limit)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* optional limits - must be used as a pair */
|
||||
if (apdu_len >= 2) {
|
||||
/* optional checking - most likely was already done prior to this call
|
||||
*/
|
||||
if (apdu[0] != PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu[1] != SERVICE_UNCONFIRMED_WHO_IS) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = whois_decode_service_request(
|
||||
&apdu[2], apdu_len - 2, pLow_limit, pHigh_limit);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static void testWhoIs(void)
|
||||
{
|
||||
uint8_t apdu[480] = { 0 };
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
int32_t low_limit = -1;
|
||||
int32_t high_limit = -1;
|
||||
int32_t test_low_limit = 0;
|
||||
int32_t test_high_limit = 0;
|
||||
|
||||
/* normal who-is without limits */
|
||||
len = whois_encode_apdu(&apdu[0], low_limit, high_limit);
|
||||
zassert_true(len > 0, NULL);
|
||||
apdu_len = len;
|
||||
|
||||
len = whois_decode_apdu(
|
||||
&apdu[0], apdu_len, &test_low_limit, &test_high_limit);
|
||||
zassert_not_equal(len, BACNET_STATUS_ERROR, NULL);
|
||||
zassert_equal(test_low_limit, low_limit, NULL);
|
||||
zassert_equal(test_high_limit, high_limit, NULL);
|
||||
|
||||
/* normal who-is with limits - complete range */
|
||||
for (low_limit = 0; low_limit <= BACNET_MAX_INSTANCE;
|
||||
low_limit += (BACNET_MAX_INSTANCE / 4)) {
|
||||
for (high_limit = 0; high_limit <= BACNET_MAX_INSTANCE;
|
||||
high_limit += (BACNET_MAX_INSTANCE / 4)) {
|
||||
len = whois_encode_apdu(&apdu[0], low_limit, high_limit);
|
||||
apdu_len = len;
|
||||
zassert_true(len > 0, NULL);
|
||||
len = whois_decode_apdu(
|
||||
&apdu[0], apdu_len, &test_low_limit, &test_high_limit);
|
||||
zassert_not_equal(len, BACNET_STATUS_ERROR, NULL);
|
||||
zassert_equal(test_low_limit, low_limit, NULL);
|
||||
zassert_equal(test_high_limit, high_limit, NULL);
|
||||
}
|
||||
}
|
||||
/* abnormal case:
|
||||
who-is with no limits, but with APDU containing 2 limits */
|
||||
low_limit = 0;
|
||||
high_limit = 0;
|
||||
len = whois_encode_apdu(&apdu[0], low_limit, high_limit);
|
||||
zassert_true(len > 0, NULL);
|
||||
apdu_len = len;
|
||||
low_limit = -1;
|
||||
high_limit = -1;
|
||||
len = whois_encode_apdu(&apdu[0], low_limit, high_limit);
|
||||
zassert_true(len > 0, NULL);
|
||||
apdu_len = len;
|
||||
len = whois_decode_apdu(
|
||||
&apdu[0], apdu_len, &test_low_limit, &test_high_limit);
|
||||
zassert_not_equal(len, BACNET_STATUS_ERROR, NULL);
|
||||
zassert_equal(test_low_limit, low_limit, NULL);
|
||||
zassert_equal(test_high_limit, high_limit, NULL);
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(whois_tests,
|
||||
ztest_unit_test(testWhoIs)
|
||||
);
|
||||
|
||||
ztest_run_test_suite(whois_tests);
|
||||
}
|
||||
Reference in New Issue
Block a user