Skip to content

Commit

Permalink
tests/kernel: Add test for constructors in C
Browse files Browse the repository at this point in the history
Make sure constructors are run even when not using C++.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed Jun 21, 2024
1 parent 2dd73b3 commit 3cd79fe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/kernel/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ target_sources(app PRIVATE
src/bitarray.c
src/byteorder.c
src/clock.c
src/constructor.c
src/main.c
src/timeout_order.c
src/multilib.c
Expand Down
43 changes: 43 additions & 0 deletions tests/kernel/common/src/constructor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Keith Packard
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <errno.h>
#include <zephyr/tc_util.h>
#include <zephyr/ztest.h>
/**
* @addtogroup kernel_common_tests
* @{
*/

/**
* @brief Test if constructors work
*
*/

static int constructor_value;

#define CONSTRUCTOR_VALUE 1

void
__attribute__((__constructor__))
__constructor_init(void)
{
constructor_value = CONSTRUCTOR_VALUE;
}

ZTEST(constructor, test_constructor)
{
zassert_equal(constructor_value, CONSTRUCTOR_VALUE,
"constructor test failed: constructor not called");
}

extern void *common_setup(void);
ZTEST_SUITE(constructor, NULL, common_setup, NULL, NULL, NULL);

/**
* @}
*/

0 comments on commit 3cd79fe

Please sign in to comment.