OLD | NEW |
1 #include "SkChecksum.h" | 1 #include "SkChecksum.h" |
2 #include "SkString.h" | 2 #include "SkString.h" |
3 #include "SkTHash.h" | 3 #include "SkTHash.h" |
4 #include "Test.h" | 4 #include "Test.h" |
5 | 5 |
6 namespace { uint32_t hash_int(const int& k) { return SkChecksum::Mix(k); } } | 6 // Tests use of const foreach(). map.count() is of course the better way to do
this. |
7 | 7 static int count(const SkTHashMap<int, double>& map) { |
8 static void set_negative_key(int key, double* d) { *d = -key; } | 8 int n = 0; |
| 9 map.foreach([&n](int, double) { n++; }); |
| 10 return n; |
| 11 } |
9 | 12 |
10 DEF_TEST(HashMap, r) { | 13 DEF_TEST(HashMap, r) { |
11 SkTHashMap<int, double, hash_int> map; | 14 SkTHashMap<int, double> map; |
12 | 15 |
13 map.set(3, 4.0); | 16 map.set(3, 4.0); |
14 REPORTER_ASSERT(r, map.count() == 1); | 17 REPORTER_ASSERT(r, map.count() == 1); |
15 | 18 |
16 double* found = map.find(3); | 19 double* found = map.find(3); |
17 REPORTER_ASSERT(r, found); | 20 REPORTER_ASSERT(r, found); |
18 REPORTER_ASSERT(r, *found == 4.0); | 21 REPORTER_ASSERT(r, *found == 4.0); |
19 | 22 |
20 map.foreach(set_negative_key); | 23 map.foreach([](int key, double* d){ *d = -key; }); |
| 24 REPORTER_ASSERT(r, count(map) == 1); |
| 25 |
21 found = map.find(3); | 26 found = map.find(3); |
22 REPORTER_ASSERT(r, found); | 27 REPORTER_ASSERT(r, found); |
23 REPORTER_ASSERT(r, *found == -3.0); | 28 REPORTER_ASSERT(r, *found == -3.0); |
24 | 29 |
25 REPORTER_ASSERT(r, !map.find(2)); | 30 REPORTER_ASSERT(r, !map.find(2)); |
26 | 31 |
27 const int N = 20; | 32 const int N = 20; |
28 | 33 |
29 for (int i = 0; i < N; i++) { | 34 for (int i = 0; i < N; i++) { |
30 map.set(i, 2.0*i); | 35 map.set(i, 2.0*i); |
31 } | 36 } |
32 for (int i = 0; i < N; i++) { | 37 for (int i = 0; i < N; i++) { |
33 double* found = map.find(i);; | 38 double* found = map.find(i);; |
34 REPORTER_ASSERT(r, found); | 39 REPORTER_ASSERT(r, found); |
35 REPORTER_ASSERT(r, *found == i*2.0); | 40 REPORTER_ASSERT(r, *found == i*2.0); |
36 } | 41 } |
37 for (int i = N; i < 2*N; i++) { | 42 for (int i = N; i < 2*N; i++) { |
38 REPORTER_ASSERT(r, !map.find(i)); | 43 REPORTER_ASSERT(r, !map.find(i)); |
39 } | 44 } |
40 | 45 |
41 REPORTER_ASSERT(r, map.count() == N); | 46 REPORTER_ASSERT(r, map.count() == N); |
42 | 47 |
43 map.reset(); | 48 map.reset(); |
44 REPORTER_ASSERT(r, map.count() == 0); | 49 REPORTER_ASSERT(r, map.count() == 0); |
45 } | 50 } |
46 | 51 |
47 namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size());
} } | |
48 | |
49 DEF_TEST(HashSet, r) { | 52 DEF_TEST(HashSet, r) { |
50 SkTHashSet<SkString, hash_string> set; | 53 SkTHashSet<SkString> set; |
51 | 54 |
52 set.add(SkString("Hello")); | 55 set.add(SkString("Hello")); |
53 set.add(SkString("World")); | 56 set.add(SkString("World")); |
54 | 57 |
55 REPORTER_ASSERT(r, set.count() == 2); | 58 REPORTER_ASSERT(r, set.count() == 2); |
56 | 59 |
57 REPORTER_ASSERT(r, set.contains(SkString("Hello"))); | 60 REPORTER_ASSERT(r, set.contains(SkString("Hello"))); |
58 REPORTER_ASSERT(r, set.contains(SkString("World"))); | 61 REPORTER_ASSERT(r, set.contains(SkString("World"))); |
59 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye"))); | 62 REPORTER_ASSERT(r, !set.contains(SkString("Goodbye"))); |
60 | 63 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 set.add(copyCounter2); | 120 set.add(copyCounter2); |
118 REPORTER_ASSERT(r, globalCounter == 3); | 121 REPORTER_ASSERT(r, globalCounter == 3); |
119 REPORTER_ASSERT(r, set.contains(copyCounter1)); | 122 REPORTER_ASSERT(r, set.contains(copyCounter1)); |
120 REPORTER_ASSERT(r, set.contains(copyCounter2)); | 123 REPORTER_ASSERT(r, set.contains(copyCounter2)); |
121 REPORTER_ASSERT(r, globalCounter == 3); | 124 REPORTER_ASSERT(r, globalCounter == 3); |
122 set.add(copyCounter1); | 125 set.add(copyCounter1); |
123 set.add(copyCounter2); | 126 set.add(copyCounter2); |
124 // We allow copies for same-value adds for now. | 127 // We allow copies for same-value adds for now. |
125 REPORTER_ASSERT(r, globalCounter == 5); | 128 REPORTER_ASSERT(r, globalCounter == 5); |
126 } | 129 } |
OLD | NEW |