blob: 623e597eeb2d48dc0238f4a72ae27a8043195558 [file] [log] [blame]
mtklein979e0ea2015-02-12 13:20:08 -08001#include "SkChecksum.h"
2#include "SkString.h"
3#include "SkTHash.h"
4#include "Test.h"
5
fmalita79ca0812015-02-12 17:32:49 -08006namespace { uint32_t hash_int(const int& k) { return SkChecksum::Mix(k); } }
mtklein979e0ea2015-02-12 13:20:08 -08007
8static void set_negative_key(int key, double* d) { *d = -key; }
9
10DEF_TEST(HashMap, r) {
11 SkTHashMap<int, double, hash_int> map;
12
13 map.set(3, 4.0);
14 REPORTER_ASSERT(r, map.count() == 1);
15
16 double* found = map.find(3);
17 REPORTER_ASSERT(r, found);
18 REPORTER_ASSERT(r, *found == 4.0);
19
20 map.foreach(set_negative_key);
21 found = map.find(3);
22 REPORTER_ASSERT(r, found);
23 REPORTER_ASSERT(r, *found == -3.0);
24
25 REPORTER_ASSERT(r, !map.find(2));
26
27 const int N = 20;
28
29 for (int i = 0; i < N; i++) {
30 map.set(i, 2.0*i);
31 }
32 for (int i = 0; i < N; i++) {
33 double* found = map.find(i);;
34 REPORTER_ASSERT(r, found);
35 REPORTER_ASSERT(r, *found == i*2.0);
36 }
37 for (int i = N; i < 2*N; i++) {
38 REPORTER_ASSERT(r, !map.find(i));
39 }
40
41 REPORTER_ASSERT(r, map.count() == N);
mtklein2aa1f7e2015-02-20 12:35:32 -080042
43 map.reset();
44 REPORTER_ASSERT(r, map.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080045}
46
fmalita79ca0812015-02-12 17:32:49 -080047namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size()); } }
mtklein979e0ea2015-02-12 13:20:08 -080048
49DEF_TEST(HashSet, r) {
50 SkTHashSet<SkString, hash_string> set;
51
52 set.add(SkString("Hello"));
53 set.add(SkString("World"));
54
55 REPORTER_ASSERT(r, set.count() == 2);
56
57 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
58 REPORTER_ASSERT(r, set.contains(SkString("World")));
59 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
mtklein2aa1f7e2015-02-20 12:35:32 -080060
61 set.reset();
62 REPORTER_ASSERT(r, set.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080063}
fmalita79ca0812015-02-12 17:32:49 -080064
65namespace {
66
67class CopyCounter {
68public:
69 CopyCounter() : fID(0), fCounter(NULL) {}
70
71 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
72
73 CopyCounter(const CopyCounter& other)
74 : fID(other.fID)
75 , fCounter(other.fCounter) {
76 SkASSERT(fCounter);
77 *fCounter += 1;
78 }
79
80 void operator=(const CopyCounter& other) {
81 fID = other.fID;
82 fCounter = other.fCounter;
83 *fCounter += 1;
84 }
85
86 bool operator==(const CopyCounter& other) const {
87 return fID == other.fID;
88 }
89
90private:
91 uint32_t fID;
92 uint32_t* fCounter;
93};
94
95uint32_t hash_copy_counter(const CopyCounter&) {
96 return 0; // let them collide, what do we care?
97}
98
99}
100
101DEF_TEST(HashSetCopyCounter, r) {
102 SkTHashSet<CopyCounter, hash_copy_counter> set;
103
104 uint32_t globalCounter = 0;
105 CopyCounter copyCounter1(1, &globalCounter);
106 CopyCounter copyCounter2(2, &globalCounter);
107 REPORTER_ASSERT(r, globalCounter == 0);
108
109 set.add(copyCounter1);
110 REPORTER_ASSERT(r, globalCounter == 1);
111 REPORTER_ASSERT(r, set.contains(copyCounter1));
112 REPORTER_ASSERT(r, globalCounter == 1);
113 set.add(copyCounter1);
114 // We allow copies for same-value adds for now.
115 REPORTER_ASSERT(r, globalCounter == 2);
116
117 set.add(copyCounter2);
118 REPORTER_ASSERT(r, globalCounter == 3);
119 REPORTER_ASSERT(r, set.contains(copyCounter1));
120 REPORTER_ASSERT(r, set.contains(copyCounter2));
121 REPORTER_ASSERT(r, globalCounter == 3);
122 set.add(copyCounter1);
123 set.add(copyCounter2);
124 // We allow copies for same-value adds for now.
125 REPORTER_ASSERT(r, globalCounter == 5);
126}