[compiler-rt] dc603b0 - [NFC][sanitizer] Add basic hash test

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 5 21:30:30 PDT 2021


Author: Vitaly Buka
Date: 2021-10-05T20:54:06-07:00
New Revision: dc603b0e53053772f48f13e1f8a3186f1e9990c7

URL: https://github.com/llvm/llvm-project/commit/dc603b0e53053772f48f13e1f8a3186f1e9990c7
DIFF: https://github.com/llvm/llvm-project/commit/dc603b0e53053772f48f13e1f8a3186f1e9990c7.diff

LOG: [NFC][sanitizer] Add basic hash test

Differential Revision: https://reviews.llvm.org/D111176

Added: 
    compiler-rt/lib/sanitizer_common/tests/sanitizer_hash_test.cpp

Modified: 
    compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
index 632b84f7f2078..3682819d0e7a7 100644
--- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
@@ -18,6 +18,7 @@ set(SANITIZER_UNITTESTS
   sanitizer_deadlock_detector_test.cpp
   sanitizer_flags_test.cpp
   sanitizer_format_interceptor_test.cpp
+  sanitizer_hash_test.cpp
   sanitizer_ioctl_test.cpp
   sanitizer_libc_test.cpp
   sanitizer_linux_test.cpp

diff  --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_hash_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_hash_test.cpp
new file mode 100644
index 0000000000000..989a412b7469e
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_hash_test.cpp
@@ -0,0 +1,34 @@
+//===-- sanitizer_hash_test.cpp -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of Sanitizers.
+//
+//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_hash.h"
+
+#include "gtest/gtest.h"
+
+namespace __sanitizer {
+
+// Tests matche a few hashes generated by https://github.com/aappleby/smhasher.
+
+TEST(SanitizerCommon, Hash32Seed) {
+  EXPECT_EQ(MurMur2HashBuilder(0).get(), 275646681u);
+  EXPECT_EQ(MurMur2HashBuilder(1).get(), 3030210376u);
+  EXPECT_EQ(MurMur2HashBuilder(3).get(), 1816185114u);
+}
+
+TEST(SanitizerCommon, Hash32Add) {
+  MurMur2HashBuilder h(123 * sizeof(u32));
+  for (u32 i = 0; i < 123; ++i) h.add(i);
+  EXPECT_EQ(h.get(), 351963665u);
+  for (u32 i = 0; i < 123; ++i) h.add(-i);
+  EXPECT_EQ(h.get(), 2640061027u);
+}
+
+}  // namespace __sanitizer


        


More information about the llvm-commits mailing list