blob: 3f826480ad52437bcef981386a4de8bf13efd7ea [file] [log] [blame]
edisonn@google.com04115a12013-02-25 20:07:24 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
edisonn@google.com04115a12013-02-25 20:07:24 +00008#include "SkTSet.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
edisonn@google.com04115a12013-02-25 20:07:24 +000010
11// Tests the SkTSet<T> class template.
12// Functions that just call SkTDArray are not tested.
13
14static void TestTSet_basic(skiatest::Reporter* reporter) {
15 SkTSet<int> set0;
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);
21
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);
30
31#ifdef SK_DEBUG
32 set0.validate();
33#endif
34}
35
36#define COUNT 1732
37#define PRIME1 10007
38#define PRIME2 1733
39
40// Generates a series of positive unique pseudo-random numbers.
41static int f(int i) {
42 return (long(i) * PRIME1) % PRIME2;
43}
44
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000045// Will expose contains() too.
edisonn@google.com04115a12013-02-25 20:07:24 +000046static void TestTSet_advanced(skiatest::Reporter* reporter) {
47 SkTSet<int> set0;
48
49 for (int i = 0; i < COUNT; i++) {
50 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
51 if (i > 0) {
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)));
55 }
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)));
62 }
63
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000064 // Test deterministic output
65 for (int i = 0; i < COUNT; i++) {
66 REPORTER_ASSERT(reporter, set0[i] == f(i));
67 }
68
edisonn@google.com04115a12013-02-25 20:07:24 +000069 // Test copy constructor too.
70 SkTSet<int> set1 = set0;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000071
edisonn@google.com04115a12013-02-25 20:07:24 +000072 REPORTER_ASSERT(reporter, set0.count() == set1.count());
73 REPORTER_ASSERT(reporter, !set1.contains(-1000));
74
75 for (int i = 0; i < COUNT; i++) {
76 REPORTER_ASSERT(reporter, set1.contains(f(i)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000077 REPORTER_ASSERT(reporter, set1[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000078 }
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000079
edisonn@google.com04115a12013-02-25 20:07:24 +000080 // Test operator= too.
81 SkTSet<int> set2;
82 set2 = set0;
83
84 REPORTER_ASSERT(reporter, set0.count() == set2.count());
85 REPORTER_ASSERT(reporter, !set2.contains(-1000));
86
87 for (int i = 0; i < COUNT; i++) {
88 REPORTER_ASSERT(reporter, set2.contains(f(i)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000089 REPORTER_ASSERT(reporter, set2[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000090 }
91
92#ifdef SK_DEBUG
93 set0.validate();
94 set1.validate();
95 set2.validate();
96#endif
97}
98
99static void TestTSet_merge(skiatest::Reporter* reporter) {
100 SkTSet<int> set;
101 SkTSet<int> setOdd;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000102
edisonn@google.com04115a12013-02-25 20:07:24 +0000103 for (int i = 0; i < COUNT; i++) {
104 REPORTER_ASSERT(reporter, set.add(2 * i));
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000105 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
edisonn@google.com04115a12013-02-25 20:07:24 +0000106 }
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);
110
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);
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000114
edisonn@google.com04115a12013-02-25 20:07:24 +0000115 for (int i = 0; i < 2 * COUNT; i++) {
116 REPORTER_ASSERT(reporter, set.contains(i));
117 }
118
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +0000119 // 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);
123 }
124
edisonn@google.com04115a12013-02-25 20:07:24 +0000125#ifdef SK_DEBUG
126 set.validate();
127 setOdd.validate();
128#endif
129}
130
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000131DEF_TEST(TSet, reporter) {
edisonn@google.com04115a12013-02-25 20:07:24 +0000132 TestTSet_basic(reporter);
133 TestTSet_advanced(reporter);
134 TestTSet_merge(reporter);
135}