diff options
author | mtklein <mtklein@chromium.org> | 2015-03-20 13:48:42 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-20 13:48:42 -0700 |
commit | 02f46cf878535fb79317d15ebed66dfa3f2cd772 (patch) | |
tree | a1a4c7994b395cd2ef88b4df62057d728d61fb4d /tests | |
parent | ce6acc91085c4b6d87d4bac84e66193908e648f9 (diff) |
Some usability ideas around SkTHash.
- By default, use new SkGoodHash to hash keys, which is:
* for 4 byte values, use SkChecksum::Mix,
* for SkStrings, use SkChecksum::Murmur3 on the data,
* for other structs, shallow hash the struct with Murmur3.
- Expand SkChecksum::Murmur3 to support non-4-byte-aligned data.
- Add const foreach() methods.
- Have foreach() take a functor, which allows lambdas.
BUG=skia:
Review URL: https://codereview.chromium.org/1021033002
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ChecksumTest.cpp | 13 | ||||
-rw-r--r-- | tests/HashTest.cpp | 19 |
2 files changed, 24 insertions, 8 deletions
diff --git a/tests/ChecksumTest.cpp b/tests/ChecksumTest.cpp index 6248678043..3658bd771e 100644 --- a/tests/ChecksumTest.cpp +++ b/tests/ChecksumTest.cpp @@ -50,3 +50,16 @@ DEF_TEST(Checksum, r) { } } } + +DEF_TEST(GoodHash, r) { + ASSERT(SkGoodHash(( int32_t)4) == 614249093); // 4 bytes. Hits SkChecksum::Mix fast path. + ASSERT(SkGoodHash((uint32_t)4) == 614249093); // (Ditto) + + // None of these are 4 byte sized, so they use SkChecksum::Murmur3, not SkChecksum::Mix. + ASSERT(SkGoodHash((uint64_t)4) == 3491892518); + ASSERT(SkGoodHash((uint16_t)4) == 899251846); + ASSERT(SkGoodHash( (uint8_t)4) == 962700458); + + // Tests SkString is correctly specialized. + ASSERT(SkGoodHash(SkString("Hi")) == 55667557); +} diff --git a/tests/HashTest.cpp b/tests/HashTest.cpp index 623e597eeb..5abf3db50a 100644 --- a/tests/HashTest.cpp +++ b/tests/HashTest.cpp @@ -3,12 +3,15 @@ #include "SkTHash.h" #include "Test.h" -namespace { uint32_t hash_int(const int& k) { return SkChecksum::Mix(k); } } - -static void set_negative_key(int key, double* d) { *d = -key; } +// Tests use of const foreach(). map.count() is of course the better way to do this. +static int count(const SkTHashMap<int, double>& map) { + int n = 0; + map.foreach([&n](int, double) { n++; }); + return n; +} DEF_TEST(HashMap, r) { - SkTHashMap<int, double, hash_int> map; + SkTHashMap<int, double> map; map.set(3, 4.0); REPORTER_ASSERT(r, map.count() == 1); @@ -17,7 +20,9 @@ DEF_TEST(HashMap, r) { REPORTER_ASSERT(r, found); REPORTER_ASSERT(r, *found == 4.0); - map.foreach(set_negative_key); + map.foreach([](int key, double* d){ *d = -key; }); + REPORTER_ASSERT(r, count(map) == 1); + found = map.find(3); REPORTER_ASSERT(r, found); REPORTER_ASSERT(r, *found == -3.0); @@ -44,10 +49,8 @@ DEF_TEST(HashMap, r) { REPORTER_ASSERT(r, map.count() == 0); } -namespace { uint32_t hash_string(const SkString& s) { return SkToInt(s.size()); } } - DEF_TEST(HashSet, r) { - SkTHashSet<SkString, hash_string> set; + SkTHashSet<SkString> set; set.add(SkString("Hello")); set.add(SkString("World")); |