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

Side by Side Diff: tests/TSetTest.cpp

Issue 19283005: Deterministic SkTSet and PDF Output (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Add sorting for testing in SkTSet::validate() and some code review fixes Created 7 years, 5 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 | Annotate | Revision Log
« src/pdf/SkTSet.h ('K') | « src/pdf/SkTSet.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
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include "Test.h" 7 #include "Test.h"
8 #include "SkTSet.h" 8 #include "SkTSet.h"
9 9
10 // Tests the SkTSet<T> class template. 10 // Tests the SkTSet<T> class template.
(...skipping 23 matching lines...) Expand all
34 34
35 #define COUNT 1732 35 #define COUNT 1732
36 #define PRIME1 10007 36 #define PRIME1 10007
37 #define PRIME2 1733 37 #define PRIME2 1733
38 38
39 // Generates a series of positive unique pseudo-random numbers. 39 // Generates a series of positive unique pseudo-random numbers.
40 static int f(int i) { 40 static int f(int i) {
41 return (long(i) * PRIME1) % PRIME2; 41 return (long(i) * PRIME1) % PRIME2;
42 } 42 }
43 43
44 // Will expose contains() and find() too. 44 // Will expose contains() too.
45 static void TestTSet_advanced(skiatest::Reporter* reporter) { 45 static void TestTSet_advanced(skiatest::Reporter* reporter) {
46 SkTSet<int> set0; 46 SkTSet<int> set0;
47 47
48 for (int i = 0; i < COUNT; i++) { 48 for (int i = 0; i < COUNT; i++) {
49 REPORTER_ASSERT(reporter, !set0.contains(f(i))); 49 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
50 if (i > 0) { 50 if (i > 0) {
51 REPORTER_ASSERT(reporter, set0.contains(f(0))); 51 REPORTER_ASSERT(reporter, set0.contains(f(0)));
52 REPORTER_ASSERT(reporter, set0.contains(f(i / 2))); 52 REPORTER_ASSERT(reporter, set0.contains(f(i / 2)));
53 REPORTER_ASSERT(reporter, set0.contains(f(i - 1))); 53 REPORTER_ASSERT(reporter, set0.contains(f(i - 1)));
54 } 54 }
55 REPORTER_ASSERT(reporter, !set0.contains(f(i))); 55 REPORTER_ASSERT(reporter, !set0.contains(f(i)));
56 REPORTER_ASSERT(reporter, set0.count() == i); 56 REPORTER_ASSERT(reporter, set0.count() == i);
57 REPORTER_ASSERT(reporter, set0.add(f(i))); 57 REPORTER_ASSERT(reporter, set0.add(f(i)));
58 REPORTER_ASSERT(reporter, set0.contains(f(i))); 58 REPORTER_ASSERT(reporter, set0.contains(f(i)));
59 REPORTER_ASSERT(reporter, set0.count() == i + 1); 59 REPORTER_ASSERT(reporter, set0.count() == i + 1);
60 REPORTER_ASSERT(reporter, !set0.add(f(i))); 60 REPORTER_ASSERT(reporter, !set0.add(f(i)));
61 } 61 }
62 62
63 // Test deterministic output
64 for (int i = 0; i < COUNT; i++) {
65 REPORTER_ASSERT(reporter, set0[i] == f(i));
66 }
67
63 // Test copy constructor too. 68 // Test copy constructor too.
64 SkTSet<int> set1 = set0; 69 SkTSet<int> set1 = set0;
65 70
66 REPORTER_ASSERT(reporter, set0.count() == set1.count()); 71 REPORTER_ASSERT(reporter, set0.count() == set1.count());
67 REPORTER_ASSERT(reporter, !set1.contains(-1000)); 72 REPORTER_ASSERT(reporter, !set1.contains(-1000));
68 73
69 for (int i = 0; i < COUNT; i++) { 74 for (int i = 0; i < COUNT; i++) {
70 REPORTER_ASSERT(reporter, set1.contains(f(i))); 75 REPORTER_ASSERT(reporter, set1.contains(f(i)));
76 REPORTER_ASSERT(reporter, set1[i] == f(i));
71 } 77 }
72 78
73 // Test operator= too. 79 // Test operator= too.
74 SkTSet<int> set2; 80 SkTSet<int> set2;
75 set2 = set0; 81 set2 = set0;
76 82
77 REPORTER_ASSERT(reporter, set0.count() == set2.count()); 83 REPORTER_ASSERT(reporter, set0.count() == set2.count());
78 REPORTER_ASSERT(reporter, !set2.contains(-1000)); 84 REPORTER_ASSERT(reporter, !set2.contains(-1000));
79 85
80 for (int i = 0; i < COUNT; i++) { 86 for (int i = 0; i < COUNT; i++) {
81 REPORTER_ASSERT(reporter, set2.contains(f(i))); 87 REPORTER_ASSERT(reporter, set2.contains(f(i)));
88 REPORTER_ASSERT(reporter, set2[i] == f(i));
82 } 89 }
83 90
84 #ifdef SK_DEBUG 91 #ifdef SK_DEBUG
85 set0.validate(); 92 set0.validate();
86 set1.validate(); 93 set1.validate();
87 set2.validate(); 94 set2.validate();
88 #endif 95 #endif
89 } 96 }
90 97
91 static void TestTSet_merge(skiatest::Reporter* reporter) { 98 static void TestTSet_merge(skiatest::Reporter* reporter) {
92 SkTSet<int> set; 99 SkTSet<int> set;
93 SkTSet<int> setOdd; 100 SkTSet<int> setOdd;
94 101
95 for (int i = 0; i < COUNT; i++) { 102 for (int i = 0; i < COUNT; i++) {
96 REPORTER_ASSERT(reporter, set.add(2 * i)); 103 REPORTER_ASSERT(reporter, set.add(2 * i));
97 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1)); 104 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1));
98 } 105 }
99 // mergeInto returns the number of duplicates. Expected 0. 106 // mergeInto returns the number of duplicates. Expected 0.
100 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0); 107 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0);
101 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); 108 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
102 109
103 // mergeInto should now find all new numbers duplicate. 110 // mergeInto should now find all new numbers duplicate.
104 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count()); 111 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count());
105 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); 112 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT);
106 113
107 for (int i = 0; i < 2 * COUNT; i++) { 114 for (int i = 0; i < 2 * COUNT; i++) {
108 REPORTER_ASSERT(reporter, set.contains(i)); 115 REPORTER_ASSERT(reporter, set.contains(i));
109 } 116 }
110 117
118 // 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
111 #ifdef SK_DEBUG 124 #ifdef SK_DEBUG
112 set.validate(); 125 set.validate();
113 setOdd.validate(); 126 setOdd.validate();
114 #endif 127 #endif
115 } 128 }
116 129
117 static void TestTSet(skiatest::Reporter* reporter) { 130 static void TestTSet(skiatest::Reporter* reporter) {
118 TestTSet_basic(reporter); 131 TestTSet_basic(reporter);
119 TestTSet_advanced(reporter); 132 TestTSet_advanced(reporter);
120 TestTSet_merge(reporter); 133 TestTSet_merge(reporter);
121 } 134 }
122 135
123 #include "TestClassDef.h" 136 #include "TestClassDef.h"
124 DEFINE_TESTCLASS("TSet", TSetTest, TestTSet) 137 DEFINE_TESTCLASS("TSet", TSetTest, TestTSet)
OLDNEW
« src/pdf/SkTSet.h ('K') | « src/pdf/SkTSet.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698