blob: d2acafe08b04aef88c2b71d013a3ce0db0097c17 [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++) {
45 double* found = map.find(i);;
46 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
55 map.reset();
56 REPORTER_ASSERT(r, map.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080057}
58
mtklein979e0ea2015-02-12 13:20:08 -080059DEF_TEST(HashSet, r) {
mtklein02f46cf2015-03-20 13:48:42 -070060 SkTHashSet<SkString> set;
mtklein979e0ea2015-02-12 13:20:08 -080061
62 set.add(SkString("Hello"));
63 set.add(SkString("World"));
64
65 REPORTER_ASSERT(r, set.count() == 2);
66
67 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
68 REPORTER_ASSERT(r, set.contains(SkString("World")));
69 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
mtklein2aa1f7e2015-02-20 12:35:32 -080070
mtkleinfb8307c2015-04-01 11:21:27 -070071 REPORTER_ASSERT(r, set.find(SkString("Hello")));
72 REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello"));
73
mtklein2aa1f7e2015-02-20 12:35:32 -080074 set.reset();
75 REPORTER_ASSERT(r, set.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080076}
fmalita79ca0812015-02-12 17:32:49 -080077
78namespace {
79
80class CopyCounter {
81public:
82 CopyCounter() : fID(0), fCounter(NULL) {}
83
84 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
85
86 CopyCounter(const CopyCounter& other)
87 : fID(other.fID)
88 , fCounter(other.fCounter) {
89 SkASSERT(fCounter);
90 *fCounter += 1;
91 }
92
93 void operator=(const CopyCounter& other) {
94 fID = other.fID;
95 fCounter = other.fCounter;
96 *fCounter += 1;
97 }
98
99 bool operator==(const CopyCounter& other) const {
100 return fID == other.fID;
101 }
102
103private:
104 uint32_t fID;
105 uint32_t* fCounter;
106};
107
108uint32_t hash_copy_counter(const CopyCounter&) {
109 return 0; // let them collide, what do we care?
110}
111
112}
113
114DEF_TEST(HashSetCopyCounter, r) {
115 SkTHashSet<CopyCounter, hash_copy_counter> set;
116
117 uint32_t globalCounter = 0;
118 CopyCounter copyCounter1(1, &globalCounter);
119 CopyCounter copyCounter2(2, &globalCounter);
120 REPORTER_ASSERT(r, globalCounter == 0);
121
122 set.add(copyCounter1);
123 REPORTER_ASSERT(r, globalCounter == 1);
124 REPORTER_ASSERT(r, set.contains(copyCounter1));
125 REPORTER_ASSERT(r, globalCounter == 1);
126 set.add(copyCounter1);
127 // We allow copies for same-value adds for now.
128 REPORTER_ASSERT(r, globalCounter == 2);
129
130 set.add(copyCounter2);
131 REPORTER_ASSERT(r, globalCounter == 3);
132 REPORTER_ASSERT(r, set.contains(copyCounter1));
133 REPORTER_ASSERT(r, set.contains(copyCounter2));
134 REPORTER_ASSERT(r, globalCounter == 3);
135 set.add(copyCounter1);
136 set.add(copyCounter2);
137 // We allow copies for same-value adds for now.
138 REPORTER_ASSERT(r, globalCounter == 5);
139}