/* Copyright (c) Marshall Clow 2013. Distributed under 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) For more information, see http://www.boost.org */ #include #include #include #include #include "iterator_test.hpp" #define BOOST_TEST_MAIN #include namespace ba = boost::algorithm; template void test_reduce(Iter first, Iter last, T init, Op op, T x) { BOOST_CHECK(ba::reduce(first, last, init, op) == x); } template void test_reduce(Iter first, Iter last, Op op, T x) { BOOST_CHECK(ba::reduce(first, last, op) == x); } template void test_reduce(Iter first, Iter last, T x) { BOOST_CHECK(ba::reduce(first, last) == x); } template void test_init_op() { int ia[] = {1, 2, 3, 4, 5, 6}; unsigned sa = sizeof(ia) / sizeof(ia[0]); test_reduce(Iter(ia), Iter(ia), 0, std::plus(), 0); test_reduce(Iter(ia), Iter(ia), 1, std::multiplies(), 1); test_reduce(Iter(ia), Iter(ia+1), 0, std::plus(), 1); test_reduce(Iter(ia), Iter(ia+1), 2, std::multiplies(), 2); test_reduce(Iter(ia), Iter(ia+2), 0, std::plus(), 3); test_reduce(Iter(ia), Iter(ia+2), 3, std::multiplies(), 6); test_reduce(Iter(ia), Iter(ia+sa), 0, std::plus(), 21); test_reduce(Iter(ia), Iter(ia+sa), 4, std::multiplies(), 2880); } void test_reduce_init_op() { test_init_op >(); test_init_op >(); test_init_op >(); test_init_op >(); test_init_op(); { char ia[] = {1, 2, 3, 4, 5, 6, 7, 8}; unsigned sa = sizeof(ia) / sizeof(ia[0]); unsigned res = boost::algorithm::reduce(ia, ia+sa, 1U, std::multiplies()); BOOST_CHECK(res == 40320); // 8! will not fit into a char } } template void test_init() { int ia[] = {1, 2, 3, 4, 5, 6}; unsigned sa = sizeof(ia) / sizeof(ia[0]); test_reduce(Iter(ia), Iter(ia), 0, 0); test_reduce(Iter(ia), Iter(ia), 1, 1); test_reduce(Iter(ia), Iter(ia+1), 0, 1); test_reduce(Iter(ia), Iter(ia+1), 2, 3); test_reduce(Iter(ia), Iter(ia+2), 0, 3); test_reduce(Iter(ia), Iter(ia+2), 3, 6); test_reduce(Iter(ia), Iter(ia+sa), 0, 21); test_reduce(Iter(ia), Iter(ia+sa), 4, 25); } void test_reduce_init() { test_init >(); test_init >(); test_init >(); test_init >(); test_init(); } template void test() { int ia[] = {1, 2, 3, 4, 5, 6}; unsigned sa = sizeof(ia) / sizeof(ia[0]); test_reduce(Iter(ia), Iter(ia), 0); test_reduce(Iter(ia), Iter(ia+1), 1); test_reduce(Iter(ia), Iter(ia+2), 3); test_reduce(Iter(ia), Iter(ia+sa), 21); } void test_reduce() { test >(); test >(); test >(); test >(); test(); } BOOST_AUTO_TEST_CASE( test_main ) { test_reduce(); test_reduce_init(); test_reduce_init_op(); }