From db49cd38994161de537a66a30eb855b81ffb30d6 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 21 Jun 2024 10:17:33 -0700 Subject: [PATCH] tests/kernel: Validate constructor execution order 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 --- tests/kernel/common/src/constructor.c | 34 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/kernel/common/src/constructor.c b/tests/kernel/common/src/constructor.c index 75f866df0f0..6aca7f7cbe3 100644 --- a/tests/kernel/common/src/constructor.c +++ b/tests/kernel/common/src/constructor.c @@ -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) +void __attribute__((__constructor__)) __constructor_init(void) { - constructor_value = CONSTRUCTOR_VALUE; + constructor_values[constructor_number++] = 31415; +} + +void __attribute__((__constructor__(101))) __constructor_init_priority_101(void) +{ + constructor_values[constructor_number++] = 101; +} + +void __attribute__((__constructor__(1000))) __constructor_init_priority_1000(void) +{ + 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, + "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);