AK: Add randomized tests for BinaryHeap

This commit is contained in:
Martin Janiczek 2023-09-28 00:36:05 +02:00 committed by Tim Schumacher
parent b56e022ce8
commit 1452693183
Notes: sideshowbarker 2024-07-17 03:45:48 +09:00

View file

@ -9,6 +9,8 @@
#include <AK/BinaryHeap.h>
#include <AK/DeprecatedString.h>
using namespace Test::Randomized;
TEST_CASE(construct)
{
BinaryHeap<int, int, 5> empty;
@ -65,3 +67,38 @@ TEST_CASE(large_populate_reverse)
EXPECT_EQ(ints.pop_min(), i);
}
}
RANDOMIZED_TEST_CASE(pop_min_is_min)
{
GEN(vec, Gen::vector(1, 10, []() { return Gen::unsigned_int(); }));
auto sorted { vec };
AK::quick_sort(sorted);
BinaryHeap<u32, u32, 10> heap;
// insert in a non-sorted order
for (u32 n : vec) {
heap.insert(n, n);
}
// check in a sorted order
for (u32 sorted_n : sorted) {
EXPECT_EQ(heap.pop_min(), sorted_n);
}
}
RANDOMIZED_TEST_CASE(peek_min_same_as_pop_min)
{
GEN(vec, Gen::vector(1, 10, []() { return Gen::unsigned_int(); }));
BinaryHeap<u32, u32, 10> heap;
for (u32 n : vec) {
heap.insert(n, n);
}
while (!heap.is_empty()) {
u32 peeked = heap.peek_min();
u32 popped = heap.pop_min();
EXPECT_EQ(peeked, popped);
}
}