1515#include < algorithm>
1616#include < cassert>
1717#include < functional>
18- #include < random>
1918#include < vector>
2019
20+ #include " constexpr_random.h"
2121#include " test_macros.h"
2222
2323#if TEST_STD_VER >= 11
@@ -67,15 +67,14 @@ struct S {
6767#include " test_iterators.h"
6868#include " counting_predicates.h"
6969
70- std::mt19937 randomness;
71-
72- template <class Iter >
73- void test_one_randomized (unsigned N, unsigned M) {
70+ template <class Iter , class RandSrc >
71+ TEST_CONSTEXPR_CXX26 void test_one (unsigned N, unsigned M, RandSrc& randomness) {
72+ assert (M <= N);
7473 typedef typename std::iterator_traits<Iter>::value_type value_type;
7574 value_type* ia = new value_type[N];
7675 for (unsigned i = 0 ; i < N; ++i)
7776 ia[i] = i;
78- std ::shuffle (ia, ia + N, randomness);
77+ support ::shuffle (ia, ia + N, randomness);
7978 std::sort (ia, ia + M, std::greater<value_type>());
8079 std::sort (ia + M, ia + N, std::greater<value_type>());
8180 binary_counting_predicate<std::greater<value_type>, value_type, value_type> pred ((std::greater<value_type>()));
@@ -91,71 +90,38 @@ void test_one_randomized(unsigned N, unsigned M) {
9190 delete[] ia;
9291}
9392
94- template <class Iter >
95- TEST_CONSTEXPR_CXX26 void test_one_non_randomized (unsigned N, unsigned M) {
96- typedef typename std::iterator_traits<Iter>::value_type value_type;
97-
98- value_type* ia = new value_type[N];
99- const unsigned long small_prime = 19937 ;
100- const unsigned long large_prime = 212987 ;
101- unsigned long product_mod = small_prime;
102- for (unsigned i = 0 ; i < N; ++i) {
103- ia[i] = static_cast <int >(product_mod);
104- product_mod = product_mod * small_prime % large_prime;
105- }
106- std::sort (ia, ia + M, std::greater<value_type>());
107- std::sort (ia + M, ia + N, std::greater<value_type>());
108- binary_counting_predicate<std::greater<value_type>, value_type, value_type> pred ((std::greater<value_type>()));
109- std::inplace_merge (Iter (ia), Iter (ia + M), Iter (ia + N), std::ref (pred));
110- if (N > 0 ) {
111- assert (std::is_sorted (ia, ia + N, std::greater<value_type>()));
112- #if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
113- assert (pred.count () <= (N - 1 ));
114- #endif
115- }
116- delete[] ia;
117- }
118-
119- template <class Iter >
120- TEST_CONSTEXPR_CXX26 void test_one (unsigned N, unsigned M) {
121- assert (M <= N);
122- if (!TEST_IS_CONSTANT_EVALUATED) {
123- test_one_randomized<Iter>(N, M);
124- }
125- test_one_non_randomized<Iter>(N, M);
126- }
127-
128- template <class Iter >
129- TEST_CONSTEXPR_CXX26 void test (unsigned N) {
130- test_one<Iter>(N, 0 );
131- test_one<Iter>(N, N / 4 );
132- test_one<Iter>(N, N / 2 );
133- test_one<Iter>(N, 3 * N / 4 );
134- test_one<Iter>(N, N);
93+ template <class Iter , class RandSrc >
94+ TEST_CONSTEXPR_CXX26 void test (unsigned N, RandSrc& randomness) {
95+ test_one<Iter>(N, 0 , randomness);
96+ test_one<Iter>(N, N / 4 , randomness);
97+ test_one<Iter>(N, N / 2 , randomness);
98+ test_one<Iter>(N, 3 * N / 4 , randomness);
99+ test_one<Iter>(N, N, randomness);
135100}
136101
137- template <class Iter >
138- TEST_CONSTEXPR_CXX26 void test () {
139- test_one<Iter>(0 , 0 );
140- test_one<Iter>(1 , 0 );
141- test_one<Iter>(1 , 1 );
142- test_one<Iter>(2 , 0 );
143- test_one<Iter>(2 , 1 );
144- test_one<Iter>(2 , 2 );
145- test_one<Iter>(3 , 0 );
146- test_one<Iter>(3 , 1 );
147- test_one<Iter>(3 , 2 );
148- test_one<Iter>(3 , 3 );
149- test<Iter>(4 );
150- test<Iter>(20 );
102+ template <class Iter , class RandSrc >
103+ TEST_CONSTEXPR_CXX26 void test (RandSrc& randomness) {
104+ test_one<Iter>(0 , 0 , randomness);
105+ test_one<Iter>(1 , 0 , randomness);
106+ test_one<Iter>(1 , 1 , randomness);
107+ test_one<Iter>(2 , 0 , randomness);
108+ test_one<Iter>(2 , 1 , randomness);
109+ test_one<Iter>(2 , 2 , randomness);
110+ test_one<Iter>(3 , 0 , randomness);
111+ test_one<Iter>(3 , 1 , randomness);
112+ test_one<Iter>(3 , 2 , randomness);
113+ test_one<Iter>(3 , 3 , randomness);
114+ test<Iter>(4 , randomness);
115+ test<Iter>(20 , randomness);
116+ test<Iter>(50 , randomness);
151117#if defined(_LIBCPP_HARDENING_MODE)
152118 if (!TEST_IS_CONSTANT_EVALUATED) // avoid blowing past constant evaluation limit
153119#endif
154120 {
155- test<Iter>(100 );
121+ test<Iter>(100 , randomness );
156122 }
157123 if (!TEST_IS_CONSTANT_EVALUATED) { // avoid blowing past constant evaluation limit
158- test<Iter>(1000 );
124+ test<Iter>(1000 , randomness );
159125 }
160126}
161127
@@ -178,57 +144,35 @@ TEST_CONSTEXPR_CXX26 void test_PR31166() {
178144 }
179145}
180146
181- #if TEST_STD_VER >= 11
182- void test_wrapped_randomized (int N, unsigned M) {
183- std::unique_ptr<int >* ia = new std::unique_ptr<int >[N];
184- for (int i = 0 ; i < N; ++i)
185- ia[i].reset (new int (i));
186- std::shuffle (ia, ia + N, randomness);
187- std::sort (ia, ia + M, indirect_less ());
188- std::sort (ia + M, ia + N, indirect_less ());
189- std::inplace_merge (ia, ia + M, ia + N, indirect_less ());
190- if (N > 0 ) {
191- assert (*ia[0 ] == 0 );
192- assert (*ia[N - 1 ] == N - 1 );
193- assert (std::is_sorted (ia, ia + N, indirect_less ()));
194- }
195- delete[] ia;
196- }
197-
198- TEST_CONSTEXPR_CXX26 void test_wrapped_non_randomized (int N, unsigned M) {
199- std::unique_ptr<int >* ia = new std::unique_ptr<int >[N];
200-
201- const unsigned long small_prime = 19937 ;
202- const unsigned long large_prime = 212987 ;
203- unsigned long product_mod = small_prime;
204- for (int i = 0 ; i < N; ++i) {
205- ia[i].reset (new int (static_cast <int >(product_mod)));
206- product_mod = product_mod * small_prime % large_prime;
207- }
208- std::sort (ia, ia + M, indirect_less ());
209- std::sort (ia + M, ia + N, indirect_less ());
210- std::inplace_merge (ia, ia + M, ia + N, indirect_less ());
211- if (N > 0 ) {
212- assert (std::is_sorted (ia, ia + N, indirect_less ()));
213- }
214- delete[] ia;
215- }
216- #endif // TEST_STD_VER >= 11
217-
218147TEST_CONSTEXPR_CXX26 bool test () {
219- test<bidirectional_iterator<int *> >();
220- test<random_access_iterator<int *> >();
221- test<int *>();
148+ support::minstd_rand randomness;
149+
150+ test<bidirectional_iterator<int *> >(randomness);
151+ test<random_access_iterator<int *> >(randomness);
152+ test<int *>(randomness);
222153
223154#if TEST_STD_VER >= 11
224- test<bidirectional_iterator<S*> >();
225- test<random_access_iterator<S*> >();
226- test<S*>();
155+ test<bidirectional_iterator<S*> >(randomness );
156+ test<random_access_iterator<S*> >(randomness );
157+ test<S*>(randomness );
227158
228- if (!TEST_IS_CONSTANT_EVALUATED) {
229- test_wrapped_randomized (100 , 50 );
159+ {
160+ constexpr int N = 100 ;
161+ constexpr int M = 50 ;
162+ std::unique_ptr<int >* ia = new std::unique_ptr<int >[N];
163+ for (int i = 0 ; i < N; ++i)
164+ ia[i].reset (new int (i));
165+ support::shuffle (ia, ia + N, randomness);
166+ std::sort (ia, ia + M, indirect_less ());
167+ std::sort (ia + M, ia + N, indirect_less ());
168+ std::inplace_merge (ia, ia + M, ia + N, indirect_less ());
169+ if (N > 0 ) {
170+ assert (*ia[0 ] == 0 );
171+ assert (*ia[N - 1 ] == N - 1 );
172+ assert (std::is_sorted (ia, ia + N, indirect_less ()));
173+ }
174+ delete[] ia;
230175 }
231- test_wrapped_non_randomized (100 , 50 );
232176#endif // TEST_STD_VER >= 11
233177
234178 test_PR31166 ();
0 commit comments