#include #include #include #include #include #include using Void::Concurrency::ThreadPool; Void::Testing::Test thread_count_matches_requested() { ThreadPool pool{4}; Void::Testing::expect_eq(pool.thread_count(), std::size_t{4}); } Void::Testing::Test defaults_to_hardware_concurrency() { ThreadPool pool; Void::Testing::expect(pool.thread_count() >= 1); } Void::Testing::Test submit_returns_correct_value() { ThreadPool pool{2}; auto fut = pool.submit([] { return 42; }); Void::Testing::expect_eq(fut.get(), 42); } Void::Testing::Test tasks_run_concurrently() { constexpr int N = 8; ThreadPool pool{N}; std::atomic counter{0}; std::vector> futs; futs.reserve(N); for (int i = 0; i < N; ++i) { futs.push_back(pool.submit([&] { ++counter; std::this_thread::sleep_for(std::chrono::milliseconds{5}); })); } for (auto& f : futs) { f.get(); } Void::Testing::expect_eq(counter.load(), N); } Void::Testing::Test parallel_sum_is_correct() { ThreadPool pool{4}; constexpr int N = 100; std::atomic total{0}; std::vector> futs; futs.reserve(N); for (int i = 1; i <= N; ++i) { futs.push_back(pool.submit([&, i] { total += i; })); } for (auto& f : futs) { f.get(); } Void::Testing::expect_eq(total.load(), 5050LL); } Void::Testing::Test exception_propagates_through_future() { ThreadPool pool{1}; auto fut = pool.submit([] -> int { throw std::runtime_error("boom"); }); bool caught = false; try { fut.get(); } catch (const std::runtime_error& e) { caught = (std::string{e.what()} == "boom"); } Void::Testing::expect(caught); } Void::Testing::Test submit_after_destruction_throws() { std::optional> fut; { ThreadPool pool{1}; fut = pool.submit([] { return 1; }); fut->get(); } // pool is now destroyed; this just verifies destruction was clean Void::Testing::expect(true); }