Added BACnetRecipient and BACnetAddressBinding codecs for EPICS application (#1163)
* Added BACnetRecipient and BACnetAddressBinding encoding, decoding, ASCII conversion, comparison, and copy functions. * Added library specific string manipulation utilities including strcmp, strncmp, stptok, and snprintf with offset functions. * Added test/README about verification and validation testing.
This commit is contained in:
@@ -46,6 +46,13 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
|
||||
add_compile_options(-Wno-write-strings)
|
||||
add_compile_options(-Wno-implicit-fallthrough)
|
||||
|
||||
# some consistent defines used for all builds - avoids inconsistent error
|
||||
add_compile_definitions(
|
||||
BIG_ENDIAN=0
|
||||
PRINT_ENABLED=1
|
||||
BACAPP_PRINT_ENABLED=1
|
||||
)
|
||||
|
||||
add_link_options(-fprofile-arcs -ftest-coverage)
|
||||
endif()
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# BACnet Stack Verification and Validation Testing
|
||||
|
||||
CMake and C99 are used for the verification and validation testing.
|
||||
|
||||
## Testing in the Github workflow pipeline with Makefile
|
||||
|
||||
The Makefile 'test' recipe is used for the CMake recipe used in the pipeline.
|
||||
|
||||
The 'retest' can be used to re-build the test.
|
||||
|
||||
Changing to the test/build folder and running 'make test' can show
|
||||
more details about a failing build or failing test. Running the actual
|
||||
test by running ./test/build/bacnet/.../test_xyz has more detailed
|
||||
results of running the test.
|
||||
|
||||
## Testing with VS Code with CMake extension and C/C++ build tools
|
||||
|
||||
* Open the test/ folder as a workspace.
|
||||
* The folder structure follows the src/bacnet folder structure.
|
||||
* Configure the CMake extension to use the C/C++ build tools kit.
|
||||
* Configure the CMake extension to use the Debug build type.
|
||||
* Build each or 'all' project using the CMake extension.
|
||||
* Each unit or verification test can be build individually.
|
||||
* The LCOV Cmake recipe will create Line-of-Code Coverage that
|
||||
can be viewed in a browser starting at test/build/lcoverage/index.html
|
||||
* The CTest extension can also be used to run the tests.
|
||||
|
||||
## Validation and Unit testing with ctest or ztest
|
||||
|
||||
* The ctest suite is used from cmake managing the tests. Add new test
|
||||
files following the src/bacnet folder structure into the CMakeLists.txt
|
||||
file. Copy and existing test folder at the same folder depth to start.
|
||||
* The ztest framework (from Zephyr) has been ported to use in the tests
|
||||
and includes the fff mocking macros. The zassert_x() macros include a
|
||||
formatted print as the last argument so that print style debug can be
|
||||
used to show values that fail which aids in writing a test.
|
||||
* Tests can also be written without the zassert_x() macros by including
|
||||
assert.h and using the assert() macro.
|
||||
|
||||
## Use pre-commit tools for changes and additions
|
||||
|
||||
* This library uses 'pre-commit' tools to format the files.
|
||||
* Run pre-commit after staging a file and before committing
|
||||
to avoid pipeline build failures.
|
||||
* If a file fails pre-commit checking, the file will be re-formated
|
||||
correctly by the pre-commit tool. Add the fixed file to the staged
|
||||
files and run pre-commit again.
|
||||
@@ -37,9 +37,11 @@ add_executable(${PROJECT_NAME}
|
||||
# Support files and stubs (pathname alphabetical)
|
||||
${SRC_DIR}/bacnet/bacaction.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
${ZTST_DIR}/ztest_mock.c
|
||||
|
||||
@@ -31,6 +31,10 @@ static void test_BACNET_ADDRESS(void)
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_same(NULL, NULL);
|
||||
zassert_false(status, NULL);
|
||||
bacnet_address_copy(&dest, NULL);
|
||||
bacnet_address_copy(&src, NULL);
|
||||
status = bacnet_address_same(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
/* happy day cases */
|
||||
status = bacnet_address_same(&dest, &dest);
|
||||
@@ -44,6 +48,42 @@ static void test_BACNET_ADDRESS(void)
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_same(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
mac.len = 1;
|
||||
mac.adr[0] = 3;
|
||||
adr.len = 1;
|
||||
adr.adr[0] = 4;
|
||||
dnet = 1234;
|
||||
status = bacnet_address_init(&src, &mac, dnet, &adr);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_address_copy(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_net_same(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
dnet = 0;
|
||||
status = bacnet_address_init(&src, &mac, dnet, &adr);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_address_copy(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_net_same(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
/* net + adr */
|
||||
dnet = 1;
|
||||
status = bacnet_address_init(&src, NULL, dnet, &adr);
|
||||
status = bacnet_address_net_same(&dest, &src);
|
||||
zassert_false(status, NULL);
|
||||
/* net=0, mac */
|
||||
dnet = 1;
|
||||
status = bacnet_address_init(&dest, NULL, dnet, &adr);
|
||||
zassert_true(status, NULL);
|
||||
dnet = 0;
|
||||
status = bacnet_address_init(&src, &mac, dnet, NULL);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_net_same(&dest, &src);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_net_same(NULL, &src);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_net_same(&dest, NULL);
|
||||
zassert_false(status, NULL);
|
||||
/* remote dnet is non-zero */
|
||||
dnet = 1;
|
||||
status = bacnet_address_init(&dest, &mac, dnet, &adr);
|
||||
@@ -105,6 +145,11 @@ static void test_BACNET_ADDRESS(void)
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_same(&dest, &src);
|
||||
zassert_true(status, NULL);
|
||||
/* initialize using copy */
|
||||
bacnet_address_copy(&dest, NULL);
|
||||
zassert_equal(dest.mac_len, 0, NULL);
|
||||
zassert_equal(dest.len, 0, NULL);
|
||||
zassert_equal(dest.net, 0, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
@@ -215,8 +260,36 @@ static void test_BACnetAddress_Codec(void)
|
||||
zassert_equal(value.net, test_value.net, NULL);
|
||||
zassert_equal(value.mac_len, test_value.mac_len, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
/* test deprecated function */
|
||||
test_len = decode_bacnet_address(apdu, &test_value);
|
||||
zassert_equal(len, test_len, NULL);
|
||||
zassert_equal(value.net, test_value.net, NULL);
|
||||
zassert_equal(value.mac_len, test_value.mac_len, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
/* negative tests - NULL value */
|
||||
test_len = bacnet_address_decode(apdu, sizeof(apdu), NULL);
|
||||
zassert_equal(len, test_len, NULL);
|
||||
/* network address */
|
||||
value.net = 1;
|
||||
value.len = 3;
|
||||
value.adr[0] = 1;
|
||||
value.adr[1] = 2;
|
||||
value.adr[2] = 3;
|
||||
len = encode_bacnet_address(NULL, &value);
|
||||
test_len = encode_bacnet_address(apdu, &value);
|
||||
zassert_true(len > 0, NULL);
|
||||
zassert_true(test_len > 0, NULL);
|
||||
zassert_equal(len, test_len, "len=%d test_len=%d", len, test_len);
|
||||
test_len = bacnet_address_decode(apdu, sizeof(apdu), &test_value);
|
||||
zassert_equal(len, test_len, NULL);
|
||||
zassert_equal(value.net, test_value.net, NULL);
|
||||
zassert_equal(value.mac_len, test_value.mac_len, NULL);
|
||||
zassert_equal(value.len, test_value.len, NULL);
|
||||
zassert_equal(value.len, 3, NULL);
|
||||
zassert_equal(value.adr[0], test_value.adr[0], NULL);
|
||||
zassert_equal(value.adr[1], test_value.adr[1], NULL);
|
||||
zassert_equal(value.adr[2], test_value.adr[2], NULL);
|
||||
/* context tagged */
|
||||
tag_number = 1;
|
||||
len = encode_context_bacnet_address(NULL, tag_number, &value);
|
||||
test_len = encode_context_bacnet_address(apdu, tag_number, &value);
|
||||
@@ -229,6 +302,12 @@ static void test_BACnetAddress_Codec(void)
|
||||
zassert_equal(value.net, test_value.net, NULL);
|
||||
zassert_equal(value.mac_len, test_value.mac_len, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
/* validate deprecated function */
|
||||
test_len = decode_context_bacnet_address(apdu, tag_number, &test_value);
|
||||
zassert_equal(len, test_len, NULL);
|
||||
zassert_equal(value.net, test_value.net, NULL);
|
||||
zassert_equal(value.mac_len, test_value.mac_len, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
test_len = bacnet_address_context_decode(
|
||||
apdu, sizeof(apdu), tag_number, &test_value);
|
||||
/* negative tests - NULL value */
|
||||
@@ -256,9 +335,18 @@ static void test_bacnet_vmac_entry_codec(void)
|
||||
{
|
||||
uint8_t apdu[MAX_APDU];
|
||||
BACNET_VMAC_ENTRY value = { 0 }, test_value = { 0 };
|
||||
BACNET_ADDRESS test_addr = { 0 };
|
||||
int test_len = 0, apdu_len = 0, null_len = 0;
|
||||
bool status = false;
|
||||
unsigned i;
|
||||
|
||||
status = bacnet_vmac_address_set(NULL, 0);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_vmac_address_set(&test_addr, 12345);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(test_addr.mac_len, 3, NULL);
|
||||
zassert_equal(test_addr.net, 0, NULL);
|
||||
/* VMAC */
|
||||
value.virtual_mac_address.adr[0] = 1;
|
||||
value.virtual_mac_address.adr[1] = 2;
|
||||
value.virtual_mac_address.adr[2] = 3;
|
||||
@@ -268,6 +356,8 @@ static void test_bacnet_vmac_entry_codec(void)
|
||||
value.native_mac_address[2] = 6;
|
||||
value.native_mac_address[3] = 7;
|
||||
value.native_mac_address_len = 4;
|
||||
null_len = bacnet_vmac_entry_encode(NULL, sizeof(apdu), NULL);
|
||||
zassert_equal(null_len, 0, NULL);
|
||||
null_len = bacnet_vmac_entry_encode(NULL, sizeof(apdu), &value);
|
||||
apdu_len = bacnet_vmac_entry_encode(apdu, sizeof(apdu), &value);
|
||||
zassert_true(apdu_len > 0, NULL);
|
||||
@@ -302,6 +392,124 @@ static void test_bacnet_vmac_entry_codec(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacnet_address_tests, test_bacnet_address_ascii)
|
||||
#else
|
||||
static void test_bacnet_address_ascii(void)
|
||||
#endif
|
||||
{
|
||||
char ascii_mac_net_adr[80] = "{ff:00:ff:01:ff:02,1,7f}";
|
||||
char ascii_mac_net[80] = "{192.168.1.1:47808,0}";
|
||||
BACNET_ADDRESS value = { 0 };
|
||||
bool status;
|
||||
|
||||
status = bacnet_address_from_ascii(NULL, NULL);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_from_ascii(&value, NULL);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_from_ascii(NULL, ascii_mac_net_adr);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_from_ascii(&value, ascii_mac_net_adr);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
zassert_equal(value.mac[0], 0xff, NULL);
|
||||
zassert_equal(value.mac[1], 0x00, NULL);
|
||||
zassert_equal(value.mac[2], 0xff, NULL);
|
||||
zassert_equal(value.mac[3], 0x01, NULL);
|
||||
zassert_equal(value.mac[4], 0xff, NULL);
|
||||
zassert_equal(value.mac[5], 0x02, NULL);
|
||||
zassert_equal(value.net, 1, NULL);
|
||||
zassert_equal(value.len, 1, NULL);
|
||||
zassert_equal(value.adr[0], 0x7f, NULL);
|
||||
status = bacnet_address_from_ascii(&value, ascii_mac_net);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(value.mac_len, 6, NULL);
|
||||
zassert_equal(value.mac[0], 192, NULL);
|
||||
zassert_equal(value.mac[1], 168, NULL);
|
||||
zassert_equal(value.mac[2], 1, NULL);
|
||||
zassert_equal(value.mac[3], 1, NULL);
|
||||
zassert_equal(value.net, 0, NULL);
|
||||
zassert_equal(value.len, 0, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacnet_address_tests, test_BACNET_ADDRESS_BINDING)
|
||||
#else
|
||||
static void test_BACNET_ADDRESS_BINDING(void)
|
||||
#endif
|
||||
{
|
||||
uint8_t apdu[MAX_APDU] = { 0 };
|
||||
BACNET_ADDRESS addr = {
|
||||
.mac_len = 1, .mac[0] = 0x01, .net = 0, .adr[0] = 0, .len = 0
|
||||
};
|
||||
BACNET_ADDRESS_BINDING binding = { 0 }, test_binding = { 0 };
|
||||
bool status = false;
|
||||
int len, test_len, apdu_len, null_len;
|
||||
char str[80] = "";
|
||||
|
||||
null_len = bacnet_address_binding_type_encode(NULL, NULL);
|
||||
zassert_equal(null_len, 0, NULL);
|
||||
status = bacnet_address_binding_init(&binding, 12345, &addr);
|
||||
len = bacnet_address_binding_type_encode(NULL, &binding);
|
||||
null_len = bacnet_address_binding_type_encode(NULL, &binding);
|
||||
zassert_not_equal(null_len, 0, NULL);
|
||||
apdu_len = bacnet_address_binding_type_encode(apdu, &binding);
|
||||
zassert_not_equal(null_len, 0, NULL);
|
||||
zassert_equal(null_len, apdu_len, NULL);
|
||||
test_len = bacnet_address_binding_decode(apdu, apdu_len, &test_binding);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
status = bacnet_address_same(
|
||||
&binding.device_address, &test_binding.device_address);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_binding_same(&binding, &test_binding);
|
||||
zassert_true(status, NULL);
|
||||
null_len = bacnet_address_binding_encode(NULL, 0, &binding);
|
||||
zassert_equal(null_len, 0, NULL);
|
||||
null_len = bacnet_address_binding_encode(NULL, 0, NULL);
|
||||
zassert_equal(null_len, 0, NULL);
|
||||
null_len = bacnet_address_binding_encode(NULL, sizeof(apdu), &binding);
|
||||
zassert_not_equal(null_len, 0, NULL);
|
||||
apdu_len = bacnet_address_binding_encode(apdu, sizeof(apdu), &binding);
|
||||
zassert_equal(null_len, apdu_len, NULL);
|
||||
/* shrink the apdu size for decoding test */
|
||||
while (apdu_len) {
|
||||
apdu_len--;
|
||||
test_len = bacnet_address_binding_decode(apdu, apdu_len, &test_binding);
|
||||
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||
}
|
||||
/* negative tests - NULL value */
|
||||
apdu_len = bacnet_address_binding_encode(apdu, sizeof(apdu), &binding);
|
||||
zassert_equal(null_len, apdu_len, NULL);
|
||||
test_len = bacnet_address_binding_decode(apdu, sizeof(apdu), NULL);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
test_len = bacnet_address_binding_decode(NULL, 0, NULL);
|
||||
zassert_equal(test_len, BACNET_STATUS_ERROR, NULL);
|
||||
status = bacnet_address_binding_copy(&test_binding, &binding);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_binding_same(&test_binding, &binding);
|
||||
zassert_true(status, NULL);
|
||||
/* ASCII */
|
||||
len = bacnet_address_binding_to_ascii(&binding, str, sizeof(str));
|
||||
zassert_true(len > 0, NULL);
|
||||
status = bacnet_address_binding_from_ascii(&test_binding, str);
|
||||
zassert_true(status, NULL);
|
||||
status = bacnet_address_binding_same(&test_binding, &binding);
|
||||
zassert_true(status, NULL);
|
||||
/* negative tests - NULL value */
|
||||
status = bacnet_address_binding_same(NULL, &binding);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_binding_same(&test_binding, NULL);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_binding_copy(NULL, &binding);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_binding_copy(&test_binding, NULL);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_address_binding_init(NULL, 0, &addr);
|
||||
zassert_false(status, NULL);
|
||||
len = bacnet_address_binding_to_ascii(NULL, str, sizeof(str));
|
||||
zassert_equal(len, 0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -314,8 +522,9 @@ void test_main(void)
|
||||
bacnet_address_tests, ztest_unit_test(test_BACNET_ADDRESS),
|
||||
ztest_unit_test(test_BACNET_MAC_ADDRESS),
|
||||
ztest_unit_test(test_BACnetAddress_Codec),
|
||||
ztest_unit_test(test_bacnet_vmac_entry_codec));
|
||||
|
||||
ztest_unit_test(test_bacnet_vmac_entry_codec),
|
||||
ztest_unit_test(test_bacnet_address_ascii),
|
||||
ztest_unit_test(test_BACNET_ADDRESS_BINDING));
|
||||
ztest_run_test_suite(bacnet_address_tests);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1334,7 +1334,8 @@ void test_bacapp_snprintf(
|
||||
/* normal case */
|
||||
len = bacapp_snprintf_value(str, str_len + 1, &object_value);
|
||||
zassert_mem_equal(
|
||||
str, expected, str_len, "str='%s' expected='%s'", str, expected);
|
||||
str, expected, str_len, "%s: str='%s' expected='%s'",
|
||||
bactext_application_tag_name(tag_number), str, expected);
|
||||
zassert_equal(len, str_len, NULL);
|
||||
/* test when buffer is too small the behavior matches snprintf(). */
|
||||
test_len = len;
|
||||
@@ -1380,14 +1381,17 @@ static void test_bacapp_sprintf_epics(void)
|
||||
BACNET_APPLICATION_TAG_DOUBLE, "-3.14159", "-3.141590");
|
||||
test_bacapp_snprintf(
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "1234567890ABCDEF",
|
||||
"1234567890ABCDEF");
|
||||
"X'1234567890ABCDEF'");
|
||||
test_bacapp_snprintf(
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "12-34-56-78-90-AB-CD-EF",
|
||||
"1234567890ABCDEF");
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "X'1234567890ABCDEF'",
|
||||
"X'1234567890ABCDEF'");
|
||||
test_bacapp_snprintf(
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "12 34 56 78 90 AB CD EF",
|
||||
"1234567890ABCDEF");
|
||||
test_bacapp_snprintf(BACNET_APPLICATION_TAG_OCTET_STRING, "", "");
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "X'12-34-56-78-90-AB-CD-EF'",
|
||||
"X'1234567890ABCDEF'");
|
||||
test_bacapp_snprintf(
|
||||
BACNET_APPLICATION_TAG_OCTET_STRING, "X'12 34 56 78 90 AB CD EF'",
|
||||
"X'1234567890ABCDEF'");
|
||||
test_bacapp_snprintf(BACNET_APPLICATION_TAG_OCTET_STRING, "", "X''");
|
||||
test_bacapp_snprintf(
|
||||
BACNET_APPLICATION_TAG_CHARACTER_STRING, "Karg!", "\"Karg!\"");
|
||||
test_bacapp_snprintf(BACNET_APPLICATION_TAG_CHARACTER_STRING, "", "\"\"");
|
||||
|
||||
+117
-46
@@ -11,6 +11,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <zephyr/ztest.h>
|
||||
#include <bacnet/bacaddr.h>
|
||||
#include <bacnet/bacdest.h>
|
||||
|
||||
/**
|
||||
@@ -21,49 +22,6 @@
|
||||
/**
|
||||
* @brief Test
|
||||
*/
|
||||
static void
|
||||
testBACnetRecipientData(BACNET_RECIPIENT *data1, BACNET_RECIPIENT *data2)
|
||||
{
|
||||
unsigned i = 0;
|
||||
|
||||
if (data1 && data2) {
|
||||
zassert_equal(data1->tag, data2->tag, NULL);
|
||||
if (data1->tag == BACNET_RECIPIENT_TAG_DEVICE) {
|
||||
zassert_equal(
|
||||
data1->type.device.type, data2->type.device.type, NULL);
|
||||
zassert_equal(
|
||||
data1->type.device.instance, data2->type.device.instance, NULL);
|
||||
} else if (data1->tag == BACNET_RECIPIENT_TAG_ADDRESS) {
|
||||
zassert_equal(
|
||||
data1->type.address.net, data2->type.address.net, NULL);
|
||||
if (data1->type.address.net == BACNET_BROADCAST_NETWORK) {
|
||||
zassert_equal(
|
||||
data1->type.address.mac_len, data2->type.address.mac_len,
|
||||
NULL);
|
||||
} else if (data1->type.address.net) {
|
||||
zassert_equal(
|
||||
data1->type.address.len, data2->type.address.len, NULL);
|
||||
for (i = 0; i < data1->type.address.len; i++) {
|
||||
zassert_equal(
|
||||
data1->type.address.adr[i], data2->type.address.adr[i],
|
||||
NULL);
|
||||
}
|
||||
} else {
|
||||
zassert_equal(
|
||||
data1->type.address.mac_len, data2->type.address.mac_len,
|
||||
NULL);
|
||||
for (i = 0; i < data1->type.address.mac_len; i++) {
|
||||
zassert_equal(
|
||||
data1->type.address.mac[i], data2->type.address.mac[i],
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
zassert_true(data1->tag < BACNET_RECIPIENT_TAG_MAX, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacnet_destination_tests, testBACnetDestination)
|
||||
#else
|
||||
@@ -73,6 +31,7 @@ static void testBACnetDestination(void)
|
||||
uint8_t apdu[MAX_APDU] = { 0 };
|
||||
BACNET_DESTINATION destination = { 0 }, test_destination = { 0 };
|
||||
int apdu_len = 0, null_len = 0, test_len = 0;
|
||||
bool status = false;
|
||||
|
||||
destination.Recipient.tag = BACNET_RECIPIENT_TAG_DEVICE;
|
||||
destination.Recipient.type.device.type = OBJECT_DEVICE;
|
||||
@@ -82,8 +41,8 @@ static void testBACnetDestination(void)
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len = bacnet_destination_decode(apdu, apdu_len, &test_destination);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
testBACnetRecipientData(
|
||||
&destination.Recipient, &test_destination.Recipient);
|
||||
status = bacnet_destination_same(&destination, &test_destination);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
destination.Recipient.tag = BACNET_RECIPIENT_TAG_ADDRESS;
|
||||
destination.Recipient.type.address.net = 1234;
|
||||
@@ -99,6 +58,8 @@ static void testBACnetDestination(void)
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len = bacnet_destination_decode(apdu, apdu_len, &test_destination);
|
||||
zassert_equal(test_len, apdu_len, NULL);
|
||||
status = bacnet_destination_same(&destination, &test_destination);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
null_len = bacnet_destination_encode(NULL, &destination);
|
||||
apdu_len = bacnet_destination_encode(apdu, &destination);
|
||||
@@ -107,12 +68,27 @@ static void testBACnetDestination(void)
|
||||
zassert_equal(test_len, apdu_len, NULL);
|
||||
test_len = bacnet_destination_decode(apdu, apdu_len, NULL);
|
||||
zassert_equal(test_len, apdu_len, NULL);
|
||||
status = bacnet_destination_same(&destination, &test_destination);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_destination_copy(&test_destination, &destination);
|
||||
status = bacnet_destination_same(&destination, &test_destination);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
/* decoding, some negative tests */
|
||||
test_len = bacnet_destination_decode(NULL, apdu_len, &test_destination);
|
||||
zassert_equal(test_len, BACNET_STATUS_REJECT, NULL);
|
||||
test_len = bacnet_destination_decode(apdu, 0, &test_destination);
|
||||
zassert_equal(test_len, BACNET_STATUS_REJECT, NULL);
|
||||
/* context */
|
||||
null_len = bacnet_destination_context_encode(NULL, 4, &destination);
|
||||
apdu_len = bacnet_destination_context_encode(apdu, 4, &destination);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len =
|
||||
bacnet_destination_context_decode(apdu, apdu_len, 4, &test_destination);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
/* defaults */
|
||||
status = bacnet_destination_default(&destination);
|
||||
zassert_false(status, NULL);
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
@@ -163,6 +139,99 @@ static void test_BACnetDestination_ASCII(void)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacnet_destination_tests, testBACnetRecipient)
|
||||
#else
|
||||
static void testBACnetRecipient(void)
|
||||
#endif
|
||||
{
|
||||
uint8_t apdu[MAX_APDU] = { 0 };
|
||||
BACNET_RECIPIENT value = { 0 }, test_value = { 0 };
|
||||
BACNET_MAC_ADDRESS mac = { .len = 1, .adr[0] = 0x01 };
|
||||
BACNET_MAC_ADDRESS adr = { .len = 1, .adr[0] = 0x02 };
|
||||
uint16_t snet = 1234;
|
||||
BACNET_ADDRESS address = { 0 };
|
||||
int apdu_len = 0, null_len = 0, test_len = 0;
|
||||
uint8_t tag_number = 4;
|
||||
bool status = false;
|
||||
|
||||
/* device */
|
||||
bacnet_recipient_device_set(&value, OBJECT_DEVICE, 123);
|
||||
status = bacnet_recipient_device_valid(&value);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_recipient_copy(&test_value, &value);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
null_len = bacnet_recipient_encode(NULL, &value);
|
||||
apdu_len = bacnet_recipient_encode(apdu, &value);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len = bacnet_recipient_decode(apdu, apdu_len, &test_value);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
/* address */
|
||||
status = bacnet_address_init(&address, &mac, snet, &adr);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_recipient_address_set(&value, &address);
|
||||
bacnet_recipient_copy(&test_value, &value);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
null_len = bacnet_recipient_encode(NULL, &value);
|
||||
apdu_len = bacnet_recipient_encode(apdu, &value);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len = bacnet_recipient_decode(apdu, apdu_len, &test_value);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
/* context */
|
||||
null_len = bacnet_recipient_context_encode(NULL, tag_number, &value);
|
||||
apdu_len = bacnet_recipient_context_encode(apdu, tag_number, &value);
|
||||
zassert_equal(apdu_len, null_len, NULL);
|
||||
test_len = bacnet_recipient_context_decode(
|
||||
apdu, apdu_len, tag_number, &test_value);
|
||||
zassert_equal(apdu_len, test_len, NULL);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
bacnet_recipient_device_wildcard_set(&value);
|
||||
status = bacnet_recipient_device_wildcard(&value);
|
||||
zassert_true(status, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacnet_destination_tests, test_BACnetRecipient_ASCII)
|
||||
#else
|
||||
static void test_BACnetRecipient_ASCII(void)
|
||||
#endif
|
||||
{
|
||||
bool status = false;
|
||||
BACNET_RECIPIENT value = { 0 }, test_value = { 0 };
|
||||
BACNET_ADDRESS address = { 0 };
|
||||
BACNET_MAC_ADDRESS mac = { .len = 1, .adr[0] = 0x01 };
|
||||
BACNET_MAC_ADDRESS adr = { .len = 1, .adr[0] = 0x02 };
|
||||
uint16_t snet = 1234;
|
||||
int len = 0;
|
||||
char ascii[80] = "";
|
||||
|
||||
bacnet_recipient_device_set(&value, OBJECT_DEVICE, 4194303);
|
||||
len = bacnet_recipient_to_ascii(&value, ascii, sizeof(ascii));
|
||||
zassert_true(len > 0, NULL);
|
||||
status = bacnet_recipient_from_ascii(&test_value, ascii);
|
||||
zassert_true(status, "ascii=%s", ascii);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
|
||||
status = bacnet_address_init(&address, &mac, snet, &adr);
|
||||
zassert_true(status, NULL);
|
||||
bacnet_recipient_address_set(&value, &address);
|
||||
len = bacnet_recipient_to_ascii(&value, ascii, sizeof(ascii));
|
||||
zassert_true(len > 0, NULL);
|
||||
status = bacnet_recipient_from_ascii(&test_value, ascii);
|
||||
zassert_true(status, "ascii=%s", ascii);
|
||||
status = bacnet_recipient_same(&value, &test_value);
|
||||
zassert_true(status, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST_SUITE(bacnet_destination_tests, NULL, NULL, NULL, NULL, NULL);
|
||||
#else
|
||||
@@ -170,7 +239,9 @@ void test_main(void)
|
||||
{
|
||||
ztest_test_suite(
|
||||
bacnet_destination_tests, ztest_unit_test(testBACnetDestination),
|
||||
ztest_unit_test(test_BACnetDestination_ASCII));
|
||||
ztest_unit_test(test_BACnetDestination_ASCII),
|
||||
ztest_unit_test(testBACnetRecipient),
|
||||
ztest_unit_test(test_BACnetRecipient_ASCII));
|
||||
|
||||
ztest_run_test_suite(bacnet_destination_tests);
|
||||
}
|
||||
|
||||
@@ -5,9 +5,24 @@
|
||||
* @date 2004
|
||||
* @copyright SPDX-License-Identifier: MIT
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <zephyr/ztest.h>
|
||||
#include <bacnet/bacstr.h>
|
||||
|
||||
/**
|
||||
* @brief compare two double precision floating points to 3 decimal places
|
||||
* @param x1 - first comparison value
|
||||
* @param x2 - second comparison value
|
||||
* @return true if the value is the same to 3 decimal points
|
||||
*/
|
||||
static bool is_float_equal(double x1, double x2)
|
||||
{
|
||||
return fabs(x1 - x2) < 0.001;
|
||||
}
|
||||
|
||||
/**
|
||||
* @addtogroup bacnet_tests
|
||||
* @{
|
||||
@@ -120,6 +135,8 @@ static void testCharacterString(void)
|
||||
{
|
||||
BACNET_CHARACTER_STRING bacnet_string = { 0 }, bacnet_string2 = { 0 };
|
||||
const char *value = "Joshua,Mary,Anna,Christopher";
|
||||
const char *utf8_value = "Joshua😍Mary😍Anna😍Christopher";
|
||||
const char *result = NULL;
|
||||
char test_value[MAX_APDU] = "Patricia";
|
||||
char test_append_value[MAX_APDU] = " and the Kids";
|
||||
char test_append_string[MAX_APDU] = "";
|
||||
@@ -129,12 +146,19 @@ static void testCharacterString(void)
|
||||
size_t test_length = 0;
|
||||
size_t i = 0;
|
||||
|
||||
/* verify initialization */
|
||||
/* verify UTF8 initialization */
|
||||
status = characterstring_init(&bacnet_string, CHARACTER_UTF8, NULL, 0);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(characterstring_length(&bacnet_string), 0, NULL);
|
||||
zassert_equal(
|
||||
characterstring_encoding(&bacnet_string), CHARACTER_UTF8, NULL);
|
||||
/* verify ANSI initialization */
|
||||
status = characterstring_init(&bacnet_string, CHARACTER_ANSI_X34, NULL, 0);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(characterstring_length(&bacnet_string), 0, NULL);
|
||||
zassert_equal(
|
||||
characterstring_encoding(&bacnet_string), CHARACTER_ANSI_X34, NULL);
|
||||
|
||||
/* empty string is the same as NULL */
|
||||
status = characterstring_same(&bacnet_string, NULL);
|
||||
zassert_true(status, NULL);
|
||||
@@ -156,11 +180,11 @@ static void testCharacterString(void)
|
||||
status = characterstring_init(
|
||||
&bacnet_string, CHARACTER_ANSI_X34, &test_value[0], test_length);
|
||||
zassert_true(status, NULL);
|
||||
value = characterstring_value(&bacnet_string);
|
||||
result = characterstring_value(&bacnet_string);
|
||||
length = characterstring_length(&bacnet_string);
|
||||
zassert_equal(length, test_length, NULL);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
zassert_equal(value[i], test_value[i], NULL);
|
||||
zassert_equal(result[i], test_value[i], NULL);
|
||||
}
|
||||
test_length = characterstring_copy_value(
|
||||
test_string, sizeof(test_string), &bacnet_string);
|
||||
@@ -174,10 +198,10 @@ static void testCharacterString(void)
|
||||
test_length = strlen(test_append_string);
|
||||
zassert_true(status, NULL);
|
||||
length = characterstring_length(&bacnet_string);
|
||||
value = characterstring_value(&bacnet_string);
|
||||
result = characterstring_value(&bacnet_string);
|
||||
zassert_equal(length, test_length, NULL);
|
||||
for (i = 0; i < test_length; i++) {
|
||||
zassert_equal(value[i], test_append_string[i], NULL);
|
||||
zassert_equal(result[i], test_append_string[i], NULL);
|
||||
}
|
||||
/* init from valid ASCII string */
|
||||
status = characterstring_init_ansi(&bacnet_string, value);
|
||||
@@ -223,6 +247,22 @@ static void testCharacterString(void)
|
||||
status = characterstring_ansi_copy(
|
||||
test_append_string, sizeof(test_append_string), &bacnet_string);
|
||||
zassert_equal(strncmp(value, test_append_string, strlen(value)), 0, NULL);
|
||||
/* UTF-8 specific */
|
||||
status = characterstring_init_ansi(&bacnet_string, value);
|
||||
zassert_true(status, NULL);
|
||||
length = characterstring_length(&bacnet_string);
|
||||
status = characterstring_init_ansi(&bacnet_string, utf8_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(
|
||||
characterstring_encoding(&bacnet_string), CHARACTER_UTF8, NULL);
|
||||
status = characterstring_valid(&bacnet_string);
|
||||
zassert_true(status, NULL);
|
||||
test_length = characterstring_utf8_length(&bacnet_string);
|
||||
zassert_equal(
|
||||
length, test_length, "value=\"%s\" length=%d, test_length=%d", value,
|
||||
length, test_length);
|
||||
status = characterstring_printable(&bacnet_string);
|
||||
zassert_false(status, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -394,6 +434,17 @@ static void test_bacnet_stricmp(void)
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_stricmp(test_name_a, NULL);
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
/* case sensitive */
|
||||
rv = bacnet_strcmp(name_a, name_a);
|
||||
zassert_equal(rv, 0, NULL);
|
||||
rv = bacnet_strcmp(name_a, test_name_a);
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strcmp(test_name_a, test_name_b);
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strcmp(NULL, test_name_b);
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strcmp(test_name_a, NULL);
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -409,6 +460,18 @@ static void test_bacnet_strnicmp(void)
|
||||
const char *name_a = "Patricia", *test_name_a = "patricia";
|
||||
const char *name_b = "CamelCase", *test_name_b = "CAMELCASE";
|
||||
|
||||
/* case sensitive */
|
||||
rv = bacnet_strncmp(name_a, name_a, strlen(name_a));
|
||||
zassert_equal(rv, 0, NULL);
|
||||
rv = bacnet_strncmp(name_a, test_name_a, strlen(name_a));
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strncmp(test_name_a, test_name_b, strlen(test_name_a));
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strncmp(NULL, test_name_b, strlen(test_name_b));
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
rv = bacnet_strncmp(test_name_a, NULL, strlen(test_name_a));
|
||||
zassert_not_equal(rv, 0, NULL);
|
||||
/* case insensitive */
|
||||
rv = bacnet_strnicmp(name_a, test_name_a, strlen(name_a));
|
||||
zassert_equal(rv, 0, NULL);
|
||||
rv = bacnet_strnicmp(name_b, test_name_b, strlen(name_b));
|
||||
@@ -451,6 +514,299 @@ static void test_bacnet_strnlen(void)
|
||||
test_len = bacnet_strnlen(test_name, 512);
|
||||
zassert_equal(len, test_len, "len=%u test_len=%d", len, test_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API for bacnet_strto. functions
|
||||
*/
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacstr_tests, test_bacnet_strto)
|
||||
#else
|
||||
static void test_bacnet_strto(void)
|
||||
#endif
|
||||
{
|
||||
bool status;
|
||||
const char *empty_string = "";
|
||||
const char *extra_text_string = "123yyx";
|
||||
const char *test_unsigned_long_string = "1234567890";
|
||||
unsigned long unsigned_long_value, test_unsigned_long_value = 1234567890;
|
||||
const char *test_long_string = "-1234567890";
|
||||
long long_value, test_long_value = -1234567890;
|
||||
const char *test_float_positive_string = "1.23";
|
||||
float float_value, test_float_value = 1.23f;
|
||||
double double_value, test_double_value = 1.23;
|
||||
long double long_double_value, test_long_double_value = 1.23L;
|
||||
const char *test_float_negative_string = "-1.23";
|
||||
float float_negative_value, test_float_negative_value = -1.23f;
|
||||
double double_negative_value, test_double_negative_value = -1.23;
|
||||
long double long_double_negative_value,
|
||||
test_long_double_negative_value = -1.23L;
|
||||
char buffer[80] = "", *ascii_result = NULL;
|
||||
|
||||
/* unsigned long */
|
||||
status = bacnet_strtoul(test_unsigned_long_string, &unsigned_long_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(unsigned_long_value, test_unsigned_long_value, NULL);
|
||||
status = bacnet_strtoul(empty_string, &unsigned_long_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_strtoul(extra_text_string, &unsigned_long_value);
|
||||
zassert_false(status, NULL);
|
||||
ascii_result = bacnet_ultoa(unsigned_long_value, buffer, sizeof(buffer));
|
||||
zassert_equal(bacnet_strcmp(buffer, test_unsigned_long_string), 0, NULL);
|
||||
zassert_equal(ascii_result, buffer, NULL);
|
||||
ascii_result = bacnet_utoa(unsigned_long_value, buffer, sizeof(buffer));
|
||||
zassert_equal(bacnet_strcmp(buffer, test_unsigned_long_string), 0, NULL);
|
||||
zassert_equal(ascii_result, buffer, NULL);
|
||||
/* long */
|
||||
status = bacnet_strtol(test_long_string, &long_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(long_value, test_long_value, NULL);
|
||||
status = bacnet_strtol(empty_string, &long_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_strtol(extra_text_string, &long_value);
|
||||
zassert_false(status, NULL);
|
||||
ascii_result = bacnet_ltoa(long_value, buffer, sizeof(buffer));
|
||||
zassert_equal(bacnet_strcmp(buffer, test_long_string), 0, NULL);
|
||||
zassert_equal(ascii_result, buffer, NULL);
|
||||
ascii_result = bacnet_itoa(long_value, buffer, sizeof(buffer));
|
||||
zassert_equal(bacnet_strcmp(buffer, test_long_string), 0, NULL);
|
||||
zassert_equal(ascii_result, buffer, NULL);
|
||||
/* single precision */
|
||||
status = bacnet_strtof(test_float_positive_string, &float_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(is_float_equal(float_value, test_float_value), NULL);
|
||||
status = bacnet_strtof(test_float_negative_string, &float_negative_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(
|
||||
is_float_equal(float_negative_value, test_float_negative_value), NULL);
|
||||
status = bacnet_strtof(empty_string, &float_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_strtof(extra_text_string, &float_value);
|
||||
zassert_false(status, NULL);
|
||||
/* double precision */
|
||||
status = bacnet_strtod(test_float_positive_string, &double_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(is_float_equal(double_value, test_double_value), NULL);
|
||||
status = bacnet_strtod(test_float_negative_string, &double_negative_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(
|
||||
is_float_equal(double_negative_value, test_double_negative_value),
|
||||
NULL);
|
||||
status = bacnet_strtod(empty_string, &double_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_strtod(extra_text_string, &double_value);
|
||||
zassert_false(status, NULL);
|
||||
ascii_result =
|
||||
bacnet_dtoa(double_negative_value, buffer, sizeof(buffer), 2);
|
||||
zassert_equal(bacnet_strcmp(buffer, test_float_negative_string), 0, NULL);
|
||||
zassert_equal(ascii_result, buffer, NULL);
|
||||
/* long double precision */
|
||||
status = bacnet_strtold(test_float_positive_string, &long_double_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(
|
||||
is_float_equal(long_double_value, test_long_double_value), NULL);
|
||||
status =
|
||||
bacnet_strtold(test_float_negative_string, &long_double_negative_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_true(
|
||||
is_float_equal(
|
||||
long_double_negative_value, test_long_double_negative_value),
|
||||
NULL);
|
||||
status = bacnet_strtold(empty_string, &long_double_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_strtold(extra_text_string, &long_double_value);
|
||||
zassert_false(status, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API for bacnet_string_to_x functions
|
||||
*/
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacstr_tests, test_bacnet_string_to_x)
|
||||
#else
|
||||
static void test_bacnet_string_to_x(void)
|
||||
#endif
|
||||
{
|
||||
bool status;
|
||||
const char *empty_string = "";
|
||||
const char *extra_text_string = "123yyx";
|
||||
const char *test_uint8_t_string = "123";
|
||||
const char *test_uint16_t_string = "12345";
|
||||
const char *test_uint32_t_string = "1234567890";
|
||||
const char *test_int32_t_string = "-1234567890";
|
||||
const char *test_true_string = "true";
|
||||
const char *test_false_string = "false";
|
||||
const char *test_active_string = "active";
|
||||
const char *test_inactive_string = "inactive";
|
||||
const char *test_true_numeric_string = "1";
|
||||
const char *test_false_numeric_string = "0";
|
||||
const char *test_unsigned_string = "1234567890";
|
||||
const char *test_ascii_string = "abcdefghijklmnopqrstuvwxyz";
|
||||
uint8_t uint8_t_value, test_uint8_t_value = 123;
|
||||
uint16_t uint16_t_value, test_uint16_t_value = 12345;
|
||||
uint32_t uint32_t_value, test_uint32_t_value = 1234567890;
|
||||
int32_t int32_t_value, test_int32_t_value = -1234567890;
|
||||
BACNET_UNSIGNED_INTEGER bacnet_unsigned_integer,
|
||||
test_bacnet_unsigned_integer = 1234567890;
|
||||
bool bool_value, test_true_value = true, test_false_value = false;
|
||||
char ascii_string[80] = "", *ascii_string_result = NULL;
|
||||
|
||||
/* uint8_t */
|
||||
status = bacnet_string_to_uint8(test_uint8_t_string, &uint8_t_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(uint8_t_value, test_uint8_t_value, NULL);
|
||||
status = bacnet_string_to_uint8(empty_string, &uint8_t_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_string_to_uint8(extra_text_string, &uint8_t_value);
|
||||
zassert_false(status, NULL);
|
||||
/* uint16_t */
|
||||
status = bacnet_string_to_uint16(test_uint16_t_string, &uint16_t_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(uint16_t_value, test_uint16_t_value, NULL);
|
||||
status = bacnet_string_to_uint16(empty_string, &uint16_t_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_string_to_uint16(extra_text_string, &uint16_t_value);
|
||||
zassert_false(status, NULL);
|
||||
/* uint32_t */
|
||||
status = bacnet_string_to_uint32(test_uint32_t_string, &uint32_t_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(uint32_t_value, test_uint32_t_value, NULL);
|
||||
status = bacnet_string_to_uint32(empty_string, &uint32_t_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_string_to_uint32(extra_text_string, &uint32_t_value);
|
||||
zassert_false(status, NULL);
|
||||
/* int32_t */
|
||||
status = bacnet_string_to_int32(test_int32_t_string, &int32_t_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(int32_t_value, test_int32_t_value, NULL);
|
||||
status = bacnet_string_to_int32(empty_string, &int32_t_value);
|
||||
zassert_false(status, NULL);
|
||||
/* bool */
|
||||
status = bacnet_string_to_bool(test_true_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_true_value, NULL);
|
||||
status = bacnet_string_to_bool(test_false_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_false_value, NULL);
|
||||
status = bacnet_string_to_bool(empty_string, &bool_value);
|
||||
zassert_false(status, NULL);
|
||||
status = bacnet_string_to_bool(extra_text_string, &bool_value);
|
||||
zassert_false(status, NULL);
|
||||
/* active/inactive */
|
||||
status = bacnet_string_to_bool(test_active_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_true_value, NULL);
|
||||
status = bacnet_string_to_bool(test_inactive_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_false_value, NULL);
|
||||
status = bacnet_string_to_bool(empty_string, &bool_value);
|
||||
zassert_false(status, NULL);
|
||||
/* 0/1 */
|
||||
status = bacnet_string_to_bool(test_true_numeric_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_true_value, NULL);
|
||||
status = bacnet_string_to_bool(test_false_numeric_string, &bool_value);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bool_value, test_false_value, NULL);
|
||||
/* bacnet_unsigned_integer */
|
||||
status = bacnet_string_to_unsigned(
|
||||
test_unsigned_string, &bacnet_unsigned_integer);
|
||||
zassert_true(status, NULL);
|
||||
zassert_equal(bacnet_unsigned_integer, test_bacnet_unsigned_integer, NULL);
|
||||
status = bacnet_string_to_unsigned(empty_string, &bacnet_unsigned_integer);
|
||||
zassert_false(status, NULL);
|
||||
status =
|
||||
bacnet_string_to_unsigned(extra_text_string, &bacnet_unsigned_integer);
|
||||
zassert_false(status, NULL);
|
||||
/* ascii string */
|
||||
ascii_string_result = bacnet_snprintf_to_ascii(
|
||||
ascii_string, sizeof(ascii_string), "%s", test_ascii_string);
|
||||
zassert_equal(
|
||||
bacnet_strcmp(ascii_string_result, test_ascii_string), 0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API for bacnet trim functions
|
||||
*/
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacstr_tests, test_bacnet_string_trim)
|
||||
#else
|
||||
static void test_bacnet_string_trim(void)
|
||||
#endif
|
||||
{
|
||||
char trim_left[80] = " abcdefg", *trim_left_result = NULL;
|
||||
char trim_right[80] = "abcdefg ", *trim_right_result = NULL;
|
||||
char trim_both[80] = " abcdefg ", *trim_both_result = NULL;
|
||||
char *trim_test_value = "abcdefg";
|
||||
char *empty_string = "";
|
||||
|
||||
trim_left_result = bacnet_ltrim(trim_left, " ");
|
||||
trim_right_result = bacnet_rtrim(trim_right, " ");
|
||||
trim_both_result = bacnet_trim(trim_both, " ");
|
||||
zassert_equal(bacnet_strcmp(trim_left_result, trim_test_value), 0, NULL);
|
||||
zassert_equal(bacnet_strcmp(trim_right_result, trim_test_value), 0, NULL);
|
||||
zassert_equal(bacnet_strcmp(trim_both_result, trim_test_value), 0, NULL);
|
||||
trim_left_result = bacnet_ltrim(empty_string, " ");
|
||||
trim_right_result = bacnet_rtrim(empty_string, " ");
|
||||
trim_both_result = bacnet_trim(empty_string, " ");
|
||||
zassert_equal(bacnet_strcmp(trim_left_result, empty_string), 0, NULL);
|
||||
zassert_equal(bacnet_strcmp(trim_right_result, empty_string), 0, NULL);
|
||||
zassert_equal(bacnet_strcmp(trim_both_result, empty_string), 0, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test bacnet_stptok string tokenizer
|
||||
*/
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacstr_tests, test_bacnet_stptok)
|
||||
#else
|
||||
static void test_bacnet_stptok(void)
|
||||
#endif
|
||||
{
|
||||
char *pCmd = "I Love You\r\n";
|
||||
char token[80] = "";
|
||||
|
||||
pCmd = bacnet_stptok(pCmd, token, sizeof(token), " \r\n");
|
||||
|
||||
zassert_true(bacnet_strcmp(token, "I") == 0, NULL);
|
||||
zassert_true(bacnet_strcmp(pCmd, "Love You\r\n") == 0, NULL);
|
||||
|
||||
pCmd = bacnet_stptok(pCmd, token, sizeof(token), " \r\n");
|
||||
|
||||
zassert_true(bacnet_strcmp(token, "Love") == 0, NULL);
|
||||
zassert_true(bacnet_strcmp(pCmd, "You\r\n") == 0, NULL);
|
||||
|
||||
pCmd = bacnet_stptok(pCmd, token, sizeof(token), " \r\n");
|
||||
|
||||
zassert_true(bacnet_strcmp(token, "You") == 0, NULL);
|
||||
zassert_true(pCmd == NULL, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test encode/decode API for bacnet snprintf and shift functions
|
||||
*/
|
||||
#if defined(CONFIG_ZTEST_NEW_API)
|
||||
ZTEST(bacstr_tests, test_bacnet_snprintf)
|
||||
#else
|
||||
static void test_bacnet_snprintf(void)
|
||||
#endif
|
||||
{
|
||||
int buf_len = 0, str_len;
|
||||
int i;
|
||||
char str[30] = "";
|
||||
|
||||
str_len = sizeof(str);
|
||||
for (i = 0; i < 5; i++) {
|
||||
/* appending formatted strings */
|
||||
buf_len = bacnet_snprintf(str, str_len, buf_len, "{");
|
||||
buf_len =
|
||||
bacnet_snprintf(str, str_len, buf_len, "REALLY BIG STRING BASS");
|
||||
buf_len = bacnet_snprintf(str, str_len, buf_len, "}");
|
||||
}
|
||||
zassert_equal(buf_len, str_len, "buf_len=%d str_len=%d", buf_len, str_len);
|
||||
zassert_equal(
|
||||
str[buf_len - 1], 0, "str[%d]=%c", buf_len - 1, str[buf_len - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -465,8 +821,12 @@ void test_main(void)
|
||||
ztest_unit_test(testCharacterString), ztest_unit_test(testOctetString),
|
||||
ztest_unit_test(test_bacnet_stricmp),
|
||||
ztest_unit_test(test_bacnet_strnicmp),
|
||||
ztest_unit_test(test_bacnet_strnlen));
|
||||
|
||||
ztest_unit_test(test_bacnet_strnlen),
|
||||
ztest_unit_test(test_bacnet_strto),
|
||||
ztest_unit_test(test_bacnet_string_to_x),
|
||||
ztest_unit_test(test_bacnet_string_trim),
|
||||
ztest_unit_test(test_bacnet_stptok),
|
||||
ztest_unit_test(test_bacnet_snprintf));
|
||||
ztest_run_test_suite(bacstr_tests);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -34,10 +34,12 @@ add_executable(${PROJECT_NAME}
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/hostnport.c
|
||||
${SRC_DIR}/bacnet/iam.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/basic/sys/debug.c
|
||||
|
||||
@@ -35,9 +35,11 @@ add_executable(${PROJECT_NAME}
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/iam.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/basic/sys/debug.c
|
||||
|
||||
@@ -33,9 +33,11 @@ add_executable(${PROJECT_NAME}
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/iam.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/basic/sys/debug.c
|
||||
|
||||
@@ -40,8 +40,10 @@ add_executable(${PROJECT_NAME}
|
||||
${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/ringbuf.c
|
||||
${SRC_DIR}/bacnet/basic/sys/days.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
# Test and test library files
|
||||
./src/main.c
|
||||
|
||||
@@ -45,8 +45,10 @@ add_executable(${PROJECT_NAME}
|
||||
${SRC_DIR}/bacnet/bacaddr.c
|
||||
${SRC_DIR}/bacnet/bacdcode.c
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/npdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
# Support files and stubs (pathname alphabetical)
|
||||
|
||||
@@ -42,11 +42,13 @@ add_executable(${PROJECT_NAME}
|
||||
${SRC_DIR}/bacnet/bacint.c
|
||||
${SRC_DIR}/bacnet/bacreal.c
|
||||
${SRC_DIR}/bacnet/bacstr.c
|
||||
${SRC_DIR}/bacnet/bactext.c
|
||||
${SRC_DIR}/bacnet/basic/service/h_apdu.c
|
||||
${SRC_DIR}/bacnet/basic/sys/bigend.c
|
||||
${SRC_DIR}/bacnet/basic/sys/debug.c
|
||||
${SRC_DIR}/bacnet/basic/tsm/tsm.c
|
||||
${SRC_DIR}/bacnet/dcc.c
|
||||
${SRC_DIR}/bacnet/indtext.c
|
||||
${SRC_DIR}/bacnet/proplist.c
|
||||
${SRC_DIR}/bacnet/reject.c
|
||||
${SRC_DIR}/bacnet/rp.c
|
||||
|
||||
Reference in New Issue
Block a user