blob: c2f2be2ae62d528c23a1ff3b0d5a2e4dac76a34a [file] [log] [blame]
epoger@google.com4adfab82012-11-02 18:35:04 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "Test.h"
epoger@google.com31114c62012-12-12 17:22:23 +00009
epoger@google.com4adfab82012-11-02 18:35:04 +000010#include "SkChecksum.h"
epoger@google.com0bba6bd2012-12-07 15:12:01 +000011
12// Word size that is large enough to hold results of any checksum type.
13typedef uint64_t checksum_result;
epoger@google.com4adfab82012-11-02 18:35:04 +000014
15namespace skiatest {
16 class ChecksumTestClass : public Test {
17 public:
18 static Test* Factory(void*) {return SkNEW(ChecksumTestClass); }
19 protected:
20 virtual void onGetName(SkString* name) { name->set("Checksum"); }
21 virtual void onRun(Reporter* reporter) {
22 this->fReporter = reporter;
23 RunTest();
24 }
25 private:
26 enum Algorithm {
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000027 kSkChecksum,
28 kMurmur3,
epoger@google.com4adfab82012-11-02 18:35:04 +000029 };
30
31 // Call Compute(data, size) on the appropriate checksum algorithm,
32 // depending on this->fWhichAlgorithm.
epoger@google.com0bba6bd2012-12-07 15:12:01 +000033 checksum_result ComputeChecksum(const char *data, size_t size) {
epoger@google.com4adfab82012-11-02 18:35:04 +000034 switch(fWhichAlgorithm) {
35 case kSkChecksum:
epoger@google.com0bba6bd2012-12-07 15:12:01 +000036 REPORTER_ASSERT_MESSAGE(fReporter,
37 reinterpret_cast<uintptr_t>(data) % 4 == 0,
38 "test data pointer is not 32-bit aligned");
39 REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size),
40 "test data size is not 32-bit aligned");
41 return SkChecksum::Compute(reinterpret_cast<const uint32_t *>(data), size);
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +000042 case kMurmur3:
43 REPORTER_ASSERT_MESSAGE(fReporter,
44 reinterpret_cast<uintptr_t>(data) % 4 == 0,
45 "test data pointer is not 32-bit aligned");
46 REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size),
47 "test data size is not 32-bit aligned");
48 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t *>(data), size);
epoger@google.com4adfab82012-11-02 18:35:04 +000049 default:
50 SkString message("fWhichAlgorithm has unknown value ");
51 message.appendf("%d", fWhichAlgorithm);
52 fReporter->reportFailed(message);
53 }
54 // we never get here
55 return 0;
56 }
57
58 // Confirm that the checksum algorithm (specified by fWhichAlgorithm)
59 // generates the same results if called twice over the same data.
60 void TestChecksumSelfConsistency(size_t buf_size) {
61 SkAutoMalloc storage(buf_size);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000062 char* ptr = reinterpret_cast<char *>(storage.get());
63
64 REPORTER_ASSERT(fReporter,
65 GetTestDataChecksum(8, 0) ==
66 GetTestDataChecksum(8, 0));
67 REPORTER_ASSERT(fReporter,
68 GetTestDataChecksum(8, 0) !=
69 GetTestDataChecksum(8, 1));
epoger@google.com4adfab82012-11-02 18:35:04 +000070
71 sk_bzero(ptr, buf_size);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000072 checksum_result prev = 0;
epoger@google.com4adfab82012-11-02 18:35:04 +000073
74 // assert that as we change values (from 0 to non-zero) in
75 // our buffer, we get a different value
76 for (size_t i = 0; i < buf_size; ++i) {
epoger@google.com0bba6bd2012-12-07 15:12:01 +000077 ptr[i] = (i & 0x7f) + 1; // need some non-zero value here
epoger@google.com4adfab82012-11-02 18:35:04 +000078
79 // Try checksums of different-sized chunks, but always
80 // 32-bit aligned and big enough to contain all the
epoger@google.comf74dd162012-11-21 19:04:17 +000081 // nonzero bytes. (Remaining bytes will still be zero
82 // from the initial sk_bzero() call.)
epoger@google.com4adfab82012-11-02 18:35:04 +000083 size_t checksum_size = (((i/4)+1)*4);
84 REPORTER_ASSERT(fReporter, checksum_size <= buf_size);
85
epoger@google.com0bba6bd2012-12-07 15:12:01 +000086 checksum_result curr = ComputeChecksum(ptr, checksum_size);
epoger@google.com4adfab82012-11-02 18:35:04 +000087 REPORTER_ASSERT(fReporter, prev != curr);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000088 checksum_result again = ComputeChecksum(ptr, checksum_size);
epoger@google.com4adfab82012-11-02 18:35:04 +000089 REPORTER_ASSERT(fReporter, again == curr);
90 prev = curr;
91 }
92 }
93
epoger@google.comf74dd162012-11-21 19:04:17 +000094 // Return the checksum of a buffer of bytes 'len' long.
95 // The pattern of values within the buffer will be consistent
96 // for every call, based on 'seed'.
epoger@google.com0bba6bd2012-12-07 15:12:01 +000097 checksum_result GetTestDataChecksum(size_t len, char seed=0) {
epoger@google.comf74dd162012-11-21 19:04:17 +000098 SkAutoMalloc storage(len);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000099 char* start = reinterpret_cast<char *>(storage.get());
100 char* ptr = start;
epoger@google.comf74dd162012-11-21 19:04:17 +0000101 for (size_t i = 0; i < len; ++i) {
102 *ptr++ = ((seed+i) & 0x7f);
103 }
epoger@google.com0bba6bd2012-12-07 15:12:01 +0000104 checksum_result result = ComputeChecksum(start, len);
epoger@google.comf74dd162012-11-21 19:04:17 +0000105 return result;
epoger@google.com4adfab82012-11-02 18:35:04 +0000106 }
107
108 void RunTest() {
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +0000109 const Algorithm algorithms[] = { kSkChecksum, kMurmur3 };
110 for (size_t i = 0; i < SK_ARRAY_COUNT(algorithms); i++) {
111 fWhichAlgorithm = algorithms[i];
epoger@google.com4adfab82012-11-02 18:35:04 +0000112
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +0000113 // Test self-consistency of checksum algorithms.
114 TestChecksumSelfConsistency(128);
epoger@google.comf74dd162012-11-21 19:04:17 +0000115
commit-bot@chromium.org70d75ca2013-07-23 20:25:34 +0000116 // Test checksum results that should be consistent across
117 // versions and platforms.
118 REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
119
120 const bool colision1 = GetTestDataChecksum(128) == GetTestDataChecksum(256);
121 const bool colision2 = GetTestDataChecksum(132) == GetTestDataChecksum(260);
122 if (fWhichAlgorithm == kSkChecksum) {
123 // TODO: note the weakness exposed by these collisions...
124 // We need to improve the SkChecksum algorithm.
125 // We would prefer that these asserts FAIL!
126 // Filed as https://code.google.com/p/skia/issues/detail?id=981
127 // ('SkChecksum algorithm allows for way too many collisions')
128 REPORTER_ASSERT(fReporter, colision1);
129 REPORTER_ASSERT(fReporter, colision2);
130 } else {
131 REPORTER_ASSERT(fReporter, !colision1);
132 REPORTER_ASSERT(fReporter, !colision2);
133 }
134 }
epoger@google.com4adfab82012-11-02 18:35:04 +0000135 }
136
137 Reporter* fReporter;
138 Algorithm fWhichAlgorithm;
139 };
140
141 static TestRegistry gReg(ChecksumTestClass::Factory);
142}