blob: 5abf3db50a56885bd4fa7c0931590c6d1f2323d4 [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
mtklein02f46cf2015-03-20 13:48:42 -07006// Tests use of const foreach(). map.count() is of course the better way to do this.
7static int count(const SkTHashMap<int, double>& map) {
8 int n = 0;
9 map.foreach([&n](int, double) { n++; });
10 return n;
11}
mtklein979e0ea2015-02-12 13:20:08 -080012
13DEF_TEST(HashMap, r) {
mtklein02f46cf2015-03-20 13:48:42 -070014 SkTHashMap<int, double> map;
mtklein979e0ea2015-02-12 13:20:08 -080015
16 map.set(3, 4.0);
17 REPORTER_ASSERT(r, map.count() == 1);
18
19 double* found = map.find(3);
20 REPORTER_ASSERT(r, found);
21 REPORTER_ASSERT(r, *found == 4.0);
22
mtklein02f46cf2015-03-20 13:48:42 -070023 map.foreach([](int key, double* d){ *d = -key; });
24 REPORTER_ASSERT(r, count(map) == 1);
25
mtklein979e0ea2015-02-12 13:20:08 -080026 found = map.find(3);
27 REPORTER_ASSERT(r, found);
28 REPORTER_ASSERT(r, *found == -3.0);
29
30 REPORTER_ASSERT(r, !map.find(2));
31
32 const int N = 20;
33
34 for (int i = 0; i < N; i++) {
35 map.set(i, 2.0*i);
36 }
37 for (int i = 0; i < N; i++) {
38 double* found = map.find(i);;
39 REPORTER_ASSERT(r, found);
40 REPORTER_ASSERT(r, *found == i*2.0);
41 }
42 for (int i = N; i < 2*N; i++) {
43 REPORTER_ASSERT(r, !map.find(i));
44 }
45
46 REPORTER_ASSERT(r, map.count() == N);
mtklein2aa1f7e2015-02-20 12:35:32 -080047
48 map.reset();
49 REPORTER_ASSERT(r, map.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080050}
51
mtklein979e0ea2015-02-12 13:20:08 -080052DEF_TEST(HashSet, r) {
mtklein02f46cf2015-03-20 13:48:42 -070053 SkTHashSet<SkString> set;
mtklein979e0ea2015-02-12 13:20:08 -080054
55 set.add(SkString("Hello"));
56 set.add(SkString("World"));
57
58 REPORTER_ASSERT(r, set.count() == 2);
59
60 REPORTER_ASSERT(r, set.contains(SkString("Hello")));
61 REPORTER_ASSERT(r, set.contains(SkString("World")));
62 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
mtklein2aa1f7e2015-02-20 12:35:32 -080063
64 set.reset();
65 REPORTER_ASSERT(r, set.count() == 0);
mtklein979e0ea2015-02-12 13:20:08 -080066}
fmalita79ca0812015-02-12 17:32:49 -080067
68namespace {
69
70class CopyCounter {
71public:
72 CopyCounter() : fID(0), fCounter(NULL) {}
73
74 CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
75
76 CopyCounter(const CopyCounter& other)
77 : fID(other.fID)
78 , fCounter(other.fCounter) {
79 SkASSERT(fCounter);
80 *fCounter += 1;
81 }
82
83 void operator=(const CopyCounter& other) {
84 fID = other.fID;
85 fCounter = other.fCounter;
86 *fCounter += 1;
87 }
88
89 bool operator==(const CopyCounter& other) const {
90 return fID == other.fID;
91 }
92
93private:
94 uint32_t fID;
95 uint32_t* fCounter;
96};
97
98uint32_t hash_copy_counter(const CopyCounter&) {
99 return 0; // let them collide, what do we care?
100}
101
102}
103
104DEF_TEST(HashSetCopyCounter, r) {
105 SkTHashSet<CopyCounter, hash_copy_counter> set;
106
107 uint32_t globalCounter = 0;
108 CopyCounter copyCounter1(1, &globalCounter);
109 CopyCounter copyCounter2(2, &globalCounter);
110 REPORTER_ASSERT(r, globalCounter == 0);
111
112 set.add(copyCounter1);
113 REPORTER_ASSERT(r, globalCounter == 1);
114 REPORTER_ASSERT(r, set.contains(copyCounter1));
115 REPORTER_ASSERT(r, globalCounter == 1);
116 set.add(copyCounter1);
117 // We allow copies for same-value adds for now.
118 REPORTER_ASSERT(r, globalCounter == 2);
119
120 set.add(copyCounter2);
121 REPORTER_ASSERT(r, globalCounter == 3);
122 REPORTER_ASSERT(r, set.contains(copyCounter1));
123 REPORTER_ASSERT(r, set.contains(copyCounter2));
124 REPORTER_ASSERT(r, globalCounter == 3);
125 set.add(copyCounter1);
126 set.add(copyCounter2);
127 // We allow copies for same-value adds for now.
128 REPORTER_ASSERT(r, globalCounter == 5);
129}