Skip to content

Commit

Permalink
tests: port assert test to zephyr unit tests
Browse files Browse the repository at this point in the history
Assert tests to use zephyr unit testing framework.
It fixes false negative result when the posix board was used.

Signed-off-by: Krzysztof Taborowski <[email protected]>
  • Loading branch information
ktaborowski committed Dec 2, 2024
1 parent e04a8ef commit db4ef7b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
15 changes: 9 additions & 6 deletions tests/unit_tests/pal_assert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sidewalk_test_assert)

set(SIDEWALK_BASE $ENV{ZEPHYR_BASE}/../sidewalk)
# Workaround on build system not able to find this definition (in kernel.h)
add_definitions(-DARCH_STACK_PTR_ALIGN=8)
# add test file
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

# generate runner for the test
test_runner_generate(${app_sources})
target_include_directories(testbinary PRIVATE ${SIDEWALK_BASE}/subsys/sal/common/sid_pal_ifc)
target_sources(testbinary PRIVATE
${SIDEWALK_BASE}/subsys/sal/sid_pal/src/sid_assert.c
src/main.c
)
8 changes: 2 additions & 6 deletions tests/unit_tests/pal_assert/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
config SIDEWALK_BUILD
default y

config SIDEWALK_ASSERT
default y
imply ASSERT
config SIDEWALK_LOG_LEVEL
default 0

source "Kconfig.zephyr"
3 changes: 2 additions & 1 deletion tests/unit_tests/pal_assert/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
CONFIG_UNITY=y
CONFIG_ZTEST=y
CONFIG_ASSERT=y
40 changes: 20 additions & 20 deletions tests/unit_tests/pal_assert/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <unity.h>
#include <sid_pal_assert_ifc.h>
#include <stdbool.h>
#include <zephyr/toolchain.h>
#include <zephyr/ztest.h>
#include <zephyr/fff.h>

void setUp(void)
DEFINE_FFF_GLOBALS;

FAKE_VOID_FUNC_VARARG(z_log_minimal_printk, const char *, ...);

static void suite_setup(void *f)
{
RESET_FAKE(z_log_minimal_printk);
}

/******************************************************************
Expand All @@ -24,28 +29,23 @@ void assert_post_action(const char *file, unsigned int line)

if (should_assert) {
should_assert = false;
TEST_PASS_MESSAGE("Asserted.");
ztest_test_pass();
}
TEST_FAIL_MESSAGE("Asserted, but should not.");
ztest_test_fail();
}

void test_sid_pal_assert(void)
ZTEST(assert_tests, test_sid_pal_assert_true)
{
should_assert = false;
SID_PAL_ASSERT(true);

should_assert = true;
SID_PAL_ASSERT(false);
TEST_ASSERT_FALSE_MESSAGE(should_assert, "No assert when it should be.");
should_assert = false;
SID_PAL_ASSERT(true);
zassert_false(should_assert, "No assert when it should be.");
}

/* It is required to be added to each test. That is because unity is using
* different main signature (returns int) and zephyr expects main which does
* not return value.
*/
extern int unity_main(void);

int main(void)
ZTEST(assert_tests, test_sid_pal_assert_false)
{
return unity_main();
should_assert = true;
SID_PAL_ASSERT(false);
zassert_false(should_assert, "Asserted, but should not.");
}

ZTEST_SUITE(assert_tests, NULL, NULL, suite_setup, NULL, NULL);
6 changes: 2 additions & 4 deletions tests/unit_tests/pal_assert/testcase.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests:
sidewalk.unit_tests.assert:
sysbuild: true
platform_allow: native_posix
sysbuild: false
tags: Sidewalk
integration_platforms:
- native_posix
type: unit

0 comments on commit db4ef7b

Please sign in to comment.