blob: 2823698a337c8964f27a278c967535b39b365657 [file] [log] [blame]
epoger@google.com908f5832013-04-12 02:23:55 +00001
2/*
3 * Copyright 2013 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
10#include "SkBitmap.h"
11#include "SkBitmapHasher.h"
12#include "SkColor.h"
13
14// Word size that is large enough to hold results of any checksum type.
15typedef uint64_t checksum_result;
16
17namespace skiatest {
18 class BitmapHasherTestClass : public Test {
19 public:
20 static Test* Factory(void*) {return SkNEW(BitmapHasherTestClass); }
21 protected:
22 virtual void onGetName(SkString* name) { name->set("BitmapHasher"); }
23 virtual void onRun(Reporter* reporter) {
24 this->fReporter = reporter;
25 RunTest();
26 }
27 private:
28
29 // Fill in bitmap with test data.
30 void CreateTestBitmap(SkBitmap &bitmap, SkBitmap::Config config, int width, int height,
31 SkColor color) {
32 bitmap.setConfig(config, width, height);
33 REPORTER_ASSERT(fReporter, bitmap.allocPixels());
34 bitmap.setIsOpaque(true);
35 bitmap.eraseColor(color);
36 }
37
38 void RunTest() {
39 // Test SkBitmapHasher
40 SkBitmap bitmap;
41 SkHashDigest digest;
42 // initial test case
43 CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 333, 555, SK_ColorBLUE);
44 REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
epoger@google.comb4ca46d2013-05-03 17:35:39 +000045#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
46 REPORTER_ASSERT(fReporter, digest == 0xfb2903562766ef87ULL);
47#else
epoger@google.com908f5832013-04-12 02:23:55 +000048 REPORTER_ASSERT(fReporter, digest == 0x18f9df68b1b02f38ULL);
epoger@google.comb4ca46d2013-05-03 17:35:39 +000049#endif
epoger@google.com908f5832013-04-12 02:23:55 +000050 // same pixel data but different dimensions should yield a different checksum
51 CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorBLUE);
52 REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
epoger@google.comb4ca46d2013-05-03 17:35:39 +000053#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
54 REPORTER_ASSERT(fReporter, digest == 0xfe04023fb97d0f61ULL);
55#else
epoger@google.com908f5832013-04-12 02:23:55 +000056 REPORTER_ASSERT(fReporter, digest == 0x6b0298183f786c8eULL);
epoger@google.comb4ca46d2013-05-03 17:35:39 +000057#endif
epoger@google.com908f5832013-04-12 02:23:55 +000058 // same dimensions but different color should yield a different checksum
59 CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorGREEN);
60 REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
epoger@google.comb4ca46d2013-05-03 17:35:39 +000061#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
62 REPORTER_ASSERT(fReporter, digest == 0x2423c51cad6d1edcULL);
63#else
epoger@google.com908f5832013-04-12 02:23:55 +000064 REPORTER_ASSERT(fReporter, digest == 0xc6b4b3f6fadaaf37ULL);
epoger@google.comb4ca46d2013-05-03 17:35:39 +000065#endif
epoger@google.com908f5832013-04-12 02:23:55 +000066 // same pixel colors in a different config should yield the same checksum
67 CreateTestBitmap(bitmap, SkBitmap::kARGB_4444_Config, 555, 333, SK_ColorGREEN);
68 REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
epoger@google.comb4ca46d2013-05-03 17:35:39 +000069#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
70 REPORTER_ASSERT(fReporter, digest == 0x2423c51cad6d1edcULL);
71#else
epoger@google.com908f5832013-04-12 02:23:55 +000072 REPORTER_ASSERT(fReporter, digest == 0xc6b4b3f6fadaaf37ULL);
epoger@google.comb4ca46d2013-05-03 17:35:39 +000073#endif
epoger@google.com908f5832013-04-12 02:23:55 +000074 }
75
76 Reporter* fReporter;
77 };
78
79 static TestRegistry gReg(BitmapHasherTestClass::Factory);
80}