[obj2yaml] - Stop triggering UB when dumping corrupted strings.
[llvm-complete.git] / unittests / ADT / SparseBitVectorTest.cpp
blobdb8d58a11992466510a17d4b0e560c7551ab9bce
1 //===- llvm/unittest/ADT/SparseBitVectorTest.cpp - SparseBitVector tests --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SparseBitVector.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
16 TEST(SparseBitVectorTest, TrivialOperation) {
17 SparseBitVector<> Vec;
18 EXPECT_EQ(0U, Vec.count());
19 EXPECT_FALSE(Vec.test(17));
20 Vec.set(5);
21 EXPECT_TRUE(Vec.test(5));
22 EXPECT_FALSE(Vec.test(17));
23 Vec.reset(6);
24 EXPECT_TRUE(Vec.test(5));
25 EXPECT_FALSE(Vec.test(6));
26 Vec.reset(5);
27 EXPECT_FALSE(Vec.test(5));
28 EXPECT_TRUE(Vec.test_and_set(17));
29 EXPECT_FALSE(Vec.test_and_set(17));
30 EXPECT_TRUE(Vec.test(17));
31 Vec.clear();
32 EXPECT_FALSE(Vec.test(17));
34 Vec.set(5);
35 const SparseBitVector<> ConstVec = Vec;
36 EXPECT_TRUE(ConstVec.test(5));
37 EXPECT_FALSE(ConstVec.test(17));
39 Vec.set(1337);
40 EXPECT_TRUE(Vec.test(1337));
41 Vec = ConstVec;
42 EXPECT_FALSE(Vec.test(1337));
44 Vec.set(1337);
45 EXPECT_FALSE(Vec.empty());
46 SparseBitVector<> MovedVec(std::move(Vec));
47 EXPECT_TRUE(Vec.empty());
48 EXPECT_TRUE(MovedVec.test(5));
49 EXPECT_TRUE(MovedVec.test(1337));
51 Vec = std::move(MovedVec);
52 EXPECT_TRUE(MovedVec.empty());
53 EXPECT_FALSE(Vec.empty());
56 TEST(SparseBitVectorTest, IntersectWith) {
57 SparseBitVector<> Vec, Other;
59 Vec.set(1);
60 Other.set(1);
61 EXPECT_FALSE(Vec &= Other);
62 EXPECT_TRUE(Vec.test(1));
64 Vec.clear();
65 Vec.set(5);
66 Other.clear();
67 Other.set(6);
68 EXPECT_TRUE(Vec &= Other);
69 EXPECT_TRUE(Vec.empty());
71 Vec.clear();
72 Vec.set(5);
73 Other.clear();
74 Other.set(225);
75 EXPECT_TRUE(Vec &= Other);
76 EXPECT_TRUE(Vec.empty());
78 Vec.clear();
79 Vec.set(225);
80 Other.clear();
81 Other.set(5);
82 EXPECT_TRUE(Vec &= Other);
83 EXPECT_TRUE(Vec.empty());
86 TEST(SparseBitVectorTest, SelfAssignment) {
87 SparseBitVector<> Vec, Other;
89 Vec.set(23);
90 Vec.set(234);
91 Vec = static_cast<SparseBitVector<> &>(Vec);
92 EXPECT_TRUE(Vec.test(23));
93 EXPECT_TRUE(Vec.test(234));
95 Vec.clear();
96 Vec.set(17);
97 Vec.set(256);
98 EXPECT_FALSE(Vec |= Vec);
99 EXPECT_TRUE(Vec.test(17));
100 EXPECT_TRUE(Vec.test(256));
102 Vec.clear();
103 Vec.set(56);
104 Vec.set(517);
105 EXPECT_FALSE(Vec &= Vec);
106 EXPECT_TRUE(Vec.test(56));
107 EXPECT_TRUE(Vec.test(517));
109 Vec.clear();
110 Vec.set(99);
111 Vec.set(333);
112 EXPECT_TRUE(Vec.intersectWithComplement(Vec));
113 EXPECT_TRUE(Vec.empty());
114 EXPECT_FALSE(Vec.intersectWithComplement(Vec));
116 Vec.clear();
117 Vec.set(28);
118 Vec.set(43);
119 Vec.intersectWithComplement(Vec, Vec);
120 EXPECT_TRUE(Vec.empty());
122 Vec.clear();
123 Vec.set(42);
124 Vec.set(567);
125 Other.set(55);
126 Other.set(567);
127 Vec.intersectWithComplement(Vec, Other);
128 EXPECT_TRUE(Vec.test(42));
129 EXPECT_FALSE(Vec.test(567));
131 Vec.clear();
132 Vec.set(19);
133 Vec.set(21);
134 Other.clear();
135 Other.set(19);
136 Other.set(31);
137 Vec.intersectWithComplement(Other, Vec);
138 EXPECT_FALSE(Vec.test(19));
139 EXPECT_TRUE(Vec.test(31));
141 Vec.clear();
142 Vec.set(1);
143 Other.clear();
144 Other.set(59);
145 Other.set(75);
146 Vec.intersectWithComplement(Other, Other);
147 EXPECT_TRUE(Vec.empty());
150 TEST(SparseBitVectorTest, Find) {
151 SparseBitVector<> Vec;
152 Vec.set(1);
153 EXPECT_EQ(1, Vec.find_first());
154 EXPECT_EQ(1, Vec.find_last());
156 Vec.set(2);
157 EXPECT_EQ(1, Vec.find_first());
158 EXPECT_EQ(2, Vec.find_last());
160 Vec.set(0);
161 Vec.set(3);
162 EXPECT_EQ(0, Vec.find_first());
163 EXPECT_EQ(3, Vec.find_last());
165 Vec.reset(1);
166 Vec.reset(0);
167 Vec.reset(3);
168 EXPECT_EQ(2, Vec.find_first());
169 EXPECT_EQ(2, Vec.find_last());
171 // Set some large bits to ensure we are pulling bits from more than just a
172 // single bitword.
173 Vec.set(500);
174 Vec.set(2000);
175 Vec.set(3000);
176 Vec.set(4000);
177 Vec.reset(2);
178 EXPECT_EQ(500, Vec.find_first());
179 EXPECT_EQ(4000, Vec.find_last());
181 Vec.reset(500);
182 Vec.reset(3000);
183 Vec.reset(4000);
184 EXPECT_EQ(2000, Vec.find_first());
185 EXPECT_EQ(2000, Vec.find_last());
187 Vec.clear();