-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.hs
198 lines (161 loc) · 6.13 KB
/
Tests.hs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import Test.HUnit
import Data.List
import Utils
import Board
sequenceAssertions assertions = sequence assertions >> return ()
-- ==============================
-- Board testing
-- ==============================
refSizex = 6
refSizey = 6
refSize = (refSizey, refSizex)
refBoard = mkBoard refSizex refSizey
refPos1 = (1, 1)
-- peg validity
-- ==============================
outOfBoardPegs = mkPegs [
--corners
(0, 0, White),
(0, refSizey - 1, White),
(refSizey - 1, refSizex - 1, White),
(refSizey - 1, 0, White),
--only white can play
(0, 1, Black),
(refSizey - 1, 1, Black),
--only black can play
(1, 0, White),
(1, refSizex -1, White),
--out ot the board
(-1, 0, White), (refSizey, 0, White)
]
--peg cannot be placed
testOutOfBoard = let newTestBoards = map (placePegFallback refBoard) outOfBoardPegs
in TestCase $ sequenceAssertions$ map (assertEqual "testOutOfBoard fail" refBoard) newTestBoards
--tests that it is not possible to place peg on peg
testPegOnPeg =
TestCase $ do
let b1 = placePegFallback refBoard $ mkPeg 1 1 White
let b2 = placePegFallback b1 $ mkPeg 1 1 White
assertEqual "testPegOnPeg fail" b1 b2
testInvalidPeg = TestList [testOutOfBoard, testPegOnPeg]
-- peg connecting
-- ==============================
{- scenario:
- . . . . . . .
- . . W . W . .
- . W B . . W .
- . . . ? B . .
- . W . W . W .
- . . B . . . .
- . . . . . . .
-}
connectedPegs = mkPegs [(1, 2, White), (1, 4, White),
(2, 1, White), (2, 5, White),
(4, 1, White), (4, 5, White)]
separatePegs = mkPegs [(2, 2, Black), (3, 4, Black),
(4, 3, White), (5, 2, Black)]
testPegConnect = TestCase $ assertEqual "testPegConnect fail" connectedPegs $
filterConnectedPegs (mkPeg 3 3 White) (connectedPegs ++ separatePegs)
-- peg spoiling pair generation
-- ==============================
--low level spoiling functionality
{-
scenario 1, 2 scenario 3, 4
. w . . . . w . b .
. . . b . . . . w .
. b w . . . . b . .
-}
--(test number, bridge pair, spoil pair)
dataSpoil = [ ("1", ((2, 1), (1, 3)), ((0, 1), (2, 2))),
("2", ((0, 1), (2, 2)), ((2, 1), (1, 3))),
("3", ((0, 1), (1, 3)), ((2, 2), (0, 3))),
("4", ((2, 2), (0, 3)), ((1, 3), (0, 1)))
]
--test creator
mkTestSpoil (label, bridgePair, spoiledPair) = TestCase $
do
assertBool ("testSpoilSymmetry " ++ label ++ " fail") $
sort (genSpoilPairs bridgePair) == sort (genSpoilPairs $ swapPair bridgePair)
assertBool ("testSpoilElem " ++ label ++ " fail") $
elem spoiledPair (genSpoilPairs bridgePair) ||
elem (swapPair spoiledPair) (genSpoilPairs bridgePair)
testGenSpoilPairs = TestList $ map mkTestSpoil dataSpoil
-- peg bridges
-- ==============================
{-
scenario1: scenario2:
. . . . . . . . . .
. b . . . . b w1 . .
. . . w1 . . . . b .
. w1 b . . . w1 . . .
-}
--in test data white is connected while black is not connected (only one bridge possible)
dataBridges = [--scenario1 (in both color combinations)
mkPegs [(3, 1, White), (2, 3, White), (1, 1, Black), (3, 2, Black)],
mkPegs [(1, 1, White), (3, 2, White), (3, 1, Black), (2, 3, Black)],
--scenario2
mkPegs [(3, 1, White), (1, 2, White), (1, 1, Black), (2, 3, Black)],
mkPegs [(1, 1, White), (2, 3, White), (3, 1, Black), (1, 2, Black)]
]
bridgeCheck :: Board -> Color -> Bool
bridgeCheck board color = arePegsConnected board (pegsByColor (getBoardPegs board) color)
mkTestBridge :: Pegs -> Test
mkTestBridge pegSeq =
let testBoard = placePegSeq refBoard pegSeq
resultSpoil = not . (flip bridgeCheck Black) $ testBoard
resultBuild = flip bridgeCheck White $ testBoard
in TestCase $ sequenceAssertions $
[assertBool "testBridgeBuild fail" resultBuild,
assertBool "testBridgeSpoil fail" resultSpoil]
testBridges = TestList $ map mkTestBridge dataBridges
-- peg placing
-- ==============================
-- playable positions test
-- ==============================
testPlayablePos =
let testBoard = (mkBoard 7 7)
in TestCase $ do
assertBool "testPlayablePos fail"
--playable positions are different because of the edges
(getEmptyPos testBoard White /= getEmptyPos testBoard Black &&
--it is not possible to play into corners
getEmptyPos testBoard White \\ getCorners (bdSize testBoard) == getEmptyPos testBoard White &&
--test elem is present
elem (1, 1) (getEmptyPos testBoard White) &&
--test elem is not present after peg is placed
not (elem (1, 1) $ getEmptyPos (placePeg testBoard (mkPeg 1 1 Black)) White))
-- winning check
-- ==============================
--tests placing pegs, connectivity,
--asymmetrical boardsizes and winning check
{-
scenario1: scenario2:
. . w1 . . . . w . .
. . b1 . . b1 . . . b1
b1 . . w1 b . . b1 w1 .
. w1 . . . . w1 . . .
-}
--(id, who wins, peg list)
dataWinCheck =
[("1", White, mkPegs [(0, 2, White), (1, 2, Black), (3, 1, White),
(2, 0, Black), (2, 3, White), (2, 4, Black)]),
("2", Black, mkPegs [(0, 2, White), (2, 2, Black), (3, 1, White),
(1, 4, Black), (2, 3, White), (1, 0, Black)])
]
winCheckBoard = mkBoard 4 5
mkTestWinCheck (label, winner, pegSeq) =
let testBoard = placePegSeq winCheckBoard pegSeq
in TestCase $
do
--putStrLn $ show testBoard
--putStrLn $ show $ getWinner testBoard
assertBool ("testWinCheck " ++ label ++ " fail") $ Just winner == getWinner testBoard
testWinCheck = TestList $ map mkTestWinCheck dataWinCheck
-- ==============================
-- Test running
-- ==============================
boardTest = TestList [testPegConnect, testInvalidPeg, testGenSpoilPairs,
testBridges, testPlayablePos, testWinCheck]
runTests = runTestTT boardTest
run = runTests
main = runTests