1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr.h>
8 #include <ztest.h>
9 #include <arch/cpu.h>
10
11 #include <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 */
test_bitfield(void)34 void test_bitfield(void)
35 {
36 uint32_t b1 = 0U;
37 unsigned char b2[BITFIELD_SIZE >> 3] = { 0 };
38 unsigned int bit;
39 int ret;
40
41 TC_PRINT("twiddling bits....\n");
42
43 for (bit = 0U; bit < 32; ++bit) {
44 sys_set_bit((mem_addr_t)&b1, bit);
45
46 zassert_equal(b1, (1 << bit),
47 "sys_set_bit failed on bit %d\n", bit);
48
49 zassert_true(sys_test_bit((mem_addr_t)&b1, bit),
50 "sys_test_bit did not detect bit %d\n", bit);
51
52 sys_clear_bit((mem_addr_t)&b1, bit);
53 zassert_equal(b1, 0, "sys_clear_bit failed for bit %d\n", bit);
54
55 zassert_false(sys_test_bit((mem_addr_t)&b1, bit),
56 "sys_test_bit erroneously detected bit %d\n",
57 bit);
58
59 zassert_false(sys_test_and_set_bit((mem_addr_t)&b1, bit),
60 "sys_test_and_set_bit erroneously"
61 " detected bit %d\n", bit);
62 zassert_equal(b1, (1 << bit),
63 "sys_test_and_set_bit did not set bit %d\n",
64 bit);
65 zassert_true(sys_test_and_set_bit((mem_addr_t)&b1, bit),
66 "sys_test_and_set_bit did not detect bit %d\n",
67 bit);
68 zassert_equal(b1, (1 << bit),
69 "sys_test_and_set_bit cleared bit %d\n", bit);
70
71 zassert_true(sys_test_and_clear_bit((mem_addr_t)&b1, bit),
72 "sys_test_and_clear_bit did not detect bit %d\n",
73 bit);
74 zassert_equal(b1, 0, "sys_test_and_clear_bit did not clear"
75 " bit %d\n", bit);
76 zassert_false(sys_test_and_clear_bit((mem_addr_t)&b1, bit),
77 "sys_test_and_clear_bit erroneously detected"
78 " bit %d\n", bit);
79 zassert_equal(b1, 0, "sys_test_and_clear_bit set bit %d\n",
80 bit);
81 }
82
83 for (bit = 0U; bit < BITFIELD_SIZE; ++bit) {
84 sys_bitfield_set_bit((mem_addr_t)b2, bit);
85 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
86 "sys_bitfield_set_bit failed for bit %d\n",
87 bit);
88 zassert_true(sys_bitfield_test_bit((mem_addr_t)b2, bit),
89 "sys_bitfield_test_bit did not detect bit %d\n",
90 bit);
91
92 sys_bitfield_clear_bit((mem_addr_t)b2, bit);
93 zassert_equal(b2[BIT_INDEX(bit)], 0,
94 "sys_bitfield_clear_bit failed for bit %d\n",
95 bit);
96 zassert_false(sys_bitfield_test_bit((mem_addr_t)b2, bit),
97 "sys_bitfield_test_bit erroneously detected"
98 " bit %d\n", bit);
99
100 ret = sys_bitfield_test_and_set_bit((mem_addr_t)b2, bit);
101 zassert_false(ret, "sys_bitfield_test_and_set_bit erroneously"
102 " detected bit %d\n", bit);
103
104 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
105 "sys_bitfield_test_and_set_bit did not set"
106 " bit %d\n", bit);
107 zassert_true(sys_bitfield_test_and_set_bit((mem_addr_t)b2, bit),
108 "sys_bitfield_test_and_set_bit did not detect bit"
109 " %d\n", bit);
110 zassert_equal(b2[BIT_INDEX(bit)], BIT_VAL(bit),
111 "sys_bitfield_test_and_set_bit cleared bit %d\n",
112 bit);
113
114 zassert_true(sys_bitfield_test_and_clear_bit((mem_addr_t)b2,
115 bit), "sys_bitfield_test_and_clear_bit did not"
116 " detect bit %d\n", bit);
117 zassert_equal(b2[BIT_INDEX(bit)], 0,
118 "sys_bitfield_test_and_clear_bit did not"
119 " clear bit %d\n", bit);
120 zassert_false(sys_bitfield_test_and_clear_bit((mem_addr_t)b2,
121 bit), "sys_bitfield_test_and_clear_bit"
122 " erroneously detected bit %d\n", bit);
123 zassert_equal(b2[BIT_INDEX(bit)], 0,
124 "sys_bitfield_test_and_clear_bit set bit %d\n",
125 bit);
126 }
127 }
128
129 /**
130 * @}
131 */
132