-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsertion_sort.ml
More file actions
69 lines (49 loc) · 1.35 KB
/
Copy pathinsertion_sort.ml
File metadata and controls
69 lines (49 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open QCheck
let rec insert (l : int list) (x : int) : int list =
match l with
| [] -> [ x ]
| y :: ys -> if x < y then x :: y :: ys else y :: insert ys x
let sort : int list -> int list = List.fold_left insert []
let test_insert_length =
Test.make ~name:"test_insert_length"
(tup2 int (list int))
(fun (x, l) ->
List.length (insert l x) = 1 + List.length l
)
let test_insert_in =
Test.make ~name:"test_insert_in"
(tup2 int (list int))
(fun (x, l) ->
List.mem x (insert l x)
)
let test_insert_preserve =
Test.make ~name:"test_insert_preserve"
(tup3 small_int small_int (list small_int))
(fun (x, y, l) ->
List.mem y l ==>
List.mem y (insert l x)
)
let test_insert_partition =
Test.make ~name:"test_insert_partition"
(tup2 small_int (list small_int))
(fun (x, l) ->
let sorted = sort l in
let lt_x, ge_x = List.partition (fun y -> y < x) sorted in
insert sorted x = lt_x @ x :: ge_x
)
let test_insert_sorted =
Test.make ~name:"test_insert_sorted"
(tup2 small_int (list small_int))
(fun (x, l) ->
insert (sort l) x = sort (x :: l)
)
;;
(* Run the tests with `dune utop` *)
QCheck_runner.run_tests ~verbose:true
[
test_insert_length;
test_insert_in;
test_insert_preserve;
test_insert_partition;
test_insert_sorted
]