blob: 10fd3bea3f9d6da3f4ab2b1ed02097f925359fa4 [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{
Corentin Wallez94d099d2015-09-01 13:56:56 -040052 // We don't use the FAIL gtest macro here since it returns immediately,
53 // causing an unreachable code warning in MSVS
54 bool sawBit = false;
Jamie Madillb3a60aa2015-08-14 14:44:06 -040055 for (unsigned long bit : IterateBitSet(mStateBits))
56 {
Corentin Wallez94d099d2015-09-01 13:56:56 -040057 sawBit = true;
Jamie Madillb3a60aa2015-08-14 14:44:06 -040058 UNUSED_TRACE_VARIABLE(bit);
Jamie Madillb3a60aa2015-08-14 14:44:06 -040059 }
Corentin Wallez94d099d2015-09-01 13:56:56 -040060 EXPECT_FALSE(sawBit);
Jamie Madillb3a60aa2015-08-14 14:44:06 -040061}
62
63// Test iterating a result of combining two bitsets.
64TEST_F(BitSetIteratorTest, NonLValueBitset)
65{
66 std::bitset<40> otherBits;
67
68 mStateBits.set(1);
69 mStateBits.set(2);
70 mStateBits.set(3);
71 mStateBits.set(4);
72
73 otherBits.set(0);
74 otherBits.set(1);
75 otherBits.set(3);
76 otherBits.set(5);
77
78 std::set<unsigned long> seenBits;
79
80 for (unsigned long bit : IterateBitSet(mStateBits & otherBits))
81 {
82 EXPECT_EQ(0u, seenBits.count(bit));
83 seenBits.insert(bit);
84 EXPECT_TRUE(mStateBits[bit]);
85 EXPECT_TRUE(otherBits[bit]);
86 }
87
88 EXPECT_EQ((mStateBits & otherBits).count(), seenBits.size());
89}
90
Jamie Madill6b2a0b02015-08-03 14:15:08 -040091} // anonymous namespace