Skip to content

Commit 08c5415

Browse files
committed
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 <[email protected]>
1 parent d574e07 commit 08c5415

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

tests/kernel/common/src/constructor.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,37 @@
1818
*
1919
*/
2020

21-
static int constructor_value;
21+
static int constructor_number;
22+
static int constructor_values[3];
2223

23-
#define CONSTRUCTOR_VALUE 1
24+
void __attribute__((__constructor__)) __constructor_init(void)
25+
{
26+
constructor_values[constructor_number++] = 31415;
27+
}
28+
29+
void __attribute__((__constructor__(101))) __constructor_init_priority_101(void)
30+
{
31+
constructor_values[constructor_number++] = 101;
32+
}
2433

25-
void
26-
__attribute__((__constructor__))
27-
__constructor_init(void)
34+
void __attribute__((__constructor__(1000))) __constructor_init_priority_1000(void)
2835
{
29-
constructor_value = CONSTRUCTOR_VALUE;
36+
constructor_values[constructor_number++] = 1000;
3037
}
3138

3239
ZTEST(constructor, test_constructor)
3340
{
34-
zassert_equal(constructor_value, CONSTRUCTOR_VALUE,
35-
"constructor test failed: constructor not called");
41+
zassert_equal(constructor_number, 3,
42+
"constructor test failed: constructor missing");
43+
zassert_equal(constructor_values[0], 101,
44+
"constructor priority test failed:"
45+
"constructor 101 not called first");
46+
zassert_equal(constructor_values[1], 1000,
47+
"constructor priority test failed:"
48+
"constructor 1000 not called second");
49+
zassert_equal(constructor_values[2], 31415,
50+
"constructor priority test failed:"
51+
"constructor without priority not called last");
3652
}
3753

3854
extern void *common_setup(void);

0 commit comments

Comments
 (0)