Browse Source

update: using xxh3 algorithm

master
Dnomd343 8 months ago
parent
commit
ee27786cb3
  1. 2
      src/core_test/CMakeLists.txt
  2. 25
      src/core_test/cases/all_cases.cc
  3. 6
      src/core_test/ffi/all_cases.cc
  4. 0
      src/core_test/utility/exposer.h
  5. 40
      src/core_test/utility/hash.h
  6. 27
      src/core_test/utils/xxhsum.h

2
src/core_test/CMakeLists.txt

@ -8,7 +8,7 @@ set(KLOTSKI_TEST_DEPS klotski_core
# ------------------------------------------------------------------------------------ #
include_directories(utils)
include_directories(utility)
include_directories(${KLOTSKI_ROOT_DIR}/src/core/ffi)

25
src/core_test/cases/all_cases.cc

@ -1,14 +1,11 @@
#include <string>
#include <vector>
#include "xxhsum.h"
#include "hash.h"
#include "exposer.h"
#include "all_cases.h"
#include "gtest/gtest.h"
#include "BS_thread_pool.hpp"
using xxhash::xxhsum;
using klotski::cases::AllCases;
using klotski::cases::BasicRanges;
@ -16,10 +13,16 @@ using klotski::cases::ALL_CASES_NUM;
using klotski::cases::ALL_CASES_NUM_;
using klotski::cases::BASIC_RANGES_NUM;
static const auto TEST_THREAD_NUM = 256;
static constexpr int TEST_THREAD_NUM = 256;
static constexpr uint64_t BASIC_RANGES_XXH3 = 0x82b040060044e336;
static const std::string ALL_CASES_XXHASH = "d589c8a45983ebb6";
static const std::string BASIC_RANGES_XXHASH = "5e7f633b7bd8af37";
static constexpr std::array<uint64_t, 16> ALL_CASES_XXH3 = {
0x71c8ff7a71c93da0, 0x2a5247ee8bfed666, 0xf4efc8fc692d58e2, 0x2d06800538d394c2,
0xb3f7cc1b962d6944, 0x7e2792f8ab777faa, 0x4b8e78026cca8a27, 0x2d06800538d394c2,
0x8acd688c5ab93c42, 0xedca5101ed81cc77, 0xe8dc9d30c91ce682, 0x2d06800538d394c2,
0x2cdf6c14a7ce3e9a, 0xb9dd04a315583f5c, 0x19046e49c44ae90d, 0x2d06800538d394c2,
};
/// Forcibly modify private variables to reset state.
PRIVATE_ACCESS(AllCases, available_, bool)
@ -39,12 +42,12 @@ void all_cases_reset() {
void basic_ranges_verify() {
auto &basic_ranges = BasicRanges::instance().fetch();
EXPECT_EQ(basic_ranges.size(), BASIC_RANGES_NUM); // verify basic ranges size
EXPECT_EQ(xxhsum(basic_ranges), BASIC_RANGES_XXHASH); // verify basic ranges checksum
EXPECT_EQ(hash::xxh3(basic_ranges), BASIC_RANGES_XXH3); // verify basic ranges checksum
}
/// Verify that whether all cases data is correct.
void all_cases_verify() {
auto &all_cases = AllCases::instance().fetch();
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
}
@ -55,11 +58,9 @@ void all_cases_verify() {
});
EXPECT_EQ(all_cases_num, ALL_CASES_NUM_); // verify all cases global size
std::string all_cases_xxh;
for (uint64_t head = 0; head < 16; ++head) {
all_cases_xxh += xxhsum(AllCases::instance().fetch()[head]);
EXPECT_EQ(hash::xxh3(all_cases[head]), ALL_CASES_XXH3[head]); // verify all cases checksum
}
EXPECT_EQ(xxhsum(all_cases_xxh), ALL_CASES_XXHASH); // verify all cases checksum
}
std::unique_ptr<BS::thread_pool> race_test(int parallel, const std::function<void()> &item) {

6
src/core_test/ffi/all_cases.cc

@ -2,14 +2,12 @@
#include <string>
#include <thread>
#include "md5.h"
#include "hash.h"
#include "exposer.h"
#include "klotski.h"
#include "all_cases.h"
#include "gtest/gtest.h"
using md5::MD5;
using klotski::cases::AllCases;
using klotski::cases::BasicRanges;
@ -179,5 +177,5 @@ TEST(AllCases, all_cases_export) {
all_cases_str += tmp;
}
}
EXPECT_EQ(MD5::Hash(all_cases_str), ALL_CASES_MD5);
EXPECT_EQ(hash::md5(all_cases_str), ALL_CASES_MD5);
}

0
src/core_test/utils/exposer.h → src/core_test/utility/exposer.h

40
src/core_test/utility/hash.h

@ -0,0 +1,40 @@
#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>
inline std::string md5(const std::vector<T> &data) {
return md5(data.data(), data.size() * sizeof(T));
}
template <typename T>
inline uint64_t xxh3(const std::vector<T> &data) {
return xxh3(data.data(), data.size() * sizeof(T));
}
} // namespace hash

27
src/core_test/utils/xxhsum.h

@ -1,27 +0,0 @@
#pragma once
#include <string>
#include "xxhash.h"
namespace xxhash {
std::string xxhsum(const void *data, size_t size) {
char *hash;
auto state = XXH64_createState();
XXH64_reset(state, 0);
XXH64_update(state, data, size);
asprintf(&hash, "%016llx", XXH64_digest(state));
XXH64_freeState(state);
return hash;
}
std::string xxhsum(const std::string &data) {
return xxhsum(data.c_str(), data.length());
}
template <typename T>
std::string xxhsum(const std::vector<T> &data) {
return xxhsum(data.data(), data.size() * sizeof(T));
}
} // namespace xxhsum
Loading…
Cancel
Save