Skip to content

Commit c802971

Browse files
Janiczektimschumi
authored andcommitted
AK: Add liveness tests for BinarySearch
1 parent 1452693 commit c802971

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Tests/AK/TestBinarySearch.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <cstring>
1313
#include <new>
1414

15+
using namespace Test::Randomized;
16+
1517
TEST_CASE(vector_ints)
1618
{
1719
Vector<int> ints;
@@ -117,3 +119,27 @@ TEST_CASE(unsigned_to_signed_regression)
117119
EXPECT_EQ(binary_search(input, 1u, &nearby_index), &input[1]);
118120
EXPECT_EQ(nearby_index, 1u);
119121
}
122+
123+
RANDOMIZED_TEST_CASE(finds_number_that_is_present)
124+
{
125+
GEN(vec, Gen::vector(1, 16, []() { return Gen::unsigned_int(); }));
126+
GEN(i, Gen::unsigned_int(0, vec.size() - 1));
127+
AK::quick_sort(vec);
128+
u32 n = vec[i];
129+
auto ptr = binary_search(vec, n);
130+
EXPECT_NE(ptr, nullptr);
131+
EXPECT_EQ(*ptr, n);
132+
}
133+
134+
RANDOMIZED_TEST_CASE(doesnt_find_number_that_is_not_present)
135+
{
136+
GEN(vec, Gen::vector(1, 16, []() { return Gen::unsigned_int(); }));
137+
AK::quick_sort(vec);
138+
139+
u32 not_present = 0;
140+
while (!vec.find(not_present).is_end()) {
141+
++not_present;
142+
}
143+
144+
EXPECT_EQ(binary_search(vec, not_present), nullptr);
145+
}

0 commit comments

Comments
 (0)