• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mbarbin / super-master-mind / 236

15 Mar 2026 05:30PM UTC coverage: 89.377% (+1.1%) from 88.313%
236

push

github

web-flow
Merge pull request #52 from mbarbin/improve-stdlib

Improve local stdlib

176 of 188 new or added lines in 15 files covered. (93.62%)

1220 of 1365 relevant lines covered (89.38%)

2333623.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.17
/test/stdlib/test__nonempty_list.ml
1
(*********************************************************************************)
2
(*  super-master-mind: A solver for the super master mind game                   *)
3
(*  SPDX-FileCopyrightText: 2021-2025 Mathieu Barbin <mathieu.barbin@gmail.com>  *)
4
(*  SPDX-License-Identifier: MIT                                                 *)
5
(*********************************************************************************)
6

7
let to_dyn t = Dyn.list Dyn.int (Nonempty_list.to_list t)
9✔
8
let print t = print_dyn (to_dyn t)
9✔
9

10
let%expect_test "singleton" =
11
  print (Nonempty_list.singleton 42);
1✔
12
  [%expect {| [ 42 ] |}];
1✔
13
  ()
14
;;
15

16
let%expect_test "hd" =
17
  print_dyn (Dyn.int (Nonempty_list.hd (Nonempty_list.of_list_exn [ 1; 2; 3 ])));
1✔
18
  [%expect {| 1 |}];
1✔
19
  ()
20
;;
21

22
let%expect_test "tl" =
23
  print_dyn (Dyn.list Dyn.int (Nonempty_list.tl (Nonempty_list.of_list_exn [ 1; 2; 3 ])));
1✔
24
  [%expect {| [ 2; 3 ] |}];
1✔
25
  print_dyn (Dyn.list Dyn.int (Nonempty_list.tl (Nonempty_list.singleton 1)));
1✔
26
  [%expect {| [] |}];
1✔
27
  ()
28
;;
29

30
let%expect_test "cons" =
31
  print (Nonempty_list.cons 0 (Nonempty_list.of_list_exn [ 1; 2 ]));
1✔
32
  [%expect {| [ 0; 1; 2 ] |}];
1✔
33
  ()
34
;;
35

36
let%expect_test "of_list_exn" =
37
  print (Nonempty_list.of_list_exn [ 1; 2; 3 ]);
1✔
38
  [%expect {| [ 1; 2; 3 ] |}];
1✔
39
  ()
40
;;
41

42
let%expect_test "of_list_exn empty" =
43
  require_does_raise (fun () -> Nonempty_list.of_list_exn []);
1✔
44
  [%expect {| Invalid_argument("Nonempty_list.of_list_exn") |}];
1✔
45
  ()
46
;;
47

48
let%expect_test "to_list" =
49
  print_dyn (Dyn.list Dyn.int (Nonempty_list.to_list Nonempty_list.(1 :: [ 2; 3 ])));
1✔
50
  [%expect {| [ 1; 2; 3 ] |}];
1✔
51
  ()
52
;;
53

54
let%expect_test "to_array" =
55
  let arr = Nonempty_list.to_array (Nonempty_list.of_list_exn [ 1; 2; 3 ]) in
1✔
56
  print_dyn (Dyn.list Dyn.int (Array.to_list arr));
1✔
57
  [%expect {| [ 1; 2; 3 ] |}];
1✔
58
  ()
59
;;
60

61
let%expect_test "length" =
62
  print_dyn (Dyn.int (Nonempty_list.length (Nonempty_list.singleton 1)));
1✔
63
  [%expect {| 1 |}];
1✔
64
  print_dyn (Dyn.int (Nonempty_list.length (Nonempty_list.of_list_exn [ 1; 2; 3 ])));
1✔
65
  [%expect {| 3 |}];
1✔
66
  ()
67
;;
68

69
let%expect_test "init" =
70
  print (Nonempty_list.init 4 ~f:(fun i -> i * 10));
1✔
71
  [%expect {| [ 0; 10; 20; 30 ] |}];
1✔
72
  ()
73
;;
74

75
let%expect_test "init invalid" =
76
  require_does_raise (fun () -> Nonempty_list.init 0 ~f:Fun.id);
1✔
77
  [%expect {| Invalid_argument("Nonempty_list.init") |}];
1✔
78
  ()
79
;;
80

81
let%expect_test "append" =
82
  print (Nonempty_list.append (Nonempty_list.of_list_exn [ 1; 2 ]) [ 3; 4 ]);
1✔
83
  [%expect {| [ 1; 2; 3; 4 ] |}];
1✔
84
  ()
85
;;
86

87
let%expect_test "map" =
88
  print (Nonempty_list.map (Nonempty_list.of_list_exn [ 1; 2; 3 ]) ~f:(fun x -> x * 10));
1✔
89
  [%expect {| [ 10; 20; 30 ] |}];
1✔
90
  ()
91
;;
92

93
let%expect_test "mapi" =
94
  print
1✔
95
    (Nonempty_list.mapi (Nonempty_list.of_list_exn [ 10; 20; 30 ]) ~f:(fun i x -> i + x));
1✔
96
  [%expect {| [ 10; 21; 32 ] |}];
1✔
97
  ()
98
;;
99

100
let%expect_test "iter" =
101
  Nonempty_list.iter
1✔
102
    (Nonempty_list.of_list_exn [ 1; 2; 3 ])
1✔
103
    ~f:(fun x -> print_dyn (Dyn.int x));
3✔
104
  [%expect
1✔
105
    {|
106
    1
107
    2
108
    3
109
    |}];
1✔
110
  ()
111
;;
112

113
let%expect_test "fold" =
114
  let t = Nonempty_list.of_list_exn [ 1; 2; 3 ] in
1✔
115
  print_dyn (Dyn.int (Nonempty_list.fold t ~init:0 ~f:(fun acc x -> acc + x)));
1✔
116
  [%expect {| 6 |}];
1✔
117
  ()
118
;;
119

120
let%expect_test "find" =
121
  let t = Nonempty_list.of_list_exn [ 1; 2; 3 ] in
1✔
122
  print_dyn (Dyn.option Dyn.int (Nonempty_list.find t ~f:(fun x -> x = 2)));
1✔
123
  [%expect {| Some 2 |}];
1✔
124
  print_dyn (Dyn.option Dyn.int (Nonempty_list.find t ~f:(fun x -> x = 5)));
1✔
125
  [%expect {| None |}];
1✔
126
  ()
127
;;
128

129
let%expect_test "filter" =
130
  let result =
1✔
131
    Nonempty_list.filter
132
      (Nonempty_list.of_list_exn [ 1; 2; 3; 4; 5 ])
1✔
133
      ~f:(fun x -> x mod 2 = 0)
5✔
134
  in
135
  print_dyn (Dyn.list Dyn.int result);
1✔
136
  [%expect {| [ 2; 4 ] |}];
1✔
137
  ()
138
;;
139

140
let%expect_test "filter_map" =
141
  let result =
1✔
142
    Nonempty_list.filter_map
143
      (Nonempty_list.of_list_exn [ 1; 2; 3; 4; 5 ])
1✔
144
      ~f:(fun x -> if x mod 2 = 0 then Some (x * 10) else None)
2✔
145
  in
146
  print_dyn (Dyn.list Dyn.int result);
1✔
147
  [%expect {| [ 20; 40 ] |}];
1✔
148
  ()
149
;;
150

151
let%expect_test "concat_map" =
152
  let result =
1✔
153
    Nonempty_list.concat_map
154
      (Nonempty_list.of_list_exn [ 1; 2; 3 ])
1✔
155
      ~f:(fun x -> Nonempty_list.of_list_exn [ x; x * 10 ])
3✔
156
  in
157
  print result;
1✔
158
  [%expect {| [ 1; 10; 2; 20; 3; 30 ] |}];
1✔
159
  ()
160
;;
161

162
let%expect_test "max_elt" =
163
  let result =
1✔
164
    Nonempty_list.max_elt
165
      (Nonempty_list.of_list_exn [ 3; 1; 4; 1; 5 ])
1✔
166
      ~compare:Int.compare
167
  in
168
  print_dyn (Dyn.int result);
1✔
169
  [%expect {| 5 |}];
1✔
170
  ()
171
;;
172

173
let%expect_test "sum" =
174
  let module I = struct
1✔
175
    type t = int
176

177
    let zero = 0
178
    let ( + ) = ( + )
179
  end
180
  in
181
  let result =
182
    Nonempty_list.sum (module I) (Nonempty_list.of_list_exn [ 1; 2; 3; 4 ]) ~f:Fun.id
1✔
183
  in
184
  print_dyn (Dyn.int result);
1✔
185
  [%expect {| 10 |}];
1✔
186
  ()
187
;;
188

189
let%expect_test "equal" =
190
  let eq a b = Nonempty_list.equal Int.equal a b in
1✔
191
  print_dyn (Dyn.bool (eq Nonempty_list.(1 :: [ 2 ]) Nonempty_list.(1 :: [ 2 ])));
1✔
192
  [%expect {| true |}];
1✔
193
  print_dyn (Dyn.bool (eq Nonempty_list.(1 :: [ 2 ]) Nonempty_list.(1 :: [ 3 ])));
1✔
194
  [%expect {| false |}];
1✔
195
  ()
196
;;
197

198
let%expect_test "compare" =
199
  let cmp a b = Nonempty_list.compare Int.compare a b in
1✔
200
  print_dyn (Ordering.to_dyn (cmp Nonempty_list.(1 :: [ 2 ]) Nonempty_list.(1 :: [ 2 ])));
1✔
201
  [%expect {| Eq |}];
1✔
202
  print_dyn (Ordering.to_dyn (cmp Nonempty_list.(1 :: [ 2 ]) Nonempty_list.(1 :: [ 3 ])));
1✔
203
  [%expect {| Lt |}];
1✔
204
  ()
205
;;
206

207
let%expect_test "zip" =
208
  let a = Nonempty_list.of_list_exn [ 1; 2; 3 ] in
1✔
209
  let b = Nonempty_list.of_list_exn [ 10; 20; 30 ] in
1✔
210
  (match Nonempty_list.zip a b with
1✔
211
   | Ok zipped ->
1✔
212
     print_dyn
1✔
213
       (Dyn.list
1✔
214
          (fun (x, y) -> Dyn.Tuple [ Dyn.int x; Dyn.int y ])
3✔
215
          (Nonempty_list.to_list zipped))
1✔
NEW
216
   | Unequal_lengths -> print_dyn (Dyn.string "unequal"));
×
217
  [%expect {| [ (1, 10); (2, 20); (3, 30) ] |}];
1✔
218
  let c = Nonempty_list.of_list_exn [ 1; 2 ] in
219
  (match Nonempty_list.zip c b with
1✔
NEW
220
   | Ok _ -> print_dyn (Dyn.string "ok")
×
221
   | Unequal_lengths -> print_dyn (Dyn.string "unequal"));
1✔
222
  [%expect {| "unequal" |}];
1✔
223
  ()
224
;;
225

226
let%expect_test "pattern matching" =
227
  let show (t : int Nonempty_list.t) =
1✔
228
    match t with
3✔
229
    | [ x ] -> print_dyn (Dyn.variant "One" [ Dyn.int x ])
1✔
230
    | [ x; y ] -> print_dyn (Dyn.variant "Two" [ Dyn.int x; Dyn.int y ])
1✔
231
    | _ :: _ :: _ :: _ -> print_dyn (Dyn.variant "Many" [])
1✔
232
  in
233
  show (Nonempty_list.singleton 1);
1✔
234
  [%expect {| One 1 |}];
1✔
235
  show (Nonempty_list.of_list_exn [ 1; 2 ]);
1✔
236
  [%expect {| Two (1, 2) |}];
1✔
237
  show (Nonempty_list.of_list_exn [ 1; 2; 3 ]);
1✔
238
  [%expect {| Many |}];
1✔
239
  ()
240
;;
241

242
let%expect_test "cons construction" =
243
  print Nonempty_list.(1 :: [ 2; 3 ]);
1✔
244
  [%expect {| [ 1; 2; 3 ] |}];
1✔
245
  ()
246
;;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc