Skip to content

Commit 1b88211

Browse files
committed
Add a sorters test with distributions generated with RapidCheck
1 parent 555d0be commit 1b88211

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ add_executable(main-tests
208208
distributions/pipe_organ.cpp
209209
distributions/push_front.cpp
210210
distributions/push_middle.cpp
211+
distributions/rapidcheck.cpp
211212
distributions/shuffled.cpp
212213
distributions/shuffled_16_values.cpp
213214

tests/distributions/rapidcheck.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2025 Morwenn
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#include <algorithm>
6+
#include <deque>
7+
#include <forward_list>
8+
#include <list>
9+
#include <vector>
10+
#include <catch2/catch_template_test_macros.hpp>
11+
#include <rapidcheck.h>
12+
#include <rapidcheck/catch.h>
13+
#include <cpp-sort/sorters.h>
14+
#include <cpp-sort/utility/buffer.h>
15+
#include <cpp-sort/utility/functional.h>
16+
17+
TEMPLATE_TEST_CASE( "test random-access sorters with distributions generated by RapidCheck", "[distributions]",
18+
cppsort::adaptive_shivers_sorter,
19+
cppsort::cartesian_tree_sorter,
20+
cppsort::d_ary_heap_sorter<2>,
21+
cppsort::d_ary_heap_sorter<3>,
22+
cppsort::d_ary_heap_sorter<4>,
23+
cppsort::d_ary_heap_sorter<5>,
24+
cppsort::d_ary_heap_sorter<6>,
25+
cppsort::d_ary_heap_sorter<7>,
26+
cppsort::d_ary_heap_sorter<8>,
27+
cppsort::d_ary_heap_sorter<9>,
28+
cppsort::drop_merge_sorter,
29+
cppsort::grail_sorter<>,
30+
cppsort::grail_sorter<
31+
cppsort::utility::dynamic_buffer<cppsort::utility::sqrt>
32+
>,
33+
cppsort::heap_sorter,
34+
cppsort::insertion_sorter,
35+
cppsort::mel_sorter,
36+
cppsort::merge_sorter,
37+
cppsort::merge_insertion_sorter,
38+
cppsort::pdq_sorter,
39+
cppsort::poplar_sorter,
40+
cppsort::quick_merge_sorter,
41+
cppsort::quick_sorter,
42+
cppsort::selection_sorter,
43+
cppsort::ska_sorter,
44+
cppsort::slab_sorter,
45+
cppsort::smooth_sorter,
46+
cppsort::spin_sorter,
47+
cppsort::splay_sorter,
48+
cppsort::split_sorter,
49+
cppsort::spread_sorter,
50+
cppsort::std_sorter,
51+
cppsort::tim_sorter,
52+
cppsort::verge_sorter,
53+
cppsort::wiki_sorter<>,
54+
cppsort::wiki_sorter<
55+
cppsort::utility::dynamic_buffer<cppsort::utility::half>
56+
> )
57+
{
58+
TestType sorter;
59+
60+
rc::prop("with std::vector", [sorter](std::vector<int> collection) {
61+
auto copy = collection;
62+
63+
sorter(collection);
64+
bool res = std::is_sorted(collection.begin(), collection.end());
65+
66+
cppsort::pdq_sort(copy);
67+
return res && collection == copy;
68+
});
69+
70+
rc::prop("with std::deque", [sorter](std::deque<int> collection) {
71+
auto copy = collection;
72+
73+
sorter(collection);
74+
bool res = std::is_sorted(collection.begin(), collection.end());
75+
76+
cppsort::pdq_sort(copy);
77+
return res && collection == copy;
78+
});
79+
}
80+
81+
TEMPLATE_TEST_CASE( "test bidirectional sorters with distributions generated by RapidCheck", "[distributions]",
82+
cppsort::cartesian_tree_sorter,
83+
cppsort::drop_merge_sorter,
84+
cppsort::insertion_sorter,
85+
cppsort::mel_sorter,
86+
cppsort::merge_sorter,
87+
cppsort::quick_merge_sorter,
88+
cppsort::quick_sorter,
89+
cppsort::selection_sorter,
90+
cppsort::slab_sorter,
91+
cppsort::splay_sorter,
92+
cppsort::verge_sorter )
93+
{
94+
TestType sorter;
95+
96+
rc::prop("with std::list", [sorter](std::list<int> collection) {
97+
auto copy = collection;
98+
99+
sorter(collection);
100+
bool res = std::is_sorted(collection.begin(), collection.end());
101+
102+
cppsort::merge_sort(copy);
103+
return res && collection == copy;
104+
});
105+
}
106+
107+
TEMPLATE_TEST_CASE( "test forward sorters with distributions generated by RapidCheck", "[distributions]",
108+
cppsort::cartesian_tree_sorter,
109+
cppsort::mel_sorter,
110+
cppsort::merge_sorter,
111+
cppsort::quick_merge_sorter,
112+
cppsort::quick_sorter,
113+
cppsort::selection_sorter,
114+
cppsort::splay_sorter )
115+
{
116+
TestType sorter;
117+
118+
rc::prop("with std::forward_list", [sorter](std::forward_list<int> collection) {
119+
auto copy = collection;
120+
121+
sorter(collection);
122+
bool res = std::is_sorted(collection.begin(), collection.end());
123+
124+
cppsort::merge_sort(copy);
125+
return res && collection == copy;
126+
});
127+
}

0 commit comments

Comments
 (0)