1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "BitSet_test"
18 
19 #include <unistd.h>
20 
21 #include <android/log.h>
22 #include <gtest/gtest.h>
23 #include <utils/BitSet.h>
24 
25 namespace android {
26 
27 class BitSet32Test : public testing::Test {
28 protected:
29     BitSet32 b1;
30     BitSet32 b2;
TearDown()31     virtual void TearDown() {
32         b1.clear();
33         b2.clear();
34     }
35 };
36 
37 
TEST_F(BitSet32Test,BitWiseOr)38 TEST_F(BitSet32Test, BitWiseOr) {
39     b1.markBit(2);
40     b2.markBit(4);
41 
42     BitSet32 tmp = b1 | b2;
43     EXPECT_EQ(tmp.count(), 2u);
44     EXPECT_TRUE(tmp.hasBit(2) && tmp.hasBit(4));
45     // Check that the operator is symmetric
46     EXPECT_TRUE((b2 | b1) == (b1 | b2));
47 
48     b1 |= b2;
49     EXPECT_EQ(b1.count(), 2u);
50     EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4));
51     EXPECT_TRUE(b2.hasBit(4) && b2.count() == 1u);
52 }
TEST_F(BitSet32Test,BitWiseAnd_Disjoint)53 TEST_F(BitSet32Test, BitWiseAnd_Disjoint) {
54     b1.markBit(2);
55     b1.markBit(4);
56     b1.markBit(6);
57 
58     BitSet32 tmp = b1 & b2;
59     EXPECT_TRUE(tmp.isEmpty());
60     // Check that the operator is symmetric
61     EXPECT_TRUE((b2 & b1) == (b1 & b2));
62 
63     b2 &= b1;
64     EXPECT_TRUE(b2.isEmpty());
65     EXPECT_EQ(b1.count(), 3u);
66     EXPECT_TRUE(b1.hasBit(2) && b1.hasBit(4) && b1.hasBit(6));
67 }
68 
TEST_F(BitSet32Test,BitWiseAnd_NonDisjoint)69 TEST_F(BitSet32Test, BitWiseAnd_NonDisjoint) {
70     b1.markBit(2);
71     b1.markBit(4);
72     b1.markBit(6);
73     b2.markBit(3);
74     b2.markBit(6);
75     b2.markBit(9);
76 
77     BitSet32 tmp = b1 & b2;
78     EXPECT_EQ(tmp.count(), 1u);
79     EXPECT_TRUE(tmp.hasBit(6));
80     // Check that the operator is symmetric
81     EXPECT_TRUE((b2 & b1) == (b1 & b2));
82 
83     b1 &= b2;
84     EXPECT_EQ(b1.count(), 1u);
85     EXPECT_EQ(b2.count(), 3u);
86     EXPECT_TRUE(b2.hasBit(3) && b2.hasBit(6) && b2.hasBit(9));
87 }
88 
TEST_F(BitSet32Test,MarkFirstUnmarkedBit)89 TEST_F(BitSet32Test, MarkFirstUnmarkedBit) {
90     b1.markBit(1);
91 
92     b1.markFirstUnmarkedBit();
93     EXPECT_EQ(b1.count(), 2u);
94     EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1));
95 
96     b1.markFirstUnmarkedBit();
97     EXPECT_EQ(b1.count(), 3u);
98     EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1) && b1.hasBit(2));
99 }
100 
TEST_F(BitSet32Test,ClearFirstMarkedBit)101 TEST_F(BitSet32Test, ClearFirstMarkedBit) {
102     b1.markBit(0);
103     b1.markBit(10);
104 
105     b1.clearFirstMarkedBit();
106     EXPECT_EQ(b1.count(), 1u);
107     EXPECT_TRUE(b1.hasBit(10));
108 
109     b1.markBit(30);
110     b1.clearFirstMarkedBit();
111     EXPECT_EQ(b1.count(), 1u);
112     EXPECT_TRUE(b1.hasBit(30));
113 }
114 
TEST_F(BitSet32Test,ClearLastMarkedBit)115 TEST_F(BitSet32Test, ClearLastMarkedBit) {
116     b1.markBit(10);
117     b1.markBit(31);
118 
119     b1.clearLastMarkedBit();
120     EXPECT_EQ(b1.count(), 1u);
121     EXPECT_TRUE(b1.hasBit(10));
122 
123     b1.markBit(5);
124     b1.clearLastMarkedBit();
125     EXPECT_EQ(b1.count(), 1u);
126     EXPECT_TRUE(b1.hasBit(5));
127 }
128 
TEST_F(BitSet32Test,FillAndClear)129 TEST_F(BitSet32Test, FillAndClear) {
130     EXPECT_TRUE(b1.isEmpty());
131     for (size_t i = 0; i < 32; i++) {
132         b1.markFirstUnmarkedBit();
133     }
134     EXPECT_TRUE(b1.isFull());
135     b1.clear();
136     EXPECT_TRUE(b1.isEmpty());
137 }
138 
TEST_F(BitSet32Test,GetIndexOfBit)139 TEST_F(BitSet32Test, GetIndexOfBit) {
140     b1.markBit(1);
141     b1.markBit(4);
142     EXPECT_EQ(0U, b1.getIndexOfBit(1));
143     EXPECT_EQ(1U, b1.getIndexOfBit(4));
144     b1.markFirstUnmarkedBit();
145     EXPECT_EQ(1U, b1.getIndexOfBit(1));
146     EXPECT_EQ(2U, b1.getIndexOfBit(4));
147 }
148 
149 class BitSet64Test : public testing::Test {
150 protected:
151     BitSet64 b1;
152     BitSet64 b2;
TearDown()153     virtual void TearDown() {
154         b1.clear();
155         b2.clear();
156     }
157 };
158 
159 
TEST_F(BitSet64Test,BitWiseOr)160 TEST_F(BitSet64Test, BitWiseOr) {
161     b1.markBit(20);
162     b2.markBit(40);
163 
164     BitSet64 tmp = b1 | b2;
165     EXPECT_EQ(tmp.count(), 2u);
166     EXPECT_TRUE(tmp.hasBit(20) && tmp.hasBit(40));
167     // Check that the operator is symmetric
168     EXPECT_TRUE((b2 | b1) == (b1 | b2));
169 
170     b1 |= b2;
171     EXPECT_EQ(b1.count(), 2u);
172     EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40));
173     EXPECT_TRUE(b2.hasBit(40) && b2.count() == 1u);
174 }
TEST_F(BitSet64Test,BitWiseAnd_Disjoint)175 TEST_F(BitSet64Test, BitWiseAnd_Disjoint) {
176     b1.markBit(20);
177     b1.markBit(40);
178     b1.markBit(60);
179 
180     BitSet64 tmp = b1 & b2;
181     EXPECT_TRUE(tmp.isEmpty());
182     // Check that the operator is symmetric
183     EXPECT_TRUE((b2 & b1) == (b1 & b2));
184 
185     b2 &= b1;
186     EXPECT_TRUE(b2.isEmpty());
187     EXPECT_EQ(b1.count(), 3u);
188     EXPECT_TRUE(b1.hasBit(20) && b1.hasBit(40) && b1.hasBit(60));
189 }
190 
TEST_F(BitSet64Test,BitWiseAnd_NonDisjoint)191 TEST_F(BitSet64Test, BitWiseAnd_NonDisjoint) {
192     b1.markBit(20);
193     b1.markBit(40);
194     b1.markBit(60);
195     b2.markBit(30);
196     b2.markBit(60);
197     b2.markBit(63);
198 
199     BitSet64 tmp = b1 & b2;
200     EXPECT_EQ(tmp.count(), 1u);
201     EXPECT_TRUE(tmp.hasBit(60));
202     // Check that the operator is symmetric
203     EXPECT_TRUE((b2 & b1) == (b1 & b2));
204 
205     b1 &= b2;
206     EXPECT_EQ(b1.count(), 1u);
207     EXPECT_EQ(b2.count(), 3u);
208     EXPECT_TRUE(b2.hasBit(30) && b2.hasBit(60) && b2.hasBit(63));
209 }
210 
TEST_F(BitSet64Test,MarkFirstUnmarkedBit)211 TEST_F(BitSet64Test, MarkFirstUnmarkedBit) {
212     b1.markBit(1);
213 
214     b1.markFirstUnmarkedBit();
215     EXPECT_EQ(b1.count(), 2u);
216     EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1));
217 
218     b1.markFirstUnmarkedBit();
219     EXPECT_EQ(b1.count(), 3u);
220     EXPECT_TRUE(b1.hasBit(0) && b1.hasBit(1) && b1.hasBit(2));
221 }
222 
TEST_F(BitSet64Test,ClearFirstMarkedBit)223 TEST_F(BitSet64Test, ClearFirstMarkedBit) {
224     b1.markBit(0);
225     b1.markBit(10);
226 
227     b1.clearFirstMarkedBit();
228     EXPECT_EQ(b1.count(), 1u);
229     EXPECT_TRUE(b1.hasBit(10));
230 
231     b1.markBit(50);
232     b1.clearFirstMarkedBit();
233     EXPECT_EQ(b1.count(), 1u);
234     EXPECT_TRUE(b1.hasBit(50));
235 }
236 
TEST_F(BitSet64Test,ClearLastMarkedBit)237 TEST_F(BitSet64Test, ClearLastMarkedBit) {
238     b1.markBit(10);
239     b1.markBit(63);
240 
241     b1.clearLastMarkedBit();
242     EXPECT_EQ(b1.count(), 1u);
243     EXPECT_TRUE(b1.hasBit(10));
244 
245     b1.markBit(5);
246     b1.clearLastMarkedBit();
247     EXPECT_EQ(b1.count(), 1u);
248     EXPECT_TRUE(b1.hasBit(5));
249 }
250 
TEST_F(BitSet64Test,FillAndClear)251 TEST_F(BitSet64Test, FillAndClear) {
252     EXPECT_TRUE(b1.isEmpty());
253     for (size_t i = 0; i < 64; i++) {
254         b1.markFirstUnmarkedBit();
255     }
256     EXPECT_TRUE(b1.isFull());
257     b1.clear();
258     EXPECT_TRUE(b1.isEmpty());
259 }
260 
TEST_F(BitSet64Test,GetIndexOfBit)261 TEST_F(BitSet64Test, GetIndexOfBit) {
262     b1.markBit(10);
263     b1.markBit(40);
264     EXPECT_EQ(0U, b1.getIndexOfBit(10));
265     EXPECT_EQ(1U, b1.getIndexOfBit(40));
266     b1.markFirstUnmarkedBit();
267     EXPECT_EQ(1U, b1.getIndexOfBit(10));
268     EXPECT_EQ(2U, b1.getIndexOfBit(40));
269 }
270 
271 } // namespace android
272