packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#33 · main · 4 min 05 s
0Étoiles
0Forks
3Issues
| 1 | // std/array's sort, property-checked against an independent oracle: an |
| 2 | // insertion sort on lists (too slow to matter, too simple to get wrong). |
| 3 | // The properties of a correct stable sort — sorted output, stability, and |
| 4 | // the output being a permutation of the input — all hold of an exact |
| 5 | // match with the oracle. |
| 6 | // |
| 7 | // Sizes stay small: the interpreter runs these too, and a sort there is |
| 8 | // milliseconds per element. tests/run/sort.lse covers the bigger |
| 9 | // patterns, tests/bench/sorts.lse the big ones. |
| 10 | |
| 11 | import check |
| 12 | import gen |
| 13 | import std/array |
| 14 | import std/list |
| 15 | import std/random.{type random} |
| 16 | import std/test.{assert, assert_equal} |
| 17 | |
| 18 | /// The oracle: insertion sort on lists. Stable: an equal `x` lands after |
| 19 | /// the `y`s already there. |
| 20 | fn oracle :: xs: List<Int> -> List<Int> |
| 21 | { |
| 22 | list.fold xs [] insert |
| 23 | } |
| 24 | |
| 25 | fn insert :: sorted: List<Int> x: Int -> List<Int> |
| 26 | { |
| 27 | match sorted |
| 28 | { [] -> [x] |
| 29 | , [y, ..rest] -> if x < y { [x, y, ..rest] } else { [y, ..insert rest x] } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// The oracle with a key: pairs `(key, value)` by key, stable. |
| 34 | fn oracle_by_key :: xs: List<(Int, Int)> -> List<(Int, Int)> |
| 35 | { |
| 36 | list.fold xs [] insert_by_key |
| 37 | } |
| 38 | |
| 39 | fn insert_by_key :: sorted: List<(Int, Int)> x: (Int, Int) -> List<(Int, Int)> |
| 40 | { |
| 41 | match sorted |
| 42 | { [] -> [x] |
| 43 | , [y, ..rest] -> if x.0 < y.0 |
| 44 | { |
| 45 | [x, y, ..rest] |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | [y, ..insert_by_key rest x] |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | fn sorted_by_array :: xs: List<Int> -> List<Int> |
| 55 | { |
| 56 | array.to_list (array.sort (array.from_list xs)) |
| 57 | } |
| 58 | |
| 59 | /// Random lists, any values. |
| 60 | fn any_lists :: -> List<Int> / <random> |
| 61 | { |
| 62 | gen.list ~min: 0 ~max: 32 fn { gen.int ~low: 0 ~high: 1000 } |
| 63 | } |
| 64 | |
| 65 | /// Random lists over 3 distinct values (the low-cardinality path: equal |
| 66 | /// partitions, long runs of equals). |
| 67 | fn low_cardinal_lists :: -> List<Int> / <random> |
| 68 | { |
| 69 | gen.list ~min: 0 ~max: 32 fn { gen.int ~low: 0 ~high: 2 } |
| 70 | } |
| 71 | |
| 72 | @test |
| 73 | fn sorted_output_matches_the_oracle :: |
| 74 | { |
| 75 | check.seeded |
| 76 | ~seed: 380 |
| 77 | ~runs: 25 |
| 78 | any_lists |
| 79 | fn xs { sorted_by_array xs == oracle xs } |
| 80 | } |
| 81 | |
| 82 | @test |
| 83 | fn low_cardinality_output_matches_the_oracle :: |
| 84 | { |
| 85 | check.seeded |
| 86 | ~seed: 381 |
| 87 | ~runs: 25 |
| 88 | low_cardinal_lists |
| 89 | fn xs { sorted_by_array xs == oracle xs } |
| 90 | } |
| 91 | |
| 92 | @test |
| 93 | fn the_sort_is_stable :: |
| 94 | { |
| 95 | // Pairs (key, position) sorted by key only: the positions of equal keys |
| 96 | // must keep their original order. |
| 97 | check.seeded |
| 98 | ~seed: 382 |
| 99 | ~runs: 25 |
| 100 | ( fn |
| 101 | { |
| 102 | let xs = low_cardinal_lists () |
| 103 | list.zip xs (list.range 1 (list.length xs)) |
| 104 | } |
| 105 | ) |
| 106 | fn xs |
| 107 | { |
| 108 | let by_key = fn a: (Int, Int) b: (Int, Int) { compare a.0 b.0 } |
| 109 | array.to_list (array.sort_by (array.from_list xs) by_key) |
| 110 | == oracle_by_key xs |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @test |
| 115 | fn threshold_and_pattern_cases_match_the_oracle :: |
| 116 | { |
| 117 | // Around the algorithm's thresholds (insertion sort 20, the quicksort |
| 118 | // base case 32, run detection beyond), and the detected patterns. |
| 119 | let cases = |
| 120 | list.append |
| 121 | ( list.map |
| 122 | [0, 1, 2, 19, 20, 21, 31, 32, 33, 63, 64, 65, 100] |
| 123 | fn n { list.reverse (list.range 1 n) } |
| 124 | ) |
| 125 | [ list.range 1 100 |
| 126 | , list.map |
| 127 | (list.range 1 100) |
| 128 | fn i |
| 129 | { |
| 130 | if i < 100 - i |
| 131 | { |
| 132 | i |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | 100 - i |
| 137 | } |
| 138 | } |
| 139 | , list.map (list.range 1 100) fn _ { 7 } |
| 140 | ] |
| 141 | list.each cases fn xs { assert_equal (sorted_by_array xs) (oracle xs) } |
| 142 | } |