1 /*
2 * Copyright (c) 2022 BayLibre SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9
10 /**
11 * @brief Test the Z_POW2_CEIL() macro
12 *
13 * @defgroup test_pow2_ceil Z_POW2_CEIL() tests
14 *
15 * @ingroup all_tests
16 *
17 * @{
18 * @}
19 */
20
21 /**
22 * @brief Verify compile-time constant results
23 *
24 * @ingroup test_pow2_ceil
25 *
26 * @details Check if static array allocations are sized as expected.
27 */
28
29 char static_array1[Z_POW2_CEIL(1)];
30 char static_array2[Z_POW2_CEIL(2)];
31 char static_array3[Z_POW2_CEIL(3)];
32 char static_array4[Z_POW2_CEIL(4)];
33 char static_array5[Z_POW2_CEIL(5)];
34 char static_array7[Z_POW2_CEIL(7)];
35 char static_array8[Z_POW2_CEIL(8)];
36 char static_array9[Z_POW2_CEIL(9)];
37
38 BUILD_ASSERT(sizeof(static_array1) == 1);
39 BUILD_ASSERT(sizeof(static_array2) == 2);
40 BUILD_ASSERT(sizeof(static_array3) == 4);
41 BUILD_ASSERT(sizeof(static_array4) == 4);
42 BUILD_ASSERT(sizeof(static_array5) == 8);
43 BUILD_ASSERT(sizeof(static_array7) == 8);
44 BUILD_ASSERT(sizeof(static_array8) == 8);
45 BUILD_ASSERT(sizeof(static_array9) == 16);
46
47 /**
48 * @brief Verify run-time non-constant results
49 *
50 * @ingroup test_pow2_ceil
51 *
52 * @details Check if run-time non-constant results are as expected.
53 * Use a volatile variable to prevent compiler optimizations.
54 */
55
test_pow2_ceil_x(unsigned long test_value,unsigned long expected_result)56 static void test_pow2_ceil_x(unsigned long test_value,
57 unsigned long expected_result)
58 {
59 volatile unsigned int x = test_value;
60 unsigned int result = Z_POW2_CEIL(x);
61
62 zassert_equal(result, expected_result,
63 "ZPOW2_CEIL(%lu) returned %lu, expected %lu",
64 test_value, result, expected_result);
65 }
66
ZTEST(pow2,test_pow2_ceil)67 ZTEST(pow2, test_pow2_ceil)
68 {
69 test_pow2_ceil_x(1, 1);
70 test_pow2_ceil_x(2, 2);
71 test_pow2_ceil_x(3, 4);
72 test_pow2_ceil_x(4, 4);
73 test_pow2_ceil_x(5, 8);
74 test_pow2_ceil_x(7, 8);
75 test_pow2_ceil_x(8, 8);
76 test_pow2_ceil_x(9, 16);
77 }
78
79 extern void *common_setup(void);
80 ZTEST_SUITE(pow2, NULL, common_setup, NULL, NULL, NULL);
81