Skip to content

Commit

Permalink
[libc++] [test] Remove epicyclic workarounds for vector/span; use T[]…
Browse files Browse the repository at this point in the history
… or std::array.

Simplify the test code, and drive-by also test that these algorithms
return the right iterator as their return value.

Differential Revision: https://reviews.llvm.org/D100876
  • Loading branch information
Arthur O'Dwyer committed Apr 21, 2021
1 parent b6db6f5 commit e9d8761
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 354 deletions.
Expand Up @@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//

// <numeric>
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: clang-8
// UNSUPPORTED: gcc-9

// <numeric>

// Became constexpr in C++20
// template<class InputIterator, class OutputIterator, class T>
// OutputIterator exclusive_scan(InputIterator first, InputIterator last,
Expand All @@ -23,55 +24,39 @@
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>

#include "test_macros.h"
#include "test_iterators.h"
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER > 17
#include <span>
#endif

template <class Iter1, class T, class Iter2>
template <class Iter1, class T>
TEST_CONSTEXPR_CXX20 void
test(Iter1 first, Iter1 last, T init, Iter2 rFirst, Iter2 rLast)
test(Iter1 first, Iter1 last, T init, const T *rFirst, const T *rLast)
{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
size_t size = std::distance(first, last);
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)

std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
#else
assert((size <= 5) && "Increment the size of the array");
typename std::iterator_traits<Iter1>::value_type b[5];
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
#endif
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
T out[5];

// Not in place
std::exclusive_scan(first, last, v.begin(), init);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// Not in place
T *end = std::exclusive_scan(first, last, out, init);
assert(std::equal(out, end, rFirst, rLast));

// In place
std::copy(first, last, v.begin());
std::exclusive_scan(v.begin(), v.end(), v.begin(), init);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// In place
std::copy(first, last, out);
end = std::exclusive_scan(out, end, out, init);
assert(std::equal(out, end, rFirst, rLast));
}

template <class Iter>
TEST_CONSTEXPR_CXX20 void
test()
{
int ia[] = {1, 3, 5, 7, 9};
int ia[] = {1, 3, 5, 7, 9};
const int pRes[] = {0, 1, 4, 9, 16};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure

for (unsigned int i = 0; i < sa; ++i )
for (unsigned int i = 0; i < sa; ++i) {
test(Iter(ia), Iter(ia + i), 0, pRes, pRes + i);
}
}

constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
Expand Down
Expand Up @@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//

// <numeric>
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: clang-8
// UNSUPPORTED: gcc-9

// <numeric>

// Became constexpr in C++20
// template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
// OutputIterator
Expand All @@ -24,50 +25,33 @@
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>

#include "test_macros.h"
#include "test_iterators.h"
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER > 17
#include <span>
#endif

template <class Iter1, class T, class Op, class Iter2>
template <class Iter1, class T, class Op>
TEST_CONSTEXPR_CXX20 void
test(Iter1 first, Iter1 last, T init, Op op, Iter2 rFirst, Iter2 rLast)
test(Iter1 first, Iter1 last, T init, Op op, const T *rFirst, const T *rLast)
{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
size_t size = std::distance(first, last);
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)

std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
#else
assert((size <= 5) && "Increment the size of the array");
typename std::iterator_traits<Iter1>::value_type b[5];
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
#endif
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
T out[5];

// Not in place
std::exclusive_scan(first, last, v.begin(), init, op);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// Not in place
T *end = std::exclusive_scan(first, last, out, init, op);
assert(std::equal(out, end, rFirst, rLast));

// In place
std::copy(first, last, v.begin());
std::exclusive_scan(v.begin(), v.end(), v.begin(), init, op);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// In place
std::copy(first, last, out);
end = std::exclusive_scan(out, end, out, init, op);
assert(std::equal(out, end, rFirst, rLast));
}


template <class Iter>
TEST_CONSTEXPR_CXX20 void
test()
{
int ia[] = {1, 3, 5, 7, 9};
int ia[] = {1, 3, 5, 7, 9};
const int pRes[] = {0, 1, 4, 9, 16};
const int mRes[] = {1, 1, 3, 15, 105};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
Expand All @@ -77,7 +61,7 @@ test()
for (unsigned int i = 0; i < sa; ++i ) {
test(Iter(ia), Iter(ia + i), 0, std::plus<>(), pRes, pRes + i);
test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i);
}
}
}

TEST_CONSTEXPR_CXX20 bool
Expand All @@ -95,18 +79,8 @@ test()
{
std::array<unsigned char, 10> v;
std::iota(v.begin(), v.end(), static_cast<unsigned char>(1));
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
std::vector<size_t> res;
std::exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>());
#else
std::array<size_t, 10> res;
std::exclusive_scan(v.begin(), v.end(), res.begin(), 1, std::multiplies<>());
#endif

assert(res.size() == 10);
size_t j = 1;
Expand Down
Expand Up @@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//

// <numeric>
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: clang-8
// UNSUPPORTED: gcc-9

// <numeric>

// Became constexpr in C++20
// template<class InputIterator, class OutputIterator, class T>
// OutputIterator inclusive_scan(InputIterator first, InputIterator last,
Expand All @@ -23,56 +24,40 @@
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>

#include "test_macros.h"
#include "test_iterators.h"
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER > 17
#include <span>
#endif

template <class Iter1, class Iter2>
template <class Iter1, class T>
TEST_CONSTEXPR_CXX20 void
test(Iter1 first, Iter1 last, Iter2 rFirst, Iter2 rLast)
test(Iter1 first, Iter1 last, const T *rFirst, const T *rLast)
{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
size_t size = std::distance(first, last);
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)

std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
#else
assert((size <= 5) && "Increment the size of the array");
typename std::iterator_traits<Iter1>::value_type b[5];
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
#endif
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
T out[5];

// Not in place
std::inclusive_scan(first, last, v.begin());
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// Not in place
T *end = std::inclusive_scan(first, last, out);
assert(std::equal(out, end, rFirst, rLast));

// In place
std::copy(first, last, v.begin());
std::inclusive_scan(v.begin(), v.end(), v.begin());
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// In place
std::copy(first, last, out);
end = std::inclusive_scan(out, end, out);
assert(std::equal(out, end, rFirst, rLast));
}


template <class Iter>
TEST_CONSTEXPR_CXX20 void
test()
{
int ia[] = {1, 3, 5, 7, 9};
int ia[] = {1, 3, 5, 7, 9};
const int pRes[] = {1, 4, 9, 16, 25};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure

for (unsigned int i = 0; i < sa; ++i )
for (unsigned int i = 0; i < sa; ++i ) {
test(Iter(ia), Iter(ia + i), pRes, pRes + i);
}
}

constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
Expand Down Expand Up @@ -106,18 +91,8 @@ basic_tests()
}

{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
std::vector<size_t> v, res;
std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res));
#else
std::array<size_t, 0> v, res;
std::inclusive_scan(v.begin(), v.end(), res.begin());
#endif
assert(res.empty());
}
}
Expand Down
Expand Up @@ -6,11 +6,12 @@
//
//===----------------------------------------------------------------------===//

// <numeric>
// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: clang-8
// UNSUPPORTED: gcc-9

// <numeric>

// Became constexpr in C++20
// template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
// OutputIterator
Expand All @@ -24,50 +25,33 @@
#include <cassert>
#include <functional>
#include <iterator>
#include <vector>

#include "test_macros.h"
#include "test_iterators.h"
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER > 17
#include <span>
#endif

template <class Iter1, class Op, class Iter2>
template <class Iter1, class Op, class T>
TEST_CONSTEXPR_CXX20 void
test(Iter1 first, Iter1 last, Op op, Iter2 rFirst, Iter2 rLast)
test(Iter1 first, Iter1 last, Op op, const T *rFirst, const T *rLast)
{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
size_t size = std::distance(first, last);
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)

std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
#else
assert((size <= 5) && "Increment the size of the array");
typename std::iterator_traits<Iter1>::value_type b[5];
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
#endif
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
T out[5];

// Not in place
std::inclusive_scan(first, last, v.begin(), op);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// Not in place
T *end = std::inclusive_scan(first, last, out, op);
assert(std::equal(out, end, rFirst, rLast));

// In place
std::copy(first, last, v.begin());
std::inclusive_scan(v.begin(), v.end(), v.begin(), op);
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
// In place
std::copy(first, last, out);
end = std::inclusive_scan(out, end, out, op);
assert(std::equal(out, end, rFirst, rLast));
}


template <class Iter>
TEST_CONSTEXPR_CXX20 void
test()
{
int ia[] = {1, 3, 5, 7, 9};
int ia[] = {1, 3, 5, 7, 9};
const int pRes[] = {1, 4, 9, 16, 25};
const int mRes[] = {1, 3, 15, 105, 945};
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
Expand All @@ -77,7 +61,7 @@ test()
for (unsigned int i = 0; i < sa; ++i ) {
test(Iter(ia), Iter(ia + i), std::plus<>(), pRes, pRes + i);
test(Iter(ia), Iter(ia + i), std::multiplies<>(), mRes, mRes + i);
}
}
}

constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
Expand Down Expand Up @@ -111,18 +95,8 @@ basic_tests()
}

{
// C++17 doesn't test constexpr so can use a vector.
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
// don't have the support yet. In these cases use a std::span for the test.
// FIXME Remove constexpr vector workaround introduced in D90569
#if TEST_STD_VER < 20 || \
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
std::vector<size_t> v, res;
std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>());
#else
std::array<size_t, 0> v, res;
std::inclusive_scan(v.begin(), v.end(), res.begin(), std::plus<>());
#endif
assert(res.empty());
}
}
Expand Down

0 comments on commit e9d8761

Please sign in to comment.