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 | |
mtklein | 469a3fe | 2015-08-07 09:33:37 -0700 | [diff] [blame] | 26 | REPORTER_ASSERT(r, map.approxBytesUsed() > 0); |
| 27 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 28 | double* found = map.find(3); |
| 29 | REPORTER_ASSERT(r, found); |
| 30 | REPORTER_ASSERT(r, *found == 4.0); |
| 31 | |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 32 | map.foreach([](int key, double* d){ *d = -key; }); |
| 33 | REPORTER_ASSERT(r, count(map) == 1); |
| 34 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 35 | found = map.find(3); |
| 36 | REPORTER_ASSERT(r, found); |
| 37 | REPORTER_ASSERT(r, *found == -3.0); |
| 38 | |
| 39 | REPORTER_ASSERT(r, !map.find(2)); |
| 40 | |
| 41 | const int N = 20; |
| 42 | |
| 43 | for (int i = 0; i < N; i++) { |
| 44 | map.set(i, 2.0*i); |
| 45 | } |
| 46 | for (int i = 0; i < N; i++) { |
tfarina | 567ff2f | 2015-04-27 07:01:44 -0700 | [diff] [blame] | 47 | double* found = map.find(i); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 48 | REPORTER_ASSERT(r, found); |
| 49 | REPORTER_ASSERT(r, *found == i*2.0); |
| 50 | } |
| 51 | for (int i = N; i < 2*N; i++) { |
| 52 | REPORTER_ASSERT(r, !map.find(i)); |
| 53 | } |
| 54 | |
| 55 | REPORTER_ASSERT(r, map.count() == N); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 56 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 57 | for (int i = 0; i < N/2; i++) { |
| 58 | map.remove(i); |
| 59 | } |
| 60 | for (int i = 0; i < N; i++) { |
| 61 | double* found = map.find(i); |
| 62 | REPORTER_ASSERT(r, (found == nullptr) == (i < N/2)); |
| 63 | } |
| 64 | REPORTER_ASSERT(r, map.count() == N/2); |
| 65 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 66 | map.reset(); |
| 67 | REPORTER_ASSERT(r, map.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 68 | } |
| 69 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 70 | DEF_TEST(HashSet, r) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 71 | SkTHashSet<SkString> set; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 72 | |
| 73 | set.add(SkString("Hello")); |
| 74 | set.add(SkString("World")); |
| 75 | |
| 76 | REPORTER_ASSERT(r, set.count() == 2); |
| 77 | |
| 78 | REPORTER_ASSERT(r, set.contains(SkString("Hello"))); |
| 79 | REPORTER_ASSERT(r, set.contains(SkString("World"))); |
| 80 | REPORTER_ASSERT(r, !set.contains(SkString("Goodbye"))); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 81 | |
mtklein | fb8307c | 2015-04-01 11:21:27 -0700 | [diff] [blame] | 82 | REPORTER_ASSERT(r, set.find(SkString("Hello"))); |
| 83 | REPORTER_ASSERT(r, *set.find(SkString("Hello")) == SkString("Hello")); |
| 84 | |
mtklein | 33d73c3 | 2015-04-21 06:53:56 -0700 | [diff] [blame] | 85 | set.remove(SkString("Hello")); |
| 86 | REPORTER_ASSERT(r, !set.contains(SkString("Hello"))); |
| 87 | REPORTER_ASSERT(r, set.count() == 1); |
| 88 | |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 89 | set.reset(); |
| 90 | REPORTER_ASSERT(r, set.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 91 | } |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 92 | |
| 93 | namespace { |
| 94 | |
| 95 | class CopyCounter { |
| 96 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | CopyCounter() : fID(0), fCounter(nullptr) {} |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 98 | |
| 99 | CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {} |
| 100 | |
| 101 | CopyCounter(const CopyCounter& other) |
| 102 | : fID(other.fID) |
| 103 | , fCounter(other.fCounter) { |
| 104 | SkASSERT(fCounter); |
| 105 | *fCounter += 1; |
| 106 | } |
| 107 | |
| 108 | void operator=(const CopyCounter& other) { |
| 109 | fID = other.fID; |
| 110 | fCounter = other.fCounter; |
| 111 | *fCounter += 1; |
| 112 | } |
| 113 | |
| 114 | bool operator==(const CopyCounter& other) const { |
| 115 | return fID == other.fID; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | uint32_t fID; |
| 120 | uint32_t* fCounter; |
| 121 | }; |
| 122 | |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 123 | struct HashCopyCounter { |
| 124 | uint32_t operator()(const CopyCounter&) const { |
| 125 | return 0; // let them collide, what do we care? |
| 126 | } |
| 127 | }; |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 128 | |
| 129 | } |
| 130 | |
| 131 | DEF_TEST(HashSetCopyCounter, r) { |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 132 | SkTHashSet<CopyCounter, HashCopyCounter> set; |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 133 | |
| 134 | uint32_t globalCounter = 0; |
| 135 | CopyCounter copyCounter1(1, &globalCounter); |
| 136 | CopyCounter copyCounter2(2, &globalCounter); |
| 137 | REPORTER_ASSERT(r, globalCounter == 0); |
| 138 | |
| 139 | set.add(copyCounter1); |
| 140 | REPORTER_ASSERT(r, globalCounter == 1); |
| 141 | REPORTER_ASSERT(r, set.contains(copyCounter1)); |
| 142 | REPORTER_ASSERT(r, globalCounter == 1); |
| 143 | set.add(copyCounter1); |
| 144 | // We allow copies for same-value adds for now. |
| 145 | REPORTER_ASSERT(r, globalCounter == 2); |
| 146 | |
| 147 | set.add(copyCounter2); |
| 148 | REPORTER_ASSERT(r, globalCounter == 3); |
| 149 | REPORTER_ASSERT(r, set.contains(copyCounter1)); |
| 150 | REPORTER_ASSERT(r, set.contains(copyCounter2)); |
| 151 | REPORTER_ASSERT(r, globalCounter == 3); |
| 152 | set.add(copyCounter1); |
| 153 | set.add(copyCounter2); |
| 154 | // We allow copies for same-value adds for now. |
| 155 | REPORTER_ASSERT(r, globalCounter == 5); |
| 156 | } |