mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK+Tests: Add Vector::find_first_index_if()
This commit is contained in:
parent
1352f8820b
commit
89e55c5297
Notes:
sideshowbarker
2024-07-17 02:59:43 +09:00
Author: https://github.com/AtkinsSJ Commit: https://github.com/SerenityOS/serenity/commit/89e55c5297 Pull-request: https://github.com/SerenityOS/serenity/pull/18289
2 changed files with 17 additions and 0 deletions
|
@ -796,6 +796,15 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
Optional<size_t> find_first_index_if(TUnaryPredicate&& finder) const
|
||||
{
|
||||
auto maybe_result = AK::find_if(begin(), end(), finder);
|
||||
if (maybe_result == end())
|
||||
return {};
|
||||
return maybe_result.index();
|
||||
}
|
||||
|
||||
void reverse()
|
||||
{
|
||||
for (size_t i = 0; i < size() / 2; ++i)
|
||||
|
|
|
@ -423,6 +423,14 @@ TEST_CASE(should_find_index)
|
|||
EXPECT(!v.find_first_index(42).has_value());
|
||||
}
|
||||
|
||||
TEST_CASE(should_find_predicate_index)
|
||||
{
|
||||
Vector<int> v { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
|
||||
|
||||
EXPECT_EQ(4u, v.find_first_index_if([](auto const v) { return v == 0; }).value());
|
||||
EXPECT(!v.find_first_index_if([](auto const v) { return v == 123; }).has_value());
|
||||
}
|
||||
|
||||
TEST_CASE(should_contain_start)
|
||||
{
|
||||
// Tests whether value is found if at the start of the range.
|
||||
|
|
Loading…
Reference in a new issue