Serenity Operating System
1/*
2 * Copyright (c) 2021, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8
9#include <AK/Array.h>
10#include <AK/Find.h>
11
12TEST_CASE(should_return_end_if_not_in_container)
13{
14 constexpr Array<int, 10> a {};
15
16 static_assert(a.end() == AK::find(a.begin(), a.end(), 1));
17
18 EXPECT(a.end() == AK::find(a.begin(), a.end(), 1));
19}
20
21TEST_CASE(should_return_iterator_to_first_matching_value_in_container)
22{
23 static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
24
25 constexpr auto expected = a.begin() + 4;
26
27 static_assert(expected == AK::find(a.begin(), a.end(), 0));
28
29 EXPECT(expected == AK::find(a.begin(), a.end(), 0));
30}
31
32TEST_CASE(should_return_iterator_to_first_predicate_matching_value_in_container)
33{
34 static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
35
36 constexpr auto expected = a.begin() + 4;
37
38 static_assert(expected == AK::find_if(a.begin(), a.end(), [](auto v) { return v == 0; }));
39
40 EXPECT(expected == AK::find_if(a.begin(), a.end(), [](auto v) { return v == 0; }));
41
42 auto find_me = 8;
43 EXPECT(find_me == *AK::find_if(a.begin(), a.end(), [&](auto v) { return v == find_me; }));
44}
45
46TEST_CASE(should_return_index_to_first_predicate_matching_value_in_container)
47{
48 static constexpr Array<int, 10> a { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
49
50 static_assert(4 == AK::find_index(a.begin(), a.end(), 0));
51
52 EXPECT(4 == AK::find_index(a.begin(), a.end(), 0));
53}