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

mbarbin / super-master-mind / 217

31 Jan 2026 12:24PM UTC coverage: 50.206% (-38.1%) from 88.313%
217

Pull #45

github

web-flow
Merge 235b668da into 3d48eb3d9
Pull Request #45: Dune pkg ci migration

610 of 1215 relevant lines covered (50.21%)

597078.5 hits per line

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

0.0
/test/expect/test__code.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%expect_test "to_dyn" =
8
  let t = Code.create_exn [| Green; Blue; Orange; White; Red |] in
×
9
  print_dyn (Code.to_dyn t);
×
10
  [%expect {| [| Green;  Blue;  Orange;  White;  Red |] |}]
×
11
;;
12

13
let%expect_test "analyze" =
14
  let tested = Array.create ~len:(Lazy.force Cue.cardinality) false in
×
15
  let test ~solution ~candidate =
×
16
    let solution = Code.create_exn solution
×
17
    and candidate = Code.create_exn candidate in
×
18
    let cue = Code.analyze ~solution ~candidate in
19
    tested.(Cue.to_index cue) <- true;
×
20
    print_dyn (Cue.to_dyn cue);
×
21
    (* Check that [Code.analyze] is commutative. *)
22
    let cue' = Code.analyze ~solution:candidate ~candidate:solution in
×
23
    assert (Cue.equal cue cue')
×
24
  in
25
  let solution : Color.Hum.t array = [| Black; Blue; Brown; Green; Orange |] in
26
  test ~solution ~candidate:[| Red; White; Yellow; Red; Red |];
27
  [%expect {| { white = 0; black = 0 } |}];
×
28
  test ~solution ~candidate:[| Black; White; Yellow; Red; Red |];
29
  [%expect {| { white = 0; black = 1 } |}];
×
30
  test ~solution ~candidate:[| Black; Black; Yellow; Red; Red |];
31
  [%expect {| { white = 0; black = 1 } |}];
×
32
  test ~solution ~candidate:[| Black; Black; Yellow; Orange; Orange |];
33
  [%expect {| { white = 0; black = 2 } |}];
×
34
  test ~solution ~candidate:[| Black; Brown; Brown; Orange; Orange |];
35
  [%expect {| { white = 0; black = 3 } |}];
×
36
  test ~solution ~candidate:[| Black; Brown; Brown; Green; Orange |];
37
  [%expect {| { white = 0; black = 4 } |}];
×
38
  test ~solution ~candidate:[| Black; Blue; Brown; Green; Orange |];
39
  [%expect {| { white = 0; black = 5 } |}];
×
40
  test ~solution ~candidate:[| Red; White; Yellow; Red; Black |];
41
  [%expect {| { white = 1; black = 0 } |}];
×
42
  test ~solution ~candidate:[| Red; White; Yellow; Black; Black |];
43
  [%expect {| { white = 1; black = 0 } |}];
×
44
  test ~solution ~candidate:[| Black; Black; Blue; Yellow; White |];
45
  [%expect {| { white = 1; black = 1 } |}];
×
46
  test ~solution ~candidate:[| Black; Black; Blue; Green; White |];
47
  [%expect {| { white = 1; black = 2 } |}];
×
48
  test ~solution ~candidate:[| Black; Black; Blue; Green; Orange |];
49
  [%expect {| { white = 1; black = 3 } |}];
×
50
  test ~solution ~candidate:[| Red; White; Yellow; Blue; Black |];
51
  [%expect {| { white = 2; black = 0 } |}];
×
52
  test ~solution ~candidate:[| Red; White; Brown; Blue; Black |];
53
  [%expect {| { white = 2; black = 1 } |}];
×
54
  test ~solution ~candidate:[| Red; Blue; Brown; Orange; Black |];
55
  [%expect {| { white = 2; black = 2 } |}];
×
56
  test ~solution ~candidate:[| Green; Blue; Brown; Black; Orange |];
57
  [%expect {| { white = 2; black = 3 } |}];
×
58
  test ~solution ~candidate:[| Green; White; Yellow; Blue; Black |];
59
  [%expect {| { white = 3; black = 0 } |}];
×
60
  test ~solution ~candidate:[| Green; White; Brown; Blue; Black |];
61
  [%expect {| { white = 3; black = 1 } |}];
×
62
  test ~solution ~candidate:[| Green; Blue; Brown; Orange; Black |];
63
  [%expect {| { white = 3; black = 2 } |}];
×
64
  test ~solution ~candidate:[| Green; Orange; Yellow; Blue; Black |];
65
  [%expect {| { white = 4; black = 0 } |}];
×
66
  test ~solution ~candidate:[| Green; Orange; Brown; Blue; Black |];
67
  [%expect {| { white = 4; black = 1 } |}];
×
68
  test ~solution ~candidate:[| Green; Brown; Orange; Blue; Black |];
69
  [%expect {| { white = 5; black = 0 } |}];
×
70
  let not_tested =
71
    tested
72
    |> Array.filter_mapi ~f:(fun i tested -> Option.some_if (not tested) i)
×
73
    |> Array.map ~f:Cue.of_index_exn
×
74
  in
75
  print_dyn (Dyn.array Cue.to_dyn not_tested);
×
76
  require (Array.is_empty not_tested);
×
77
  [%expect {| [||] |}]
×
78
;;
79

80
let%expect_test "repetition of colors in the solution" =
81
  let test ~solution ~candidate =
×
82
    let cue =
×
83
      Code.(analyze ~solution:(create_exn solution) ~candidate:(create_exn candidate))
×
84
    in
85
    print_dyn (Cue.to_dyn cue)
×
86
  in
87
  let solution : Color.Hum.t array = [| Black; Green; Brown; Green; Brown |] in
88
  test ~solution ~candidate:[| Red; White; Yellow; Red; Red |];
89
  [%expect {| { white = 0; black = 0 } |}];
×
90
  test ~solution ~candidate:[| Green; White; Green; Red; Red |];
91
  [%expect {| { white = 2; black = 0 } |}];
×
92
  test ~solution ~candidate:[| Green; Green; Green; Red; Red |];
93
  [%expect {| { white = 1; black = 1 } |}];
×
94
  test ~solution ~candidate:[| Green; Green; Green; Brown; Red |];
95
  [%expect {| { white = 2; black = 1 } |}]
×
96
;;
97

98
let%expect_test "create_exn" =
99
  let size = Lazy.force Code.size in
×
100
  print_dyn (Dyn.record [ "size", Dyn.int size ]);
×
101
  [%expect {| { size = 5 } |}];
×
102
  require_does_raise (fun () : Code.t -> Code.create_exn [||]);
×
103
  [%expect
×
104
    {| ("Invalid code size.", { code = [||]; code_size = 0; expected_size = 5 }) |}];
×
105
  require_does_raise (fun () : Code.t -> Code.create_exn [| Red |]);
×
106
  [%expect
×
107
    {| ("Invalid code size.", { code = [| Red |]; code_size = 1; expected_size = 5 }) |}];
×
108
  require_does_raise (fun () : Code.t ->
109
    Code.create_exn (Array.create ~len:(size + 1) Color.Hum.Red));
×
110
  [%expect
×
111
    {|
112
    ("Invalid code size.",
113
     { code = [| Red;  Red;  Red;  Red;  Red;  Red |]
114
     ; code_size = 6
115
     ; expected_size = 5
116
     })
117
    |}];
×
118
  ()
119
;;
120

121
let%expect_test "indices" =
122
  for i = 0 to Lazy.force Code.cardinality - 1 do
×
123
    let code = Code.of_index_exn i in
×
124
    let index = Code.to_index code in
×
125
    let hum = Code.to_hum code in
×
126
    let code' = Code.create_exn hum in
×
127
    assert (Int.equal i index);
×
128
    assert (Code.equal code code')
×
129
  done;
130
  [%expect {||}];
×
131
  require_does_raise (fun () : Code.t -> Code.of_index_exn (Lazy.force Code.cardinality));
×
132
  [%expect {| ("Index out of bounds.", { index = 32768; cardinality = 32768 }) |}];
×
133
  ()
134
;;
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