1
exercism fetch haskell go-counting

test/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
import Data.Bifunctor    (first)
import Data.MultiSet     (fromOccurList, toOccurList)
import Data.Set          (toAscList)
import Data.Tuple        (swap)
import Test.Hspec        (Spec, it, shouldBe, shouldMatchList)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import Counting (Color(Black,White), territories, territoryFor)

main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs

specs :: Spec
specs = do

    let board5x5 = [ "  B  "
                   , " B B "
                   , "B W B"
                   , " W W "
                   , "  W  " ]

        board9x9 = [ "  B   B  "
                   , "B   B   B"
                   , "WBBBWBBBW"
                   , "W W W W W"
                   , "         "
                   , " W W W W "
                   , "B B   B B"
                   , " W BBB W "
                   , "   B B   " ]

        shouldHaveTerritories = shouldMatchList
                              . map (first toAscList)
                              . territories

        shouldScore = shouldMatchList
                    . toOccurList
                    . fromOccurList
                    . map (swap . first length)
                    . territories

        territoryIn xss = fmap (first toAscList) . territoryFor xss

    it "minimal board, no territories" $
      [ "B" ] `shouldHaveTerritories` []

    it "one territory, covering the whole board" $
      [ " " ] `shouldHaveTerritories` [([ (1, 1) ], Nothing)]

    it "two territories, rectangular board" $
      [ " BW "
      , " BW " ] `shouldHaveTerritories` [ ([ (1, 1)
                                            , (1, 2) ], Just Black)
                                         , ([ (4, 1)
                                            , (4, 2) ], Just White) ]

    it "5x5 score" $
      board5x5 `shouldScore` [ (Nothing   , 9)
                             , (Just Black, 6)
                             , (Just White, 1) ]

    it "5x5 territory for black" $
      territoryIn board5x5 (1, 2) `shouldBe` Just ([ (1, 1)
                                                   , (1, 2)
                                                   , (2, 1) ], Just Black)

    it "5x5 territory for white" $
      territoryIn board5x5 (3, 4) `shouldBe` Just ([ (3, 4) ], Just White)

    it "5x5 open territory" $
      territoryIn board5x5 (2, 5) `shouldBe` Just ([ (1, 4)
                                                   , (1, 5)
                                                   , (2, 5) ], Nothing)

    it "5x5 non-territory (stone)" $
      territoryIn board5x5 (2, 2) `shouldBe` Nothing

    it "5x5 non-territory (too low coordinate)" $
      territoryIn board5x5 (0, 2) `shouldBe` Nothing

    it "5x5 non-territory (too high coordinate)" $
      territoryIn board5x5 (2, 6) `shouldBe` Nothing

    it "9x9 score" $
      board9x9 `shouldScore` [ (Nothing   , 33)
                             , (Just Black, 14) ]