1
exercism fetch ocaml bowling

test.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
130
131
132
133
134
135
136
137
138
139
140
141
142
(* Test/exercise version: "1.0.1" *)

open! Core
open OUnit2
open Bowling

type game = Bowling.t

let to_ok = function
| Ok x -> x
| Error e -> failwith @@ "should be OK but got Error " ^ e

let set_previous_frames (frames : int list): game =
  List.fold frames ~init:new_game ~f:(fun g f -> roll f g |> to_ok)

let score_printer = function
| Ok n -> Int.to_string n
| Error e -> e
let assert_score exp game = assert_equal ~printer:score_printer exp (score game)

let roll_printer = function
| Ok _ -> "Ok <some game>"
| Error e -> e
let assert_roll exp frame game = assert_equal ~printer:roll_printer exp (roll frame game)

let tests = [
   "should be able to score a game with all zeros" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 0) g
   );
   "should be able to score a game with no strikes or spares" >:: (fun _ ->
      let g = set_previous_frames [3; 6; 3; 6; 3; 6; 3; 6; 3; 6; 3; 6; 3; 6; 3; 6; 3; 6; 3; 6] in
      assert_score (Ok 90) g
   );
   "a spare followed by zeros is worth ten points" >:: (fun _ ->
      let g = set_previous_frames [6; 4; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 10) g
   );
   "points scored in the roll after a spare are counted twice" >:: (fun _ ->
      let g = set_previous_frames [6; 4; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 16) g
   );
   "consecutive spares each get a one roll bonus" >:: (fun _ ->
      let g = set_previous_frames [5; 5; 3; 7; 4; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 31) g
   );
   "a spare in the last frame gets a one roll bonus that is counted once" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 7; 3; 7] in
      assert_score (Ok 17) g
   );
   "a strike earns ten points in a frame with a single roll" >:: (fun _ ->
      let g = set_previous_frames [10; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 10) g
   );
   "points scored in the two rolls after a strike are counted twice as a bonus" >:: (fun _ ->
      let g = set_previous_frames [10; 5; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 26) g
   );
   "consecutive strikes each get the two roll bonus" >:: (fun _ ->
      let g = set_previous_frames [10; 10; 10; 5; 3; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_score (Ok 81) g
   );
   "a strike in the last frame gets a two roll bonus that is counted once" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 7; 1] in
      assert_score (Ok 18) g
   );
   "rolling a spare with the two roll bonus does not get a bonus roll" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 7; 3] in
      assert_score (Ok 20) g
   );
   "strikes with the two roll bonus do not get bonus rolls" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 10; 10] in
      assert_score (Ok 30) g
   );
   "a strike with the one roll bonus after a spare in the last frame does not get a bonus" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 7; 3; 10] in
      assert_score (Ok 20) g
   );
   "all strikes is a perfect game" >:: (fun _ ->
      let g = set_previous_frames [10; 10; 10; 10; 10; 10; 10; 10; 10; 10; 10; 10] in
      assert_score (Ok 300) g
   );
   "rolls cannot score negative points" >:: (fun _ ->
      let g = set_previous_frames [] in
      assert_roll (Error "Negative roll is invalid") (-1) g
   );
   "a roll cannot score more than 10 points" >:: (fun _ ->
      let g = set_previous_frames [] in
      assert_roll (Error "Pin count exceeds pins on the lane") 11 g
   );
   "two rolls in a frame cannot score more than 10 points" >:: (fun _ ->
      let g = set_previous_frames [5] in
      assert_roll (Error "Pin count exceeds pins on the lane") 6 g
   );
   "bonus roll after a strike in the last frame cannot score more than 10 points" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10] in
      assert_roll (Error "Pin count exceeds pins on the lane") 11 g
   );
   "two bonus rolls after a strike in the last frame cannot score more than 10 points" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 5] in
      assert_roll (Error "Pin count exceeds pins on the lane") 6 g
   );
   "two bonus rolls after a strike in the last frame can score more than 10 points if one is a strike" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 10; 6] in
      assert_score (Ok 26) g
   );
   "the second bonus rolls after a strike in the last frame cannot be a strike if the first one is not a strike" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 6] in
      assert_roll (Error "Pin count exceeds pins on the lane") 10 g
   );
   "second bonus roll after a strike in the last frame cannot score more than 10 points" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 10] in
      assert_roll (Error "Pin count exceeds pins on the lane") 11 g
   );
   "an unstarted game cannot be scored" >:: (fun _ ->
      let g = set_previous_frames [] in
      assert_score (Error "Score cannot be taken until the end of the game") g
   );
   "an incomplete game cannot be scored" >:: (fun _ ->
      let g = set_previous_frames [0; 0] in
      assert_score (Error "Score cannot be taken until the end of the game") g
   );
   "cannot roll if game already has ten frames" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0] in
      assert_roll (Error "Cannot roll after game is over") 0 g
   );
   "bonus rolls for a strike in the last frame must be rolled before score can be calculated" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10] in
      assert_score (Error "Score cannot be taken until the end of the game") g
   );
   "both bonus rolls for a strike in the last frame must be rolled before score can be calculated" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 10; 10] in
      assert_score (Error "Score cannot be taken until the end of the game") g
   );
   "bonus roll for a spare in the last frame must be rolled before score can be calculated" >:: (fun _ ->
      let g = set_previous_frames [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 7; 3] in
      assert_score (Error "Score cannot be taken until the end of the game") g
   );
]

let () =
  run_test_tt_main ("bowling tests" >::: tests)