blob: fa663121a8a690110528dd27d8a2afc54a2b9c1e [file] [log] [blame]
Dan Gohmancb89afc2010-01-05 15:04:49 +00001//===- 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 Gohman3f5e9152010-04-30 20:50:28 +000010// Some of these tests fail on PowerPC for unknown reasons.
11#ifndef __ppc__
12
Dan Gohmancb89afc2010-01-05 15:04:49 +000013#include "llvm/ADT/BitVector.h"
14#include "gtest/gtest.h"
15
16using namespace llvm;
17
18namespace {
19
20TEST(BitVectorTest, TrivialOperation) {
21 BitVector Vec;
22 EXPECT_EQ(0U, Vec.count());
23 EXPECT_EQ(0U, Vec.size());
24 EXPECT_FALSE(Vec.any());
Dan Gohmanfab4c9e2010-09-27 15:48:37 +000025 EXPECT_TRUE(Vec.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +000026 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 Gohmanfab4c9e2010-09-27 15:48:37 +000033 EXPECT_TRUE(Vec.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +000034 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 Gohmanfab4c9e2010-09-27 15:48:37 +000041 EXPECT_FALSE(Vec.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +000042 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 Gohmanfab4c9e2010-09-27 15:48:37 +000049 EXPECT_FALSE(Inv.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +000050 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 Gohman3f5e9152010-04-30 20:50:28 +000065 Vec.resize(57, false);
Dan Gohmancb89afc2010-01-05 15:04:49 +000066 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 Gohman3f5e9152010-04-30 20:50:28 +000076 EXPECT_FALSE(Vec[56]);
77 Vec.resize(61, false);
Dan Gohmancb89afc2010-01-05 15:04:49 +000078
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 Gohmanfab4c9e2010-09-27 15:48:37 +0000130 EXPECT_FALSE(Vec.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +0000131 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 Gohmanfab4c9e2010-09-27 15:48:37 +0000138 EXPECT_TRUE(Inv.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +0000139 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 Gohmanfab4c9e2010-09-27 15:48:37 +0000146 EXPECT_TRUE(Vec.all());
Dan Gohmancb89afc2010-01-05 15:04:49 +0000147 EXPECT_TRUE(Vec.none());
148 EXPECT_TRUE(Vec.empty());
149}
150
Dan Gohmane7962c92010-02-10 05:54:04 +0000151TEST(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 Kramerc9e31cc2010-02-10 13:34:02 +0000167 EXPECT_EQ(4U, A.count());
168 EXPECT_EQ(50U, A.size());
Dan Gohmane7962c92010-02-10 05:54:04 +0000169
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 Kramerc9e31cc2010-02-10 13:34:02 +0000177 EXPECT_EQ(2U, A.count());
178 EXPECT_EQ(50U, A.size());
Dan Gohmane7962c92010-02-10 05:54:04 +0000179
180 B.resize(100);
181 B.set();
182
183 A ^= B;
184 EXPECT_TRUE(A.test(2));
185 EXPECT_TRUE(A.test(7));
Benjamin Kramerc9e31cc2010-02-10 13:34:02 +0000186 EXPECT_EQ(98U, A.count());
187 EXPECT_EQ(100U, A.size());
Dan Gohmancb89afc2010-01-05 15:04:49 +0000188}
Dan Gohmane7962c92010-02-10 05:54:04 +0000189
Dan Gohman3f5e9152010-04-30 20:50:28 +0000190TEST(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 Gohmane7962c92010-02-10 05:54:04 +0000199}
200
Dale Johannesence97b752010-02-09 22:15:27 +0000201#endif