Skip to content

Commit 511951b

Browse files
committed
Test Exc(XY) = Exc(X) + Exc(Y) if X <= Y only for sequences of distinct elements
1 parent a3f1089 commit 511951b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

tests/probes/every_probe_common.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ TEMPLATE_TEST_CASE( "test prefix monotonicity", "[probe]",
257257
}
258258

259259
TEMPLATE_TEST_CASE( "test M(XY) = M(X) + M(Y) if X <= Y for most probes M", "[probe]",
260-
decltype(cppsort::probe::exc),
261260
decltype(cppsort::probe::ham),
262261
decltype(cppsort::probe::inv),
263262
decltype(cppsort::probe::rem),

tests/probes/exc.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
*/
55
#include <forward_list>
66
#include <iterator>
7+
#include <random>
78
#include <vector>
89
#include <catch2/catch_test_macros.hpp>
10+
#include <rapidcheck.h>
11+
#include <rapidcheck/catch.h>
912
#include <cpp-sort/probes/exc.h>
1013
#include <cpp-sort/utility/size.h>
1114
#include <testing-tools/distributions.h>
@@ -64,4 +67,24 @@ TEST_CASE( "measure of disorder: exc", "[probe][exc]" )
6467
CHECK( exc(seq) == 1 );
6568
CHECK( exc(subseq) == 2 );
6669
}
70+
71+
// Property formalized by Estivill-Castro in *Sorting and Measures of Disorder*,
72+
// only works when X has distinct values
73+
74+
rc::prop("Exc(XY) = Exc(X) + Exc(Y) if X ≤ Y", []() {
75+
using diff_t = std::vector<int>::difference_type;
76+
using param_t = std::uniform_int_distribution<diff_t>::param_type;
77+
78+
auto sequence = *rc::gen::unique<std::vector<int>>(rc::gen::arbitrary<int>());
79+
80+
// Split the sequence into two consequent subsequences X and Y
81+
auto size = static_cast<diff_t>(sequence.size());
82+
std::uniform_int_distribution<diff_t> dist;
83+
auto x_begin = sequence.begin();
84+
auto y_begin = x_begin + dist(hasard::engine(), param_t{0, size});
85+
std::nth_element(x_begin, y_begin, sequence.end());
86+
87+
using cppsort::probe::exc;
88+
return exc(sequence) == exc(x_begin, y_begin) + exc(y_begin, sequence.end());
89+
});
6790
}

0 commit comments

Comments
 (0)