about summary refs log tree commit diff
path: root/assessments/dotted-squares/Spec.hs
blob: b5d604085b9b9d07f9dc1a2fe4088684d11046a5 (plain) (blame)
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
--------------------------------------------------------------------------------
module Spec where
--------------------------------------------------------------------------------
import Test.Hspec
import Main hiding (main)
import qualified Data.HashSet as HS
--------------------------------------------------------------------------------

main :: IO ()
main = hspec $ do
  describe "dotted-squares" $ do
    describe "parseInput" $ do
      it "works as expected" $ do
        input <- readFile "input-a.txt"
        parseInput input `shouldBe` Just (Game mempty [ mkLine (Point 0 0) (Point 1 0)
                                                      , mkLine (Point 0 0) (Point 0 1)
                                                      ])

      it "fails when the game has too many user moves" $ do
        input <- readFile "too-many-moves.txt"
        parseInput input `shouldBe` Nothing

      it "fails when the game has too few user moves" $ do
        input <- readFile "too-few-moves.txt"
        parseInput input `shouldBe` Nothing

    describe "shiftLine" $ do
      let horizontal = mkLineDir 1 1 DirRight
          vertical   = mkLineDir 1 1 DirUp
      it "can move a horizontal line up" $
        shiftLine DirUp horizontal `shouldBe` mkLineDir 1 2 DirRight
      it "can move a horizontal line down" $
        shiftLine DirDown horizontal `shouldBe` mkLineDir 1 0 DirRight
      it "can move a horizontal line left" $
        shiftLine DirLeft horizontal `shouldBe` mkLineDir 0 1 DirRight
      it "can move a horizontal line right" $
        shiftLine DirRight horizontal `shouldBe` mkLineDir 2 1 DirRight
      it "can move a vertical line up" $
        shiftLine DirUp vertical `shouldBe` mkLineDir 1 2 DirUp
      it "can move a vertical line down" $
        shiftLine DirDown vertical `shouldBe` mkLineDir 1 0 DirUp
      it "can move a vertical line left" $
        shiftLine DirLeft vertical `shouldBe` mkLineDir 0 1 DirUp
      it "can move a vertical line right" $
        shiftLine DirRight vertical `shouldBe` mkLineDir 2 1 DirUp

    describe "rotateLine" $ do
      let horizontal = mkLineDir 1 1 DirRight -- 1,1;2,1
          vertical   = mkLineDir 1 1 DirUp    -- 1,1;1,2
      it "can rotate a horizontal line CW anchored at its beginning" $
        rotateLine Beg CW horizontal `shouldBe` mkLineDir 1 1 DirDown
      it "can rotate a horizontal line CCW anchored at its beginning" $
        rotateLine Beg CCW horizontal `shouldBe` mkLineDir 1 1 DirUp
      it "can rotate a horizontal line CW anchored at its end" $
        rotateLine End CW horizontal `shouldBe` mkLineDir 2 1 DirUp
      it "can rotate a horizontal line CCW anchored at its end" $
        rotateLine End CCW horizontal `shouldBe` mkLineDir 2 1 DirDown

      it "can rotate a vertical line CW anchored at its beginning" $
        rotateLine Beg CW vertical `shouldBe` mkLineDir 1 1 DirRight
      it "can rotate a vertical line CCW anchored at its beginning" $
        rotateLine Beg CCW vertical `shouldBe` mkLineDir 1 1 DirLeft
      it "can rotate a vertical line CW anchored at its end" $
        rotateLine End CW vertical `shouldBe` mkLineDir 1 2 DirLeft
      it "can rotate a vertical line CCW anchored at its end" $
        rotateLine End CCW vertical `shouldBe` mkLineDir 1 2 DirRight

    describe "closesAnySquare" $ do
      let threeSides = [ (0, 0, DirRight)
                       , (0, 0, DirUp)
                       , (0, 1, DirRight)
                       ]
                       |> fmap (\(x, y, dir) -> mkLineDir x y dir)
                       |> HS.fromList
      it "returns true the line we supply makes a square" $
        closesAnySquare threeSides (mkLineDir 1 1 DirDown) `shouldBe` True
      it "returns false the line we supply doesn't make a square" $
        closesAnySquare threeSides (mkLineDir 1 1 DirUp) `shouldBe` False
      it "returns false when we have no existing lines" $
        closesAnySquare mempty (mkLineDir 1 1 DirUp) `shouldBe` False