epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 1 | |
| 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" |
| 9 | #include "SkChecksum.h" |
| 10 | #include "SkConsistentChecksum.h" |
| 11 | |
| 12 | namespace skiatest { |
| 13 | class ChecksumTestClass : public Test { |
| 14 | public: |
| 15 | static Test* Factory(void*) {return SkNEW(ChecksumTestClass); } |
| 16 | protected: |
| 17 | virtual void onGetName(SkString* name) { name->set("Checksum"); } |
| 18 | virtual void onRun(Reporter* reporter) { |
| 19 | this->fReporter = reporter; |
| 20 | RunTest(); |
| 21 | } |
| 22 | private: |
| 23 | enum Algorithm { |
| 24 | kSkChecksum, |
| 25 | kSkConsistentChecksum |
| 26 | }; |
| 27 | |
| 28 | // Call Compute(data, size) on the appropriate checksum algorithm, |
| 29 | // depending on this->fWhichAlgorithm. |
| 30 | uint32_t ComputeChecksum(uint32_t* data, size_t size) { |
| 31 | // Our checksum algorithms require 32-bit aligned data. |
| 32 | // If either of these tests fail, then the algorithm |
| 33 | // doesn't have a chance. |
| 34 | REPORTER_ASSERT_MESSAGE(fReporter, |
| 35 | reinterpret_cast<uintptr_t>(data) % 4 == 0, |
| 36 | "test data pointer is not 32-bit aligned"); |
| 37 | REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size), |
| 38 | "test data size is not 32-bit aligned"); |
| 39 | |
| 40 | switch(fWhichAlgorithm) { |
| 41 | case kSkChecksum: |
| 42 | return SkChecksum::Compute(data, size); |
| 43 | case kSkConsistentChecksum: |
| 44 | return SkConsistentChecksum::Compute(data, size); |
| 45 | default: |
| 46 | SkString message("fWhichAlgorithm has unknown value "); |
| 47 | message.appendf("%d", fWhichAlgorithm); |
| 48 | fReporter->reportFailed(message); |
| 49 | } |
| 50 | // we never get here |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | // Confirm that the checksum algorithm (specified by fWhichAlgorithm) |
| 55 | // generates the same results if called twice over the same data. |
| 56 | void TestChecksumSelfConsistency(size_t buf_size) { |
| 57 | SkAutoMalloc storage(buf_size); |
| 58 | uint32_t* ptr = (uint32_t*)storage.get(); |
| 59 | char* cptr = (char*)ptr; |
| 60 | |
| 61 | sk_bzero(ptr, buf_size); |
| 62 | uint32_t prev = 0; |
| 63 | |
| 64 | // assert that as we change values (from 0 to non-zero) in |
| 65 | // our buffer, we get a different value |
| 66 | for (size_t i = 0; i < buf_size; ++i) { |
| 67 | cptr[i] = (i & 0x7f) + 1; // need some non-zero value here |
| 68 | |
| 69 | // Try checksums of different-sized chunks, but always |
| 70 | // 32-bit aligned and big enough to contain all the |
epoger@google.com | f74dd16 | 2012-11-21 19:04:17 +0000 | [diff] [blame^] | 71 | // nonzero bytes. (Remaining bytes will still be zero |
| 72 | // from the initial sk_bzero() call.) |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 73 | size_t checksum_size = (((i/4)+1)*4); |
| 74 | REPORTER_ASSERT(fReporter, checksum_size <= buf_size); |
| 75 | |
| 76 | uint32_t curr = ComputeChecksum(ptr, checksum_size); |
| 77 | REPORTER_ASSERT(fReporter, prev != curr); |
| 78 | uint32_t again = ComputeChecksum(ptr, checksum_size); |
| 79 | REPORTER_ASSERT(fReporter, again == curr); |
| 80 | prev = curr; |
| 81 | } |
| 82 | } |
| 83 | |
epoger@google.com | f74dd16 | 2012-11-21 19:04:17 +0000 | [diff] [blame^] | 84 | // Return the checksum of a buffer of bytes 'len' long. |
| 85 | // The pattern of values within the buffer will be consistent |
| 86 | // for every call, based on 'seed'. |
| 87 | uint32_t GetTestDataChecksum(size_t len, char seed=0) { |
| 88 | SkAutoMalloc storage(len); |
| 89 | uint32_t* start = (uint32_t *)storage.get(); |
| 90 | char* ptr = (char *)start; |
| 91 | for (size_t i = 0; i < len; ++i) { |
| 92 | *ptr++ = ((seed+i) & 0x7f); |
| 93 | } |
| 94 | uint32_t result = ComputeChecksum(start, len); |
| 95 | return result; |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void RunTest() { |
| 99 | // Test self-consistency of checksum algorithms. |
| 100 | fWhichAlgorithm = kSkChecksum; |
| 101 | REPORTER_ASSERT(fReporter, |
epoger@google.com | f74dd16 | 2012-11-21 19:04:17 +0000 | [diff] [blame^] | 102 | GetTestDataChecksum(8, 0) == |
| 103 | GetTestDataChecksum(8, 0)); |
| 104 | REPORTER_ASSERT(fReporter, |
| 105 | GetTestDataChecksum(8, 0) != |
| 106 | GetTestDataChecksum(8, 1)); |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 107 | TestChecksumSelfConsistency(128); |
| 108 | fWhichAlgorithm = kSkConsistentChecksum; |
| 109 | REPORTER_ASSERT(fReporter, |
epoger@google.com | f74dd16 | 2012-11-21 19:04:17 +0000 | [diff] [blame^] | 110 | GetTestDataChecksum(8, 0) == |
| 111 | GetTestDataChecksum(8, 0)); |
| 112 | REPORTER_ASSERT(fReporter, |
| 113 | GetTestDataChecksum(8, 0) != |
| 114 | GetTestDataChecksum(8, 1)); |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 115 | TestChecksumSelfConsistency(128); |
| 116 | |
| 117 | // Test checksum results that should be consistent across |
| 118 | // versions and platforms. |
| 119 | fWhichAlgorithm = kSkChecksum; |
| 120 | REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0); |
| 121 | fWhichAlgorithm = kSkConsistentChecksum; |
| 122 | REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0); |
epoger@google.com | f74dd16 | 2012-11-21 19:04:17 +0000 | [diff] [blame^] | 123 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(4) == 0x03020100); |
| 124 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(8) == 0x07860485); |
| 125 | |
| 126 | // TODO: note the weakness exposed by these collisions... |
| 127 | // We need to improve the SkConsistentChecksum algorithm |
| 128 | // (and maybe SkChecksum too?) |
| 129 | // We would prefer that these asserts FAIL! |
| 130 | // Filed as https://code.google.com/p/skia/issues/detail?id=981 |
| 131 | // ('SkChecksum algorithm allows for way too many collisions') |
| 132 | fWhichAlgorithm = kSkChecksum; |
| 133 | REPORTER_ASSERT(fReporter, |
| 134 | GetTestDataChecksum(128) == GetTestDataChecksum(256)); |
| 135 | REPORTER_ASSERT(fReporter, |
| 136 | GetTestDataChecksum(132) == GetTestDataChecksum(260)); |
| 137 | fWhichAlgorithm = kSkConsistentChecksum; |
| 138 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(128) == 0); |
| 139 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(132) == 0x03020100); |
| 140 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(256) == 0); |
| 141 | REPORTER_ASSERT(fReporter, GetTestDataChecksum(260) == 0x03020100); |
epoger@google.com | 4adfab8 | 2012-11-02 18:35:04 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | Reporter* fReporter; |
| 145 | Algorithm fWhichAlgorithm; |
| 146 | }; |
| 147 | |
| 148 | static TestRegistry gReg(ChecksumTestClass::Factory); |
| 149 | } |