You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libshit/test/shared_ptr.cpp

55 lines
1.6 KiB
C++

#include "libshit/shared_ptr.hpp"
#include "libshit/doctest.hpp"
#include "libshit/random.hpp"
#include <array>
#include <thread>
#include <vector>
namespace Libshit::Test
{
TEST_SUITE_BEGIN("Libshit::SharedPtr");
namespace
{
struct Foo : RefCounted {};
}
// Skip this test on valgrind on CI. First, valgrind is slow a shit to begin
// with then there is the problem that valgrind forces execution onto a single
// CPU while thread_concurrency still reports the real CPU count... so the
// more cores you have, the slower this test becomes. For example, on my
// 4-core + hypertreading (=8 threads) CPU, to run the unoptimized
// libshit-tests takes 2 seconds, with valgrind it takes 7.5 minutes.
TEST_CASE_TEMPLATE("multithread[no_valgrind]", T, Foo, int)
{
std::array<Libshit::SmartPtr<T>, 10> ptrs;
for (auto& p : ptrs) p = Libshit::MakeSmart<T>();
static constexpr const unsigned COUNT = 1000000;
auto fun = [&]()
{
std::array<Libshit::SmartPtr<T>, 10> strong;
std::array<Libshit::WeakSmartPtr<T>, 10> weak;
Xoshiro128p rnd;
for (unsigned i = 0; i < COUNT; ++i)
if (rnd.Gen<bool>())
strong[rnd.Gen<unsigned, 10>()] = ptrs[rnd.Gen<unsigned, 10>()];
else
weak[rnd.Gen<unsigned, 10>()] = ptrs[rnd.Gen<unsigned, 10>()];
};
std::vector<std::thread> thrs;
for (unsigned i = 0, n = std::thread::hardware_concurrency(); i < n; ++i)
thrs.emplace_back(fun);
for (auto& t : thrs) t.join();
for (auto& p : ptrs)
CHECK(p.use_count() == 1);
}
TEST_SUITE_END();
}