some style changes in card tests

This commit is contained in:
Gregory 2020-06-04 23:05:49 +03:00
parent 304d944679
commit 1ae966103c
2 changed files with 30 additions and 23 deletions

View file

@ -36,6 +36,7 @@ val remove_card: remove_card = fn (cards, to_remove, exp) =>
| (false, _) => raise exp | (false, _) => raise exp
end end
(* not tail recursive *)
type all_same_color = card list -> bool type all_same_color = card list -> bool
val rec all_same_color: all_same_color = fn cards => val rec all_same_color: all_same_color = fn cards =>
case cards of case cards of
@ -48,6 +49,9 @@ type sum_cards = card list -> int
val sum_cards: sum_cards = fn cards => val sum_cards: sum_cards = fn cards =>
cards |> fold (fn card => fn sum => card_value card + sum) 0 cards |> fold (fn card => fn sum => card_value card + sum) 0
(* even shorter via partial application :) *)
val sum_cards: sum_cards = fold (fn card => fn sum => card_value card + sum) 0
type score = card list * int -> int type score = card list * int -> int
val score: score = fn (cards, goal) => val score: score = fn (cards, goal) =>
let let

View file

@ -2,28 +2,34 @@ use "test.sml";
use "card.sml"; use "card.sml";
val () = val () =
assert (card_color (Spade, King) = Black) assert
"card_color: returns right color" $ card_color (Spade, King) = Black
$ "card_color: returns right color"
val () = val () =
assert (card_color (Heart, King) = Red) assert
"card_color: returns right color x2" $ card_color (Heart, King) = Red
$ "card_color: returns right color x2"
val () = val () =
assert (card_value (Heart, Num 5) = 5) assert
"card_value: number card" $ card_value (Heart, Num 5) = 5
$ "card_value: number card"
val () = val () =
assert (card_value (Heart, Ace) = 11) assert
"card_value: ace is 11" $ card_value (Heart, Ace) = 11
$ "card_value: ace is 11"
val () = val () =
assert (card_value (Heart, Jack) = 10) assert
"card_value: jack is 10" $ card_value (Heart, Jack) = 10
$ "card_value: jack is 10"
val () = val () =
assert (remove_card ([(Heart, Jack)], (Heart, Jack), IllegalMove) = []) assert
"remove_card: removes card" $ remove_card ([(Heart, Jack)], (Heart, Jack), IllegalMove) = []
$ "remove_card: removes card"
val () = val () =
let let
@ -33,24 +39,21 @@ val () =
val exp = IllegalMove val exp = IllegalMove
in in
assert assert
(remove_card (deck, card, exp) = expected) $ remove_card (deck, card, exp) = expected
"remove_card: leaves duplicates" $ "remove_card: leaves duplicates"
end end
val _ = val [] =
let let
val deck = [] val deck = []
val card = (Heart, Jack) val card = (Heart, Jack)
val expected = IllegalMove val expected = []
val exp = IllegalMove val exp = IllegalMove
in in
remove_card (deck, card, exp) remove_card (deck, card, exp)
handle IllegalMove => handle IllegalMove =>
let assert true "remove_card: raises exception"
val () = assert true "remove_card: raises exception" |> (fn _ => expected)
in
[]
end
end end
val () = val () =
@ -201,12 +204,12 @@ val 0 =
val deck = [(Club, Jack), (Club, Num 3), (Heart, Num 10), (Club, Jack)] val deck = [(Club, Jack), (Club, Num 3), (Heart, Num 10), (Club, Jack)]
val moves = [Discard (Club, Jack), Draw, Draw, Draw] val moves = [Discard (Club, Jack), Draw, Draw, Draw]
val goal = 19 val goal = 19
val expected = 6 val expected = 0
in in
officiate (deck, moves, goal) officiate (deck, moves, goal)
handle IllegalMove => handle IllegalMove =>
assert true "officiate: raises exception on missing card" assert true "officiate: raises exception on missing card"
|> (fn _ => 0) |> (fn _ => expected)
end end
val () = complete () val () = complete ()