From 2afaa931892d607d0e0e3a9091f0ce432e360f81 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 1 Jun 2024 16:32:42 +0800 Subject: [PATCH] test: update AllCases test suites --- src/core_test/cases/all_cases.cc | 90 ++++++++++++++------------------ 1 file changed, 38 insertions(+), 52 deletions(-) diff --git a/src/core_test/cases/all_cases.cc b/src/core_test/cases/all_cases.cc index 3a6fab1..e975dbf 100644 --- a/src/core_test/cases/all_cases.cc +++ b/src/core_test/cases/all_cases.cc @@ -1,12 +1,12 @@ #include "hash.h" #include "helper.h" -static constexpr std::array ALL_CASES_XXH3 = { +static constexpr auto ALL_CASES_XXH3 = std::to_array({ 0x71c8ff7a71c93da0, 0x2a5247ee8bfed666, 0xf4efc8fc692d58e2, 0x2d06800538d394c2, 0xb3f7cc1b962d6944, 0x7e2792f8ab777faa, 0x4b8e78026cca8a27, 0x2d06800538d394c2, 0x8acd688c5ab93c42, 0xedca5101ed81cc77, 0xe8dc9d30c91ce682, 0x2d06800538d394c2, 0x2cdf6c14a7ce3e9a, 0xb9dd04a315583f5c, 0x19046e49c44ae90d, 0x2d06800538d394c2, -}; +}); class AllCasesTest : public testing::Test, public Concurrent { protected: @@ -31,13 +31,6 @@ protected: for (int head = 0; head < 16; ++head) { EXPECT_EQ(all_cases[head].size(), ALL_CASES_NUM[head]); // verify all cases size } - - auto all_cases_num = 0; - std::for_each(all_cases.begin(), all_cases.end(), [&all_cases_num](auto &ranges) { - all_cases_num += ranges.size(); - }); - EXPECT_EQ(all_cases_num, ALL_CASES_NUM_); // verify all cases global size - for (int head = 0; head < 16; ++head) { EXPECT_EQ(hash::xxh3(all_cases[head]), ALL_CASES_XXH3[head]); // verify all cases checksum } @@ -45,27 +38,30 @@ protected: }; TEST_FF(AllCases, constant) { + EXPECT_EQ(ALL_CASES_NUM_, 29334498); + EXPECT_EQ(ALL_CASES_NUM[0], 2942906); EXPECT_EQ(ALL_CASES_NUM[1], 2260392); EXPECT_EQ(ALL_CASES_NUM[2], 2942906); - EXPECT_EQ(ALL_CASES_NUM[3], 0); EXPECT_EQ(ALL_CASES_NUM[4], 2322050); EXPECT_EQ(ALL_CASES_NUM[5], 1876945); EXPECT_EQ(ALL_CASES_NUM[6], 2322050); - EXPECT_EQ(ALL_CASES_NUM[7], 0); EXPECT_EQ(ALL_CASES_NUM[8], 2322050); EXPECT_EQ(ALL_CASES_NUM[9], 1876945); EXPECT_EQ(ALL_CASES_NUM[10], 2322050); - EXPECT_EQ(ALL_CASES_NUM[11], 0); EXPECT_EQ(ALL_CASES_NUM[12], 2942906); EXPECT_EQ(ALL_CASES_NUM[13], 2260392); EXPECT_EQ(ALL_CASES_NUM[14], 2942906); + + EXPECT_EQ(ALL_CASES_NUM[3], 0); + EXPECT_EQ(ALL_CASES_NUM[7], 0); + EXPECT_EQ(ALL_CASES_NUM[11], 0); EXPECT_EQ(ALL_CASES_NUM[15], 0); - EXPECT_EQ(ALL_CASES_NUM_, 29334498); + EXPECT_EQ(klotski::array_sum(ALL_CASES_NUM), ALL_CASES_NUM_); } TEST_FF(AllCases, all_cases) { @@ -88,62 +84,52 @@ TEST_FF(AllCases, all_cases_race) { Verify(); } -TEST_FF(AllCases, all_cases_parallel) { - // AllCases::instance().build_parallel(executor_.Entry()); - // EXPECT_TRUE(Available()); - // Verify(); - - // AllCases::instance().build_parallel(executor_.Entry()); - // EXPECT_TRUE(Available()); - // Verify(); -} - -TEST_FF(AllCases, all_cases_parallel_race) { - // racer_.Begin([this] { - // AllCases::instance().build_parallel(executor_.Entry()); - // }); - // EXPECT_FALSE(Available()); - // racer_.Join(); - // EXPECT_TRUE(Available()); - // Verify(); -} - TEST_FF(AllCases, all_cases_async) { - std::atomic_flag flag; - - flag.clear(); - AllCases::instance().build_async(executor_.Entry(), [&flag]() { - flag.test_and_set(); - flag.notify_all(); + condition_.clear(); + AllCases::instance().build_async(executor_.Entry(), [this] { + EXPECT_FALSE(condition_.test_and_set()); + condition_.notify_all(); }); EXPECT_FALSE(Available()); - flag.wait(false); + condition_.wait(false); EXPECT_TRUE(Available()); Verify(); - flag.clear(); - AllCases::instance().build_async(executor_.Entry(), [&flag]() { - flag.test_and_set(); - flag.notify_all(); + condition_.clear(); + AllCases::instance().build_async(executor_.Entry(), [this] { + EXPECT_FALSE(condition_.test_and_set()); + condition_.notify_all(); }); EXPECT_TRUE(Available()); - flag.wait(false); + condition_.wait(false); EXPECT_TRUE(Available()); Verify(); } TEST_FF(AllCases, all_cases_async_race) { - std::atomic callback_num(0); - - racer_.Start([this, &callback_num] { - AllCases::instance().build_async(executor_.Entry(), [&callback_num]() { - callback_num.fetch_add(1); + counter_.store(0); + racer_.Start([this] { + AllCases::instance().build_async(executor_.Entry(), [this] { + counter_.fetch_add(1); }); }); EXPECT_FALSE(Available()); - racer_.Join(); EXPECT_TRUE(Available()); - EXPECT_EQ(callback_num.load(), racer_.RaceNum()); + EXPECT_EQ(counter_.load(), racer_.RaceNum()); + Verify(); +} + +TEST_FF(AllCases, all_cases_async_block) { + condition_.clear(); + serial_.Entry()([this] { + AllCases::instance().build_async(serial_.Entry(), [this] { + EXPECT_FALSE(condition_.test_and_set()); + condition_.notify_all(); + }); + }); + EXPECT_FALSE(Available()); + condition_.wait(false); + EXPECT_TRUE(Available()); Verify(); }