epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 7 | |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 8 | #include "SkChecksum.h" |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 9 | #include "SkRandom.h" |
| 10 | #include "Test.h" |
epoger@google.com | 0bba6bd | 2012-12-07 15:12:01 +0000 | [diff] [blame] | 11 | |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 12 | |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 13 | // Murmur3 has an optional third seed argument, so we wrap it to fit a uniform type. |
reed | bbf9f6d | 2014-07-10 14:29:43 -0700 | [diff] [blame] | 14 | static uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::Murmur3(d, l); } |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 15 | |
| 16 | #define ASSERT(x) REPORTER_ASSERT(r, x) |
| 17 | |
| 18 | DEF_TEST(Checksum, r) { |
| 19 | // Algorithms to test. They're currently all uint32_t(const uint32_t*, size_t). |
reed | bbf9f6d | 2014-07-10 14:29:43 -0700 | [diff] [blame] | 20 | typedef uint32_t(*algorithmProc)(const uint32_t*, size_t); |
mtklein | 56da025 | 2015-11-16 11:16:23 -0800 | [diff] [blame] | 21 | const algorithmProc kAlgorithms[] = { &murmur_noseed }; |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 22 | |
| 23 | // Put 128 random bytes into two identical buffers. Any multiple of 4 will do. |
| 24 | const size_t kBytes = SkAlign4(128); |
| 25 | SkRandom rand; |
| 26 | uint32_t data[kBytes/4], tweaked[kBytes/4]; |
| 27 | for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { |
| 28 | data[i] = tweaked[i] = rand.nextU(); |
| 29 | } |
| 30 | |
| 31 | // Test each algorithm. |
| 32 | for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { |
| 33 | const algorithmProc algorithm = kAlgorithms[i]; |
| 34 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | // Hash of nullptr is always 0. |
| 36 | ASSERT(algorithm(nullptr, 0) == 0); |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 37 | |
| 38 | const uint32_t hash = algorithm(data, kBytes); |
| 39 | // Should be deterministic. |
| 40 | ASSERT(hash == algorithm(data, kBytes)); |
| 41 | |
| 42 | // Changing any single element should change the hash. |
| 43 | for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { |
| 44 | const uint32_t saved = tweaked[j]; |
| 45 | tweaked[j] = rand.nextU(); |
| 46 | const uint32_t tweakedHash = algorithm(tweaked, kBytes); |
| 47 | ASSERT(tweakedHash != hash); |
| 48 | ASSERT(tweakedHash == algorithm(tweaked, kBytes)); |
| 49 | tweaked[j] = saved; |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 50 | } |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 51 | } |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 52 | } |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 53 | |
| 54 | DEF_TEST(GoodHash, r) { |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 55 | ASSERT(SkGoodHash()(( int32_t)4) == 614249093); // 4 bytes. Hits SkChecksum::Mix fast path. |
| 56 | ASSERT(SkGoodHash()((uint32_t)4) == 614249093); // (Ditto) |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 57 | |
| 58 | // None of these are 4 byte sized, so they use SkChecksum::Murmur3, not SkChecksum::Mix. |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 59 | ASSERT(SkGoodHash()((uint64_t)4) == 3491892518); |
| 60 | ASSERT(SkGoodHash()((uint16_t)4) == 899251846); |
| 61 | ASSERT(SkGoodHash()( (uint8_t)4) == 962700458); |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 62 | |
| 63 | // Tests SkString is correctly specialized. |
mtklein | c8d1dd4 | 2015-10-15 12:23:01 -0700 | [diff] [blame] | 64 | ASSERT(SkGoodHash()(SkString("Hi")) == 55667557); |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 65 | } |