blob: fc2c7526df223d0cca8fe4f938cadfa5ce9d4585 [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//
Jamie Madill20e005b2017-04-07 14:19:22 -04006// bitset_utils_unittest:
7// Tests bitset helpers and custom classes.
Jamie Madill6b2a0b02015-08-03 14:15:08 -04008//
9
10#include <gtest/gtest.h>
11
Jamie Madill20e005b2017-04-07 14:19:22 -040012#include "common/bitset_utils.h"
Jamie Madill6b2a0b02015-08-03 14:15:08 -040013
14using namespace angle;
15
16namespace
17{
18class BitSetIteratorTest : public testing::Test
19{
20 protected:
Jamie Madill6de51852017-04-12 09:53:01 -040021 BitSet<40> mStateBits;
Jamie Madill6b2a0b02015-08-03 14:15:08 -040022};
23
Jamie Madillb3a60aa2015-08-14 14:44:06 -040024// Simple iterator test.
Jamie Madill6b2a0b02015-08-03 14:15:08 -040025TEST_F(BitSetIteratorTest, Iterator)
26{
Jamie Madill6de51852017-04-12 09:53:01 -040027 std::set<size_t> originalValues;
Jamie Madill6b2a0b02015-08-03 14:15:08 -040028 originalValues.insert(2);
29 originalValues.insert(6);
30 originalValues.insert(8);
31 originalValues.insert(35);
32
Jamie Madill6de51852017-04-12 09:53:01 -040033 for (size_t value : originalValues)
Jamie Madill6b2a0b02015-08-03 14:15:08 -040034 {
35 mStateBits.set(value);
36 }
37
Jamie Madill6de51852017-04-12 09:53:01 -040038 std::set<size_t> readValues;
39 for (size_t bit : mStateBits)
Jamie Madill6b2a0b02015-08-03 14:15:08 -040040 {
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 Madill6de51852017-04-12 09:53:01 -040055 for (size_t bit : mStateBits)
Jamie Madillb3a60aa2015-08-14 14:44:06 -040056 {
Corentin Wallez94d099d2015-09-01 13:56:56 -040057 sawBit = true;
Jamie Madillc6dbc252018-04-30 19:07:56 -040058 ANGLE_UNUSED_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{
Jamie Madill6de51852017-04-12 09:53:01 -040066 BitSet<40> otherBits;
Jamie Madillb3a60aa2015-08-14 14:44:06 -040067
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 Madill6de51852017-04-12 09:53:01 -040078 std::set<size_t> seenBits;
Jamie Madillb3a60aa2015-08-14 14:44:06 -040079
Jamie Madill6de51852017-04-12 09:53:01 -040080 angle::BitSet<40> maskedBits = (mStateBits & otherBits);
81 for (size_t bit : maskedBits)
Jamie Madillb3a60aa2015-08-14 14:44:06 -040082 {
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
Shao37219c82017-04-26 10:59:48 +080092// Test bit assignments.
93TEST_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 Madill6b2a0b02015-08-03 14:15:08 -0400112} // anonymous namespace