|
8 | 8 |
|
9 | 9 | // <algorithm> |
10 | 10 |
|
11 | | -// template<BidirectionalIterator Iter> |
12 | | -// requires ShuffleIterator<Iter> |
13 | | -// && LessThanComparable<Iter::value_type> |
14 | | -// void |
15 | | -// inplace_merge(Iter first, Iter middle, Iter last); |
| 11 | +// template<class BidirectionalIterator> |
| 12 | +// constexpr void // constexpr since C++26 |
| 13 | +// inplace_merge(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); |
16 | 14 |
|
17 | 15 | #include <algorithm> |
18 | 16 | #include <cassert> |
19 | | -#include <random> |
20 | 17 | #include <vector> |
21 | 18 |
|
22 | 19 | #include "count_new.h" |
| 20 | +#include "constexpr_random.h" |
23 | 21 | #include "test_iterators.h" |
24 | 22 | #include "test_macros.h" |
25 | 23 |
|
26 | 24 | #if TEST_STD_VER >= 11 |
27 | 25 | struct S { |
28 | | - S() : i_(0) {} |
29 | | - S(int i) : i_(i) {} |
30 | | - |
31 | | - S(const S& rhs) : i_(rhs.i_) {} |
32 | | - S( S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; } |
33 | | - |
34 | | - S& operator =(const S& rhs) { i_ = rhs.i_; return *this; } |
35 | | - S& operator =( S&& rhs) { i_ = rhs.i_; rhs.i_ = -2; assert(this != &rhs); return *this; } |
36 | | - S& operator =(int i) { i_ = i; return *this; } |
37 | | - |
38 | | - bool operator <(const S& rhs) const { return i_ < rhs.i_; } |
39 | | - bool operator ==(const S& rhs) const { return i_ == rhs.i_; } |
40 | | - bool operator ==(int i) const { return i_ == i; } |
41 | | - |
42 | | - void set(int i) { i_ = i; } |
43 | | - |
44 | | - int i_; |
45 | | - }; |
| 26 | + TEST_CONSTEXPR_CXX26 S() : i_(0) {} |
| 27 | + TEST_CONSTEXPR_CXX26 S(int i) : i_(i) {} |
| 28 | + |
| 29 | + TEST_CONSTEXPR_CXX26 S(const S& rhs) : i_(rhs.i_) {} |
| 30 | + TEST_CONSTEXPR_CXX26 S(S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; } |
| 31 | + |
| 32 | + TEST_CONSTEXPR_CXX26 S& operator=(const S& rhs) { |
| 33 | + i_ = rhs.i_; |
| 34 | + return *this; |
| 35 | + } |
| 36 | + TEST_CONSTEXPR_CXX26 S& operator=(S&& rhs) { |
| 37 | + i_ = rhs.i_; |
| 38 | + rhs.i_ = -2; |
| 39 | + assert(this != &rhs); |
| 40 | + return *this; |
| 41 | + } |
| 42 | + TEST_CONSTEXPR_CXX26 S& operator=(int i) { |
| 43 | + i_ = i; |
| 44 | + return *this; |
| 45 | + } |
| 46 | + |
| 47 | + TEST_CONSTEXPR_CXX26 bool operator<(const S& rhs) const { return i_ < rhs.i_; } |
| 48 | + TEST_CONSTEXPR_CXX26 bool operator==(const S& rhs) const { return i_ == rhs.i_; } |
| 49 | + TEST_CONSTEXPR_CXX26 bool operator==(int i) const { return i_ == i; } |
| 50 | + |
| 51 | + TEST_CONSTEXPR_CXX26 void set(int i) { i_ = i; } |
| 52 | + |
| 53 | + int i_; |
| 54 | +}; |
46 | 55 | #endif |
47 | 56 |
|
48 | | -std::mt19937 randomness; |
49 | | - |
50 | | -template <class Iter> |
51 | | -void |
52 | | -test_one(unsigned N, unsigned M) |
53 | | -{ |
54 | | - typedef typename std::iterator_traits<Iter>::value_type value_type; |
55 | | - assert(M <= N); |
56 | | - value_type* ia = new value_type[N]; |
57 | | - for (unsigned i = 0; i < N; ++i) |
58 | | - ia[i] = i; |
59 | | - std::shuffle(ia, ia+N, randomness); |
60 | | - std::sort(ia, ia+M); |
61 | | - std::sort(ia+M, ia+N); |
62 | | - std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N)); |
63 | | - if(N > 0) |
64 | | - { |
65 | | - assert(ia[0] == 0); |
66 | | - assert(ia[N-1] == static_cast<value_type>(N-1)); |
67 | | - assert(std::is_sorted(ia, ia+N)); |
68 | | - } |
69 | | - delete [] ia; |
| 57 | +template <class Iter, class RandSrc> |
| 58 | +TEST_CONSTEXPR_CXX26 void test_one(unsigned N, unsigned M, RandSrc& randomness) { |
| 59 | + assert(M <= N); |
| 60 | + |
| 61 | + typedef typename std::iterator_traits<Iter>::value_type value_type; |
| 62 | + value_type* ia = new value_type[N]; |
| 63 | + for (unsigned i = 0; i < N; ++i) |
| 64 | + ia[i] = i; |
| 65 | + support::shuffle(ia, ia + N, randomness); |
| 66 | + std::sort(ia, ia + M); |
| 67 | + std::sort(ia + M, ia + N); |
| 68 | + std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N)); |
| 69 | + if (N > 0) { |
| 70 | + assert(ia[0] == 0); |
| 71 | + assert(ia[N - 1] == static_cast<value_type>(N - 1)); |
| 72 | + assert(std::is_sorted(ia, ia + N)); |
| 73 | + } |
| 74 | + delete[] ia; |
70 | 75 | } |
71 | 76 |
|
72 | | -template <class Iter> |
73 | | -void |
74 | | -test(unsigned N) |
75 | | -{ |
76 | | - test_one<Iter>(N, 0); |
77 | | - test_one<Iter>(N, N/4); |
78 | | - test_one<Iter>(N, N/2); |
79 | | - test_one<Iter>(N, 3*N/4); |
80 | | - test_one<Iter>(N, N); |
| 77 | +template <class Iter, class RandSrc> |
| 78 | +TEST_CONSTEXPR_CXX26 void test(unsigned N, RandSrc& randomness) { |
| 79 | + test_one<Iter>(N, 0, randomness); |
| 80 | + test_one<Iter>(N, N / 4, randomness); |
| 81 | + test_one<Iter>(N, N / 2, randomness); |
| 82 | + test_one<Iter>(N, 3 * N / 4, randomness); |
| 83 | + test_one<Iter>(N, N, randomness); |
81 | 84 | } |
82 | 85 |
|
83 | | -template <class Iter> |
84 | | -void |
85 | | -test() |
86 | | -{ |
87 | | - test_one<Iter>(0, 0); |
88 | | - test_one<Iter>(1, 0); |
89 | | - test_one<Iter>(1, 1); |
90 | | - test_one<Iter>(2, 0); |
91 | | - test_one<Iter>(2, 1); |
92 | | - test_one<Iter>(2, 2); |
93 | | - test_one<Iter>(3, 0); |
94 | | - test_one<Iter>(3, 1); |
95 | | - test_one<Iter>(3, 2); |
96 | | - test_one<Iter>(3, 3); |
97 | | - test<Iter>(4); |
98 | | - test<Iter>(100); |
99 | | - test<Iter>(1000); |
| 86 | +template <class Iter, class RandSrc> |
| 87 | +TEST_CONSTEXPR_CXX26 void test(RandSrc& randomness) { |
| 88 | + test_one<Iter>(0, 0, randomness); |
| 89 | + test_one<Iter>(1, 0, randomness); |
| 90 | + test_one<Iter>(1, 1, randomness); |
| 91 | + test_one<Iter>(2, 0, randomness); |
| 92 | + test_one<Iter>(2, 1, randomness); |
| 93 | + test_one<Iter>(2, 2, randomness); |
| 94 | + test_one<Iter>(3, 0, randomness); |
| 95 | + test_one<Iter>(3, 1, randomness); |
| 96 | + test_one<Iter>(3, 2, randomness); |
| 97 | + test_one<Iter>(3, 3, randomness); |
| 98 | + test<Iter>(4, randomness); |
| 99 | + test<Iter>(50, randomness); |
| 100 | + if (!TEST_IS_CONSTANT_EVALUATED) { // avoid exceeding the constant evaluation step limit |
| 101 | + test<Iter>(100, randomness); |
| 102 | + test<Iter>(1000, randomness); |
| 103 | + } |
100 | 104 | } |
101 | 105 |
|
102 | | -int main(int, char**) |
103 | | -{ |
104 | | - test<bidirectional_iterator<int*> >(); |
105 | | - test<random_access_iterator<int*> >(); |
106 | | - test<int*>(); |
| 106 | +TEST_CONSTEXPR_CXX26 bool test() { |
| 107 | + support::simple_random_generator randomness; |
| 108 | + |
| 109 | + test<bidirectional_iterator<int*> >(randomness); |
| 110 | + test<random_access_iterator<int*> >(randomness); |
| 111 | + test<int*>(randomness); |
107 | 112 |
|
108 | 113 | #if TEST_STD_VER >= 11 |
109 | | - test<bidirectional_iterator<S*> >(); |
110 | | - test<random_access_iterator<S*> >(); |
111 | | - test<S*>(); |
| 114 | + test<bidirectional_iterator<S*> >(randomness); |
| 115 | + test<random_access_iterator<S*> >(randomness); |
| 116 | + test<S*>(randomness); |
112 | 117 | #endif |
113 | 118 |
|
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +int main(int, char**) { |
| 123 | + test(); |
| 124 | +#if TEST_STD_VER >= 26 |
| 125 | + static_assert(test()); |
| 126 | +#endif // TEST_STD_VER >= 26 |
| 127 | + |
114 | 128 | #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS) |
115 | | - { |
116 | | - std::vector<int> vec(150, 3); |
117 | | - getGlobalMemCounter()->throw_after = 0; |
118 | | - std::inplace_merge(vec.begin(), vec.begin() + 100, vec.end()); |
119 | | - assert(std::all_of(vec.begin(), vec.end(), [](int i) { return i == 3; })); |
120 | | - } |
| 129 | + if (!TEST_IS_CONSTANT_EVALUATED) { |
| 130 | + std::vector<int> vec(150, 3); |
| 131 | + getGlobalMemCounter()->throw_after = 0; |
| 132 | + std::inplace_merge(vec.begin(), vec.begin() + 100, vec.end()); |
| 133 | + assert(std::all_of(vec.begin(), vec.end(), [](int i) { return i == 3; })); |
| 134 | + } |
121 | 135 | #endif // TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS) |
122 | 136 |
|
123 | 137 | return 0; |
|
0 commit comments