blob: 848986162d673eb24a9b1460d12615c7dbe8e0f8 [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
26 double* found = map.find(3);
27 REPORTER_ASSERT(r, found);
28 REPORTER_ASSERT(r, *found == 4.0);
29
mtklein02f46cf2015-03-20 13:48:42 -070030 map.foreach([](int key, double* d){ *d = -key; });
31 REPORTER_ASSERT(r, count(map) == 1);
32
mtklein979e0ea2015-02-12 13:20:08 -080033 found = map.find(3);
34 REPORTER_ASSERT(r, found);
35 REPORTER_ASSERT(r, *found == -3.0);
36
37 REPORTER_ASSERT(r, !map.find(2));
38
39 const int N = 20;
40
41 for (int i = 0; i < N; i++) {
42 map.set(i, 2.0*i);
43 }
44 for (int i = 0; i < N; i++) {
tfarina567ff2f2015-04-27 07:01:44 -070045 double* found = map.find(i);
mtklein979e0ea2015-02-12 13:20:08 -080046 REPORTER_ASSERT(r, found);
47 REPORTER_ASSERT(r, *found == i*2.0);
48 }
49 for (int i = N; i < 2*N; i++) {
50 REPORTER_ASSERT(r, !map.find(i));
51 }
52
53 REPORTER_ASSERT(r, map.count() == N);
mtklein2aa1f7e2015-02-20 12:35:32 -080054
mtklein33d73c32015-04-21 06:53:56 -070055 for (int i = 0; i < N/2; i++) {
56 map.remove(i);
57 }
58 for (int i = 0; i < N; i++) {
59 double* found = map.find(i);
60 REPORTER_ASSERT(r, (found == nullptr) == (i < N/2));
61 }
62 REPORTER_ASSERT(r, map.count() == N/2);
63
mtklein2aa1f7e2015-02-20 12:35:32 -080064 map.reset();
65 REPORTER_ASSERT(r, map.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080066}
67
mtklein979e0ea2015-02-12 13:20:08 -080068DEF_TEST(HashSet, r) {
mtklein02f46cf2015-03-20 13:48:42 -070069 SkTHashSet<SkString> set;
mtklein979e0ea2015-02-12 13:20:08 -080070
71 set.add(SkString("Hello"));
72 set.add(SkString("World"));
73
74 REPORTER_ASSERT(r, set.count() == 2);
75
76 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
77 REPORTER_ASSERT(r, set.contains(SkString("World")));
78 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
mtklein2aa1f7e2015-02-20 12:35:32 -080079
mtkleinfb8307c2015-04-01 11:21:27 -070080 REPORTER_ASSERT(r, set.find(SkString("Hello")));
81 REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello"));
82
mtklein33d73c32015-04-21 06:53:56 -070083 set.remove(SkString("Hello"));
84 REPORTER_ASSERT(r, !set.contains(SkString("Hello")));
85 REPORTER_ASSERT(r, set.count() == 1);
86
mtklein2aa1f7e2015-02-20 12:35:32 -080087 set.reset();
88 REPORTER_ASSERT(r, set.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080089}
fmalita79ca0812015-02-12 17:32:49 -080090
91namespace {
92
93class CopyCounter {
94public:
95 CopyCounter() : fID(0), fCounter(NULL) {}
96
97 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
98
99 CopyCounter(const CopyCounter& other)
100 : fID(other.fID)
101 , fCounter(other.fCounter) {
102 SkASSERT(fCounter);
103 *fCounter += 1;
104 }
105
106 void operator=(const CopyCounter& other) {
107 fID = other.fID;
108 fCounter = other.fCounter;
109 *fCounter += 1;
110 }
111
112 bool operator==(const CopyCounter& other) const {
113 return fID == other.fID;
114 }
115
116private:
117 uint32_t fID;
118 uint32_t* fCounter;
119};
120
121uint32_t hash_copy_counter(const CopyCounter&) {
122 return 0; // let them collide, what do we care?
123}
124
125}
126
127DEF_TEST(HashSetCopyCounter, r) {
128 SkTHashSet<CopyCounter, hash_copy_counter> set;
129
130 uint32_t globalCounter = 0;
131 CopyCounter copyCounter1(1, &globalCounter);
132 CopyCounter copyCounter2(2, &globalCounter);
133 REPORTER_ASSERT(r, globalCounter == 0);
134
135 set.add(copyCounter1);
136 REPORTER_ASSERT(r, globalCounter == 1);
137 REPORTER_ASSERT(r, set.contains(copyCounter1));
138 REPORTER_ASSERT(r, globalCounter == 1);
139 set.add(copyCounter1);
140 // We allow copies for same-value adds for now.
141 REPORTER_ASSERT(r, globalCounter == 2);
142
143 set.add(copyCounter2);
144 REPORTER_ASSERT(r, globalCounter == 3);
145 REPORTER_ASSERT(r, set.contains(copyCounter1));
146 REPORTER_ASSERT(r, set.contains(copyCounter2));
147 REPORTER_ASSERT(r, globalCounter == 3);
148 set.add(copyCounter1);
149 set.add(copyCounter2);
150 // We allow copies for same-value adds for now.
151 REPORTER_ASSERT(r, globalCounter == 5);
152}