1 //===-- sanitizer_hash_test.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of Sanitizers.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_hash.h"
13 
14 #include "gtest/gtest.h"
15 
16 namespace __sanitizer {
17 
18 // Tests matche a few hashes generated by https://github.com/aappleby/smhasher.
19 
20 TEST(SanitizerCommon, Hash32Seed) {
21   EXPECT_EQ(MurMur2HashBuilder(0).get(), 275646681u);
22   EXPECT_EQ(MurMur2HashBuilder(1).get(), 3030210376u);
23   EXPECT_EQ(MurMur2HashBuilder(3).get(), 1816185114u);
24 }
25 
26 TEST(SanitizerCommon, Hash32Add) {
27   MurMur2HashBuilder h(123 * sizeof(u32));
28   for (u32 i = 0; i < 123; ++i) h.add(i);
29   EXPECT_EQ(h.get(), 351963665u);
30   for (u32 i = 0; i < 123; ++i) h.add(-i);
31   EXPECT_EQ(h.get(), 2640061027u);
32 }
33 
34 TEST(SanitizerCommon, Hash64Seed) {
35   EXPECT_EQ(MurMur2Hash64Builder(0).get(), 4469829599815726255ull);
36   EXPECT_EQ(MurMur2Hash64Builder(1).get(), 14121968454562043709ull);
37   EXPECT_EQ(MurMur2Hash64Builder(3).get(), 8040757559320203998ull);
38 }
39 
40 TEST(SanitizerCommon, Hash64Add) {
41   MurMur2Hash64Builder h(123 * sizeof(u64));
42   for (u32 i = 0; i < 123; ++i) h.add(i);
43   EXPECT_EQ(h.get(), 11366430808886012537ull);
44   for (u32 i = 0; i < 123; ++i) h.add(-i);
45   EXPECT_EQ(h.get(), 10843188204560467446ull);
46 }
47 
48 }  // namespace __sanitizer
49