• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/ADT/SmallBitVectorTest.cpp - SmallBitVector 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 
10 #include "llvm/ADT/SmallBitVector.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
15 namespace {
16 
TEST(SmallBitVectorTest,TrivialOperation)17 TEST(SmallBitVectorTest, TrivialOperation) {
18   SmallBitVector Vec;
19   EXPECT_EQ(0U, Vec.count());
20   EXPECT_EQ(0U, Vec.size());
21   EXPECT_FALSE(Vec.any());
22   EXPECT_TRUE(Vec.all());
23   EXPECT_TRUE(Vec.none());
24   EXPECT_TRUE(Vec.empty());
25 
26   Vec.resize(5, true);
27   EXPECT_EQ(5U, Vec.count());
28   EXPECT_EQ(5U, Vec.size());
29   EXPECT_TRUE(Vec.any());
30   EXPECT_TRUE(Vec.all());
31   EXPECT_FALSE(Vec.none());
32   EXPECT_FALSE(Vec.empty());
33 
34   Vec.resize(11);
35   EXPECT_EQ(5U, Vec.count());
36   EXPECT_EQ(11U, Vec.size());
37   EXPECT_TRUE(Vec.any());
38   EXPECT_FALSE(Vec.all());
39   EXPECT_FALSE(Vec.none());
40   EXPECT_FALSE(Vec.empty());
41 
42   SmallBitVector Inv = ~Vec;
43   EXPECT_EQ(6U, Inv.count());
44   EXPECT_EQ(11U, Inv.size());
45   EXPECT_TRUE(Inv.any());
46   EXPECT_FALSE(Inv.all());
47   EXPECT_FALSE(Inv.none());
48   EXPECT_FALSE(Inv.empty());
49 
50   EXPECT_FALSE(Inv == Vec);
51   EXPECT_TRUE(Inv != Vec);
52   Vec = ~Vec;
53   EXPECT_TRUE(Inv == Vec);
54   EXPECT_FALSE(Inv != Vec);
55 
56   // Add some "interesting" data to Vec.
57   Vec.resize(23, true);
58   Vec.resize(25, false);
59   Vec.resize(26, true);
60   Vec.resize(29, false);
61   Vec.resize(33, true);
62   Vec.resize(57, false);
63   unsigned Count = 0;
64   for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) {
65     ++Count;
66     EXPECT_TRUE(Vec[i]);
67     EXPECT_TRUE(Vec.test(i));
68   }
69   EXPECT_EQ(Count, Vec.count());
70   EXPECT_EQ(Count, 23u);
71   EXPECT_FALSE(Vec[0]);
72   EXPECT_TRUE(Vec[32]);
73   EXPECT_FALSE(Vec[56]);
74   Vec.resize(61, false);
75 
76   SmallBitVector Copy = Vec;
77   SmallBitVector Alt(3, false);
78   Alt.resize(6, true);
79   std::swap(Alt, Vec);
80   EXPECT_TRUE(Copy == Alt);
81   EXPECT_TRUE(Vec.size() == 6);
82   EXPECT_TRUE(Vec.count() == 3);
83   EXPECT_TRUE(Vec.find_first() == 3);
84   std::swap(Copy, Vec);
85 
86   // Add some more "interesting" data.
87   Vec.resize(68, true);
88   Vec.resize(78, false);
89   Vec.resize(89, true);
90   Vec.resize(90, false);
91   Vec.resize(91, true);
92   Vec.resize(130, false);
93   Count = 0;
94   for (unsigned i = Vec.find_first(); i != -1u; i = Vec.find_next(i)) {
95     ++Count;
96     EXPECT_TRUE(Vec[i]);
97     EXPECT_TRUE(Vec.test(i));
98   }
99   EXPECT_EQ(Count, Vec.count());
100   EXPECT_EQ(Count, 42u);
101   EXPECT_FALSE(Vec[0]);
102   EXPECT_TRUE(Vec[32]);
103   EXPECT_FALSE(Vec[60]);
104   EXPECT_FALSE(Vec[129]);
105 
106   Vec.flip(60);
107   EXPECT_TRUE(Vec[60]);
108   EXPECT_EQ(Count + 1, Vec.count());
109   Vec.flip(60);
110   EXPECT_FALSE(Vec[60]);
111   EXPECT_EQ(Count, Vec.count());
112 
113   Vec.reset(32);
114   EXPECT_FALSE(Vec[32]);
115   EXPECT_EQ(Count - 1, Vec.count());
116   Vec.set(32);
117   EXPECT_TRUE(Vec[32]);
118   EXPECT_EQ(Count, Vec.count());
119 
120   Vec.flip();
121   EXPECT_EQ(Vec.size() - Count, Vec.count());
122 
123   Vec.reset();
124   EXPECT_EQ(0U, Vec.count());
125   EXPECT_EQ(130U, Vec.size());
126   EXPECT_FALSE(Vec.any());
127   EXPECT_FALSE(Vec.all());
128   EXPECT_TRUE(Vec.none());
129   EXPECT_FALSE(Vec.empty());
130 
131   Inv = ~SmallBitVector();
132   EXPECT_EQ(0U, Inv.count());
133   EXPECT_EQ(0U, Inv.size());
134   EXPECT_FALSE(Inv.any());
135   EXPECT_TRUE(Inv.all());
136   EXPECT_TRUE(Inv.none());
137   EXPECT_TRUE(Inv.empty());
138 
139   Vec.clear();
140   EXPECT_EQ(0U, Vec.count());
141   EXPECT_EQ(0U, Vec.size());
142   EXPECT_FALSE(Vec.any());
143   EXPECT_TRUE(Vec.all());
144   EXPECT_TRUE(Vec.none());
145   EXPECT_TRUE(Vec.empty());
146 }
147 
TEST(SmallBitVectorTest,CompoundAssignment)148 TEST(SmallBitVectorTest, CompoundAssignment) {
149   SmallBitVector A;
150   A.resize(10);
151   A.set(4);
152   A.set(7);
153 
154   SmallBitVector B;
155   B.resize(50);
156   B.set(5);
157   B.set(18);
158 
159   A |= B;
160   EXPECT_TRUE(A.test(4));
161   EXPECT_TRUE(A.test(5));
162   EXPECT_TRUE(A.test(7));
163   EXPECT_TRUE(A.test(18));
164   EXPECT_EQ(4U, A.count());
165   EXPECT_EQ(50U, A.size());
166 
167   B.resize(10);
168   B.set();
169   B.reset(2);
170   B.reset(7);
171   A &= B;
172   EXPECT_FALSE(A.test(2));
173   EXPECT_FALSE(A.test(7));
174   EXPECT_EQ(2U, A.count());
175   EXPECT_EQ(50U, A.size());
176 
177   B.resize(100);
178   B.set();
179 
180   A ^= B;
181   EXPECT_TRUE(A.test(2));
182   EXPECT_TRUE(A.test(7));
183   EXPECT_EQ(98U, A.count());
184   EXPECT_EQ(100U, A.size());
185 }
186 
TEST(SmallBitVectorTest,ProxyIndex)187 TEST(SmallBitVectorTest, ProxyIndex) {
188   SmallBitVector Vec(3);
189   EXPECT_TRUE(Vec.none());
190   Vec[0] = Vec[1] = Vec[2] = true;
191   EXPECT_EQ(Vec.size(), Vec.count());
192   Vec[2] = Vec[1] = Vec[0] = false;
193   EXPECT_TRUE(Vec.none());
194 }
195 
196 }
197