diff --git a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt --- a/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt @@ -18,6 +18,7 @@ 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 --- /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