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 | { |
Corentin Wallez | 94d099d | 2015-09-01 13:56:56 -0400 | [diff] [blame] | 52 | // 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 Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 55 | for (unsigned long bit : IterateBitSet(mStateBits)) |
| 56 | { |
Corentin Wallez | 94d099d | 2015-09-01 13:56:56 -0400 | [diff] [blame] | 57 | sawBit = true; |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 58 | UNUSED_TRACE_VARIABLE(bit); |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 59 | } |
Corentin Wallez | 94d099d | 2015-09-01 13:56:56 -0400 | [diff] [blame] | 60 | EXPECT_FALSE(sawBit); |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Test iterating a result of combining two bitsets. |
| 64 | TEST_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 Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 91 | } // anonymous namespace |