• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Red Hat
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <gtest/gtest.h>
25 #include "util/bitset.h"
26 
TEST(bitset,sizes)27 TEST(bitset, sizes)
28 {
29    EXPECT_EQ(sizeof(BITSET_WORD), 4);
30 
31    BITSET_DECLARE(mask32, 32);
32    BITSET_DECLARE(mask64, 64);
33    BITSET_DECLARE(mask128, 128);
34 
35    EXPECT_EQ(sizeof(mask32), 4);
36    EXPECT_EQ(sizeof(mask64), 8);
37    EXPECT_EQ(sizeof(mask128), 16);
38 }
39 
TEST(bitset,testsetclear)40 TEST(bitset, testsetclear)
41 {
42    BITSET_DECLARE(mask128, 128);
43    BITSET_ZERO(mask128);
44 
45    for (int i = 0; i < 128; i++) {
46       EXPECT_EQ(BITSET_TEST(mask128, i), false);
47       BITSET_SET(mask128, i);
48       EXPECT_EQ(BITSET_TEST(mask128, i), true);
49       BITSET_CLEAR(mask128, i);
50       EXPECT_EQ(BITSET_TEST(mask128, i), false);
51    }
52 }
53 
TEST(bitset,testsetones)54 TEST(bitset, testsetones)
55 {
56    BITSET_DECLARE(mask128, 128);
57    BITSET_ONES(mask128);
58 
59    EXPECT_EQ(BITSET_FFS(mask128), 1);
60 
61    for (int i = 0; i < 128; i++) {
62       EXPECT_EQ(BITSET_TEST(mask128, i), true);
63       BITSET_CLEAR(mask128, i);
64       EXPECT_EQ(BITSET_TEST(mask128, i), false);
65       BITSET_SET(mask128, i);
66       EXPECT_EQ(BITSET_TEST(mask128, i), true);
67    }
68 }
69 
TEST(bitset,testbasicrange)70 TEST(bitset, testbasicrange)
71 {
72    BITSET_DECLARE(mask128, 128);
73    BITSET_ZERO(mask128);
74 
75    const int max_set = 15;
76    BITSET_SET_RANGE(mask128, 0, max_set);
77    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, max_set), true);
78    EXPECT_EQ(BITSET_TEST_RANGE(mask128, max_set + 1, max_set + 15), false);
79    for (int i = 0; i < 128; i++) {
80       if (i <= max_set)
81          EXPECT_EQ(BITSET_TEST(mask128, i), true);
82       else
83          EXPECT_EQ(BITSET_TEST(mask128, i), false);
84    }
85    BITSET_CLEAR_RANGE(mask128, 0, max_set);
86    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, max_set), false);
87    for (int i = 0; i < 128; i++) {
88       EXPECT_EQ(BITSET_TEST(mask128, i), false);
89    }
90 }
91 
TEST(bitset,testbitsetffs)92 TEST(bitset, testbitsetffs)
93 {
94    BITSET_DECLARE(mask128, 128);
95    BITSET_ZERO(mask128);
96 
97    EXPECT_EQ(BITSET_FFS(mask128), 0);
98 
99    BITSET_SET(mask128, 14);
100    EXPECT_EQ(BITSET_FFS(mask128), 15);
101 
102    BITSET_SET(mask128, 28);
103    EXPECT_EQ(BITSET_FFS(mask128), 15);
104 
105    BITSET_CLEAR(mask128, 14);
106    EXPECT_EQ(BITSET_FFS(mask128), 29);
107 
108    BITSET_SET_RANGE(mask128, 14, 18);
109    EXPECT_EQ(BITSET_FFS(mask128), 15);
110 }
111 
TEST(bitset,testrangebits)112 TEST(bitset, testrangebits)
113 {
114    BITSET_DECLARE(mask128, 128);
115    BITSET_ZERO(mask128);
116 
117    BITSET_SET_RANGE(mask128, 0, 31);
118    BITSET_SET_RANGE(mask128, 32, 63);
119    BITSET_SET_RANGE(mask128, 64, 95);
120    BITSET_SET_RANGE(mask128, 96, 127);
121 
122    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, 31), true);
123    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 32, 63), true);
124    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 64, 95), true);
125    EXPECT_EQ(BITSET_TEST_RANGE(mask128, 96, 127), true);
126    for (int i = 0; i < 128; i++) {
127       EXPECT_EQ(BITSET_TEST(mask128, i), true);
128    }
129 }
130