blob: c9b1bc967bd852bbf848e8c2f166d8e05da70187 [file] [log] [blame]
mtkleinfb8307c2015-04-01 11:21:27 -07001/*
2 * Copyright 2015 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
mtklein979e0ea2015-02-12 13:20:08 -08008#include "SkChecksum.h"
9#include "SkString.h"
10#include "SkTHash.h"
11#include "Test.h"
12
mtklein02f46cf2015-03-20 13:48:42 -070013// Tests use of const foreach(). map.count() is of course the better way to do this.
14static int count(const SkTHashMap<int, double>& map) {
15 int n = 0;
16 map.foreach([&n](int, double) { n++; });
17 return n;
18}
mtklein979e0ea2015-02-12 13:20:08 -080019
20DEF_TEST(HashMap, r) {
mtklein02f46cf2015-03-20 13:48:42 -070021 SkTHashMap<int, double> map;
mtklein979e0ea2015-02-12 13:20:08 -080022
23 map.set(3, 4.0);
24 REPORTER_ASSERT(r, map.count() == 1);
25
mtklein469a3fe2015-08-07 09:33:37 -070026 REPORTER_ASSERT(r, map.approxBytesUsed() > 0);
27
mtklein979e0ea2015-02-12 13:20:08 -080028 double* found = map.find(3);
29 REPORTER_ASSERT(r, found);
30 REPORTER_ASSERT(r, *found == 4.0);
31
mtklein02f46cf2015-03-20 13:48:42 -070032 map.foreach([](int key, double* d){ *d = -key; });
33 REPORTER_ASSERT(r, count(map) == 1);
34
mtklein979e0ea2015-02-12 13:20:08 -080035 found = map.find(3);
36 REPORTER_ASSERT(r, found);
37 REPORTER_ASSERT(r, *found == -3.0);
38
39 REPORTER_ASSERT(r, !map.find(2));
40
41 const int N = 20;
42
43 for (int i = 0; i < N; i++) {
44 map.set(i, 2.0*i);
45 }
46 for (int i = 0; i < N; i++) {
tfarina567ff2f2015-04-27 07:01:44 -070047 double* found = map.find(i);
mtklein979e0ea2015-02-12 13:20:08 -080048 REPORTER_ASSERT(r, found);
49 REPORTER_ASSERT(r, *found == i*2.0);
50 }
51 for (int i = N; i < 2*N; i++) {
52 REPORTER_ASSERT(r, !map.find(i));
53 }
54
55 REPORTER_ASSERT(r, map.count() == N);
mtklein2aa1f7e2015-02-20 12:35:32 -080056
mtklein33d73c32015-04-21 06:53:56 -070057 for (int i = 0; i < N/2; i++) {
58 map.remove(i);
59 }
60 for (int i = 0; i < N; i++) {
61 double* found = map.find(i);
62 REPORTER_ASSERT(r, (found == nullptr) == (i < N/2));
63 }
64 REPORTER_ASSERT(r, map.count() == N/2);
65
mtklein2aa1f7e2015-02-20 12:35:32 -080066 map.reset();
67 REPORTER_ASSERT(r, map.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080068}
69
mtklein979e0ea2015-02-12 13:20:08 -080070DEF_TEST(HashSet, r) {
mtklein02f46cf2015-03-20 13:48:42 -070071 SkTHashSet<SkString> set;
mtklein979e0ea2015-02-12 13:20:08 -080072
73 set.add(SkString("Hello"));
74 set.add(SkString("World"));
75
76 REPORTER_ASSERT(r, set.count() == 2);
77
78 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
79 REPORTER_ASSERT(r, set.contains(SkString("World")));
80 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
mtklein2aa1f7e2015-02-20 12:35:32 -080081
mtkleinfb8307c2015-04-01 11:21:27 -070082 REPORTER_ASSERT(r, set.find(SkString("Hello")));
83 REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello"));
84
mtklein33d73c32015-04-21 06:53:56 -070085 set.remove(SkString("Hello"));
86 REPORTER_ASSERT(r, !set.contains(SkString("Hello")));
87 REPORTER_ASSERT(r, set.count() == 1);
88
mtklein2aa1f7e2015-02-20 12:35:32 -080089 set.reset();
90 REPORTER_ASSERT(r, set.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080091}
fmalita79ca0812015-02-12 17:32:49 -080092
93namespace {
94
95class CopyCounter {
96public:
halcanary96fcdcc2015-08-27 07:41:13 -070097 CopyCounter() : fID(0), fCounter(nullptr) {}
fmalita79ca0812015-02-12 17:32:49 -080098
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
118private:
119 uint32_t fID;
120 uint32_t* fCounter;
121};
122
mtkleinc8d1dd42015-10-15 12:23:01 -0700123struct HashCopyCounter {
124 uint32_t operator()(const CopyCounter&) const {
125 return 0; // let them collide, what do we care?
126 }
127};
fmalita79ca0812015-02-12 17:32:49 -0800128
129}
130
131DEF_TEST(HashSetCopyCounter, r) {
mtkleinc8d1dd42015-10-15 12:23:01 -0700132 SkTHashSet<CopyCounter, HashCopyCounter> set;
fmalita79ca0812015-02-12 17:32:49 -0800133
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}