Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 1 | // |
| 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 | |
| 14 | using namespace angle; |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | class BitSetIteratorTest : public testing::Test |
| 19 | { |
| 20 | protected: |
| 21 | std::bitset<40> mStateBits; |
| 22 | }; |
| 23 | |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 24 | // Simple iterator test. |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 25 | TEST_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 Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 49 | // Test an empty iterator. |
| 50 | TEST_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. |
| 60 | TEST_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 Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 87 | } // anonymous namespace |