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 #include <memory>
18
19 #include "bit_vector.h"
20 #include "gtest/gtest.h"
21
22 namespace art {
23
TEST(BitVector,Test)24 TEST(BitVector, Test) {
25 const size_t kBits = 32;
26
27 BitVector bv(kBits, false, Allocator::GetMallocAllocator());
28 EXPECT_EQ(1U, bv.GetStorageSize());
29 EXPECT_EQ(sizeof(uint32_t), bv.GetSizeOf());
30 EXPECT_FALSE(bv.IsExpandable());
31
32 EXPECT_EQ(0U, bv.NumSetBits());
33 EXPECT_EQ(0U, bv.NumSetBits(1));
34 EXPECT_EQ(0U, bv.NumSetBits(kBits));
35 for (size_t i = 0; i < kBits; i++) {
36 EXPECT_FALSE(bv.IsBitSet(i));
37 }
38 EXPECT_EQ(0U, bv.GetRawStorageWord(0));
39 EXPECT_EQ(0U, *bv.GetRawStorage());
40
41 EXPECT_TRUE(bv.Indexes().begin().Done());
42 EXPECT_TRUE(bv.Indexes().begin() == bv.Indexes().end());
43
44 bv.SetBit(0);
45 bv.SetBit(kBits - 1);
46 EXPECT_EQ(2U, bv.NumSetBits());
47 EXPECT_EQ(1U, bv.NumSetBits(1));
48 EXPECT_EQ(2U, bv.NumSetBits(kBits));
49 EXPECT_TRUE(bv.IsBitSet(0));
50 for (size_t i = 1; i < kBits - 1; i++) {
51 EXPECT_FALSE(bv.IsBitSet(i));
52 }
53 EXPECT_TRUE(bv.IsBitSet(kBits - 1));
54 EXPECT_EQ(0x80000001U, bv.GetRawStorageWord(0));
55 EXPECT_EQ(0x80000001U, *bv.GetRawStorage());
56
57 BitVector::IndexIterator iterator = bv.Indexes().begin();
58 EXPECT_TRUE(iterator != bv.Indexes().end());
59 EXPECT_EQ(0, *iterator);
60 ++iterator;
61 EXPECT_TRUE(iterator != bv.Indexes().end());
62 EXPECT_EQ(static_cast<int>(kBits - 1), *iterator);
63 ++iterator;
64 EXPECT_TRUE(iterator == bv.Indexes().end());
65 }
66
TEST(BitVector,NoopAllocator)67 TEST(BitVector, NoopAllocator) {
68 const uint32_t kWords = 2;
69
70 uint32_t bits[kWords];
71 memset(bits, 0, sizeof(bits));
72
73 BitVector bv(0U, false, Allocator::GetNoopAllocator(), kWords, bits);
74 EXPECT_EQ(kWords, bv.GetStorageSize());
75 EXPECT_EQ(kWords * sizeof(uint32_t), bv.GetSizeOf());
76 EXPECT_EQ(bits, bv.GetRawStorage());
77 EXPECT_EQ(0U, bv.NumSetBits());
78
79 bv.SetBit(8);
80 EXPECT_EQ(1U, bv.NumSetBits());
81 EXPECT_EQ(0x00000100U, bv.GetRawStorageWord(0));
82 EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1));
83 EXPECT_EQ(1U, bv.NumSetBits());
84
85 bv.SetBit(16);
86 EXPECT_EQ(2U, bv.NumSetBits());
87 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
88 EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1));
89 EXPECT_EQ(2U, bv.NumSetBits());
90
91 bv.SetBit(32);
92 EXPECT_EQ(3U, bv.NumSetBits());
93 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
94 EXPECT_EQ(0x00000001U, bv.GetRawStorageWord(1));
95 EXPECT_EQ(3U, bv.NumSetBits());
96
97 bv.SetBit(48);
98 EXPECT_EQ(4U, bv.NumSetBits());
99 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
100 EXPECT_EQ(0x00010001U, bv.GetRawStorageWord(1));
101 EXPECT_EQ(4U, bv.NumSetBits());
102
103 EXPECT_EQ(0U, bv.NumSetBits(1));
104
105 EXPECT_EQ(0U, bv.NumSetBits(8));
106 EXPECT_EQ(1U, bv.NumSetBits(9));
107 EXPECT_EQ(1U, bv.NumSetBits(10));
108
109 EXPECT_EQ(1U, bv.NumSetBits(16));
110 EXPECT_EQ(2U, bv.NumSetBits(17));
111 EXPECT_EQ(2U, bv.NumSetBits(18));
112
113 EXPECT_EQ(2U, bv.NumSetBits(32));
114 EXPECT_EQ(3U, bv.NumSetBits(33));
115 EXPECT_EQ(3U, bv.NumSetBits(34));
116
117 EXPECT_EQ(3U, bv.NumSetBits(48));
118 EXPECT_EQ(4U, bv.NumSetBits(49));
119 EXPECT_EQ(4U, bv.NumSetBits(50));
120
121 EXPECT_EQ(4U, bv.NumSetBits(64));
122 }
123
TEST(BitVector,SetInitialBits)124 TEST(BitVector, SetInitialBits) {
125 const uint32_t kWords = 2;
126
127 uint32_t bits[kWords];
128 memset(bits, 0, sizeof(bits));
129
130 BitVector bv(0U, false, Allocator::GetNoopAllocator(), kWords, bits);
131 bv.SetInitialBits(0u);
132 EXPECT_EQ(0u, bv.NumSetBits());
133 bv.SetInitialBits(1u);
134 EXPECT_EQ(1u, bv.NumSetBits());
135 bv.SetInitialBits(32u);
136 EXPECT_EQ(32u, bv.NumSetBits());
137 bv.SetInitialBits(63u);
138 EXPECT_EQ(63u, bv.NumSetBits());
139 bv.SetInitialBits(64u);
140 EXPECT_EQ(64u, bv.NumSetBits());
141 }
142
143 } // namespace art
144