2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 // Tests the SkTSet<T> class template.
12 // Functions that just call SkTDArray are not tested.
14 static void TestTSet_basic(skiatest::Reporter* reporter) {
16 REPORTER_ASSERT(reporter, set0.isEmpty());
17 REPORTER_ASSERT(reporter, !set0.contains(-1));
18 REPORTER_ASSERT(reporter, !set0.contains(0));
19 REPORTER_ASSERT(reporter, !set0.contains(1));
20 REPORTER_ASSERT(reporter, set0.count() == 0);
22 REPORTER_ASSERT(reporter, set0.add(0));
23 REPORTER_ASSERT(reporter, !set0.isEmpty());
24 REPORTER_ASSERT(reporter, !set0.contains(-1));
25 REPORTER_ASSERT(reporter, set0.contains(0));
26 REPORTER_ASSERT(reporter, !set0.contains(1));
27 REPORTER_ASSERT(reporter, set0.count() == 1);
28 REPORTER_ASSERT(reporter, !set0.add(0));
29 REPORTER_ASSERT(reporter, set0.count() == 1);
40 // Generates a series of positive unique pseudo-random numbers.
42 return (long(i) * PRIME1) % PRIME2;
45 // Will expose contains() too.
46 static void TestTSet_advanced(skiatest::Reporter* reporter) {
49 for (int i = 0; i < COUNT; i++) {
50 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
52 REPORTER_ASSERT(reporter, set0.contains(f(0)));
53 REPORTER_ASSERT(reporter, set0.contains(f(i / 2)));
54 REPORTER_ASSERT(reporter, set0.contains(f(i - 1)));
56 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
57 REPORTER_ASSERT(reporter, set0.count() == i);
58 REPORTER_ASSERT(reporter, set0.add(f(i)));
59 REPORTER_ASSERT(reporter, set0.contains(f(i)));
60 REPORTER_ASSERT(reporter, set0.count() == i + 1);
61 REPORTER_ASSERT(reporter, !set0.add(f(i)));
64 // Test deterministic output
65 for (int i = 0; i < COUNT; i++) {
66 REPORTER_ASSERT(reporter, set0[i] == f(i));
69 // Test copy constructor too.
70 SkTSet<int> set1 = set0;
72 REPORTER_ASSERT(reporter, set0.count() == set1.count());
73 REPORTER_ASSERT(reporter, !set1.contains(-1000));
75 for (int i = 0; i < COUNT; i++) {
76 REPORTER_ASSERT(reporter, set1.contains(f(i)));
77 REPORTER_ASSERT(reporter, set1[i] == f(i));
80 // Test operator= too.
84 REPORTER_ASSERT(reporter, set0.count() == set2.count());
85 REPORTER_ASSERT(reporter, !set2.contains(-1000));
87 for (int i = 0; i < COUNT; i++) {
88 REPORTER_ASSERT(reporter, set2.contains(f(i)));
89 REPORTER_ASSERT(reporter, set2[i] == f(i));
99 static void TestTSet_merge(skiatest::Reporter* reporter) {
103 for (int i = 0; i < COUNT; i++) {
104 REPORTER_ASSERT(reporter, set.add(2 * i));
105 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
107 // mergeInto returns the number of duplicates. Expected 0.
108 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0);
109 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
111 // mergeInto should now find all new numbers duplicate.
112 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count());
113 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
115 for (int i = 0; i < 2 * COUNT; i++) {
116 REPORTER_ASSERT(reporter, set.contains(i));
119 // check deterministic output
120 for (int i = 0; i < COUNT; i++) {
121 REPORTER_ASSERT(reporter, set[i] == 2 * i);
122 REPORTER_ASSERT(reporter, set[COUNT + i] == 2 * i + 1);
131 DEF_TEST(TSet, reporter) {
132 TestTSet_basic(reporter);
133 TestTSet_advanced(reporter);
134 TestTSet_merge(reporter);