no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / mfbt / tests / TestBitSet.cpp
blob2bd1923a15650dc785d54152023be5c81ae9ddda
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>
13 class BitSetSuite {
14 template <size_t N>
15 using TestBitSet = BitSet<N, Storage>;
17 static constexpr size_t kBitsPerWord = sizeof(Storage) * 8;
19 static constexpr Storage kAllBitsSet = ~Storage{0};
21 public:
22 void testLength() {
23 MOZ_RELEASE_ASSERT(TestBitSet<1>().Storage().LengthBytes() ==
24 sizeof(Storage));
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;
38 bitset1.SetAll();
39 TestBitSet<kBitsPerWord> bitsetW;
40 bitsetW.SetAll();
41 TestBitSet<kBitsPerWord + 1> bitsetW1;
42 bitsetW1.SetAll();
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] ==
51 kAllBitsSet);
52 MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>(bitsetW1).Storage()[0] ==
53 kAllBitsSet);
54 MOZ_RELEASE_ASSERT(TestBitSet<kBitsPerWord + 1>(bitsetW1).Storage()[1] ==
55 1);
57 MOZ_RELEASE_ASSERT(TestBitSet<1>(bitset1.Storage()).Storage()[0] == 1);
58 MOZ_RELEASE_ASSERT(
59 TestBitSet<kBitsPerWord>(bitsetW.Storage()).Storage()[0] ==
60 kAllBitsSet);
61 MOZ_RELEASE_ASSERT(
62 TestBitSet<kBitsPerWord + 1>(bitsetW1.Storage()).Storage()[0] ==
63 kAllBitsSet);
64 MOZ_RELEASE_ASSERT(
65 TestBitSet<kBitsPerWord + 1>(bitsetW1.Storage()).Storage()[1] == 1);
68 void testSetBit() {
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]);
75 bitset[3] = true;
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]);
85 bitset.ResetAll();
86 for (size_t i = 0; i < decltype(bitset)::Size(); i++) {
87 MOZ_RELEASE_ASSERT(!bitset[i]);
90 bitset.SetAll();
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);
98 bitset.ResetAll();
99 for (size_t i = 0; i < decltype(bitset)::Size(); i++) {
100 MOZ_RELEASE_ASSERT(!bitset[i]);
104 void runTests() {
105 testLength();
106 testConstruct();
107 testSetBit();
111 int main() {
112 BitSetSuite<uint8_t>().runTests();
113 BitSetSuite<uint32_t>().runTests();
114 BitSetSuite<uint64_t>().runTests();
116 return 0;