1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/BitSet.h"
10 using mozilla::BitSet
;
12 template <typename Storage
>
15 using TestBitSet
= BitSet
<N
, Storage
>;
17 static constexpr size_t kBitsPerWord
= sizeof(Storage
) * 8;
19 static constexpr Storage kAllBitsSet
= ~Storage
{0};
23 MOZ_RELEASE_ASSERT(TestBitSet
<1>().Storage().LengthBytes() ==
26 MOZ_RELEASE_ASSERT(TestBitSet
<1>().Storage().Length() == 1);
27 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
>().Storage().Length() == 1);
28 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
+ 1>().Storage().Length() == 2);
31 void testConstruct() {
32 MOZ_RELEASE_ASSERT(TestBitSet
<1>().Storage()[0] == 0);
33 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
>().Storage()[0] == 0);
34 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
+ 1>().Storage()[0] == 0);
35 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
+ 1>().Storage()[1] == 0);
37 TestBitSet
<1> bitset1
;
39 TestBitSet
<kBitsPerWord
> bitsetW
;
41 TestBitSet
<kBitsPerWord
+ 1> bitsetW1
;
44 MOZ_RELEASE_ASSERT(bitset1
.Storage()[0] == 1);
45 MOZ_RELEASE_ASSERT(bitsetW
.Storage()[0] == kAllBitsSet
);
46 MOZ_RELEASE_ASSERT(bitsetW1
.Storage()[0] == kAllBitsSet
);
47 MOZ_RELEASE_ASSERT(bitsetW1
.Storage()[1] == 1);
49 MOZ_RELEASE_ASSERT(TestBitSet
<1>(bitset1
).Storage()[0] == 1);
50 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
>(bitsetW
).Storage()[0] ==
52 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
+ 1>(bitsetW1
).Storage()[0] ==
54 MOZ_RELEASE_ASSERT(TestBitSet
<kBitsPerWord
+ 1>(bitsetW1
).Storage()[1] ==
57 MOZ_RELEASE_ASSERT(TestBitSet
<1>(bitset1
.Storage()).Storage()[0] == 1);
59 TestBitSet
<kBitsPerWord
>(bitsetW
.Storage()).Storage()[0] ==
62 TestBitSet
<kBitsPerWord
+ 1>(bitsetW1
.Storage()).Storage()[0] ==
65 TestBitSet
<kBitsPerWord
+ 1>(bitsetW1
.Storage()).Storage()[1] == 1);
69 TestBitSet
<kBitsPerWord
+ 2> bitset
;
70 MOZ_RELEASE_ASSERT(!bitset
.Test(3));
71 MOZ_RELEASE_ASSERT(!bitset
[3]);
72 MOZ_RELEASE_ASSERT(!bitset
.Test(kBitsPerWord
+ 1));
73 MOZ_RELEASE_ASSERT(!bitset
[kBitsPerWord
+ 1]);
76 MOZ_RELEASE_ASSERT(bitset
.Test(3));
77 MOZ_RELEASE_ASSERT(bitset
[3]);
79 bitset
[kBitsPerWord
+ 1] = true;
80 MOZ_RELEASE_ASSERT(bitset
.Test(3));
81 MOZ_RELEASE_ASSERT(bitset
[3]);
82 MOZ_RELEASE_ASSERT(bitset
.Test(kBitsPerWord
+ 1));
83 MOZ_RELEASE_ASSERT(bitset
[kBitsPerWord
+ 1]);
86 for (size_t i
= 0; i
< decltype(bitset
)::Size(); i
++) {
87 MOZ_RELEASE_ASSERT(!bitset
[i
]);
91 for (size_t i
= 0; i
< decltype(bitset
)::Size(); i
++) {
92 MOZ_RELEASE_ASSERT(bitset
[i
]);
95 // Test trailing unused bits are not set by SetAll().
96 MOZ_RELEASE_ASSERT(bitset
.Storage()[1] == 3);
99 for (size_t i
= 0; i
< decltype(bitset
)::Size(); i
++) {
100 MOZ_RELEASE_ASSERT(!bitset
[i
]);
112 BitSetSuite
<uint8_t>().runTests();
113 BitSetSuite
<uint32_t>().runTests();
114 BitSetSuite
<uint64_t>().runTests();