skia2/tests/BitmapHasherTest.cpp
epoger@google.com b4ca46d748 SkBitmapHasher: use 64-bit-truncated MD5 instead of 64-bit CityHash
BUG=https://code.google.com/p/skia/issues/detail?id=1257

(if we change our mind within the next few days, we can toggle the
BITMAPHASHER_USES_TRUNCATED_MD5 #ifdef ; at some point, we'll remove that
option so we can delete our CityHash implementation entirely)

R=bungeman@google.com

Review URL: https://codereview.chromium.org/14054012

git-svn-id: http://skia.googlecode.com/svn/trunk@8992 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-05-03 17:35:39 +00:00

81 lines
3.1 KiB
C++

/*
* Copyright 2013 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 "SkBitmap.h"
#include "SkBitmapHasher.h"
#include "SkColor.h"
// Word size that is large enough to hold results of any checksum type.
typedef uint64_t checksum_result;
namespace skiatest {
class BitmapHasherTestClass : public Test {
public:
static Test* Factory(void*) {return SkNEW(BitmapHasherTestClass); }
protected:
virtual void onGetName(SkString* name) { name->set("BitmapHasher"); }
virtual void onRun(Reporter* reporter) {
this->fReporter = reporter;
RunTest();
}
private:
// Fill in bitmap with test data.
void CreateTestBitmap(SkBitmap &bitmap, SkBitmap::Config config, int width, int height,
SkColor color) {
bitmap.setConfig(config, width, height);
REPORTER_ASSERT(fReporter, bitmap.allocPixels());
bitmap.setIsOpaque(true);
bitmap.eraseColor(color);
}
void RunTest() {
// Test SkBitmapHasher
SkBitmap bitmap;
SkHashDigest digest;
// initial test case
CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 333, 555, SK_ColorBLUE);
REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
REPORTER_ASSERT(fReporter, digest == 0xfb2903562766ef87ULL);
#else
REPORTER_ASSERT(fReporter, digest == 0x18f9df68b1b02f38ULL);
#endif
// same pixel data but different dimensions should yield a different checksum
CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorBLUE);
REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
REPORTER_ASSERT(fReporter, digest == 0xfe04023fb97d0f61ULL);
#else
REPORTER_ASSERT(fReporter, digest == 0x6b0298183f786c8eULL);
#endif
// same dimensions but different color should yield a different checksum
CreateTestBitmap(bitmap, SkBitmap::kARGB_8888_Config, 555, 333, SK_ColorGREEN);
REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
REPORTER_ASSERT(fReporter, digest == 0x2423c51cad6d1edcULL);
#else
REPORTER_ASSERT(fReporter, digest == 0xc6b4b3f6fadaaf37ULL);
#endif
// same pixel colors in a different config should yield the same checksum
CreateTestBitmap(bitmap, SkBitmap::kARGB_4444_Config, 555, 333, SK_ColorGREEN);
REPORTER_ASSERT(fReporter, SkBitmapHasher::ComputeDigest(bitmap, &digest));
#ifdef BITMAPHASHER_USES_TRUNCATED_MD5
REPORTER_ASSERT(fReporter, digest == 0x2423c51cad6d1edcULL);
#else
REPORTER_ASSERT(fReporter, digest == 0xc6b4b3f6fadaaf37ULL);
#endif
}
Reporter* fReporter;
};
static TestRegistry gReg(BitmapHasherTestClass::Factory);
}