-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtake_drop.ml
129 lines (98 loc) · 2.57 KB
/
take_drop.ml
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
open QCheck
(* ################################################
Definitions
################################################ *)
let rec take l n =
if n > 0 then
match l with
| [] -> []
| x :: xs -> x :: take xs (n-1)
else
[]
let rec drop l n =
if n > 0 then
match l with
| [] -> []
| _ :: xs -> drop xs (n-1)
else
l
(* ################################################
Start of tests
################################################ *)
(**
Combining [take l n] and [drop l n] gives the starting list
*)
let test_take_drop_append take drop =
Test.make ~name:"test_take_drop_append"
(tup2 small_nat (list int)) (fun (n, l) ->
take l n @ drop l n = l
)
(**
Taking then dropping the same amount gives nothing
*)
let test_take_drop_nil take drop =
Test.make ~name:"test_take_drop_nil"
(tup2 small_nat (list int)) (fun (n, l) ->
drop (take l n) n = []
)
(**
Taking the same amount twice doesn't change the result
*)
let test_take_take take =
Test.make ~name:"test_take_take"
(tup2 small_nat (list int)) (fun (n, l) ->
take (take l n) n = take l n
)
let ( -- ) a b = List.init (b - a) (fun x -> a + x)
(**
[take l n] takes at least [n] elements and at most [List.length l]
*)
let test_take_length take =
QCheck.Test.make ~name:"test_take_length"
(tup2 small_nat (list small_int)) (fun (n, l) ->
List.length (take l n) = min (List.length l) n
)
(**
[drop l n] drops at most [n] elements
*)
let test_drop_length drop =
QCheck.Test.make ~name:"test_drop_length"
(tup2 small_nat (list small_int)) (fun (n, l) ->
List.length (drop l n) = max 0 (List.length l - n)
)
(**
[take l n] is a sublist of [l]
*)
let test_take_sublist take =
QCheck.Test.make ~name:"test_take_sublist"
(tup2 small_nat (list small_int)) (fun (n, l) ->
let t = take l n in
List.for_all
(fun i -> List.mem (List.nth t i) l)
(0 -- List.length t)
)
(**
[drop l n] is a sublist of [l]
*)
let test_drop_sublist drop =
QCheck.Test.make ~name:"test_drop_sublist"
(tup2 small_nat (list small_int)) (fun (n, l) ->
let t = drop l n in
List.for_all
(fun i -> List.mem (List.nth t i) l)
(0 -- List.length t)
)
;;
(* ################################################
Test runner
################################################ *)
QCheck_runner.run_tests ~verbose:true
[
test_take_drop_append take drop;
test_take_drop_nil take drop;
test_take_take take;
test_take_length take;
test_drop_length drop;
test_take_sublist take;
test_drop_sublist drop;
];;