mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
AK: Add randomized tests for BinaryHeap
This commit is contained in:
parent
b56e022ce8
commit
1452693183
Notes:
sideshowbarker
2024-07-17 03:45:48 +09:00
Author: https://github.com/Janiczek Commit: https://github.com/SerenityOS/serenity/commit/1452693183 Pull-request: https://github.com/SerenityOS/serenity/pull/21618 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/timschumi ✅
1 changed files with 37 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue