4
4
module Main where
5
5
6
6
import Data.Matrix.Static
7
+ import Data.Monoid (Sum (Sum ), Product (Product ))
7
8
import Test.Tasty
8
9
import Test.Tasty.HUnit
9
10
10
11
main :: IO ()
11
12
main = defaultMain tests
12
13
13
14
tests :: TestTree
14
- tests = testGroup " Unit Tests" [ docExamples ]
15
+ tests = testGroup " Unit Tests" [ docExamples, instanceTests ]
16
+
17
+ instanceTests :: TestTree
18
+ instanceTests =
19
+ let u = fromListUnsafe @ 2 @ 2 @ (Int -> Int ) (map (*) [1 ,2 ,3 ,4 ])
20
+ v = fromListUnsafe @ 2 @ 2 @ (Int -> Int ) (map (+) [3 ,- 4 ,0 ,2 ])
21
+ w = fromListUnsafe @ 2 @ 2 @ Int [4 ,- 1 ,2 ,2 ]
22
+ a = fromListUnsafe @ 2 @ 2 @ (Sum Int ) (map Sum [- 1 ,- 4 ,0 ,2 ])
23
+ x = 2
24
+ f = (+ 5 )
25
+ p = fromListUnsafe @ 2 @ 2 @ Int [2 ,2 ,2 ,2 ]
26
+ in testGroup " Instance Tests"
27
+ [ testGroup " Applicative laws"
28
+ [ testCase " identitiy" $ pure id <*> w @?= w
29
+ , testCase " composition" $ pure (.) <*> u <*> v <*> w @?= u <*> (v <*> w)
30
+ , testCase " homomorphism" $ pure f <*> pure x @?= (pure (f x) :: Matrix 2 2 Int )
31
+ , testCase " interchange" $ u <*> pure x @?= (pure ($ x) <*> u :: Matrix 2 2 Int )
32
+ ]
33
+ , testGroup " Monoidal laws"
34
+ [ testCase " right identity" $ a <> mempty @?= a
35
+ , testCase " left identity" $ mempty <> a @?= a
36
+ , testCase " associativity" $
37
+ let b = fromListUnsafe @ 2 @ 2 @ (Sum Int ) (map Sum [- 1 ,- 4 ,0 ,2 ])
38
+ c = fromListUnsafe @ 2 @ 2 @ (Sum Int ) (map Sum [- 1 ,- 4 ,0 ,2 ])
39
+ in (a <> b) <> c @?= a <> (b <> c)
40
+ ]
41
+ ]
15
42
16
43
docExamples :: TestTree
17
44
docExamples =
@@ -38,7 +65,7 @@ docExamples =
38
65
zero @ 2 @ 2 @ Int @?= fromListUnsafe [0 ,0 ,0 ,0 ]
39
66
, testCase " matrix" $
40
67
(matrix (\ (i,j) -> 2 * i- j) :: Matrix 2 4 Int ) @?=
41
- fromListUnsafe [1 ,0 ,( - 1 ),( - 2 ) ,3 ,2 ,1 ,0 ]
68
+ fromListUnsafe [1 ,0 ,- 1 , - 2 ,3 ,2 ,1 ,0 ]
42
69
, testCase " identity" $
43
70
identity @ 3 @ Int @?=
44
71
fromListUnsafe [1 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ]
@@ -123,3 +150,5 @@ docExamples =
123
150
, testCase " diagProd" $
124
151
diagProd mat33 @?= 45
125
152
]
153
+
154
+
0 commit comments