blob: fbcf0259509af037aea5703402a8b81b4c183da8 [file] [log] [blame]
Michael Wrightd614ee42013-05-21 14:11:34 -07001/*
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
Michael Wrightd614ee42013-05-21 14:11:34 -070019#include <unistd.h>
20
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070021#include <android/log.h>
22#include <gtest/gtest.h>
23#include <utils/BitSet.h>
24
Michael Wrightd614ee42013-05-21 14:11:34 -070025namespace android {
26
Michael Wrightbab6ea02014-03-18 17:25:20 -070027class BitSet32Test : public testing::Test {
Michael Wrightd614ee42013-05-21 14:11:34 -070028protected:
29 BitSet32 b1;
30 BitSet32 b2;
31 virtual void TearDown() {
32 b1.clear();
33 b2.clear();
34 }
35};
36
37
Michael Wrightbab6ea02014-03-18 17:25:20 -070038TEST_F(BitSet32Test, BitWiseOr) {
Michael Wrightd614ee42013-05-21 14:11:34 -070039 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}
Michael Wrightbab6ea02014-03-18 17:25:20 -070053TEST_F(BitSet32Test, BitWiseAnd_Disjoint) {
Michael Wrightd614ee42013-05-21 14:11:34 -070054 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
Michael Wrightbab6ea02014-03-18 17:25:20 -070069TEST_F(BitSet32Test, BitWiseAnd_NonDisjoint) {
Michael Wrightd614ee42013-05-21 14:11:34 -070070 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}
Michael Wrightbab6ea02014-03-18 17:25:20 -070088
Michael Wright2ec06452014-03-19 11:23:01 -070089TEST_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
101TEST_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
115TEST_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
129TEST_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
139TEST_F(BitSet32Test, GetIndexOfBit) {
140 b1.markBit(1);
141 b1.markBit(4);
Nick Kralevich3e6c4512015-08-17 18:26:13 -0700142 EXPECT_EQ(0U, b1.getIndexOfBit(1));
143 EXPECT_EQ(1U, b1.getIndexOfBit(4));
Michael Wright2ec06452014-03-19 11:23:01 -0700144 b1.markFirstUnmarkedBit();
Nick Kralevich3e6c4512015-08-17 18:26:13 -0700145 EXPECT_EQ(1U, b1.getIndexOfBit(1));
146 EXPECT_EQ(2U, b1.getIndexOfBit(4));
Michael Wright2ec06452014-03-19 11:23:01 -0700147}
148
Michael Wrightbab6ea02014-03-18 17:25:20 -0700149class BitSet64Test : public testing::Test {
150protected:
151 BitSet64 b1;
152 BitSet64 b2;
153 virtual void TearDown() {
154 b1.clear();
155 b2.clear();
156 }
157};
158
159
160TEST_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}
175TEST_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
191TEST_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}
Michael Wright2ec06452014-03-19 11:23:01 -0700210
211TEST_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
223TEST_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
237TEST_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
251TEST_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
261TEST_F(BitSet64Test, GetIndexOfBit) {
262 b1.markBit(10);
263 b1.markBit(40);
Nick Kralevich3e6c4512015-08-17 18:26:13 -0700264 EXPECT_EQ(0U, b1.getIndexOfBit(10));
265 EXPECT_EQ(1U, b1.getIndexOfBit(40));
Michael Wright2ec06452014-03-19 11:23:01 -0700266 b1.markFirstUnmarkedBit();
Nick Kralevich3e6c4512015-08-17 18:26:13 -0700267 EXPECT_EQ(1U, b1.getIndexOfBit(10));
268 EXPECT_EQ(2U, b1.getIndexOfBit(40));
Michael Wright2ec06452014-03-19 11:23:01 -0700269}
270
Michael Wrightd614ee42013-05-21 14:11:34 -0700271} // namespace android