Skip to content

Commit 61cc660

Browse files
committed
refactor tests: do not extract tests into separate functions
1 parent 47afcf1 commit 61cc660

File tree

1 file changed

+60
-103
lines changed

1 file changed

+60
-103
lines changed

libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/stable_sort.pass.cpp

Lines changed: 60 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -80,96 +80,48 @@ test_sort_()
8080
}
8181

8282
template <int N, int M>
83-
TEST_CONSTEXPR_CXX26 std::array<int, N> init_saw_tooth_pattern() {
84-
std::array<int, N> array;
85-
for (int i = 0, x = 0; i < N; ++i) {
83+
TEST_CONSTEXPR_CXX26 void test_larger_sorts() {
84+
static_assert(N != 0);
85+
static_assert(M != 0);
86+
87+
// create array length N filled with M different numbers
88+
std::array<int, N> array_;
89+
int* const array = array_.data();
90+
int x = 0;
91+
for (int i = 0; i < N; ++i) {
8692
array[i] = x;
8793
if (++x == M)
8894
x = 0;
8995
}
90-
return array;
91-
}
92-
93-
template <int N, int M>
94-
TEST_CONSTEXPR_CXX26 std::array<int, N> sort_saw_tooth_pattern() {
95-
std::array<int, N> array = init_saw_tooth_pattern<N, M>();
96-
std::stable_sort(array.begin(), array.end());
97-
return array;
98-
}
99-
100-
template <int N, int M>
101-
TEST_CONSTEXPR_CXX26 std::array<int, N> sort_already_sorted() {
102-
std::array<int, N> array = sort_saw_tooth_pattern<N, M>();
103-
std::stable_sort(array.begin(), array.end());
104-
return array;
105-
}
106-
107-
template <int N, int M>
108-
TEST_CONSTEXPR_CXX26 std::array<int, N> sort_reversely_sorted() {
109-
std::array<int, N> array = sort_saw_tooth_pattern<N, M>();
110-
std::reverse(array.begin(), array.end());
111-
std::stable_sort(array.begin(), array.end());
112-
return array;
113-
}
114-
115-
template <int N, int M>
116-
TEST_CONSTEXPR_CXX26 std::array<int, N> sort_swapped_sorted_ranges() {
117-
std::array<int, N> array = sort_saw_tooth_pattern<N, M>();
118-
std::swap_ranges(array.begin(), array.begin() + N / 2, array.begin() + N / 2);
119-
std::stable_sort(array.begin(), array.end());
120-
return array;
121-
}
122-
123-
template <int N, int M>
124-
TEST_CONSTEXPR_CXX26 std::array<int, N> sort_reversely_swapped_sorted_ranges() {
125-
std::array<int, N> array = sort_saw_tooth_pattern<N, M>();
126-
std::reverse(array.begin(), array.end());
127-
std::swap_ranges(array.begin(), array.begin() + N / 2, array.begin() + N / 2);
128-
std::stable_sort(array.begin(), array.end());
129-
return array;
130-
}
131-
132-
template <int N, int M>
133-
TEST_CONSTEXPR_CXX26 void test_larger_sorts() {
134-
static_assert(N > 0, "");
135-
static_assert(M > 0, "");
136-
137-
{ // test saw tooth pattern
138-
std::array<int, N> array = sort_saw_tooth_pattern<N, M>();
139-
assert(std::is_sorted(array.begin(), array.end()));
140-
}
141-
96+
// test saw tooth pattern
97+
std::stable_sort(array, array + N);
98+
assert(std::is_sorted(array, array + N));
99+
// test random pattern
142100
#if TEST_STD_VER >= 26
143-
if !consteval
101+
if !consteval // random-number generators not constexpr-friendly
144102
#endif
145-
{ // test random pattern
146-
// random-number generators not constexpr-friendly
103+
{
147104
static std::mt19937 randomness;
148-
std::array<int, N> array = init_saw_tooth_pattern<N, M>();
149-
std::shuffle(array.begin(), array.end(), randomness);
150-
std::stable_sort(array.begin(), array.end());
151-
assert(std::is_sorted(array.begin(), array.end()));
152-
}
153-
154-
{ // test sorted pattern
155-
std::array<int, N> array = sort_already_sorted<N, M>();
156-
assert(std::is_sorted(array.begin(), array.end()));
157-
}
158-
159-
{ // test reverse sorted pattern
160-
std::array<int, N> array = sort_reversely_sorted<N, M>();
161-
assert(std::is_sorted(array.begin(), array.end()));
162-
}
163-
164-
{ // test swap ranges 2 pattern
165-
std::array<int, N> array = sort_swapped_sorted_ranges<N, M>();
166-
assert(std::is_sorted(array.begin(), array.end()));
167-
}
168-
169-
{ // test reverse swap ranges 2 pattern
170-
std::array<int, N> array = sort_reversely_swapped_sorted_ranges<N, M>();
171-
assert(std::is_sorted(array.begin(), array.end()));
105+
std::shuffle(array, array + N, randomness);
106+
std::stable_sort(array, array + N);
107+
assert(std::is_sorted(array, array + N));
172108
}
109+
// test sorted pattern
110+
std::stable_sort(array, array + N);
111+
assert(std::is_sorted(array, array + N));
112+
// test reverse sorted pattern
113+
std::reverse(array, array + N);
114+
std::stable_sort(array, array + N);
115+
assert(std::is_sorted(array, array + N));
116+
// test swap ranges 2 pattern
117+
std::swap_ranges(array, array + N / 2, array + N / 2);
118+
std::stable_sort(array, array + N);
119+
assert(std::is_sorted(array, array + N));
120+
// test reverse swap ranges 2 pattern
121+
std::reverse(array, array + N);
122+
std::swap_ranges(array, array + N / 2, array + N / 2);
123+
std::stable_sort(array, array + N);
124+
assert(std::is_sorted(array, array + N));
173125
}
174126

175127
template <int N>
@@ -185,16 +137,16 @@ TEST_CONSTEXPR_CXX26 void test_larger_sorts() {
185137
test_larger_sorts<N, N>();
186138
}
187139

188-
int main(int, char**) {
189-
{ // test null range
190-
int d = 0;
191-
std::stable_sort(&d, &d);
140+
TEST_CONSTEXPR_CXX26 void test() {
141+
// test null range
142+
int d = 0;
143+
std::stable_sort(&d, &d);
144+
145+
// exhaustively test all possibilities up to length 8
192146
#if TEST_STD_VER >= 26
193-
static_assert((std::stable_sort(&d, &d), true));
147+
if !consteval
194148
#endif
195-
}
196-
197-
{ // exhaustively test all possibilities up to length 8
149+
{
198150
test_sort_<1>();
199151
test_sort_<2>();
200152
test_sort_<3>();
@@ -205,30 +157,35 @@ int main(int, char**) {
205157
test_sort_<8>();
206158
}
207159

208-
{ // larger sorts
209-
// run- and compile-time tests
210-
test_larger_sorts<256>();
211-
test_larger_sorts<257>();
160+
test_larger_sorts<256>();
161+
test_larger_sorts<257>();
162+
test_larger_sorts<499>();
163+
test_larger_sorts<500>();
164+
test_larger_sorts<997>();
212165
#if TEST_STD_VER >= 26
213-
static_assert((test_larger_sorts<256>(), true));
214-
static_assert((test_larger_sorts<257>(), true));
166+
if !consteval // only runtime tests bc. error: "constexpr evaluation hit maximum step limit"
215167
#endif
216-
217-
// only runtime tests bc. error: "constexpr evaluation hit maximum step limit"
218-
test_larger_sorts<499>();
219-
test_larger_sorts<500>();
220-
test_larger_sorts<997>();
168+
{
221169
test_larger_sorts<1000>();
222170
test_larger_sorts<1009>();
223171
}
224172

225-
#ifndef TEST_HAS_NO_EXCEPTIONS
173+
#if !defined(TEST_HAS_NO_EXCEPTIONS)
174+
# if TEST_STD_VER >= 26
175+
if !consteval
176+
# endif
226177
{ // check that the algorithm works without memory
227178
std::vector<int> vec(150, 3);
228179
getGlobalMemCounter()->throw_after = 0;
229180
std::stable_sort(vec.begin(), vec.end());
230181
}
231-
#endif
182+
#endif // !defined(TEST_HAS_NO_EXCEPTIONS)
183+
}
232184

185+
int main(int, char**) {
186+
test();
187+
#if TEST_STD_VER >= 26
188+
static_assert((test(), true));
189+
#endif
233190
return 0;
234191
}

0 commit comments

Comments
 (0)