Skip to content

Commit e9d8761

Browse files
committed
[libc++] [test] Remove epicyclic workarounds for vector/span; use T[] 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
1 parent b6db6f5 commit e9d8761

File tree

8 files changed

+126
-354
lines changed

8 files changed

+126
-354
lines changed

libcxx/test/std/numerics/numeric.ops/exclusive.scan/exclusive_scan.pass.cpp

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

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

13+
// <numeric>
14+
1415
// Became constexpr in C++20
1516
// template<class InputIterator, class OutputIterator, class T>
1617
// OutputIterator exclusive_scan(InputIterator first, InputIterator last,
@@ -23,55 +24,39 @@
2324
#include <cassert>
2425
#include <functional>
2526
#include <iterator>
26-
#include <vector>
2727

2828
#include "test_macros.h"
2929
#include "test_iterators.h"
30-
// FIXME Remove constexpr vector workaround introduced in D90569
31-
#if TEST_STD_VER > 17
32-
#include <span>
33-
#endif
3430

35-
template <class Iter1, class T, class Iter2>
31+
template <class Iter1, class T>
3632
TEST_CONSTEXPR_CXX20 void
37-
test(Iter1 first, Iter1 last, T init, Iter2 rFirst, Iter2 rLast)
33+
test(Iter1 first, Iter1 last, T init, const T *rFirst, const T *rLast)
3834
{
39-
// C++17 doesn't test constexpr so can use a vector.
40-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
41-
// don't have the support yet. In these cases use a std::span for the test.
42-
// FIXME Remove constexpr vector workaround introduced in D90569
43-
size_t size = std::distance(first, last);
44-
#if TEST_STD_VER < 20 || \
45-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
46-
47-
std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
48-
#else
49-
assert((size <= 5) && "Increment the size of the array");
50-
typename std::iterator_traits<Iter1>::value_type b[5];
51-
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
52-
#endif
35+
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
36+
T out[5];
5337

54-
// Not in place
55-
std::exclusive_scan(first, last, v.begin(), init);
56-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
38+
// Not in place
39+
T *end = std::exclusive_scan(first, last, out, init);
40+
assert(std::equal(out, end, rFirst, rLast));
5741

58-
// In place
59-
std::copy(first, last, v.begin());
60-
std::exclusive_scan(v.begin(), v.end(), v.begin(), init);
61-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
42+
// In place
43+
std::copy(first, last, out);
44+
end = std::exclusive_scan(out, end, out, init);
45+
assert(std::equal(out, end, rFirst, rLast));
6246
}
6347

6448
template <class Iter>
6549
TEST_CONSTEXPR_CXX20 void
6650
test()
6751
{
68-
int ia[] = {1, 3, 5, 7, 9};
52+
int ia[] = {1, 3, 5, 7, 9};
6953
const int pRes[] = {0, 1, 4, 9, 16};
7054
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
7155
static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
7256

73-
for (unsigned int i = 0; i < sa; ++i )
57+
for (unsigned int i = 0; i < sa; ++i) {
7458
test(Iter(ia), Iter(ia + i), 0, pRes, pRes + i);
59+
}
7560
}
7661

7762
constexpr size_t triangle(size_t n) { return n*(n+1)/2; }

libcxx/test/std/numerics/numeric.ops/exclusive.scan/exclusive_scan_init_op.pass.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

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

13+
// <numeric>
14+
1415
// Became constexpr in C++20
1516
// template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
1617
// OutputIterator
@@ -24,50 +25,33 @@
2425
#include <cassert>
2526
#include <functional>
2627
#include <iterator>
27-
#include <vector>
2828

2929
#include "test_macros.h"
3030
#include "test_iterators.h"
31-
// FIXME Remove constexpr vector workaround introduced in D90569
32-
#if TEST_STD_VER > 17
33-
#include <span>
34-
#endif
3531

36-
template <class Iter1, class T, class Op, class Iter2>
32+
template <class Iter1, class T, class Op>
3733
TEST_CONSTEXPR_CXX20 void
38-
test(Iter1 first, Iter1 last, T init, Op op, Iter2 rFirst, Iter2 rLast)
34+
test(Iter1 first, Iter1 last, T init, Op op, const T *rFirst, const T *rLast)
3935
{
40-
// C++17 doesn't test constexpr so can use a vector.
41-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
42-
// don't have the support yet. In these cases use a std::span for the test.
43-
// FIXME Remove constexpr vector workaround introduced in D90569
44-
size_t size = std::distance(first, last);
45-
#if TEST_STD_VER < 20 || \
46-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
47-
48-
std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
49-
#else
50-
assert((size <= 5) && "Increment the size of the array");
51-
typename std::iterator_traits<Iter1>::value_type b[5];
52-
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
53-
#endif
36+
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
37+
T out[5];
5438

55-
// Not in place
56-
std::exclusive_scan(first, last, v.begin(), init, op);
57-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
39+
// Not in place
40+
T *end = std::exclusive_scan(first, last, out, init, op);
41+
assert(std::equal(out, end, rFirst, rLast));
5842

59-
// In place
60-
std::copy(first, last, v.begin());
61-
std::exclusive_scan(v.begin(), v.end(), v.begin(), init, op);
62-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
43+
// In place
44+
std::copy(first, last, out);
45+
end = std::exclusive_scan(out, end, out, init, op);
46+
assert(std::equal(out, end, rFirst, rLast));
6347
}
6448

6549

6650
template <class Iter>
6751
TEST_CONSTEXPR_CXX20 void
6852
test()
6953
{
70-
int ia[] = {1, 3, 5, 7, 9};
54+
int ia[] = {1, 3, 5, 7, 9};
7155
const int pRes[] = {0, 1, 4, 9, 16};
7256
const int mRes[] = {1, 1, 3, 15, 105};
7357
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
@@ -77,7 +61,7 @@ test()
7761
for (unsigned int i = 0; i < sa; ++i ) {
7862
test(Iter(ia), Iter(ia + i), 0, std::plus<>(), pRes, pRes + i);
7963
test(Iter(ia), Iter(ia + i), 1, std::multiplies<>(), mRes, mRes + i);
80-
}
64+
}
8165
}
8266

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

11185
assert(res.size() == 10);
11286
size_t j = 1;

libcxx/test/std/numerics/numeric.ops/inclusive.scan/inclusive_scan.pass.cpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

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

13+
// <numeric>
14+
1415
// Became constexpr in C++20
1516
// template<class InputIterator, class OutputIterator, class T>
1617
// OutputIterator inclusive_scan(InputIterator first, InputIterator last,
@@ -23,56 +24,40 @@
2324
#include <cassert>
2425
#include <functional>
2526
#include <iterator>
26-
#include <vector>
2727

2828
#include "test_macros.h"
2929
#include "test_iterators.h"
30-
// FIXME Remove constexpr vector workaround introduced in D90569
31-
#if TEST_STD_VER > 17
32-
#include <span>
33-
#endif
3430

35-
template <class Iter1, class Iter2>
31+
template <class Iter1, class T>
3632
TEST_CONSTEXPR_CXX20 void
37-
test(Iter1 first, Iter1 last, Iter2 rFirst, Iter2 rLast)
33+
test(Iter1 first, Iter1 last, const T *rFirst, const T *rLast)
3834
{
39-
// C++17 doesn't test constexpr so can use a vector.
40-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
41-
// don't have the support yet. In these cases use a std::span for the test.
42-
// FIXME Remove constexpr vector workaround introduced in D90569
43-
size_t size = std::distance(first, last);
44-
#if TEST_STD_VER < 20 || \
45-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
46-
47-
std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
48-
#else
49-
assert((size <= 5) && "Increment the size of the array");
50-
typename std::iterator_traits<Iter1>::value_type b[5];
51-
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
52-
#endif
35+
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
36+
T out[5];
5337

54-
// Not in place
55-
std::inclusive_scan(first, last, v.begin());
56-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
38+
// Not in place
39+
T *end = std::inclusive_scan(first, last, out);
40+
assert(std::equal(out, end, rFirst, rLast));
5741

58-
// In place
59-
std::copy(first, last, v.begin());
60-
std::inclusive_scan(v.begin(), v.end(), v.begin());
61-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
42+
// In place
43+
std::copy(first, last, out);
44+
end = std::inclusive_scan(out, end, out);
45+
assert(std::equal(out, end, rFirst, rLast));
6246
}
6347

6448

6549
template <class Iter>
6650
TEST_CONSTEXPR_CXX20 void
6751
test()
6852
{
69-
int ia[] = {1, 3, 5, 7, 9};
53+
int ia[] = {1, 3, 5, 7, 9};
7054
const int pRes[] = {1, 4, 9, 16, 25};
7155
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
7256
static_assert(sa == sizeof(pRes) / sizeof(pRes[0])); // just to be sure
7357

74-
for (unsigned int i = 0; i < sa; ++i )
58+
for (unsigned int i = 0; i < sa; ++i ) {
7559
test(Iter(ia), Iter(ia + i), pRes, pRes + i);
60+
}
7661
}
7762

7863
constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
@@ -106,18 +91,8 @@ basic_tests()
10691
}
10792

10893
{
109-
// C++17 doesn't test constexpr so can use a vector.
110-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
111-
// don't have the support yet. In these cases use a std::span for the test.
112-
// FIXME Remove constexpr vector workaround introduced in D90569
113-
#if TEST_STD_VER < 20 || \
114-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
115-
std::vector<size_t> v, res;
116-
std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res));
117-
#else
11894
std::array<size_t, 0> v, res;
11995
std::inclusive_scan(v.begin(), v.end(), res.begin());
120-
#endif
12196
assert(res.empty());
12297
}
12398
}

libcxx/test/std/numerics/numeric.ops/inclusive.scan/inclusive_scan_op.pass.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

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

13+
// <numeric>
14+
1415
// Became constexpr in C++20
1516
// template<class InputIterator, class OutputIterator, class T, class BinaryOperation>
1617
// OutputIterator
@@ -24,50 +25,33 @@
2425
#include <cassert>
2526
#include <functional>
2627
#include <iterator>
27-
#include <vector>
2828

2929
#include "test_macros.h"
3030
#include "test_iterators.h"
31-
// FIXME Remove constexpr vector workaround introduced in D90569
32-
#if TEST_STD_VER > 17
33-
#include <span>
34-
#endif
3531

36-
template <class Iter1, class Op, class Iter2>
32+
template <class Iter1, class Op, class T>
3733
TEST_CONSTEXPR_CXX20 void
38-
test(Iter1 first, Iter1 last, Op op, Iter2 rFirst, Iter2 rLast)
34+
test(Iter1 first, Iter1 last, Op op, const T *rFirst, const T *rLast)
3935
{
40-
// C++17 doesn't test constexpr so can use a vector.
41-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
42-
// don't have the support yet. In these cases use a std::span for the test.
43-
// FIXME Remove constexpr vector workaround introduced in D90569
44-
size_t size = std::distance(first, last);
45-
#if TEST_STD_VER < 20 || \
46-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
47-
48-
std::vector<typename std::iterator_traits<Iter1>::value_type> v(size);
49-
#else
50-
assert((size <= 5) && "Increment the size of the array");
51-
typename std::iterator_traits<Iter1>::value_type b[5];
52-
std::span<typename std::iterator_traits<Iter1>::value_type> v{b, size};
53-
#endif
36+
assert((rLast - rFirst) <= 5); // or else increase the size of "out"
37+
T out[5];
5438

55-
// Not in place
56-
std::inclusive_scan(first, last, v.begin(), op);
57-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
39+
// Not in place
40+
T *end = std::inclusive_scan(first, last, out, op);
41+
assert(std::equal(out, end, rFirst, rLast));
5842

59-
// In place
60-
std::copy(first, last, v.begin());
61-
std::inclusive_scan(v.begin(), v.end(), v.begin(), op);
62-
assert(std::equal(v.begin(), v.end(), rFirst, rLast));
43+
// In place
44+
std::copy(first, last, out);
45+
end = std::inclusive_scan(out, end, out, op);
46+
assert(std::equal(out, end, rFirst, rLast));
6347
}
6448

6549

6650
template <class Iter>
6751
TEST_CONSTEXPR_CXX20 void
6852
test()
6953
{
70-
int ia[] = {1, 3, 5, 7, 9};
54+
int ia[] = {1, 3, 5, 7, 9};
7155
const int pRes[] = {1, 4, 9, 16, 25};
7256
const int mRes[] = {1, 3, 15, 105, 945};
7357
const unsigned sa = sizeof(ia) / sizeof(ia[0]);
@@ -77,7 +61,7 @@ test()
7761
for (unsigned int i = 0; i < sa; ++i ) {
7862
test(Iter(ia), Iter(ia + i), std::plus<>(), pRes, pRes + i);
7963
test(Iter(ia), Iter(ia + i), std::multiplies<>(), mRes, mRes + i);
80-
}
64+
}
8165
}
8266

8367
constexpr size_t triangle(size_t n) { return n*(n+1)/2; }
@@ -111,18 +95,8 @@ basic_tests()
11195
}
11296

11397
{
114-
// C++17 doesn't test constexpr so can use a vector.
115-
// C++20 can use vector in constexpr evaluation, but both libc++ and MSVC
116-
// don't have the support yet. In these cases use a std::span for the test.
117-
// FIXME Remove constexpr vector workaround introduced in D90569
118-
#if TEST_STD_VER < 20 || \
119-
(defined(__cpp_lib_constexpr_vector) && __cpp_lib_constexpr_vector >= 201907L)
120-
std::vector<size_t> v, res;
121-
std::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<>());
122-
#else
12398
std::array<size_t, 0> v, res;
12499
std::inclusive_scan(v.begin(), v.end(), res.begin(), std::plus<>());
125-
#endif
126100
assert(res.empty());
127101
}
128102
}

0 commit comments

Comments
 (0)