1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2012 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
9cb93a386Sopenharmony_ci#include "include/private/SkChecksum.h"
10cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
11cb93a386Sopenharmony_ci#include "src/core/SkOpts.h"
12cb93a386Sopenharmony_ci#include "tests/Test.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciDEF_TEST(Checksum, r) {
15cb93a386Sopenharmony_ci    // Put 128 random bytes into two identical buffers.  Any multiple of 4 will do.
16cb93a386Sopenharmony_ci    const size_t kBytes = SkAlign4(128);
17cb93a386Sopenharmony_ci    SkRandom rand;
18cb93a386Sopenharmony_ci    uint32_t data[kBytes/4], tweaked[kBytes/4];
19cb93a386Sopenharmony_ci    for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
20cb93a386Sopenharmony_ci        data[i] = tweaked[i] = rand.nextU();
21cb93a386Sopenharmony_ci    }
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci    // Hash of nullptr is always 0.
24cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_ci    const uint32_t hash = SkOpts::hash(data, kBytes);
27cb93a386Sopenharmony_ci    // Should be deterministic.
28cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    // Changing any single element should change the hash.
31cb93a386Sopenharmony_ci    for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
32cb93a386Sopenharmony_ci        const uint32_t saved = tweaked[j];
33cb93a386Sopenharmony_ci        tweaked[j] = rand.nextU();
34cb93a386Sopenharmony_ci        const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
35cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, tweakedHash != hash);
36cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
37cb93a386Sopenharmony_ci        tweaked[j] = saved;
38cb93a386Sopenharmony_ci    }
39cb93a386Sopenharmony_ci}
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ciDEF_TEST(GoodHash, r) {
42cb93a386Sopenharmony_ci    // 4 bytes --> hits SkChecksum::Mix fast path.
43cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) ==  614249093);
44cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) ==  614249093);
45cb93a386Sopenharmony_ci}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ciDEF_TEST(ChecksumCollisions, r) {
48cb93a386Sopenharmony_ci    // We noticed a few workloads that would cause hash collisions due to the way
49cb93a386Sopenharmony_ci    // our optimized hashes split into three concurrent hashes and merge those hashes together.
50cb93a386Sopenharmony_ci    //
51cb93a386Sopenharmony_ci    // One of these two workloads ought to cause an unintentional hash collision on very similar
52cb93a386Sopenharmony_ci    // data in those old algorithms, the float version on 32-bit x86 and double elsewhere.
53cb93a386Sopenharmony_ci    {
54cb93a386Sopenharmony_ci        float a[9] = { 0, 1, 2,
55cb93a386Sopenharmony_ci                       3, 4, 5,
56cb93a386Sopenharmony_ci                       6, 7, 8, };
57cb93a386Sopenharmony_ci        float b[9] = { 1, 2, 0,
58cb93a386Sopenharmony_ci                       4, 5, 3,
59cb93a386Sopenharmony_ci                       7, 8, 6, };
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
62cb93a386Sopenharmony_ci    }
63cb93a386Sopenharmony_ci    {
64cb93a386Sopenharmony_ci        double a[9] = { 0, 1, 2,
65cb93a386Sopenharmony_ci                        3, 4, 5,
66cb93a386Sopenharmony_ci                        6, 7, 8, };
67cb93a386Sopenharmony_ci        double b[9] = { 1, 2, 0,
68cb93a386Sopenharmony_ci                        4, 5, 3,
69cb93a386Sopenharmony_ci                        7, 8, 6, };
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
72cb93a386Sopenharmony_ci    }
73cb93a386Sopenharmony_ci}
74cb93a386Sopenharmony_ci
75cb93a386Sopenharmony_ciDEF_TEST(ChecksumConsistent, r) {
76cb93a386Sopenharmony_ci    // We've decided to make SkOpts::hash() always return consistent results, so spot check a few:
77cb93a386Sopenharmony_ci    uint8_t bytes[256];
78cb93a386Sopenharmony_ci    for (int i = 0; i < 256; i++) {
79cb93a386Sopenharmony_ci        bytes[i] = i;
80cb93a386Sopenharmony_ci    }
81cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes,  0) == 0x00000000, "%08x", SkOpts::hash(bytes,  0));
82cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes,  1) == 0x00000000, "%08x", SkOpts::hash(bytes,  1));
83cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes,  2) == 0xf26b8303, "%08x", SkOpts::hash(bytes,  2));
84cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes,  7) == 0x18678721, "%08x", SkOpts::hash(bytes,  7));
85cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes, 32) == 0x9d1ef96b, "%08x", SkOpts::hash(bytes, 32));
86cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes, 63) == 0xc4b07d3a, "%08x", SkOpts::hash(bytes, 63));
87cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes, 64) == 0x3535a461, "%08x", SkOpts::hash(bytes, 64));
88cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes, 99) == 0x3f98a130, "%08x", SkOpts::hash(bytes, 99));
89cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, SkOpts::hash(bytes,255) == 0x3b9ceab2, "%08x", SkOpts::hash(bytes,255));
90cb93a386Sopenharmony_ci}
91