// Range v3 library // // Copyright Eric Niebler 2014-present // Copyright Casey Carter 2015 // // 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 #include #include #include #include "../simple_test.hpp" #include "../test_utils.hpp" #include "../test_iterators.hpp" RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS namespace { std::mt19937 gen; template void test_iter(Iter first, Sent last) { RANGES_ENSURE(first != last); auto rng = ranges::make_subrange(first, last); auto v = ranges::max(rng); for (Iter i = first; i != last; ++i) CHECK(!(v < *i)); } template void test_iter(unsigned N) { RANGES_ENSURE(N > 0); std::unique_ptr a{new int[N]}; std::iota(a.get(), a.get()+N, 0); std::shuffle(a.get(), a.get()+N, gen); test_iter(Iter(a.get()), Sent(a.get()+N)); } template void test_iter() { test_iter(1); test_iter(2); test_iter(3); test_iter(10); test_iter(1000); } template void test_iter_comp(Iter first, Sent last) { RANGES_ENSURE(first != last); auto rng = ranges::make_subrange(first, last); auto comp = std::greater(); auto v = ranges::max(rng, comp); for (Iter i = first; i != last; ++i) CHECK(!comp(v, *i)); } template void test_iter_comp(unsigned N) { RANGES_ENSURE(N > 0); std::unique_ptr a{new int[N]}; std::iota(a.get(), a.get()+N, 0); std::shuffle(a.get(), a.get()+N, gen); test_iter_comp(Iter(a.get()), Sent(a.get()+N)); } template void test_iter_comp() { test_iter_comp(1); test_iter_comp(2); test_iter_comp(3); test_iter_comp(10); test_iter_comp(1000); } struct S { int i; }; } int main() { test_iter >(); test_iter >(); test_iter >(); test_iter >(); test_iter(); test_iter, Sentinel>(); test_iter, Sentinel>(); test_iter, Sentinel>(); test_iter, Sentinel>(); test_iter_comp >(); test_iter_comp >(); test_iter_comp >(); test_iter_comp >(); test_iter_comp(); test_iter_comp, Sentinel>(); test_iter_comp, Sentinel>(); test_iter_comp, Sentinel>(); test_iter_comp, Sentinel>(); // Works with projections? S s[] = {S{1},S{2},S{3},S{4},S{40},S{5},S{6},S{7},S{8},S{9}}; S v = ranges::max(s, std::less{}, &S::i); CHECK(v.i == 40); // Works with initializer_lists? (Regression test for #1004) CHECK(ranges::max({4,3,1,2,6,5}) == 6); return test_result(); }