mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 1 | #include "SkChecksum.h" |
| 2 | #include "SkString.h" |
| 3 | #include "SkTHash.h" |
| 4 | #include "Test.h" |
| 5 | |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 6 | namespace { uint32_t hash_int(const int& k) { return SkChecksum::Mix(k); } } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 7 | |
| 8 | static void set_negative_key(int key, double* d) { *d = -key; } |
| 9 | |
| 10 | DEF_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); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 42 | |
| 43 | map.reset(); |
| 44 | REPORTER_ASSERT(r, map.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 45 | } |
| 46 | |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 47 | namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size()); } } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 48 | |
| 49 | DEF_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"))); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 60 | |
| 61 | set.reset(); |
| 62 | REPORTER_ASSERT(r, set.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 63 | } |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 64 | |
| 65 | namespace { |
| 66 | |
| 67 | class CopyCounter { |
| 68 | public: |
| 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 | |
| 90 | private: |
| 91 | uint32_t fID; |
| 92 | uint32_t* fCounter; |
| 93 | }; |
| 94 | |
| 95 | uint32_t hash_copy_counter(const CopyCounter&) { |
| 96 | return 0; // let them collide, what do we care? |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | DEF_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 | } |