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