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 | // |
Jamie Madill | 20e005b | 2017-04-07 14:19:22 -0400 | [diff] [blame] | 6 | // bitset_utils_unittest: |
| 7 | // Tests bitset helpers and custom classes. |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 8 | // |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Jamie Madill | 20e005b | 2017-04-07 14:19:22 -0400 | [diff] [blame] | 12 | #include "common/bitset_utils.h" |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 13 | |
| 14 | using namespace angle; |
| 15 | |
| 16 | namespace |
| 17 | { |
| 18 | class BitSetIteratorTest : public testing::Test |
| 19 | { |
| 20 | protected: |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 21 | BitSet<40> mStateBits; |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 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 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 27 | std::set<size_t> originalValues; |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 28 | originalValues.insert(2); |
| 29 | originalValues.insert(6); |
| 30 | originalValues.insert(8); |
| 31 | originalValues.insert(35); |
| 32 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 33 | for (size_t value : originalValues) |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 34 | { |
| 35 | mStateBits.set(value); |
| 36 | } |
| 37 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 38 | std::set<size_t> readValues; |
| 39 | for (size_t bit : mStateBits) |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 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 | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 55 | for (size_t bit : mStateBits) |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 56 | { |
Corentin Wallez | 94d099d | 2015-09-01 13:56:56 -0400 | [diff] [blame] | 57 | sawBit = true; |
Jamie Madill | c6dbc25 | 2018-04-30 19:07:56 -0400 | [diff] [blame] | 58 | ANGLE_UNUSED_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 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 66 | BitSet<40> otherBits; |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 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 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 78 | std::set<size_t> seenBits; |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 79 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 80 | angle::BitSet<40> maskedBits = (mStateBits & otherBits); |
| 81 | for (size_t bit : maskedBits) |
Jamie Madill | b3a60aa | 2015-08-14 14:44:06 -0400 | [diff] [blame] | 82 | { |
| 83 | EXPECT_EQ(0u, seenBits.count(bit)); |
| 84 | seenBits.insert(bit); |
| 85 | EXPECT_TRUE(mStateBits[bit]); |
| 86 | EXPECT_TRUE(otherBits[bit]); |
| 87 | } |
| 88 | |
| 89 | EXPECT_EQ((mStateBits & otherBits).count(), seenBits.size()); |
| 90 | } |
| 91 | |
Shao | 37219c8 | 2017-04-26 10:59:48 +0800 | [diff] [blame] | 92 | // Test bit assignments. |
| 93 | TEST_F(BitSetIteratorTest, BitAssignment) |
| 94 | { |
| 95 | std::set<size_t> originalValues; |
| 96 | originalValues.insert(2); |
| 97 | originalValues.insert(6); |
| 98 | originalValues.insert(8); |
| 99 | originalValues.insert(35); |
| 100 | |
| 101 | for (size_t value : originalValues) |
| 102 | { |
| 103 | (mStateBits[value] = false) = true; |
| 104 | } |
| 105 | |
| 106 | for (size_t value : originalValues) |
| 107 | { |
| 108 | EXPECT_TRUE(mStateBits.test(value)); |
| 109 | } |
| 110 | } |
| 111 | |
Jamie Madill | 6b2a0b0 | 2015-08-03 14:15:08 -0400 | [diff] [blame] | 112 | } // anonymous namespace |