Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / events / gesture_detection / bitset_32_unittest.cc
bloba684af867c8c9a280eeb75b00e7fe02de844df90
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/events/gesture_detection/bitset_32.h"
9 namespace ui {
11 class BitSet32Test : public testing::Test {
12 public:
13 void TearDown() override {
14 b1.clear();
15 b2.clear();
18 protected:
19 BitSet32 b1;
20 BitSet32 b2;
23 TEST_F(BitSet32Test, Basic) {
24 BitSet32 bits;
26 // Test the empty set.
27 EXPECT_EQ(0U, bits.count());
28 EXPECT_TRUE(bits.is_empty());
29 EXPECT_FALSE(bits.is_full());
30 EXPECT_FALSE(bits.has_bit(0));
31 EXPECT_FALSE(bits.has_bit(31));
33 // Mark the first bit.
34 bits.mark_bit(0);
35 EXPECT_EQ(1U, bits.count());
36 EXPECT_FALSE(bits.is_empty());
37 EXPECT_FALSE(bits.is_full());
38 EXPECT_TRUE(bits.has_bit(0));
39 EXPECT_FALSE(bits.has_bit(31));
40 EXPECT_EQ(0U, bits.first_marked_bit());
41 EXPECT_EQ(0U, bits.last_marked_bit());
42 EXPECT_EQ(1U, bits.first_unmarked_bit());
44 // Mark the last bit.
45 bits.mark_bit(31);
46 EXPECT_EQ(2U, bits.count());
47 EXPECT_FALSE(bits.is_empty());
48 EXPECT_FALSE(bits.is_full());
49 EXPECT_TRUE(bits.has_bit(0));
50 EXPECT_TRUE(bits.has_bit(31));
51 EXPECT_FALSE(bits.has_bit(15));
52 EXPECT_EQ(0U, bits.first_marked_bit());
53 EXPECT_EQ(31U, bits.last_marked_bit());
54 EXPECT_EQ(1U, bits.first_unmarked_bit());
55 EXPECT_EQ(0U, bits.get_index_of_bit(0));
56 EXPECT_EQ(1U, bits.get_index_of_bit(1));
57 EXPECT_EQ(1U, bits.get_index_of_bit(2));
58 EXPECT_EQ(1U, bits.get_index_of_bit(31));
60 // Clear the first bit.
61 bits.clear_first_marked_bit();
62 EXPECT_EQ(1U, bits.count());
63 EXPECT_FALSE(bits.is_empty());
64 EXPECT_FALSE(bits.is_full());
65 EXPECT_FALSE(bits.has_bit(0));
66 EXPECT_TRUE(bits.has_bit(31));
67 EXPECT_EQ(31U, bits.first_marked_bit());
68 EXPECT_EQ(31U, bits.last_marked_bit());
69 EXPECT_EQ(0U, bits.first_unmarked_bit());
70 EXPECT_EQ(0U, bits.get_index_of_bit(0));
71 EXPECT_EQ(0U, bits.get_index_of_bit(1));
72 EXPECT_EQ(0U, bits.get_index_of_bit(31));
74 // Clear the last bit (the set should be empty).
75 bits.clear_last_marked_bit();
76 EXPECT_EQ(0U, bits.count());
77 EXPECT_TRUE(bits.is_empty());
78 EXPECT_FALSE(bits.is_full());
79 EXPECT_FALSE(bits.has_bit(0));
80 EXPECT_FALSE(bits.has_bit(31));
81 EXPECT_EQ(0U, bits.get_index_of_bit(0));
82 EXPECT_EQ(0U, bits.get_index_of_bit(31));
83 EXPECT_EQ(BitSet32(), bits);
85 // Mark the first unmarked bit (bit 0).
86 bits.mark_first_unmarked_bit();
87 EXPECT_EQ(1U, bits.count());
88 EXPECT_FALSE(bits.is_empty());
89 EXPECT_FALSE(bits.is_full());
90 EXPECT_TRUE(bits.has_bit(0));
91 EXPECT_EQ(0U, bits.first_marked_bit());
92 EXPECT_EQ(0U, bits.last_marked_bit());
93 EXPECT_EQ(1U, bits.first_unmarked_bit());
95 // Mark the next unmarked bit (bit 1).
96 bits.mark_first_unmarked_bit();
97 EXPECT_EQ(2U, bits.count());
98 EXPECT_FALSE(bits.is_empty());
99 EXPECT_FALSE(bits.is_full());
100 EXPECT_TRUE(bits.has_bit(0));
101 EXPECT_TRUE(bits.has_bit(1));
102 EXPECT_EQ(0U, bits.first_marked_bit());
103 EXPECT_EQ(1U, bits.last_marked_bit());
104 EXPECT_EQ(2U, bits.first_unmarked_bit());
105 EXPECT_EQ(0U, bits.get_index_of_bit(0));
106 EXPECT_EQ(1U, bits.get_index_of_bit(1));
107 EXPECT_EQ(2U, bits.get_index_of_bit(2));
110 TEST_F(BitSet32Test, BitWiseOr) {
111 b1.mark_bit(2);
112 b2.mark_bit(4);
114 BitSet32 tmp = b1 | b2;
115 EXPECT_EQ(tmp.count(), 2u);
116 EXPECT_TRUE(tmp.has_bit(2) && tmp.has_bit(4));
117 // Check that the operator is symmetric.
118 EXPECT_TRUE((b2 | b1) == (b1 | b2));
120 b1 |= b2;
121 EXPECT_EQ(b1.count(), 2u);
122 EXPECT_TRUE(b1.has_bit(2) && b1.has_bit(4));
123 EXPECT_TRUE(b2.has_bit(4) && b2.count() == 1u);
126 TEST_F(BitSet32Test, BitWiseAnd_Disjoint) {
127 b1.mark_bit(2);
128 b1.mark_bit(4);
129 b1.mark_bit(6);
131 BitSet32 tmp = b1 & b2;
132 EXPECT_TRUE(tmp.is_empty());
133 // Check that the operator is symmetric.
134 EXPECT_TRUE((b2 & b1) == (b1 & b2));
136 b2 &= b1;
137 EXPECT_TRUE(b2.is_empty());
138 EXPECT_EQ(b1.count(), 3u);
139 EXPECT_TRUE(b1.has_bit(2) && b1.has_bit(4) && b1.has_bit(6));
142 TEST_F(BitSet32Test, BitWiseAnd_NonDisjoint) {
143 b1.mark_bit(2);
144 b1.mark_bit(4);
145 b1.mark_bit(6);
146 b2.mark_bit(3);
147 b2.mark_bit(6);
148 b2.mark_bit(9);
150 BitSet32 tmp = b1 & b2;
151 EXPECT_EQ(tmp.count(), 1u);
152 EXPECT_TRUE(tmp.has_bit(6));
153 // Check that the operator is symmetric.
154 EXPECT_TRUE((b2 & b1) == (b1 & b2));
156 b1 &= b2;
157 EXPECT_EQ(b1.count(), 1u);
158 EXPECT_EQ(b2.count(), 3u);
159 EXPECT_TRUE(b2.has_bit(3) && b2.has_bit(6) && b2.has_bit(9));
162 TEST_F(BitSet32Test, MarkFirstUnmarkedBit) {
163 b1.mark_bit(1);
165 b1.mark_first_unmarked_bit();
166 EXPECT_EQ(b1.count(), 2u);
167 EXPECT_TRUE(b1.has_bit(0) && b1.has_bit(1));
169 b1.mark_first_unmarked_bit();
170 EXPECT_EQ(b1.count(), 3u);
171 EXPECT_TRUE(b1.has_bit(0) && b1.has_bit(1) && b1.has_bit(2));
174 TEST_F(BitSet32Test, ClearFirstMarkedBit) {
175 b1.mark_bit(0);
176 b1.mark_bit(10);
178 b1.clear_first_marked_bit();
179 EXPECT_EQ(b1.count(), 1u);
180 EXPECT_TRUE(b1.has_bit(10));
182 b1.mark_bit(30);
183 b1.clear_first_marked_bit();
184 EXPECT_EQ(b1.count(), 1u);
185 EXPECT_TRUE(b1.has_bit(30));
188 TEST_F(BitSet32Test, ClearLastMarkedBit) {
189 b1.mark_bit(10);
190 b1.mark_bit(31);
192 b1.clear_last_marked_bit();
193 EXPECT_EQ(b1.count(), 1u);
194 EXPECT_TRUE(b1.has_bit(10));
196 b1.mark_bit(5);
197 b1.clear_last_marked_bit();
198 EXPECT_EQ(b1.count(), 1u);
199 EXPECT_TRUE(b1.has_bit(5));
202 TEST_F(BitSet32Test, FillAndClear) {
203 EXPECT_TRUE(b1.is_empty());
204 for (size_t i = 0; i < 32; i++) {
205 b1.mark_first_unmarked_bit();
207 EXPECT_TRUE(b1.is_full());
208 b1.clear();
209 EXPECT_TRUE(b1.is_empty());
212 TEST_F(BitSet32Test, GetIndexOfBit) {
213 b1.mark_bit(11);
214 b1.mark_bit(29);
215 EXPECT_EQ(b1.get_index_of_bit(11), 0u);
216 EXPECT_EQ(b1.get_index_of_bit(29), 1u);
217 b1.mark_first_unmarked_bit();
218 EXPECT_EQ(b1.get_index_of_bit(11), 1u);
219 EXPECT_EQ(b1.get_index_of_bit(29), 2u);
222 } // namespace ui