mtklein | fb8307c | 2015-04-01 11:21:27 -0700 | [diff] [blame] | 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 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 8 | #include "SkChecksum.h" |
| 9 | #include "SkString.h" |
| 10 | #include "SkTHash.h" |
| 11 | #include "Test.h" |
| 12 | |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 13 | // Tests use of const foreach(). map.count() is of course the better way to do this. |
| 14 | static int count(const SkTHashMap<int, double>& map) { |
| 15 | int n = 0; |
| 16 | map.foreach([&n](int, double) { n++; }); |
| 17 | return n; |
| 18 | } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 19 | |
| 20 | DEF_TEST(HashMap, r) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 21 | SkTHashMap<int, double> map; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 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 | |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 30 | map.foreach([](int key, double* d){ *d = -key; }); |
| 31 | REPORTER_ASSERT(r, count(map) == 1); |
| 32 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 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++) { |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 45 | double* found = map.find(i); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 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); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 54 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 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 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 64 | map.reset(); |
| 65 | REPORTER_ASSERT(r, map.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 66 | } |
| 67 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 68 | DEF_TEST(HashSet, r) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 69 | SkTHashSet<SkString> set; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 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"))); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 79 | |
mtklein | fb8307c | 2015-04-01 11:21:27 -0700 | [diff] [blame] | 80 | REPORTER_ASSERT(r, set.find(SkString("Hello"))); |
| 81 | REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello")); |
| 82 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 83 | set.remove(SkString("Hello")); |
| 84 | REPORTER_ASSERT(r, !set.contains(SkString("Hello"))); |
| 85 | REPORTER_ASSERT(r, set.count() == 1); |
| 86 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 87 | set.reset(); |
| 88 | REPORTER_ASSERT(r, set.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 89 | } |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 90 | |
| 91 | namespace { |
| 92 | |
| 93 | class CopyCounter { |
| 94 | public: |
| 95 | CopyCounter() : fID(0), fCounter(NULL) {} |
| 96 | |
| 97 | CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {} |
| 98 | |
| 99 | CopyCounter(const CopyCounter& other) |
| 100 | : fID(other.fID) |
| 101 | , fCounter(other.fCounter) { |
| 102 | SkASSERT(fCounter); |
| 103 | *fCounter += 1; |
| 104 | } |
| 105 | |
| 106 | void operator=(const CopyCounter& other) { |
| 107 | fID = other.fID; |
| 108 | fCounter = other.fCounter; |
| 109 | *fCounter += 1; |
| 110 | } |
| 111 | |
| 112 | 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 | |
| 121 | uint32_t hash_copy_counter(const CopyCounter&) { |
| 122 | return 0; // let them collide, what do we care? |
| 123 | } |
| 124 | |
| 125 | } |
| 126 | |
| 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 | } |