1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9 #include "TestClassDef.h"
10 #include "SkTSet.h"
11
12 // Tests the SkTSet<T> class template.
13 // Functions that just call SkTDArray are not tested.
14
TestTSet_basic(skiatest::Reporter * reporter)15 static void TestTSet_basic(skiatest::Reporter* reporter) {
16 SkTSet<int> set0;
17 REPORTER_ASSERT(reporter, set0.isEmpty());
18 REPORTER_ASSERT(reporter, !set0.contains(-1));
19 REPORTER_ASSERT(reporter, !set0.contains(0));
20 REPORTER_ASSERT(reporter, !set0.contains(1));
21 REPORTER_ASSERT(reporter, set0.count() == 0);
22
23 REPORTER_ASSERT(reporter, set0.add(0));
24 REPORTER_ASSERT(reporter, !set0.isEmpty());
25 REPORTER_ASSERT(reporter, !set0.contains(-1));
26 REPORTER_ASSERT(reporter, set0.contains(0));
27 REPORTER_ASSERT(reporter, !set0.contains(1));
28 REPORTER_ASSERT(reporter, set0.count() == 1);
29 REPORTER_ASSERT(reporter, !set0.add(0));
30 REPORTER_ASSERT(reporter, set0.count() == 1);
31
32 #ifdef SK_DEBUG
33 set0.validate();
34 #endif
35 }
36
37 #define COUNT 1732
38 #define PRIME1 10007
39 #define PRIME2 1733
40
41 // Generates a series of positive unique pseudo-random numbers.
f(int i)42 static int f(int i) {
43 return (long(i) * PRIME1) % PRIME2;
44 }
45
46 // Will expose contains() too.
TestTSet_advanced(skiatest::Reporter * reporter)47 static void TestTSet_advanced(skiatest::Reporter* reporter) {
48 SkTSet<int> set0;
49
50 for (int i = 0; i < COUNT; i++) {
51 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
52 if (i > 0) {
53 REPORTER_ASSERT(reporter, set0.contains(f(0)));
54 REPORTER_ASSERT(reporter, set0.contains(f(i / 2)));
55 REPORTER_ASSERT(reporter, set0.contains(f(i - 1)));
56 }
57 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
58 REPORTER_ASSERT(reporter, set0.count() == i);
59 REPORTER_ASSERT(reporter, set0.add(f(i)));
60 REPORTER_ASSERT(reporter, set0.contains(f(i)));
61 REPORTER_ASSERT(reporter, set0.count() == i + 1);
62 REPORTER_ASSERT(reporter, !set0.add(f(i)));
63 }
64
65 // Test deterministic output
66 for (int i = 0; i < COUNT; i++) {
67 REPORTER_ASSERT(reporter, set0[i] == f(i));
68 }
69
70 // Test copy constructor too.
71 SkTSet<int> set1 = set0;
72
73 REPORTER_ASSERT(reporter, set0.count() == set1.count());
74 REPORTER_ASSERT(reporter, !set1.contains(-1000));
75
76 for (int i = 0; i < COUNT; i++) {
77 REPORTER_ASSERT(reporter, set1.contains(f(i)));
78 REPORTER_ASSERT(reporter, set1[i] == f(i));
79 }
80
81 // Test operator= too.
82 SkTSet<int> set2;
83 set2 = set0;
84
85 REPORTER_ASSERT(reporter, set0.count() == set2.count());
86 REPORTER_ASSERT(reporter, !set2.contains(-1000));
87
88 for (int i = 0; i < COUNT; i++) {
89 REPORTER_ASSERT(reporter, set2.contains(f(i)));
90 REPORTER_ASSERT(reporter, set2[i] == f(i));
91 }
92
93 #ifdef SK_DEBUG
94 set0.validate();
95 set1.validate();
96 set2.validate();
97 #endif
98 }
99
TestTSet_merge(skiatest::Reporter * reporter)100 static void TestTSet_merge(skiatest::Reporter* reporter) {
101 SkTSet<int> set;
102 SkTSet<int> setOdd;
103
104 for (int i = 0; i < COUNT; i++) {
105 REPORTER_ASSERT(reporter, set.add(2 * i));
106 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
107 }
108 // mergeInto returns the number of duplicates. Expected 0.
109 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0);
110 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
111
112 // mergeInto should now find all new numbers duplicate.
113 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count());
114 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
115
116 for (int i = 0; i < 2 * COUNT; i++) {
117 REPORTER_ASSERT(reporter, set.contains(i));
118 }
119
120 // check deterministic output
121 for (int i = 0; i < COUNT; i++) {
122 REPORTER_ASSERT(reporter, set[i] == 2 * i);
123 REPORTER_ASSERT(reporter, set[COUNT + i] == 2 * i + 1);
124 }
125
126 #ifdef SK_DEBUG
127 set.validate();
128 setOdd.validate();
129 #endif
130 }
131
DEF_TEST(TSet,reporter)132 DEF_TEST(TSet, reporter) {
133 TestTSet_basic(reporter);
134 TestTSet_advanced(reporter);
135 TestTSet_merge(reporter);
136 }
137