|
2 | 2 | * Copyright (c) 2016-2025 Morwenn |
3 | 3 | * SPDX-License-Identifier: MIT |
4 | 4 | */ |
| 5 | +#include <algorithm> |
5 | 6 | #include <forward_list> |
| 7 | +#include <random> |
| 8 | +#include <type_traits> |
6 | 9 | #include <vector> |
7 | 10 | #include <catch2/catch_test_macros.hpp> |
| 11 | +#include <rapidcheck.h> |
| 12 | +#include <rapidcheck/catch.h> |
8 | 13 | #include <cpp-sort/probes/dis.h> |
9 | 14 | #include <cpp-sort/utility/size.h> |
10 | 15 | #include <testing-tools/internal_compare.h> |
| 16 | +#include <testing-tools/random.h> |
11 | 17 |
|
12 | 18 | TEST_CASE( "measure of disorder: dis", "[probe][dis]" ) |
13 | 19 | { |
@@ -58,4 +64,22 @@ TEST_CASE( "measure of disorder: dis", "[probe][dis]" ) |
58 | 64 | CHECK( dis(li) == max_n ); |
59 | 65 | CHECK( dis(li.begin(), li.end()) == max_n ); |
60 | 66 | } |
| 67 | + |
| 68 | + // Sorting and Measures of Disorder |
| 69 | + // by Vladimir Estivill-Castro |
| 70 | + |
| 71 | + rc::prop("Dis(XY) = max{Dis(X), Dis(Y)} if X ≤ Y", [](std::vector<int> sequence) { |
| 72 | + using diff_t = std::vector<int>::difference_type; |
| 73 | + using param_t = std::uniform_int_distribution<diff_t>::param_type; |
| 74 | + |
| 75 | + // Split the sequence into two consecutive subsequences X and Y |
| 76 | + auto size = static_cast<diff_t>(sequence.size()); |
| 77 | + std::uniform_int_distribution<diff_t> dist; |
| 78 | + auto x_begin = sequence.begin(); |
| 79 | + auto y_begin = x_begin + dist(hasard::engine(), param_t{0, size}); |
| 80 | + std::nth_element(x_begin, y_begin, sequence.end()); |
| 81 | + |
| 82 | + using cppsort::probe::dis; |
| 83 | + return dis(sequence) == (std::max)(dis(x_begin, y_begin), dis(y_begin, sequence.end())); |
| 84 | + }); |
61 | 85 | } |
0 commit comments