blob: 2bd1923a15650dc785d54152023be5c81ae9ddda (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Assertions.h"
#include "mozilla/BitSet.h"
using mozilla::BitSet;
template <typename Storage>
class BitSetSuite {
template <size_t N>
using TestBitSet = BitSet<N, Storage>;
static constexpr size_t kBitsPerWord = sizeof(Storage) * 8;
static constexpr Storage kAllBitsSet = ~Storage{0};
public:
void testLength() {
MOZ_RELEASE_ASSERT(TestBitSet<1>().Storage().LengthBytes() ==
sizeof(Storage));
MOZ_RELEASE_ASSERT(TestBitSet<1>().Storage().Length() == 1);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord>().Storage().Length() == 1);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>().Storage().Length() == 2);
}
void testConstruct() {
MOZ_RELEASE_ASSERT(TestBitSet<1>().Storage()[0] == 0);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord>().Storage()[0] == 0);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>().Storage()[0] == 0);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>().Storage()[1] == 0);
TestBitSet<1> bitset1;
bitset1.SetAll();
TestBitSet<kBitsPerWord> bitsetW;
bitsetW.SetAll();
TestBitSet<kBitsPerWord + 1> bitsetW1;
bitsetW1.SetAll();
MOZ_RELEASE_ASSERT(bitset1.Storage()[0] == 1);
MOZ_RELEASE_ASSERT(bitsetW.Storage()[0] == kAllBitsSet);
MOZ_RELEASE_ASSERT(bitsetW1.Storage()[0] == kAllBitsSet);
MOZ_RELEASE_ASSERT(bitsetW1.Storage()[1] == 1);
MOZ_RELEASE_ASSERT(TestBitSet<1>(bitset1).Storage()[0] == 1);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord>(bitsetW).Storage()[0] ==
kAllBitsSet);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>(bitsetW1).Storage()[0] ==
kAllBitsSet);
MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>(bitsetW1).Storage()[1] ==
1);
MOZ_RELEASE_ASSERT(TestBitSet<1>(bitset1.Storage()).Storage()[0] == 1);
MOZ_RELEASE_ASSERT(
TestBitSet<kBitsPerWord>(bitsetW.Storage()).Storage()[0] ==
kAllBitsSet);
MOZ_RELEASE_ASSERT(
TestBitSet<kBitsPerWord + 1>(bitsetW1.Storage()).Storage()[0] ==
kAllBitsSet);
MOZ_RELEASE_ASSERT(
TestBitSet<kBitsPerWord + 1>(bitsetW1.Storage()).Storage()[1] == 1);
}
void testSetBit() {
TestBitSet<kBitsPerWord + 2> bitset;
MOZ_RELEASE_ASSERT(!bitset.Test(3));
MOZ_RELEASE_ASSERT(!bitset[3]);
MOZ_RELEASE_ASSERT(!bitset.Test(kBitsPerWord + 1));
MOZ_RELEASE_ASSERT(!bitset[kBitsPerWord + 1]);
bitset[3] = true;
MOZ_RELEASE_ASSERT(bitset.Test(3));
MOZ_RELEASE_ASSERT(bitset[3]);
bitset[kBitsPerWord + 1] = true;
MOZ_RELEASE_ASSERT(bitset.Test(3));
MOZ_RELEASE_ASSERT(bitset[3]);
MOZ_RELEASE_ASSERT(bitset.Test(kBitsPerWord + 1));
MOZ_RELEASE_ASSERT(bitset[kBitsPerWord + 1]);
bitset.ResetAll();
for (size_t i = 0; i < decltype(bitset)::Size(); i++) {
MOZ_RELEASE_ASSERT(!bitset[i]);
}
bitset.SetAll();
for (size_t i = 0; i < decltype(bitset)::Size(); i++) {
MOZ_RELEASE_ASSERT(bitset[i]);
}
// Test trailing unused bits are not set by SetAll().
MOZ_RELEASE_ASSERT(bitset.Storage()[1] == 3);
bitset.ResetAll();
for (size_t i = 0; i < decltype(bitset)::Size(); i++) {
MOZ_RELEASE_ASSERT(!bitset[i]);
}
}
void runTests() {
testLength();
testConstruct();
testSetBit();
}
};
int main() {
BitSetSuite<uint8_t>().runTests();
BitSetSuite<uint32_t>().runTests();
BitSetSuite<uint64_t>().runTests();
return 0;
}
|