OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 | 7 |
8 #include "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkString.h" | 9 #include "SkString.h" |
10 #include "SkTHash.h" | 10 #include "SkTHash.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello")); | 83 REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello")); |
84 | 84 |
85 set.remove(SkString("Hello")); | 85 set.remove(SkString("Hello")); |
86 REPORTER_ASSERT(r, !set.contains(SkString("Hello"))); | 86 REPORTER_ASSERT(r, !set.contains(SkString("Hello"))); |
87 REPORTER_ASSERT(r, set.count() == 1); | 87 REPORTER_ASSERT(r, set.count() == 1); |
88 | 88 |
89 set.reset(); | 89 set.reset(); |
90 REPORTER_ASSERT(r, set.count() == 0); | 90 REPORTER_ASSERT(r, set.count() == 0); |
91 } | 91 } |
92 | 92 |
93 namespace { | |
94 | |
95 class CopyCounter { | |
96 public: | |
97 CopyCounter() : fID(0), fCounter(nullptr) {} | |
98 | |
99 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {} | |
100 | |
101 CopyCounter(const CopyCounter& other) | |
102 : fID(other.fID) | |
103 , fCounter(other.fCounter) { | |
104 SkASSERT(fCounter); | |
105 *fCounter += 1; | |
106 } | |
107 | |
108 void operator=(const CopyCounter& other) { | |
109 fID = other.fID; | |
110 fCounter = other.fCounter; | |
111 *fCounter += 1; | |
112 } | |
113 | |
114 bool operator==(const CopyCounter& other) const { | |
115 return fID == other.fID; | |
116 } | |
117 | |
118 private: | |
119 uint32_t fID; | |
120 uint32_t* fCounter; | |
121 }; | |
122 | |
123 struct HashCopyCounter { | |
124 uint32_t operator()(const CopyCounter&) const { | |
125 return 0; // let them collide, what do we care? | |
126 } | |
127 }; | |
128 | |
129 } | |
130 | |
131 DEF_TEST(HashSetCopyCounter, r) { | |
132 SkTHashSet<CopyCounter, HashCopyCounter> set; | |
133 | |
134 uint32_t globalCounter = 0; | |
135 CopyCounter copyCounter1(1, &globalCounter); | |
136 CopyCounter copyCounter2(2, &globalCounter); | |
137 REPORTER_ASSERT(r, globalCounter == 0); | |
138 | |
139 set.add(copyCounter1); | |
140 REPORTER_ASSERT(r, globalCounter == 1); | |
141 REPORTER_ASSERT(r, set.contains(copyCounter1)); | |
142 REPORTER_ASSERT(r, globalCounter == 1); | |
143 set.add(copyCounter1); | |
144 // We allow copies for same-value adds for now. | |
145 REPORTER_ASSERT(r, globalCounter == 2); | |
146 | |
147 set.add(copyCounter2); | |
148 REPORTER_ASSERT(r, globalCounter == 3); | |
149 REPORTER_ASSERT(r, set.contains(copyCounter1)); | |
150 REPORTER_ASSERT(r, set.contains(copyCounter2)); | |
151 REPORTER_ASSERT(r, globalCounter == 3); | |
152 set.add(copyCounter1); | |
153 set.add(copyCounter2); | |
154 // We allow copies for same-value adds for now. | |
155 REPORTER_ASSERT(r, globalCounter == 5); | |
156 } | |
OLD | NEW |