Skip to content

Commit b9cb3f5

Browse files
Use constexpr-ized minstd_rand as random source in test files
1 parent 0fd8637 commit b9cb3f5

File tree

2 files changed

+89
-171
lines changed

2 files changed

+89
-171
lines changed

libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
#include <algorithm>
1616
#include <cassert>
17-
#include <random>
1817
#include <vector>
1918

2019
#include "count_new.h"
20+
#include "constexpr_random.h"
2121
#include "test_iterators.h"
2222
#include "test_macros.h"
2323

@@ -54,15 +54,15 @@ struct S {
5454
};
5555
#endif
5656

57-
std::mt19937 randomness;
57+
template <class Iter, class RandSrc>
58+
TEST_CONSTEXPR_CXX26 void test_one(unsigned N, unsigned M, RandSrc& randomness) {
59+
assert(M <= N);
5860

59-
template <class Iter>
60-
void test_one_randomized(unsigned N, unsigned M) {
6161
typedef typename std::iterator_traits<Iter>::value_type value_type;
6262
value_type* ia = new value_type[N];
6363
for (unsigned i = 0; i < N; ++i)
6464
ia[i] = i;
65-
std::shuffle(ia, ia + N, randomness);
65+
support::shuffle(ia, ia + N, randomness);
6666
std::sort(ia, ia + M);
6767
std::sort(ia + M, ia + N);
6868
std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N));
@@ -74,77 +74,51 @@ void test_one_randomized(unsigned N, unsigned M) {
7474
delete[] ia;
7575
}
7676

77-
template <class Iter>
78-
TEST_CONSTEXPR_CXX26 void test_one_non_randomized(unsigned N, unsigned M) {
79-
typedef typename std::iterator_traits<Iter>::value_type value_type;
80-
value_type* ia = new value_type[N];
81-
const unsigned long small_prime = 19937;
82-
const unsigned long large_prime = 212987;
83-
unsigned long product_mod = small_prime;
84-
for (unsigned i = 0; i < N; ++i) {
85-
ia[i] = static_cast<int>(product_mod);
86-
product_mod = product_mod * small_prime % large_prime;
87-
}
88-
std::sort(ia, ia + M);
89-
std::sort(ia + M, ia + N);
90-
std::inplace_merge(Iter(ia), Iter(ia + M), Iter(ia + N));
91-
if (N > 0) {
92-
assert(std::is_sorted(ia, ia + N));
93-
}
94-
delete[] ia;
95-
}
96-
97-
template <class Iter>
98-
TEST_CONSTEXPR_CXX26 void test_one(unsigned N, unsigned M) {
99-
assert(M <= N);
100-
if (!TEST_IS_CONSTANT_EVALUATED) {
101-
test_one_randomized<Iter>(N, M);
102-
}
103-
test_one_non_randomized<Iter>(N, M);
104-
}
105-
106-
template <class Iter>
107-
TEST_CONSTEXPR_CXX26 void test(unsigned N) {
108-
test_one<Iter>(N, 0);
109-
test_one<Iter>(N, N / 4);
110-
test_one<Iter>(N, N / 2);
111-
test_one<Iter>(N, 3 * N / 4);
112-
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);
11384
}
11485

115-
template <class Iter>
116-
TEST_CONSTEXPR_CXX26 void test() {
117-
test_one<Iter>(0, 0);
118-
test_one<Iter>(1, 0);
119-
test_one<Iter>(1, 1);
120-
test_one<Iter>(2, 0);
121-
test_one<Iter>(2, 1);
122-
test_one<Iter>(2, 2);
123-
test_one<Iter>(3, 0);
124-
test_one<Iter>(3, 1);
125-
test_one<Iter>(3, 2);
126-
test_one<Iter>(3, 3);
127-
test<Iter>(4);
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);
128100
#if defined(_LIBCPP_HARDENING_MODE)
129101
if (!TEST_IS_CONSTANT_EVALUATED) // avoid blowing past constant evaluation limit
130102
#endif
131103
{
132-
test<Iter>(100);
104+
test<Iter>(100, randomness);
133105
}
134106
if (!TEST_IS_CONSTANT_EVALUATED) { // avoid blowing past constant evaluation limit
135-
test<Iter>(1000);
107+
test<Iter>(1000, randomness);
136108
}
137109
}
138110

139111
TEST_CONSTEXPR_CXX26 bool test() {
140-
test<bidirectional_iterator<int*> >();
141-
test<random_access_iterator<int*> >();
142-
test<int*>();
112+
support::minstd_rand randomness;
113+
114+
test<bidirectional_iterator<int*> >(randomness);
115+
test<random_access_iterator<int*> >(randomness);
116+
test<int*>(randomness);
143117

144118
#if TEST_STD_VER >= 11
145-
test<bidirectional_iterator<S*> >();
146-
test<random_access_iterator<S*> >();
147-
test<S*>();
119+
test<bidirectional_iterator<S*> >(randomness);
120+
test<random_access_iterator<S*> >(randomness);
121+
test<S*>(randomness);
148122
#endif
149123

150124
return true;

libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp

Lines changed: 53 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
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-
218147
TEST_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

Comments
 (0)