blob: 1403f50c0499f6877561f5f4cac6409a05f305f3 [file] [log] [blame]
Brian Carlstrom413e89f2013-10-21 23:53:49 -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
Ian Rogers700a4022014-05-19 16:49:03 -070017#include <memory>
18
Brian Carlstromba150c32013-08-27 17:31:03 -070019#include "bit_vector.h"
20#include "gtest/gtest.h"
Brian Carlstrom413e89f2013-10-21 23:53:49 -070021
22namespace art {
23
24TEST(BitVector, Test) {
25 const size_t kBits = 32;
26
27 BitVector bv(kBits, false, Allocator::GetMallocAllocator());
28 EXPECT_EQ(1U, bv.GetStorageSize());
Ian Rogersef7d42f2014-01-06 12:55:46 -080029 EXPECT_EQ(sizeof(uint32_t), bv.GetSizeOf());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070030 EXPECT_FALSE(bv.IsExpandable());
31
Brian Carlstromba150c32013-08-27 17:31:03 -070032 EXPECT_EQ(0U, bv.NumSetBits());
Vladimir Markod3c5beb2014-04-11 16:32:51 +010033 EXPECT_EQ(0U, bv.NumSetBits(1));
34 EXPECT_EQ(0U, bv.NumSetBits(kBits));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070035 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
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010041 EXPECT_TRUE(bv.Indexes().begin().Done());
42 EXPECT_TRUE(bv.Indexes().begin() == bv.Indexes().end());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070043
44 bv.SetBit(0);
45 bv.SetBit(kBits - 1);
Brian Carlstromba150c32013-08-27 17:31:03 -070046 EXPECT_EQ(2U, bv.NumSetBits());
Vladimir Markod3c5beb2014-04-11 16:32:51 +010047 EXPECT_EQ(1U, bv.NumSetBits(1));
48 EXPECT_EQ(2U, bv.NumSetBits(kBits));
Brian Carlstrom413e89f2013-10-21 23:53:49 -070049 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
Vladimir Markoa5b8fde2014-05-23 15:16:44 +010057 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());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070065}
66
67TEST(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());
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 EXPECT_EQ(kWords * sizeof(uint32_t), bv.GetSizeOf());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070076 EXPECT_EQ(bits, bv.GetRawStorage());
Brian Carlstromba150c32013-08-27 17:31:03 -070077 EXPECT_EQ(0U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070078
79 bv.SetBit(8);
Brian Carlstromba150c32013-08-27 17:31:03 -070080 EXPECT_EQ(1U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070081 EXPECT_EQ(0x00000100U, bv.GetRawStorageWord(0));
82 EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1));
Brian Carlstromba150c32013-08-27 17:31:03 -070083 EXPECT_EQ(1U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070084
85 bv.SetBit(16);
Brian Carlstromba150c32013-08-27 17:31:03 -070086 EXPECT_EQ(2U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070087 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
88 EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1));
Brian Carlstromba150c32013-08-27 17:31:03 -070089 EXPECT_EQ(2U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070090
91 bv.SetBit(32);
Brian Carlstromba150c32013-08-27 17:31:03 -070092 EXPECT_EQ(3U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070093 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
94 EXPECT_EQ(0x00000001U, bv.GetRawStorageWord(1));
Brian Carlstromba150c32013-08-27 17:31:03 -070095 EXPECT_EQ(3U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070096
97 bv.SetBit(48);
Brian Carlstromba150c32013-08-27 17:31:03 -070098 EXPECT_EQ(4U, bv.NumSetBits());
Brian Carlstrom413e89f2013-10-21 23:53:49 -070099 EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0));
100 EXPECT_EQ(0x00010001U, bv.GetRawStorageWord(1));
Brian Carlstromba150c32013-08-27 17:31:03 -0700101 EXPECT_EQ(4U, bv.NumSetBits());
102
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100103 EXPECT_EQ(0U, bv.NumSetBits(1));
Brian Carlstromba150c32013-08-27 17:31:03 -0700104
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100105 EXPECT_EQ(0U, bv.NumSetBits(8));
Brian Carlstromba150c32013-08-27 17:31:03 -0700106 EXPECT_EQ(1U, bv.NumSetBits(9));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100107 EXPECT_EQ(1U, bv.NumSetBits(10));
Brian Carlstromba150c32013-08-27 17:31:03 -0700108
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100109 EXPECT_EQ(1U, bv.NumSetBits(16));
Brian Carlstromba150c32013-08-27 17:31:03 -0700110 EXPECT_EQ(2U, bv.NumSetBits(17));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100111 EXPECT_EQ(2U, bv.NumSetBits(18));
Brian Carlstromba150c32013-08-27 17:31:03 -0700112
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100113 EXPECT_EQ(2U, bv.NumSetBits(32));
Brian Carlstromba150c32013-08-27 17:31:03 -0700114 EXPECT_EQ(3U, bv.NumSetBits(33));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100115 EXPECT_EQ(3U, bv.NumSetBits(34));
Brian Carlstromba150c32013-08-27 17:31:03 -0700116
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100117 EXPECT_EQ(3U, bv.NumSetBits(48));
Brian Carlstromba150c32013-08-27 17:31:03 -0700118 EXPECT_EQ(4U, bv.NumSetBits(49));
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100119 EXPECT_EQ(4U, bv.NumSetBits(50));
Brian Carlstromba150c32013-08-27 17:31:03 -0700120
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100121 EXPECT_EQ(4U, bv.NumSetBits(64));
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700122}
123
Vladimir Marko4812d432014-03-11 12:42:25 +0000124TEST(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
Brian Carlstrom413e89f2013-10-21 23:53:49 -0700143} // namespace art