101b335832eca137a93a834a23097ec86675f3fc
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / OptionSet.cpp
1 /*
2  * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "Test.h"
29 #include <wtf/OptionSet.h>
30
31 namespace TestWebKitAPI {
32
33 enum class ExampleFlags : uint64_t {
34     A = 1 << 0,
35     B = 1 << 1,
36     C = 1 << 2,
37     D = 1ULL << 31,
38     E = 1ULL << 63,
39 };
40
41 TEST(WTF_OptionSet, EmptySet)
42 {
43     OptionSet<ExampleFlags> set;
44     EXPECT_TRUE(set.isEmpty());
45     EXPECT_FALSE(set.contains(ExampleFlags::A));
46     EXPECT_FALSE(set.contains(ExampleFlags::B));
47     EXPECT_FALSE(set.contains(ExampleFlags::C));
48     EXPECT_FALSE(set.contains(ExampleFlags::D));
49     EXPECT_FALSE(set.contains(ExampleFlags::E));
50 }
51
52 TEST(WTF_OptionSet, ContainsOneFlag)
53 {
54     OptionSet<ExampleFlags> set = ExampleFlags::A;
55     EXPECT_FALSE(set.isEmpty());
56     EXPECT_TRUE(set.contains(ExampleFlags::A));
57     EXPECT_FALSE(set.contains(ExampleFlags::B));
58     EXPECT_FALSE(set.contains(ExampleFlags::C));
59     EXPECT_FALSE(set.contains(ExampleFlags::D));
60     EXPECT_FALSE(set.contains(ExampleFlags::E));
61 }
62
63 TEST(WTF_OptionSet, Equal)
64 {
65     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B };
66     
67     EXPECT_TRUE((set == OptionSet<ExampleFlags> { ExampleFlags::A, ExampleFlags::B }));
68     EXPECT_TRUE((set == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::A }));
69     EXPECT_FALSE(set == ExampleFlags::B);
70 }
71
72 TEST(WTF_OptionSet, NotEqual)
73 {
74     OptionSet<ExampleFlags> set = ExampleFlags::A;
75     
76     EXPECT_TRUE(set != ExampleFlags::B);
77     EXPECT_FALSE(set != ExampleFlags::A);
78 }
79
80 TEST(WTF_OptionSet, Or)
81 {
82     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C };
83     OptionSet<ExampleFlags> set2 { ExampleFlags::C, ExampleFlags::D };
84
85     EXPECT_TRUE(((set | ExampleFlags::A) == OptionSet<ExampleFlags> { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C }));
86     EXPECT_TRUE(((set | ExampleFlags::D) == OptionSet<ExampleFlags> { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C, ExampleFlags::D }));
87     EXPECT_TRUE(((set | set2) == OptionSet<ExampleFlags> { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C, ExampleFlags::D }));
88 }
89
90 TEST(WTF_OptionSet, Minus)
91 {
92     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C };
93
94     EXPECT_TRUE(((set - ExampleFlags::A) == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::C }));
95     EXPECT_TRUE(((set - ExampleFlags::D) == OptionSet<ExampleFlags> { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C }));
96     EXPECT_TRUE((set - set).isEmpty());
97 }
98
99 TEST(WTF_OptionSet, MinusEqual)
100 {
101     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B, ExampleFlags::C };
102
103     EXPECT_TRUE(((set -= ExampleFlags::A) == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::C }));
104     EXPECT_TRUE((set == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::C }));
105     EXPECT_TRUE(((set -= ExampleFlags::D) == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::C }));
106     EXPECT_TRUE((set == OptionSet<ExampleFlags> { ExampleFlags::B, ExampleFlags::C }));
107     EXPECT_TRUE((set -= set).isEmpty());
108     EXPECT_TRUE(set.isEmpty());
109 }
110
111 TEST(WTF_OptionSet, ContainsTwoFlags)
112 {
113     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B };
114     EXPECT_FALSE(set.isEmpty());
115     EXPECT_TRUE(set.contains(ExampleFlags::A));
116     EXPECT_TRUE(set.contains(ExampleFlags::B));
117     EXPECT_FALSE(set.contains(ExampleFlags::C));
118     EXPECT_FALSE(set.contains(ExampleFlags::D));
119     EXPECT_FALSE(set.contains(ExampleFlags::E));
120 }
121
122 TEST(WTF_OptionSet, ContainsTwoFlags2)
123 {
124     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::D };
125     EXPECT_FALSE(set.isEmpty());
126     EXPECT_TRUE(set.contains(ExampleFlags::A));
127     EXPECT_TRUE(set.contains(ExampleFlags::D));
128     EXPECT_FALSE(set.contains(ExampleFlags::B));
129     EXPECT_FALSE(set.contains(ExampleFlags::C));
130     EXPECT_FALSE(set.contains(ExampleFlags::E));
131 }
132
133 TEST(WTF_OptionSet, ContainsTwoFlags3)
134 {
135     OptionSet<ExampleFlags> set { ExampleFlags::D, ExampleFlags::E };
136     EXPECT_FALSE(set.isEmpty());
137     EXPECT_TRUE(set.contains(ExampleFlags::D));
138     EXPECT_TRUE(set.contains(ExampleFlags::E));
139     EXPECT_FALSE(set.contains(ExampleFlags::A));
140     EXPECT_FALSE(set.contains(ExampleFlags::B));
141     EXPECT_FALSE(set.contains(ExampleFlags::C));
142 }
143
144 TEST(WTF_OptionSet, OperatorBitwiseOr)
145 {
146     OptionSet<ExampleFlags> set = ExampleFlags::A;
147     set |= ExampleFlags::C;
148     EXPECT_TRUE(set.contains(ExampleFlags::A));
149     EXPECT_FALSE(set.contains(ExampleFlags::B));
150     EXPECT_TRUE(set.contains(ExampleFlags::C));
151 }
152
153 TEST(WTF_OptionSet, EmptyOptionSetToRawValueToOptionSet)
154 {
155     OptionSet<ExampleFlags> set;
156     EXPECT_TRUE(set.isEmpty());
157     EXPECT_FALSE(set.contains(ExampleFlags::A));
158     EXPECT_FALSE(set.contains(ExampleFlags::B));
159     EXPECT_FALSE(set.contains(ExampleFlags::C));
160
161     auto set2 = OptionSet<ExampleFlags>::fromRaw(set.toRaw());
162     EXPECT_TRUE(set2.isEmpty());
163     EXPECT_FALSE(set2.contains(ExampleFlags::A));
164     EXPECT_FALSE(set2.contains(ExampleFlags::B));
165     EXPECT_FALSE(set2.contains(ExampleFlags::C));
166 }
167
168 TEST(WTF_OptionSet, OptionSetThatContainsOneFlagToRawValueToOptionSet)
169 {
170     OptionSet<ExampleFlags> set = ExampleFlags::A;
171     EXPECT_FALSE(set.isEmpty());
172     EXPECT_TRUE(set.contains(ExampleFlags::A));
173     EXPECT_FALSE(set.contains(ExampleFlags::B));
174     EXPECT_FALSE(set.contains(ExampleFlags::C));
175     EXPECT_FALSE(set.contains(ExampleFlags::D));
176     EXPECT_FALSE(set.contains(ExampleFlags::E));
177
178     auto set2 = OptionSet<ExampleFlags>::fromRaw(set.toRaw());
179     EXPECT_FALSE(set2.isEmpty());
180     EXPECT_TRUE(set2.contains(ExampleFlags::A));
181     EXPECT_FALSE(set2.contains(ExampleFlags::B));
182     EXPECT_FALSE(set2.contains(ExampleFlags::C));
183     EXPECT_FALSE(set2.contains(ExampleFlags::D));
184     EXPECT_FALSE(set2.contains(ExampleFlags::E));
185 }
186
187 TEST(WTF_OptionSet, OptionSetThatContainsOneFlagToRawValueToOptionSet2)
188 {
189     OptionSet<ExampleFlags> set = ExampleFlags::E;
190     EXPECT_FALSE(set.isEmpty());
191     EXPECT_TRUE(set.contains(ExampleFlags::E));
192     EXPECT_FALSE(set.contains(ExampleFlags::A));
193     EXPECT_FALSE(set.contains(ExampleFlags::B));
194     EXPECT_FALSE(set.contains(ExampleFlags::C));
195     EXPECT_FALSE(set.contains(ExampleFlags::D));
196
197     auto set2 = OptionSet<ExampleFlags>::fromRaw(set.toRaw());
198     EXPECT_FALSE(set2.isEmpty());
199     EXPECT_TRUE(set2.contains(ExampleFlags::E));
200     EXPECT_FALSE(set2.contains(ExampleFlags::A));
201     EXPECT_FALSE(set2.contains(ExampleFlags::B));
202     EXPECT_FALSE(set2.contains(ExampleFlags::C));
203     EXPECT_FALSE(set2.contains(ExampleFlags::D));
204 }
205
206 TEST(WTF_OptionSet, OptionSetThatContainsTwoFlagsToRawValueToOptionSet)
207 {
208     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::C };
209     EXPECT_FALSE(set.isEmpty());
210     EXPECT_TRUE(set.contains(ExampleFlags::A));
211     EXPECT_TRUE(set.contains(ExampleFlags::C));
212     EXPECT_FALSE(set.contains(ExampleFlags::B));
213
214     auto set2 = OptionSet<ExampleFlags>::fromRaw(set.toRaw());
215     EXPECT_FALSE(set2.isEmpty());
216     EXPECT_TRUE(set2.contains(ExampleFlags::A));
217     EXPECT_TRUE(set2.contains(ExampleFlags::C));
218     EXPECT_FALSE(set2.contains(ExampleFlags::B));
219 }
220
221 TEST(WTF_OptionSet, OptionSetThatContainsTwoFlagsToRawValueToOptionSet2)
222 {
223     OptionSet<ExampleFlags> set { ExampleFlags::D, ExampleFlags::E };
224     EXPECT_FALSE(set.isEmpty());
225     EXPECT_TRUE(set.contains(ExampleFlags::D));
226     EXPECT_TRUE(set.contains(ExampleFlags::E));
227     EXPECT_FALSE(set.contains(ExampleFlags::A));
228     EXPECT_FALSE(set.contains(ExampleFlags::B));
229     EXPECT_FALSE(set.contains(ExampleFlags::C));
230
231     auto set2 = OptionSet<ExampleFlags>::fromRaw(set.toRaw());
232     EXPECT_FALSE(set2.isEmpty());
233     EXPECT_TRUE(set2.contains(ExampleFlags::D));
234     EXPECT_TRUE(set2.contains(ExampleFlags::E));
235     EXPECT_FALSE(set2.contains(ExampleFlags::A));
236     EXPECT_FALSE(set2.contains(ExampleFlags::B));
237     EXPECT_FALSE(set2.contains(ExampleFlags::C));
238 }
239
240 TEST(WTF_OptionSet, TwoIteratorsIntoSameOptionSet)
241 {
242     OptionSet<ExampleFlags> set { ExampleFlags::C, ExampleFlags::B };
243     OptionSet<ExampleFlags>::iterator it1 = set.begin();
244     OptionSet<ExampleFlags>::iterator it2 = it1;
245     ++it1;
246     EXPECT_STRONG_ENUM_EQ(ExampleFlags::C, *it1);
247     EXPECT_STRONG_ENUM_EQ(ExampleFlags::B, *it2);
248 }
249
250 TEST(WTF_OptionSet, IterateOverOptionSetThatContainsTwoFlags)
251 {
252     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::C };
253     OptionSet<ExampleFlags>::iterator it = set.begin();
254     OptionSet<ExampleFlags>::iterator end = set.end();
255     EXPECT_TRUE(it != end);
256     EXPECT_STRONG_ENUM_EQ(ExampleFlags::A, *it);
257     ++it;
258     EXPECT_STRONG_ENUM_EQ(ExampleFlags::C, *it);
259     ++it;
260     EXPECT_TRUE(it == end);
261 }
262
263 TEST(WTF_OptionSet, IterateOverOptionSetThatContainsFlags2)
264 {
265     OptionSet<ExampleFlags> set { ExampleFlags::D, ExampleFlags::E };
266     OptionSet<ExampleFlags>::iterator it = set.begin();
267     OptionSet<ExampleFlags>::iterator end = set.end();
268     EXPECT_TRUE(it != end);
269     EXPECT_STRONG_ENUM_EQ(ExampleFlags::D, *it);
270     ++it;
271     EXPECT_STRONG_ENUM_EQ(ExampleFlags::E, *it);
272     ++it;
273     EXPECT_TRUE(it == end);
274 }
275
276 TEST(WTF_OptionSet, NextItemAfterLargestIn32BitFlagSet)
277 {
278     enum class ThirtyTwoBitFlags : uint32_t {
279         A = 1UL << 31,
280     };
281     OptionSet<ThirtyTwoBitFlags> set { ThirtyTwoBitFlags::A };
282     OptionSet<ThirtyTwoBitFlags>::iterator it = set.begin();
283     OptionSet<ThirtyTwoBitFlags>::iterator end = set.end();
284     EXPECT_TRUE(it != end);
285     ++it;
286     EXPECT_TRUE(it == end);
287 }
288
289 TEST(WTF_OptionSet, NextItemAfterLargestIn64BitFlagSet)
290 {
291     enum class SixtyFourBitFlags : uint64_t {
292         A = 1ULL << 63,
293     };
294     OptionSet<SixtyFourBitFlags> set { SixtyFourBitFlags::A };
295     OptionSet<SixtyFourBitFlags>::iterator it = set.begin();
296     OptionSet<SixtyFourBitFlags>::iterator end = set.end();
297     EXPECT_TRUE(it != end);
298     ++it;
299     EXPECT_TRUE(it == end);
300 }
301
302 TEST(WTF_OptionSet, IterationOrderTheSameRegardlessOfInsertionOrder)
303 {
304     OptionSet<ExampleFlags> set1 = ExampleFlags::C;
305     set1 |= ExampleFlags::A;
306
307     OptionSet<ExampleFlags> set2 = ExampleFlags::A;
308     set2 |= ExampleFlags::C;
309
310     OptionSet<ExampleFlags>::iterator it1 = set1.begin();
311     OptionSet<ExampleFlags>::iterator it2 = set2.begin();
312
313     EXPECT_TRUE(*it1 == *it2);
314     ++it1;
315     ++it2;
316     EXPECT_TRUE(*it1 == *it2);
317 }
318
319 TEST(WTF_OptionSet, OperatorAnd)
320 {
321     OptionSet<ExampleFlags> a { ExampleFlags::A };
322     OptionSet<ExampleFlags> ac { ExampleFlags::A, ExampleFlags::C };
323     OptionSet<ExampleFlags> bc { ExampleFlags::B, ExampleFlags::C };
324     {
325         auto set = a & ac;
326         EXPECT_TRUE(!!set);
327         EXPECT_FALSE(set.isEmpty());
328         EXPECT_TRUE(set.contains(ExampleFlags::A));
329         EXPECT_FALSE(set.contains(ExampleFlags::B));
330         EXPECT_FALSE(set.contains(ExampleFlags::C));
331     }
332     {
333         auto set = a & bc;
334         EXPECT_FALSE(!!set);
335         EXPECT_TRUE(set.isEmpty());
336         EXPECT_FALSE(set.contains(ExampleFlags::A));
337         EXPECT_FALSE(set.contains(ExampleFlags::B));
338         EXPECT_FALSE(set.contains(ExampleFlags::C));
339     }
340     {
341         auto set = ac & bc;
342         EXPECT_TRUE(!!set);
343         EXPECT_FALSE(set.isEmpty());
344         EXPECT_FALSE(set.contains(ExampleFlags::A));
345         EXPECT_FALSE(set.contains(ExampleFlags::B));
346         EXPECT_TRUE(set.contains(ExampleFlags::C));
347     }
348     {
349         auto set = ExampleFlags::A & bc;
350         EXPECT_FALSE(!!set);
351         EXPECT_TRUE(set.isEmpty());
352         EXPECT_FALSE(set.contains(ExampleFlags::A));
353         EXPECT_FALSE(set.contains(ExampleFlags::B));
354         EXPECT_FALSE(set.contains(ExampleFlags::C));
355     }
356     {
357         auto set = ExampleFlags::A & ac;
358         EXPECT_TRUE(!!set);
359         EXPECT_FALSE(set.isEmpty());
360         EXPECT_TRUE(set.contains(ExampleFlags::A));
361         EXPECT_FALSE(set.contains(ExampleFlags::B));
362         EXPECT_FALSE(set.contains(ExampleFlags::C));
363     }
364     {
365         auto set = bc & ExampleFlags::A;
366         EXPECT_FALSE(!!set);
367         EXPECT_TRUE(set.isEmpty());
368         EXPECT_FALSE(set.contains(ExampleFlags::A));
369         EXPECT_FALSE(set.contains(ExampleFlags::B));
370         EXPECT_FALSE(set.contains(ExampleFlags::C));
371     }
372     {
373         auto set = ac & ExampleFlags::A;
374         EXPECT_TRUE(!!set);
375         EXPECT_FALSE(set.isEmpty());
376         EXPECT_TRUE(set.contains(ExampleFlags::A));
377         EXPECT_FALSE(set.contains(ExampleFlags::B));
378         EXPECT_FALSE(set.contains(ExampleFlags::C));
379     }
380 }
381
382 TEST(WTF_OptionSet, OperatorXor)
383 {
384     OptionSet<ExampleFlags> a { ExampleFlags::A };
385     OptionSet<ExampleFlags> ac { ExampleFlags::A, ExampleFlags::C };
386     OptionSet<ExampleFlags> bc { ExampleFlags::B, ExampleFlags::C };
387     {
388         auto set = a ^ ac;
389         EXPECT_FALSE(set.contains(ExampleFlags::A));
390         EXPECT_FALSE(set.contains(ExampleFlags::B));
391         EXPECT_TRUE(set.contains(ExampleFlags::C));
392     }
393     {
394         auto set = a ^ bc;
395         EXPECT_TRUE(set.contains(ExampleFlags::A));
396         EXPECT_TRUE(set.contains(ExampleFlags::B));
397         EXPECT_TRUE(set.contains(ExampleFlags::C));
398     }
399     {
400         auto set = ac ^ bc;
401         EXPECT_TRUE(set.contains(ExampleFlags::A));
402         EXPECT_TRUE(set.contains(ExampleFlags::B));
403         EXPECT_FALSE(set.contains(ExampleFlags::C));
404     }
405 }
406
407 TEST(WTF_OptionSet, ContainsAny)
408 {
409     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B };
410
411     EXPECT_TRUE(set.containsAny({ ExampleFlags::A }));
412     EXPECT_TRUE(set.containsAny({ ExampleFlags::B }));
413     EXPECT_FALSE(set.containsAny({ ExampleFlags::C }));
414     EXPECT_FALSE(set.containsAny({ ExampleFlags::C, ExampleFlags::D }));
415     EXPECT_TRUE(set.containsAny({ ExampleFlags::A, ExampleFlags::B }));
416     EXPECT_TRUE(set.containsAny({ ExampleFlags::B, ExampleFlags::C }));
417     EXPECT_TRUE(set.containsAny({ ExampleFlags::A, ExampleFlags::C }));
418     EXPECT_TRUE(set.containsAny({ ExampleFlags::A, ExampleFlags::B, ExampleFlags::C }));
419 }
420
421 TEST(WTF_OptionSet, ContainsAll)
422 {
423     OptionSet<ExampleFlags> set { ExampleFlags::A, ExampleFlags::B };
424
425     EXPECT_TRUE(set.containsAll({ ExampleFlags::A }));
426     EXPECT_TRUE(set.containsAll({ ExampleFlags::B }));
427     EXPECT_FALSE(set.containsAll({ ExampleFlags::C }));
428     EXPECT_FALSE(set.containsAll({ ExampleFlags::C, ExampleFlags::D }));
429     EXPECT_TRUE(set.containsAll({ ExampleFlags::A, ExampleFlags::B }));
430     EXPECT_FALSE(set.containsAll({ ExampleFlags::B, ExampleFlags::C }));
431     EXPECT_FALSE(set.containsAll({ ExampleFlags::A, ExampleFlags::C }));
432     EXPECT_FALSE(set.containsAll({ ExampleFlags::A, ExampleFlags::B, ExampleFlags::C }));
433 }
434
435 } // namespace TestWebKitAPI