Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: tests/GrSetTest.cpp

Issue 176903003: Add GrSet class built on top of RedBlackTree (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Bench Added Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/gpu/GrSet.h ('K') | « src/gpu/GrSet.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 // This is a GPU-backend specific test
9 #if SK_SUPPORT_GPU
10
11 #include "GrSet.h"
12 #include "SkRandom.h"
13 #include "Test.h"
14
15 typedef GrSet<int> Set;
16 typedef GrSet<const char*, GrStrLess> Set2;
17
18 DEF_TEST(GrSetTest, reporter) {
19 Set set;
20
21 REPORTER_ASSERT(reporter, set.empty());
22
23 SkRandom r;
24
25 int count[1000] = {0};
26 // add 10K ints
27 for (int i = 0; i < 10000; ++i) {
28 int x = r.nextU() % 1000;
29 Set::Iter xi = set.insert(x);
30 REPORTER_ASSERT(reporter, *xi == x);
31 REPORTER_ASSERT(reporter, !set.empty());
32 count[x] = 1;
33 }
34 set.insert(0);
35 count[0] = 1;
36 set.insert(999);
37 count[999] = 1;
38 int totalCount = 0;
39 for (int i = 0; i < 1000; ++i) {
40 totalCount += count[i];
41 }
42 REPORTER_ASSERT(reporter, *set.begin() == 0);
43 REPORTER_ASSERT(reporter, *set.last() == 999);
44 REPORTER_ASSERT(reporter, --(++set.begin()) == set.begin());
45 REPORTER_ASSERT(reporter, --set.end() == set.last());
46 REPORTER_ASSERT(reporter, set.count() == totalCount);
47
48 int c = 0;
49 // check that we iterate through the correct number of
50 // elements and they are properly sorted.
51 for (Set::Iter a = set.begin(); set.end() != a; ++a) {
52 Set::Iter b = a;
53 ++b;
54 ++c;
55 REPORTER_ASSERT(reporter, b == set.end() || *a <= *b);
56 }
57 REPORTER_ASSERT(reporter, c == set.count());
58
59 // check that the set finds all ints and only ints added to set
60 for (int i = 0; i < 1000; ++i) {
61 bool existsFind = set.find(i) != set.end();
62 bool existsCount = 0 != count[i];
63 REPORTER_ASSERT(reporter, existsFind == existsCount);
64 }
65 // remove all the ints between 100 and 200.
66 for (int i = 100; i < 200; ++i) {
67 set.remove(set.find(i));
68 if (1 == count[i]) {
69 count[i] = 0;
70 --totalCount;
71 }
72 REPORTER_ASSERT(reporter, set.count() == totalCount);
73 REPORTER_ASSERT(reporter, set.find(i) == set.end());
74 }
75 // remove the 0 entry. (tests removing begin())
76 REPORTER_ASSERT(reporter, *set.begin() == 0);
77 REPORTER_ASSERT(reporter, *(--set.end()) == 999);
78 set.remove(set.find(0));
79 count[0] = 0;
80 --totalCount;
81 REPORTER_ASSERT(reporter, set.count() == totalCount);
82 REPORTER_ASSERT(reporter, set.find(0) == set.end());
83 REPORTER_ASSERT(reporter, 0 < *set.begin());
84
85 // remove all the 999 entries (tests removing last()).
86 set.remove(set.find(999));
87 count[999] = 0;
88 --totalCount;
89 REPORTER_ASSERT(reporter, set.count() == totalCount);
90 REPORTER_ASSERT(reporter, set.find(999) == set.end());
91 REPORTER_ASSERT(reporter, 999 > *(--set.end()));
92 REPORTER_ASSERT(reporter, set.last() == --set.end());
93
94 // Make sure iteration still goes through correct number of entries
95 // and is still sorted correctly.
96 c = 0;
97 for (Set::Iter a = set.begin(); set.end() != a; ++a) {
98 Set::Iter b = a;
99 ++b;
100 ++c;
101 REPORTER_ASSERT(reporter, b == set.end() || *a <= *b);
102 }
103 REPORTER_ASSERT(reporter, c == set.count());
104
105 // repeat check that the set finds all ints and only ints added to set
106 for (int i = 0; i < 1000; ++i) {
107 bool existsFind = set.find(i) != set.end();
108 bool existsCount = 0 != count[i];
109 REPORTER_ASSERT(reporter, existsFind == existsCount);
110 }
111
112 // remove all entries
113 while (!set.empty()) {
114 set.remove(set.begin());
115 }
116
117 // test reset on empty set.
118 set.reset();
119 REPORTER_ASSERT(reporter, set.empty());
120
121 const char* char1 = "dog";
122 const char* char2 = "cat";
123 const char* char3 = "dog";
124
125 Set2 set2;
126
127 set2.insert("ape");
128 set2.insert(char1);
129 set2.insert(char2);
130 set2.insert(char3);
131 set2.insert("ant");
132 set2.insert("cat");
133
134 REPORTER_ASSERT(reporter, set2.count() == 4);
135 REPORTER_ASSERT(reporter, set2.find("dog") == set2.last());
136 REPORTER_ASSERT(reporter, set2.find("cat") != set2.end());
137 REPORTER_ASSERT(reporter, set2.find("ant") == set2.begin());
138 REPORTER_ASSERT(reporter, set2.find("bug") == set2.end());
139
140 set2.remove(set2.find("ant"));
141 REPORTER_ASSERT(reporter, set2.find("ant") == set2.end());
142 REPORTER_ASSERT(reporter, set2.count() == 3);
143
144 set2.reset();
145 REPORTER_ASSERT(reporter, set2.empty());
146 }
147
148 #endif
149
OLDNEW
« src/gpu/GrSet.h ('K') | « src/gpu/GrSet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698