LCOV - code coverage report
Current view: top level - test/core/support - murmur_hash_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 17 19 89.5 %
Date: 2015-10-10 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "src/core/support/murmur_hash.h"
      35             : #include <grpc/support/log.h>
      36             : #include <grpc/support/string_util.h>
      37             : #include "test/core/util/test_config.h"
      38             : 
      39             : #include <string.h>
      40             : 
      41             : typedef gpr_uint32 (*hash_func)(const void *key, size_t len, gpr_uint32 seed);
      42             : 
      43             : /* From smhasher:
      44             :    This should hopefully be a thorough and uambiguous test of whether a hash
      45             :    is correctly implemented on a given platform */
      46             : 
      47           1 : static void verification_test(hash_func hash, gpr_uint32 expected) {
      48             :   gpr_uint8 key[256];
      49             :   gpr_uint32 hashes[256];
      50           1 :   gpr_uint32 final = 0;
      51             :   size_t i;
      52             : 
      53           1 :   memset(key, 0, sizeof(key));
      54           1 :   memset(hashes, 0, sizeof(hashes));
      55             : 
      56             :   /* Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
      57             :      the seed */
      58             : 
      59         257 :   for (i = 0; i < 256; i++) {
      60         256 :     key[i] = (gpr_uint8)i;
      61         256 :     hashes[i] = hash(key, i, (gpr_uint32)(256u - i));
      62             :   }
      63             : 
      64             :   /* Then hash the result array */
      65             : 
      66           1 :   final = hash(hashes, sizeof(hashes), 0);
      67             : 
      68             :   /* The first four bytes of that hash, interpreted as a little-endian integer,
      69             :      is our
      70             :      verification value */
      71             : 
      72           1 :   if (expected != final) {
      73           0 :     gpr_log(GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)",
      74             :             final, expected);
      75           0 :     abort();
      76             :   } else {
      77           1 :     gpr_log(GPR_INFO, "Verification value 0x%08X : Passed!", final);
      78             :   }
      79           1 : }
      80             : 
      81           1 : int main(int argc, char **argv) {
      82           1 :   grpc_test_init(argc, argv);
      83             :   /* basic tests to verify that things don't crash */
      84           1 :   gpr_murmur_hash3("", 0, 0);
      85           1 :   gpr_murmur_hash3("xyz", 3, 0);
      86           1 :   verification_test(gpr_murmur_hash3, 0xB0F57EE3);
      87           1 :   return 0;
      88             : }

Generated by: LCOV version 1.10