blob: ee33d32acd5aa185bc26c5bbe950c8c14c4bf70d [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 {
epoger@google.com0a117be2013-05-08 16:04:02 +000027 kSkChecksum
epoger@google.com4adfab82012-11-02 18:35:04 +000028 };
29
30 // Call Compute(data, size) on the appropriate checksum algorithm,
31 // depending on this->fWhichAlgorithm.
epoger@google.com0bba6bd2012-12-07 15:12:01 +000032 checksum_result ComputeChecksum(const char *data, size_t size) {
epoger@google.com4adfab82012-11-02 18:35:04 +000033 switch(fWhichAlgorithm) {
34 case kSkChecksum:
epoger@google.com0bba6bd2012-12-07 15:12:01 +000035 REPORTER_ASSERT_MESSAGE(fReporter,
36 reinterpret_cast<uintptr_t>(data) % 4 == 0,
37 "test data pointer is not 32-bit aligned");
38 REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size),
39 "test data size is not 32-bit aligned");
40 return SkChecksum::Compute(reinterpret_cast<const uint32_t *>(data), size);
epoger@google.com4adfab82012-11-02 18:35:04 +000041 default:
42 SkString message("fWhichAlgorithm has unknown value ");
43 message.appendf("%d", fWhichAlgorithm);
44 fReporter->reportFailed(message);
45 }
46 // we never get here
47 return 0;
48 }
49
50 // Confirm that the checksum algorithm (specified by fWhichAlgorithm)
51 // generates the same results if called twice over the same data.
52 void TestChecksumSelfConsistency(size_t buf_size) {
53 SkAutoMalloc storage(buf_size);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000054 char* ptr = reinterpret_cast<char *>(storage.get());
55
56 REPORTER_ASSERT(fReporter,
57 GetTestDataChecksum(8, 0) ==
58 GetTestDataChecksum(8, 0));
59 REPORTER_ASSERT(fReporter,
60 GetTestDataChecksum(8, 0) !=
61 GetTestDataChecksum(8, 1));
epoger@google.com4adfab82012-11-02 18:35:04 +000062
63 sk_bzero(ptr, buf_size);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000064 checksum_result prev = 0;
epoger@google.com4adfab82012-11-02 18:35:04 +000065
66 // assert that as we change values (from 0 to non-zero) in
67 // our buffer, we get a different value
68 for (size_t i = 0; i < buf_size; ++i) {
epoger@google.com0bba6bd2012-12-07 15:12:01 +000069 ptr[i] = (i & 0x7f) + 1; // need some non-zero value here
epoger@google.com4adfab82012-11-02 18:35:04 +000070
71 // Try checksums of different-sized chunks, but always
72 // 32-bit aligned and big enough to contain all the
epoger@google.comf74dd162012-11-21 19:04:17 +000073 // nonzero bytes. (Remaining bytes will still be zero
74 // from the initial sk_bzero() call.)
epoger@google.com4adfab82012-11-02 18:35:04 +000075 size_t checksum_size = (((i/4)+1)*4);
76 REPORTER_ASSERT(fReporter, checksum_size <= buf_size);
77
epoger@google.com0bba6bd2012-12-07 15:12:01 +000078 checksum_result curr = ComputeChecksum(ptr, checksum_size);
epoger@google.com4adfab82012-11-02 18:35:04 +000079 REPORTER_ASSERT(fReporter, prev != curr);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000080 checksum_result again = ComputeChecksum(ptr, checksum_size);
epoger@google.com4adfab82012-11-02 18:35:04 +000081 REPORTER_ASSERT(fReporter, again == curr);
82 prev = curr;
83 }
84 }
85
epoger@google.comf74dd162012-11-21 19:04:17 +000086 // Return the checksum of a buffer of bytes 'len' long.
87 // The pattern of values within the buffer will be consistent
88 // for every call, based on 'seed'.
epoger@google.com0bba6bd2012-12-07 15:12:01 +000089 checksum_result GetTestDataChecksum(size_t len, char seed=0) {
epoger@google.comf74dd162012-11-21 19:04:17 +000090 SkAutoMalloc storage(len);
epoger@google.com0bba6bd2012-12-07 15:12:01 +000091 char* start = reinterpret_cast<char *>(storage.get());
92 char* ptr = start;
epoger@google.comf74dd162012-11-21 19:04:17 +000093 for (size_t i = 0; i < len; ++i) {
94 *ptr++ = ((seed+i) & 0x7f);
95 }
epoger@google.com0bba6bd2012-12-07 15:12:01 +000096 checksum_result result = ComputeChecksum(start, len);
epoger@google.comf74dd162012-11-21 19:04:17 +000097 return result;
epoger@google.com4adfab82012-11-02 18:35:04 +000098 }
99
100 void RunTest() {
101 // Test self-consistency of checksum algorithms.
102 fWhichAlgorithm = kSkChecksum;
epoger@google.com4adfab82012-11-02 18:35:04 +0000103 TestChecksumSelfConsistency(128);
epoger@google.com4adfab82012-11-02 18:35:04 +0000104
105 // Test checksum results that should be consistent across
106 // versions and platforms.
107 fWhichAlgorithm = kSkChecksum;
108 REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
epoger@google.comf74dd162012-11-21 19:04:17 +0000109
110 // TODO: note the weakness exposed by these collisions...
epoger@google.com0bba6bd2012-12-07 15:12:01 +0000111 // We need to improve the SkChecksum algorithm.
epoger@google.comf74dd162012-11-21 19:04:17 +0000112 // We would prefer that these asserts FAIL!
113 // Filed as https://code.google.com/p/skia/issues/detail?id=981
114 // ('SkChecksum algorithm allows for way too many collisions')
115 fWhichAlgorithm = kSkChecksum;
116 REPORTER_ASSERT(fReporter,
117 GetTestDataChecksum(128) == GetTestDataChecksum(256));
118 REPORTER_ASSERT(fReporter,
119 GetTestDataChecksum(132) == GetTestDataChecksum(260));
epoger@google.com4adfab82012-11-02 18:35:04 +0000120 }
121
122 Reporter* fReporter;
123 Algorithm fWhichAlgorithm;
124 };
125
126 static TestRegistry gReg(ChecksumTestClass::Factory);
127}