skia2/tests/ChecksumTest.cpp
Ben Wagner b607a8fbf0 IWYU for some test files starting with 'C'.
Change-Id: Iea274ae52da8b4b87ec55222c856f40a0d88c4e0
Reviewed-on: https://skia-review.googlesource.com/113746
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-on: https://skia-review.googlesource.com/113944
Reviewed-by: Ben Wagner <bungeman@google.com>
2018-03-13 15:16:22 +00:00

46 lines
1.4 KiB
C++

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkChecksum.h"
#include "SkOpts.h"
#include "SkRandom.h"
#include "SkTypes.h"
#include "Test.h"
DEF_TEST(Checksum, r) {
// Put 128 random bytes into two identical buffers. Any multiple of 4 will do.
const size_t kBytes = SkAlign4(128);
SkRandom rand;
uint32_t data[kBytes/4], tweaked[kBytes/4];
for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
data[i] = tweaked[i] = rand.nextU();
}
// Hash of nullptr is always 0.
REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
const uint32_t hash = SkOpts::hash(data, kBytes);
// Should be deterministic.
REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
// Changing any single element should change the hash.
for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
const uint32_t saved = tweaked[j];
tweaked[j] = rand.nextU();
const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
REPORTER_ASSERT(r, tweakedHash != hash);
REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
tweaked[j] = saved;
}
}
DEF_TEST(GoodHash, r) {
// 4 bytes --> hits SkChecksum::Mix fast path.
REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) == 614249093);
REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) == 614249093);
}