|
@ -27,24 +27,24 @@ PRIVATE_ACCESS(BasicRanges, available_, bool) |
|
|
|
|
|
|
|
|
/// Reset basic ranges build state, note it is thread-unsafe.
|
|
|
/// Reset basic ranges build state, note it is thread-unsafe.
|
|
|
void basic_ranges_reset() { |
|
|
void basic_ranges_reset() { |
|
|
access_BasicRanges_available_(BasicRanges::Instance()) = false; |
|
|
access_BasicRanges_available_(BasicRanges::instance()) = false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Reset all cases build state, note it is thread-unsafe.
|
|
|
/// Reset all cases build state, note it is thread-unsafe.
|
|
|
void all_cases_reset() { |
|
|
void all_cases_reset() { |
|
|
access_AllCases_available_(AllCases::Instance()) = false; |
|
|
access_AllCases_available_(AllCases::instance()) = false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Verify that whether basic ranges data is correct.
|
|
|
/// Verify that whether basic ranges data is correct.
|
|
|
void basic_ranges_verify() { |
|
|
void basic_ranges_verify() { |
|
|
auto &basic_ranges = BasicRanges::Instance().Fetch(); |
|
|
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(xxhsum(basic_ranges), BASIC_RANGES_XXHASH); // verify basic ranges checksum
|
|
|
EXPECT_EQ(xxhsum(basic_ranges), BASIC_RANGES_XXHASH); // verify basic ranges checksum
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Verify that whether all cases data is correct.
|
|
|
/// Verify that whether all cases data is correct.
|
|
|
void all_cases_verify() { |
|
|
void all_cases_verify() { |
|
|
auto &all_cases = AllCases::Instance().Fetch(); |
|
|
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
|
|
|
} |
|
|
} |
|
@ -57,18 +57,18 @@ void all_cases_verify() { |
|
|
|
|
|
|
|
|
std::string all_cases_xxh; |
|
|
std::string all_cases_xxh; |
|
|
for (uint64_t head = 0; head < 16; ++head) { |
|
|
for (uint64_t head = 0; head < 16; ++head) { |
|
|
all_cases_xxh += xxhsum(AllCases::Instance().Fetch()[head]); |
|
|
all_cases_xxh += xxhsum(AllCases::instance().fetch()[head]); |
|
|
} |
|
|
} |
|
|
EXPECT_EQ(xxhsum(all_cases_xxh), ALL_CASES_XXHASH); // verify all cases checksum
|
|
|
EXPECT_EQ(xxhsum(all_cases_xxh), ALL_CASES_XXHASH); // verify all cases checksum
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TEST(Cases, basic_ranges) { |
|
|
TEST(Cases, basic_ranges) { |
|
|
basic_ranges_reset(); |
|
|
basic_ranges_reset(); |
|
|
EXPECT_FALSE(BasicRanges::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(BasicRanges::instance().is_available()); |
|
|
BasicRanges::Instance().Build(); |
|
|
BasicRanges::instance().build(); |
|
|
EXPECT_TRUE(BasicRanges::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(BasicRanges::instance().is_available()); |
|
|
BasicRanges::Instance().Build(); |
|
|
BasicRanges::instance().build(); |
|
|
EXPECT_TRUE(BasicRanges::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(BasicRanges::instance().is_available()); |
|
|
basic_ranges_verify(); |
|
|
basic_ranges_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -77,21 +77,21 @@ TEST(Cases, basic_ranges_mutex) { |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
|
|
|
|
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
auto _ = pool.submit(&BasicRanges::Build, &BasicRanges::Instance()); |
|
|
auto _ = pool.submit(&BasicRanges::build, &BasicRanges::instance()); |
|
|
} |
|
|
} |
|
|
EXPECT_FALSE(BasicRanges::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(BasicRanges::instance().is_available()); |
|
|
pool.wait_for_tasks(); |
|
|
pool.wait_for_tasks(); |
|
|
EXPECT_TRUE(BasicRanges::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(BasicRanges::instance().is_available()); |
|
|
basic_ranges_verify(); |
|
|
basic_ranges_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TEST(Cases, all_cases) { |
|
|
TEST(Cases, all_cases) { |
|
|
all_cases_reset(); |
|
|
all_cases_reset(); |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
AllCases::Instance().Build(); |
|
|
AllCases::instance().build(); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
AllCases::Instance().Build(); |
|
|
AllCases::instance().build(); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -100,26 +100,26 @@ TEST(Cases, all_cases_mutex) { |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
|
|
|
|
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
auto _ = pool.submit(&AllCases::Build, &AllCases::Instance()); |
|
|
auto _ = pool.submit(&AllCases::build, &AllCases::instance()); |
|
|
} |
|
|
} |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
pool.wait_for_tasks(); |
|
|
pool.wait_for_tasks(); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TEST(Cases, all_cases_parallel) { |
|
|
TEST(Cases, all_cases_parallel) { |
|
|
all_cases_reset(); |
|
|
all_cases_reset(); |
|
|
BS::thread_pool executor; |
|
|
BS::thread_pool executor; |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
AllCases::Instance().BuildParallel([&executor](auto &&func) { |
|
|
AllCases::instance().build_parallel([&executor](auto &&func) { |
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}); |
|
|
}); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
AllCases::Instance().BuildParallel([&executor](auto &&func) { |
|
|
AllCases::instance().build_parallel([&executor](auto &&func) { |
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}); |
|
|
}); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -129,42 +129,43 @@ TEST(Cases, all_cases_parallel_mutex) { |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
|
|
|
|
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
auto _ = pool.submit(&AllCases::BuildParallel, &AllCases::Instance(), [&executor](auto &&func) { |
|
|
auto _ = pool.submit(&AllCases::build_parallel, &AllCases::instance(), [&executor](auto &&func) { |
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
pool.wait_for_tasks(); |
|
|
pool.wait_for_tasks(); |
|
|
executor.wait_for_tasks(); |
|
|
executor.wait_for_tasks(); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TEST(Cases, all_cases_async) { |
|
|
TEST(Cases, all_cases_async) { |
|
|
all_cases_reset(); |
|
|
all_cases_reset(); |
|
|
|
|
|
std::atomic_flag flag; |
|
|
BS::thread_pool executor; |
|
|
BS::thread_pool executor; |
|
|
|
|
|
|
|
|
std::promise<void> promise_1; |
|
|
flag.clear(); |
|
|
auto future_1 = promise_1.get_future(); |
|
|
AllCases::instance().build_parallel_async([&executor](auto &&func) { |
|
|
AllCases::Instance().BuildParallelAsync([&executor](auto &&func) { |
|
|
|
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}, [&promise_1]() { |
|
|
}, [&flag]() { // callback function
|
|
|
promise_1.set_value(); |
|
|
flag.test_and_set(); |
|
|
|
|
|
flag.notify_all(); |
|
|
}); |
|
|
}); |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
future_1.wait(); |
|
|
flag.wait(false); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
|
|
|
|
|
|
std::promise<void> promise_2; |
|
|
flag.clear(); |
|
|
auto future_2 = promise_2.get_future(); |
|
|
AllCases::instance().build_parallel_async([&executor](auto &&func) { |
|
|
AllCases::Instance().BuildParallelAsync([&executor](auto &&func) { |
|
|
|
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}, [&promise_2]() { |
|
|
}, [&flag]() { // callback function
|
|
|
promise_2.set_value(); |
|
|
flag.test_and_set(); |
|
|
|
|
|
flag.notify_all(); |
|
|
}); |
|
|
}); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
future_2.wait(); |
|
|
flag.wait(false); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -175,16 +176,16 @@ TEST(Cases, all_cases_async_mutex) { |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
BS::thread_pool pool(TEST_THREAD_NUM); |
|
|
|
|
|
|
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
for (int i = 0; i < TEST_THREAD_NUM; ++i) { |
|
|
auto _ = pool.submit(&AllCases::BuildParallelAsync, &AllCases::Instance(), [&executor](auto &&func) { |
|
|
auto _ = pool.submit(&AllCases::build_parallel_async, &AllCases::instance(), [&executor](auto &&func) { |
|
|
executor.push_task(func); |
|
|
executor.push_task(func); |
|
|
}, [&callback_num]() { |
|
|
}, [&callback_num]() { |
|
|
callback_num.fetch_add(1); |
|
|
callback_num.fetch_add(1); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
EXPECT_FALSE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_FALSE(AllCases::instance().is_available()); |
|
|
pool.wait_for_tasks(); |
|
|
pool.wait_for_tasks(); |
|
|
executor.wait_for_tasks(); |
|
|
executor.wait_for_tasks(); |
|
|
EXPECT_TRUE(AllCases::Instance().IsAvailable()); |
|
|
EXPECT_TRUE(AllCases::instance().is_available()); |
|
|
EXPECT_EQ(callback_num.load(), TEST_THREAD_NUM); |
|
|
EXPECT_EQ(callback_num.load(), TEST_THREAD_NUM); |
|
|
all_cases_verify(); |
|
|
all_cases_verify(); |
|
|
} |
|
|
} |
|
|