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 "SkChecksum.h" |
9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
10 #include "Test.h" | 10 #include "Test.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 SkRandom rand; | 25 SkRandom rand; |
26 uint32_t data[kBytes/4], tweaked[kBytes/4]; | 26 uint32_t data[kBytes/4], tweaked[kBytes/4]; |
27 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { | 27 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { |
28 data[i] = tweaked[i] = rand.nextU(); | 28 data[i] = tweaked[i] = rand.nextU(); |
29 } | 29 } |
30 | 30 |
31 // Test each algorithm. | 31 // Test each algorithm. |
32 for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { | 32 for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { |
33 const algorithmProc algorithm = kAlgorithms[i]; | 33 const algorithmProc algorithm = kAlgorithms[i]; |
34 | 34 |
35 // Hash of NULL is always 0. | 35 // Hash of nullptr is always 0. |
36 ASSERT(algorithm(NULL, 0) == 0); | 36 ASSERT(algorithm(nullptr, 0) == 0); |
37 | 37 |
38 const uint32_t hash = algorithm(data, kBytes); | 38 const uint32_t hash = algorithm(data, kBytes); |
39 // Should be deterministic. | 39 // Should be deterministic. |
40 ASSERT(hash == algorithm(data, kBytes)); | 40 ASSERT(hash == algorithm(data, kBytes)); |
41 | 41 |
42 // Changing any single element should change the hash. | 42 // Changing any single element should change the hash. |
43 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { | 43 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { |
44 const uint32_t saved = tweaked[j]; | 44 const uint32_t saved = tweaked[j]; |
45 tweaked[j] = rand.nextU(); | 45 tweaked[j] = rand.nextU(); |
46 const uint32_t tweakedHash = algorithm(tweaked, kBytes); | 46 const uint32_t tweakedHash = algorithm(tweaked, kBytes); |
47 ASSERT(tweakedHash != hash); | 47 ASSERT(tweakedHash != hash); |
48 ASSERT(tweakedHash == algorithm(tweaked, kBytes)); | 48 ASSERT(tweakedHash == algorithm(tweaked, kBytes)); |
49 tweaked[j] = saved; | 49 tweaked[j] = saved; |
50 } | 50 } |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 DEF_TEST(GoodHash, r) { | 54 DEF_TEST(GoodHash, r) { |
55 ASSERT(SkGoodHash(( int32_t)4) == 614249093); // 4 bytes. Hits SkChecksum
::Mix fast path. | 55 ASSERT(SkGoodHash(( int32_t)4) == 614249093); // 4 bytes. Hits SkChecksum
::Mix fast path. |
56 ASSERT(SkGoodHash((uint32_t)4) == 614249093); // (Ditto) | 56 ASSERT(SkGoodHash((uint32_t)4) == 614249093); // (Ditto) |
57 | 57 |
58 // None of these are 4 byte sized, so they use SkChecksum::Murmur3, not SkCh
ecksum::Mix. | 58 // None of these are 4 byte sized, so they use SkChecksum::Murmur3, not SkCh
ecksum::Mix. |
59 ASSERT(SkGoodHash((uint64_t)4) == 3491892518); | 59 ASSERT(SkGoodHash((uint64_t)4) == 3491892518); |
60 ASSERT(SkGoodHash((uint16_t)4) == 899251846); | 60 ASSERT(SkGoodHash((uint16_t)4) == 899251846); |
61 ASSERT(SkGoodHash( (uint8_t)4) == 962700458); | 61 ASSERT(SkGoodHash( (uint8_t)4) == 962700458); |
62 | 62 |
63 // Tests SkString is correctly specialized. | 63 // Tests SkString is correctly specialized. |
64 ASSERT(SkGoodHash(SkString("Hi")) == 55667557); | 64 ASSERT(SkGoodHash(SkString("Hi")) == 55667557); |
65 } | 65 } |
OLD | NEW |