programming_languages/sml/week3/card_tests.sml

222 lines
4.9 KiB
Standard ML
Raw Permalink Normal View History

2020-06-04 03:02:24 +03:00
use "test.sml";
use "card.sml";
val () =
2020-06-04 23:05:49 +03:00
assert
$ card_color (Spade, King) = Black
$ "card_color: returns right color"
2020-06-04 03:02:24 +03:00
val () =
2020-06-04 23:05:49 +03:00
assert
$ card_color (Heart, King) = Red
$ "card_color: returns right color x2"
2020-06-04 03:02:24 +03:00
val () =
2020-06-04 23:05:49 +03:00
assert
$ card_value (Heart, Num 5) = 5
$ "card_value: number card"
2020-06-04 03:02:24 +03:00
val () =
2020-06-04 23:05:49 +03:00
assert
$ card_value (Heart, Ace) = 11
$ "card_value: ace is 11"
2020-06-04 03:02:24 +03:00
val () =
2020-06-04 23:05:49 +03:00
assert
$ card_value (Heart, Jack) = 10
$ "card_value: jack is 10"
2020-06-04 03:02:24 +03:00
val () =
2020-06-04 23:05:49 +03:00
assert
$ remove_card ([(Heart, Jack)], (Heart, Jack), IllegalMove) = []
$ "remove_card: removes card"
2020-06-04 03:02:24 +03:00
val () =
let
val deck = [(Heart, Jack), (Heart, Jack)]
val card = (Heart, Jack)
val expected = [(Heart, Jack)]
val exp = IllegalMove
in
assert
2020-06-04 23:05:49 +03:00
$ remove_card (deck, card, exp) = expected
$ "remove_card: leaves duplicates"
2020-06-04 03:02:24 +03:00
end
2020-06-04 23:05:49 +03:00
val [] =
2020-06-04 03:02:24 +03:00
let
val deck = []
val card = (Heart, Jack)
2020-06-04 23:05:49 +03:00
val expected = []
2020-06-04 03:02:24 +03:00
val exp = IllegalMove
in
remove_card (deck, card, exp)
handle IllegalMove =>
2020-06-04 23:05:49 +03:00
assert true "remove_card: raises exception"
|> (fn _ => expected)
2020-06-04 03:02:24 +03:00
end
val () =
let
val deck = []
val expected = true
in
assert
$ all_same_color deck = expected
$ "all_same_color: true on empty list"
end
val () =
let
val deck = [(Club, Jack)]
val expected = true
in
assert
$ all_same_color deck = expected
$ "all_same_color: true on one value"
end
val () =
let
val deck = [(Club, Jack), (Diamond, Jack)]
val expected = false
in
assert
$ all_same_color deck = expected
$ "all_same_color: false on two different colors"
end
val () =
let
val deck = []
val expected = 0
in
assert
$ sum_cards deck = expected
$ "sum_cards: zero on empty list"
end
val () =
let
val deck = [(Club, Jack), (Diamond, Ace)]
val expected = 21
in
assert
$ sum_cards deck = expected
$ "sum_cards: correct sum"
end
val () =
let
val deck = []
val goal = 20
val expected = 10
in
assert
$ score (deck, goal) = expected
$ "score: empty list returns goal / 2 because empty deck = same color"
end
val () =
let
val deck = [(Club, Jack), (Diamond, Jack)]
val goal = 21
val expected = 1
in
assert
$ score (deck, goal) = expected
$ "score: when sum < goal then score = goal - sum"
end
val () =
let
val deck = [(Club, Jack), (Diamond, Jack)]
val goal = 9
val expected = 33
in
assert
$ score (deck, goal) = expected
$ "score: when sum > goal then score = 3 * (sum - goal)"
end
val () =
let
val deck = [(Club, Jack), (Club, Ace)]
val goal = 18
val expected = 4
in
assert
$ score (deck, goal) = expected
$ "score: when all cards are the same color score is divied by 2"
end
val () =
let
val deck = [(Club, Jack), (Club, Ace)]
val moves = [Draw, Draw]
val goal = 18
val expected = 4
in
assert
$ officiate (deck, moves, goal) = expected
$ "officiate: player draw all deck, get correct result"
end
val () =
let
val deck = [(Club, Jack)]
val moves = [Draw, Draw]
val goal = 21
val expected = 5
in
assert
$ officiate (deck, moves, goal) = expected
$ "officiate: game stops on empty deck even if there are moves"
end
val () =
let
val deck = [(Club, Jack), (Club, Num 3), (Club, Num 10), (Club, Jack)]
val moves = [Draw, Draw, Draw, Draw]
val goal = 19
val expected = 6
in
assert
$ officiate (deck, moves, goal) = expected
$ "officiate: game stops on sum > goal"
end
val () =
let
val deck = [(Club, Jack), (Club, Num 3), (Heart, Num 10), (Club, Jack)]
val moves = [Draw, Discard (Club, Jack), Draw, Draw]
val goal = 19
val expected = 6
in
assert
$ officiate (deck, moves, goal) = expected
$ "officiate: player can discard cards"
end
val 0 =
let
val deck = [(Club, Jack), (Club, Num 3), (Heart, Num 10), (Club, Jack)]
val moves = [Discard (Club, Jack), Draw, Draw, Draw]
val goal = 19
2020-06-04 23:05:49 +03:00
val expected = 0
2020-06-04 03:02:24 +03:00
in
officiate (deck, moves, goal)
handle IllegalMove =>
assert true "officiate: raises exception on missing card"
2020-06-04 23:05:49 +03:00
|> (fn _ => expected)
2020-06-04 03:02:24 +03:00
end
2020-06-05 01:43:53 +03:00
fun by2 a = a * 2
val () =
assert
$ map by2 [1, 2, 3] = [2, 4, 6]
$ "map test"
2020-06-04 03:02:24 +03:00
val () = complete ()