Skip to content

[libc][CPP] Add all_of and find_if_not to algorithm.h #94058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions libc/src/__support/CPP/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
return (a < b) ? a : b;
}

template <class InputIt, class UnaryPred>
LIBC_INLINE constexpr InputIt find_if_not(InputIt first, InputIt last,
UnaryPred q) {
for (; first != last; ++first)
if (!q(*first))
return first;

return last;
}

template <class InputIt, class UnaryPred>
LIBC_INLINE constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
return find_if_not(first, last, p) == last;
}

} // namespace cpp
} // namespace LIBC_NAMESPACE

Expand Down
10 changes: 10 additions & 0 deletions libc/test/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
add_custom_target(libc-cpp-utils-tests)

add_libc_test(
algorithm_test
SUITE
libc-cpp-utils-tests
SRCS
algorithm_test.cpp
DEPENDS
libc.src.__support.CPP.algorithm
)

add_libc_test(
array_test
SUITE
Expand Down
47 changes: 47 additions & 0 deletions libc/test/src/__support/CPP/algorithm_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===-- Unittests for Algorithm -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/algorithm.h"
#include "src/__support/CPP/array.h"
#include "test/UnitTest/Test.h"

// TODO(https://github.com/llvm/llvm-project/issues/94066): Add unittests for
// the remaining algorithm functions.
namespace LIBC_NAMESPACE::cpp {

TEST(LlvmLibcAlgorithmTest, FindIfNot) {
array<int, 4> nums{1, 2, 3, 4};
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 0; }),
nums.begin());
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 1; }),
nums.begin() + 1);
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 4; }),
nums.begin() + 3);
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 5; }),
nums.end());

EXPECT_EQ(
find_if_not(nums.begin() + 1, nums.end(), [](int i) { return i == 0; }),
nums.begin() + 1);
EXPECT_EQ(
find_if_not(nums.begin(), nums.begin(), [](int i) { return i == 0; }),
nums.begin());
}

TEST(LlvmLibcAlgorithmTest, AllOf) {
array<int, 4> nums{1, 2, 3, 4};
EXPECT_TRUE(all_of(nums.begin(), nums.end(), [](int i) { return i < 5; }));
EXPECT_FALSE(all_of(nums.begin(), nums.end(), [](int i) { return i < 4; }));
EXPECT_TRUE(
all_of(nums.begin(), nums.begin() + 3, [](int i) { return i < 4; }));
EXPECT_TRUE(
all_of(nums.begin() + 1, nums.end(), [](int i) { return i > 1; }));
EXPECT_TRUE(all_of(nums.begin(), nums.begin(), [](int i) { return i < 0; }));
}

} // namespace LIBC_NAMESPACE::cpp
Loading