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