Skip to content

Commit c9af144

Browse files
committed
Create a separate test file for vectors and matrices
1 parent 6644a01 commit c9af144

File tree

3 files changed

+100
-51
lines changed

3 files changed

+100
-51
lines changed

tests/FSharpPlus.Tests/FSharpPlus.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Include="Lens.fs" />
3636
<Compile Include="Extensions.fs" />
3737
<Compile Include="BifoldableTests.fs" />
38+
<Compile Include="Matrix.fs" />
3839
<Compile Include="TypeLevel.fs" />
3940
</ItemGroup>
4041
<ItemGroup>

tests/FSharpPlus.Tests/Matrix.fs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
namespace FSharpPlus.Tests
2+
3+
open System
4+
open NUnit.Framework
5+
open Helpers
6+
7+
open FSharpPlus
8+
open FSharpPlus.Data
9+
open FSharpPlus.TypeLevel
10+
11+
module VectorTests =
12+
[<Test>]
13+
let constructorAndDeconstructorWorks() =
14+
let v1 = vector (1,2,3,4,5)
15+
let v2 = vector (1,2,3,4,5,6,7,8,9,0,1,2,3,4,5)
16+
let (Vector(_,_,_,_,_)) = v1
17+
let (Vector(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)) = v2
18+
()
19+
20+
[<Test>]
21+
let applicativeWorks() =
22+
let v = vector ((fun i -> i + 1), (fun i -> i * 2))
23+
let u = vector (2, 3)
24+
let vu = v <*> u
25+
NUnit.Framework.Assert.IsInstanceOf<Option<Vector<int,S<S<Z>>>>> (Some vu)
26+
CollectionAssert.AreEqual ([|3; 6|], Vector.toArray vu)
27+
28+
[<Test>]
29+
let satisfiesApplicativeLaws() =
30+
let u = vector ((fun i -> i - 1), (fun i -> i * 2))
31+
let v = vector ((fun i -> i + 1), (fun i -> i * 3))
32+
let w = vector (1, 1)
33+
34+
areEqual (result id <*> v) v
35+
areEqual (result (<<) <*> u <*> v <*> w) (u <*> (v <*> w))
36+
areEqual (result 2) ((result (fun i -> i + 1) : Vector<int -> int, S<S<Z>>>) <*> result 1)
37+
areEqual (u <*> result 1) (result ((|>) 1) <*> u)
38+
39+
[<Test>]
40+
let satisfiesMonadLaws() =
41+
let k = fun (a: int) -> vector (a - 1, a * 2)
42+
let h = fun (a: int) -> vector (a + 1, a * 3)
43+
let m = vector (1, 2)
44+
45+
areEqual (result 2 >>= k) (k 2)
46+
areEqual (m >>= result) m
47+
areEqual (m >>= (fun x -> k x >>= h)) ((m >>= k) >>= h)
48+
49+
module MatrixTests =
50+
[<Test>]
51+
let constructorAndDeconstructorWorks() =
52+
let m1 =
53+
matrix (
54+
(1,0,0,0),
55+
(0,1,0,0),
56+
(0,0,1,0)
57+
)
58+
let m2 =
59+
matrix (
60+
(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
61+
(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
62+
(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
63+
(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0),
64+
(0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0),
65+
(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0),
66+
(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0),
67+
(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0)
68+
)
69+
let (Matrix(_x1,_x2,_x3)) = m1
70+
let (Matrix(_y1: int*int*int*int*int*int*int*int*int*int*int*int*int*int*int*int,_y2,_y3,_y4,_y5,_y6,_y7,_y8)) = m2
71+
()
72+
73+
[<Test>]
74+
let satisfiesApplicativeLaws() =
75+
let u = matrix (
76+
((fun i -> i - 1), (fun i -> i * 2)),
77+
((fun i -> i + 1), (fun i -> i * 3))
78+
)
79+
let v = matrix (
80+
((fun i -> i - 2), (fun i -> i * 5)),
81+
((fun i -> i + 2), (fun i -> i * 7))
82+
)
83+
let w = matrix ((1, 1), (1, 2))
84+
85+
areEqual (result id <*> v) v
86+
areEqual (result (<<) <*> u <*> v <*> w) (u <*> (v <*> w))
87+
areEqual ((result (fun i -> i + 1) : Matrix<int -> int, S<S<Z>>, S<S<Z>>>) <*> result 1) (result 2)
88+
areEqual (u <*> result 1) (result ((|>) 1) <*> u)
89+
90+
[<Test>]
91+
let satisfiesMonadLaws() =
92+
let k = fun (a: int) -> matrix ((a - 1, a * 2), (a + 1, a * 3))
93+
let h = fun (a: int) -> matrix ((a - 2, a * 5), (a + 2, a * 7))
94+
let m = matrix ((1, 1), (1, 2))
95+
96+
areEqual (result 2 >>= k) (k 2)
97+
areEqual (m >>= result) m
98+
areEqual (m >>= (fun x -> k x >>= h)) ((m >>= k) >>= h)

tests/FSharpPlus.Tests/TypeLevel.fs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,8 @@ module NatTests =
150150
Assert (g2 =^ S(S(S(S(S(S Z))))))
151151

152152

153-
open FSharpPlus.Data
154-
155-
module MatrixTests =
156-
[<Test>]
157-
let matrixTests =
158-
let v1 = vector (1,2,3,4,5)
159-
let v2 = vector (1,2,3,4,5,6,7,8,9,0,1,2,3,4,5)
160-
let (Vector(_,_,_,_,_)) = v1
161-
let (Vector(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)) = v2
162-
163-
let m1 =
164-
matrix (
165-
(1,0,0,0),
166-
(0,1,0,0),
167-
(0,0,1,0)
168-
)
169-
let m2 =
170-
matrix (
171-
(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
172-
(0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
173-
(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0),
174-
(0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0),
175-
(0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0),
176-
(0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0),
177-
(0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0),
178-
(0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0)
179-
)
180-
let (Matrix(_x1,_x2,_x3)) = m1
181-
let (Matrix(_y1: int*int*int*int*int*int*int*int*int*int*int*int*int*int*int*int,_y2,_y3,_y4,_y5,_y6,_y7,_y8)) = m2
182-
()
183-
184153
open Helpers
154+
open FSharpPlus.Data
185155

186156
module TypeProviderTests =
187157
type ``0`` = TypeNat<0>
@@ -206,23 +176,3 @@ module TypeProviderTests =
206176
Assert (Matrix.colLength row1 =^ (Z |> S |> S |> S))
207177
areEqual 5 (Matrix.get Z (S Z) row1)
208178
areEqual [3; 6; 9] (Vector.toList col2)
209-
210-
module TestFunctors1 =
211-
[<Test>]
212-
let applicativeOperatorWorks() =
213-
let v = vector ((fun i -> i + 1), (fun i -> i * 2))
214-
let u = vector (2, 3)
215-
let vu = v <*> u
216-
NUnit.Framework.Assert.IsInstanceOf<Option<Vector<int,S<S<Z>>>>> (Some vu)
217-
CollectionAssert.AreEqual ([|3; 6|], Vector.toArray vu)
218-
219-
module TestFunctors2 =
220-
open FSharpPlus
221-
222-
[<Test>]
223-
let applicativeWorksWithoutSubsumption() =
224-
let v = vector ((fun i -> i + 1), (fun i -> i * 2))
225-
let u = vector (2, 3)
226-
let vu = v <*> u
227-
NUnit.Framework.Assert.IsInstanceOf<Option<Vector<int,S<S<Z>>>>> (Some vu)
228-
CollectionAssert.AreEqual ([|3; 6|], Vector.toArray vu)

0 commit comments

Comments
 (0)