More constexpr algorithms from P0202. search/search_n

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@322566 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2018-01-16 15:48:27 +00:00
parent 661dff0e60
commit d6082200f6
7 changed files with 101 additions and 26 deletions

View File

@@ -11,7 +11,7 @@
// template<ForwardIterator Iter1, ForwardIterator Iter2>
// requires HasEqualTo<Iter1::value_type, Iter2::value_type>
// Iter1
// constexpr Iter1 // constexpr after C++17
// search(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
//
// template<class ForwardIterator, class Searcher>
@@ -21,8 +21,30 @@
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
struct MySearcherC {
template <typename Iterator>
std::pair<Iterator, Iterator>
TEST_CONSTEXPR operator() (Iterator b, Iterator e) const
{
return std::make_pair(b, e);
}
};
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3};
int ib[] = {0, 1, 5, 3};
int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
return (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia)) == ic+3)
&& (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib)) == std::end(ic))
&& (std::search(std::begin(ic), std::end(ic), MySearcherC()) == std::begin(ic))
;
}
#endif
int searcher_called = 0;
struct MySearcher {
@@ -97,4 +119,7 @@ int main()
}
#endif
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}

View File

@@ -10,16 +10,27 @@
// <algorithm>
// template<class ForwardIterator, class Size, class T>
// ForwardIterator
// constexpr ForwardIterator // constexpr after C++17
// search_n(ForwardIterator first, ForwardIterator last, Size count,
// const T& value);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "user_defined_integral.hpp"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 0, 1, 1, 2, 2};
return (std::search_n(std::begin(ia), std::end(ia), 1, 0) == ia)
&& (std::search_n(std::begin(ia), std::end(ia), 2, 1) == ia+2)
&& (std::search_n(std::begin(ia), std::end(ia), 1, 3) == std::end(ia))
;
}
#endif
template <class Iter>
void
test()
@@ -74,4 +85,8 @@ int main()
test<forward_iterator<const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}

View File

@@ -10,16 +10,29 @@
// <algorithm>
// template<class ForwardIterator, class Size, class T, class BinaryPredicate>
// ForwardIterator
// constexpr ForwardIterator // constexpr after C++17
// search_n(ForwardIterator first, ForwardIterator last, Size count,
// const T& value, BinaryPredicate pred);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "user_defined_integral.hpp"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 0, 1, 1, 2, 2};
return (std::search_n(std::begin(ia), std::end(ia), 1, 0, eq) == ia)
&& (std::search_n(std::begin(ia), std::end(ia), 2, 1, eq) == ia+2)
&& (std::search_n(std::begin(ia), std::end(ia), 1, 3, eq) == std::end(ia))
;
}
#endif
struct count_equal
{
static unsigned count;
@@ -151,4 +164,8 @@ int main()
test<forward_iterator<const int*> >();
test<bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*> >();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}

View File

@@ -11,14 +11,28 @@
// template<ForwardIterator Iter1, ForwardIterator Iter2>
// requires HasEqualTo<Iter1::value_type, Iter2::value_type>
// Iter1
// constexpr Iter1 // constexpr after C++17
// search(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {0, 1, 2, 3};
int ib[] = {0, 1, 5, 3};
int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
return (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia), eq) == ic+3)
&& (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib), eq) == std::end(ic))
;
}
#endif
struct count_equal
{
static unsigned count;
@@ -108,4 +122,8 @@ int main()
test<random_access_iterator<const int*>, forward_iterator<const int*> >();
test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}