blob: 4026c6a6f89db3cc27d3542821fb6878a1136721 [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 "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
edisonn@google.com04115a12013-02-25 20:07:24 +000010#include "SkTSet.h"
11
12// Tests the SkTSet<T> class template.
13// Functions that just call SkTDArray are not tested.
14
15static 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.
42static int f(int i) {
43 return (long(i) * PRIME1) % PRIME2;
44}
45
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000046// Will expose contains() too.
edisonn@google.com04115a12013-02-25 20:07:24 +000047static 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
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000065 // Test deterministic output
66 for (int i = 0; i < COUNT; i++) {
67 REPORTER_ASSERT(reporter, set0[i] == f(i));
68 }
69
edisonn@google.com04115a12013-02-25 20:07:24 +000070 // Test copy constructor too.
71 SkTSet<int> set1 = set0;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000072
edisonn@google.com04115a12013-02-25 20:07:24 +000073 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)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000078 REPORTER_ASSERT(reporter, set1[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000079 }
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000080
edisonn@google.com04115a12013-02-25 20:07:24 +000081 // 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)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000090 REPORTER_ASSERT(reporter, set2[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000091 }
92
93#ifdef SK_DEBUG
94 set0.validate();
95 set1.validate();
96 set2.validate();
97#endif
98}
99
100static void TestTSet_merge(skiatest::Reporter* reporter) {
101 SkTSet<int> set;
102 SkTSet<int> setOdd;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000103
edisonn@google.com04115a12013-02-25 20:07:24 +0000104 for (int i = 0; i < COUNT; i++) {
105 REPORTER_ASSERT(reporter, set.add(2 * i));
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000106 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
edisonn@google.com04115a12013-02-25 20:07:24 +0000107 }
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);
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000115
edisonn@google.com04115a12013-02-25 20:07:24 +0000116 for (int i = 0; i < 2 * COUNT; i++) {
117 REPORTER_ASSERT(reporter, set.contains(i));
118 }
119
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +0000120 // 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
edisonn@google.com04115a12013-02-25 20:07:24 +0000126#ifdef SK_DEBUG
127 set.validate();
128 setOdd.validate();
129#endif
130}
131
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000132DEF_TEST(TSet, reporter) {
edisonn@google.com04115a12013-02-25 20:07:24 +0000133 TestTSet_basic(reporter);
134 TestTSet_advanced(reporter);
135 TestTSet_merge(reporter);
136}