epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 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 | */ |
epoger@google.com | 31114c6 | 2012-12-12 17:22:23 +0000 | [diff] [blame] | 7 | |
mtklein | 9dcdc35 | 2016-08-08 12:54:08 -0700 | [diff] [blame] | 8 | #include "SkChecksum.h" |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 9 | #include "SkOpts.h" |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 10 | #include "SkRandom.h" |
Ben Wagner | b607a8f | 2018-03-12 13:46:21 -0400 | [diff] [blame^] | 11 | #include "SkTypes.h" |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 12 | #include "Test.h" |
epoger@google.com | 0bba6bd | 2012-12-07 15:12:01 +0000 | [diff] [blame] | 13 | |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 14 | DEF_TEST(Checksum, r) { |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 15 | // 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 | |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 23 | // Hash of nullptr is always 0. |
| 24 | REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0); |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 25 | |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 26 | const uint32_t hash = SkOpts::hash(data, kBytes); |
| 27 | // Should be deterministic. |
| 28 | REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes)); |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 29 | |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 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)); |
| 37 | tweaked[j] = saved; |
commit-bot@chromium.org | 2b0f7c3 | 2014-01-09 17:48:48 +0000 | [diff] [blame] | 38 | } |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 39 | } |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 40 | |
| 41 | DEF_TEST(GoodHash, r) { |
mtklein | 4e97607 | 2016-08-08 09:06:27 -0700 | [diff] [blame] | 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); |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 45 | } |