mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:07:45 +00:00
AK: Add randomized tests for BinaryHeap
This commit is contained in:
parent
b56e022ce8
commit
1452693183
1 changed files with 37 additions and 0 deletions
|
@ -9,6 +9,8 @@
|
||||||
#include <AK/BinaryHeap.h>
|
#include <AK/BinaryHeap.h>
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
|
|
||||||
|
using namespace Test::Randomized;
|
||||||
|
|
||||||
TEST_CASE(construct)
|
TEST_CASE(construct)
|
||||||
{
|
{
|
||||||
BinaryHeap<int, int, 5> empty;
|
BinaryHeap<int, int, 5> empty;
|
||||||
|
@ -65,3 +67,38 @@ TEST_CASE(large_populate_reverse)
|
||||||
EXPECT_EQ(ints.pop_min(), i);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue