1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #include <zephyr/arch/cpu.h>
10
11 #include <zephyr/tc_util.h>
12
13 #ifdef CONFIG_BIG_ENDIAN
14 #define BIT_INDEX(bit) ((3 - ((bit >> 3) & 0x3)) + 4*(bit >> 5))
15 #else
16 #define BIT_INDEX(bit) (bit >> 3)
17 #endif
18 #define BIT_VAL(bit) (1 << (bit & 0x7))
19 #define BITFIELD_SIZE 512
20
21 /**
22 * @addtogroup kernel_common_tests
23 * @{
24 */
25
26 /**
27 * @brief Test bitfield operations
28 *
29 * @see sys_test_bit(), sys_set_bit(), sys_clear_bit(),
30 * sys_bitfield_set_bit(), sys_bitfield_clear_bit(),
31 * sys_bitfield_test_bit(), sys_bitfield_test_and_set_bit(),
32 * sys_bitfield_test_and_clear_bit()
33 */
ZTEST(bitfield,test_bitfield)34 ZTEST(bitfield, test_bitfield)
35 {
36 #ifdef CONFIG_ARM
37 ztest_test_skip();
38 #else
39 uint32_t b1 = 0U;
40 unsigned char b2[BITFIELD_SIZE >> 3] = { 0 };
41 unsigned int bit;
42 int ret;
43
44 TC_PRINT("twiddling bits....\n");
45
46 for (bit = 0U; bit < 32; ++bit) {
47 sys_set_bit((mem_addr_t)&b1, bit);
48
49 zassert_equal(b1, (1 << bit),
50 "sys_set_bit failed on bit %d\n", bit);
51
52 zassert_true(sys_test_bit((mem_addr_t)&b1, bit),
53 "sys_test_bit did not detect bit %d\n", bit);
54
55 sys_clear_bit((mem_addr_t)&b1, bit);
56 zassert_equal(b1, 0, "sys_clear_bit failed for bit %d\n", bit);
57
58 zassert_false(sys_test_bit((mem_addr_t)&b1, bit),
59 "sys_test_bit erroneously detected bit %d\n",
60 bit);
61
62 zassert_false(sys_test_and_set_bit((mem_addr_t)&b1, bit),
63 "sys_test_and_set_bit erroneously"
64 " detected bit %d\n", bit);
65 zassert_equal(b1, (1 << bit),
66 "sys_test_and_set_bit did not set bit %d\n",
67 bit);
68 zassert_true(sys_test_and_set_bit((mem_addr_t)&b1, bit),
69 "sys_test_and_set_bit did not detect bit %d\n",
70 bit);
71 zassert_equal(b1, (1 << bit),
72 "sys_test_and_set_bit cleared bit %d\n", bit);
73
74 zassert_true(sys_test_and_clear_bit((mem_addr_t)&b1, bit),
75 "sys_test_and_clear_bit did not detect bit %d\n",
76 bit);
77 zassert_equal(b1, 0, "sys_test_and_clear_bit did not clear"
78 " bit %d\n", bit);
79 zassert_false(sys_test_and_clear_bit((mem_addr_t)&b1, bit),
80 "sys_test_and_clear_bit erroneously detected"
81 " bit %d\n", bit);
82 zassert_equal(b1, 0, "sys_test_and_clear_bit set bit %d\n",
83 bit);
84 }
85
86 for (bit = 0U; bit < BITFIELD_SIZE; ++bit) {
87 sys_bitfield_set_bit((mem_addr_t)b2, bit);
88 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
89 "sys_bitfield_set_bit failed for bit %d\n",
90 bit);
91 zassert_true(sys_bitfield_test_bit((mem_addr_t)b2, bit),
92 "sys_bitfield_test_bit did not detect bit %d\n",
93 bit);
94
95 sys_bitfield_clear_bit((mem_addr_t)b2, bit);
96 zassert_equal(b2[BIT_INDEX(bit)], 0,
97 "sys_bitfield_clear_bit failed for bit %d\n",
98 bit);
99 zassert_false(sys_bitfield_test_bit((mem_addr_t)b2, bit),
100 "sys_bitfield_test_bit erroneously detected"
101 " bit %d\n", bit);
102
103 ret = sys_bitfield_test_and_set_bit((mem_addr_t)b2, bit);
104 zassert_false(ret, "sys_bitfield_test_and_set_bit erroneously"
105 " detected bit %d\n", bit);
106
107 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
108 "sys_bitfield_test_and_set_bit did not set"
109 " bit %d\n", bit);
110 zassert_true(sys_bitfield_test_and_set_bit((mem_addr_t)b2, bit),
111 "sys_bitfield_test_and_set_bit did not detect bit"
112 " %d\n", bit);
113 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
114 "sys_bitfield_test_and_set_bit cleared bit %d\n",
115 bit);
116
117 zassert_true(sys_bitfield_test_and_clear_bit((mem_addr_t)b2,
118 bit), "sys_bitfield_test_and_clear_bit did not"
119 " detect bit %d\n", bit);
120 zassert_equal(b2[BIT_INDEX(bit)], 0,
121 "sys_bitfield_test_and_clear_bit did not"
122 " clear bit %d\n", bit);
123 zassert_false(sys_bitfield_test_and_clear_bit((mem_addr_t)b2,
124 bit), "sys_bitfield_test_and_clear_bit"
125 " erroneously detected bit %d\n", bit);
126 zassert_equal(b2[BIT_INDEX(bit)], 0,
127 "sys_bitfield_test_and_clear_bit set bit %d\n",
128 bit);
129 }
130 #endif
131
132 }
133
134 extern void *common_setup(void);
135
136 ZTEST_SUITE(bitfield, NULL, common_setup, NULL, NULL, NULL);
137
138 /**
139 * @}
140 */
141