Skip to content

Commit 4ba1e28

Browse files
committed
Trying to make gen tests with TypeShape
1 parent c342ff9 commit 4ba1e28

5 files changed

Lines changed: 134 additions & 70 deletions

File tree

src/GraphBLAS-sharp/GlobalContext.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module GlobalContext =
77
type MatrixBackendFormat =
88
| CSR
99
| COO
10-
| Dense
1110

1211
type VectorBackendFormat =
1312
| Sparse

tests/GraphBLAS-sharp.Tests/Common.fs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@ namespace GraphBLAS.FSharp.Tests
22

33
open FsCheck
44
open System
5-
6-
type VectorType =
7-
| Sparse = 0
8-
| Dense = 1
9-
10-
type MatrixType =
11-
| CSR = 0
12-
| COO = 1
5+
open GraphBLAS.FSharp
6+
open Microsoft.FSharp.Reflection
137

148
type MaskType =
15-
| Regular = 0
16-
| Complemented = 1
17-
| None = 2
18-
19-
type OperationCase = {
20-
VectorCase: VectorType
21-
MatrixCase: MatrixType
22-
MaskCase: MaskType
23-
}
9+
| Regular
10+
| Complemented
11+
| None
2412

2513
module Generators =
2614
let dimension2DGenerator =
@@ -56,19 +44,16 @@ module Generators =
5644
)
5745

5846
module Utils =
59-
let rec cartesian lstlst =
60-
match lstlst with
61-
| [x] ->
62-
List.fold (fun acc elem -> [elem]::acc) [] x
47+
let rec cartesian listOfLists =
48+
match listOfLists with
49+
| [x] -> List.fold (fun acc elem -> [elem]::acc) [] x
6350
| h::t ->
6451
List.fold (fun cacc celem ->
6552
(List.fold (fun acc elem -> (elem::celem)::acc) [] h) @ cacc
66-
) [] (cartesian t)
53+
) [] (cartesian t)
6754
| _ -> []
6855

69-
/// Return all values for an enumeration type
70-
let enumValues (enumType: Type) : int list =
71-
let values = Enum.GetValues enumType
72-
let lb = values.GetLowerBound 0
73-
let ub = values.GetUpperBound 0
74-
[lb .. ub] |> List.map (fun i -> values.GetValue i :?> int)
56+
let listOfUnionCases<'a> =
57+
FSharpType.GetUnionCases typeof<'a>
58+
|> Array.map (fun caseInfo -> FSharpValue.MakeUnion(caseInfo, [||]) :?> 'a)
59+
|> List.ofArray

tests/GraphBLAS-sharp.Tests/OperationsTests/EWiseAddTests.fs

Lines changed: 107 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
88
open GlobalContext
99
open TypeShape.Core
1010
open GraphBLAS.FSharp.Tests
11+
open System
12+
13+
type OperationParameter =
14+
| MatrixFormatParam of MatrixBackendFormat
15+
| MaskTypeParam of MaskType
16+
17+
type OperationCase = {
18+
MatrixCase: MatrixBackendFormat
19+
MaskCase: MaskType
20+
}
21+
22+
let testCases =
23+
[
24+
Utils.listOfUnionCases<MatrixBackendFormat> |> List.map MatrixFormatParam
25+
Utils.listOfUnionCases<MaskType> |> List.map MaskTypeParam
26+
]
27+
|> Utils.cartesian
28+
|> List.map
29+
(fun list ->
30+
let (MatrixFormatParam marixFormat) = list.[0]
31+
let (MaskTypeParam maskType) = list.[1]
32+
{
33+
MatrixCase = marixFormat
34+
MaskCase = maskType
35+
}
36+
)
37+
38+
let createMatrix<'a when 'a : struct and 'a : equality> matrixFormat args =
39+
match matrixFormat with
40+
| CSR ->
41+
Activator.CreateInstanceGeneric<CSRMatrix<_>>(
42+
Array.singleton typeof<'a>, args
43+
)
44+
|> unbox<CSRMatrix<'a>>
45+
:> Matrix<'a>
46+
| COO ->
47+
Activator.CreateInstanceGeneric<COOMatrix<_>>(
48+
Array.singleton typeof<'a>, args
49+
)
50+
|> unbox<COOMatrix<'a>>
51+
:> Matrix<'a>
52+
53+
let createCSR<'a when 'a : struct and 'a : equality> = createMatrix<'a> CSR
54+
let createCOO<'a when 'a : struct and 'a : equality> = createMatrix<'a> COO
1155

1256
type PrimitiveType =
1357
| Float32
@@ -17,45 +61,73 @@ type PairOfSparseMatrices =
1761
static member Float32Type() =
1862
Arb.fromGen <| Generators.pairOfSparseMatricesGenerator
1963
Arb.generate<float32>
20-
0f
21-
((=) 0f)
64+
0.f
65+
((=) 0.f)
2266

2367
static member BoolType() =
2468
Arb.fromGen <| Generators.pairOfSparseMatricesGenerator
2569
Arb.generate<bool>
2670
false
2771
((=) false)
2872

29-
// type IPredicate =
30-
// abstract Invoke : 'a -> bool
31-
32-
// let equals (a: 'a) (b: 'a) = true
33-
34-
// let reflexivity = {
35-
// new IPredicate with
36-
// member this.Invoke item = equals item item
37-
// }
38-
39-
// let conf = {
40-
// Config.Verbose with
41-
// Arbitrary = [typeof<Arbi>]
42-
// }
43-
44-
// let meaning randomType =
45-
// match randomType with
46-
// | Float32 -> typeof<float32>
47-
// | Bool -> typeof<bool>
48-
49-
// let check (predicate: IPredicate) (randomType: PrimitiveTypes) =
50-
// let systemType = meaning randomType
51-
// let shape = TypeShape.Create systemType
52-
// shape.Accept {
53-
// new ITypeVisitor<bool> with
54-
// member this.Visit<'a>() =
55-
// Check.One<'a -> bool>(conf, predicate.Invoke)
56-
// true
57-
// }
58-
59-
// // генерится тип
60-
// let checkGeneric (predicate: IPredicate) =
61-
// Check.Quick<PrimitiveTypes -> bool>(check predicate)
73+
let meaning primitiveType =
74+
match primitiveType with
75+
| Float32 -> typeof<float32>
76+
| Bool -> typeof<bool>
77+
78+
let printer primitiveType =
79+
match primitiveType with
80+
| Float32 -> "float32"
81+
| Bool -> "bool"
82+
83+
let checkConcrete (testCase: OperationCase) (primitiveType: PrimitiveType) =
84+
let config = { FsCheckConfig.defaultConfig with arbitrary = [typeof<PairOfSparseMatrices>] }
85+
let systemType = meaning primitiveType
86+
let prettyType = printer primitiveType
87+
let shape = TypeShape.Create systemType
88+
shape.Accept { new ITypeVisitor<Test> with
89+
member this.Visit<'a>() =
90+
testPropertyWithConfig config (sprintf "On type %s" prettyType) <|
91+
fun (matrixA: 'a[,]) (matrixB: 'a[,]) ->
92+
// let sparseA = createMatrix<'a> testCase.MatrixCase [|
93+
// box matrixA
94+
// box ((=) 0.)
95+
// |]
96+
// let sparseB = createMatrix<'a> testCase.MatrixCase [|
97+
// box matrixB
98+
// box ((=) 0.)
99+
// |]
100+
101+
let a = Matrix.ofArray2D matrixA ((=) 0.)
102+
103+
// let result =
104+
// opencl {
105+
// return! (sparseA + sparseB) mask stdSemiring
106+
// } |> oclContext.RunSync
107+
108+
()
109+
// let a = LinearAlgebra.DenseMatrix.ofArray2 matrixA
110+
// let b = LinearAlgebra.DenseVector.ofArray matrixB
111+
// let c = b * a
112+
// let elementWiseDifference =
113+
// (result |> Vector.toSeq, c.AsArray() |> Seq.ofArray)
114+
// ||> Seq.zip
115+
// |> Seq.map (fun (a, b) -> a - b)
116+
117+
// Expect.all
118+
// elementWiseDifference
119+
// (fun diff -> abs diff < Accuracy.medium.absolute)
120+
// (sprintf "%A @ %A = %A\n case:\n %A" vector matrix result case)
121+
}
122+
123+
let testsI =
124+
testCases
125+
|> List.collect
126+
(fun case ->
127+
[
128+
Utils.listOfUnionCases<PrimitiveType>
129+
|> List.map (checkConcrete case)
130+
|> testList "Operation correctness"
131+
]
132+
)
133+
|> testList "EWiseAdd Tests"

tests/GraphBLAS-sharp.Tests/OperationsTests/VxmTests.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ open GraphBLAS.FSharp
66
open MathNet.Numerics
77
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
88
open GlobalContext
9+
open GraphBLAS.FSharp.Tests
910

1011
// let config = {
1112
// FsCheckConfig.defaultConfig with
1213
// arbitrary = [ typeof<MatrixMultiplicationPair> ]
1314
// }
1415

16+
// type OperationCase = {
17+
// VectorCase: VectorBackendFormat
18+
// MatrixCase: MatrixBackendFormat
19+
// MaskCase: MaskFormat
20+
// }
21+
1522
// let testCases =
1623
// [
1724
// typeof<VectorType>
@@ -29,9 +36,10 @@ open GlobalContext
2936

3037
// let testsInStandardSemiring =
3138
// let stdSemiring = Predefined.FloatSemiring.addMult
32-
// ptestList "Float vector-matrix multiplication tests" (
33-
// List.collect (fun case ->
34-
// // добавить возможность пропускать некоторые случаи
39+
40+
// testCases
41+
// |> List.collect
42+
// (fun case ->
3543
// let matrixBackend =
3644
// match case.MatrixCase with
3745
// | MatrixType.CSR -> CSR
@@ -93,5 +101,5 @@ open GlobalContext
93101
// ptestPropertyWithConfig config "Explicit zeroes after operation should be dropped" <|
94102
// fun a b -> a + b = b + a
95103
// ]
96-
// ) testCases
97-
// )
104+
// )
105+
// |> ptestList "Float vector-matrix multiplication tests"

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open FsCheck
44
[<Tests>]
55
let allTests =
66
testList "All Tests" [
7-
7+
EWiseAddTests.checkGeneric EWiseAddTests.reflexivity
88
]
99

1010
[<EntryPoint>]

0 commit comments

Comments
 (0)