// Range v3 library // // Copyright Eric Niebler 2014-present // // Use, modification and distribution is subject to the // Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // Project home: https://github.com/ericniebler/range-v3 //===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include #include #include "../simple_test.hpp" #include "../test_iterators.hpp" #include "../test_utils.hpp" namespace rng = ranges; void test_iter() { using namespace ranges; int ia[] = {0, 1, 2, 3, 0, 1, 2, 3}; static constexpr auto sa = size(ia); int ib[] = {1, 3, 5, 7}; static constexpr auto sb = size(ib); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ib), Sentinel(ib + sb)) == InputIterator(ia+1)); int ic[] = {7}; CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ic), Sentinel(ic + 1)) == InputIterator(ia+sa)); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ic), Sentinel(ic)) == InputIterator(ia+sa)); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia), ForwardIterator(ic), Sentinel(ic+1)) == InputIterator(ia)); } void test_iter_pred() { using namespace ranges; int ia[] = {0, 1, 2, 3, 0, 1, 2, 3}; static constexpr auto sa = size(ia); int ib[] = {1, 3, 5, 7}; static constexpr auto sb = size(ib); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ib), Sentinel(ib + sb), std::equal_to()) == InputIterator(ia+1)); int ic[] = {7}; CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ic), Sentinel(ic + 1), std::equal_to()) == InputIterator(ia+sa)); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia + sa), ForwardIterator(ic), Sentinel(ic), std::equal_to()) == InputIterator(ia+sa)); CHECK(rng::find_first_of(InputIterator(ia), Sentinel(ia), ForwardIterator(ic), Sentinel(ic+1), std::equal_to()) == InputIterator(ia)); } void test_rng() { using namespace ranges; int ia[] = {0, 1, 2, 3, 0, 1, 2, 3}; static constexpr auto sa = size(ia); int ib[] = {1, 3, 5, 7}; static constexpr auto sb = size(ib); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ib), ForwardIterator(ib + sb))) == InputIterator(ia+1)); CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ib), ForwardIterator(ib + sb))))); int ic[] = {7}; CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1))) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic))) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia)), make_subrange(ForwardIterator(ic), ForwardIterator(ic+1))) == InputIterator(ia)); CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1))))); CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic))))); CHECK(::is_dangling(rng::find_first_of(::MakeTestRange(InputIterator(ia), InputIterator(ia)), make_subrange(ForwardIterator(ic), ForwardIterator(ic+1))))); } void test_rng_pred() { using namespace ranges; int ia[] = {0, 1, 2, 3, 0, 1, 2, 3}; static constexpr auto sa = size(ia); int ib[] = {1, 3, 5, 7}; static constexpr auto sb = size(ib); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ib), ForwardIterator(ib + sb)), std::equal_to()) == InputIterator(ia+1)); int ic[] = {7}; CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1)), std::equal_to()) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic)), std::equal_to()) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia)), make_subrange(ForwardIterator(ic), ForwardIterator(ic+1)), std::equal_to()) == InputIterator(ia)); } struct S { int i; }; void test_rng_pred_proj() { using namespace ranges; S ia[] = {S{0}, S{1}, S{2}, S{3}, S{0}, S{1}, S{2}, S{3}}; static constexpr auto sa = size(ia); S ib[] = {S{1}, S{3}, S{5}, S{7}}; static constexpr auto sb = size(ib); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ib), ForwardIterator(ib + sb)), std::equal_to(), &S::i, &S::i) == InputIterator(ia+1)); S ic[] = {S{7}}; CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1)), std::equal_to(), &S::i, &S::i) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia + sa)), make_subrange(ForwardIterator(ic), ForwardIterator(ic)), std::equal_to(), &S::i, &S::i) == InputIterator(ia+sa)); CHECK(rng::find_first_of(make_subrange(InputIterator(ia), InputIterator(ia)), make_subrange(ForwardIterator(ic), ForwardIterator(ic+1)), std::equal_to(), &S::i, &S::i) == InputIterator(ia)); } void test_constexpr() { using namespace ranges; constexpr int ia[] = {0, 1, 2, 3, 0, 1, 2, 3}; constexpr auto sa = size(ia); constexpr int ib[] = {1, 3, 5, 7}; constexpr auto sb = size(ib); STATIC_CHECK( rng::find_first_of(as_lvalue(make_subrange(InputIterator(ia), InputIterator(ia + sa))), make_subrange(ForwardIterator(ib), ForwardIterator(ib + sb)), equal_to{}) == InputIterator(ia + 1)); constexpr int ic[] = {7}; STATIC_CHECK( rng::find_first_of(as_lvalue(make_subrange(InputIterator(ia), InputIterator(ia + sa))), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1)), equal_to{}) == InputIterator(ia + sa)); STATIC_CHECK( rng::find_first_of(as_lvalue(make_subrange(InputIterator(ia), InputIterator(ia + sa))), make_subrange(ForwardIterator(ic), ForwardIterator(ic)), equal_to{}) == InputIterator(ia + sa)); STATIC_CHECK( rng::find_first_of(as_lvalue(make_subrange(InputIterator(ia), InputIterator(ia))), make_subrange(ForwardIterator(ic), ForwardIterator(ic + 1)), equal_to{}) == InputIterator(ia)); } int main() { ::test_iter(); ::test_iter_pred(); ::test_rng(); ::test_rng_pred(); ::test_rng_pred_proj(); return ::test_result(); }