xref: /llvm-project/libc/test/src/__support/CPP/algorithm_test.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Unittests for Algorithm -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/CPP/algorithm.h"
10 #include "src/__support/CPP/array.h"
11 #include "src/__support/macros/config.h"
12 #include "test/UnitTest/Test.h"
13 
14 // TODO(https://github.com/llvm/llvm-project/issues/94066): Add unittests for
15 // the remaining algorithm functions.
16 namespace LIBC_NAMESPACE_DECL {
17 namespace cpp {
18 
19 TEST(LlvmLibcAlgorithmTest, FindIfNot) {
20   array<int, 4> nums{1, 2, 3, 4};
21   EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 0; }),
22             nums.begin());
23   EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 1; }),
24             nums.begin() + 1);
25   EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 4; }),
26             nums.begin() + 3);
27   EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 5; }),
28             nums.end());
29 
30   EXPECT_EQ(
31       find_if_not(nums.begin() + 1, nums.end(), [](int i) { return i == 0; }),
32       nums.begin() + 1);
33   EXPECT_EQ(
34       find_if_not(nums.begin(), nums.begin(), [](int i) { return i == 0; }),
35       nums.begin());
36 }
37 
38 TEST(LlvmLibcAlgorithmTest, AllOf) {
39   array<int, 4> nums{1, 2, 3, 4};
40   EXPECT_TRUE(all_of(nums.begin(), nums.end(), [](int i) { return i < 5; }));
41   EXPECT_FALSE(all_of(nums.begin(), nums.end(), [](int i) { return i < 4; }));
42   EXPECT_TRUE(
43       all_of(nums.begin(), nums.begin() + 3, [](int i) { return i < 4; }));
44   EXPECT_TRUE(
45       all_of(nums.begin() + 1, nums.end(), [](int i) { return i > 1; }));
46   EXPECT_TRUE(all_of(nums.begin(), nums.begin(), [](int i) { return i < 0; }));
47 }
48 
49 } // namespace cpp
50 } // namespace LIBC_NAMESPACE_DECL
51