| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkChecksum.h" | 8 #include "SkOpts.h" |
| 9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
| 10 #include "Test.h" | 10 #include "Test.h" |
| 11 | 11 |
| 12 | |
| 13 // Murmur3 has an optional third seed argument, so we wrap it to fit a uniform t
ype. | |
| 14 static uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::
Murmur3(d, l); } | |
| 15 | |
| 16 #define ASSERT(x) REPORTER_ASSERT(r, x) | |
| 17 | |
| 18 DEF_TEST(Checksum, r) { | 12 DEF_TEST(Checksum, r) { |
| 19 // Algorithms to test. They're currently all uint32_t(const uint32_t*, size
_t). | |
| 20 typedef uint32_t(*algorithmProc)(const uint32_t*, size_t); | |
| 21 const algorithmProc kAlgorithms[] = { &murmur_noseed }; | |
| 22 | |
| 23 // Put 128 random bytes into two identical buffers. Any multiple of 4 will
do. | 13 // Put 128 random bytes into two identical buffers. Any multiple of 4 will
do. |
| 24 const size_t kBytes = SkAlign4(128); | 14 const size_t kBytes = SkAlign4(128); |
| 25 SkRandom rand; | 15 SkRandom rand; |
| 26 uint32_t data[kBytes/4], tweaked[kBytes/4]; | 16 uint32_t data[kBytes/4], tweaked[kBytes/4]; |
| 27 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { | 17 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { |
| 28 data[i] = tweaked[i] = rand.nextU(); | 18 data[i] = tweaked[i] = rand.nextU(); |
| 29 } | 19 } |
| 30 | 20 |
| 31 // Test each algorithm. | 21 // Hash of nullptr is always 0. |
| 32 for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { | 22 REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0); |
| 33 const algorithmProc algorithm = kAlgorithms[i]; | |
| 34 | 23 |
| 35 // Hash of nullptr is always 0. | 24 const uint32_t hash = SkOpts::hash(data, kBytes); |
| 36 ASSERT(algorithm(nullptr, 0) == 0); | 25 // Should be deterministic. |
| 26 REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes)); |
| 37 | 27 |
| 38 const uint32_t hash = algorithm(data, kBytes); | 28 // Changing any single element should change the hash. |
| 39 // Should be deterministic. | 29 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { |
| 40 ASSERT(hash == algorithm(data, kBytes)); | 30 const uint32_t saved = tweaked[j]; |
| 41 | 31 tweaked[j] = rand.nextU(); |
| 42 // Changing any single element should change the hash. | 32 const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes); |
| 43 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { | 33 REPORTER_ASSERT(r, tweakedHash != hash); |
| 44 const uint32_t saved = tweaked[j]; | 34 REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes)); |
| 45 tweaked[j] = rand.nextU(); | 35 tweaked[j] = saved; |
| 46 const uint32_t tweakedHash = algorithm(tweaked, kBytes); | |
| 47 ASSERT(tweakedHash != hash); | |
| 48 ASSERT(tweakedHash == algorithm(tweaked, kBytes)); | |
| 49 tweaked[j] = saved; | |
| 50 } | |
| 51 } | 36 } |
| 52 } | 37 } |
| 53 | 38 |
| 54 DEF_TEST(GoodHash, r) { | 39 DEF_TEST(GoodHash, r) { |
| 55 ASSERT(SkGoodHash()(( int32_t)4) == 614249093); // 4 bytes. Hits SkChecks
um::Mix fast path. | 40 // 4 bytes --> hits SkChecksum::Mix fast path. |
| 56 ASSERT(SkGoodHash()((uint32_t)4) == 614249093); // (Ditto) | 41 REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) == 614249093); |
| 57 | 42 REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) == 614249093); |
| 58 // None of these are 4 byte sized, so they use SkChecksum::Murmur3, not SkCh
ecksum::Mix. | |
| 59 ASSERT(SkGoodHash()((uint64_t)4) == 3491892518); | |
| 60 ASSERT(SkGoodHash()((uint16_t)4) == 899251846); | |
| 61 ASSERT(SkGoodHash()( (uint8_t)4) == 962700458); | |
| 62 | |
| 63 // Tests SkString is correctly specialized. | |
| 64 ASSERT(SkGoodHash()(SkString("Hi")) == 55667557); | |
| 65 } | 43 } |
| OLD | NEW |