2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-12-02 04:19:57 +00:00
|
|
|
#include <AK/TestSuite.h>
|
|
|
|
|
|
|
|
#include <AK/BinarySearch.h>
|
2020-08-02 19:10:35 +00:00
|
|
|
#include <AK/Span.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
|
|
|
|
|
|
|
TEST_CASE(vector_ints)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
|
2020-07-26 09:22:05 +00:00
|
|
|
auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
|
|
|
|
auto test2 = *binary_search(ints.span(), 2, AK::integral_compare<int>);
|
|
|
|
auto test3 = *binary_search(ints.span(), 3, AK::integral_compare<int>);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, 1);
|
|
|
|
EXPECT_EQ(test2, 2);
|
|
|
|
EXPECT_EQ(test3, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(array_doubles)
|
|
|
|
{
|
|
|
|
double doubles[] = { 1.1, 9.9, 33.33 };
|
|
|
|
|
2020-08-26 12:14:32 +00:00
|
|
|
auto test1 = *binary_search(Span<double> { doubles, 3 }, 1.1, AK::integral_compare<double>);
|
|
|
|
auto test2 = *binary_search(Span<double> { doubles, 3 }, 9.9, AK::integral_compare<double>);
|
|
|
|
auto test3 = *binary_search(Span<double> { doubles, 3 }, 33.33, AK::integral_compare<double>);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, 1.1);
|
|
|
|
EXPECT_EQ(test2, 9.9);
|
|
|
|
EXPECT_EQ(test3, 33.33);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(vector_strings)
|
|
|
|
{
|
|
|
|
Vector<String> strings;
|
|
|
|
strings.append("bat");
|
|
|
|
strings.append("cat");
|
|
|
|
strings.append("dog");
|
|
|
|
|
|
|
|
auto string_compare = [](const String& a, const String& b) -> int {
|
|
|
|
return strcmp(a.characters(), b.characters());
|
|
|
|
};
|
2020-07-26 09:22:05 +00:00
|
|
|
auto test1 = *binary_search(strings.span(), String("bat"), string_compare);
|
|
|
|
auto test2 = *binary_search(strings.span(), String("cat"), string_compare);
|
|
|
|
auto test3 = *binary_search(strings.span(), String("dog"), string_compare);
|
2019-12-02 04:19:57 +00:00
|
|
|
EXPECT_EQ(test1, String("bat"));
|
|
|
|
EXPECT_EQ(test2, String("cat"));
|
|
|
|
EXPECT_EQ(test3, String("dog"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(single_element)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
|
2020-07-26 09:22:05 +00:00
|
|
|
auto test1 = *binary_search(ints.span(), 1, AK::integral_compare<int>);
|
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-07-26 09:22:05 +00:00
|
|
|
auto test1 = binary_search(ints.span(), -1, AK::integral_compare<int>);
|
|
|
|
auto test2 = binary_search(ints.span(), 0, AK::integral_compare<int>);
|
|
|
|
auto test3 = binary_search(ints.span(), 4, AK::integral_compare<int>);
|
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-07-26 09:22:05 +00:00
|
|
|
auto test1 = binary_search(ints.span(), 1, AK::integral_compare<int>);
|
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 };
|
|
|
|
|
|
|
|
constexpr auto test1 = *binary_search(array.span(), 1, AK::integral_compare<int>);
|
|
|
|
constexpr auto test2 = *binary_search(array.span(), 17, AK::integral_compare<int>);
|
|
|
|
constexpr auto test3 = *binary_search(array.span(), 42, AK::integral_compare<int>);
|
|
|
|
constexpr auto test4 = binary_search(array.span(), 99, AK::integral_compare<int>);
|
|
|
|
|
|
|
|
static_assert(test1 == 1, "Binary search should find value at compile-time.");
|
|
|
|
static_assert(test2 == 17, "Binary search should find value at compile-time.");
|
|
|
|
static_assert(test3 == 42, "Binary search should find value at compile-time.");
|
|
|
|
static_assert(test4 == nullptr, "Binary search should return nullptr for missing value at compile-time.");
|
|
|
|
}
|
|
|
|
|
2019-12-02 04:19:57 +00:00
|
|
|
TEST_MAIN(BinarySearch)
|