unit/bacnet/bits test (#391)

Co-authored-by: Gregory Shue <gregory.shue@legrand.com>
This commit is contained in:
Greg Shue
2023-02-02 11:45:47 -08:00
committed by GitHub
parent eb36033fd8
commit 338d445b99
6 changed files with 258 additions and 0 deletions
+4
View File
@@ -32,6 +32,10 @@ add_custom_command(TARGET lcov
# add tests
#
list(APPEND testdirs
unit/bacnet/bits
)
list(APPEND testdirs
bacnet/abort
bacnet/alarm_ack
+41
View File
@@ -0,0 +1,41 @@
# 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/unit/bacnet/[a-zA-Z_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/unit/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
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
# NOTE: bits.h contains the complete implementation of the API.
# Support files and stubs (pathname alphabetical)
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
+183
View File
@@ -0,0 +1,183 @@
/*
* Copyright (c) 2023 Legrand North America, LLC.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/ztest.h>
#undef BIT
#undef _BV
#undef BIT_SET
#undef BIT_CLEAR
#undef BIT_FLIP
#undef BIT_CHECK
#undef BITMASK_SET
#undef BITMASK_CLEAR
#undef BITMASK_FLIP
#undef BITMASK_CHECK
#include "bacnet/bits.h"
/* NOTE: These tests must be compatible with C90, so they do not
* verify support for ULL.
*/
static void test_BIT(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
bitpos -= 1;
zassert_true(BIT(bitpos) == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test__BV(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
bitpos -= 1;
zassert_true(BIT(bitpos) == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test_BIT_SET(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = 0U;
bitpos -= 1;
BIT_SET(a, bitpos);
zassert_true(a == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test_BIT_CLEAR(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
BIT_CLEAR(a, bitpos);
zassert_true(~(a) == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test_BIT_FLIP(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
BIT_FLIP(a, bitpos);
zassert_true(a == ~(1U << bitpos), NULL);
BIT_FLIP(a, bitpos);
zassert_true(a == ~0U, NULL);
a = 0U;
BIT_FLIP(a, bitpos);
zassert_true(a == (1U << bitpos), NULL);
BIT_FLIP(a, bitpos);
zassert_true(a == 0U, NULL);
} while (bitpos > 0);
}
static void test_BIT_CHECK(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
zassert_true(BIT_CHECK(a, bitpos), NULL);
a = 0U;
zassert_false(BIT_CHECK(a, bitpos), NULL);
} while (bitpos > 0);
}
static void test_BITMASK_SET(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = 0U;
bitpos -= 1;
BITMASK_SET(a, (1U << bitpos));
zassert_true(a == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test_BITMASK_CLEAR(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
BITMASK_CLEAR(a, (1U << bitpos));
zassert_true(~(a) == (1U << bitpos), NULL);
} while (bitpos > 0);
}
static void test_BITMASK_FLIP(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
BITMASK_FLIP(a, (1U << bitpos));
zassert_true(a == ~(1U << bitpos), NULL);
BITMASK_FLIP(a, (1U << bitpos));
zassert_true(a == ~0U, NULL);
a = 0U;
BITMASK_FLIP(a, (1U << bitpos));
zassert_true(a == (1U << bitpos), NULL);
BITMASK_FLIP(a, (1U << bitpos));
zassert_true(a == 0U, NULL);
} while (bitpos > 0);
}
static void test_BITMASK_CHECK(void)
{
unsigned int bitpos = sizeof(unsigned int) * 8;
do {
unsigned int a = ~0U;
bitpos -= 1;
zassert_true(BITMASK_CHECK(a, (1U << bitpos)), NULL);
a = 0U;
zassert_false(BITMASK_CHECK(a, (1U << bitpos)), NULL);
} while (bitpos > 0);
}
void test_main(void)
{
ztest_test_suite(bacnet_bits,
ztest_unit_test(test_BIT),
ztest_unit_test(test__BV),
ztest_unit_test(test_BIT_SET),
ztest_unit_test(test_BIT_CLEAR),
ztest_unit_test(test_BIT_FLIP),
ztest_unit_test(test_BIT_CHECK),
ztest_unit_test(test_BITMASK_SET),
ztest_unit_test(test_BITMASK_CLEAR),
ztest_unit_test(test_BITMASK_FLIP),
ztest_unit_test(test_BITMASK_CHECK)
);
ztest_run_test_suite(bacnet_bits);
}
@@ -0,0 +1,26 @@
# Copyright (c) 2022 Legrand North America, LLC.
#
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
if(NOT ZEPHYR_CURRENT_MODULE_DIR)
string(REGEX REPLACE "/zephyr/tests/[a-zA-Z_/-]*$" ""
ZEPHYR_CURRENT_MODULE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()
set(SOURCES
${ZEPHYR_CURRENT_MODULE_DIR}/test/unit/bacnet/bits/src/main.c
)
project(bacnet_bits)
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
# NOTE for Zephyr >= v3.2.0:
# - Zephyr unittest builds for target 'testbinary' instead of 'app'.
# - Zephyr unittest does not generate ZEPHYR_<modulename>_MODULE_DIR.
# So we have to use relative paths to get to the source.
target_include_directories(testbinary PRIVATE
${ZEPHYR_CURRENT_MODULE_DIR}/src
)
+1
View File
@@ -0,0 +1 @@
# This file is intentionally empty
@@ -0,0 +1,3 @@
tests:
bacnet.bits.unit:
type: unit