blob: 50198a9ac8e73ccd69a052e4d6641ddfab1bfc9d [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// Copyright 2017 The NXT Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <gtest/gtest.h>
16
Corentin Wallezfffe6df2017-07-06 14:41:13 -040017#include "common/BitSetIterator.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040018
19// This is ANGLE's BitSetIterator_unittests.cpp file.
20
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021class BitSetIteratorTest : public testing::Test {
22 protected:
23 std::bitset<40> mStateBits;
24};
25
26// Simple iterator test.
27TEST_F(BitSetIteratorTest, Iterator) {
28 std::set<unsigned long> originalValues;
29 originalValues.insert(2);
30 originalValues.insert(6);
31 originalValues.insert(8);
32 originalValues.insert(35);
33
34 for (unsigned long value : originalValues) {
35 mStateBits.set(value);
36 }
37
38 std::set<unsigned long> readValues;
39 for (unsigned long bit : IterateBitSet(mStateBits)) {
40 EXPECT_EQ(1u, originalValues.count(bit));
41 EXPECT_EQ(0u, readValues.count(bit));
42 readValues.insert(bit);
43 }
44
45 EXPECT_EQ(originalValues.size(), readValues.size());
46}
47
48// Test an empty iterator.
49TEST_F(BitSetIteratorTest, EmptySet) {
50 // We don't use the FAIL gtest macro here since it returns immediately,
51 // causing an unreachable code warning in MSVS
52 bool sawBit = false;
53 for (unsigned long bit : IterateBitSet(mStateBits)) {
Corentin Wallez82565b32018-06-12 10:28:52 -040054 NXT_UNUSED(bit);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040055 sawBit = true;
56 }
57 EXPECT_FALSE(sawBit);
58}
59
60// Test iterating a result of combining two bitsets.
61TEST_F(BitSetIteratorTest, NonLValueBitset) {
62 std::bitset<40> otherBits;
63
64 mStateBits.set(1);
65 mStateBits.set(2);
66 mStateBits.set(3);
67 mStateBits.set(4);
68
69 otherBits.set(0);
70 otherBits.set(1);
71 otherBits.set(3);
72 otherBits.set(5);
73
74 std::set<unsigned long> seenBits;
75
76 for (unsigned long bit : IterateBitSet(mStateBits & otherBits)) {
77 EXPECT_EQ(0u, seenBits.count(bit));
78 seenBits.insert(bit);
79 EXPECT_TRUE(mStateBits[bit]);
80 EXPECT_TRUE(otherBits[bit]);
81 }
82
83 EXPECT_EQ((mStateBits & otherBits).count(), seenBits.size());
84}