Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: port assert test to zephyr unit tests #650

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's some unit_tsting platform issue, not sure how to fix properly

# 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