1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Red Hat
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <gtest/gtest.h>
25bf215546Sopenharmony_ci#include "util/bitset.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ciTEST(bitset, sizes)
28bf215546Sopenharmony_ci{
29bf215546Sopenharmony_ci   EXPECT_EQ(sizeof(BITSET_WORD), 4);
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci   BITSET_DECLARE(mask32, 32);
32bf215546Sopenharmony_ci   BITSET_DECLARE(mask64, 64);
33bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   EXPECT_EQ(sizeof(mask32), 4);
36bf215546Sopenharmony_ci   EXPECT_EQ(sizeof(mask64), 8);
37bf215546Sopenharmony_ci   EXPECT_EQ(sizeof(mask128), 16);
38bf215546Sopenharmony_ci}
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ciTEST(bitset, test_set_clear)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
43bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   for (int i = 0; i < 128; i++) {
46bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
47bf215546Sopenharmony_ci      BITSET_SET(mask128, i);
48bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
49bf215546Sopenharmony_ci      BITSET_CLEAR(mask128, i);
50bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
51bf215546Sopenharmony_ci   }
52bf215546Sopenharmony_ci}
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ciTEST(bitset, test_set_ones)
55bf215546Sopenharmony_ci{
56bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
57bf215546Sopenharmony_ci   BITSET_ONES(mask128);
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 1);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   for (int i = 0; i < 128; i++) {
62bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
63bf215546Sopenharmony_ci      BITSET_CLEAR(mask128, i);
64bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
65bf215546Sopenharmony_ci      BITSET_SET(mask128, i);
66bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
67bf215546Sopenharmony_ci   }
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ciTEST(bitset, test_basic_range)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
73bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   const int max_set = 15;
76bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 0, max_set);
77bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE_INSIDE_WORD(mask128, 0, max_set), true);
78bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE_INSIDE_WORD(mask128, max_set + 1, max_set + 15), false);
79bf215546Sopenharmony_ci   for (int i = 0; i < 128; i++) {
80bf215546Sopenharmony_ci      if (i <= max_set)
81bf215546Sopenharmony_ci         EXPECT_EQ(BITSET_TEST(mask128, i), true);
82bf215546Sopenharmony_ci      else
83bf215546Sopenharmony_ci         EXPECT_EQ(BITSET_TEST(mask128, i), false);
84bf215546Sopenharmony_ci   }
85bf215546Sopenharmony_ci   BITSET_CLEAR_RANGE(mask128, 0, max_set);
86bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE_INSIDE_WORD(mask128, 0, max_set), false);
87bf215546Sopenharmony_ci   for (int i = 0; i < 128; i++) {
88bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ciTEST(bitset, test_bitset_ffs)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
95bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 0);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   BITSET_SET(mask128, 14);
100bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 15);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   BITSET_SET(mask128, 28);
103bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 15);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   BITSET_CLEAR(mask128, 14);
106bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 29);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 14, 18);
109bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_FFS(mask128), 15);
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ciTEST(bitset, test_range_bits)
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci   BITSET_DECLARE(mask128, 128);
115bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 0, 31);
118bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 32, 63);
119bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 64, 95);
120bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(mask128, 96, 127);
121bf215546Sopenharmony_ci   for (int i = 0; i < 128; i++) {
122bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
123bf215546Sopenharmony_ci   }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
126bf215546Sopenharmony_ci   BITSET_SET_RANGE(mask128, 20, 80);
127bf215546Sopenharmony_ci   for (int i = 0; i <= 19; i++)
128bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
129bf215546Sopenharmony_ci   for (int i = 20; i <= 80; i++)
130bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
131bf215546Sopenharmony_ci   for (int i = 81; i <= 127; i++)
132bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   BITSET_ZERO(mask128);
135bf215546Sopenharmony_ci   BITSET_SET(mask128, 20);
136bf215546Sopenharmony_ci   BITSET_SET(mask128, 80);
137bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, 128), true);
138bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, 19), false);
139bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 21, 79), false);
140bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 81, 127), false);
141bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, 79), true);
142bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 21, 128), true);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   BITSET_ONES(mask128);
145bf215546Sopenharmony_ci   BITSET_CLEAR_RANGE(mask128, 20, 80);
146bf215546Sopenharmony_ci   for (int i = 0; i <= 19; i++)
147bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
148bf215546Sopenharmony_ci   for (int i = 20; i <= 80; i++)
149bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), false);
150bf215546Sopenharmony_ci   for (int i = 81; i <= 127; i++)
151bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(mask128, i), true);
152bf215546Sopenharmony_ci}
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ciTEST(bitset, test_and)
155bf215546Sopenharmony_ci{
156bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
157bf215546Sopenharmony_ci   BITSET_DECLARE(a, 128);
158bf215546Sopenharmony_ci   BITSET_DECLARE(b, 128);
159bf215546Sopenharmony_ci   BITSET_ZERO(r);
160bf215546Sopenharmony_ci   BITSET_ZERO(a);
161bf215546Sopenharmony_ci   BITSET_ZERO(b);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   BITSET_AND(r, a, b);
164bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
165bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
166bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
167bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(a, 32, 63);
171bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(b, 96, 127);
172bf215546Sopenharmony_ci   BITSET_AND(r, a, b);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
175bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
176bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
177bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   BITSET_SET(a, 80);
181bf215546Sopenharmony_ci   BITSET_SET(b, 80);
182bf215546Sopenharmony_ci   BITSET_AND(r, a, b);
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 80), true);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
187bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
188bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), true);
189bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
190bf215546Sopenharmony_ci}
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ciTEST(bitset, test_or)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
195bf215546Sopenharmony_ci   BITSET_DECLARE(a, 128);
196bf215546Sopenharmony_ci   BITSET_DECLARE(b, 128);
197bf215546Sopenharmony_ci   BITSET_ZERO(r);
198bf215546Sopenharmony_ci   BITSET_ZERO(a);
199bf215546Sopenharmony_ci   BITSET_ZERO(b);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci   BITSET_OR(r, a, b);
202bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
203bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
204bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
205bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(a, 32, 63);
209bf215546Sopenharmony_ci   BITSET_SET_RANGE_INSIDE_WORD(b, 96, 127);
210bf215546Sopenharmony_ci   BITSET_OR(r, a, b);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
213bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
214bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
215bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), true);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   BITSET_SET(a, 80);
219bf215546Sopenharmony_ci   BITSET_OR(r, a, b);
220bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 80), true);
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   BITSET_SET(b, 81);
223bf215546Sopenharmony_ci   BITSET_OR(r, a, b);
224bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 81), true);
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
227bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
228bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), true);
229bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), true);
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ciTEST(bitset, test_not)
233bf215546Sopenharmony_ci{
234bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
235bf215546Sopenharmony_ci   BITSET_ZERO(r);
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
238bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
239bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
240bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   BITSET_NOT(r);
243bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), true);
244bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
245bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), true);
246bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), true);
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   BITSET_CLEAR_RANGE(r, 32, 63);
249bf215546Sopenharmony_ci   BITSET_NOT(r);
250bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
251bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
252bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
253bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
254bf215546Sopenharmony_ci}
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ciTEST(bitset, test_shr_zero)
257bf215546Sopenharmony_ci{
258bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   BITSET_ZERO(r);
261bf215546Sopenharmony_ci   BITSET_SET(r, 127);
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci   BITSET_SHR(r, 0);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 127), true);
266bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
267bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
268bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
269bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), true);
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ciTEST(bitset, test_shl_zero)
273bf215546Sopenharmony_ci{
274bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
275bf215546Sopenharmony_ci
276bf215546Sopenharmony_ci   BITSET_ZERO(r);
277bf215546Sopenharmony_ci   BITSET_SET(r, 0);
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ci   BITSET_SHL(r, 0);
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 0), true);
282bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), true);
283bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
284bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
285bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
286bf215546Sopenharmony_ci}
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ciTEST(bitset, test_shr_walking_bit)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   BITSET_ZERO(r);
293bf215546Sopenharmony_ci   BITSET_SET(r, 127);
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   for (int i = 127; i >= 0; i--) {
296bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(r, i), true);
297bf215546Sopenharmony_ci      BITSET_SHR(r, 1);
298bf215546Sopenharmony_ci   }
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
301bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
302bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
303bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
304bf215546Sopenharmony_ci}
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ciTEST(bitset, test_shl_walking_bit)
307bf215546Sopenharmony_ci{
308bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   BITSET_ZERO(r);
311bf215546Sopenharmony_ci   BITSET_SET(r, 0);
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   for (unsigned int i = 0; i < 128; i++) {
314bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(r, i), true);
315bf215546Sopenharmony_ci      BITSET_SHL(r, 1);
316bf215546Sopenharmony_ci   }
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
319bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
320bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), false);
321bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
322bf215546Sopenharmony_ci}
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ciTEST(bitset, test_shr_multiple_words)
325bf215546Sopenharmony_ci{
326bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci   BITSET_ZERO(r);
329bf215546Sopenharmony_ci   BITSET_SET(r, 127);
330bf215546Sopenharmony_ci   BITSET_SHR(r, 50);
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 127), false);
333bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 77), true);
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   BITSET_ZERO(r);
337bf215546Sopenharmony_ci   BITSET_SET(r, 127);
338bf215546Sopenharmony_ci   BITSET_SHR(r, 80);
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 127), false);
341bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 47), true);
342bf215546Sopenharmony_ci
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci   BITSET_ZERO(r);
345bf215546Sopenharmony_ci   BITSET_SET(r, 127);
346bf215546Sopenharmony_ci   BITSET_SHR(r, 126);
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 127), false);
349bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 1), true);
350bf215546Sopenharmony_ci}
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ciTEST(bitset, test_shl_multiple_words)
353bf215546Sopenharmony_ci{
354bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci   BITSET_ZERO(r);
357bf215546Sopenharmony_ci   BITSET_SET(r, 0);
358bf215546Sopenharmony_ci   BITSET_SHL(r, 50);
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 0), false);
361bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 50), true);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   BITSET_ZERO(r);
365bf215546Sopenharmony_ci   BITSET_SET(r, 0);
366bf215546Sopenharmony_ci   BITSET_SHL(r, 80);
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 0), false);
369bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 80), true);
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci
372bf215546Sopenharmony_ci   BITSET_ZERO(r);
373bf215546Sopenharmony_ci   BITSET_SET(r, 0);
374bf215546Sopenharmony_ci   BITSET_SHL(r, 126);
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 0), false);
377bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 126), true);
378bf215546Sopenharmony_ci}
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ciTEST(bitset, test_shr_two_words)
381bf215546Sopenharmony_ci{
382bf215546Sopenharmony_ci   BITSET_DECLARE(r, 64);
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   BITSET_ZERO(r);
385bf215546Sopenharmony_ci   BITSET_SET(r, 63);
386bf215546Sopenharmony_ci   BITSET_SHR(r, 50);
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 63), false);
389bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 13), true);
390bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), true);
391bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), false);
392bf215546Sopenharmony_ci}
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ciTEST(bitset, test_shl_two_words)
395bf215546Sopenharmony_ci{
396bf215546Sopenharmony_ci   BITSET_DECLARE(r, 64);
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci   BITSET_ZERO(r);
399bf215546Sopenharmony_ci   BITSET_SET(r, 0);
400bf215546Sopenharmony_ci   BITSET_SHL(r, 50);
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 0), false);
403bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 50), true);
404bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
405bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
406bf215546Sopenharmony_ci}
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ciTEST(bitset, test_setrange_across_word_boundary)
409bf215546Sopenharmony_ci{
410bf215546Sopenharmony_ci   BITSET_DECLARE(r, 128);
411bf215546Sopenharmony_ci   BITSET_ZERO(r);
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_ci   BITSET_SET_RANGE(r, 62, 65);
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 0, 31), false);
416bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 32, 63), true);
417bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 64, 95), true);
418bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST_RANGE(r, 96, 127), false);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 61), false);
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci   for (int i = 62; i <= 65; i++)
423bf215546Sopenharmony_ci      EXPECT_EQ(BITSET_TEST(r, i), true);
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci   EXPECT_EQ(BITSET_TEST(r, 66), false);
426bf215546Sopenharmony_ci}
427