Skip to content

Commit

Permalink
[libc][CPP] Add all_of and find_if_not to algorithm.h (#94058)
Browse files Browse the repository at this point in the history
This is needed for the allocator implementation for malloc.
  • Loading branch information
PiJoules committed Jun 3, 2024
1 parent 25b037b commit e44cea5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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

0 comments on commit e44cea5

Please sign in to comment.