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 | |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame^] | 6 | // Tests use of const foreach(). map.count() is of course the better way to do this. |
| 7 | static int count(const SkTHashMap<int, double>& map) { |
| 8 | int n = 0; |
| 9 | map.foreach([&n](int, double) { n++; }); |
| 10 | return n; |
| 11 | } |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 12 | |
| 13 | DEF_TEST(HashMap, r) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame^] | 14 | SkTHashMap<int, double> map; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 15 | |
| 16 | map.set(3, 4.0); |
| 17 | REPORTER_ASSERT(r, map.count() == 1); |
| 18 | |
| 19 | double* found = map.find(3); |
| 20 | REPORTER_ASSERT(r, found); |
| 21 | REPORTER_ASSERT(r, *found == 4.0); |
| 22 | |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame^] | 23 | map.foreach([](int key, double* d){ *d = -key; }); |
| 24 | REPORTER_ASSERT(r, count(map) == 1); |
| 25 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 26 | found = map.find(3); |
| 27 | REPORTER_ASSERT(r, found); |
| 28 | REPORTER_ASSERT(r, *found == -3.0); |
| 29 | |
| 30 | REPORTER_ASSERT(r, !map.find(2)); |
| 31 | |
| 32 | const int N = 20; |
| 33 | |
| 34 | for (int i = 0; i < N; i++) { |
| 35 | map.set(i, 2.0*i); |
| 36 | } |
| 37 | for (int i = 0; i < N; i++) { |
| 38 | double* found = map.find(i);; |
| 39 | REPORTER_ASSERT(r, found); |
| 40 | REPORTER_ASSERT(r, *found == i*2.0); |
| 41 | } |
| 42 | for (int i = N; i < 2*N; i++) { |
| 43 | REPORTER_ASSERT(r, !map.find(i)); |
| 44 | } |
| 45 | |
| 46 | REPORTER_ASSERT(r, map.count() == N); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 47 | |
| 48 | map.reset(); |
| 49 | REPORTER_ASSERT(r, map.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 50 | } |
| 51 | |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 52 | DEF_TEST(HashSet, r) { |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame^] | 53 | SkTHashSet<SkString> set; |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 54 | |
| 55 | set.add(SkString("Hello")); |
| 56 | set.add(SkString("World")); |
| 57 | |
| 58 | REPORTER_ASSERT(r, set.count() == 2); |
| 59 | |
| 60 | REPORTER_ASSERT(r, set.contains(SkString("Hello"))); |
| 61 | REPORTER_ASSERT(r, set.contains(SkString("World"))); |
| 62 | REPORTER_ASSERT(r, !set.contains(SkString("Goodbye"))); |
mtklein | 2aa1f7e | 2015-02-20 12:35:32 -0800 | [diff] [blame] | 63 | |
| 64 | set.reset(); |
| 65 | REPORTER_ASSERT(r, set.count() == 0); |
mtklein | 979e0ea | 2015-02-12 13:20:08 -0800 | [diff] [blame] | 66 | } |
fmalita | 79ca081 | 2015-02-12 17:32:49 -0800 | [diff] [blame] | 67 | |
| 68 | namespace { |
| 69 | |
| 70 | class CopyCounter { |
| 71 | public: |
| 72 | CopyCounter() : fID(0), fCounter(NULL) {} |
| 73 | |
| 74 | CopyCounter(uint32_t id, uint32_t* counter) : fID(id), fCounter(counter) {} |
| 75 | |
| 76 | CopyCounter(const CopyCounter& other) |
| 77 | : fID(other.fID) |
| 78 | , fCounter(other.fCounter) { |
| 79 | SkASSERT(fCounter); |
| 80 | *fCounter += 1; |
| 81 | } |
| 82 | |
| 83 | void operator=(const CopyCounter& other) { |
| 84 | fID = other.fID; |
| 85 | fCounter = other.fCounter; |
| 86 | *fCounter += 1; |
| 87 | } |
| 88 | |
| 89 | bool operator==(const CopyCounter& other) const { |
| 90 | return fID == other.fID; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | uint32_t fID; |
| 95 | uint32_t* fCounter; |
| 96 | }; |
| 97 | |
| 98 | uint32_t hash_copy_counter(const CopyCounter&) { |
| 99 | return 0; // let them collide, what do we care? |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | DEF_TEST(HashSetCopyCounter, r) { |
| 105 | SkTHashSet<CopyCounter, hash_copy_counter> set; |
| 106 | |
| 107 | uint32_t globalCounter = 0; |
| 108 | CopyCounter copyCounter1(1, &globalCounter); |
| 109 | CopyCounter copyCounter2(2, &globalCounter); |
| 110 | REPORTER_ASSERT(r, globalCounter == 0); |
| 111 | |
| 112 | set.add(copyCounter1); |
| 113 | REPORTER_ASSERT(r, globalCounter == 1); |
| 114 | REPORTER_ASSERT(r, set.contains(copyCounter1)); |
| 115 | REPORTER_ASSERT(r, globalCounter == 1); |
| 116 | set.add(copyCounter1); |
| 117 | // We allow copies for same-value adds for now. |
| 118 | REPORTER_ASSERT(r, globalCounter == 2); |
| 119 | |
| 120 | set.add(copyCounter2); |
| 121 | REPORTER_ASSERT(r, globalCounter == 3); |
| 122 | REPORTER_ASSERT(r, set.contains(copyCounter1)); |
| 123 | REPORTER_ASSERT(r, set.contains(copyCounter2)); |
| 124 | REPORTER_ASSERT(r, globalCounter == 3); |
| 125 | set.add(copyCounter1); |
| 126 | set.add(copyCounter2); |
| 127 | // We allow copies for same-value adds for now. |
| 128 | REPORTER_ASSERT(r, globalCounter == 5); |
| 129 | } |