28 #include <unordered_map>
43 TEST(RandomInsertPiggyTest, Scoping) {
48 TEST(RandomInsertPiggyTest, Insertion) {
62 TEST(RandomInsertPiggyTest, DoubleClear) {
81 TEST(RandomInsertPiggyTest, ParallelInsert) {
82 constexpr
size_t limit = 10000;
86 #pragma omp parallel for
87 for (
size_t i = 0;
i < limit; ++
i) {
92 for (
size_t i = 0;
i < limit; ++
i) {
99 TEST(PiggyTest, Scoping) {
104 TEST(PiggyTest, Append) {
117 constexpr
size_t N = 10000;
119 for (
size_t i = 0;
i < N; ++
i) {
124 for (
size_t i = 3;
i < N + 3; ++
i) {
129 TEST(PiggyTest, ElementCreation) {
137 TEST(PiggyTest, Iteration) {
139 constexpr
size_t N = 10000;
140 for (
size_t i = 0;
i < N; ++
i) {
144 for (
size_t e : pl) {
149 TEST(PiggyTest, CopyCtor) {
151 constexpr
size_t N = 10000;
152 for (
size_t i = 0;
i < N; ++
i) {
160 for (
size_t i = 0;
i < N; ++
i) {
165 auto pl1It = pl.
begin();
166 auto pl2It = pl2.begin();
170 if (pl1It == pl.
end() && pl2It == pl2.end()) {
175 if (pl1It == pl.
end() || pl2It == pl2.end()) {
176 EXPECT_FALSE(
true &&
"whoops, looks like the iterators are broken");
199 TEST(PiggyTest, DoubleClear) {
218 TEST(PiggyTest, ParallelElementSpawning) {
219 constexpr
size_t N = 10000;
222 #pragma omp parallel for
223 for (
size_t i = 0;
i < N; ++
i) {
229 for (
size_t i = 0;
i < N; ++
i) {
234 TEST(PiggyTest, ParallelAppend) {
235 constexpr
size_t N = 10000;
238 #pragma omp parallel for
239 for (
size_t i = 0;
i < N; ++
i) {
244 std::set<size_t> verifier;
245 for (
size_t i = 0;
i < N; ++
i) {
246 verifier.emplace(pl.
get(
i));
251 for (
size_t e : verifier) {
256 #endif // ifdef _OPENMP
259 TEST(DjTest, Scoping) {
263 TEST(DjTest, MakeNode) {
281 TEST(DjTest, TestUnion) {
315 TEST(DjTest, Clear) {
322 for (
size_t i = 0;
i < 10000; ++
i) {
331 TEST(DjTest, ParallelScaling) {
335 constexpr
size_t N = 10000;
337 #pragma omp parallel for
338 for (
size_t i = 0;
i < N; ++
i) {
345 #pragma omp parallel for
346 for (
size_t i = 0;
i < N - 1; ++
i) {
353 for (
size_t i = 0;
i < N; ++
i) {
357 #endif // ifdef _OPENMP
360 TEST(SparseDjTest, Scoping) {
364 TEST(SparseDjTest, MakeNode) {
375 constexpr
size_t N = 10000;
376 for (
size_t i = 0;
i < N; ++
i) {
383 TEST(SparseDjTest, TestUnion) {
403 TEST(SparseDjTest, SignedData) {
413 TEST(SparseDjTest, ParallelDense) {
418 constexpr
size_t N = 1000000;
420 std::vector<size_t> data_source;
421 for (
size_t i = 0;
i < N; ++
i) {
422 data_source.push_back(
i);
424 std::shuffle(data_source.begin(), data_source.end(), std::random_device());
427 #pragma omp parallel for
428 for (
size_t i = 0;
i < N; ++
i) {
429 size_t val = data_source[
i];
432 pl.
append(std::make_pair(a, val));
433 pl.
append(std::make_pair(
b, val));
437 std::unordered_map<size_t, size_t> mapper;
438 for (
size_t i = 0;
i < pl.
size(); ++
i) {
439 size_t sparse = pl.
get(
i).second;
440 size_t dense = pl.
get(
i).first;
442 if (mapper.count(sparse) == 1) {
443 if (mapper[sparse] != dense) {
445 throw std::runtime_error(
446 "invalid state detected, different dense values for same sparse values");
449 mapper.emplace(sparse, dense);
458 TEST(SparseDjTest, ParallelScaling) {
461 constexpr
size_t N = 1000000;
463 #pragma omp parallel for
464 for (
size_t i = 0;
i < N; ++
i) {
471 #pragma omp parallel for
472 for (
size_t i = 0;
i < N - 1; ++
i) {
477 for (
size_t i = 0;
i < N - 1; ++
i) {
483 TEST(SparseDjTest, ParallelTest) {
485 constexpr
size_t N = 1000000;
487 #pragma omp parallel for
488 for (
size_t i = 0;
i < N; ++
i) {
495 #endif // ifdef _OPENMP
497 typedef std::pair<size_t, size_t>
TestPair;
504 TEST(LambdaBTreeTest, Scoping) {
509 TEST(LambdaBTreeTest, Insert) {
514 std::atomic<size_t> assigner(0);
519 std::function<TestPair::second_type(
TestPair&)> update_fn = [&](
TestPair& tp) {
520 tp.second = assigner.fetch_add(1);
545 TestPair alreadyExistsPair = {55, 12313123};
549 "newly inserted function called for an element that already exists!");
559 TEST(LambdaBTreeTest, ParallelInsert) {
564 constexpr
size_t N = 10000;
568 std::atomic<size_t> assigner(0);
569 std::function<TestPair::second_type(
TestPair&)> update_fn = [&](
TestPair& tp) {
570 tp.second = assigner.fetch_add(1);
576 #pragma omp parallel for
577 for (
size_t i = 0;
i < N; ++
i) {
579 t.insert(tp, update_fn);
583 std::set<TestPair::second_type> covering;
589 covering.emplace(
p.second);
596 for (
size_t i = 0;
i < N; ++
i) {
603 size_t num_threads = omp_get_max_threads();
607 const size_t N2 = (N / num_threads) * num_threads;
609 std::atomic<size_t> assigner(0);
610 std::function<TestPair::second_type(
TestPair&)> update_fn = [&](
TestPair& tp) {
611 tp.second = assigner.fetch_add(1);
616 #pragma omp parallel for
617 for (
size_t i = 0;
i < N2; ++
i) {
618 TestPair tp = {
i / num_threads, 213812309};
621 t.insert(tp, update_fn);
634 EXPECT_EQ(assigner.load(), N2 / num_threads);
638 std::vector<bool> verifier(N2 / num_threads,
false);
640 if (verifier.at(
p.second)) {
641 EXPECT_TRUE(
false &&
"duplicate posteriors found within the lambdatree");
644 verifier.at(
p.second) =
true;
648 #endif // ifdef _OPENMP
651 TEST(LambdaBTree, ContendParallel) {
657 std::atomic<size_t> counter{0};
659 constexpr
size_t N = 100;
661 p.second = counter++;
666 std::vector<size_t> data_source;
667 for (
size_t i = 0;
i < N; ++
i) {
668 data_source.push_back(
i);
670 std::shuffle(data_source.begin(), data_source.end(), std::random_device());
672 #pragma omp parallel for
673 for (
size_t i = 0;
i < N; ++
i) {
674 size_t val = data_source[
i];
677 std::pair<size_t, size_t> paira = {val, -1};
678 std::pair<size_t, size_t> pairb = {val, -1};
679 size_t a = tlt.insert(paira, fun);
680 size_t b = tlt.insert(pairb, fun);
683 std::cout <<
"Different values observed for key " << val <<
": " << a <<
" vs. " <<
b <<
"\n";
685 throw std::runtime_error(
"Error detected!");
689 if (N != tlt.size()) {
690 throw std::runtime_error(
"Wrong tree size!");
695 std::cout <<
"number of times the functor has been called: " << counter.load() << std::endl;