2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkTypes.h"
9 #include "include/private/SkChecksum.h"
10 #include "include/utils/SkRandom.h"
11 #include "src/core/SkOpts.h"
12 #include "tests/Test.h"
14 DEF_TEST(Checksum, r) {
15 // Put 128 random bytes into two identical buffers. Any multiple of 4 will do.
16 const size_t kBytes = SkAlign4(128);
18 uint32_t data[kBytes/4], tweaked[kBytes/4];
19 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
20 data[i] = tweaked[i] = rand.nextU();
23 // Hash of nullptr is always 0.
24 REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
26 const uint32_t hash = SkOpts::hash(data, kBytes);
27 // Should be deterministic.
28 REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
30 // Changing any single element should change the hash.
31 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
32 const uint32_t saved = tweaked[j];
33 tweaked[j] = rand.nextU();
34 const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
35 REPORTER_ASSERT(r, tweakedHash != hash);
36 REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
41 DEF_TEST(GoodHash, r) {
42 // 4 bytes --> hits SkChecksum::Mix fast path.
43 REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) == 614249093);
44 REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) == 614249093);
47 DEF_TEST(ChecksumCollisions, r) {
48 // We noticed a few workloads that would cause hash collisions due to the way
49 // our optimized hashes split into three concurrent hashes and merge those hashes together.
51 // One of these two workloads ought to cause an unintentional hash collision on very similar
52 // data in those old algorithms, the float version on 32-bit x86 and double elsewhere.
54 float a[9] = { 0, 1, 2,
57 float b[9] = { 1, 2, 0,
61 REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
64 double a[9] = { 0, 1, 2,
67 double b[9] = { 1, 2, 0,
71 REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
75 DEF_TEST(ChecksumConsistent, r) {
76 // We've decided to make SkOpts::hash() always return consistent results, so spot check a few:
78 for (int i = 0; i < 256; i++) {
81 REPORTER_ASSERT(r, SkOpts::hash(bytes, 0) == 0x00000000, "%08x", SkOpts::hash(bytes, 0));
82 REPORTER_ASSERT(r, SkOpts::hash(bytes, 1) == 0x00000000, "%08x", SkOpts::hash(bytes, 1));
83 REPORTER_ASSERT(r, SkOpts::hash(bytes, 2) == 0xf26b8303, "%08x", SkOpts::hash(bytes, 2));
84 REPORTER_ASSERT(r, SkOpts::hash(bytes, 7) == 0x18678721, "%08x", SkOpts::hash(bytes, 7));
85 REPORTER_ASSERT(r, SkOpts::hash(bytes, 32) == 0x9d1ef96b, "%08x", SkOpts::hash(bytes, 32));
86 REPORTER_ASSERT(r, SkOpts::hash(bytes, 63) == 0xc4b07d3a, "%08x", SkOpts::hash(bytes, 63));
87 REPORTER_ASSERT(r, SkOpts::hash(bytes, 64) == 0x3535a461, "%08x", SkOpts::hash(bytes, 64));
88 REPORTER_ASSERT(r, SkOpts::hash(bytes, 99) == 0x3f98a130, "%08x", SkOpts::hash(bytes, 99));
89 REPORTER_ASSERT(r, SkOpts::hash(bytes,255) == 0x3b9ceab2, "%08x", SkOpts::hash(bytes,255));
92 DEF_TEST(ChecksumStrings, r) {
93 constexpr char kMessage[] = "Checksums are supported for SkString, string, and string_view.";
94 const uint32_t expectedHash = SkOpts::hash(kMessage, strlen(kMessage));
96 REPORTER_ASSERT(r, expectedHash == SkGoodHash()(SkString(kMessage)));
97 REPORTER_ASSERT(r, expectedHash == SkGoodHash()(std::string(kMessage)));
98 REPORTER_ASSERT(r, expectedHash == SkGoodHash()(std::string_view(kMessage)));