2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-25 05:53:23 +00:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-12-02 04:19:57 +00:00
|
|
|
|
|
|
|
#include <AK/BinarySearch.h>
|
2020-08-02 19:10:35 +00:00
|
|
|
#include <AK/Span.h>
|
2021-11-10 10:05:21 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-05-03 17:21:00 +00:00
|
|
|
#include <cstring>
|
2020-08-02 19:10:35 +00:00
|
|
|
#include <new>
|
2019-12-02 04:19:57 +00:00
|
|
|
|
2023-09-29 20:59:20 +00:00
|
|
|
using namespace Test::Randomized;
|
|
|
|
|
2019-12-02 04:19:57 +00:00
|
|
|
TEST_CASE(vector_ints)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
auto test1 = *binary_search(ints, 1);
|
|
|
|
auto test2 = *binary_search(ints, 2);
|
|
|
|
auto test3 = *binary_search(ints, 3);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, 1);
|
|
|
|
EXPECT_EQ(test2, 2);
|
|
|
|
EXPECT_EQ(test3, 3);
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
TEST_CASE(span_rvalue_reference)
|
|
|
|
{
|
|
|
|
Array<long, 3> array { 1, 2, 3 };
|
|
|
|
|
|
|
|
size_t nearby_index = 0;
|
|
|
|
auto* pointer = binary_search(array.span(), 2, &nearby_index);
|
|
|
|
|
|
|
|
EXPECT_EQ(nearby_index, 1u);
|
|
|
|
EXPECT_EQ(pointer, &array[1]);
|
|
|
|
}
|
|
|
|
|
2019-12-02 04:19:57 +00:00
|
|
|
TEST_CASE(array_doubles)
|
|
|
|
{
|
2020-12-29 15:13:16 +00:00
|
|
|
Array<double, 3> array { 1.1, 9.9, 33.33 };
|
|
|
|
|
|
|
|
EXPECT_EQ(binary_search(array, 1.1), &array[0]);
|
|
|
|
EXPECT_EQ(binary_search(array, 33.33), &array[2]);
|
|
|
|
EXPECT_EQ(binary_search(array, 9.9), &array[1]);
|
2019-12-02 04:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(vector_strings)
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString> strings;
|
2019-12-02 04:19:57 +00:00
|
|
|
strings.append("bat");
|
|
|
|
strings.append("cat");
|
|
|
|
strings.append("dog");
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
auto string_compare = [](ByteString const& a, ByteString const& b) -> int {
|
2019-12-02 04:19:57 +00:00
|
|
|
return strcmp(a.characters(), b.characters());
|
|
|
|
};
|
2023-12-16 14:19:34 +00:00
|
|
|
auto test1 = *binary_search(strings, ByteString("bat"), nullptr, string_compare);
|
|
|
|
auto test2 = *binary_search(strings, ByteString("cat"), nullptr, string_compare);
|
|
|
|
auto test3 = *binary_search(strings, ByteString("dog"), nullptr, string_compare);
|
|
|
|
EXPECT_EQ(test1, ByteString("bat"));
|
|
|
|
EXPECT_EQ(test2, ByteString("cat"));
|
|
|
|
EXPECT_EQ(test3, ByteString("dog"));
|
2019-12-02 04:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(single_element)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
auto test1 = *binary_search(ints, 1);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(not_found)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
auto test1 = binary_search(ints, -1);
|
|
|
|
auto test2 = binary_search(ints, 0);
|
|
|
|
auto test3 = binary_search(ints, 4);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, nullptr);
|
|
|
|
EXPECT_EQ(test2, nullptr);
|
|
|
|
EXPECT_EQ(test3, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(no_elements)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
auto test1 = binary_search(ints, 1);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:30:35 +00:00
|
|
|
TEST_CASE(constexpr_array_search)
|
|
|
|
{
|
|
|
|
constexpr Array<int, 3> array = { 1, 17, 42 };
|
|
|
|
|
2020-12-29 15:13:16 +00:00
|
|
|
static_assert(binary_search(array, 42) == &array[2]);
|
|
|
|
static_assert(binary_search(array, 17) == &array[1]);
|
|
|
|
static_assert(binary_search(array, 3) == nullptr);
|
2020-10-13 22:30:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 20:32:59 +00:00
|
|
|
TEST_CASE(unsigned_to_signed_regression)
|
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
Array<u32, 5> const input { 0, 1, 2, 3, 4 };
|
2021-01-01 20:32:59 +00:00
|
|
|
|
|
|
|
// The algorithm computes 1 - input[2] = -1, and if this is (incorrectly) cast
|
|
|
|
// to an unsigned then it will look in the wrong direction and miss the 1.
|
|
|
|
|
|
|
|
size_t nearby_index = 1;
|
|
|
|
EXPECT_EQ(binary_search(input, 1u, &nearby_index), &input[1]);
|
|
|
|
EXPECT_EQ(nearby_index, 1u);
|
|
|
|
}
|
2023-09-29 20:59:20 +00:00
|
|
|
|
|
|
|
RANDOMIZED_TEST_CASE(finds_number_that_is_present)
|
|
|
|
{
|
2023-12-30 15:23:59 +00:00
|
|
|
GEN(vec, Gen::vector(1, 16, []() { return Gen::number_u64(); }));
|
|
|
|
GEN(i, Gen::number_u64(0, vec.size() - 1));
|
2023-09-29 20:59:20 +00:00
|
|
|
AK::quick_sort(vec);
|
2023-12-30 15:23:59 +00:00
|
|
|
u64 n = vec[i];
|
2023-09-29 20:59:20 +00:00
|
|
|
auto ptr = binary_search(vec, n);
|
|
|
|
EXPECT_NE(ptr, nullptr);
|
|
|
|
EXPECT_EQ(*ptr, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
RANDOMIZED_TEST_CASE(doesnt_find_number_that_is_not_present)
|
|
|
|
{
|
2023-12-30 15:23:59 +00:00
|
|
|
GEN(vec, Gen::vector(1, 16, []() { return Gen::number_u64(); }));
|
2023-09-29 20:59:20 +00:00
|
|
|
AK::quick_sort(vec);
|
|
|
|
|
2023-12-30 15:23:59 +00:00
|
|
|
u64 not_present = 0;
|
2023-09-29 20:59:20 +00:00
|
|
|
while (!vec.find(not_present).is_end()) {
|
|
|
|
++not_present;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(binary_search(vec, not_present), nullptr);
|
|
|
|
}
|