Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/BitVectorTest.cpp - BitVector tests --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Dan Gohman | 3f5e915 | 2010-04-30 20:50:28 +0000 | [diff] [blame] | 10 | // Some of these tests fail on PowerPC for unknown reasons. |
| 11 | #ifndef __ppc__ |
| 12 | |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/BitVector.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | TEST(BitVectorTest, TrivialOperation) { |
| 21 | BitVector Vec; |
| 22 | EXPECT_EQ(0U, Vec.count()); |
| 23 | EXPECT_EQ(0U, Vec.size()); |
| 24 | EXPECT_FALSE(Vec.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 25 | EXPECT_TRUE(Vec.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 26 | EXPECT_TRUE(Vec.none()); |
| 27 | EXPECT_TRUE(Vec.empty()); |
| 28 | |
| 29 | Vec.resize(5, true); |
| 30 | EXPECT_EQ(5U, Vec.count()); |
| 31 | EXPECT_EQ(5U, Vec.size()); |
| 32 | EXPECT_TRUE(Vec.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 33 | EXPECT_TRUE(Vec.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 34 | EXPECT_FALSE(Vec.none()); |
| 35 | EXPECT_FALSE(Vec.empty()); |
| 36 | |
| 37 | Vec.resize(11); |
| 38 | EXPECT_EQ(5U, Vec.count()); |
| 39 | EXPECT_EQ(11U, Vec.size()); |
| 40 | EXPECT_TRUE(Vec.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 41 | EXPECT_FALSE(Vec.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 42 | EXPECT_FALSE(Vec.none()); |
| 43 | EXPECT_FALSE(Vec.empty()); |
| 44 | |
| 45 | BitVector Inv = ~Vec; |
| 46 | EXPECT_EQ(6U, Inv.count()); |
| 47 | EXPECT_EQ(11U, Inv.size()); |
| 48 | EXPECT_TRUE(Inv.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 49 | EXPECT_FALSE(Inv.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 50 | EXPECT_FALSE(Inv.none()); |
| 51 | EXPECT_FALSE(Inv.empty()); |
| 52 | |
| 53 | EXPECT_FALSE(Inv == Vec); |
| 54 | EXPECT_TRUE(Inv != Vec); |
| 55 | Vec = ~Vec; |
| 56 | EXPECT_TRUE(Inv == Vec); |
| 57 | EXPECT_FALSE(Inv != Vec); |
| 58 | |
| 59 | // Add some "interesting" data to Vec. |
| 60 | Vec.resize(23, true); |
| 61 | Vec.resize(25, false); |
| 62 | Vec.resize(26, true); |
| 63 | Vec.resize(29, false); |
| 64 | Vec.resize(33, true); |
Dan Gohman | 3f5e915 | 2010-04-30 20:50:28 +0000 | [diff] [blame] | 65 | Vec.resize(57, false); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 66 | unsigned Count = 0; |
| 67 | for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) { |
| 68 | ++Count; |
| 69 | EXPECT_TRUE(Vec[i]); |
| 70 | EXPECT_TRUE(Vec.test(i)); |
| 71 | } |
| 72 | EXPECT_EQ(Count, Vec.count()); |
| 73 | EXPECT_EQ(Count, 23u); |
| 74 | EXPECT_FALSE(Vec[0]); |
| 75 | EXPECT_TRUE(Vec[32]); |
Dan Gohman | 3f5e915 | 2010-04-30 20:50:28 +0000 | [diff] [blame] | 76 | EXPECT_FALSE(Vec[56]); |
| 77 | Vec.resize(61, false); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 78 | |
| 79 | BitVector Copy = Vec; |
| 80 | BitVector Alt(3, false); |
| 81 | Alt.resize(6, true); |
| 82 | std::swap(Alt, Vec); |
| 83 | EXPECT_TRUE(Copy == Alt); |
| 84 | EXPECT_TRUE(Vec.size() == 6); |
| 85 | EXPECT_TRUE(Vec.count() == 3); |
| 86 | EXPECT_TRUE(Vec.find_first() == 3); |
| 87 | std::swap(Copy, Vec); |
| 88 | |
| 89 | // Add some more "interesting" data. |
| 90 | Vec.resize(68, true); |
| 91 | Vec.resize(78, false); |
| 92 | Vec.resize(89, true); |
| 93 | Vec.resize(90, false); |
| 94 | Vec.resize(91, true); |
| 95 | Vec.resize(130, false); |
| 96 | Count = 0; |
| 97 | for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) { |
| 98 | ++Count; |
| 99 | EXPECT_TRUE(Vec[i]); |
| 100 | EXPECT_TRUE(Vec.test(i)); |
| 101 | } |
| 102 | EXPECT_EQ(Count, Vec.count()); |
| 103 | EXPECT_EQ(Count, 42u); |
| 104 | EXPECT_FALSE(Vec[0]); |
| 105 | EXPECT_TRUE(Vec[32]); |
| 106 | EXPECT_FALSE(Vec[60]); |
| 107 | EXPECT_FALSE(Vec[129]); |
| 108 | |
| 109 | Vec.flip(60); |
| 110 | EXPECT_TRUE(Vec[60]); |
| 111 | EXPECT_EQ(Count + 1, Vec.count()); |
| 112 | Vec.flip(60); |
| 113 | EXPECT_FALSE(Vec[60]); |
| 114 | EXPECT_EQ(Count, Vec.count()); |
| 115 | |
| 116 | Vec.reset(32); |
| 117 | EXPECT_FALSE(Vec[32]); |
| 118 | EXPECT_EQ(Count - 1, Vec.count()); |
| 119 | Vec.set(32); |
| 120 | EXPECT_TRUE(Vec[32]); |
| 121 | EXPECT_EQ(Count, Vec.count()); |
| 122 | |
| 123 | Vec.flip(); |
| 124 | EXPECT_EQ(Vec.size() - Count, Vec.count()); |
| 125 | |
| 126 | Vec.reset(); |
| 127 | EXPECT_EQ(0U, Vec.count()); |
| 128 | EXPECT_EQ(130U, Vec.size()); |
| 129 | EXPECT_FALSE(Vec.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 130 | EXPECT_FALSE(Vec.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 131 | EXPECT_TRUE(Vec.none()); |
| 132 | EXPECT_FALSE(Vec.empty()); |
| 133 | |
| 134 | Inv = ~BitVector(); |
| 135 | EXPECT_EQ(0U, Inv.count()); |
| 136 | EXPECT_EQ(0U, Inv.size()); |
| 137 | EXPECT_FALSE(Inv.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 138 | EXPECT_TRUE(Inv.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 139 | EXPECT_TRUE(Inv.none()); |
| 140 | EXPECT_TRUE(Inv.empty()); |
| 141 | |
| 142 | Vec.clear(); |
| 143 | EXPECT_EQ(0U, Vec.count()); |
| 144 | EXPECT_EQ(0U, Vec.size()); |
| 145 | EXPECT_FALSE(Vec.any()); |
Dan Gohman | fab4c9e | 2010-09-27 15:48:37 +0000 | [diff] [blame] | 146 | EXPECT_TRUE(Vec.all()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 147 | EXPECT_TRUE(Vec.none()); |
| 148 | EXPECT_TRUE(Vec.empty()); |
| 149 | } |
| 150 | |
Dan Gohman | e7962c9 | 2010-02-10 05:54:04 +0000 | [diff] [blame] | 151 | TEST(BitVectorTest, CompoundAssignment) { |
| 152 | BitVector A; |
| 153 | A.resize(10); |
| 154 | A.set(4); |
| 155 | A.set(7); |
| 156 | |
| 157 | BitVector B; |
| 158 | B.resize(50); |
| 159 | B.set(5); |
| 160 | B.set(18); |
| 161 | |
| 162 | A |= B; |
| 163 | EXPECT_TRUE(A.test(4)); |
| 164 | EXPECT_TRUE(A.test(5)); |
| 165 | EXPECT_TRUE(A.test(7)); |
| 166 | EXPECT_TRUE(A.test(18)); |
Benjamin Kramer | c9e31cc | 2010-02-10 13:34:02 +0000 | [diff] [blame] | 167 | EXPECT_EQ(4U, A.count()); |
| 168 | EXPECT_EQ(50U, A.size()); |
Dan Gohman | e7962c9 | 2010-02-10 05:54:04 +0000 | [diff] [blame] | 169 | |
| 170 | B.resize(10); |
| 171 | B.set(); |
| 172 | B.reset(2); |
| 173 | B.reset(7); |
| 174 | A &= B; |
| 175 | EXPECT_FALSE(A.test(2)); |
| 176 | EXPECT_FALSE(A.test(7)); |
Benjamin Kramer | c9e31cc | 2010-02-10 13:34:02 +0000 | [diff] [blame] | 177 | EXPECT_EQ(2U, A.count()); |
| 178 | EXPECT_EQ(50U, A.size()); |
Dan Gohman | e7962c9 | 2010-02-10 05:54:04 +0000 | [diff] [blame] | 179 | |
| 180 | B.resize(100); |
| 181 | B.set(); |
| 182 | |
| 183 | A ^= B; |
| 184 | EXPECT_TRUE(A.test(2)); |
| 185 | EXPECT_TRUE(A.test(7)); |
Benjamin Kramer | c9e31cc | 2010-02-10 13:34:02 +0000 | [diff] [blame] | 186 | EXPECT_EQ(98U, A.count()); |
| 187 | EXPECT_EQ(100U, A.size()); |
Dan Gohman | cb89afc | 2010-01-05 15:04:49 +0000 | [diff] [blame] | 188 | } |
Dan Gohman | e7962c9 | 2010-02-10 05:54:04 +0000 | [diff] [blame] | 189 | |
Dan Gohman | 3f5e915 | 2010-04-30 20:50:28 +0000 | [diff] [blame] | 190 | TEST(BitVectorTest, ProxyIndex) { |
| 191 | BitVector Vec(3); |
| 192 | EXPECT_TRUE(Vec.none()); |
| 193 | Vec[0] = Vec[1] = Vec[2] = true; |
| 194 | EXPECT_EQ(Vec.size(), Vec.count()); |
| 195 | Vec[2] = Vec[1] = Vec[0] = false; |
| 196 | EXPECT_TRUE(Vec.none()); |
| 197 | } |
| 198 | |
Dan Gohman | e7962c9 | 2010-02-10 05:54:04 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Dale Johannesen | ce97b75 | 2010-02-09 22:15:27 +0000 | [diff] [blame] | 201 | #endif |