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 "SkRandom.h"
     10 #include "Test.h"
     11 
     12 
     13 // Murmur3 has an optional third seed argument, so we wrap it to fit a uniform type.
     14 static uint32_t murmur_noseed(const uint32_t* d, size_t l) { return SkChecksum::Murmur3(d, l); }
     15 
     16 #define ASSERT(x) REPORTER_ASSERT(r, x)
     17 
     18 DEF_TEST(Checksum, r) {
     19     // Algorithms to test.  They're currently all uint32_t(const uint32_t*, size_t).
     20     typedef uint32_t(*algorithmProc)(const uint32_t*, size_t);
     21     const algorithmProc kAlgorithms[] = { &SkChecksum::Compute, &murmur_noseed };
     22 
     23     // Put 128 random bytes into two identical buffers.  Any multiple of 4 will do.
     24     const size_t kBytes = SkAlign4(128);
     25     SkRandom rand;
     26     uint32_t data[kBytes/4], tweaked[kBytes/4];
     27     for (size_t i = 0; i < SK_ARRAY_COUNT(tweaked); ++i) {
     28         data[i] = tweaked[i] = rand.nextU();
     29     }
     30 
     31     // Test each algorithm.
     32     for (size_t i = 0; i < SK_ARRAY_COUNT(kAlgorithms); ++i) {
     33         const algorithmProc algorithm = kAlgorithms[i];
     34 
     35         // Hash of NULL is always 0.
     36         ASSERT(algorithm(NULL, 0) == 0);
     37 
     38         const uint32_t hash = algorithm(data, kBytes);
     39         // Should be deterministic.
     40         ASSERT(hash == algorithm(data, kBytes));
     41 
     42         // Changing any single element should change the hash.
     43         for (size_t j = 0; j < SK_ARRAY_COUNT(tweaked); ++j) {
     44             const uint32_t saved = tweaked[j];
     45             tweaked[j] = rand.nextU();
     46             const uint32_t tweakedHash = algorithm(tweaked, kBytes);
     47             ASSERT(tweakedHash != hash);
     48             ASSERT(tweakedHash == algorithm(tweaked, kBytes));
     49             tweaked[j] = saved;
     50         }
     51     }
     52 }
     53