blob: 3425bcd296fbedb87b9e885e9fdbb749e5fdf168 [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"
9#include "SkChecksum.h"
10#include "SkConsistentChecksum.h"
11
12namespace 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
71 // nonzero bytes.
72 size_t checksum_size = (((i/4)+1)*4);
73 REPORTER_ASSERT(fReporter, checksum_size <= buf_size);
74
75 uint32_t curr = ComputeChecksum(ptr, checksum_size);
76 REPORTER_ASSERT(fReporter, prev != curr);
77 uint32_t again = ComputeChecksum(ptr, checksum_size);
78 REPORTER_ASSERT(fReporter, again == curr);
79 prev = curr;
80 }
81 }
82
83 // Return the checksum of a portion of this static test data
84 // (8 bytes repeated twice): "1234567812345678"
85 uint32_t GetTestDataChecksum(size_t offset, size_t len) {
86 static char testbytes[] = "1234567812345678";
87 uint32_t* ptr = reinterpret_cast<uint32_t*>(testbytes);
88 return ComputeChecksum(ptr, len);
89 }
90
91 void RunTest() {
92 // Test self-consistency of checksum algorithms.
93 fWhichAlgorithm = kSkChecksum;
94 REPORTER_ASSERT(fReporter,
95 GetTestDataChecksum(0, 8) ==
96 GetTestDataChecksum(8, 8));
97 TestChecksumSelfConsistency(128);
98 fWhichAlgorithm = kSkConsistentChecksum;
99 REPORTER_ASSERT(fReporter,
100 GetTestDataChecksum(0, 8) ==
101 GetTestDataChecksum(8, 8));
102 TestChecksumSelfConsistency(128);
103
104 // Test checksum results that should be consistent across
105 // versions and platforms.
106 fWhichAlgorithm = kSkChecksum;
107 REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
108 fWhichAlgorithm = kSkConsistentChecksum;
109 REPORTER_ASSERT(fReporter, ComputeChecksum(NULL, 0) == 0);
110 REPORTER_ASSERT(fReporter, GetTestDataChecksum(0, 8) == 0xa12fac2c);
111 REPORTER_ASSERT(fReporter, GetTestDataChecksum(8, 8) == 0xa12fac2c);
112 REPORTER_ASSERT(fReporter, GetTestDataChecksum(8, 4) == 0x34333231);
113 }
114
115 Reporter* fReporter;
116 Algorithm fWhichAlgorithm;
117 };
118
119 static TestRegistry gReg(ChecksumTestClass::Factory);
120}