14adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com/* 24adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com * Copyright 2012 Google Inc. 34adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com * 44adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com * Use of this source code is governed by a BSD-style license that can be 54adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com * found in the LICENSE file. 64adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com */ 731114c69f39befd50d2b755b7d0dd1cda2c6d2abepoger@google.com 84adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com#include "SkChecksum.h" 92b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org#include "SkRandom.h" 102b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org#include "Test.h" 112b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 122b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 132b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org// Murmur3 has an optional third seed argument, so we wrap it to fit a uniform type. 142b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.orgstatic uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::Murmur3(d, l); } 152b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 162b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org#define ASSERT(x) REPORTER_ASSERT(r, x) 172b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 182b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.orgDEF_TEST(Checksum, r) { 192b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Algorithms to test. They're currently all uint32_t(const uint32_t*, size_t). 202b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org typedef uint32_t(*algorithmProc)(const uint32_t*, size_t); 212b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const algorithmProc kAlgorithms[] = { &SkChecksum::Compute, &murmur_noseed }; 222b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 232b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Put 128 random bytes into two identical buffers. Any multiple of 4 will do. 242b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const size_t kBytes = SkAlign4(128); 252b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org SkRandom rand; 262b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org uint32_t data[kBytes/4], tweaked[kBytes/4]; 272b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { 282b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org data[i] = tweaked[i] = rand.nextU(); 292b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org } 302b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 312b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Test each algorithm. 322b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) { 332b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const algorithmProc algorithm = kAlgorithms[i]; 342b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 352b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Hash of NULL is always 0. 362b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org ASSERT(algorithm(NULL, 0) == 0); 372b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 382b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const uint32_t hash = algorithm(data, kBytes); 392b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Should be deterministic. 402b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org ASSERT(hash == algorithm(data, kBytes)); 412b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 422b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Changing any single element should change the hash. 432b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { 442b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const uint32_t saved = tweaked[j]; 452b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org tweaked[j] = rand.nextU(); 462b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const uint32_t tweakedHash = algorithm(tweaked, kBytes); 472b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org ASSERT(tweakedHash != hash); 482b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org ASSERT(tweakedHash == algorithm(tweaked, kBytes)); 492b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org tweaked[j] = saved; 504adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com } 512b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org } 524adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com} 53