Bug 1714154 [wpt PR 29187] - Fix OOF fragments to be up-to-date under certain conditi...
[gecko.git] / mfbt / tests / TestBitSet.cpp
blobe8a4a5267fada04007fd8062746030c939dfa129
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/BitSet.h"
9 using mozilla::BitSet;
11 // Work around issue with commas in macro use.
12 template <size_t N>
13 using BitSetUint8 = BitSet<N, uint8_t>;
14 template <size_t N>
15 using BitSetUint32 = BitSet<N, uint32_t>;
17 void TestBitSet() {
18 MOZ_RELEASE_ASSERT(BitSetUint8<1>().Storage().LengthBytes() == 1);
19 MOZ_RELEASE_ASSERT(BitSetUint32<1>().Storage().LengthBytes() == 4);
21 MOZ_RELEASE_ASSERT(BitSetUint8<1>().Storage().Length() == 1);
22 MOZ_RELEASE_ASSERT(BitSetUint8<8>().Storage().Length() == 1);
23 MOZ_RELEASE_ASSERT(BitSetUint8<9>().Storage().Length() == 2);
25 MOZ_RELEASE_ASSERT(BitSetUint32<1>().Storage().Length() == 1);
26 MOZ_RELEASE_ASSERT(BitSetUint32<32>().Storage().Length() == 1);
27 MOZ_RELEASE_ASSERT(BitSetUint32<33>().Storage().Length() == 2);
29 BitSetUint8<10> bitset;
30 MOZ_RELEASE_ASSERT(!bitset.Test(3));
31 MOZ_RELEASE_ASSERT(!bitset[3]);
33 bitset[3] = true;
34 MOZ_RELEASE_ASSERT(bitset.Test(3));
35 MOZ_RELEASE_ASSERT(bitset[3]);
37 bitset.ResetAll();
38 for (size_t i = 0; i < bitset.Size(); i++) {
39 MOZ_RELEASE_ASSERT(!bitset[i]);
42 bitset.SetAll();
43 for (size_t i = 0; i < bitset.Size(); i++) {
44 MOZ_RELEASE_ASSERT(bitset[i]);
47 bitset.ResetAll();
48 for (size_t i = 0; i < bitset.Size(); i++) {
49 MOZ_RELEASE_ASSERT(!bitset[i]);
52 // Test trailing unused bits are not set by SetAll().
53 bitset.SetAll();
54 BitSetUint8<16> bitset2(bitset.Storage());
55 MOZ_RELEASE_ASSERT(bitset.Size() < bitset2.Size());
56 MOZ_RELEASE_ASSERT(bitset.Storage().Length() == bitset2.Storage().Length());
57 for (size_t i = 0; i < bitset.Size(); i++) {
58 MOZ_RELEASE_ASSERT(bitset2[i]);
60 for (size_t i = bitset.Size(); i < bitset2.Size(); i++) {
61 MOZ_RELEASE_ASSERT(!bitset2[i]);
65 int main() {
66 TestBitSet();
67 return 0;