mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-02 15:31:08 +02:00
C++17 algorithms
This commit is contained in:
@ -12,7 +12,7 @@ import testing ;
|
||||
alias unit_test_framework
|
||||
: # sources
|
||||
/boost//unit_test_framework
|
||||
;
|
||||
;
|
||||
|
||||
|
||||
{
|
||||
@ -31,13 +31,13 @@ alias unit_test_framework
|
||||
[ run clamp_test.cpp unit_test_framework : : : : clamp_test ]
|
||||
[ run power_test.cpp unit_test_framework : : : : power_test ]
|
||||
[ compile-fail power_fail1.cpp : : : : ]
|
||||
|
||||
|
||||
# Cxx11 tests
|
||||
[ run all_of_test.cpp unit_test_framework : : : : all_of_test ]
|
||||
[ run any_of_test.cpp unit_test_framework : : : : any_of_test ]
|
||||
[ run none_of_test.cpp unit_test_framework : : : : none_of_test ]
|
||||
[ run one_of_test.cpp unit_test_framework : : : : one_of_test ]
|
||||
|
||||
|
||||
[ run ordered_test.cpp unit_test_framework : : : : ordered_test ]
|
||||
[ run find_if_not_test1.cpp unit_test_framework : : : : find_if_not_test1 ]
|
||||
[ run copy_if_test1.cpp unit_test_framework : : : : copy_if_test1 ]
|
||||
@ -53,6 +53,16 @@ alias unit_test_framework
|
||||
[ run equal_test.cpp unit_test_framework : : : : equal_test ]
|
||||
[ run mismatch_test.cpp unit_test_framework : : : : mismatch_test ]
|
||||
|
||||
# Cxx17 tests
|
||||
[ run for_each_n_test.cpp unit_test_framework : : : : for_each_n_test ]
|
||||
[ run reduce_test.cpp unit_test_framework : : : : reduce_test ]
|
||||
[ run transform_reduce_test.cpp unit_test_framework : : : : transform_reduce_test ]
|
||||
[ run inclusive_scan_test.cpp unit_test_framework : : : : inclusive_scan_test ]
|
||||
[ run exclusive_scan_test.cpp unit_test_framework : : : : exclusive_scan_test ]
|
||||
[ run transform_inclusive_scan_test.cpp unit_test_framework : : : : transform_inclusive_scan_test ]
|
||||
[ run transform_exclusive_scan_test.cpp unit_test_framework : : : : transform_exclusive_scan_test ]
|
||||
# Maybe GCD and LCM as well
|
||||
|
||||
# Hex tests
|
||||
[ run hex_test1.cpp unit_test_framework : : : : hex_test1 ]
|
||||
[ run hex_test2.cpp unit_test_framework : : : : hex_test2 ]
|
||||
@ -70,7 +80,7 @@ alias unit_test_framework
|
||||
|
||||
# Is_palindrome tests
|
||||
[ run is_palindrome_test.cpp unit_test_framework : : : : is_palindrome_test ]
|
||||
|
||||
|
||||
# Is_partitioned_until tests
|
||||
[ run is_partitioned_until_test.cpp unit_test_framework : : : : is_partitioned_until_test ]
|
||||
;
|
||||
|
72
test/exclusive_scan_test.cpp
Normal file
72
test/exclusive_scan_test.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2017.
|
||||
|
||||
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 <vector>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx11/iota.hpp>
|
||||
#include <boost/algorithm/cxx17/exclusive_scan.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
int triangle(int n) { return n*(n+1)/2; }
|
||||
|
||||
void basic_tests_init()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 50);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 50 + (int) i * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 30);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 30 + triangle(i-1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::exclusive_scan(v.begin(), v.end(), v.begin(), 40);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 40 + triangle(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void test_exclusive_scan_init()
|
||||
{
|
||||
basic_tests_init();
|
||||
}
|
||||
|
||||
void test_exclusive_scan_init_op()
|
||||
{
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_exclusive_scan_init();
|
||||
test_exclusive_scan_init_op();
|
||||
}
|
66
test/for_each_n_test.cpp
Normal file
66
test/for_each_n_test.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
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 <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx17/for_each_n.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
struct for_each_test
|
||||
{
|
||||
for_each_test() {}
|
||||
static int count;
|
||||
void operator()(int& i) {++i; ++count;}
|
||||
};
|
||||
|
||||
int for_each_test::count = 0;
|
||||
|
||||
void test_for_each_n ()
|
||||
{
|
||||
typedef input_iterator<int*> Iter;
|
||||
int ia[] = {0, 1, 2, 3, 4, 5};
|
||||
const unsigned s = sizeof(ia)/sizeof(ia[0]);
|
||||
|
||||
{
|
||||
for_each_test::count = 0;
|
||||
Iter it = ba::for_each_n(Iter(ia), 0, for_each_test());
|
||||
BOOST_CHECK(it == Iter(ia));
|
||||
BOOST_CHECK(for_each_test::count == 0);
|
||||
}
|
||||
|
||||
{
|
||||
for_each_test::count = 0;
|
||||
Iter it = ba::for_each_n(Iter(ia), s, for_each_test());
|
||||
|
||||
BOOST_CHECK(it == Iter(ia+s));
|
||||
BOOST_CHECK(for_each_test::count == s);
|
||||
for (unsigned i = 0; i < s; ++i)
|
||||
BOOST_CHECK(ia[i] == static_cast<int>(i+1));
|
||||
}
|
||||
|
||||
{
|
||||
for_each_test::count = 0;
|
||||
Iter it = ba::for_each_n(Iter(ia), 1, for_each_test());
|
||||
|
||||
BOOST_CHECK(it == Iter(ia+1));
|
||||
BOOST_CHECK(for_each_test::count == 1);
|
||||
for (unsigned i = 0; i < 1; ++i)
|
||||
BOOST_CHECK(ia[i] == static_cast<int>(i+2));
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_for_each_n ();
|
||||
}
|
153
test/inclusive_scan_test.cpp
Normal file
153
test/inclusive_scan_test.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2017.
|
||||
|
||||
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 <vector>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx11/iota.hpp>
|
||||
#include <boost/algorithm/cxx17/inclusive_scan.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
int triangle(int n) { return n*(n+1)/2; }
|
||||
|
||||
void basic_tests_op()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == (int)(i+1) * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == triangle(i + 1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>());
|
||||
assert(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
void test_inclusive_scan_op()
|
||||
{
|
||||
basic_tests_op();
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void basic_tests_init()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == 50 + (int)(i+1) * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == 40 + triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
assert(v[i] == 30 + triangle(i + 1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
|
||||
assert(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void test_inclusive_scan_init()
|
||||
{
|
||||
basic_tests_init();
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
void basic_tests_op_init()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 50 + (int)(i+1) * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 40 + triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 30 + triangle(i + 1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
|
||||
BOOST_CHECK(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
void test_inclusive_scan_op_init()
|
||||
{
|
||||
basic_tests_op_init();
|
||||
BOOST_CHECK(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_inclusive_scan_op();
|
||||
test_inclusive_scan_init();
|
||||
test_inclusive_scan_op_init();
|
||||
}
|
128
test/reduce_test.cpp
Normal file
128
test/reduce_test.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
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 <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx17/reduce.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
template <class Iter, class T, class Op>
|
||||
void
|
||||
test_reduce(Iter first, Iter last, T init, Op op, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::reduce(first, last, init, op) == x);
|
||||
}
|
||||
|
||||
template <class Iter, class T, class Op>
|
||||
void
|
||||
test_reduce(Iter first, Iter last, Op op, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::reduce(first, last, op) == x);
|
||||
}
|
||||
|
||||
template <class Iter, class T>
|
||||
void
|
||||
test_reduce(Iter first, Iter last, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::reduce(first, last) == x);
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
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<int>(), 0);
|
||||
test_reduce(Iter(ia), Iter(ia), 1, std::multiplies<int>(), 1);
|
||||
test_reduce(Iter(ia), Iter(ia+1), 0, std::plus<int>(), 1);
|
||||
test_reduce(Iter(ia), Iter(ia+1), 2, std::multiplies<int>(), 2);
|
||||
test_reduce(Iter(ia), Iter(ia+2), 0, std::plus<int>(), 3);
|
||||
test_reduce(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), 6);
|
||||
test_reduce(Iter(ia), Iter(ia+sa), 0, std::plus<int>(), 21);
|
||||
test_reduce(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), 2880);
|
||||
}
|
||||
|
||||
void test_reduce_init_op()
|
||||
{
|
||||
test_init_op<input_iterator<const int*> >();
|
||||
test_init_op<forward_iterator<const int*> >();
|
||||
test_init_op<bidirectional_iterator<const int*> >();
|
||||
test_init_op<random_access_iterator<const int*> >();
|
||||
test_init_op<const int*>();
|
||||
|
||||
{
|
||||
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<unsigned>());
|
||||
BOOST_CHECK(res == 40320); // 8! will not fit into a char
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
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<input_iterator<const int*> >();
|
||||
test_init<forward_iterator<const int*> >();
|
||||
test_init<bidirectional_iterator<const int*> >();
|
||||
test_init<random_access_iterator<const int*> >();
|
||||
test_init<const int*>();
|
||||
}
|
||||
|
||||
|
||||
template <class Iter>
|
||||
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<input_iterator<const int*> >();
|
||||
test<forward_iterator<const int*> >();
|
||||
test<bidirectional_iterator<const int*> >();
|
||||
test<random_access_iterator<const int*> >();
|
||||
test<const int*>();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_reduce();
|
||||
test_reduce_init();
|
||||
test_reduce_init_op();
|
||||
}
|
137
test/transform_exclusive_scan_test.cpp
Normal file
137
test/transform_exclusive_scan_test.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2017.
|
||||
|
||||
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 <vector>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx11/iota.hpp>
|
||||
#include <boost/algorithm/cxx17/transform_exclusive_scan.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
int triangle(int n) { return n*(n+1)/2; }
|
||||
|
||||
template <class _Tp>
|
||||
struct identity
|
||||
{
|
||||
const _Tp& operator()(const _Tp& __x) const { return __x;}
|
||||
};
|
||||
|
||||
|
||||
template <class Iter1, class BOp, class UOp, class T, class Iter2>
|
||||
void
|
||||
test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
|
||||
{
|
||||
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
|
||||
// Test not in-place
|
||||
ba::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
|
||||
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
|
||||
// Test in-place
|
||||
v.clear();
|
||||
v.assign(first, last);
|
||||
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
|
||||
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
}
|
||||
|
||||
|
||||
template <class Iter>
|
||||
void
|
||||
test()
|
||||
{
|
||||
int ia[] = { 1, 3, 5, 7, 9};
|
||||
const int pResI0[] = { 0, 1, 4, 9, 16}; // with identity
|
||||
const int mResI0[] = { 0, 0, 0, 0, 0};
|
||||
const int pResN0[] = { 0, -1, -4, -9, -16}; // with negate
|
||||
const int mResN0[] = { 0, 0, 0, 0, 0};
|
||||
const int pResI2[] = { 2, 3, 6, 11, 18}; // with identity
|
||||
const int mResI2[] = { 2, 2, 6, 30, 210};
|
||||
const int pResN2[] = { 2, 1, -2, -7, -14}; // with negate
|
||||
const int mResN2[] = { 2, -2, 6, -30, 210};
|
||||
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
|
||||
|
||||
for (unsigned int i = 0; i < sa; ++i ) {
|
||||
test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
|
||||
test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
|
||||
}
|
||||
}
|
||||
|
||||
void basic_tests()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 50 + (int) i * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 30 + triangle(i-1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 40 + triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<int>(), identity<int>());
|
||||
BOOST_CHECK(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void test_transform_exclusive_scan_init()
|
||||
{
|
||||
basic_tests();
|
||||
// All the iterator categories
|
||||
test<input_iterator <const int*> >();
|
||||
test<forward_iterator <const int*> >();
|
||||
test<bidirectional_iterator<const int*> >();
|
||||
test<random_access_iterator<const int*> >();
|
||||
test<const int*>();
|
||||
test< int*>();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_transform_exclusive_scan_init();
|
||||
}
|
233
test/transform_inclusive_scan_test.cpp
Normal file
233
test/transform_inclusive_scan_test.cpp
Normal file
@ -0,0 +1,233 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2017.
|
||||
|
||||
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 <vector>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx11/iota.hpp>
|
||||
#include <boost/algorithm/cxx17/transform_inclusive_scan.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
int triangle(int n) { return n*(n+1)/2; }
|
||||
|
||||
template <class _Tp>
|
||||
struct identity
|
||||
{
|
||||
const _Tp& operator()(const _Tp& __x) const { return __x;}
|
||||
};
|
||||
|
||||
|
||||
template <class Iter1, class BOp, class UOp, class Iter2>
|
||||
void
|
||||
transform_inclusive_scan_test(Iter1 first, Iter1 last, BOp bop, UOp uop, Iter2 rFirst, Iter2 rLast)
|
||||
{
|
||||
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
|
||||
// Test not in-place
|
||||
ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop);
|
||||
BOOST_CHECK(std::distance(first, last) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
|
||||
// Test in-place
|
||||
v.clear();
|
||||
v.assign(first, last);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop);
|
||||
BOOST_CHECK(std::distance(first, last) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
}
|
||||
|
||||
|
||||
template <class Iter>
|
||||
void
|
||||
transform_inclusive_scan_test()
|
||||
{
|
||||
int ia[] = { 1, 3, 5, 7, 9};
|
||||
const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity
|
||||
const int mResI0[] = { 1, 3, 15, 105, 945};
|
||||
const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate
|
||||
const int mResN0[] = { -1, 3, -15, 105, -945};
|
||||
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
|
||||
|
||||
for (unsigned int i = 0; i < sa; ++i ) {
|
||||
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), pResI0, pResI0 + i);
|
||||
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), mResI0, mResI0 + i);
|
||||
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), pResN0, pResN0 + i);
|
||||
transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), mResN0, mResN0 + i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Basic sanity
|
||||
void basic_transform_inclusive_scan_tests()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == (int)(i+1) * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>());
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == triangle(i + 1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), identity<int>());
|
||||
BOOST_CHECK(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
void test_transform_inclusive_scan()
|
||||
{
|
||||
basic_transform_inclusive_scan_tests();
|
||||
|
||||
// All the iterator categories
|
||||
transform_inclusive_scan_test<input_iterator <const int*> >();
|
||||
transform_inclusive_scan_test<forward_iterator <const int*> >();
|
||||
transform_inclusive_scan_test<bidirectional_iterator<const int*> >();
|
||||
transform_inclusive_scan_test<random_access_iterator<const int*> >();
|
||||
transform_inclusive_scan_test<const int*>();
|
||||
transform_inclusive_scan_test< int*>();
|
||||
}
|
||||
|
||||
|
||||
template <class Iter1, class BOp, class UOp, class T, class Iter2>
|
||||
void
|
||||
transform_inclusive_scan_init_test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
|
||||
{
|
||||
std::vector<typename std::iterator_traits<Iter1>::value_type> v;
|
||||
// Test not in-place
|
||||
ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop, init);
|
||||
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
|
||||
// Test in-place
|
||||
v.clear();
|
||||
v.assign(first, last);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop, init);
|
||||
BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
|
||||
BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
|
||||
}
|
||||
|
||||
|
||||
template <class Iter>
|
||||
void
|
||||
transform_inclusive_scan_init_test()
|
||||
{
|
||||
int ia[] = { 1, 3, 5, 7, 9};
|
||||
const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity
|
||||
const int mResI0[] = { 0, 0, 0, 0, 0};
|
||||
const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate
|
||||
const int mResN0[] = { 0, 0, 0, 0, 0};
|
||||
const int pResI2[] = { 3, 6, 11, 18, 27}; // with identity
|
||||
const int mResI2[] = { 2, 6, 30, 210, 1890};
|
||||
const int pResN2[] = { 1, -2, -7, -14, -23}; // with negate
|
||||
const int mResN2[] = { -2, 6, -30, 210, -1890};
|
||||
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
|
||||
BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
|
||||
|
||||
for (unsigned int i = 0; i < sa; ++i ) {
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
|
||||
transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Basic sanity
|
||||
void basic_transform_inclusive_scan_init_tests()
|
||||
{
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
std::fill(v.begin(), v.end(), 3);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 50);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 50 + (int) (i + 1) * 3);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 0);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 30);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 30 + triangle(i));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v(10);
|
||||
ba::iota(v.begin(), v.end(), 1);
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), identity<int>(), 40);
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
BOOST_CHECK(v[i] == 40 + triangle(i + 1));
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> v, res;
|
||||
ba::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), identity<int>(), 1);
|
||||
BOOST_CHECK(res.empty());
|
||||
}
|
||||
}
|
||||
|
||||
void test_transform_inclusive_scan_init()
|
||||
{
|
||||
basic_transform_inclusive_scan_init_tests();
|
||||
|
||||
// All the iterator categories
|
||||
transform_inclusive_scan_init_test<input_iterator <const int*> >();
|
||||
transform_inclusive_scan_init_test<forward_iterator <const int*> >();
|
||||
transform_inclusive_scan_init_test<bidirectional_iterator<const int*> >();
|
||||
transform_inclusive_scan_init_test<random_access_iterator<const int*> >();
|
||||
transform_inclusive_scan_init_test<const int*>();
|
||||
transform_inclusive_scan_init_test< int*>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_transform_inclusive_scan();
|
||||
test_transform_inclusive_scan_init();
|
||||
}
|
188
test/transform_reduce_test.cpp
Normal file
188
test/transform_reduce_test.cpp
Normal file
@ -0,0 +1,188 @@
|
||||
/*
|
||||
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 <boost/config.hpp>
|
||||
#include <boost/algorithm/cxx17/transform_reduce.hpp>
|
||||
|
||||
#include "iterator_test.hpp"
|
||||
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
template <class _Tp>
|
||||
struct identity
|
||||
{
|
||||
const _Tp& operator()(const _Tp& __x) const { return __x;}
|
||||
};
|
||||
|
||||
template <class _Tp>
|
||||
struct twice
|
||||
{
|
||||
const _Tp operator()(const _Tp& __x) const { return 2 * __x; }
|
||||
};
|
||||
|
||||
|
||||
template <class Iter1, class T, class BOp, class UOp>
|
||||
void
|
||||
test_init_bop_uop(Iter1 first1, Iter1 last1, T init, BOp bOp, UOp uOp, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::transform_reduce(first1, last1, init, bOp, uOp) == x);
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
void
|
||||
test_init_bop_uop()
|
||||
{
|
||||
int ia[] = {1, 2, 3, 4, 5, 6};
|
||||
unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
|
||||
test_init_bop_uop(Iter(ia), Iter(ia), 0, std::plus<int>(), identity<int>(), 0);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia), 1, std::multiplies<int>(), identity<int>(), 1);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+1), 0, std::multiplies<int>(), identity<int>(), 0);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+1), 2, std::plus<int>(), identity<int>(), 3);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+2), 0, std::plus<int>(), identity<int>(), 3);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), identity<int>(), 6);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), identity<int>(), 2880);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::plus<int>(), identity<int>(), 25);
|
||||
|
||||
test_init_bop_uop(Iter(ia), Iter(ia), 0, std::plus<int>(), twice<int>(), 0);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia), 1, std::multiplies<int>(), twice<int>(), 1);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+1), 0, std::multiplies<int>(), twice<int>(), 0);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+1), 2, std::plus<int>(), twice<int>(), 4);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+2), 0, std::plus<int>(), twice<int>(), 6);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), twice<int>(), 24);
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), twice<int>(), 184320); // 64 * 2880
|
||||
test_init_bop_uop(Iter(ia), Iter(ia+sa), 4, std::plus<int>(), twice<int>(), 46);
|
||||
}
|
||||
|
||||
void test_transform_reduce_init_bop_uop()
|
||||
{
|
||||
BOOST_CHECK ( true );
|
||||
}
|
||||
|
||||
template <class Iter1, class Iter2, class T, class Op1, class Op2>
|
||||
void
|
||||
test_init_bop_bop(Iter1 first1, Iter1 last1, Iter2 first2, T init, Op1 op1, Op2 op2, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::transform_reduce(first1, last1, first2, init, op1, op2) == x);
|
||||
}
|
||||
|
||||
template <class SIter, class UIter>
|
||||
void
|
||||
test_init_bop_bop()
|
||||
{
|
||||
int ia[] = {1, 2, 3, 4, 5, 6};
|
||||
unsigned int ua[] = {2, 4, 6, 8, 10,12};
|
||||
unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
BOOST_CHECK(sa == sizeof(ua) / sizeof(ua[0])); // just to be sure
|
||||
|
||||
test_init_bop_bop(SIter(ia), SIter(ia), UIter(ua), 0, std::plus<int>(), std::multiplies<int>(), 0);
|
||||
test_init_bop_bop(UIter(ua), UIter(ua), SIter(ia), 1, std::multiplies<int>(), std::plus<int>(), 1);
|
||||
test_init_bop_bop(SIter(ia), SIter(ia+1), UIter(ua), 0, std::multiplies<int>(), std::plus<int>(), 0);
|
||||
test_init_bop_bop(UIter(ua), UIter(ua+1), SIter(ia), 2, std::plus<int>(), std::multiplies<int>(), 4);
|
||||
test_init_bop_bop(SIter(ia), SIter(ia+2), UIter(ua), 0, std::plus<int>(), std::multiplies<int>(), 10);
|
||||
test_init_bop_bop(UIter(ua), UIter(ua+2), SIter(ia), 3, std::multiplies<int>(), std::plus<int>(), 54);
|
||||
test_init_bop_bop(SIter(ia), SIter(ia+sa), UIter(ua), 4, std::multiplies<int>(), std::plus<int>(), 2099520);
|
||||
test_init_bop_bop(UIter(ua), UIter(ua+sa), SIter(ia), 4, std::plus<int>(), std::multiplies<int>(), 186);
|
||||
}
|
||||
|
||||
void test_transform_reduce_init_bop_bop()
|
||||
{
|
||||
// All the iterator categories
|
||||
test_init_bop_bop<input_iterator <const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<input_iterator <const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<input_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init_bop_bop<input_iterator <const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init_bop_bop<forward_iterator <const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<forward_iterator <const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<forward_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init_bop_bop<forward_iterator <const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init_bop_bop<bidirectional_iterator<const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<bidirectional_iterator<const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<bidirectional_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init_bop_bop<bidirectional_iterator<const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init_bop_bop<random_access_iterator<const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<random_access_iterator<const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init_bop_bop<random_access_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init_bop_bop<random_access_iterator<const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
// just plain pointers (const vs. non-const, too)
|
||||
test_init_bop_bop<const int*, const unsigned int *>();
|
||||
test_init_bop_bop<const int*, unsigned int *>();
|
||||
test_init_bop_bop< int*, const unsigned int *>();
|
||||
test_init_bop_bop< int*, unsigned int *>();
|
||||
}
|
||||
|
||||
template <class Iter1, class Iter2, class T>
|
||||
void
|
||||
test_init(Iter1 first1, Iter1 last1, Iter2 first2, T init, T x)
|
||||
{
|
||||
BOOST_CHECK(ba::transform_reduce(first1, last1, first2, init) == x);
|
||||
}
|
||||
|
||||
template <class SIter, class UIter>
|
||||
void
|
||||
test_init()
|
||||
{
|
||||
int ia[] = {1, 2, 3, 4, 5, 6};
|
||||
unsigned int ua[] = {2, 4, 6, 8, 10,12};
|
||||
unsigned sa = sizeof(ia) / sizeof(ia[0]);
|
||||
BOOST_CHECK(sa == sizeof(ua) / sizeof(ua[0])); // just to be sure
|
||||
|
||||
test_init(SIter(ia), SIter(ia), UIter(ua), 0, 0);
|
||||
test_init(UIter(ua), UIter(ua), SIter(ia), 1, 1);
|
||||
test_init(SIter(ia), SIter(ia+1), UIter(ua), 0, 2);
|
||||
test_init(UIter(ua), UIter(ua+1), SIter(ia), 2, 4);
|
||||
test_init(SIter(ia), SIter(ia+2), UIter(ua), 0, 10);
|
||||
test_init(UIter(ua), UIter(ua+2), SIter(ia), 3, 13);
|
||||
test_init(SIter(ia), SIter(ia+sa), UIter(ua), 0, 182);
|
||||
test_init(UIter(ua), UIter(ua+sa), SIter(ia), 4, 186);
|
||||
}
|
||||
|
||||
void test_transform_reduce_init()
|
||||
{
|
||||
// All the iterator categories
|
||||
test_init<input_iterator <const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init<input_iterator <const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init<input_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init<input_iterator <const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init<forward_iterator <const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init<forward_iterator <const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init<forward_iterator <const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init<forward_iterator <const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init<bidirectional_iterator<const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init<bidirectional_iterator<const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init<bidirectional_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init<bidirectional_iterator<const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
test_init<random_access_iterator<const int*>, input_iterator <const unsigned int*> >();
|
||||
test_init<random_access_iterator<const int*>, forward_iterator <const unsigned int*> >();
|
||||
test_init<random_access_iterator<const int*>, bidirectional_iterator<const unsigned int*> >();
|
||||
test_init<random_access_iterator<const int*>, random_access_iterator<const unsigned int*> >();
|
||||
|
||||
// just plain pointers (const vs. non-const, too)
|
||||
test_init<const int*, const unsigned int *>();
|
||||
test_init<const int*, unsigned int *>();
|
||||
test_init< int*, const unsigned int *>();
|
||||
test_init< int*, unsigned int *>();
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( test_main )
|
||||
{
|
||||
test_transform_reduce_init();
|
||||
test_transform_reduce_init_bop_uop();
|
||||
test_transform_reduce_init_bop_bop();
|
||||
}
|
Reference in New Issue
Block a user