1 //===-- sanitizer_bitvector_test.cpp --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of Sanitizer runtime.
10 // Tests for sanitizer_bitvector.h.
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_bitvector.h"
15 #include "sanitizer_test_utils.h"
17 #include "gtest/gtest.h"
24 using namespace __sanitizer
;
28 // Check the 'bv' == 's' and that the indexes go in increasing order.
29 // Also check the BV::Iterator
31 static void CheckBV(const BV
&bv
, const set
<uptr
> &s
) {
35 uptr last_idx
= bv
.size();
37 for (typename
BV::Iterator
it(bv
); it
.hasNext();) {
40 if (last_idx
!= bv
.size())
41 EXPECT_LT(last_idx
, idx
);
43 EXPECT_TRUE(s
.count(idx
));
45 EXPECT_EQ(count
, s
.size());
49 uptr idx
= t
.getAndClearFirstOne();
50 if (last_idx
!= bv
.size())
51 EXPECT_LT(last_idx
, idx
);
53 EXPECT_TRUE(t_s
.erase(idx
));
55 EXPECT_TRUE(t_s
.empty());
59 void Print(const BV
&bv
) {
63 uptr idx
= t
.getAndClearFirstOne();
64 fprintf(stderr
, "%lu ", idx
);
66 fprintf(stderr
, "\n");
69 void Print(const set
<uptr
> &s
) {
70 for (set
<uptr
>::iterator it
= s
.begin(); it
!= s
.end(); ++it
) {
72 fprintf(stderr
, "%llu ", *it
);
74 fprintf(stderr
, "%zu ", *it
);
77 fprintf(stderr
, "\n");
81 void TestBitVector(uptr expected_size
) {
84 EXPECT_EQ(expected_size
, BV::kSize
);
86 EXPECT_TRUE(bv
.empty());
88 EXPECT_FALSE(bv
.empty());
89 EXPECT_FALSE(bv
.getBit(4));
90 EXPECT_FALSE(bv
.getBit(6));
91 EXPECT_TRUE(bv
.getBit(5));
93 EXPECT_FALSE(bv
.getBit(5));
98 for (uptr it
= 0; it
< 1000; it
++) {
99 uptr bit
= ((uptr
)my_rand() % bv
.size());
100 EXPECT_EQ(bv
.getBit(bit
), s
.count(bit
) == 1);
101 switch (my_rand() % 2) {
103 EXPECT_EQ(bv
.setBit(bit
), s
.insert(bit
).second
);
106 size_t old_size
= s
.size();
108 EXPECT_EQ(bv
.clearBit(bit
), old_size
> s
.size());
111 EXPECT_EQ(bv
.getBit(bit
), s
.count(bit
) == 1);
114 vector
<uptr
>bits(bv
.size());
115 // Test setUnion, setIntersection, setDifference,
116 // intersectsWith, and getAndClearFirstOne.
117 for (uptr it
= 0; it
< 30; it
++) {
119 for (size_t j
= 0; j
< bits
.size(); j
++) bits
[j
] = j
;
120 std::shuffle(bits
.begin(), bits
.end(), r
);
121 set
<uptr
> s
, s1
, t_s
;
124 uptr n_bits
= ((uptr
)my_rand() % bv
.size()) + 1;
125 uptr n_bits1
= (uptr
)my_rand() % (bv
.size() / 2);
126 EXPECT_TRUE(n_bits
> 0 && n_bits
<= bv
.size());
127 EXPECT_TRUE(n_bits1
< bv
.size() / 2);
128 for (uptr i
= 0; i
< n_bits
; i
++) {
133 for (uptr i
= 0; i
< n_bits1
; i
++) {
134 bv1
.setBit(bits
[bv
.size() / 2 + i
]);
135 s1
.insert(bits
[bv
.size() / 2 + i
]);
140 set_intersection(s
.begin(), s
.end(), s1
.begin(), s1
.end(),
141 back_insert_iterator
<vector
<uptr
> >(vec
));
142 EXPECT_EQ(bv
.intersectsWith(bv1
), !vec
.empty());
147 t_s
.insert(s1
.begin(), s1
.end());
148 EXPECT_EQ(t_bv
.setUnion(bv1
), s
.size() != t_s
.size());
152 t_s
= set
<uptr
>(vec
.begin(), vec
.end());
154 EXPECT_EQ(t_bv
.setIntersection(bv1
), s
.size() != t_s
.size());
159 set_difference(s
.begin(), s
.end(), s1
.begin(), s1
.end(),
160 back_insert_iterator
<vector
<uptr
> >(vec
));
161 t_s
= set
<uptr
>(vec
.begin(), vec
.end());
163 EXPECT_EQ(t_bv
.setDifference(bv1
), s
.size() != t_s
.size());
168 TEST(SanitizerCommon
, BasicBitVector
) {
169 TestBitVector
<BasicBitVector
<u8
> >(8);
170 TestBitVector
<BasicBitVector
<u16
> >(16);
171 TestBitVector
<BasicBitVector
<> >(SANITIZER_WORDSIZE
);
174 TEST(SanitizerCommon
, TwoLevelBitVector
) {
175 uptr ws
= SANITIZER_WORDSIZE
;
176 TestBitVector
<TwoLevelBitVector
<1, BasicBitVector
<u8
> > >(8 * 8);
177 TestBitVector
<TwoLevelBitVector
<> >(ws
* ws
);
178 TestBitVector
<TwoLevelBitVector
<2> >(ws
* ws
* 2);
179 TestBitVector
<TwoLevelBitVector
<3> >(ws
* ws
* 3);
180 TestBitVector
<TwoLevelBitVector
<3, BasicBitVector
<u16
> > >(16 * 16 * 3);