OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 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 "Test.h" |
| 9 #include "SkTHashCache.h" |
| 10 |
| 11 |
| 12 // Tests the SkTHashCache<T> class template. |
| 13 |
| 14 struct Tint { |
| 15 uint32_t value; |
| 16 |
| 17 bool operator==(const Tint& rhs) const { |
| 18 return value == rhs.value; |
| 19 } |
| 20 }; |
| 21 |
| 22 class Element { |
| 23 public: |
| 24 |
| 25 bool operator==(const Element& rhs) const { |
| 26 return value == rhs.value && key == rhs.key; |
| 27 } |
| 28 |
| 29 static const Tint& GetKey(const Element& element) { |
| 30 return element.key; |
| 31 } |
| 32 |
| 33 static uint32_t Hash(const Tint& key) { |
| 34 return key.value; |
| 35 } |
| 36 |
| 37 Element(Tint key, int value) : key(key), value(value) { |
| 38 } |
| 39 |
| 40 Tint key; |
| 41 int value; |
| 42 }; |
| 43 |
| 44 typedef SkTHashCache<Element, Tint> CacheType; |
| 45 |
| 46 DEF_TEST(THashCache, reporter) { |
| 47 Tint k11 = {11}; |
| 48 Element e11(k11, 22); |
| 49 |
| 50 e11.value = e11.value; |
| 51 Element e11Collision(k11, 0); |
| 52 // Element e42(4, 2); |
| 53 |
| 54 //Some tests for the class Element |
| 55 REPORTER_ASSERT(reporter, Element::GetKey(e11) == k11); |
| 56 REPORTER_ASSERT(reporter, Element::Hash(k11) == 11); |
| 57 |
| 58 CacheType cache; |
| 59 |
| 60 // Is the cahce correctly initialized ? |
| 61 REPORTER_ASSERT(reporter, 0 == cache.size()); |
| 62 REPORTER_ASSERT(reporter, NULL == cache.find(k11)); |
| 63 |
| 64 Element& e11_c = cache.add(e11); |
| 65 e11_c.value = e11_c.value; |
| 66 |
| 67 // Tests for simple insertion, verifying that the returned element |
| 68 // has the same values as the original one |
| 69 REPORTER_ASSERT(reporter, 1 == cache.size()); |
| 70 REPORTER_ASSERT(reporter, NULL != cache.find(k11)); |
| 71 REPORTER_ASSERT(reporter, e11_c == e11); |
| 72 |
| 73 Element& e11Collision_c = cache.add(e11Collision); |
| 74 // Verifying that, in case of collision, the element alerady in the cache is
not removed |
| 75 REPORTER_ASSERT(reporter, e11Collision_c == e11); |
| 76 REPORTER_ASSERT(reporter, 1 == cache.size()); |
| 77 |
| 78 Tint k42 = {42}; |
| 79 Element e42(k42, 2); |
| 80 cache.add(e42); |
| 81 // Can we add more than one element? |
| 82 REPORTER_ASSERT(reporter, NULL != cache.find(k11)); |
| 83 REPORTER_ASSERT(reporter, NULL != cache.find(k42)); |
| 84 REPORTER_ASSERT(reporter, 2 == cache.size()); |
| 85 |
| 86 cache.reset(); |
| 87 // Does clear do its job? |
| 88 REPORTER_ASSERT(reporter, 0 == cache.size()); |
| 89 REPORTER_ASSERT(reporter, NULL == cache.find(k11)); |
| 90 REPORTER_ASSERT(reporter, NULL == cache.find(k42)); |
| 91 } |
OLD | NEW |