Index: tests/ChecksumTest.cpp |
diff --git a/tests/ChecksumTest.cpp b/tests/ChecksumTest.cpp |
index c2f2be2ae62d528c23a1ff3b0d5a2e4dac76a34a..6316e6343f186bb96b3a393deff6906b47c7311c 100644 |
--- a/tests/ChecksumTest.cpp |
+++ b/tests/ChecksumTest.cpp |
@@ -1,142 +1,50 @@ |
- |
/* |
* Copyright 2012 Google Inc. |
* |
* Use of this source code is governed by a BSD-style license that can be |
* found in the LICENSE file. |
*/ |
-#include "Test.h" |
#include "SkChecksum.h" |
+#include "SkRandom.h" |
+#include "Test.h" |
+#include "TestClassDef.h" |
-// Word size that is large enough to hold results of any checksum type. |
-typedef uint64_t checksum_result; |
- |
-namespace skiatest { |
- class ChecksumTestClass : public Test { |
- public: |
- static Test* Factory(void*) {return SkNEW(ChecksumTestClass); } |
- protected: |
- virtual void onGetName(SkString* name) { name->set("Checksum"); } |
- virtual void onRun(Reporter* reporter) { |
- this->fReporter = reporter; |
- RunTest(); |
- } |
- private: |
- enum Algorithm { |
- kSkChecksum, |
- kMurmur3, |
- }; |
- |
- // Call Compute(data, size) on the appropriate checksum algorithm, |
- // depending on this->fWhichAlgorithm. |
- checksum_result ComputeChecksum(const char *data, size_t size) { |
- switch(fWhichAlgorithm) { |
- case kSkChecksum: |
- REPORTER_ASSERT_MESSAGE(fReporter, |
- reinterpret_cast<uintptr_t>(data) % 4 == 0, |
- "test data pointer is not 32-bit aligned"); |
- REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size), |
- "test data size is not 32-bit aligned"); |
- return SkChecksum::Compute(reinterpret_cast<const uint32_t *>(data), size); |
- case kMurmur3: |
- REPORTER_ASSERT_MESSAGE(fReporter, |
- reinterpret_cast<uintptr_t>(data) % 4 == 0, |
- "test data pointer is not 32-bit aligned"); |
- REPORTER_ASSERT_MESSAGE(fReporter, SkIsAlign4(size), |
- "test data size is not 32-bit aligned"); |
- return SkChecksum::Murmur3(reinterpret_cast<const uint32_t *>(data), size); |
- default: |
- SkString message("fWhichAlgorithm has unknown value "); |
- message.appendf("%d", fWhichAlgorithm); |
- fReporter->reportFailed(message); |
- } |
- // we never get here |
- return 0; |
- } |
- |
- // Confirm that the checksum algorithm (specified by fWhichAlgorithm) |
- // generates the same results if called twice over the same data. |
- void TestChecksumSelfConsistency(size_t buf_size) { |
- SkAutoMalloc storage(buf_size); |
- char* ptr = reinterpret_cast<char *>(storage.get()); |
- |
- REPORTER_ASSERT(fReporter, |
- GetTestDataChecksum(8, 0) == |
- GetTestDataChecksum(8, 0)); |
- REPORTER_ASSERT(fReporter, |
- GetTestDataChecksum(8, 0) != |
- GetTestDataChecksum(8, 1)); |
- |
- sk_bzero(ptr, buf_size); |
- checksum_result prev = 0; |
- |
- // assert that as we change values (from 0 to non-zero) in |
- // our buffer, we get a different value |
- for (size_t i = 0; i < buf_size; ++i) { |
- ptr[i] = (i & 0x7f) + 1; // need some non-zero value here |
- |
- // Try checksums of different-sized chunks, but always |
- // 32-bit aligned and big enough to contain all the |
- // nonzero bytes. (Remaining bytes will still be zero |
- // from the initial sk_bzero() call.) |
- size_t checksum_size = (((i/4)+1)*4); |
- REPORTER_ASSERT(fReporter, checksum_size <= buf_size); |
- |
- checksum_result curr = ComputeChecksum(ptr, checksum_size); |
- REPORTER_ASSERT(fReporter, prev != curr); |
- checksum_result again = ComputeChecksum(ptr, checksum_size); |
- REPORTER_ASSERT(fReporter, again == curr); |
- prev = curr; |
- } |
- } |
- |
- // Return the checksum of a buffer of bytes 'len' long. |
- // The pattern of values within the buffer will be consistent |
- // for every call, based on 'seed'. |
- checksum_result GetTestDataChecksum(size_t len, char seed=0) { |
- SkAutoMalloc storage(len); |
- char* start = reinterpret_cast<char *>(storage.get()); |
- char* ptr = start; |
- for (size_t i = 0; i < len; ++i) { |
- *ptr++ = ((seed+i) & 0x7f); |
- } |
- checksum_result result = ComputeChecksum(start, len); |
- return result; |
- } |
- |
- void RunTest() { |
- const Algorithm algorithms[] = { kSkChecksum, kMurmur3 }; |
- for (size_t i = 0; i < SK_ARRAY_COUNT(algorithms); i++) { |
- fWhichAlgorithm = algorithms[i]; |
- |
- // Test self-consistency of checksum algorithms. |
- TestChecksumSelfConsistency(128); |
+// Murmur3 has an optional third seed argument, so we wrap it to fit a uniform type. |
+static uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::Murmur3(d, l); } |
- // Test checksum results that should be consistent across |
- // versions and platforms. |
- REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0); |
+#define ASSERT(x) REPORTER_ASSERT(r, x) |
- const bool colision1 = GetTestDataChecksum(128) == GetTestDataChecksum(256); |
- const bool colision2 = GetTestDataChecksum(132) == GetTestDataChecksum(260); |
- if (fWhichAlgorithm == kSkChecksum) { |
- // TODO: note the weakness exposed by these collisions... |
- // We need to improve the SkChecksum algorithm. |
- // We would prefer that these asserts FAIL! |
- // Filed as https://code.google.com/p/skia/issues/detail?id=981 |
- // ('SkChecksum algorithm allows for way too many collisions') |
- REPORTER_ASSERT(fReporter, colision1); |
- REPORTER_ASSERT(fReporter, colision2); |
- } else { |
- REPORTER_ASSERT(fReporter, !colision1); |
- REPORTER_ASSERT(fReporter, !colision2); |
- } |
- } |
- } |
- |
- Reporter* fReporter; |
- Algorithm fWhichAlgorithm; |
+DEF_TEST(Checksum, r) { |
+ // Algorithms to test. They're currently all uint32_t(const uint32_t*, size_t). |
+ uint32_t(*kAlgorithms[])(const uint32_t*, size_t) = { |
tfarina
2014/01/08 18:32:40
array of pointers to functions, the beauty of C :)
mtklein
2014/01/08 18:41:55
Yeah, this always takes me forever to remember how
bungeman-skia
2014/01/09 16:08:33
I know that doing this inline is awesome and all,
mtklein
2014/01/09 16:21:46
Done.
|
+ &SkChecksum::Compute, |
+ &murmur_noseed, |
}; |
- static TestRegistry gReg(ChecksumTestClass::Factory); |
+ // Put 128 random bytes into two identical buffers. Any multiple of 4 will do. |
+ const size_t kBytes = 128; |
+ SkRandom rand; |
+ uint32_t data[kBytes/4], tweaked[kBytes/4]; |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); i++) data[i] = tweaked[i] = rand.nextU(); |
tfarina
2014/01/08 18:32:40
ok, you put random numbers in an array of 32 eleme
bungeman-skia
2014/01/09 16:08:33
The magic of putting this all on one line is almos
mtklein
2014/01/09 16:21:46
Done.
|
+ |
+ // Test each algorithm. |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); i++) { |
tfarina
2014/01/08 18:32:40
then you iterate through the array of function poi
bungeman-skia
2014/01/09 16:08:33
Totally ocd, but the post increment makes me wince
mtklein
2014/01/09 16:21:46
Done.
|
+ // Hash of NULL is always 0. |
+ ASSERT(kAlgorithms[i](NULL, 0) == 0); |
bungeman-skia
2014/01/09 16:08:33
There's a lot of kAlgorithms[i] going on here, can
mtklein
2014/01/09 16:21:46
Done.
|
+ |
+ const uint32_t hash = kAlgorithms[i](data, kBytes); |
tfarina
2014/01/08 18:32:40
so you call the algorithm to hash the data and sto
|
+ // Should be deterministic. |
+ ASSERT(hash == kAlgorithms[i](data, kBytes)); |
tfarina
2014/01/08 18:32:40
make sure it will always return the same hash for
|
+ |
+ // Changing any one element should change the hash. |
tfarina
2014/01/08 18:32:40
"any one element", that is hard to translate to my
mtklein
2014/01/08 18:41:55
"any single element"? The idea is that we have [
|
+ for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); j++) { |
+ const uint32_t saved = tweaked[j]; |
+ tweaked[j] = rand.nextU(); |
tfarina
2014/01/08 18:32:40
ok, so here you are changing the data you have pre
|
+ const uint32_t tweakedHash = kAlgorithms[i](tweaked, kBytes); |
tfarina
2014/01/08 18:32:40
then you call the algorithm again, to obtain a new
|
+ ASSERT(tweakedHash != hash); |
tfarina
2014/01/08 18:32:40
and it should be different for the original hash,
|
+ ASSERT(tweakedHash == kAlgorithms[i](tweaked, kBytes)); |
tfarina
2014/01/08 18:32:40
then you hash again to make sure the algorithm is
|
+ tweaked[j] = saved; |
tfarina
2014/01/08 18:32:40
and restore the original value (that part I didn't
mtklein
2014/01/08 18:41:55
Not strictly necessary, but we want to make sure w
|
+ } |
+ } |
} |