|
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>
|
|
25 | 23 |
|
26 | 24 | #if TEST_STD_VER >= 11
|
27 | 25 | struct S {
|
28 |
| - S() : i_(0) {} |
29 |
| - S(int i) : i_(i) {} |
| 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 | +}; |
| 55 | +#endif // TEST_STD_VER >= 11 |
30 | 56 |
|
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; } |
| 57 | +std::mt19937 randomness; |
43 | 58 |
|
44 |
| - int i_; |
45 |
| - }; |
46 |
| -#endif |
| 59 | +template <class Iter> |
| 60 | +void test_one_randomized(unsigned N, unsigned M) { |
| 61 | + typedef typename std::iterator_traits<Iter>::value_type value_type; |
| 62 | + value_type* ia = new value_type[N]; |
| 63 | + |
| 64 | + for (unsigned i = 0; i < N; ++i) |
| 65 | + ia[i] = i; |
| 66 | + std::shuffle(ia, ia + N, randomness); |
| 67 | + std::sort(ia, ia + M); |
| 68 | + std::sort(ia + M, ia + N); |
| 69 | + std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N)); |
| 70 | + if (N > 0) { |
| 71 | + assert(ia[0] == 0); |
| 72 | + assert(ia[N - 1] == static_cast<value_type>(N - 1)); |
| 73 | + assert(std::is_sorted(ia, ia + N)); |
| 74 | + } |
| 75 | + delete[] ia; |
| 76 | +} |
47 | 77 |
|
48 |
| -std::mt19937 randomness; |
| 78 | +template <class Iter> |
| 79 | +TEST_CONSTEXPR_CXX26 void test_one_non_randomized(unsigned N, unsigned M) { |
| 80 | + typedef typename std::iterator_traits<Iter>::value_type value_type; |
| 81 | + value_type* ia = new value_type[N]; |
| 82 | + const unsigned long small_prime = 19937; |
| 83 | + const unsigned long large_prime = 212987; |
| 84 | + unsigned long product_mod = small_prime; |
| 85 | + for (unsigned i = 0; i < N; ++i) { |
| 86 | + ia[i] = static_cast<int>(product_mod); |
| 87 | + product_mod = product_mod * small_prime % large_prime; |
| 88 | + } |
| 89 | + std::sort(ia, ia + M); |
| 90 | + std::sort(ia + M, ia + N); |
| 91 | + std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N)); |
| 92 | + if (N > 0) { |
| 93 | + assert(std::is_sorted(ia, ia + N)); |
| 94 | + } |
| 95 | + delete[] ia; |
| 96 | +} |
49 | 97 |
|
50 | 98 | 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; |
| 99 | +TEST_CONSTEXPR_CXX26 void test_one(unsigned N, unsigned M) { |
| 100 | + assert(M <= N); |
| 101 | + if (!TEST_IS_CONSTANT_EVALUATED) { |
| 102 | + test_one_randomized<Iter>(N, M); |
| 103 | + } |
| 104 | + test_one_non_randomized<Iter>(N, M); |
70 | 105 | }
|
71 | 106 |
|
72 | 107 | 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); |
| 108 | +TEST_CONSTEXPR_CXX26 void test(unsigned N) { |
| 109 | + test_one<Iter>(N, 0); |
| 110 | + test_one<Iter>(N, N / 4); |
| 111 | + test_one<Iter>(N, N / 2); |
| 112 | + test_one<Iter>(N, 3 * N / 4); |
| 113 | + test_one<Iter>(N, N); |
81 | 114 | }
|
82 | 115 |
|
83 | 116 | 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); |
| 117 | +TEST_CONSTEXPR_CXX26 void test() { |
| 118 | + test_one<Iter>(0, 0); |
| 119 | + test_one<Iter>(1, 0); |
| 120 | + test_one<Iter>(1, 1); |
| 121 | + test_one<Iter>(2, 0); |
| 122 | + test_one<Iter>(2, 1); |
| 123 | + test_one<Iter>(2, 2); |
| 124 | + test_one<Iter>(3, 0); |
| 125 | + test_one<Iter>(3, 1); |
| 126 | + test_one<Iter>(3, 2); |
| 127 | + test_one<Iter>(3, 3); |
| 128 | + test<Iter>(4); |
| 129 | +#if defined(_LIBCPP_HARDENING_MODE) |
| 130 | + if (!TEST_IS_CONSTANT_EVALUATED) // avoid blowing past constant evaluation limit |
| 131 | +#endif |
| 132 | + { |
98 | 133 | test<Iter>(100);
|
| 134 | + } |
| 135 | + if (!TEST_IS_CONSTANT_EVALUATED) { // avoid blowing past constant evaluation limit |
99 | 136 | test<Iter>(1000);
|
| 137 | + } |
100 | 138 | }
|
101 | 139 |
|
102 |
| -int main(int, char**) |
103 |
| -{ |
104 |
| - test<bidirectional_iterator<int*> >(); |
105 |
| - test<random_access_iterator<int*> >(); |
106 |
| - test<int*>(); |
| 140 | +TEST_CONSTEXPR_CXX26 bool test() { |
| 141 | + test<bidirectional_iterator<int*> >(); |
| 142 | + test<random_access_iterator<int*> >(); |
| 143 | + test<int*>(); |
107 | 144 |
|
108 | 145 | #if TEST_STD_VER >= 11
|
109 |
| - test<bidirectional_iterator<S*> >(); |
110 |
| - test<random_access_iterator<S*> >(); |
111 |
| - test<S*>(); |
112 |
| -#endif |
| 146 | + test<bidirectional_iterator<S*> >(); |
| 147 | + test<random_access_iterator<S*> >(); |
| 148 | + test<S*>(); |
| 149 | +#endif // TEST_STD_VER >= 11 |
| 150 | + |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
| 154 | +int main(int, char**) { |
| 155 | + test(); |
| 156 | +#if TEST_STD_VER >= 26 |
| 157 | + static_assert(test()); |
| 158 | +#endif // TEST_STD_VER >= 26 |
113 | 159 |
|
114 | 160 | #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 |
| - } |
| 161 | + if (!TEST_IS_CONSTANT_EVALUATED) { |
| 162 | + std::vector<int> vec(150, 3); |
| 163 | + getGlobalMemCounter()->throw_after = 0; |
| 164 | + std::inplace_merge(vec.begin(), vec.begin() + 100, vec.end()); |
| 165 | + assert(std::all_of(vec.begin(), vec.end(), [](int i) { return i == 3; })); |
| 166 | + } |
121 | 167 | #endif // TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
|
122 | 168 |
|
123 | 169 | return 0;
|
|
0 commit comments