1
exercism fetch haskell matrix

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
import Control.Arrow     ((&&&))
import Test.Hspec        (Spec, it, shouldBe, shouldNotBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)

import qualified Data.Vector as Vector (fromList)

import Matrix
  ( Matrix
  , cols
  , column
  , flatten
  , fromList
  , fromString
  , reshape
  , row
  , rows
  , shape
  , transpose
  )

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

specs :: Spec
specs = do

    let intMatrix = fromString :: String -> Matrix Int
    let vector = Vector.fromList

    it "extract first row" $ do
      row 0 (intMatrix "1 2\n10 20") `shouldBe` vector [1, 2]
      row 0 (intMatrix "9 7\n8 6"  ) `shouldBe` vector [9, 7]

    it "extract second row" $ do
      row 1 (intMatrix "9 8 7\n19 18 17") `shouldBe` vector [19, 18, 17]
      row 1 (intMatrix "1 4 9\n16 25 36") `shouldBe` vector [16, 25, 36]

    it "extract first column" $ do
      column 0 (intMatrix "1 2 3\n4 5 6\n7 8 9\n 8 7 6")
        `shouldBe` vector [1, 4, 7, 8]
      column 1 (intMatrix "89 1903 3\n18 3 1\n9 4 800")
        `shouldBe` vector [1903, 3, 4]

    it "shape" $ do
      shape (intMatrix ""        ) `shouldBe` (0, 0)
      shape (intMatrix "1"       ) `shouldBe` (1, 1)
      shape (intMatrix "1\n2"    ) `shouldBe` (2, 1)
      shape (intMatrix "1 2"     ) `shouldBe` (1, 2)
      shape (intMatrix "1 2\n3 4") `shouldBe` (2, 2)

    it "rows & cols" $
      (rows &&& cols) (intMatrix "1 2") `shouldBe` (1, 2)

    it "eq" $ do

      intMatrix "1 2" `shouldBe`    intMatrix "1 2"
      intMatrix "2 3" `shouldNotBe` intMatrix "1 2 3"

    it "fromList" $ do
      fromList [[1 ,  2]] `shouldBe` intMatrix "1 2"
      fromList [[1], [2]] `shouldBe` intMatrix "1\n2"

    it "transpose" $ do
      transpose (intMatrix "1\n2\n3"      ) `shouldBe` intMatrix "1 2 3"
      transpose (intMatrix "1 4\n2 5\n3 6") `shouldBe` intMatrix "1 2 3\n4 5 6"

    it "reshape" $
      reshape (2, 2) (intMatrix "1 2 3 4") `shouldBe` intMatrix "1 2\n3 4"

    it "flatten" $
      flatten (intMatrix "1 2\n3 4") `shouldBe` vector [1, 2, 3, 4]