xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.all_of/all_of.pass.cpp (revision 773ae4412468433c134e668b4047c94f4599e0fd)
1 //===----------------------------------------------------------------------===//
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 // <algorithm>
10 
11 // template <class InputIterator, class Predicate>
12 //     constexpr bool       // constexpr after C++17
13 //   all_of(InputIterator first, InputIterator last, Predicate pred);
14 
15 #include <algorithm>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 
21 struct test1
22 {
operator ()test123     TEST_CONSTEXPR bool operator()(const int& i) const
24     {
25         return i % 2 == 0;
26     }
27 };
28 
29 #if TEST_STD_VER > 17
test_constexpr()30 TEST_CONSTEXPR bool test_constexpr() {
31     int ia[] = {2, 4, 6, 8};
32     int ib[] = {2, 4, 5, 8};
33     return  std::all_of(std::begin(ia), std::end(ia), test1())
34         && !std::all_of(std::begin(ib), std::end(ib), test1())
35         ;
36     }
37 #endif
38 
main(int,char **)39 int main(int, char**)
40 {
41     {
42         int ia[] = {2, 4, 6, 8};
43         const unsigned sa = sizeof(ia)/sizeof(ia[0]);
44         assert(std::all_of(cpp17_input_iterator<const int*>(ia),
45                            cpp17_input_iterator<const int*>(ia + sa), test1()) == true);
46         assert(std::all_of(cpp17_input_iterator<const int*>(ia),
47                            cpp17_input_iterator<const int*>(ia), test1()) == true);
48     }
49     {
50         const int ia[] = {2, 4, 5, 8};
51         const unsigned sa = sizeof(ia)/sizeof(ia[0]);
52         assert(std::all_of(cpp17_input_iterator<const int*>(ia),
53                            cpp17_input_iterator<const int*>(ia + sa), test1()) == false);
54         assert(std::all_of(cpp17_input_iterator<const int*>(ia),
55                            cpp17_input_iterator<const int*>(ia), test1()) == true);
56     }
57 
58 #if TEST_STD_VER > 17
59     static_assert(test_constexpr());
60 #endif
61 
62   return 0;
63 }
64