Skip to content

Commit cbd5ecc

Browse files
committed
Add tests for applicative and monoidal laws
1 parent 9178be0 commit cbd5ecc

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

test/Spec.hs

+31-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,41 @@
44
module Main where
55

66
import Data.Matrix.Static
7+
import Data.Monoid (Sum(Sum), Product(Product))
78
import Test.Tasty
89
import Test.Tasty.HUnit
910

1011
main :: IO ()
1112
main = defaultMain tests
1213

1314
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+
]
1542

1643
docExamples :: TestTree
1744
docExamples =
@@ -38,7 +65,7 @@ docExamples =
3865
zero @2 @2 @Int @?= fromListUnsafe [0,0,0,0]
3966
, testCase "matrix" $
4067
(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]
4269
, testCase "identity" $
4370
identity @3 @Int @?=
4471
fromListUnsafe [1,0,0,0,1,0,0,0,1]
@@ -123,3 +150,5 @@ docExamples =
123150
, testCase "diagProd" $
124151
diagProd mat33 @?= 45
125152
]
153+
154+

0 commit comments

Comments
 (0)