• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 
8 #include "include/core/SkString.h"
9 #include "include/core/SkTypes.h"
10 #include "include/private/SkChecksum.h"
11 #include "include/private/base/SkAlign.h"
12 #include "src/base/SkRandom.h"
13 #include "src/core/SkOpts.h"
14 #include "tests/Test.h"
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <string>
19 #include <string_view>
20 
DEF_TEST(Checksum,r)21 DEF_TEST(Checksum, r) {
22     // Put 128 random bytes into two identical buffers.  Any multiple of 4 will do.
23     const size_t kBytes = SkAlign4(128);
24     SkRandom rand;
25     uint32_t data[kBytes/4], tweaked[kBytes/4];
26     for (size_t i = 0; i < std::size(tweaked); ++i) {
27         data[i] = tweaked[i] = rand.nextU();
28     }
29 
30     // Hash of nullptr is always 0.
31     REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
32 
33     const uint32_t hash = SkOpts::hash(data, kBytes);
34     // Should be deterministic.
35     REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
36 
37     // Changing any single element should change the hash.
38     for (size_t j = 0; j < std::size(tweaked); ++j) {
39         const uint32_t saved = tweaked[j];
40         tweaked[j] = rand.nextU();
41         const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
42         REPORTER_ASSERT(r, tweakedHash != hash);
43         REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
44         tweaked[j] = saved;
45     }
46 }
47 
DEF_TEST(GoodHash,r)48 DEF_TEST(GoodHash, r) {
49     // 4 bytes --> hits SkChecksum::Mix fast path.
50     REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) ==  614249093);
51     REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) ==  614249093);
52 }
53 
DEF_TEST(ChecksumCollisions,r)54 DEF_TEST(ChecksumCollisions, r) {
55     // We noticed a few workloads that would cause hash collisions due to the way
56     // our optimized hashes split into three concurrent hashes and merge those hashes together.
57     //
58     // One of these two workloads ought to cause an unintentional hash collision on very similar
59     // data in those old algorithms, the float version on 32-bit x86 and double elsewhere.
60     {
61         float a[9] = { 0, 1, 2,
62                        3, 4, 5,
63                        6, 7, 8, };
64         float b[9] = { 1, 2, 0,
65                        4, 5, 3,
66                        7, 8, 6, };
67 
68         REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
69     }
70     {
71         double a[9] = { 0, 1, 2,
72                         3, 4, 5,
73                         6, 7, 8, };
74         double b[9] = { 1, 2, 0,
75                         4, 5, 3,
76                         7, 8, 6, };
77 
78         REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
79     }
80 }
81 
DEF_TEST(ChecksumConsistent,r)82 DEF_TEST(ChecksumConsistent, r) {
83     // We've decided to make SkOpts::hash() always return consistent results, so spot check a few:
84     uint8_t bytes[256];
85     for (int i = 0; i < 256; i++) {
86         bytes[i] = i;
87     }
88     REPORTER_ASSERT(r, SkOpts::hash(bytes,  0) == 0x00000000, "%08x", SkOpts::hash(bytes,  0));
89     REPORTER_ASSERT(r, SkOpts::hash(bytes,  1) == 0x00000000, "%08x", SkOpts::hash(bytes,  1));
90     REPORTER_ASSERT(r, SkOpts::hash(bytes,  2) == 0xf26b8303, "%08x", SkOpts::hash(bytes,  2));
91     REPORTER_ASSERT(r, SkOpts::hash(bytes,  7) == 0x18678721, "%08x", SkOpts::hash(bytes,  7));
92     REPORTER_ASSERT(r, SkOpts::hash(bytes, 32) == 0x9d1ef96b, "%08x", SkOpts::hash(bytes, 32));
93     REPORTER_ASSERT(r, SkOpts::hash(bytes, 63) == 0xc4b07d3a, "%08x", SkOpts::hash(bytes, 63));
94     REPORTER_ASSERT(r, SkOpts::hash(bytes, 64) == 0x3535a461, "%08x", SkOpts::hash(bytes, 64));
95     REPORTER_ASSERT(r, SkOpts::hash(bytes, 99) == 0x3f98a130, "%08x", SkOpts::hash(bytes, 99));
96     REPORTER_ASSERT(r, SkOpts::hash(bytes,255) == 0x3b9ceab2, "%08x", SkOpts::hash(bytes,255));
97 }
98 
DEF_TEST(ChecksumStrings,r)99 DEF_TEST(ChecksumStrings, r) {
100     constexpr char kMessage[] = "Checksums are supported for SkString, string, and string_view.";
101     const uint32_t expectedHash = SkOpts::hash(kMessage, strlen(kMessage));
102 
103     REPORTER_ASSERT(r, expectedHash == SkGoodHash()(SkString(kMessage)));
104     REPORTER_ASSERT(r, expectedHash == SkGoodHash()(std::string(kMessage)));
105     REPORTER_ASSERT(r, expectedHash == SkGoodHash()(std::string_view(kMessage)));
106 }
107