blob: da913e16e0432adc36a5bf972733472f097440da [file] [log] [blame]
Jamie Madill6b2a0b02015-08-03 14:15:08 -04001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// BitSetIteratorTest:
7// Test the IterableBitSet class.
8//
9
10#include <gtest/gtest.h>
11
12#include "common/BitSetIterator.h"
13
14using namespace angle;
15
16namespace
17{
18class BitSetIteratorTest : public testing::Test
19{
20 protected:
21 std::bitset<40> mStateBits;
22};
23
Jamie Madillb3a60aa2015-08-14 14:44:06 -040024// Simple iterator test.
Jamie Madill6b2a0b02015-08-03 14:15:08 -040025TEST_F(BitSetIteratorTest, Iterator)
26{
27 std::set<unsigned long> originalValues;
28 originalValues.insert(2);
29 originalValues.insert(6);
30 originalValues.insert(8);
31 originalValues.insert(35);
32
33 for (unsigned long value : originalValues)
34 {
35 mStateBits.set(value);
36 }
37
38 std::set<unsigned long> readValues;
39 for (unsigned long bit : IterateBitSet(mStateBits))
40 {
41 EXPECT_EQ(1u, originalValues.count(bit));
42 EXPECT_EQ(0u, readValues.count(bit));
43 readValues.insert(bit);
44 }
45
46 EXPECT_EQ(originalValues.size(), readValues.size());
47}
48
Jamie Madillb3a60aa2015-08-14 14:44:06 -040049// Test an empty iterator.
50TEST_F(BitSetIteratorTest, EmptySet)
51{
52 for (unsigned long bit : IterateBitSet(mStateBits))
53 {
54 UNUSED_TRACE_VARIABLE(bit);
55 FAIL() << "Should not be reached";
56 }
57}
58
59// Test iterating a result of combining two bitsets.
60TEST_F(BitSetIteratorTest, NonLValueBitset)
61{
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 {
78 EXPECT_EQ(0u, seenBits.count(bit));
79 seenBits.insert(bit);
80 EXPECT_TRUE(mStateBits[bit]);
81 EXPECT_TRUE(otherBits[bit]);
82 }
83
84 EXPECT_EQ((mStateBits & otherBits).count(), seenBits.size());
85}
86
Jamie Madill6b2a0b02015-08-03 14:15:08 -040087} // anonymous namespace