From 1c2d130b25c97d0e7ea26d502180949bcc22dd5e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Sep 2024 16:42:53 +0800 Subject: [PATCH] feat: add md5 and xxh3 test helper --- src/core_test/CMakeLists.txt | 5 +-- src/core_test/cases/all_cases.cc | 7 ++-- src/core_test/cases/basic_ranges.cc | 6 ++-- src/core_test/cases/ranges_union.cc | 4 +-- src/core_test/core/core.cc | 2 +- src/core_test/helper/hash.h | 44 +++++++++++++++++++++++ src/core_test/helper/internal/hash.cc | 12 +++++++ src/core_test/helper/internal/hash.inl | 25 +++++++++++++ src/core_test/helper/internal/parallel.cc | 5 ++- src/core_test/utility/hash.h | 40 --------------------- 10 files changed, 96 insertions(+), 54 deletions(-) create mode 100644 src/core_test/helper/hash.h create mode 100644 src/core_test/helper/internal/hash.cc create mode 100644 src/core_test/helper/internal/hash.inl delete mode 100644 src/core_test/utility/hash.h diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 6a4293b..d427993 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -4,7 +4,7 @@ project(core-test LANGUAGES CXX) set(CMAKE_CXX_STANDARD 23) set(KLSK_TEST_DEPS klotski_c klotski_core - GTest::gtest_main bs::thread_pool md5sum::md5 xxHash::xxh3) + GTest::gtest_main bs::thread_pool) # ------------------------------------------------------------------------------------ # @@ -14,8 +14,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) add_library(test_helper helper/internal/parallel.cc + helper/internal/hash.cc ) -target_link_libraries(test_helper PRIVATE klotski_core bs::thread_pool) +target_link_libraries(test_helper PRIVATE klotski_core bs::thread_pool md5sum::md5 xxHash::xxh3) # ------------------------------------------------------------------------------------ # diff --git a/src/core_test/cases/all_cases.cc b/src/core_test/cases/all_cases.cc index fbc3e63..ea3003b 100644 --- a/src/core_test/cases/all_cases.cc +++ b/src/core_test/cases/all_cases.cc @@ -1,8 +1,9 @@ #include +#include "helper/hash.h" #include "helper/cases.h" -#include "utility/hash.h" #include "utility/exposer.h" + #include "short_code/short_code.h" #include "common_code/common_code.h" @@ -49,7 +50,7 @@ protected: const auto &all_cases = AllCases::instance().fetch(); for (int head = 0; head < 16; ++head) { EXPECT_EQ(all_cases[head].size(), ALL_CASES_NUM[head]); // verify all cases size - EXPECT_EQ(hash::xxh3(all_cases[head]), ALL_CASES_XXH3[head]); // verify all cases checksum + EXPECT_EQ(helper::xxh3(all_cases[head]), ALL_CASES_XXH3[head]); // verify all cases checksum } } }; @@ -59,7 +60,7 @@ TEST_FF(AllCases, content) { auto &cases = AllCases::instance().fetch()[head]; EXPECT_SORTED_AND_UNIQUE(cases); EXPECT_EQ(cases.size(), ALL_CASES_NUM[head]); // size verify - EXPECT_EQ(hash::xxh3(cases), ALL_CASES_XXH3[head]); // checksum verify + EXPECT_EQ(helper::xxh3(cases), ALL_CASES_XXH3[head]); // checksum verify EXPECT_SUBSET(BasicRanges::instance().fetch(), cases); // subset verify EXPECT_COMMON_CODES(head, cases); // release verify } diff --git a/src/core_test/cases/basic_ranges.cc b/src/core_test/cases/basic_ranges.cc index 96792c9..98875dd 100644 --- a/src/core_test/cases/basic_ranges.cc +++ b/src/core_test/cases/basic_ranges.cc @@ -1,8 +1,8 @@ #include #include "group/group.h" +#include "helper/hash.h" #include "helper/cases.h" -#include "utility/hash.h" #include "utility/exposer.h" using klotski::array_sum; @@ -39,7 +39,7 @@ protected: static void Verify() { const auto &basic_ranges = BasicRanges::instance().fetch(); EXPECT_EQ(basic_ranges.size(), BASIC_RANGES_NUM_); // verify basic ranges size - EXPECT_EQ(hash::xxh3(basic_ranges), BASIC_RANGES_XXH3); // verify basic ranges checksum + EXPECT_EQ(helper::xxh3(basic_ranges), BASIC_RANGES_XXH3); // verify basic ranges checksum } }; @@ -47,7 +47,7 @@ TEST_FF(BasicRanges, content) { auto &ranges = BasicRanges::instance().fetch(); EXPECT_SORTED_AND_UNIQUE(ranges); EXPECT_EQ(ranges.size(), BASIC_RANGES_NUM_); // size verify - EXPECT_EQ(hash::xxh3(ranges), BASIC_RANGES_XXH3); // checksum verify + EXPECT_EQ(helper::xxh3(ranges), BASIC_RANGES_XXH3); // checksum verify } TEST_FF(BasicRanges, constant) { diff --git a/src/core_test/cases/ranges_union.cc b/src/core_test/cases/ranges_union.cc index 1a4f5b9..c65af63 100644 --- a/src/core_test/cases/ranges_union.cc +++ b/src/core_test/cases/ranges_union.cc @@ -1,7 +1,7 @@ #include +#include "helper/hash.h" #include "helper/cases.h" -#include "utility/hash.h" #include "ranges/ranges.h" constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23"; @@ -14,7 +14,7 @@ TEST(RangesUnion, export) { for (auto code : AllCases::instance().fetch().codes()) { buffer += std::format("{:09X}\n", code.unwrap()); } - EXPECT_EQ(hash::md5(buffer), ALL_CASES_MD5); + EXPECT_EQ(helper::md5(buffer), ALL_CASES_MD5); } TEST(RangesUnion, append) { diff --git a/src/core_test/core/core.cc b/src/core_test/core/core.cc index 3c89a94..33b06d8 100644 --- a/src/core_test/core/core.cc +++ b/src/core_test/core/core.cc @@ -10,7 +10,7 @@ #include "all_cases/all_cases.h" #include "common_code/common_code.h" -#include "utility/hash.h" +#include "helper/hash.h" constexpr auto NEXT_CASES_XXH3 = std::to_array({ 0xcd1920b50bc3bda1, 0xd881004a12384988, 0xbdefaaee9508848d, 0x2d06800538d394c2, diff --git a/src/core_test/helper/hash.h b/src/core_test/helper/hash.h new file mode 100644 index 0000000..9942bd4 --- /dev/null +++ b/src/core_test/helper/hash.h @@ -0,0 +1,44 @@ +#pragma once + +/// Provides XXH3 and MD5 hash calculation support. The former is far ahead in +/// speed, but the latter is more of a commemorative significance. + +/// They do not guarantee the security of the hash algorithm, as this scenario +/// is only used to verify the correctness of the data. + +#include +#include + +namespace helper { + +// ----------------------------------------------------------------------------------------- // + +/// Calculate XXH3 hash value as u64. +uint64_t xxh3(const std::string_view &data); + +/// Calculate XXH3 hash value as u64. +uint64_t xxh3(const void *data, uint64_t size); + +/// Calculate MD5 hash value as string. +std::string md5(const std::string_view &data); + +/// Calculate MD5 hash value as string. +std::string md5(const void *data, uint64_t size); + +// ----------------------------------------------------------------------------------------- // + +/// Calculate XXH3 hash value of continuous array. +template +requires std::is_trivial_v +uint64_t xxh3(const std::vector &data); + +/// Calculate MD5 hash value of continuous array. +template +requires std::is_trivial_v +std::string md5(const std::vector &data); + +// ----------------------------------------------------------------------------------------- // + +} // namespace helper + +#include "internal/hash.inl" diff --git a/src/core_test/helper/internal/hash.cc b/src/core_test/helper/internal/hash.cc new file mode 100644 index 0000000..a5b4513 --- /dev/null +++ b/src/core_test/helper/internal/hash.cc @@ -0,0 +1,12 @@ +#include "helper/hash.h" + +#include +#include + +std::string helper::md5(const void *data, const uint64_t size) { + return md5::MD5::Hash(data, size); +} + +uint64_t helper::xxh3(const void *data, const uint64_t size) { + return XXH_INLINE_XXH3_64bits(data, size); +} diff --git a/src/core_test/helper/internal/hash.inl b/src/core_test/helper/internal/hash.inl new file mode 100644 index 0000000..f92d073 --- /dev/null +++ b/src/core_test/helper/internal/hash.inl @@ -0,0 +1,25 @@ +#pragma once + +namespace helper { + +inline uint64_t xxh3(const std::string_view &data) { + return xxh3(data.data(), data.size()); +} + +inline std::string md5(const std::string_view &data) { + return md5(data.data(), data.size()); +} + +template +requires std::is_trivial_v +uint64_t xxh3(const std::vector &data) { + return xxh3(data.data(), data.size() * sizeof(T)); +} + +template +requires std::is_trivial_v +std::string md5(const std::vector &data) { + return md5(data.data(), data.size() * sizeof(T)); +} + +} // namespace helper diff --git a/src/core_test/helper/internal/parallel.cc b/src/core_test/helper/internal/parallel.cc index 0ab5b74..e965a6c 100644 --- a/src/core_test/helper/internal/parallel.cc +++ b/src/core_test/helper/internal/parallel.cc @@ -1,9 +1,8 @@ -#include +#include "helper/parallel.h" -#include +#include #include "group/group.h" -#include "helper/parallel.h" #include "all_cases/all_cases.h" using klotski::cases::AllCases; diff --git a/src/core_test/utility/hash.h b/src/core_test/utility/hash.h deleted file mode 100644 index ca765a1..0000000 --- a/src/core_test/utility/hash.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -/// Provides XXH3 and MD5 hash calculation support. The former is far ahead in -/// speed, but the latter is more of a commemorative significance. - -#include "md5.h" -#include "xxh3.h" - -#include -#include - -namespace hash { - -inline std::string md5(const void *data, const uint64_t size) { - return md5::MD5::Hash(data, size); -} - -inline uint64_t xxh3(const void *data, const uint64_t size) { - return XXH_INLINE_XXH3_64bits(data, size); -} - -inline std::string md5(const std::string_view &data) { - return md5(data.data(), data.size()); -} - -inline uint64_t xxh3(const std::string_view &data) { - return xxh3(data.data(), data.size()); -} - -template -std::string md5(const std::vector &data) { - return md5(data.data(), data.size() * sizeof(T)); -} - -template -uint64_t xxh3(const std::vector &data) { - return xxh3(data.data(), data.size() * sizeof(T)); -} - -} // namespace hash