Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 19 | #include "bit_vector.h" |
| 20 | #include "gtest/gtest.h" |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | TEST(BitVector, Test) { |
| 25 | const size_t kBits = 32; |
| 26 | |
| 27 | BitVector bv(kBits, false, Allocator::GetMallocAllocator()); |
| 28 | EXPECT_EQ(1U, bv.GetStorageSize()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 29 | EXPECT_EQ(sizeof(uint32_t), bv.GetSizeOf()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 30 | EXPECT_FALSE(bv.IsExpandable()); |
| 31 | |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 32 | EXPECT_EQ(0U, bv.NumSetBits()); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 33 | EXPECT_EQ(0U, bv.NumSetBits(1)); |
| 34 | EXPECT_EQ(0U, bv.NumSetBits(kBits)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 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 | |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 41 | EXPECT_TRUE(bv.Indexes().begin().Done()); |
| 42 | EXPECT_TRUE(bv.Indexes().begin() == bv.Indexes().end()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 43 | |
| 44 | bv.SetBit(0); |
| 45 | bv.SetBit(kBits - 1); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 46 | EXPECT_EQ(2U, bv.NumSetBits()); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 47 | EXPECT_EQ(1U, bv.NumSetBits(1)); |
| 48 | EXPECT_EQ(2U, bv.NumSetBits(kBits)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 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 | |
Vladimir Marko | a5b8fde | 2014-05-23 15:16:44 +0100 | [diff] [blame] | 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()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 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()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 75 | EXPECT_EQ(kWords * sizeof(uint32_t), bv.GetSizeOf()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 76 | EXPECT_EQ(bits, bv.GetRawStorage()); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 77 | EXPECT_EQ(0U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 78 | |
| 79 | bv.SetBit(8); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 80 | EXPECT_EQ(1U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 81 | EXPECT_EQ(0x00000100U, bv.GetRawStorageWord(0)); |
| 82 | EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 83 | EXPECT_EQ(1U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 84 | |
| 85 | bv.SetBit(16); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 86 | EXPECT_EQ(2U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 87 | EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0)); |
| 88 | EXPECT_EQ(0x00000000U, bv.GetRawStorageWord(1)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 89 | EXPECT_EQ(2U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 90 | |
| 91 | bv.SetBit(32); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 92 | EXPECT_EQ(3U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 93 | EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0)); |
| 94 | EXPECT_EQ(0x00000001U, bv.GetRawStorageWord(1)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 95 | EXPECT_EQ(3U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 96 | |
| 97 | bv.SetBit(48); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 98 | EXPECT_EQ(4U, bv.NumSetBits()); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 99 | EXPECT_EQ(0x00010100U, bv.GetRawStorageWord(0)); |
| 100 | EXPECT_EQ(0x00010001U, bv.GetRawStorageWord(1)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 101 | EXPECT_EQ(4U, bv.NumSetBits()); |
| 102 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 103 | EXPECT_EQ(0U, bv.NumSetBits(1)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 104 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 105 | EXPECT_EQ(0U, bv.NumSetBits(8)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 106 | EXPECT_EQ(1U, bv.NumSetBits(9)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 107 | EXPECT_EQ(1U, bv.NumSetBits(10)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 108 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 109 | EXPECT_EQ(1U, bv.NumSetBits(16)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 110 | EXPECT_EQ(2U, bv.NumSetBits(17)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 111 | EXPECT_EQ(2U, bv.NumSetBits(18)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 112 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 113 | EXPECT_EQ(2U, bv.NumSetBits(32)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 114 | EXPECT_EQ(3U, bv.NumSetBits(33)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 115 | EXPECT_EQ(3U, bv.NumSetBits(34)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 116 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 117 | EXPECT_EQ(3U, bv.NumSetBits(48)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 118 | EXPECT_EQ(4U, bv.NumSetBits(49)); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 119 | EXPECT_EQ(4U, bv.NumSetBits(50)); |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 120 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 121 | EXPECT_EQ(4U, bv.NumSetBits(64)); |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Vladimir Marko | 4812d43 | 2014-03-11 12:42:25 +0000 | [diff] [blame] | 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 | |
Brian Carlstrom | 413e89f | 2013-10-21 23:53:49 -0700 | [diff] [blame] | 143 | } // namespace art |