1 /*
2 * Copyright (c) 2019 Oticon A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/sys/util.h>
9 #include <stdio.h>
10 #include <string.h>
11
ZTEST(util,test_u8_to_dec)12 ZTEST(util, test_u8_to_dec) {
13 char text[4];
14 uint8_t len;
15
16 len = u8_to_dec(text, sizeof(text), 0);
17 zassert_equal(len, 1, "Length of 0 is not 1");
18 zassert_str_equal(text, "0", "Value=0 is not converted to \"0\"");
19
20 len = u8_to_dec(text, sizeof(text), 1);
21 zassert_equal(len, 1, "Length of 1 is not 1");
22 zassert_str_equal(text, "1", "Value=1 is not converted to \"1\"");
23
24 len = u8_to_dec(text, sizeof(text), 11);
25 zassert_equal(len, 2, "Length of 11 is not 2");
26 zassert_str_equal(text, "11", "Value=10 is not converted to \"11\"");
27
28 len = u8_to_dec(text, sizeof(text), 100);
29 zassert_equal(len, 3, "Length of 100 is not 3");
30 zassert_str_equal(text, "100",
31 "Value=100 is not converted to \"100\"");
32
33 len = u8_to_dec(text, sizeof(text), 101);
34 zassert_equal(len, 3, "Length of 101 is not 3");
35 zassert_str_equal(text, "101",
36 "Value=101 is not converted to \"101\"");
37
38 len = u8_to_dec(text, sizeof(text), 255);
39 zassert_equal(len, 3, "Length of 255 is not 3");
40 zassert_str_equal(text, "255",
41 "Value=255 is not converted to \"255\"");
42
43 memset(text, 0, sizeof(text));
44 len = u8_to_dec(text, 2, 123);
45 zassert_equal(len, 2,
46 "Length of converted value using 2 byte buffer isn't 2");
47 zassert_str_equal(text, "12",
48 "Value=123 is not converted to \"12\" using 2-byte buffer");
49
50 memset(text, 0, sizeof(text));
51 len = u8_to_dec(text, 1, 123);
52 zassert_equal(len, 1,
53 "Length of converted value using 1 byte buffer isn't 1");
54 zassert_str_equal(text, "1",
55 "Value=123 is not converted to \"1\" using 1-byte buffer");
56
57 memset(text, 0, sizeof(text));
58 len = u8_to_dec(text, 0, 123);
59 zassert_equal(len, 0,
60 "Length of converted value using 0 byte buffer isn't 0");
61 }
62
ZTEST(util,test_sign_extend)63 ZTEST(util, test_sign_extend) {
64 uint8_t u8;
65 uint16_t u16;
66 uint32_t u32;
67
68 u8 = 0x0f;
69 zassert_equal(sign_extend(u8, 3), -1);
70 zassert_equal(sign_extend(u8, 4), 0xf);
71
72 u16 = 0xfff;
73 zassert_equal(sign_extend(u16, 11), -1);
74 zassert_equal(sign_extend(u16, 12), 0xfff);
75
76 u32 = 0xfffffff;
77 zassert_equal(sign_extend(u32, 27), -1);
78 zassert_equal(sign_extend(u32, 28), 0xfffffff);
79 }
80
ZTEST(util,test_sign_extend_64)81 ZTEST(util, test_sign_extend_64) {
82 uint8_t u8;
83 uint16_t u16;
84 uint32_t u32;
85 uint64_t u64;
86
87 u8 = 0x0f;
88 zassert_equal(sign_extend_64(u8, 3), -1);
89 zassert_equal(sign_extend_64(u8, 4), 0xf);
90
91 u16 = 0xfff;
92 zassert_equal(sign_extend_64(u16, 11), -1);
93 zassert_equal(sign_extend_64(u16, 12), 0xfff);
94
95 u32 = 0xfffffff;
96 zassert_equal(sign_extend_64(u32, 27), -1);
97 zassert_equal(sign_extend_64(u32, 28), 0xfffffff);
98
99 u64 = 0xfffffffffffffff;
100 zassert_equal(sign_extend_64(u64, 59), -1);
101 zassert_equal(sign_extend_64(u64, 60), 0xfffffffffffffff);
102 }
103
ZTEST(util,test_COND_CODE_1)104 ZTEST(util, test_COND_CODE_1) {
105 #define TEST_DEFINE_1 1
106 #define TEST_DEFINE_0 0
107 /* Test validates that expected code has been injected. Failure would
108 * be seen in compilation (lack of variable or unused variable).
109 */
110 COND_CODE_1(1, (uint32_t x0 = 1;), (uint32_t y0;))
111 zassert_true((x0 == 1));
112
113 COND_CODE_1(NOT_EXISTING_DEFINE, (uint32_t x1 = 1;), (uint32_t y1 = 1;))
114 zassert_true((y1 == 1));
115
116 COND_CODE_1(TEST_DEFINE_1, (uint32_t x2 = 1;), (uint32_t y2 = 1;))
117 zassert_true((x2 == 1));
118
119 COND_CODE_1(2, (uint32_t x3 = 1;), (uint32_t y3 = 1;))
120 zassert_true((y3 == 1));
121 }
122
ZTEST(util,test_COND_CODE_0)123 ZTEST(util, test_COND_CODE_0) {
124 /* Test validates that expected code has been injected. Failure would
125 * be seen in compilation (lack of variable or unused variable).
126 */
127 COND_CODE_0(0, (uint32_t x0 = 1;), (uint32_t y0;))
128 zassert_true((x0 == 1));
129
130 COND_CODE_0(NOT_EXISTING_DEFINE, (uint32_t x1 = 1;), (uint32_t y1 = 1;))
131 zassert_true((y1 == 1));
132
133 COND_CODE_0(TEST_DEFINE_0, (uint32_t x2 = 1;), (uint32_t y2 = 1;))
134 zassert_true((x2 == 1));
135
136 COND_CODE_0(2, (uint32_t x3 = 1;), (uint32_t y3 = 1;))
137 zassert_true((y3 == 1));
138 }
139
140 #undef ZERO
141 #undef SEVEN
142 #undef A_BUILD_ERROR
143 #define ZERO 0
144 #define SEVEN 7
145 #define A_BUILD_ERROR (this would be a build error if you used || or &&)
ZTEST(util,test_UTIL_OR)146 ZTEST(util, test_UTIL_OR) {
147 zassert_equal(UTIL_OR(SEVEN, A_BUILD_ERROR), 7);
148 zassert_equal(UTIL_OR(7, 0), 7);
149 zassert_equal(UTIL_OR(SEVEN, ZERO), 7);
150 zassert_equal(UTIL_OR(0, 7), 7);
151 zassert_equal(UTIL_OR(ZERO, SEVEN), 7);
152 zassert_equal(UTIL_OR(0, 0), 0);
153 zassert_equal(UTIL_OR(ZERO, ZERO), 0);
154 }
155
ZTEST(util,test_UTIL_AND)156 ZTEST(util, test_UTIL_AND) {
157 zassert_equal(UTIL_AND(ZERO, A_BUILD_ERROR), 0);
158 zassert_equal(UTIL_AND(7, 0), 0);
159 zassert_equal(UTIL_AND(SEVEN, ZERO), 0);
160 zassert_equal(UTIL_AND(0, 7), 0);
161 zassert_equal(UTIL_AND(ZERO, SEVEN), 0);
162 zassert_equal(UTIL_AND(0, 0), 0);
163 zassert_equal(UTIL_AND(ZERO, ZERO), 0);
164 zassert_equal(UTIL_AND(7, 7), 7);
165 zassert_equal(UTIL_AND(7, SEVEN), 7);
166 zassert_equal(UTIL_AND(SEVEN, 7), 7);
167 zassert_equal(UTIL_AND(SEVEN, SEVEN), 7);
168 }
169
ZTEST(util,test_IF_ENABLED)170 ZTEST(util, test_IF_ENABLED) {
171 #define test_IF_ENABLED_FLAG_A 1
172 #define test_IF_ENABLED_FLAG_B 0
173
174 IF_ENABLED(test_IF_ENABLED_FLAG_A, (goto skipped;))
175 /* location should be skipped if IF_ENABLED macro is correct. */
176 zassert_false(true, "location should be skipped");
177 skipped:
178 IF_ENABLED(test_IF_ENABLED_FLAG_B, (zassert_false(true, "");))
179
180 IF_ENABLED(test_IF_ENABLED_FLAG_C, (zassert_false(true, "");))
181
182 zassert_true(true, "");
183
184 #undef test_IF_ENABLED_FLAG_A
185 #undef test_IF_ENABLED_FLAG_B
186 }
187
ZTEST(util,test_LISTIFY)188 ZTEST(util, test_LISTIFY) {
189 int ab0 = 1;
190 int ab1 = 1;
191 #define A_PTR(x, name0, name1) &UTIL_CAT(UTIL_CAT(name0, name1), x)
192
193 int *a[] = { LISTIFY(2, A_PTR, (,), a, b) };
194
195 zassert_equal(ARRAY_SIZE(a), 2);
196 zassert_equal(a[0], &ab0);
197 zassert_equal(a[1], &ab1);
198 }
199
ZTEST(util,test_MACRO_MAP_CAT)200 ZTEST(util, test_MACRO_MAP_CAT) {
201 int item_a_item_b_item_c_ = 1;
202
203 #undef FOO
204 #define FOO(x) item_##x##_
205 zassert_equal(MACRO_MAP_CAT(FOO, a, b, c), 1, "MACRO_MAP_CAT");
206 #undef FOO
207 }
208
inc_func(bool cleanup)209 static int inc_func(bool cleanup)
210 {
211 static int a;
212
213 if (cleanup) {
214 a = 1;
215 }
216
217 return a++;
218 }
219
220 /* Test checks if @ref Z_MAX, @ref Z_MIN and @ref Z_CLAMP return correct result
221 * and perform single evaluation of input arguments.
222 */
ZTEST(util,test_z_max_z_min_z_clamp)223 ZTEST(util, test_z_max_z_min_z_clamp) {
224 zassert_equal(Z_MAX(inc_func(true), 0), 1, "Unexpected macro result");
225 /* Z_MAX should have call inc_func only once */
226 zassert_equal(inc_func(false), 2, "Unexpected return value");
227
228 zassert_equal(Z_MIN(inc_func(false), 2), 2, "Unexpected macro result");
229 /* Z_MIN should have call inc_func only once */
230 zassert_equal(inc_func(false), 4, "Unexpected return value");
231
232 zassert_equal(Z_CLAMP(inc_func(false), 1, 3), 3, "Unexpected macro result");
233 /* Z_CLAMP should have call inc_func only once */
234 zassert_equal(inc_func(false), 6, "Unexpected return value");
235
236 zassert_equal(Z_CLAMP(inc_func(false), 10, 15), 10,
237 "Unexpected macro result");
238 /* Z_CLAMP should have call inc_func only once */
239 zassert_equal(inc_func(false), 8, "Unexpected return value");
240 }
241
ZTEST(util,test_CLAMP)242 ZTEST(util, test_CLAMP) {
243 zassert_equal(CLAMP(5, 3, 7), 5, "Unexpected clamp result");
244 zassert_equal(CLAMP(3, 3, 7), 3, "Unexpected clamp result");
245 zassert_equal(CLAMP(7, 3, 7), 7, "Unexpected clamp result");
246 zassert_equal(CLAMP(1, 3, 7), 3, "Unexpected clamp result");
247 zassert_equal(CLAMP(8, 3, 7), 7, "Unexpected clamp result");
248
249 zassert_equal(CLAMP(-5, -7, -3), -5, "Unexpected clamp result");
250 zassert_equal(CLAMP(-9, -7, -3), -7, "Unexpected clamp result");
251 zassert_equal(CLAMP(1, -7, -3), -3, "Unexpected clamp result");
252
253 zassert_equal(CLAMP(0xffffffffaULL, 0xffffffff0ULL, 0xfffffffffULL),
254 0xffffffffaULL, "Unexpected clamp result");
255 }
256
ZTEST(util,test_IN_RANGE)257 ZTEST(util, test_IN_RANGE) {
258 zassert_true(IN_RANGE(0, 0, 0), "Unexpected IN_RANGE result");
259 zassert_true(IN_RANGE(1, 0, 1), "Unexpected IN_RANGE result");
260 zassert_true(IN_RANGE(1, 0, 2), "Unexpected IN_RANGE result");
261 zassert_true(IN_RANGE(-1, -2, 2), "Unexpected IN_RANGE result");
262 zassert_true(IN_RANGE(-3, -5, -1), "Unexpected IN_RANGE result");
263 zassert_true(IN_RANGE(0, 0, UINT64_MAX), "Unexpected IN_RANGE result");
264 zassert_true(IN_RANGE(UINT64_MAX, 0, UINT64_MAX), "Unexpected IN_RANGE result");
265 zassert_true(IN_RANGE(0, INT64_MIN, INT64_MAX), "Unexpected IN_RANGE result");
266 zassert_true(IN_RANGE(INT64_MIN, INT64_MIN, INT64_MAX), "Unexpected IN_RANGE result");
267 zassert_true(IN_RANGE(INT64_MAX, INT64_MIN, INT64_MAX), "Unexpected IN_RANGE result");
268
269 zassert_false(IN_RANGE(5, 0, 2), "Unexpected IN_RANGE result");
270 zassert_false(IN_RANGE(5, 10, 0), "Unexpected IN_RANGE result");
271 zassert_false(IN_RANGE(-1, 0, 1), "Unexpected IN_RANGE result");
272 }
273
ZTEST(util,test_FOR_EACH)274 ZTEST(util, test_FOR_EACH) {
275 #define FOR_EACH_MACRO_TEST(arg) *buf++ = arg
276 #define FOR_EACH_MACRO_TEST2(arg) arg
277
278 uint8_t array[3] = {0};
279 uint8_t *buf = array;
280
281 FOR_EACH(FOR_EACH_MACRO_TEST, (;), 1, 2, 3);
282
283 zassert_equal(array[0], 1, "Unexpected value %d", array[0]);
284 zassert_equal(array[1], 2, "Unexpected value %d", array[1]);
285 zassert_equal(array[2], 3, "Unexpected value %d", array[2]);
286
287 uint8_t test0[] = { 0, FOR_EACH(FOR_EACH_MACRO_TEST2, (,))};
288
289 BUILD_ASSERT(sizeof(test0) == 1, "Unexpected length due to FOR_EACH fail");
290
291 uint8_t test1[] = { 0, FOR_EACH(FOR_EACH_MACRO_TEST2, (,), 1)};
292
293 BUILD_ASSERT(sizeof(test1) == 2, "Unexpected length due to FOR_EACH fail");
294 }
295
ZTEST(util,test_FOR_EACH_NONEMPTY_TERM)296 ZTEST(util, test_FOR_EACH_NONEMPTY_TERM) {
297 #define SQUARE(arg) (arg * arg)
298 #define SWALLOW_VA_ARGS_1(...) EMPTY
299 #define SWALLOW_VA_ARGS_2(...)
300 #define REPEAT_VA_ARGS(...) __VA_ARGS__
301
302 uint8_t array[] = {
303 FOR_EACH_NONEMPTY_TERM(SQUARE, (,))
304 FOR_EACH_NONEMPTY_TERM(SQUARE, (,),)
305 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), ,)
306 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), EMPTY, EMPTY)
307 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), SWALLOW_VA_ARGS_1(a, b))
308 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), SWALLOW_VA_ARGS_2(c, d))
309 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), 1)
310 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), 2, 3)
311 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), REPEAT_VA_ARGS(4))
312 FOR_EACH_NONEMPTY_TERM(SQUARE, (,), REPEAT_VA_ARGS(5, 6))
313 255
314 };
315
316 size_t size = ARRAY_SIZE(array);
317
318 zassert_equal(size, 7, "Unexpected size %d", size);
319 zassert_equal(array[0], 1, "Unexpected value %d", array[0]);
320 zassert_equal(array[1], 4, "Unexpected value %d", array[1]);
321 zassert_equal(array[2], 9, "Unexpected value %d", array[2]);
322 zassert_equal(array[3], 16, "Unexpected value %d", array[3]);
323 zassert_equal(array[4], 25, "Unexpected value %d", array[4]);
324 zassert_equal(array[5], 36, "Unexpected value %d", array[5]);
325 zassert_equal(array[6], 255, "Unexpected value %d", array[6]);
326 }
327
fsum(uint32_t incr,uint32_t * sum)328 static void fsum(uint32_t incr, uint32_t *sum)
329 {
330 *sum = *sum + incr;
331 }
332
ZTEST(util,test_FOR_EACH_FIXED_ARG)333 ZTEST(util, test_FOR_EACH_FIXED_ARG) {
334 uint32_t sum = 0;
335
336 FOR_EACH_FIXED_ARG(fsum, (;), &sum, 1, 2, 3);
337
338 zassert_equal(sum, 6, "Unexpected value %d", sum);
339 }
340
ZTEST(util,test_FOR_EACH_IDX)341 ZTEST(util, test_FOR_EACH_IDX) {
342 #define FOR_EACH_IDX_MACRO_TEST(n, arg) uint8_t a##n = arg
343
344 FOR_EACH_IDX(FOR_EACH_IDX_MACRO_TEST, (;), 1, 2, 3);
345
346 zassert_equal(a0, 1, "Unexpected value %d", a0);
347 zassert_equal(a1, 2, "Unexpected value %d", a1);
348 zassert_equal(a2, 3, "Unexpected value %d", a2);
349
350 #define FOR_EACH_IDX_MACRO_TEST2(n, arg) array[n] = arg
351 uint8_t array[32] = {0};
352
353 FOR_EACH_IDX(FOR_EACH_IDX_MACRO_TEST2, (;), 1, 2, 3, 4, 5, 6, 7, 8,
354 9, 10, 11, 12, 13, 14, 15);
355 for (int i = 0; i < 15; i++) {
356 zassert_equal(array[i], i + 1,
357 "Unexpected value: %d", array[i]);
358 }
359 zassert_equal(array[15], 0, "Unexpected value: %d", array[15]);
360
361 #define FOR_EACH_IDX_MACRO_TEST3(n, arg) &a##n
362
363 uint8_t *a[] = {
364 FOR_EACH_IDX(FOR_EACH_IDX_MACRO_TEST3, (,), 1, 2, 3)
365 };
366
367 zassert_equal(ARRAY_SIZE(a), 3, "Unexpected value:%zu", ARRAY_SIZE(a));
368 }
369
ZTEST(util,test_FOR_EACH_IDX_FIXED_ARG)370 ZTEST(util, test_FOR_EACH_IDX_FIXED_ARG) {
371 #undef FOO
372 #define FOO(n, arg, fixed_arg) \
373 uint8_t fixed_arg##n = arg
374
375 FOR_EACH_IDX_FIXED_ARG(FOO, (;), a, 1, 2, 3);
376
377 zassert_equal(a0, 1, "Unexpected value %d", a0);
378 zassert_equal(a1, 2, "Unexpected value %d", a1);
379 zassert_equal(a2, 3, "Unexpected value %d", a2);
380 }
381
ZTEST(util,test_IS_EMPTY)382 ZTEST(util, test_IS_EMPTY) {
383 #define test_IS_EMPTY_REAL_EMPTY
384 #define test_IS_EMPTY_NOT_EMPTY XXX_DO_NOT_REPLACE_XXX
385 zassert_true(IS_EMPTY(test_IS_EMPTY_REAL_EMPTY),
386 "Expected to be empty");
387 zassert_false(IS_EMPTY(test_IS_EMPTY_NOT_EMPTY),
388 "Expected to be non-empty");
389 zassert_false(IS_EMPTY("string"),
390 "Expected to be non-empty");
391 zassert_false(IS_EMPTY(&test_IS_EMPTY),
392 "Expected to be non-empty");
393 }
394
ZTEST(util,test_IS_EQ)395 ZTEST(util, test_IS_EQ) {
396 zassert_true(IS_EQ(0, 0), "Unexpected IS_EQ result");
397 zassert_true(IS_EQ(1, 1), "Unexpected IS_EQ result");
398 zassert_true(IS_EQ(7, 7), "Unexpected IS_EQ result");
399 zassert_true(IS_EQ(0U, 0U), "Unexpected IS_EQ result");
400 zassert_true(IS_EQ(1U, 1U), "Unexpected IS_EQ result");
401 zassert_true(IS_EQ(7U, 7U), "Unexpected IS_EQ result");
402 zassert_true(IS_EQ(1, 1U), "Unexpected IS_EQ result");
403 zassert_true(IS_EQ(1U, 1), "Unexpected IS_EQ result");
404
405 zassert_false(IS_EQ(0, 1), "Unexpected IS_EQ result");
406 zassert_false(IS_EQ(1, 7), "Unexpected IS_EQ result");
407 zassert_false(IS_EQ(7, 0), "Unexpected IS_EQ result");
408 }
409
ZTEST(util,test_LIST_DROP_EMPTY)410 ZTEST(util, test_LIST_DROP_EMPTY) {
411 /*
412 * The real definition should be:
413 * #define TEST_BROKEN_LIST ,Henry,,Dorsett,Case,
414 * but checkpatch complains, so below equivalent is defined.
415 */
416 #define TEST_BROKEN_LIST EMPTY, Henry, EMPTY, Dorsett, Case,
417 #define TEST_FIXED_LIST LIST_DROP_EMPTY(TEST_BROKEN_LIST)
418 static const char *const arr[] = {
419 FOR_EACH(STRINGIFY, (,), TEST_FIXED_LIST)
420 };
421
422 zassert_equal(ARRAY_SIZE(arr), 3, "Failed to cleanup list");
423 zassert_str_equal(arr[0], "Henry", "Failed at 0");
424 zassert_str_equal(arr[1], "Dorsett", "Failed at 1");
425 zassert_str_equal(arr[2], "Case", "Failed at 0");
426 }
427
ZTEST(util,test_nested_FOR_EACH)428 ZTEST(util, test_nested_FOR_EACH) {
429 #define FOO_1(x) a##x = x
430 #define FOO_2(x) int x
431
432 FOR_EACH(FOO_2, (;), FOR_EACH(FOO_1, (,), 0, 1, 2));
433
434 zassert_equal(a0, 0);
435 zassert_equal(a1, 1);
436 zassert_equal(a2, 2);
437 }
438
ZTEST(util,test_GET_ARG_N)439 ZTEST(util, test_GET_ARG_N) {
440 int a = GET_ARG_N(1, 10, 100, 1000);
441 int b = GET_ARG_N(2, 10, 100, 1000);
442 int c = GET_ARG_N(3, 10, 100, 1000);
443
444 zassert_equal(a, 10);
445 zassert_equal(b, 100);
446 zassert_equal(c, 1000);
447 }
448
ZTEST(util,test_GET_ARGS_LESS_N)449 ZTEST(util, test_GET_ARGS_LESS_N) {
450 uint8_t a[] = { GET_ARGS_LESS_N(0, 1, 2, 3) };
451 uint8_t b[] = { GET_ARGS_LESS_N(1, 1, 2, 3) };
452 uint8_t c[] = { GET_ARGS_LESS_N(2, 1, 2, 3) };
453
454 zassert_equal(sizeof(a), 3);
455
456 zassert_equal(sizeof(b), 2);
457 zassert_equal(b[0], 2);
458 zassert_equal(b[1], 3);
459
460 zassert_equal(sizeof(c), 1);
461 zassert_equal(c[0], 3);
462 }
463
ZTEST(util,test_mixing_GET_ARG_and_FOR_EACH)464 ZTEST(util, test_mixing_GET_ARG_and_FOR_EACH) {
465 #undef TEST_MACRO
466 #define TEST_MACRO(x) x,
467 int i;
468
469 i = GET_ARG_N(3, FOR_EACH(TEST_MACRO, (), 1, 2, 3, 4, 5));
470 zassert_equal(i, 3);
471
472 i = GET_ARG_N(2, 1, GET_ARGS_LESS_N(2, 1, 2, 3, 4, 5));
473 zassert_equal(i, 3);
474
475 #undef TEST_MACRO
476 #undef TEST_MACRO2
477 #define TEST_MACRO(x) GET_ARG_N(3, 1, 2, x),
478 #define TEST_MACRO2(...) FOR_EACH(TEST_MACRO, (), __VA_ARGS__)
479 int a[] = {
480 LIST_DROP_EMPTY(TEST_MACRO2(1, 2, 3, 4)), 5
481 };
482
483 zassert_equal(ARRAY_SIZE(a), 5);
484 zassert_equal(a[0], 1);
485 zassert_equal(a[1], 2);
486 zassert_equal(a[2], 3);
487 zassert_equal(a[3], 4);
488 zassert_equal(a[4], 5);
489 }
490
ZTEST(util,test_IS_ARRAY_ELEMENT)491 ZTEST(util, test_IS_ARRAY_ELEMENT)
492 {
493 size_t i;
494 size_t array[3];
495 uint8_t *const alias = (uint8_t *)array;
496
497 zassert_false(IS_ARRAY_ELEMENT(array, &array[-1]));
498 zassert_false(IS_ARRAY_ELEMENT(array, &array[ARRAY_SIZE(array)]));
499 zassert_false(IS_ARRAY_ELEMENT(array, &alias[1]));
500
501 for (i = 0; i < ARRAY_SIZE(array); ++i) {
502 zassert_true(IS_ARRAY_ELEMENT(array, &array[i]));
503 }
504 }
505
ZTEST(util,test_ARRAY_INDEX)506 ZTEST(util, test_ARRAY_INDEX)
507 {
508 size_t i;
509 size_t array[] = {0, 1, 2, 3};
510
511 for (i = 0; i < ARRAY_SIZE(array); ++i) {
512 zassert_equal(array[ARRAY_INDEX(array, &array[i])], i);
513 }
514 }
515
ZTEST(util,test_ARRAY_FOR_EACH)516 ZTEST(util, test_ARRAY_FOR_EACH)
517 {
518 size_t j = -1;
519 size_t array[3];
520
521 ARRAY_FOR_EACH(array, i) {
522 j = i + 1;
523 }
524
525 zassert_equal(j, ARRAY_SIZE(array));
526 }
527
ZTEST(util,test_ARRAY_FOR_EACH_PTR)528 ZTEST(util, test_ARRAY_FOR_EACH_PTR)
529 {
530 size_t j = 0;
531 size_t array[3];
532 size_t *ptr[3];
533
534 ARRAY_FOR_EACH_PTR(array, p) {
535 ptr[j] = p;
536 ++j;
537 }
538
539 zassert_equal(ptr[0], &array[0]);
540 zassert_equal(ptr[1], &array[1]);
541 zassert_equal(ptr[2], &array[2]);
542 }
543
ZTEST(util,test_PART_OF_ARRAY)544 ZTEST(util, test_PART_OF_ARRAY)
545 {
546 size_t i;
547 size_t array[3];
548 uint8_t *const alias = (uint8_t *)array;
549
550 ARG_UNUSED(i);
551 ARG_UNUSED(alias);
552
553 zassert_false(PART_OF_ARRAY(array, &array[-1]));
554 zassert_false(PART_OF_ARRAY(array, &array[ARRAY_SIZE(array)]));
555
556 for (i = 0; i < ARRAY_SIZE(array); ++i) {
557 zassert_true(PART_OF_ARRAY(array, &array[i]));
558 }
559
560 zassert_true(PART_OF_ARRAY(array, &alias[1]));
561 }
562
ZTEST(util,test_ARRAY_INDEX_FLOOR)563 ZTEST(util, test_ARRAY_INDEX_FLOOR)
564 {
565 size_t i;
566 size_t array[] = {0, 1, 2, 3};
567 uint8_t *const alias = (uint8_t *)array;
568
569 for (i = 0; i < ARRAY_SIZE(array); ++i) {
570 zassert_equal(array[ARRAY_INDEX_FLOOR(array, &array[i])], i);
571 }
572
573 zassert_equal(array[ARRAY_INDEX_FLOOR(array, &alias[1])], 0);
574 }
575
ZTEST(util,test_BIT_MASK)576 ZTEST(util, test_BIT_MASK)
577 {
578 uint32_t bitmask0 = BIT_MASK(0);
579 uint32_t bitmask1 = BIT_MASK(1);
580 uint32_t bitmask2 = BIT_MASK(2);
581 uint32_t bitmask31 = BIT_MASK(31);
582
583 zassert_equal(0x00000000UL, bitmask0);
584 zassert_equal(0x00000001UL, bitmask1);
585 zassert_equal(0x00000003UL, bitmask2);
586 zassert_equal(0x7ffffffFUL, bitmask31);
587 }
588
ZTEST(util,test_BIT_MASK64)589 ZTEST(util, test_BIT_MASK64)
590 {
591 uint64_t bitmask0 = BIT64_MASK(0);
592 uint64_t bitmask1 = BIT64_MASK(1);
593 uint64_t bitmask2 = BIT64_MASK(2);
594 uint64_t bitmask63 = BIT64_MASK(63);
595
596 zassert_equal(0x0000000000000000ULL, bitmask0);
597 zassert_equal(0x0000000000000001ULL, bitmask1);
598 zassert_equal(0x0000000000000003ULL, bitmask2);
599 zassert_equal(0x7fffffffffffffffULL, bitmask63);
600 }
601
ZTEST(util,test_IS_BIT_MASK)602 ZTEST(util, test_IS_BIT_MASK)
603 {
604 uint32_t zero32 = 0UL;
605 uint64_t zero64 = 0ULL;
606 uint32_t bitmask1 = 0x00000001UL;
607 uint32_t bitmask2 = 0x00000003UL;
608 uint32_t bitmask31 = 0x7fffffffUL;
609 uint32_t bitmask32 = 0xffffffffUL;
610 uint64_t bitmask63 = 0x7fffffffffffffffULL;
611 uint64_t bitmask64 = 0xffffffffffffffffULL;
612
613 uint32_t not_bitmask32 = 0xfffffffeUL;
614 uint64_t not_bitmask64 = 0xfffffffffffffffeULL;
615
616 zassert_true(IS_BIT_MASK(zero32));
617 zassert_true(IS_BIT_MASK(zero64));
618 zassert_true(IS_BIT_MASK(bitmask1));
619 zassert_true(IS_BIT_MASK(bitmask2));
620 zassert_true(IS_BIT_MASK(bitmask31));
621 zassert_true(IS_BIT_MASK(bitmask32));
622 zassert_true(IS_BIT_MASK(bitmask63));
623 zassert_true(IS_BIT_MASK(bitmask64));
624 zassert_false(IS_BIT_MASK(not_bitmask32));
625 zassert_false(IS_BIT_MASK(not_bitmask64));
626
627 zassert_true(IS_BIT_MASK(0));
628 zassert_true(IS_BIT_MASK(0x00000001UL));
629 zassert_true(IS_BIT_MASK(0x00000003UL));
630 zassert_true(IS_BIT_MASK(0x7fffffffUL));
631 zassert_true(IS_BIT_MASK(0xffffffffUL));
632 zassert_true(IS_BIT_MASK(0x7fffffffffffffffUL));
633 zassert_true(IS_BIT_MASK(0xffffffffffffffffUL));
634 zassert_false(IS_BIT_MASK(0xfffffffeUL));
635 zassert_false(IS_BIT_MASK(0xfffffffffffffffeULL));
636 zassert_false(IS_BIT_MASK(0x00000002UL));
637 zassert_false(IS_BIT_MASK(0x8000000000000000ULL));
638 }
639
ZTEST(util,test_IS_SHIFTED_BIT_MASK)640 ZTEST(util, test_IS_SHIFTED_BIT_MASK)
641 {
642 uint32_t bitmask32_shift1 = 0xfffffffeUL;
643 uint32_t bitmask32_shift31 = 0x80000000UL;
644 uint64_t bitmask64_shift1 = 0xfffffffffffffffeULL;
645 uint64_t bitmask64_shift63 = 0x8000000000000000ULL;
646
647 zassert_true(IS_SHIFTED_BIT_MASK(bitmask32_shift1, 1));
648 zassert_true(IS_SHIFTED_BIT_MASK(bitmask32_shift31, 31));
649 zassert_true(IS_SHIFTED_BIT_MASK(bitmask64_shift1, 1));
650 zassert_true(IS_SHIFTED_BIT_MASK(bitmask64_shift63, 63));
651
652 zassert_true(IS_SHIFTED_BIT_MASK(0xfffffffeUL, 1));
653 zassert_true(IS_SHIFTED_BIT_MASK(0xfffffffffffffffeULL, 1));
654 zassert_true(IS_SHIFTED_BIT_MASK(0x80000000UL, 31));
655 zassert_true(IS_SHIFTED_BIT_MASK(0x8000000000000000ULL, 63));
656 }
657
ZTEST(util,test_DIV_ROUND_UP)658 ZTEST(util, test_DIV_ROUND_UP)
659 {
660 zassert_equal(DIV_ROUND_UP(0, 1), 0);
661 zassert_equal(DIV_ROUND_UP(1, 2), 1);
662 zassert_equal(DIV_ROUND_UP(3, 2), 2);
663 }
664
ZTEST(util,test_DIV_ROUND_CLOSEST)665 ZTEST(util, test_DIV_ROUND_CLOSEST)
666 {
667 zassert_equal(DIV_ROUND_CLOSEST(0, 1), 0);
668 /* 5 / 2 = 2.5 -> 3 */
669 zassert_equal(DIV_ROUND_CLOSEST(5, 2), 3);
670 zassert_equal(DIV_ROUND_CLOSEST(5, -2), -3);
671 zassert_equal(DIV_ROUND_CLOSEST(-5, 2), -3);
672 zassert_equal(DIV_ROUND_CLOSEST(-5, -2), 3);
673 /* 7 / 3 = 2.(3) -> 2 */
674 zassert_equal(DIV_ROUND_CLOSEST(7, 3), 2);
675 zassert_equal(DIV_ROUND_CLOSEST(-7, 3), -2);
676 }
677
ZTEST(util,test_IF_DISABLED)678 ZTEST(util, test_IF_DISABLED)
679 {
680 #define test_IF_DISABLED_FLAG_A 0
681 #define test_IF_DISABLED_FLAG_B 1
682
683 IF_DISABLED(test_IF_DISABLED_FLAG_A, (goto skipped_a;))
684 /* location should be skipped if IF_DISABLED macro is correct. */
685 zassert_false(true, "location A should be skipped");
686 skipped_a:
687 IF_DISABLED(test_IF_DISABLED_FLAG_B, (zassert_false(true, "");))
688
689 IF_DISABLED(test_IF_DISABLED_FLAG_C, (goto skipped_c;))
690 /* location should be skipped if IF_DISABLED macro is correct. */
691 zassert_false(true, "location C should be skipped");
692 skipped_c:
693
694 zassert_true(true, "");
695
696 #undef test_IF_DISABLED_FLAG_A
697 #undef test_IF_DISABLED_FLAG_B
698 }
699
ZTEST(util,test_mem_xor_n)700 ZTEST(util, test_mem_xor_n)
701 {
702 const size_t max_len = 128;
703 uint8_t expected_result[max_len];
704 uint8_t src1[max_len];
705 uint8_t src2[max_len];
706 uint8_t dst[max_len];
707
708 memset(expected_result, 0, sizeof(expected_result));
709 memset(src1, 0, sizeof(src1));
710 memset(src2, 0, sizeof(src2));
711 memset(dst, 0, sizeof(dst));
712
713 for (size_t i = 0U; i < max_len; i++) {
714 const size_t len = i;
715
716 for (size_t j = 0U; j < len; j++) {
717 src1[j] = 0x33;
718 src2[j] = 0x0F;
719 expected_result[j] = 0x3C;
720 }
721
722 mem_xor_n(dst, src1, src2, len);
723 zassert_mem_equal(expected_result, dst, len);
724 }
725 }
726
ZTEST(util,test_mem_xor_32)727 ZTEST(util, test_mem_xor_32)
728 {
729 uint8_t expected_result[4];
730 uint8_t src1[4];
731 uint8_t src2[4];
732 uint8_t dst[4];
733
734 memset(expected_result, 0, sizeof(expected_result));
735 memset(src1, 0, sizeof(src1));
736 memset(src2, 0, sizeof(src2));
737 memset(dst, 0, sizeof(dst));
738
739 for (size_t i = 0U; i < 4; i++) {
740 src1[i] = 0x43;
741 src2[i] = 0x0F;
742 expected_result[i] = 0x4C;
743 }
744
745 mem_xor_32(dst, src1, src2);
746 zassert_mem_equal(expected_result, dst, 4);
747 }
748
ZTEST(util,test_mem_xor_128)749 ZTEST(util, test_mem_xor_128)
750 {
751 uint8_t expected_result[16];
752 uint8_t src1[16];
753 uint8_t src2[16];
754 uint8_t dst[16];
755
756 memset(expected_result, 0, sizeof(expected_result));
757 memset(src1, 0, sizeof(src1));
758 memset(src2, 0, sizeof(src2));
759 memset(dst, 0, sizeof(dst));
760
761 for (size_t i = 0U; i < 16; i++) {
762 src1[i] = 0x53;
763 src2[i] = 0x0F;
764 expected_result[i] = 0x5C;
765 }
766
767 mem_xor_128(dst, src1, src2);
768 zassert_mem_equal(expected_result, dst, 16);
769 }
770
ZTEST(util,test_CONCAT)771 ZTEST(util, test_CONCAT)
772 {
773 #define _CAT_PART1 1
774 #define CAT_PART1 _CAT_PART1
775 #define _CAT_PART2 2
776 #define CAT_PART2 _CAT_PART2
777 #define _CAT_PART3 3
778 #define CAT_PART3 _CAT_PART3
779 #define _CAT_PART4 4
780 #define CAT_PART4 _CAT_PART4
781 #define _CAT_PART5 5
782 #define CAT_PART5 _CAT_PART5
783 #define _CAT_PART6 6
784 #define CAT_PART6 _CAT_PART6
785 #define _CAT_PART7 7
786 #define CAT_PART7 _CAT_PART7
787 #define _CAT_PART8 8
788 #define CAT_PART8 _CAT_PART8
789
790 zassert_equal(CONCAT(CAT_PART1), 1);
791 zassert_equal(CONCAT(CAT_PART1, CAT_PART2), 12);
792 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3), 123);
793 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3, CAT_PART4), 1234);
794 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3, CAT_PART4, CAT_PART5), 12345);
795 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3, CAT_PART4, CAT_PART5, CAT_PART6),
796 123456);
797 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3, CAT_PART4,
798 CAT_PART5, CAT_PART6, CAT_PART7),
799 1234567);
800 zassert_equal(CONCAT(CAT_PART1, CAT_PART2, CAT_PART3, CAT_PART4,
801 CAT_PART5, CAT_PART6, CAT_PART7, CAT_PART8),
802 12345678);
803
804 zassert_equal(CONCAT(CAT_PART1, CONCAT(CAT_PART2, CAT_PART3)), 123);
805 }
806
ZTEST(util,test_SIZEOF_FIELD)807 ZTEST(util, test_SIZEOF_FIELD)
808 {
809 struct test_t {
810 uint32_t a;
811 uint8_t b;
812 uint8_t c[17];
813 int16_t d;
814 };
815
816 BUILD_ASSERT(SIZEOF_FIELD(struct test_t, a) == 4, "The a member is 4-byte wide.");
817 BUILD_ASSERT(SIZEOF_FIELD(struct test_t, b) == 1, "The b member is 1-byte wide.");
818 BUILD_ASSERT(SIZEOF_FIELD(struct test_t, c) == 17, "The c member is 17-byte wide.");
819 BUILD_ASSERT(SIZEOF_FIELD(struct test_t, d) == 2, "The d member is 2-byte wide.");
820 }
821
ZTEST(util,test_utf8_trunc_truncated)822 ZTEST(util, test_utf8_trunc_truncated)
823 {
824 char test_str[] = "€€€";
825 char expected_result[] = "€€";
826
827 /* Remove last byte from truncated_test_str and verify that it first is incorrectly
828 * truncated, followed by a proper truncation and verification
829 */
830 test_str[strlen(test_str) - 1] = '\0';
831 zassert(strcmp(test_str, "€€€") != 0, "Failed to do invalid truncation");
832 zassert(strcmp(test_str, expected_result) != 0, "Failed to do invalid truncation");
833
834 utf8_trunc(test_str);
835
836 zassert_str_equal(test_str, expected_result, "Failed to truncate");
837 }
838
ZTEST(util,test_utf8_trunc_not_truncated)839 ZTEST(util, test_utf8_trunc_not_truncated)
840 {
841 /* Attempt to truncate a valid UTF8 string and verify no changed */
842 char test_str[] = "€€€";
843 char expected_result[] = "€€€";
844
845 utf8_trunc(test_str);
846
847 zassert_str_equal(test_str, expected_result, "Failed to truncate");
848 }
849
ZTEST(util,test_utf8_trunc_zero_length)850 ZTEST(util, test_utf8_trunc_zero_length)
851 {
852 /* Attempt to truncate a valid UTF8 string and verify no changed */
853 char test_str[] = "";
854 char expected_result[] = "";
855
856 utf8_trunc(test_str);
857
858 zassert_str_equal(test_str, expected_result, "Failed to truncate");
859 }
860
ZTEST(util,test_utf8_lcpy_truncated)861 ZTEST(util, test_utf8_lcpy_truncated)
862 {
863 /* dest_str size is based on storing 2 * € plus the null terminator plus an extra space to
864 * verify that it's truncated properly
865 */
866 char dest_str[strlen("€") * 2 + 1 + 1];
867 char test_str[] = "€€€";
868 char expected_result[] = "€€";
869
870 utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
871
872 zassert_str_equal(dest_str, expected_result, "Failed to copy");
873 }
874
ZTEST(util,test_utf8_lcpy_not_truncated)875 ZTEST(util, test_utf8_lcpy_not_truncated)
876 {
877 /* dest_str size is based on storing 3 * € plus the null terminator */
878 char dest_str[strlen("€") * 3 + 1];
879 char test_str[] = "€€€";
880 char expected_result[] = "€€€";
881
882 utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
883
884 zassert_str_equal(dest_str, expected_result, "Failed to truncate");
885 }
886
ZTEST(util,test_utf8_lcpy_zero_length_copy)887 ZTEST(util, test_utf8_lcpy_zero_length_copy)
888 {
889 /* dest_str size is based on the null terminator */
890 char dest_str[1];
891 char test_str[] = "";
892 char expected_result[] = "";
893
894 utf8_lcpy(dest_str, test_str, sizeof((dest_str)));
895
896 zassert_str_equal(dest_str, expected_result, "Failed to truncate");
897 }
898
ZTEST(util,test_utf8_lcpy_zero_length_dest)899 ZTEST(util, test_utf8_lcpy_zero_length_dest)
900 {
901 char dest_str[] = "A";
902 char test_str[] = "";
903 char expected_result[] = "A"; /* expect no changes to dest_str */
904
905 utf8_lcpy(dest_str, test_str, 0);
906
907 zassert_str_equal(dest_str, expected_result, "Failed to truncate");
908 }
909
ZTEST(util,test_utf8_lcpy_null_termination)910 ZTEST(util, test_utf8_lcpy_null_termination)
911 {
912 char dest_str[] = "DEADBEEF";
913 char test_str[] = "DEAD";
914 char expected_result[] = "DEAD";
915
916 utf8_lcpy(dest_str, test_str, sizeof(dest_str));
917
918 zassert_str_equal(dest_str, expected_result, "Failed to truncate");
919 }
920
921 ZTEST_SUITE(util, NULL, NULL, NULL, NULL, NULL);
922