Added BACnetTimeValue unit test. (#312)
Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
# 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
|
||||
"/test/bacnet/[a-zA-Z_/-]*$"
|
||||
"/src"
|
||||
SRC_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
string(REGEX REPLACE
|
||||
"/test/bacnet/[a-zA-Z_/-]*$"
|
||||
"/test"
|
||||
TST_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(ZTST_DIR "${TST_DIR}/ztest/src")
|
||||
|
||||
add_compile_definitions(
|
||||
BIG_ENDIAN=0
|
||||
CONFIG_ZTEST=1
|
||||
BACAPP_ALL
|
||||
)
|
||||
|
||||
include_directories(
|
||||
${SRC_DIR}
|
||||
${TST_DIR}/ztest/include
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
# File(s) under test
|
||||
${SRC_DIR}/bacnet/bactimevalue.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/bacdevobjpropref.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/basic/sys/days.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/hostnport.c
|
||||
${SRC_DIR}/bacnet/lighting.c
|
||||
${SRC_DIR}/bacnet/timestamp.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
${ZTST_DIR}/ztest.c
|
||||
)
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Unit test for BACnetTimeValue
|
||||
* @author Steve Karg <skarg@users.sourceforge.net>
|
||||
* @date June 2022
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <ztest.h>
|
||||
#include "bacnet/bactimevalue.h"
|
||||
#include "bacnet/datetime.h"
|
||||
|
||||
|
||||
/**
|
||||
* @addtogroup bacnet_tests
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API
|
||||
*/
|
||||
static void test_BACnetTimeValue(BACNET_TIME_VALUE *value)
|
||||
{
|
||||
int len, apdu_len;
|
||||
uint8_t apdu[MAX_APDU] = { 0 };
|
||||
BACNET_TIME_VALUE test_value = { 0 };
|
||||
int diff = 0;
|
||||
bool status = false;
|
||||
uint8_t tag_number = 0;
|
||||
|
||||
len = bacapp_encode_time_value(apdu, value);
|
||||
apdu_len = bacapp_decode_time_value(apdu, &test_value);
|
||||
zassert_true(len > 0, NULL);
|
||||
zassert_true(apdu_len > 0, NULL);
|
||||
diff = datetime_compare_time(&test_value.Time, &value->Time);
|
||||
zassert_true(diff == 0, NULL);
|
||||
status = bacapp_same_value(&test_value.Value, &value->Value);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
len = bacapp_encode_context_time_value(apdu, tag_number, value);
|
||||
apdu_len = bacapp_decode_context_time_value(apdu, tag_number, &test_value);
|
||||
zassert_true(len > 0, NULL);
|
||||
zassert_true(apdu_len > 0, NULL);
|
||||
diff = datetime_compare_time(&test_value.Time, &value->Time);
|
||||
zassert_true(diff == 0, NULL);
|
||||
status = bacapp_same_value(&test_value.Value, &value->Value);
|
||||
zassert_true(status, NULL);
|
||||
/* negative testing */
|
||||
tag_number++;
|
||||
apdu_len = bacapp_decode_context_time_value(apdu, tag_number, &test_value);
|
||||
zassert_true(apdu_len < 0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API
|
||||
*/
|
||||
static void test_BACnetTimeValues(void)
|
||||
{
|
||||
BACNET_TIME_VALUE time_value = { 0 };
|
||||
|
||||
test_BACnetTimeValue(&time_value);
|
||||
bacapp_parse_application_data(BACNET_APPLICATION_TAG_REAL, "4.2",
|
||||
&time_value.Value);
|
||||
datetime_time_init_ascii(&time_value.Time, "12:00");
|
||||
test_BACnetTimeValue(&time_value);
|
||||
bacapp_parse_application_data(BACNET_APPLICATION_TAG_UNSIGNED_INT, "99999",
|
||||
&time_value.Value);
|
||||
datetime_time_init_ascii(&time_value.Time, "23:59:59");
|
||||
test_BACnetTimeValue(&time_value);
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(BACnetTimeValue_tests,
|
||||
ztest_unit_test(test_BACnetTimeValues)
|
||||
);
|
||||
|
||||
ztest_run_test_suite(BACnetTimeValue_tests);
|
||||
}
|
||||
Reference in New Issue
Block a user