[libcxx] [test] Remove nonstandard things and resolve warnings in Xxx_scan tests

Reviewed as https://reviews.llvm.org/D41748

* These tests use function objects from functional, back_inserter from iterator, and equal from algorithm, so add those headers.
* The use of iota targeting vector<unsigned char> with an int parameter triggers warnings on MSVC++ assigning an into a unsigned char&; so change the parameter to unsigned char with a static_cast.
* Avoid naming unary_function in identity here as that is removed in '17. (This also fixes naming _VSTD, _NOEXCEPT_, and other libcxx-isms)
* Change the predicate in the transform tests to add_ten so that problems with multiple application are caught.

llvm-svn: 321922
This commit is contained in:
Billy Robert O'Neal III 2018-01-06 02:18:20 +00:00
parent a263c346e5
commit 1e1195dce5
8 changed files with 98 additions and 101 deletions

View File

@ -16,8 +16,11 @@
//
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
#include "test_iterators.h"

View File

@ -17,8 +17,11 @@
// T init, BinaryOperation binary_op); // C++17
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
#include "test_iterators.h"
@ -70,7 +73,7 @@ int main()
// Make sure that the calculations are done using the init typedef
{
std::vector<unsigned char> v(10);
std::iota(v.begin(), v.end(), 1);
std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
std::vector<int> res;
std::exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>());

View File

@ -16,8 +16,11 @@
//
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
#include "test_iterators.h"

View File

@ -17,9 +17,12 @@
// BinaryOperation binary_op); // C++17
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
#include "test_iterators.h"

View File

@ -17,8 +17,11 @@
// BinaryOperation binary_op, T init); // C++17
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
#include "test_iterators.h"
@ -95,7 +98,7 @@ void basic_tests()
// Make sure that the calculations are done using the init typedef
{
std::vector<unsigned char> v(10);
std::iota(v.begin(), v.end(), 1);
std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
std::vector<int> res;
std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::multiplies<>(), 1);

View File

@ -19,26 +19,20 @@
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
#include "test_iterators.h"
template <class T = void>
struct identity : std::unary_function<T, T>
{
constexpr const T& operator()(const T& x) const { return x;}
};
template <>
struct identity<void>
{
template <class T>
constexpr auto operator()(T&& x) const
_NOEXCEPT_(noexcept(_VSTD::forward<T>(x)))
-> decltype (_VSTD::forward<T>(x))
{ return _VSTD::forward<T>(x); }
struct add_ten {
template <typename T>
constexpr auto operator()(T x) const noexcept {
return static_cast<T>(x + 10);
}
};
template <class Iter1, class BOp, class UOp, class T, class Iter2>
@ -63,14 +57,14 @@ void
test()
{
int ia[] = { 1, 3, 5, 7, 9};
const int pResI0[] = { 0, 1, 4, 9, 16}; // with identity
const int pResI0[] = { 0, 11, 24, 39, 56}; // with add_ten
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 int pResI2[] = { 2, 13, 26, 41, 58}; // with add_ten
const int mResI2[] = { 2, 22, 286, 4290, 72930};
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]);
static_assert(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
static_assert(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
@ -82,12 +76,12 @@ test()
static_assert(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<>(), identity<>(), 0, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(), 0, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), add_ten{}, 0, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_ten{}, 0, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), 0, pResN0, pResN0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 0, mResN0, mResN0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), identity<>(), 2, pResI2, pResI2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(), 2, mResI2, mResI2 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), add_ten{}, 2, pResI2, pResI2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_ten{}, 2, mResI2, mResI2 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), 2, pResN2, pResN2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 2, mResN2, mResN2 + i);
}
@ -101,46 +95,46 @@ void basic_tests()
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<>(), identity<>());
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<>(), add_ten{});
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 50 + (int) i * 3);
assert(v[i] == 50 + (int) i * 13);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<>(), identity<>());
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<>(), add_ten{});
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 30 + triangle(i-1));
assert(v[i] == 30 + triangle(i - 1) + i * 10);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<>(), identity<>());
std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<>(), add_ten{});
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 40 + triangle(i));
assert(v[i] == 40 + triangle(i) + i * 10);
}
{
std::vector<int> v, res;
std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<>(), identity<>());
std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<>(), add_ten{});
assert(res.empty());
}
// Make sure that the calculations are done using the init typedef
{
std::vector<unsigned char> v(10);
std::iota(v.begin(), v.end(), 1);
std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
std::vector<int> res;
std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>(), identity<>());
std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>(), add_ten{});
assert(res.size() == 10);
int j = 1;
assert(res[0] == 1);
for (size_t i = 1; i < res.size(); ++i)
{
j *= i;
j *= i + 10;
assert(res[i] == j);
}
}

View File

@ -20,26 +20,20 @@
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
#include "test_iterators.h"
template <class T = void>
struct identity : std::unary_function<T, T>
{
constexpr const T& operator()(const T& x) const { return x;}
};
template <>
struct identity<void>
{
template <class T>
constexpr auto operator()(T&& x) const
_NOEXCEPT_(noexcept(_VSTD::forward<T>(x)))
-> decltype (_VSTD::forward<T>(x))
{ return _VSTD::forward<T>(x); }
struct add_ten {
template <typename T>
constexpr auto operator()(T x) const noexcept {
return static_cast<T>(x + 10);
}
};
template <class Iter1, class BOp, class UOp, class Iter2>
@ -64,8 +58,8 @@ void
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 pResI0[] = { 11, 24, 39, 56, 75}; // with add_ten
const int mResI0[] = { 11, 143, 2145, 36465, 692835};
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]);
@ -75,8 +69,8 @@ test()
static_assert(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
for (unsigned int i = 0; i < sa; ++i ) {
test(Iter(ia), Iter(ia + i), std::plus<>(), identity<>(), pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(), mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), add_ten{}, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_ten{}, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), pResN0, pResN0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), mResN0, mResN0 + i);
}
@ -90,32 +84,32 @@ void basic_tests()
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>());
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{});
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == (int)(i+1) * 3);
assert(v[i] == (int)(i+1) * 13);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>());
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{});
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == triangle(i));
assert(v[i] == triangle(i) + (i + 1) * 10);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>());
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{});
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == triangle(i + 1));
assert(v[i] == triangle(i + 1) + (i + 1) * 10);
}
{
std::vector<int> v, res;
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>(), identity<>());
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>(), add_ten{});
assert(res.empty());
}
}

View File

@ -20,25 +20,19 @@
#include <numeric>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>
#include "test_iterators.h"
template <class T = void>
struct identity : std::unary_function<T, T>
{
constexpr const T& operator()(const T& x) const { return x;}
};
template <>
struct identity<void>
{
template <class T>
constexpr auto operator()(T&& x) const
_NOEXCEPT_(noexcept(_VSTD::forward<T>(x)))
-> decltype (_VSTD::forward<T>(x))
{ return _VSTD::forward<T>(x); }
struct add_ten {
template <typename T>
constexpr auto operator()(T x) const noexcept {
return static_cast<T>(x + 10);
}
};
template <class Iter1, class BOp, class UOp, class T, class Iter2>
@ -63,12 +57,12 @@ void
test()
{
int ia[] = { 1, 3, 5, 7, 9};
const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity
const int pResI0[] = { 11, 24, 39, 56, 75}; // with add_ten
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 pResI2[] = { 13, 26, 41, 58, 77}; // with add_ten
const int mResI2[] = { 22, 286, 4290, 72930, 1385670};
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]);
@ -82,12 +76,12 @@ test()
static_assert(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<>(), identity<>(), 0, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(), 0, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), add_ten{}, 0, pResI0, pResI0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_ten{}, 0, mResI0, mResI0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), 0, pResN0, pResN0 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 0, mResN0, mResN0 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), identity<>(), 2, pResI2, pResI2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(), 2, mResI2, mResI2 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), add_ten{}, 2, pResI2, pResI2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), add_ten{}, 2, mResI2, mResI2 + i);
test(Iter(ia), Iter(ia + i), std::plus<>(), std::negate<>(), 2, pResN2, pResN2 + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 2, mResN2, mResN2 + i);
}
@ -101,46 +95,46 @@ void basic_tests()
{
std::vector<int> v(10);
std::fill(v.begin(), v.end(), 3);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>(), 50);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{}, 50);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 50 + (int) (i + 1) * 3);
assert(v[i] == 50 + (int) (i + 1) * 13);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>(), 30);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{}, 30);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 30 + triangle(i));
assert(v[i] == 30 + triangle(i) + (i + 1) * 10);
}
{
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 1);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), identity<>(), 40);
std::transform_inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<>(), add_ten{}, 40);
for (size_t i = 0; i < v.size(); ++i)
assert(v[i] == 40 + triangle(i + 1));
assert(v[i] == 40 + triangle(i + 1) + (i + 1) * 10);
}
{
std::vector<int> v, res;
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>(), identity<>(), 1);
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>(), add_ten{}, 1);
assert(res.empty());
}
// Make sure that the calculations are done using the init typedef
{
std::vector<unsigned char> v(10);
std::iota(v.begin(), v.end(), 1);
std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
std::vector<int> res;
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::multiplies<>(), identity<>(), 1);
std::transform_inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::multiplies<>(), add_ten{}, 1);
assert(res.size() == 10);
int j = 1;
assert(res[0] == 1);
int j = 11;
assert(res[0] == 11);
for (size_t i = 1; i < res.size(); ++i)
{
j *= i + 1;
j *= i + 11;
assert(res[i] == j);
}
}