38# include <gtest/gtest.h>
53# include <type_traits>
55# if defined(__unix__) || defined(__APPLE__)
80 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
85 for (
int i = 10; i >= 1; --i)
97 struct Throwing_Copy_Less
99 Throwing_Copy_Less() noexcept = default;
100 Throwing_Copy_Less(const Throwing_Copy_Less &) noexcept(false) {}
101 Throwing_Copy_Less(Throwing_Copy_Less &&) noexcept(false) {}
102 Throwing_Copy_Less & operator = (
const Throwing_Copy_Less &)
noexcept(
false)
106 Throwing_Copy_Less & operator = (Throwing_Copy_Less &&) noexcept(false)
111 bool operator()(
const int a,
const int b)
const noexcept {
return a < b; }
119static_assert(std::is_nothrow_default_constructible_v<Fibonacci_Heap<int>>);
120static_assert(std::is_nothrow_move_constructible_v<Fibonacci_Heap<int>>);
121static_assert(std::is_nothrow_move_assignable_v<Fibonacci_Heap<int>>);
122static_assert(std::is_nothrow_swappable_v<Fibonacci_Heap<int>>);
123static_assert(not std::is_nothrow_default_constructible_v<Fragile_Compare_Heap>);
124static_assert(not
noexcept(Fragile_Compare_Heap(std::in_place,
125 std::declval<const Throwing_Copy_Less &>())));
126static_assert(
noexcept(Fragile_Compare_Heap(std::in_place)));
127static_assert(not std::is_nothrow_move_constructible_v<Fragile_Compare_Heap>);
128static_assert(not std::is_nothrow_move_assignable_v<Fragile_Compare_Heap>);
129static_assert(not std::is_nothrow_swappable_v<Fragile_Compare_Heap>);
131TEST(FibonacciHeapConstruction, DefaultConstructor)
134 EXPECT_TRUE(
h.is_empty());
135 EXPECT_EQ(
h.size(), 0u);
136 EXPECT_EQ(
h.get_min_node(),
nullptr);
139TEST(FibonacciHeapConstruction, ConstructorWithComparator)
145 EXPECT_EQ(max_heap.
get_min(), 10);
148TEST(FibonacciHeapConstruction, MoveConstructor)
158 EXPECT_EQ(h1.
size(), 0u);
161 EXPECT_EQ(h2.
size(), 3u);
165TEST(FibonacciHeapConstruction, MoveAssignment)
177 EXPECT_EQ(h2.
size(), 2u);
181TEST(FibonacciHeapConstruction, MoveAssignmentRoundtrip)
187 const auto original_size =
h.size();
191 EXPECT_EQ(
h.size(), original_size);
192 EXPECT_EQ(
h.get_min(), 3);
198 struct Armed_Move_Less
200 static inline bool armed =
false;
202 Armed_Move_Less() noexcept = default;
203 Armed_Move_Less(const Armed_Move_Less &) noexcept = default;
204 Armed_Move_Less & operator = (const Armed_Move_Less &) noexcept = default;
205 Armed_Move_Less & operator = (Armed_Move_Less &&) noexcept(false)
208 throw std::runtime_error(
"Armed_Move_Less");
212 bool operator()(
const int a,
const int b)
const noexcept {
return a < b; }
217 struct Own_Swap_Order
219 bool descending =
false;
221 explicit Own_Swap_Order(
const bool d =
false) noexcept : descending(d) {}
222 Own_Swap_Order(
const Own_Swap_Order &)
noexcept =
default;
223 Own_Swap_Order(Own_Swap_Order &&) noexcept(false)
225 throw std::runtime_error(
"Own_Swap_Order");
227 Own_Swap_Order & operator = (
const Own_Swap_Order &)
noexcept =
default;
228 Own_Swap_Order & operator = (Own_Swap_Order &&) noexcept(false)
230 throw std::runtime_error(
"Own_Swap_Order");
233 friend void swap(Own_Swap_Order & a, Own_Swap_Order & b)
noexcept
235 std::swap(a.descending, b.descending);
238 bool operator()(
const int a,
const int b)
const noexcept
240 return descending ? b < a : a < b;
248TEST(FibonacciHeapConstruction, MoveAssignmentWhoseComparisonThrows)
252 for (
int i = 0; i < 5; ++i)
255 Armed_Move_Less::armed =
true;
256 EXPECT_THROW(to = std::move(from), std::runtime_error);
257 Armed_Move_Less::armed =
false;
260 ASSERT_EQ(from.
size(), 5u);
261 for (
int i = 0; i < 5; ++i)
268static_assert(std::is_nothrow_swappable_v<Fibonacci_Heap<int, Own_Swap_Order>>);
270TEST(FibonacciHeapConstruction, SwapUsesTheSwapOfTheComparison)
274 for (
int i = 0; i < 4; ++i)
280 EXPECT_NO_THROW(up.
swap(down));
286 EXPECT_NO_THROW(
swap(up, down));
297 auto node = heap.insert(42);
299 EXPECT_FALSE(heap.is_empty());
300 EXPECT_EQ(heap.size(), 1u);
301 EXPECT_EQ(heap.get_min(), 42);
302 EXPECT_EQ(node->data, 42);
313 EXPECT_EQ(heap.size(), 5u);
314 EXPECT_EQ(heap.get_min(), 3);
319 for (
int i = 100; i >= 1; --i)
322 EXPECT_EQ(heap.size(), 100u);
323 EXPECT_EQ(heap.get_min(), 1);
328 for (
int i = 1; i <= 100; ++i)
331 EXPECT_EQ(heap.size(), 100u);
332 EXPECT_EQ(heap.get_min(), 1);
337 for (
int i = 0; i < 10; ++i)
340 EXPECT_EQ(heap.size(), 10u);
341 EXPECT_EQ(heap.get_min(), 42);
344 for (
int i = 0; i < 10; ++i)
345 EXPECT_EQ(heap.extract_min(), 42);
347 EXPECT_TRUE(heap.is_empty());
352 std::string s =
"hello world";
355 auto node = string_heap.
insert(std::move(s));
357 EXPECT_EQ(node->data,
"hello world");
358 EXPECT_TRUE(s.empty() || s !=
"hello world");
365 auto node = pair_heap.
emplace(42,
"answer");
367 EXPECT_EQ(node->data.first, 42);
368 EXPECT_EQ(node->data.second,
"answer");
377 EXPECT_THROW(heap.get_min(), std::underflow_error);
382 EXPECT_EQ(heap.get_min_node(),
nullptr);
388 EXPECT_EQ(heap.get_min(), 50);
391 EXPECT_EQ(heap.get_min(), 30);
394 EXPECT_EQ(heap.get_min(), 30);
397 EXPECT_EQ(heap.get_min(), 10);
400 EXPECT_EQ(heap.get_min(), 10);
409 EXPECT_THROW(heap.extract_min(), std::underflow_error);
415 int val = heap.extract_min();
418 EXPECT_TRUE(heap.is_empty());
427 EXPECT_EQ(heap.extract_min(), 10);
428 EXPECT_EQ(heap.extract_min(), 20);
429 EXPECT_EQ(heap.extract_min(), 30);
430 EXPECT_TRUE(heap.is_empty());
435 std::vector<int> input = {50, 20, 80, 10, 90, 30, 70, 40, 60, 100};
439 std::vector<int> extracted;
440 while (!heap.is_empty())
441 extracted.push_back(heap.extract_min());
451 std::vector<int> input = {5, 3, 5, 1, 3, 5, 1, 3};
455 std::vector<int> extracted;
456 while (!heap.is_empty())
457 extracted.push_back(heap.extract_min());
459 std::sort(input.begin(), input.end());
460 EXPECT_EQ(extracted, input);
471 heap.decrease_key(
nodes[0], 5);
473 EXPECT_EQ(heap.get_min(), 5);
474 EXPECT_EQ(
nodes[0]->data, 5);
480 heap.decrease_key(
nodes[1], 50);
482 EXPECT_EQ(heap.get_min(), 10);
483 EXPECT_EQ(
nodes[1]->data, 50);
489 heap.decrease_key(
nodes[0], 100);
490 EXPECT_EQ(
nodes[0]->data, 100);
495 auto node = heap.insert(50);
496 EXPECT_THROW(heap.decrease_key(node, 100), std::domain_error);
501 EXPECT_THROW(heap.decrease_key(
nullptr, 10), std::invalid_argument);
507 for (
int i = 1; i <= 20; ++i)
511 for (
int i = 0; i < 5; ++i)
515 auto node = heap.insert(100);
516 heap.decrease_key(node, 1);
518 EXPECT_EQ(heap.get_min(), 1);
524 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
525 for (
int i = 1; i <= 100; ++i)
526 nodes.push_back(heap.insert(i * 10));
529 for (
int i = 0; i < 20; ++i)
534 for (
size_t i = 50; i < 60 && i <
nodes.size(); ++i)
536 if (
nodes[i]->data > 5)
537 heap.decrease_key(
nodes[i],
static_cast<int>(i));
541 std::vector<int> extracted;
542 while (!heap.is_empty())
543 extracted.push_back(heap.extract_min());
545 for (
size_t i = 1; i < extracted.size(); ++i)
546 EXPECT_LE(extracted[i - 1], extracted[i]);
552 auto node = string_heap.
insert(
"zzzzz");
554 std::string new_val =
"aaaaa";
557 EXPECT_EQ(node->data,
"aaaaa");
566 auto node = heap.insert(50);
567 auto result = heap.update_key(node, 30);
569 EXPECT_EQ(result, node);
570 EXPECT_EQ(node->data, 30);
576 auto node = heap.insert(50);
579 auto result = heap.update_key(node, 80);
584 EXPECT_EQ(result->data, 80);
585 EXPECT_EQ(heap.size(), 3u);
588 EXPECT_EQ(heap.extract_min(), 30);
589 EXPECT_EQ(heap.extract_min(), 70);
590 EXPECT_EQ(heap.extract_min(), 80);
595 auto node = heap.insert(50);
596 auto result = heap.update_key(node, 50);
598 EXPECT_EQ(result, node);
599 EXPECT_EQ(node->data, 50);
604 EXPECT_THROW(heap.update_key(
nullptr, 10), std::invalid_argument);
613 auto node = heap.insert(42);
614 heap.delete_node(node);
616 EXPECT_TRUE(heap.is_empty());
622 auto min_node = heap.insert(10);
625 heap.delete_node(min_node);
627 EXPECT_EQ(heap.size(), 2u);
628 EXPECT_EQ(heap.get_min(), 20);
634 auto middle = heap.insert(20);
637 heap.delete_node(middle);
639 EXPECT_EQ(heap.size(), 2u);
640 EXPECT_EQ(heap.get_min(), 10);
641 EXPECT_EQ(heap.extract_min(), 10);
642 EXPECT_EQ(heap.extract_min(), 30);
647 EXPECT_THROW(heap.delete_node(
nullptr), std::invalid_argument);
653 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
654 for (
int i = 1; i <= 50; ++i)
655 nodes.push_back(heap.insert(i));
658 for (
int i = 0; i < 10; ++i)
662 heap.delete_node(
nodes[30]);
663 heap.delete_node(
nodes[40]);
664 heap.delete_node(
nodes[25]);
667 std::vector<int> extracted;
668 while (!heap.is_empty())
669 extracted.push_back(heap.extract_min());
672 EXPECT_EQ(extracted.size(), 37u);
675 for (
size_t i = 1; i < extracted.size(); ++i)
676 EXPECT_LE(extracted[i - 1], extracted[i]);
684 (void)heap.insert(10);
685 (void)heap.insert(5);
686 (void)heap.insert(20);
687 (void)heap.insert(15);
688 (void)heap.insert(3);
692 EXPECT_EQ(heap.extract_min(), 3);
695 EXPECT_EQ(heap.extract_min(), 5);
701 (void)heap.insert(1);
704 EXPECT_EQ(heap.extract_min(), 1);
708 auto min_node = heap.get_min_node();
709 heap.delete_node(min_node);
712 EXPECT_EQ(heap.size(), 2u);
715 int new_min = heap.get_min();
716 EXPECT_TRUE(new_min == 15 || new_min == 20);
719 int first = heap.extract_min();
720 int second = heap.extract_min();
721 EXPECT_LT(first, second);
722 EXPECT_TRUE(heap.is_empty());
736 EXPECT_EQ(heap.extract_min(), 10);
737 EXPECT_EQ(heap.extract_min(), 25);
741 EXPECT_EQ(heap.extract_min(), 50);
744 EXPECT_EQ(heap.size(), 2u);
747 heap.delete_node(heap.get_min_node());
750 EXPECT_EQ(heap.size(), 1u);
751 EXPECT_EQ(heap.get_min(), 200);
752 EXPECT_EQ(heap.extract_min(), 200);
753 EXPECT_TRUE(heap.is_empty());
758 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
759 for (
int i = 1; i <= 20; ++i)
760 nodes.push_back(heap.insert(i));
763 std::vector<size_t> indices(20);
764 std::iota(indices.begin(), indices.end(), 0);
765 std::random_device rd;
766 std::mt19937 g(rd());
767 std::shuffle(indices.begin(), indices.end(), g);
769 for (
size_t idx : indices)
771 heap.delete_node(
nodes[idx]);
774 EXPECT_TRUE(heap.is_empty());
798 EXPECT_EQ(h1.
size(), 2u);
811 EXPECT_EQ(h1.
size(), 2u);
829 EXPECT_EQ(h1.
size(), 6u);
834 std::vector<int> extracted;
838 std::vector<int>
expected = {3, 5, 8, 10, 12, 15};
850 h1.
merge(std::move(h2));
852 EXPECT_EQ(h1.
size(), 2u);
860 for (
int i = 0; i < 1000; i += 2)
863 for (
int i = 1; i < 1000; i += 2)
868 EXPECT_EQ(h1.
size(), 1000u);
870 std::vector<int> extracted;
874 for (
int i = 0; i < 1000; ++i)
875 EXPECT_EQ(extracted[i], i);
895 EXPECT_EQ(h1.
size(), 3u);
898 EXPECT_EQ(h2.
size(), 2u);
910 EXPECT_EQ(h2.
size(), 1u);
933 EXPECT_TRUE(heap.is_empty());
938 for (
int i = 0; i < 100; ++i)
943 EXPECT_TRUE(heap.is_empty());
944 EXPECT_EQ(heap.size(), 0u);
956 EXPECT_EQ(heap.size(), 2u);
957 EXPECT_EQ(heap.get_min(), 15);
964TEST(FibonacciHeapTypeAliases, ValueType)
966 static_assert(std::is_same_v<Fibonacci_Heap<int>::value_type,
int>);
967 static_assert(std::is_same_v<Fibonacci_Heap<std::string>::value_type, std::string>);
970TEST(FibonacciHeapTypeAliases, HandleType)
979TEST(FibonacciMaxHeap, BasicOperations)
987 EXPECT_EQ(max_heap.
get_min(), 30);
993TEST(FibonacciMaxHeap, DecreaseKey)
997 auto n1 = max_heap.
insert(10);
1004 EXPECT_EQ(max_heap.
get_min(), 50);
1018 return distance() < other.distance();
1022TEST(FibonacciHeapCustomType, PointHeap)
1026 point_heap.
insert({3.0, 4.0});
1027 point_heap.
insert({1.0, 1.0});
1028 point_heap.
insert({2.0, 2.0});
1030 EXPECT_DOUBLE_EQ(point_heap.
get_min().distance(), 2.0);
1031 EXPECT_DOUBLE_EQ(point_heap.
extract_min().distance(), 2.0);
1032 EXPECT_DOUBLE_EQ(point_heap.
extract_min().distance(), 8.0);
1033 EXPECT_DOUBLE_EQ(point_heap.
extract_min().distance(), 25.0);
1036TEST(FibonacciHeapCustomType, PairHeap)
1041 pair_heap.
insert({3,
"three"});
1042 pair_heap.
insert({1,
"one"});
1043 pair_heap.
insert({2,
"two"});
1047 EXPECT_EQ(pair_heap.
extract_min().second,
"three");
1054TEST(FibonacciHeapStress, LargeNumberOfInserts)
1057 constexpr int N = 100000;
1059 for (
int i =
N; i >= 1; --i)
1062 EXPECT_EQ(heap.
size(),
static_cast<size_t>(
N));
1066TEST(FibonacciHeapStress, LargeNumberOfExtractMin)
1069 constexpr int N = 10000;
1071 for (
int i =
N; i >= 1; --i)
1074 for (
int i = 1; i <=
N; ++i)
1080TEST(FibonacciHeapStress, InterleavedOperations)
1083 std::multiset<int> reference;
1084 std::random_device rd;
1085 std::mt19937 gen(42);
1086 std::uniform_int_distribution<> dis(1, 10000);
1088 constexpr int N = 10000;
1090 for (
int i = 0; i <
N; ++i)
1092 int op = dis(gen) % 3;
1094 if (op == 0 || reference.empty())
1099 reference.insert(val);
1105 int ref_min = *reference.begin();
1106 reference.erase(reference.begin());
1107 EXPECT_EQ(heap_min, ref_min);
1112 EXPECT_EQ(heap.
get_min(), *reference.begin());
1120 int ref_min = *reference.begin();
1121 reference.erase(reference.begin());
1122 EXPECT_EQ(heap_min, ref_min);
1126TEST(FibonacciHeapStress, ManyDecreaseKeys)
1129 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1130 constexpr int N = 5000;
1132 for (
int i = 0; i <
N; ++i)
1136 for (
int i = 0; i <
N / 4; ++i)
1141 for (
size_t i =
N / 4; i <
N; ++i)
1155 EXPECT_LE(prev, curr);
1160TEST(FibonacciHeapStress, ManyDeleteNodes)
1163 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1164 constexpr int N = 1000;
1166 for (
int i = 0; i <
N; ++i)
1170 for (
int i = 0; i <
N; i += 2)
1173 EXPECT_EQ(heap.
size(),
static_cast<size_t>(
N / 2));
1176 std::vector<int> extracted;
1180 EXPECT_EQ(extracted.size(),
static_cast<size_t>(
N / 2));
1181 for (
size_t i = 0; i < extracted.size(); ++i)
1182 EXPECT_EQ(extracted[i],
static_cast<int>(2 * i + 1));
1185TEST(FibonacciHeapStress, ManyMerges)
1187 std::vector<Fibonacci_Heap<int>> heaps(100);
1190 for (
int i = 0; i < 100; ++i)
1191 for (
int j = 0; j < 100; ++j)
1192 heaps[i].insert(i * 100 + j);
1195 for (
int i = 1; i < 100; ++i)
1196 heaps[0].
merge(heaps[i]);
1198 EXPECT_EQ(heaps[0].
size(), 10000u);
1201 int prev = heaps[0].extract_min();
1202 while (!heaps[0].is_empty())
1204 int curr = heaps[0].extract_min();
1205 EXPECT_LE(prev, curr);
1214TEST(FibonacciHeapEdgeCases, NegativeNumbers)
1231TEST(FibonacciHeapEdgeCases, IntMinMax)
1235 heap.
insert(std::numeric_limits<int>::max());
1237 heap.
insert(std::numeric_limits<int>::min());
1239 EXPECT_EQ(heap.
extract_min(), std::numeric_limits<int>::min());
1241 EXPECT_EQ(heap.
extract_min(), std::numeric_limits<int>::max());
1244TEST(FibonacciHeapEdgeCases, SingleElementOperations)
1247 auto node = heap.
insert(42);
1251 EXPECT_EQ(heap.
get_min(), 10);
1258TEST(FibonacciHeapEdgeCases, AlternatingMinMax)
1263 for (
int i = 0; i < 100; ++i)
1266 heap.
insert(std::numeric_limits<int>::min() + i / 2);
1268 heap.
insert(std::numeric_limits<int>::max() - i / 2);
1276 EXPECT_LE(prev, curr);
1286template <
typename T,
typename Compare>
1292 std::vector<T> extracted;
1297 for (
size_t i = 1; i < extracted.size(); ++i)
1299 if (extracted[i] < extracted[i - 1])
1306TEST(FibonacciHeapProperty, RandomInsertions)
1309 std::random_device rd;
1310 std::mt19937 gen(42);
1311 std::uniform_int_distribution<> dis(-10000, 10000);
1313 for (
int i = 0; i < 1000; ++i)
1319TEST(FibonacciHeapProperty, AfterDecreaseKeys)
1322 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
1324 for (
int i = 0; i < 100; ++i)
1328 for (
int i = 0; i < 20; ++i)
1332 for (
size_t i = 30; i < 50; ++i)
1338TEST(FibonacciHeapProperty, AfterMerge)
1341 std::random_device rd;
1342 std::mt19937 gen(42);
1343 std::uniform_int_distribution<> dis(1, 1000);
1345 for (
int i = 0; i < 500; ++i)
1360TEST(FibonacciHeapMemory, DestructorFreesMemory)
1363 for (
int trial = 0; trial < 10; ++trial)
1366 for (
int i = 0; i < 1000; ++i)
1372TEST(FibonacciHeapMemory, ClearFreesMemory)
1376 for (
int i = 0; i < 1000; ++i)
1381 for (
int i = 0; i < 1000; ++i)
1390 static inline long alive = 0;
1394 Counted(
const Counted & other) : key(other.key) { ++alive; }
1395 Counted & operator = (
const Counted & other) =
default;
1398 bool operator < (
const Counted & other)
const noexcept
1400 return key < other.key;
1409 std::vector<CountedHeap::Node *> fill_with_bushy_trees(CountedHeap & heap)
1411 std::mt19937 gen(141);
1412 std::vector<CountedHeap::Node *>
nodes;
1413 for (
int i = 0; i < 2000; ++i)
1414 nodes.push_back(heap.insert(Counted(
int(gen() % 1000000))));
1416 auto extract = [&heap, &
nodes]
1418 nodes.erase(std::find(
nodes.begin(),
nodes.end(), heap.get_min_node()));
1419 (void) heap.extract_min();
1421 for (
int i = 0; i < 100; ++i)
1423 for (
int i = 0; i < 300; ++i)
1426 heap.decrease_key(p, Counted(p->data.key -
int(gen() % 1000)));
1428 for (
int i = 0; i < 20; ++i)
1435 std::pair<size_t, size_t>
1436 degree_and_depth(
const std::vector<CountedHeap::Node *> &
nodes)
1438 size_t degree = 0, depth = 0;
1439 for (
auto *p :
nodes)
1441 degree = std::max(degree, p->degree);
1443 for (
auto *q = p; q->parent !=
nullptr; q = q->parent)
1445 depth = std::max(depth, d);
1453TEST(FibonacciHeapMemory, ClearDeletesEveryNodeOfBushyTrees)
1455 ASSERT_EQ(Counted::alive, 0);
1458 const auto nodes = fill_with_bushy_trees(heap);
1459 const auto [degree, depth] = degree_and_depth(
nodes);
1460 ASSERT_GE(degree, 3u);
1461 ASSERT_GE(depth, 3u);
1462 ASSERT_EQ(Counted::alive,
long(heap.size()));
1465 EXPECT_TRUE(heap.is_empty());
1466 EXPECT_EQ(Counted::alive, 0);
1468 (void) fill_with_bushy_trees(heap);
1470 EXPECT_EQ(Counted::alive, 0);
1484 struct Failing_Counted_Less
1486 Comparisons *
count =
nullptr;
1488 bool operator()(
const Counted & a,
const Counted & b)
const
1491 throw std::runtime_error(
"comparison failed");
1492 return a.key < b.key;
1500 std::vector<int> drain(Failing_Heap & heap, Comparisons &
count)
1503 std::vector<int>
keys;
1504 while (not heap.is_empty())
1505 keys.push_back(heap.extract_min().key);
1513 std::vector<int> use_heaps(Comparisons &
count)
1515 Failing_Heap heap(Failing_Counted_Less{&
count});
1516 Failing_Heap other(Failing_Counted_Less{&
count});
1519 std::vector<Failing_Heap::Node *>
nodes;
1520 for (
int key : {50, 30, 70, 10, 90, 20, 80, 40, 60, 0})
1521 nodes.push_back(heap.insert(Counted(key)));
1522 const Counted copied(35);
1523 nodes.push_back(heap.insert(copied));
1524 nodes.push_back(heap.emplace(45));
1525 (void) heap.extract_min();
1526 heap.decrease_key(
nodes[4], Counted(5));
1527 heap.decrease_key(
nodes[6], copied);
1528 nodes[2] = heap.update_key(
nodes[2], Counted(95));
1529 heap.delete_node(
nodes[7]);
1530 (void) other.insert(Counted(15));
1531 (void) other.insert(Counted(25));
1534 std::vector<int>
keys;
1535 while (not heap.is_empty())
1536 keys.push_back(heap.extract_min().key);
1541 const size_t size = heap.size();
1542 const size_t other_size = other.size();
1544 EXPECT_EQ(drain(other,
count).
size(), other_size);
1555TEST(FibonacciHeapComparison, WhatTheComparisonThrowsLeavesEveryNodeCounted)
1557 const long alive = Counted::alive;
1559 EXPECT_EQ(use_heaps(
count),
1560 (std::vector<int>{5, 10, 15, 20, 25, 30, 35, 35, 45, 50, 60, 95}));
1561 EXPECT_EQ(Counted::alive, alive);
1563 for (
long k = 1;
k <=
count.calls; ++
k)
1565 Comparisons failing;
1566 failing.fail_at =
k;
1567 EXPECT_THROW(use_heaps(failing), std::runtime_error) <<
"call " <<
k;
1568 EXPECT_EQ(Counted::alive, alive) <<
"call " <<
k;
1574TEST(FibonacciHeapComparison, InsertAndMergeLeaveTheHeapsAsTheyWere)
1576 const long alive = Counted::alive;
1579 Failing_Heap heap(Failing_Counted_Less{&
count});
1580 Failing_Heap other(Failing_Counted_Less{&
count});
1581 for (
int key : {4, 2, 6})
1582 (
void) heap.insert(Counted(key));
1583 (void) heap.extract_min();
1584 for (
int key : {5, 1})
1585 (
void) other.insert(Counted(key));
1587 const Counted three(3);
1589 EXPECT_THROW((
void) heap.insert(three), std::runtime_error);
1591 EXPECT_THROW((
void) heap.insert(Counted(3)), std::runtime_error);
1593 EXPECT_THROW((
void) heap.emplace(3), std::runtime_error);
1595 EXPECT_THROW(heap.merge(other), std::runtime_error);
1597 EXPECT_EQ(heap.size(), 2u);
1598 EXPECT_EQ(other.size(), 2u);
1599 EXPECT_EQ(drain(heap,
count), (std::vector<int>{4, 6}));
1600 EXPECT_EQ(drain(other,
count), (std::vector<int>{1, 5}));
1602 EXPECT_EQ(Counted::alive, alive);
1609TEST(FibonacciHeapComparison, ExtractAndDeleteLeaveTheirNodeInIfTheComparisonThrows)
1611 const std::vector<int>
keys = {8, 3, 5, 1, 9, 2, 7, 4, 6, 0};
1612 const long alive = Counted::alive;
1613 for (
size_t i = 0; i <=
keys.size(); ++i)
1617 for (
long k = 1; ; ++
k)
1620 Failing_Heap heap(Failing_Counted_Less{&
count});
1621 std::vector<Failing_Heap::Node *>
nodes;
1622 for (
int key :
keys)
1623 nodes.push_back(heap.insert(Counted(key)));
1624 (void) heap.extract_min();
1626 const std::vector<int>
all = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1627 std::vector<int> rest =
all;
1629 rest.erase(std::find(rest.begin(), rest.end(),
out));
1632 bool thrown =
false;
1635 if (i <
keys.size())
1636 heap.delete_node(
nodes[i]);
1638 (
void) heap.extract_min();
1640 catch (
const std::runtime_error &)
1644 const std::vector<int> &
expected = thrown ?
all : rest;
1645 EXPECT_EQ(heap.size(),
expected.size()) <<
"node " << i <<
", call " <<
k;
1646 EXPECT_EQ(drain(heap,
count),
expected) <<
"node " << i <<
", call " <<
k;
1651 EXPECT_EQ(Counted::alive, alive);
1660 static constexpr int gone = -1;
1665 Tracked(
const int k,
const int i) noexcept : key(
k), id(i) {}
1666 Tracked(
const Tracked &) =
default;
1667 Tracked & operator=(
const Tracked &) =
default;
1668 Tracked(Tracked && o) noexcept : key(o.key), id(std::exchange(o.id, gone)) {}
1669 Tracked & operator=(Tracked && o)
noexcept
1672 id = std::exchange(o.id, gone);
1675 bool operator==(
const Tracked &)
const =
default;
1678 void PrintTo(
const Tracked & x, std::ostream *os) { *os << x.key <<
'#' << x.id; }
1682 struct Failing_Tracked_Less
1684 Comparisons *
count =
nullptr;
1686 bool operator()(
const Tracked & a,
const Tracked & b)
const
1689 throw std::runtime_error(
"comparison failed");
1690 return a.key < b.key;
1701 std::string tree_errors(
const Tracked_Heap & heap, std::vector<Tracked_Heap::Node *> &
nodes)
1703 using Node = Tracked_Heap::Node;
1704 std::ostringstream err;
1706 Node *
min = heap.get_min_node();
1709 if (heap.size() != 0)
1710 err <<
"no minimum in a heap of " << heap.size() <<
"; ";
1713 std::vector<std::pair<Node *, Node *>> lists{{
min,
nullptr}};
1714 while (not lists.empty())
1716 const auto [first, parent] = lists.back();
1722 if (++
count > heap.size())
1724 err <<
"a list does not close; ";
1727 if (
nodes.size() >= heap.size())
1729 err <<
"more nodes than the " << heap.size() <<
" of the heap; ";
1733 if (p->parent != parent)
1734 err <<
"the parent of " << p->data.id <<
"; ";
1735 if (p->right->left != p or p->left->right != p)
1736 err <<
"the links of " << p->data.id <<
"; ";
1737 if (parent !=
nullptr and p->data.key < parent->data.key)
1738 err << p->data.id <<
" is less than its parent; ";
1739 if (p->child !=
nullptr)
1740 lists.emplace_back(p->child, p);
1741 else if (p->degree != 0)
1742 err <<
"the degree of " << p->data.id <<
", without children; ";
1746 if (parent !=
nullptr and parent->degree !=
count)
1747 err <<
"the degree of " << parent->data.id <<
"; ";
1749 if (
nodes.size() != heap.size())
1750 err <<
nodes.size() <<
" nodes in a heap of " << heap.size() <<
"; ";
1751 std::vector<Node *> sorted =
nodes;
1752 std::sort(sorted.begin(), sorted.end());
1753 if (std::adjacent_find(sorted.begin(), sorted.end()) != sorted.end())
1754 err <<
"a node is twice in the heap; ";
1756 if (p->data.key <
min->data.key)
1757 err << p->data.id <<
" is less than the minimum; ";
1765 void fill_with_trees(Tracked_Heap & heap,
const unsigned seed)
1767 std::mt19937 gen(
seed);
1768 std::vector<Tracked_Heap::Node *>
nodes;
1769 for (
int i = 0; i < 70; ++i)
1770 nodes.push_back(heap.insert(Tracked(
int(gen() % 40), i + 1)));
1771 auto extract = [&heap, &
nodes]
1773 nodes.erase(std::find(
nodes.begin(),
nodes.end(), heap.get_min_node()));
1774 (void) heap.extract_min();
1776 for (
int i = 0; i < 6; ++i)
1778 for (
int i = 0; i < 14; ++i)
1781 heap.decrease_key(p, Tracked(p->data.key -
int(gen() % 30), p->data.id));
1783 for (
int i = 0; i < 3; ++i)
1785 for (
int i = 0; i < 12; ++i)
1786 (
void) heap.insert(Tracked(
int(gen() % 40), 100 + i));
1799TEST(FibonacciHeapComparison, EveryFailedCallLeavesTheElementsAndTheMinimum)
1801 size_t failed = 0, marked = 0, deep = 0;
1802 for (
int op = 0; op < 6; ++op)
1804 for (
long k = 1; ; ++
k)
1806 SCOPED_TRACE(testing::Message() <<
"op " << op <<
", seed " <<
seed <<
", call " <<
k);
1808 Tracked_Heap heap(Failing_Tracked_Less{&
count});
1809 fill_with_trees(heap,
seed);
1811 std::vector<Tracked_Heap::Node *> before;
1812 ASSERT_EQ(tree_errors(heap, before),
"");
1813 std::map<Tracked_Heap::Node *, Tracked>
keys;
1814 for (
auto *p : before)
1816 keys.emplace(p, p->data);
1817 marked += p->mark ? 1 : 0;
1818 deep += p->parent !=
nullptr and p->parent->parent !=
nullptr ? 1 : 0;
1820 Tracked_Heap::Node *
min = heap.get_min_node();
1822 std::mt19937 gen(
seed*31 +
unsigned(op));
1823 Tracked_Heap::Node *x = before[gen() % before.size()];
1824 const Tracked less_key(x->data.key - 1 -
int(gen() % 30), 1000);
1825 const Tracked more_key(x->data.key + 1 +
int(gen() % 30), 1000);
1826 Tracked moved = less_key;
1829 bool thrown =
false;
1834 case 0: (void) heap.extract_min();
break;
1835 case 1: heap.delete_node(x);
break;
1836 case 2: heap.decrease_key(x, less_key);
break;
1837 case 3: heap.decrease_key(x, std::move(moved));
break;
1838 case 4: ASSERT_EQ(heap.update_key(x, less_key), x);
break;
1839 default: ASSERT_EQ(heap.update_key(x, more_key), x);
1842 catch (
const std::runtime_error &)
1848 std::vector<Tracked_Heap::Node *> after;
1849 ASSERT_EQ(tree_errors(heap, after),
"");
1853 EXPECT_EQ(heap.size(), before.size());
1854 EXPECT_EQ(heap.get_min_node(),
min);
1855 std::map<Tracked_Heap::Node *, Tracked> now;
1856 for (
auto *p : after)
1857 now.emplace(p, p->data);
1858 EXPECT_EQ(now,
keys);
1859 EXPECT_EQ(moved, less_key);
1863 EXPECT_EQ(x->data, op == 5 ? more_key : less_key);
1866 std::vector<int>
out;
1867 while (not heap.is_empty())
1868 out.push_back(heap.extract_min().key);
1869 EXPECT_TRUE(std::is_sorted(
out.begin(),
out.end()));
1870 EXPECT_EQ(
out.size(), before.size() - (not thrown
and op < 2 ? 1 : 0));
1874 EXPECT_GT(failed, 300u);
1875 EXPECT_GT(marked, 0u);
1876 EXPECT_GT(deep, 0u);
1885TEST(FibonacciHeapComparison, TheNodeThatLosesTheMinimumIsMarked)
1887 size_t marked = 0, cut = 0;
1888 for (
size_t victim = 0; victim < 10; ++victim)
1889 for (
long k = 1; ; ++
k)
1891 SCOPED_TRACE(testing::Message() <<
"node " << victim + 1 <<
", call " <<
k);
1893 Tracked_Heap heap(Failing_Tracked_Less{&
count});
1894 std::vector<Tracked_Heap::Node *> handles;
1895 for (
int id = 1;
id <= 10; ++id)
1896 handles.push_back(heap.insert(Tracked(5,
id)));
1897 Tracked_Heap::Node *
min = heap.get_min_node();
1900 bool thrown =
false;
1903 heap.delete_node(handles[victim]);
1905 catch (
const std::runtime_error &)
1913 std::vector<Tracked_Heap::Node *>
nodes;
1914 ASSERT_EQ(tree_errors(heap,
nodes),
"");
1915 ASSERT_EQ(heap.get_min_node(),
min);
1918 Tracked_Heap::Node *loser =
nullptr;
1919 for (
auto *p :
nodes)
1922 EXPECT_EQ(loser,
nullptr);
1923 EXPECT_NE(p->parent,
nullptr);
1926 if (loser ==
nullptr)
1929 if (loser->child ==
nullptr)
1932 Tracked_Heap::Node *child = loser->child;
1933 heap.decrease_key(child, Tracked(4, child->data.id));
1934 EXPECT_EQ(child->parent,
nullptr);
1935 EXPECT_EQ(loser->parent,
nullptr);
1936 EXPECT_FALSE(loser->mark);
1937 ASSERT_EQ(tree_errors(heap,
nodes),
"");
1940 EXPECT_GT(marked, 0u);
1950 static inline bool copy_throws =
false;
1951 static inline bool assignment_throws =
false;
1956 Brittle(
const int k,
const int i) noexcept : key(
k), id(i) {}
1958 Brittle(
const Brittle & other) : key(other.key), id(other.id)
1961 throw std::runtime_error(
"copy");
1964 Brittle(Brittle &&) noexcept = default;
1966 Brittle & operator=(const Brittle & other)
1970 if (assignment_throws)
1971 throw std::runtime_error(
"assignment");
1975 Brittle & operator=(Brittle &&) noexcept = default;
1977 bool operator==(const Brittle &) const = default;
1980 void PrintTo(const Brittle & x,
std::ostream *os) { *os << x.key <<
'#' << x.id; }
1984 bool operator()(
const Brittle & a,
const Brittle & b)
const noexcept {
return a.key < b.key; }
1993TEST(FibonacciHeapComparison, UpdateKeyCopiesTheKeyBeforeItChangesTheHeap)
1996 auto *one = heap.
insert(Brittle(1, 1));
1997 auto *five = heap.
insert(Brittle(5, 2));
1998 auto *seven = heap.
insert(Brittle(7, 3));
1999 (void) heap.
insert(Brittle(0, 4));
2002 ASSERT_EQ(five->parent,
nullptr);
2003 ASSERT_EQ(seven->parent, five);
2004 const Brittle nine(9, 5);
2006 Brittle::copy_throws =
true;
2007 EXPECT_THROW((
void) heap.
update_key(one, nine), std::runtime_error);
2008 EXPECT_THROW((
void) heap.
update_key(five, nine), std::runtime_error);
2009 EXPECT_THROW((
void) heap.
update_key(seven, nine), std::runtime_error);
2010 Brittle::copy_throws =
false;
2011 EXPECT_EQ(heap.
size(), 3u);
2013 EXPECT_EQ(one->data, Brittle(1, 1));
2014 EXPECT_EQ(five->data, Brittle(5, 2));
2015 EXPECT_EQ(seven->data, Brittle(7, 3));
2016 EXPECT_EQ(seven->parent, five);
2017 EXPECT_EQ(five->degree, 1u);
2021 Brittle::assignment_throws =
true;
2023 Brittle::assignment_throws =
false;
2024 EXPECT_EQ(one->data, nine);
2025 EXPECT_EQ(heap.
size(), 3u);
2042 explicit Copied_Only(
const int k) noexcept : key(
k) {}
2043 Copied_Only(
const Copied_Only &) =
default;
2044 Copied_Only & operator=(
const Copied_Only &) =
default;
2045 Copied_Only & operator=(Copied_Only &) =
delete;
2046 Copied_Only & operator=(Copied_Only &&) =
delete;
2048 bool operator<(
const Copied_Only & other)
const noexcept {
return key < other.key; }
2057TEST(FibonacciHeapComparison, UpdateKeyTakesAKeyWithNoMoveAssignment)
2059 static_assert(not std::is_move_assignable_v<Copied_Only>);
2060 static_assert(std::is_copy_assignable_v<Copied_Only>);
2062 const Copied_Only zero(0), one(1), three(3), five(5), seven(7), nine(9);
2063 auto *n1 = heap.
insert(one);
2064 auto *n5 = heap.
insert(five);
2065 auto *n7 = heap.
insert(seven);
2066 (void) heap.
insert(zero);
2069 ASSERT_EQ(n7->parent, n5);
2072 EXPECT_EQ(n7->data.key, 9);
2073 EXPECT_EQ(n7->parent,
nullptr);
2075 EXPECT_EQ(n1->data.key, 7);
2079 EXPECT_EQ(heap.
size(), 3u);
2092 static inline bool armed =
false;
2096 operator bool() const &
2099 throw std::runtime_error(
"implicit conversion");
2103 explicit operator bool() &&
noexcept {
return value; }
2108 Wary_Bool operator()(
const int a,
const int b)
const noexcept {
return Wary_Bool{a < b}; }
2117TEST(FibonacciHeapComparison, TheResultOfAComparisonIsConvertedAsInACondition)
2120 auto *five = heap.
insert(5);
2121 auto *seven = heap.
insert(7);
2122 auto *nine = heap.
insert(9);
2127 Wary_Bool::armed =
true;
2130 EXPECT_NO_THROW((
void) heap.
update_key(five, 8));
2131 Wary_Bool::armed =
false;
2132 EXPECT_EQ(heap.
size(), 3u);
2142TEST(FibonacciHeapComparison, UpdateKeyKeepsTheNodeWhenTheKeyIncreases)
2145 Tracked_Heap heap(Failing_Tracked_Less{&
count});
2146 fill_with_trees(heap, 5);
2147 std::vector<Tracked_Heap::Node *>
nodes;
2148 ASSERT_EQ(tree_errors(heap,
nodes),
"");
2149 const size_t size = heap.size();
2151 for (
auto *p :
nodes)
2153 const Tracked key(p->data.key + 7, ++
id);
2154 ASSERT_EQ(heap.update_key(p, key), p);
2155 EXPECT_EQ(p->data, key);
2156 std::vector<Tracked_Heap::Node *> now;
2157 ASSERT_EQ(tree_errors(heap, now),
"");
2158 ASSERT_EQ(heap.size(),
size);
2161 Tracked_Heap one(Failing_Tracked_Less{&
count});
2162 auto *p = one.insert(Tracked(3, 1));
2163 EXPECT_EQ(one.update_key(p, Tracked(9, 2)), p);
2164 EXPECT_EQ(one.get_min(), Tracked(9, 2));
2165 EXPECT_EQ(one.size(), 1u);
2172TEST(FibonacciHeapComparison, AMinimumThatWasLinkedIsTheMinimumAgain)
2175 for (
int op = 0; op < 2; ++op)
2176 for (
size_t which = 1; which < 12; ++which)
2177 for (
long k = 1; ; ++
k)
2179 SCOPED_TRACE(testing::Message() <<
"op " << op <<
", node " << which <<
", call " <<
k);
2181 Tracked_Heap heap(Failing_Tracked_Less{&
count});
2182 std::vector<Tracked_Heap::Node *>
nodes;
2183 for (
int i = 0; i < 12; ++i)
2184 nodes.push_back(heap.insert(Tracked(i < 8 ? 5 : 5 + i, i + 1)));
2185 Tracked_Heap::Node *
min = heap.get_min_node();
2186 Tracked_Heap::Node *x =
nodes[which];
2190 bool thrown =
false;
2194 heap.delete_node(x);
2196 (
void) heap.update_key(x, Tracked(99, 50));
2198 catch (
const std::runtime_error &)
2204 std::vector<Tracked_Heap::Node *> after;
2205 EXPECT_EQ(tree_errors(heap, after),
"");
2209 EXPECT_EQ(heap.get_min_node(),
min);
2210 EXPECT_EQ(
min->parent,
nullptr);
2211 EXPECT_EQ(heap.size(), 12u);
2213 EXPECT_GT(failed, 100u);
2218TEST(FibonacciHeapComparison, UpdateKeyOfTheMinimumToAGreaterKey)
2221 Tracked_Heap heap(Failing_Tracked_Less{&
count});
2222 std::vector<Tracked_Heap::Node *>
nodes;
2223 for (
int i = 0; i < 9; ++i)
2224 nodes.push_back(heap.insert(Tracked(10*i, i + 1)));
2225 EXPECT_EQ(heap.extract_min().key, 0);
2226 std::vector<Tracked_Heap::Node *> now;
2228 Tracked_Heap::Node *p = heap.get_min_node();
2229 ASSERT_EQ(p,
nodes[1]);
2230 ASSERT_EQ(heap.update_key(p, Tracked(15, 100)), p);
2231 EXPECT_EQ(heap.get_min_node(), p);
2232 EXPECT_EQ(tree_errors(heap, now),
"");
2234 EXPECT_EQ(heap.update_key(p, Tracked(25, 101)), p);
2235 EXPECT_EQ(heap.get_min_node(),
nodes[2]);
2236 EXPECT_EQ(tree_errors(heap, now),
"");
2238 std::vector<int>
out;
2239 while (not heap.is_empty())
2240 out.push_back(heap.extract_min().key);
2241 EXPECT_EQ(
out, (std::vector<int>{20, 25, 30, 40, 50, 60, 70, 80}));
2245TEST(FibonacciHeapPerformance, DISABLED_TimingComparison)
2247 constexpr int N = 1000000;
2249 auto start = std::chrono::high_resolution_clock::now();
2252 for (
int i =
N; i >= 1; --i)
2255 auto after_insert = std::chrono::high_resolution_clock::now();
2260 auto after_extract = std::chrono::high_resolution_clock::now();
2262 auto insert_time = std::chrono::duration_cast<std::chrono::milliseconds>(
2263 after_insert - start).count();
2264 auto extract_time = std::chrono::duration_cast<std::chrono::milliseconds>(
2265 after_extract - after_insert).count();
2267 std::cout <<
"Insert " <<
N <<
" elements: " << insert_time <<
" ms\n";
2268 std::cout <<
"Extract " <<
N <<
" elements: " << extract_time <<
" ms\n";
2275TEST(FibonacciHeapUsagePattern, DijkstraSimulation)
2283 bool operator<(
const DistNode & other)
const
2285 return distance < other.distance;
2290 std::vector<Fibonacci_Heap<DistNode>::Node *> handles(100,
nullptr);
2293 for (
int v = 0; v < 100; ++v)
2295 int dist = (v == 0) ? 0 : std::numeric_limits<int>::max();
2296 handles[v] = pq.
insert({v, dist});
2300 std::random_device rd;
2301 std::mt19937 gen(42);
2302 std::uniform_int_distribution<> dist_gen(1, 100);
2310 handles[u.vertex] =
nullptr;
2313 for (
int i = 0; i < 3; ++i)
2315 int v = dist_gen(gen) % 100;
2317 if (handles[v] !=
nullptr && handles[v]->data.distance > u.distance + 10)
2330TEST(FibonacciHeapComparator, KeyComp)
2335 EXPECT_TRUE(
cmp(1, 2));
2336 EXPECT_FALSE(
cmp(2, 1));
2337 EXPECT_FALSE(
cmp(1, 1));
2340TEST(FibonacciHeapComparator, CustomLambdaComparator)
2342 auto cmp = [](
int a,
int b) {
return a > b; };
2346 (void)heap.insert(30);
2347 (void)heap.insert(20);
2349 EXPECT_EQ(heap.extract_min(), 30);
2350 EXPECT_EQ(heap.extract_min(), 20);
2351 EXPECT_EQ(heap.extract_min(), 10);
2358TEST(FibonacciHeapEdgeCases, DecreaseKeyOnRootNode)
2361 auto node = heap.
insert(50);
2368 EXPECT_EQ(heap.
get_min(), 10);
2374TEST(FibonacciHeapEdgeCases, DecreaseKeyChildBecomesSmallerThanParent)
2379 for (
int i = 1; i <= 10; ++i)
2387 auto node = heap.
insert(1000);
2397 EXPECT_LE(prev, curr);
2402TEST(FibonacciHeapEdgeCases, DeleteNodeWithMultipleChildren)
2405 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
2408 for (
int i = 0; i < 15; ++i)
2412 for (
int i = 0; i < 4; ++i)
2420 EXPECT_EQ(heap.
size(), 10u);
2426 EXPECT_LE(prev, curr);
2431TEST(FibonacciHeapEdgeCases, MergeSelfNoOp)
2439 EXPECT_EQ(heap.
size(), 2u);
2440 EXPECT_EQ(heap.
get_min(), 10);
2443TEST(FibonacciHeapEdgeCases, UpdateKeyToSameValueOnChild)
2448 for (
int i = 1; i <= 10; ++i)
2455 auto node = heap.
insert(500);
2458 EXPECT_EQ(result, node);
2459 EXPECT_EQ(result->data, 500);
2462TEST(FibonacciHeapEdgeCases, ConsecutiveDecreaseKeys)
2466 auto node = heap.
insert(100);
2472 EXPECT_EQ(heap.
get_min(), 90);
2475 EXPECT_EQ(heap.
get_min(), 50);
2478 EXPECT_EQ(heap.
get_min(), 10);
2486TEST(FibonacciHeapEdgeCases, EmplaceWithSingleArg)
2493 EXPECT_EQ(node->data, 42);
2494 EXPECT_EQ(heap.
get_min(), 42);
2497TEST(FibonacciHeapEdgeCases, LargeDegreeTrees)
2503 constexpr int N = 10000;
2504 for (
int i =
N; i >= 1; --i)
2508 for (
int i = 0; i <
N / 2; ++i)
2511 EXPECT_EQ(val, i + 1);
2515 for (
int i =
N / 2 + 1; i <=
N; ++i)
2524TEST(FibonacciHeapEdgeCases, SwapEmptyHeaps)
2534TEST(FibonacciHeapEdgeCases, MoveAssignToSelf)
2541#pragma GCC diagnostic push
2542#pragma GCC diagnostic ignored "-Wself-move"
2543 heap = std::move(heap);
2544#pragma GCC diagnostic pop
2550TEST(FibonacciHeapEdgeCases, DeleteLastTwoNodes)
2553 auto n1 = heap.
insert(10);
2554 auto n2 = heap.
insert(20);
2557 EXPECT_EQ(heap.
size(), 1u);
2558 EXPECT_EQ(heap.
get_min(), 20);
2570TEST(FibonacciHeapRegression, DeleteAloneRootWithChildren)
2590 EXPECT_EQ(heap.
size(), 1u);
2595TEST(FibonacciHeapRegression, CascadingCutsChain)
2598 std::vector<Fibonacci_Heap<int>::Node *>
nodes;
2601 for (
int i = 1; i <= 100; ++i)
2605 for (
int i = 0; i < 30; ++i)
2611 for (
size_t i = 50; i < 70; ++i)
2613 if (
nodes[i]->data > key)
2625 EXPECT_LE(prev, curr);
2638# if defined(__unix__) || defined(__APPLE__)
2639 pthread_attr_t attr;
2640 ASSERT_EQ(pthread_attr_init(&attr), 0);
2641 ASSERT_EQ(pthread_attr_setstacksize(&attr, 256 * 1024), 0);
2642 auto start = [](
void *arg) ->
void *
2644 (*
static_cast<F *
>(arg))();
2648 const int created = pthread_create(&thread, &attr, start, &f);
2649 pthread_attr_destroy(&attr);
2650 ASSERT_EQ(created, 0);
2651 ASSERT_EQ(pthread_join(thread,
nullptr), 0);
2666 for (
long i = 1; i < n; ++i)
2668 const long b = key - 2;
2669 (void) heap.
insert(key - 3);
2671 auto *c = heap.
insert(key - 1);
2683 for (
auto *p = heap.
get_min_node(); p !=
nullptr; p = p->child)
2691TEST(FibonacciHeapRegression, ClearsATreeAsHighAsTheHeap)
2693 constexpr long n = 50000;
2695 build_chain(heap, n);
2696 ASSERT_EQ(heap.
size(),
size_t(n));
2698 ASSERT_EQ(height_of_chain(heap),
size_t(n));
2700 run_with_small_stack([&heap] { heap.
clear(); });
2702 EXPECT_EQ(heap.
size(), 0u);
2709TEST(FibonacciHeapRegression, DestroysATreeAsHighAsTheHeap)
2711 constexpr long n = 50000;
2712 auto heap = std::make_unique<Fibonacci_Heap<long>>();
2713 build_chain(*heap, n);
2714 ASSERT_EQ(height_of_chain(*heap),
size_t(n));
2716 run_with_small_stack([&heap] { heap.reset(); });
2717 EXPECT_EQ(heap,
nullptr);
2722 ::testing::InitGoogleTest(&argc, argv);
2723 return RUN_ALL_TESTS();
bool operator<(const Time &l, const Time &r)
void PrintTo(const mpq_class &q, std::ostream *os)
WeightedDigraph::Node Node
size_t size_t int32_t value
size_t size_t int32_t * out
Implementation of a Fibonacci Heap priority queue.
const T & get_min() const
Returns the minimum element without removing it.
Node * get_min_node() const noexcept
Returns a pointer to the minimum node.
Compare key_comp() const
Returns the comparison functor.
void clear() noexcept(std::is_nothrow_destructible_v< T >)
Removes all elements from the heap.
Node * insert(const T &val)
Inserts a new element (copy).
void merge(Fibonacci_Heap &other)
Merges another heap into this one.
void swap(Fibonacci_Heap &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps contents with another heap.
Node * emplace(Args &&... args)
Constructs and inserts an element in-place.
size_t size() const noexcept
Returns the number of elements in the heap.
T extract_min()
Extracts and returns the minimum element.
Compare & get_compare() noexcept
Returns the comparison functor that orders the heap.
void delete_node(Node *x)
Deletes a specific node from the heap.
Node * update_key(Node *x, const T &k)
Updates the key of a node (increase or decrease).
void decrease_key(Node *x, const T &k)
Decreases the key of a node.
bool is_empty() const noexcept
Checks if the heap is empty.
Number Theoretic Transform over Z / MOD Z.
Represents a point with rectangular coordinates in a 2D plane.
Minimal std::expected-style result type for C++20.
Fibonacci_Heap< int > heap
Fibonacci_Heap< int > heap
std::vector< Fibonacci_Heap< int >::Node * > nodes
bool verify_heap_property(Fibonacci_Heap< T, Compare > &heap)
TEST_F(FibonacciHeapTest, InsertSingleElement)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
DynArray< Graph::Node * > nodes
size_t degree(const GT &g, typename GT::Node *p, SA &sa)
Degree of a node as an Eulerian walk counts it.
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
and
Check uniqueness with explicit hash + equality functors.
Itor3 merge(Itor1 source1Beg, Itor1 source1End, Itor2 source2Beg, Itor2 source2End, Itor3 destBeg)
Merge two sorted ranges.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
bool run_with_small_stack(F &&f, size_t kb=256)
Runs f in a thread whose stack has kb KiB, and returns false if the thread could not run; without POS...
Represents a node in the Fibonacci Heap.
Node * right
Right sibling in circular list.
bool operator<(const Point &other) const
Fibonacci Heap implementation.