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 89dcdc352c14eb95a833961b22682ed9900059daemtklein#include "SkChecksum.h" 94e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein#include "SkOpts.h" 102b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org#include "SkRandom.h" 112b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org#include "Test.h" 122b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 132b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.orgDEF_TEST(Checksum, r) { 142b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org // Put 128 random bytes into two identical buffers. Any multiple of 4 will do. 152b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org const size_t kBytes = SkAlign4(128); 162b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org SkRandom rand; 172b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org uint32_t data[kBytes/4], tweaked[kBytes/4]; 182b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) { 192b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org data[i] = tweaked[i] = rand.nextU(); 202b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org } 212b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org 224e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein // Hash of nullptr is always 0. 234e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0); 244e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein 254e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein const uint32_t hash = SkOpts::hash(data, kBytes); 264e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein // Should be deterministic. 274e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes)); 284e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein 294e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein // Changing any single element should change the hash. 304e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) { 314e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein const uint32_t saved = tweaked[j]; 324e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein tweaked[j] = rand.nextU(); 334e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes); 344e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, tweakedHash != hash); 354e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes)); 364e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein tweaked[j] = saved; 372b0f7c321cd286913d63a4815e805ef55fa28e2dcommit-bot@chromium.org } 384adfab8745fd0dec1201e16c2ea89ab8e6b14ceaepoger@google.com} 3902f46cf878535fb79317d15ebed66dfa3f2cd772mtklein 4002f46cf878535fb79317d15ebed66dfa3f2cd772mtkleinDEF_TEST(GoodHash, r) { 414e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein // 4 bytes --> hits SkChecksum::Mix fast path. 424e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) == 614249093); 434e97607d9a1cef66fac16f347c5ca813ec4f9515mtklein REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) == 614249093); 4402f46cf878535fb79317d15ebed66dfa3f2cd772mtklein} 45