blob: 00857f63dcd61377090f2fc01023e22b84d4376a [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);
42}
43
fmalita79ca0812015-02-12 17:32:49 -080044namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size()); } }
mtklein979e0ea2015-02-12 13:20:08 -080045
46DEF_TEST(HashSet, r) {
47 SkTHashSet<SkString, hash_string> set;
48
49 set.add(SkString("Hello"));
50 set.add(SkString("World"));
51
52 REPORTER_ASSERT(r, set.count() == 2);
53
54 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
55 REPORTER_ASSERT(r, set.contains(SkString("World")));
56 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
57}
fmalita79ca0812015-02-12 17:32:49 -080058
59namespace {
60
61class CopyCounter {
62public:
63 CopyCounter() : fID(0), fCounter(NULL) {}
64
65 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
66
67 CopyCounter(const CopyCounter& other)
68 : fID(other.fID)
69 , fCounter(other.fCounter) {
70 SkASSERT(fCounter);
71 *fCounter += 1;
72 }
73
74 void operator=(const CopyCounter& other) {
75 fID = other.fID;
76 fCounter = other.fCounter;
77 *fCounter += 1;
78 }
79
80 bool operator==(const CopyCounter& other) const {
81 return fID == other.fID;
82 }
83
84private:
85 uint32_t fID;
86 uint32_t* fCounter;
87};
88
89uint32_t hash_copy_counter(const CopyCounter&) {
90 return 0; // let them collide, what do we care?
91}
92
93}
94
95DEF_TEST(HashSetCopyCounter, r) {
96 SkTHashSet<CopyCounter, hash_copy_counter> set;
97
98 uint32_t globalCounter = 0;
99 CopyCounter copyCounter1(1, &globalCounter);
100 CopyCounter copyCounter2(2, &globalCounter);
101 REPORTER_ASSERT(r, globalCounter == 0);
102
103 set.add(copyCounter1);
104 REPORTER_ASSERT(r, globalCounter == 1);
105 REPORTER_ASSERT(r, set.contains(copyCounter1));
106 REPORTER_ASSERT(r, globalCounter == 1);
107 set.add(copyCounter1);
108 // We allow copies for same-value adds for now.
109 REPORTER_ASSERT(r, globalCounter == 2);
110
111 set.add(copyCounter2);
112 REPORTER_ASSERT(r, globalCounter == 3);
113 REPORTER_ASSERT(r, set.contains(copyCounter1));
114 REPORTER_ASSERT(r, set.contains(copyCounter2));
115 REPORTER_ASSERT(r, globalCounter == 3);
116 set.add(copyCounter1);
117 set.add(copyCounter2);
118 // We allow copies for same-value adds for now.
119 REPORTER_ASSERT(r, globalCounter == 5);
120}