Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkChecksum.h"
      9 #include "SkOpts.h"
     10 #include "SkRandom.h"
     11 #include "Test.h"
     12 
     13 DEF_TEST(Checksum, r) {
     14     // Put 128 random bytes into two identical buffers.  Any multiple of 4 will do.
     15     const size_t kBytes = SkAlign4(128);
     16     SkRandom rand;
     17     uint32_t data[kBytes/4], tweaked[kBytes/4];
     18     for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
     19         data[i] = tweaked[i] = rand.nextU();
     20     }
     21 
     22     // Hash of nullptr is always 0.
     23     REPORTER_ASSERT(r, SkOpts::hash(nullptr, 0) == 0);
     24 
     25     const uint32_t hash = SkOpts::hash(data, kBytes);
     26     // Should be deterministic.
     27     REPORTER_ASSERT(r, hash == SkOpts::hash(data, kBytes));
     28 
     29     // Changing any single element should change the hash.
     30     for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
     31         const uint32_t saved = tweaked[j];
     32         tweaked[j] = rand.nextU();
     33         const uint32_t tweakedHash = SkOpts::hash(tweaked, kBytes);
     34         REPORTER_ASSERT(r, tweakedHash != hash);
     35         REPORTER_ASSERT(r, tweakedHash == SkOpts::hash(tweaked, kBytes));
     36         tweaked[j] = saved;
     37     }
     38 }
     39 
     40 DEF_TEST(GoodHash, r) {
     41     // 4 bytes --> hits SkChecksum::Mix fast path.
     42     REPORTER_ASSERT(r, SkGoodHash()(( int32_t)4) ==  614249093);
     43     REPORTER_ASSERT(r, SkGoodHash()((uint32_t)4) ==  614249093);
     44 }
     45