Skip to content

Commit

Permalink
tests/kernel: Validate constructor execution order
Browse files Browse the repository at this point in the history
Check the order of constructors, making sure those with a specified
priority are called in numeric order after which those without priority are
called.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed Aug 23, 2024
1 parent 71fbd8e commit 0ea6c37
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tests/kernel/common/src/constructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,37 @@
*
*/

static int constructor_value;
static int constructor_number;
static int constructor_values[3];

#define CONSTRUCTOR_VALUE 1
void __attribute__((__constructor__)) __constructor_init(void)
{
constructor_values[constructor_number++] = 31415;
}

void __attribute__((__constructor__(101))) __constructor_init_priority_101(void)
{
constructor_values[constructor_number++] = 101;
}

void
__attribute__((__constructor__))
__constructor_init(void)
void __attribute__((__constructor__(1000))) __constructor_init_priority_1000(void)
{
constructor_value = CONSTRUCTOR_VALUE;
constructor_values[constructor_number++] = 1000;
}

ZTEST(constructor, test_constructor)
{
zassert_equal(constructor_value, CONSTRUCTOR_VALUE,
"constructor test failed: constructor not called");
zassert_equal(constructor_number, 3,
"constructor test failed: constructor missing");
zassert_equal(constructor_values[0], 101,

Check notice on line 43 in tests/kernel/common/src/constructor.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/kernel/common/src/constructor.c:43 - zassert_equal(constructor_number, 3, - "constructor test failed: constructor missing"); + zassert_equal(constructor_number, 3, "constructor test failed: constructor missing");
"constructor priority test failed:"
"constructor 101 not called first");
zassert_equal(constructor_values[1], 1000,
"constructor priority test failed:"
"constructor 1000 not called second");
zassert_equal(constructor_values[2], 31415,
"constructor priority test failed:"
"constructor without priority not called last");
}

extern void *common_setup(void);
Expand Down

0 comments on commit 0ea6c37

Please sign in to comment.