blob: 478f8435f9113629e6c5b57f71e9e084fb074dc9 [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
epoger@google.com4adfab82012-11-02 18:35:04 +00008#include "SkChecksum.h"
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +00009#include "SkRandom.h"
10#include "Test.h"
11#include "TestClassDef.h"
epoger@google.com0bba6bd2012-12-07 15:12:01 +000012
epoger@google.com4adfab82012-11-02 18:35:04 +000013
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000014// Murmur3 has an optional third seed argument, so we wrap it to fit a uniform type.
15static uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::Murmur3(d, l); }
16
17#define ASSERT(x) REPORTER_ASSERT(r, x)
18
19DEF_TEST(Checksum, r) {
20 // Algorithms to test. They're currently all uint32_t(const uint32_t*, size_t).
21 typedef uint32_t(*algorithmProc)(const uint32_t*, size_t);
22 const algorithmProc kAlgorithms[] = { &SkChecksum::Compute, &murmur_noseed };
23
24 // Put 128 random bytes into two identical buffers. Any multiple of 4 will do.
25 const size_t kBytes = SkAlign4(128);
26 SkRandom rand;
27 uint32_t data[kBytes/4], tweaked[kBytes/4];
28 for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
29 data[i] = tweaked[i] = rand.nextU();
30 }
31
32 // Test each algorithm.
33 for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) {
34 const algorithmProc algorithm = kAlgorithms[i];
35
36 // Hash of NULL is always 0.
37 ASSERT(algorithm(NULL, 0) == 0);
38
39 const uint32_t hash = algorithm(data, kBytes);
40 // Should be deterministic.
41 ASSERT(hash == algorithm(data, kBytes));
42
43 // Changing any single element should change the hash.
44 for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
45 const uint32_t saved = tweaked[j];
46 tweaked[j] = rand.nextU();
47 const uint32_t tweakedHash = algorithm(tweaked, kBytes);
48 ASSERT(tweakedHash != hash);
49 ASSERT(tweakedHash == algorithm(tweaked, kBytes));
50 tweaked[j] = saved;
epoger@google.com4adfab82012-11-02 18:35:04 +000051 }
commit-bot@chromium.org2b0f7c32014-01-09 17:48:48 +000052 }
epoger@google.com4adfab82012-11-02 18:35:04 +000053}