Skip to content

Commit 803b546

Browse files
committed
improve-tests-for-floats
1 parent f0037dd commit 803b546

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <algorithm>
1919
#include <cassert>
2020
#include <iterator>
21+
#include <limits>
2122
#include <random>
2223
#include <vector>
2324

@@ -68,6 +69,13 @@ TEST_CONSTEXPR_CXX26 std::vector<T> generate_sawtooth(int N, int M) {
6869
if (++x == M)
6970
x = 0;
7071
}
72+
73+
if (std::is_signed<T>::value) {
74+
for (auto& a : v) {
75+
a -= (M / 2);
76+
}
77+
}
78+
7179
return v;
7280
}
7381

@@ -193,12 +201,38 @@ TEST_CONSTEXPR_CXX26 bool test() {
193201
return true;
194202
}
195203

204+
template <class T>
205+
bool test_floating_special_values() {
206+
static_assert(std::is_floating_point<T>::value, "");
207+
208+
auto v = generate_sawtooth<T>(1024, 512);
209+
v.insert(v.end(), 256, static_cast<T>(0.0));
210+
v.insert(v.end(), 256, static_cast<T>(-0.0));
211+
v.insert(v.end(), 256, std::numeric_limits<T>::infinity());
212+
v.insert(v.end(), 256, -std::numeric_limits<T>::infinity());
213+
214+
std::mt19937 randomness;
215+
std::shuffle(v.begin(), v.end(), randomness);
216+
217+
std::stable_sort(v.begin(), v.end());
218+
assert(std::is_sorted(v.begin(), v.end()));
219+
220+
return true;
221+
}
222+
223+
template <class T>
224+
bool test_floating() {
225+
return test<T>() && test_floating_special_values<T>();
226+
}
227+
196228
int main(int, char**) {
197229
test<int>();
198-
test<float>();
230+
test_floating<float>();
231+
test_floating<double>();
199232
#if TEST_STD_VER >= 26
200233
static_assert(test<int>());
201234
static_assert(test<float>());
235+
static_assert(test<double>());
202236
// test constexprness of radix sort branch
203237
static_assert(test<char>());
204238
#endif

0 commit comments

Comments
 (0)