blob: 1882235e30e991c387fea68e37c2738d548b7f95 [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
mtklein9dcdc352016-08-08 12:54:08 -07008#include "SkChecksum.h"
mtklein4e976072016-08-08 09:06:27 -07009#include "SkOpts.h"
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000010#include "SkRandom.h"
11#include "Test.h"
epoger@google.com0bba6bd2012-12-07 15:12:01 +000012
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000013DEF_TEST(Checksum, r) {
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000014 // Put 128 random bytes into two identical buffers. Any multiple of 4 will do.
15 const size_t kBytes = SkAlign4(128);
16 SkRandom rand;
17 uint32_t data[kBytes/4], tweaked[kBytes/4];
18 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
19 data[i] = tweaked[i] = rand.nextU();
20 }
21
mtklein4e976072016-08-08 09:06:27 -070022 // Hash of nullptr is always 0.
23 REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000024
mtklein4e976072016-08-08 09:06:27 -070025 const uint32_t hash = SkOpts::hash(data, kBytes);
26 // Should be deterministic.
27 REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000028
mtklein4e976072016-08-08 09:06:27 -070029 // Changing any single element should change the hash.
30 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
31 const uint32_t saved = tweaked[j];
32 tweaked[j] = rand.nextU();
33 const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
34 REPORTER_ASSERT(r, tweakedHash != hash);
35 REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
36 tweaked[j] = saved;
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000037 }
epoger@google.com4adfab82012-11-02 18:35:04 +000038}
mtklein02f46cf2015-03-20 13:48:42 -070039
40DEF_TEST(GoodHash, r) {
mtklein4e976072016-08-08 09:06:27 -070041 // 4 bytes --> hits SkChecksum::Mix fast path.
42 REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) == 614249093);
43 REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) == 614249093);
mtklein02f46cf2015-03-20 13:48:42 -070044}