Browse Source

feat: add md5 and xxh3 test helper

master
Dnomd343 2 months ago
parent
commit
1c2d130b25
  1. 5
      src/core_test/CMakeLists.txt
  2. 7
      src/core_test/cases/all_cases.cc
  3. 6
      src/core_test/cases/basic_ranges.cc
  4. 4
      src/core_test/cases/ranges_union.cc
  5. 2
      src/core_test/core/core.cc
  6. 44
      src/core_test/helper/hash.h
  7. 12
      src/core_test/helper/internal/hash.cc
  8. 25
      src/core_test/helper/internal/hash.inl
  9. 5
      src/core_test/helper/internal/parallel.cc
  10. 40
      src/core_test/utility/hash.h

5
src/core_test/CMakeLists.txt

@ -4,7 +4,7 @@ project(core-test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(KLSK_TEST_DEPS klotski_c klotski_core 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 add_library(test_helper
helper/internal/parallel.cc 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)
# ------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------ #

7
src/core_test/cases/all_cases.cc

@ -1,8 +1,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "helper/hash.h"
#include "helper/cases.h" #include "helper/cases.h"
#include "utility/hash.h"
#include "utility/exposer.h" #include "utility/exposer.h"
#include "short_code/short_code.h" #include "short_code/short_code.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"
@ -49,7 +50,7 @@ protected:
const auto &all_cases = AllCases::instance().fetch(); const auto &all_cases = AllCases::instance().fetch();
for (int head = 0; head < 16; ++head) { for (int head = 0; head < 16; ++head) {
EXPECT_EQ(all_cases[head].size(), ALL_CASES_NUM[head]); // verify all cases size 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]; auto &cases = AllCases::instance().fetch()[head];
EXPECT_SORTED_AND_UNIQUE(cases); EXPECT_SORTED_AND_UNIQUE(cases);
EXPECT_EQ(cases.size(), ALL_CASES_NUM[head]); // size verify 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_SUBSET(BasicRanges::instance().fetch(), cases); // subset verify
EXPECT_COMMON_CODES(head, cases); // release verify EXPECT_COMMON_CODES(head, cases); // release verify
} }

6
src/core_test/cases/basic_ranges.cc

@ -1,8 +1,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "group/group.h" #include "group/group.h"
#include "helper/hash.h"
#include "helper/cases.h" #include "helper/cases.h"
#include "utility/hash.h"
#include "utility/exposer.h" #include "utility/exposer.h"
using klotski::array_sum; using klotski::array_sum;
@ -39,7 +39,7 @@ protected:
static void Verify() { static void Verify() {
const auto &basic_ranges = BasicRanges::instance().fetch(); const auto &basic_ranges = BasicRanges::instance().fetch();
EXPECT_EQ(basic_ranges.size(), BASIC_RANGES_NUM_); // verify basic ranges size 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(); auto &ranges = BasicRanges::instance().fetch();
EXPECT_SORTED_AND_UNIQUE(ranges); EXPECT_SORTED_AND_UNIQUE(ranges);
EXPECT_EQ(ranges.size(), BASIC_RANGES_NUM_); // size verify 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) { TEST_FF(BasicRanges, constant) {

4
src/core_test/cases/ranges_union.cc

@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "helper/hash.h"
#include "helper/cases.h" #include "helper/cases.h"
#include "utility/hash.h"
#include "ranges/ranges.h" #include "ranges/ranges.h"
constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23"; constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23";
@ -14,7 +14,7 @@ TEST(RangesUnion, export) {
for (auto code : AllCases::instance().fetch().codes()) { for (auto code : AllCases::instance().fetch().codes()) {
buffer += std::format("{:09X}\n", code.unwrap()); 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) { TEST(RangesUnion, append) {

2
src/core_test/core/core.cc

@ -10,7 +10,7 @@
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"
#include "utility/hash.h" #include "helper/hash.h"
constexpr auto NEXT_CASES_XXH3 = std::to_array<uint64_t>({ constexpr auto NEXT_CASES_XXH3 = std::to_array<uint64_t>({
0xcd1920b50bc3bda1, 0xd881004a12384988, 0xbdefaaee9508848d, 0x2d06800538d394c2, 0xcd1920b50bc3bda1, 0xd881004a12384988, 0xbdefaaee9508848d, 0x2d06800538d394c2,

44
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 <vector>
#include <cstdint>
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 <typename T>
requires std::is_trivial_v<T>
uint64_t xxh3(const std::vector<T> &data);
/// Calculate MD5 hash value of continuous array.
template <typename T>
requires std::is_trivial_v<T>
std::string md5(const std::vector<T> &data);
// ----------------------------------------------------------------------------------------- //
} // namespace helper
#include "internal/hash.inl"

12
src/core_test/helper/internal/hash.cc

@ -0,0 +1,12 @@
#include "helper/hash.h"
#include <md5.h>
#include <xxh3.h>
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);
}

25
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 <typename T>
requires std::is_trivial_v<T>
uint64_t xxh3(const std::vector<T> &data) {
return xxh3(data.data(), data.size() * sizeof(T));
}
template <typename T>
requires std::is_trivial_v<T>
std::string md5(const std::vector<T> &data) {
return md5(data.data(), data.size() * sizeof(T));
}
} // namespace helper

5
src/core_test/helper/internal/parallel.cc

@ -1,9 +1,8 @@
#include <BS_thread_pool.hpp> #include "helper/parallel.h"
#include <iostream> #include <BS_thread_pool.hpp>
#include "group/group.h" #include "group/group.h"
#include "helper/parallel.h"
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
using klotski::cases::AllCases; using klotski::cases::AllCases;

40
src/core_test/utility/hash.h

@ -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 <vector>
#include <cstdint>
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 <typename T>
std::string md5(const std::vector<T> &data) {
return md5(data.data(), data.size() * sizeof(T));
}
template <typename T>
uint64_t xxh3(const std::vector<T> &data) {
return xxh3(data.data(), data.size() * sizeof(T));
}
} // namespace hash
Loading…
Cancel
Save