Skip to content

Commit

Permalink
Enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GabTux committed Mar 21, 2024
1 parent 05421cd commit cd96637
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
36 changes: 36 additions & 0 deletions test/source/ppqsort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ TEST(Patterns, HalfSorted) {
}
}

TEST(Patterns, Adversary) {
std::size_t sizes[] = {128, static_cast<std::size_t>(1e7)};
for (const auto & Size: sizes) {
std::vector<int> data;
data.resize(Size);
int candidate = 0;
int nsolid = 0;
const int gas = Size - 1;

// initially, all values are gas
std::ranges::fill(data, gas);

// fill with values from 0 to Size-1
// will be used as indices to this->data
std::vector<int> asc_vals(Size);
std::iota(asc_vals.begin(), asc_vals.end(), 0);

auto cmp = [&](int x, int y) {
if (data[x] == gas && data[y] == gas)
{
if (x == candidate)
data[x] = nsolid++;
else
data[y] = nsolid++;
}
if (data[x] == gas)
candidate = x;
else if (data[y] == gas)
candidate = y;
return data[x] < data[y];
};
ppqsort::sort(ppqsort::execution::par, asc_vals.begin(), asc_vals.end(), cmp);
ppqsort::sort(ppqsort::execution::par, data.begin(), data.end());
}
}


// Random to test general cases, different types and ranges
TYPED_TEST_SUITE_P(RandomVectorFixture);
Expand Down
22 changes: 21 additions & 1 deletion test/source/thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,30 @@ TEST(ThreadPool, TryPushConcurrent) {
ASSERT_EQ(counter, 1000);
}


TEST(ThreadPool, OneThread) {
using namespace ppqsort::impl::cpp;
ThreadPool pool(1);
std::atomic<int> counter = 0;

// Tasks to push values
auto task = [&]() { pool.push_task([&]() {counter++;}); };

// Push tasks concurrently in loop
for (int i = 0; i < 1000; ++i) {
std::ignore = std::async(std::launch::async, task);
}

pool.wait_and_stop();
ASSERT_EQ(counter, 1000);
}




TEST(ThreadPool, StopEmpty) {
using namespace ppqsort::impl::cpp;
ThreadPool pool;
pool.wait_and_stop();
}

TEST(ThreadPool, StopDuringPush) {
Expand Down

0 comments on commit cd96637

Please sign in to comment.