blob: ecea56c28c25f5bdf49afa196a33c317c083dca6 [file] [log] [blame]
epoger@google.com4adfab82012-11-02 18:35:04 +00001/*
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 */
epoger@google.com31114c62012-12-12 17:22:23 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
epoger@google.com0bba6bd2012-12-07 15:12:01 +000013
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000014DEF_TEST(Checksum, r) {
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000015 // Put 128 random bytes into two identical buffers. Any multiple of 4 will do.
16 const size_t kBytes = SkAlign4(128);
17 SkRandom rand;
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();
21 }
22
mtklein4e976072016-08-08 09:06:27 -070023 // Hash of nullptr is always 0.
24 REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000025
mtklein4e976072016-08-08 09:06:27 -070026 const uint32_t hash = SkOpts::hash(data, kBytes);
27 // Should be deterministic.
28 REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000029
mtklein4e976072016-08-08 09:06:27 -070030 // 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));
37 tweaked[j] = saved;
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000038 }
epoger@google.com4adfab82012-11-02 18:35:04 +000039}
mtklein02f46cf2015-03-20 13:48:42 -070040
41DEF_TEST(GoodHash, r) {
mtklein4e976072016-08-08 09:06:27 -070042 // 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);
mtklein02f46cf2015-03-20 13:48:42 -070045}
Mike Kleinfd69b6d2018-10-04 13:58:31 -040046
47DEF_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.
50 //
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.
53 {
54 float a[9] = { 0, 1, 2,
55 3, 4, 5,
56 6, 7, 8, };
57 float b[9] = { 1, 2, 0,
58 4, 5, 3,
59 7, 8, 6, };
60
61 REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
62 }
63 {
64 double a[9] = { 0, 1, 2,
65 3, 4, 5,
66 6, 7, 8, };
67 double b[9] = { 1, 2, 0,
68 4, 5, 3,
69 7, 8, 6, };
70
71 REPORTER_ASSERT(r, SkOpts::hash(a, sizeof(a)) != SkOpts::hash(b, sizeof(b)));
72 }
73}