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. |
mtklein | 53d4359 | 2014-07-10 14:10:45 -0700 | [diff] [blame] | 14 | static uint32_t murmur_noseed(const SkChecksum::FourByteAligned* d, size_t l) { |
| 15 | return SkChecksum::Murmur3(d, l); |
| 16 | } |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 17 | |
| 18 | #define ASSERT(x) REPORTER_ASSERT(r, x) |
| 19 | |
| 20 | DEF_TEST(Checksum, r) { |
| 21 | // Algorithms to test. They're currently all uint32_t(const uint32_t*, size_t). |
mtklein | 53d4359 | 2014-07-10 14:10:45 -0700 | [diff] [blame] | 22 | typedef uint32_t(*algorithmProc)(const SkChecksum::FourByteAligned*, size_t); |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 23 | const algorithmProc kAlgorithms[] = { &SkChecksum::Compute, &murmur_noseed }; |
| 24 | |
| 25 | // Put 128 random bytes into two identical buffers. Any multiple of 4 will do. |
| 26 | const size_t kBytes = SkAlign4(128); |
| 27 | SkRandom rand; |
| 28 | uint32_t data[kBytes/4], tweaked[kBytes/4]; |
| 29 | for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { |
| 30 | data[i] = tweaked[i] = rand.nextU(); |
| 31 | } |
| 32 | |
| 33 | // Test each algorithm. |
| 34 | for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { |
| 35 | const algorithmProc algorithm = kAlgorithms[i]; |
| 36 | |
| 37 | // Hash of NULL is always 0. |
| 38 | ASSERT(algorithm(NULL, 0) == 0); |
| 39 | |
| 40 | const uint32_t hash = algorithm(data, kBytes); |
| 41 | // Should be deterministic. |
| 42 | ASSERT(hash == algorithm(data, kBytes)); |
| 43 | |
| 44 | // Changing any single element should change the hash. |
| 45 | for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { |
| 46 | const uint32_t saved = tweaked[j]; |
| 47 | tweaked[j] = rand.nextU(); |
| 48 | const uint32_t tweakedHash = algorithm(tweaked, kBytes); |
| 49 | ASSERT(tweakedHash != hash); |
| 50 | ASSERT(tweakedHash == algorithm(tweaked, kBytes)); |
| 51 | tweaked[j] = saved; |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 52 | } |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 53 | } |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 54 | } |