1 /*
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 
8 #include "SkChecksum.h"
9 #include "SkString.h"
10 #include "SkTHash.h"
11 #include "Test.h"
12 
13 // Tests use of const foreach().  map.count() is of course the better way to do this.
count(const SkTHashMap<int,double> & map)14 static int count(const SkTHashMap<int, double>& map) {
15     int n = 0;
16     map.foreach([&n](int, double) { n++; });
17     return n;
18 }
19 
DEF_TEST(HashMap,r)20 DEF_TEST(HashMap, r) {
21     SkTHashMap<int, double> map;
22 
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 
30     map.foreach([](int key, double* d){ *d = -key; });
31     REPORTER_ASSERT(r, count(map) == 1);
32 
33     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);
54 
55     for (int i = 0; i < N/2; i++) {
56         map.remove(i);
57     }
58     for (int i = 0; i < N; i++) {
59         double* found = map.find(i);
60         REPORTER_ASSERT(r, (found == nullptr) ==  (i < N/2));
61     }
62     REPORTER_ASSERT(r, map.count() == N/2);
63 
64     map.reset();
65     REPORTER_ASSERT(r, map.count() == 0);
66 }
67 
DEF_TEST(HashSet,r)68 DEF_TEST(HashSet, r) {
69     SkTHashSet<SkString> set;
70 
71     set.add(SkString("Hello"));
72     set.add(SkString("World"));
73 
74     REPORTER_ASSERT(r, set.count() == 2);
75 
76     REPORTER_ASSERT(r, set.contains(SkString("Hello")));
77     REPORTER_ASSERT(r, set.contains(SkString("World")));
78     REPORTER_ASSERT(r, !set.contains(SkString("Goodbye")));
79 
80     REPORTER_ASSERT(r, set.find(SkString("Hello")));
81     REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello"));
82 
83     set.remove(SkString("Hello"));
84     REPORTER_ASSERT(r, !set.contains(SkString("Hello")));
85     REPORTER_ASSERT(r, set.count() == 1);
86 
87     set.reset();
88     REPORTER_ASSERT(r, set.count() == 0);
89 }
90 
91 namespace {
92 
93 class CopyCounter {
94 public:
CopyCounter()95     CopyCounter() : fID(0), fCounter(NULL) {}
96 
CopyCounter(uint32_t id,uint32_t * counter)97     CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {}
98 
CopyCounter(const CopyCounter & other)99     CopyCounter(const CopyCounter& other)
100         : fID(other.fID)
101         , fCounter(other.fCounter) {
102         SkASSERT(fCounter);
103         *fCounter += 1;
104     }
105 
operator =(const CopyCounter & other)106     void operator=(const CopyCounter& other) {
107         fID = other.fID;
108         fCounter = other.fCounter;
109         *fCounter += 1;
110     }
111 
operator ==(const CopyCounter & other) const112     bool operator==(const CopyCounter& other) const {
113         return fID == other.fID;
114     }
115 
116 private:
117     uint32_t  fID;
118     uint32_t* fCounter;
119 };
120 
hash_copy_counter(const CopyCounter &)121 uint32_t hash_copy_counter(const CopyCounter&) {
122     return 0; // let them collide, what do we care?
123 }
124 
125 }
126 
DEF_TEST(HashSetCopyCounter,r)127 DEF_TEST(HashSetCopyCounter, r) {
128     SkTHashSet<CopyCounter, hash_copy_counter> set;
129 
130     uint32_t globalCounter = 0;
131     CopyCounter copyCounter1(1, &globalCounter);
132     CopyCounter copyCounter2(2, &globalCounter);
133     REPORTER_ASSERT(r, globalCounter == 0);
134 
135     set.add(copyCounter1);
136     REPORTER_ASSERT(r, globalCounter == 1);
137     REPORTER_ASSERT(r, set.contains(copyCounter1));
138     REPORTER_ASSERT(r, globalCounter == 1);
139     set.add(copyCounter1);
140     // We allow copies for same-value adds for now.
141     REPORTER_ASSERT(r, globalCounter == 2);
142 
143     set.add(copyCounter2);
144     REPORTER_ASSERT(r, globalCounter == 3);
145     REPORTER_ASSERT(r, set.contains(copyCounter1));
146     REPORTER_ASSERT(r, set.contains(copyCounter2));
147     REPORTER_ASSERT(r, globalCounter == 3);
148     set.add(copyCounter1);
149     set.add(copyCounter2);
150     // We allow copies for same-value adds for now.
151     REPORTER_ASSERT(r, globalCounter == 5);
152 }
153