blob: 8f276f7bc74e5bba1b43467c78fd5d549486e573 [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 */
7#include "Test.h"
8#include "SkTSet.h"
9
10// Tests the SkTSet<T> class template.
11// Functions that just call SkTDArray are not tested.
12
13static void TestTSet_basic(skiatest::Reporter* reporter) {
14 SkTSet<int> set0;
15 REPORTER_ASSERT(reporter, set0.isEmpty());
16 REPORTER_ASSERT(reporter, !set0.contains(-1));
17 REPORTER_ASSERT(reporter, !set0.contains(0));
18 REPORTER_ASSERT(reporter, !set0.contains(1));
19 REPORTER_ASSERT(reporter, set0.count() == 0);
20
21 REPORTER_ASSERT(reporter, set0.add(0));
22 REPORTER_ASSERT(reporter, !set0.isEmpty());
23 REPORTER_ASSERT(reporter, !set0.contains(-1));
24 REPORTER_ASSERT(reporter, set0.contains(0));
25 REPORTER_ASSERT(reporter, !set0.contains(1));
26 REPORTER_ASSERT(reporter, set0.count() == 1);
27 REPORTER_ASSERT(reporter, !set0.add(0));
28 REPORTER_ASSERT(reporter, set0.count() == 1);
29
30#ifdef SK_DEBUG
31 set0.validate();
32#endif
33}
34
35#define COUNT 1732
36#define PRIME1 10007
37#define PRIME2 1733
38
39// Generates a series of positive unique pseudo-random numbers.
40static int f(int i) {
41 return (long(i) * PRIME1) % PRIME2;
42}
43
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000044// Will expose contains() too.
edisonn@google.com04115a12013-02-25 20:07:24 +000045static void TestTSet_advanced(skiatest::Reporter* reporter) {
46 SkTSet<int> set0;
47
48 for (int i = 0; i < COUNT; i++) {
49 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
50 if (i > 0) {
51 REPORTER_ASSERT(reporter, set0.contains(f(0)));
52 REPORTER_ASSERT(reporter, set0.contains(f(i / 2)));
53 REPORTER_ASSERT(reporter, set0.contains(f(i - 1)));
54 }
55 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
56 REPORTER_ASSERT(reporter, set0.count() == i);
57 REPORTER_ASSERT(reporter, set0.add(f(i)));
58 REPORTER_ASSERT(reporter, set0.contains(f(i)));
59 REPORTER_ASSERT(reporter, set0.count() == i + 1);
60 REPORTER_ASSERT(reporter, !set0.add(f(i)));
61 }
62
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000063 // Test deterministic output
64 for (int i = 0; i < COUNT; i++) {
65 REPORTER_ASSERT(reporter, set0[i] == f(i));
66 }
67
edisonn@google.com04115a12013-02-25 20:07:24 +000068 // Test copy constructor too.
69 SkTSet<int> set1 = set0;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000070
edisonn@google.com04115a12013-02-25 20:07:24 +000071 REPORTER_ASSERT(reporter, set0.count() == set1.count());
72 REPORTER_ASSERT(reporter, !set1.contains(-1000));
73
74 for (int i = 0; i < COUNT; i++) {
75 REPORTER_ASSERT(reporter, set1.contains(f(i)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000076 REPORTER_ASSERT(reporter, set1[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000077 }
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +000078
edisonn@google.com04115a12013-02-25 20:07:24 +000079 // Test operator= too.
80 SkTSet<int> set2;
81 set2 = set0;
82
83 REPORTER_ASSERT(reporter, set0.count() == set2.count());
84 REPORTER_ASSERT(reporter, !set2.contains(-1000));
85
86 for (int i = 0; i < COUNT; i++) {
87 REPORTER_ASSERT(reporter, set2.contains(f(i)));
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +000088 REPORTER_ASSERT(reporter, set2[i] == f(i));
edisonn@google.com04115a12013-02-25 20:07:24 +000089 }
90
91#ifdef SK_DEBUG
92 set0.validate();
93 set1.validate();
94 set2.validate();
95#endif
96}
97
98static void TestTSet_merge(skiatest::Reporter* reporter) {
99 SkTSet<int> set;
100 SkTSet<int> setOdd;
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000101
edisonn@google.com04115a12013-02-25 20:07:24 +0000102 for (int i = 0; i < COUNT; i++) {
103 REPORTER_ASSERT(reporter, set.add(2 * i));
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000104 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
edisonn@google.com04115a12013-02-25 20:07:24 +0000105 }
106 // mergeInto returns the number of duplicates. Expected 0.
107 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0);
108 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
109
110 // mergeInto should now find all new numbers duplicate.
111 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count());
112 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
skia.committer@gmail.com5ca3bd02013-02-26 07:01:22 +0000113
edisonn@google.com04115a12013-02-25 20:07:24 +0000114 for (int i = 0; i < 2 * COUNT; i++) {
115 REPORTER_ASSERT(reporter, set.contains(i));
116 }
117
commit-bot@chromium.orga7aa8102013-07-24 01:51:08 +0000118 // check deterministic output
119 for (int i = 0; i < COUNT; i++) {
120 REPORTER_ASSERT(reporter, set[i] == 2 * i);
121 REPORTER_ASSERT(reporter, set[COUNT + i] == 2 * i + 1);
122 }
123
edisonn@google.com04115a12013-02-25 20:07:24 +0000124#ifdef SK_DEBUG
125 set.validate();
126 setOdd.validate();
127#endif
128}
129
130static void TestTSet(skiatest::Reporter* reporter) {
131 TestTSet_basic(reporter);
132 TestTSet_advanced(reporter);
133 TestTSet_merge(reporter);
134}
135
136#include "TestClassDef.h"
137DEFINE_TESTCLASS("TSet", TSetTest, TestTSet)