Skip to content

Commit 4c1a4bc

Browse files
committed
refactor: tests
1 parent 4e55216 commit 4c1a4bc

15 files changed

Lines changed: 204 additions & 277 deletions

File tree

src/GraphBLAS-sharp.Backend/Common/BitonicSort.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ module internal BitonicSort =
290290
queue.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rows cols values values.Length))
291291
queue.Post(Msg.CreateRunMsg<_, _>(kernel))
292292

293-
let sortKeyValuesInplace (clContext: ClContext) workGroupSize =
293+
let sortKeyValuesInplace<'n, 'a when 'n: comparison> (clContext: ClContext) workGroupSize =
294294

295295
let localBegin = localBegin clContext workGroupSize
296296
let globalStep = globalStep clContext workGroupSize

src/GraphBLAS-sharp.Backend/Common/Scatter.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module internal Scatter =
2222
/// > val result = [| 2.8; 5.5; 6.4; 8.2; 9.1 |]
2323
/// </code>
2424
/// </example>
25-
let runInplace (clContext: ClContext) workGroupSize =
25+
let runInplace<'a> (clContext: ClContext) workGroupSize =
2626

2727
let run =
2828
<@ fun (ndRange: Range1D) (positions: ClArray<int>) (positionsLength: int) (values: ClArray<'a>) (result: ClArray<'a>) (resultLength: int) ->

tests/GraphBLAS-sharp.Tests/Common/BitonicSort.fs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ open GraphBLAS.FSharp.Tests.Context
1010

1111
let logger = Log.create "BitonicSort.Tests"
1212

13-
let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (array: ('n * 'n * 'a) []) =
13+
let context = defaultContext.ClContext
14+
let config = { defaultConfig with endSize = 1000000 }
15+
16+
let wgSize = 32
17+
let q = defaultContext.Queue
18+
19+
let makeTest sort (array: ('n * 'n * 'a) []) =
1420
if array.Length > 0 then
1521
let projection (row: 'n) (col: 'n) (v: 'a) = row, col
1622

@@ -63,23 +69,18 @@ let makeTest (context: ClContext) (q: MailboxProcessor<_>) sort (array: ('n * 'n
6369
vals)
6470
|> compareArrays (=) actualVals expectedVals
6571

66-
let testFixtures<'a when 'a: equality> config wgSize context q =
67-
let sort: MailboxProcessor<_> -> ClArray<int> -> ClArray<int> -> ClArray<'a> -> unit =
68-
BitonicSort.sortKeyValuesInplace context wgSize
72+
let testFixtures<'a when 'a: equality> =
73+
let sort =
74+
BitonicSort.sortKeyValuesInplace<int, 'a> context wgSize
6975

70-
makeTest context q sort
76+
makeTest sort
7177
|> testPropertyWithConfig config (sprintf "Correctness on %A" typeof<'a>)
7278

7379
let tests =
74-
let context = defaultContext.ClContext
75-
let config = { defaultConfig with endSize = 1000000 }
76-
77-
let wgSize = 32
78-
let q = defaultContext.Queue
7980
q.Error.Add(fun e -> failwithf "%A" e)
8081

81-
[ testFixtures<int> config wgSize context q
82-
testFixtures<float> config wgSize context q
83-
testFixtures<byte> config wgSize context q
84-
testFixtures<bool> config wgSize context q ]
82+
[ testFixtures<int>
83+
testFixtures<float>
84+
testFixtures<byte>
85+
testFixtures<bool> ]
8586
|> testList "Backend.Common.BitonicSort tests"

tests/GraphBLAS-sharp.Tests/Common/Exists.fs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ open GraphBLAS.FSharp.Backend.Objects
1111
open GraphBLAS.FSharp.Backend.Quotes
1212

1313
let logger =
14-
Log.create "Vector.containsNonZero.Tests"
14+
Log.create "ClArray.containsNonZero.Tests"
1515

1616
let context = defaultContext.ClContext
1717

1818
let q = defaultContext.Queue
1919

20-
let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero containsNonZero (array: 'a []) =
20+
let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero exists (array: 'a []) =
2121

2222
if array.Length > 0 then
2323
let vector = createVectorFromArray Dense array isZero
2424

2525
let result =
2626
match vector.ToDevice context with
2727
| ClVector.Dense clArray ->
28-
let resultCell = containsNonZero q clArray
28+
let resultCell = exists q clArray
2929
let result = Array.zeroCreate 1
3030

3131
let res =
@@ -35,6 +35,8 @@ let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero containsN
3535

3636
res.[0]
3737

38+
| _ -> failwith "Unsupported vector format"
39+
3840
$"The results should be the same, vector : {vector}"
3941
|> Expect.equal result (Array.exists (not << isZero) array)
4042

@@ -46,52 +48,40 @@ let testFixtures =
4648
let getCorrectnessTestName datatype =
4749
sprintf "Correctness on %s, %A" datatype Dense
4850

49-
[ let containsNonZeroInt =
51+
[ let exists =
5052
ClArray.exists context wgSize Predicates.isSome
5153

52-
correctnessGenericTest<int> ((=) 0) containsNonZeroInt
54+
correctnessGenericTest<int> ((=) 0) exists
5355
|> testPropertyWithConfig config (getCorrectnessTestName "int")
5456

55-
let containsNonZeroByte =
56-
ClArray.exists context wgSize Predicates.isSome
57-
58-
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte
59-
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
60-
61-
let containsNonZeroFloat =
62-
ClArray.exists context wgSize Predicates.isSome
63-
64-
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat
65-
|> testPropertyWithConfig config (getCorrectnessTestName "float")
66-
67-
let containsNonZeroBool =
68-
ClArray.exists context wgSize Predicates.isSome
69-
70-
correctnessGenericTest<bool> ((=) false) containsNonZeroBool
71-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
72-
73-
let containsNonZeroInt =
74-
ClArray.exists context wgSize Predicates.isSome
75-
76-
correctnessGenericTest<int> ((=) 0) containsNonZeroInt (Array.create 1000 0)
57+
correctnessGenericTest<int> ((=) 0) exists (Array.create 1000 0)
7758
|> testPropertyWithConfig config (getCorrectnessTestName "int zeros")
7859

79-
let containsNonZeroByte =
60+
let exists =
8061
ClArray.exists context wgSize Predicates.isSome
8162

82-
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte (Array.create 1000 0uy)
63+
correctnessGenericTest<byte> ((=) 0uy) exists
64+
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
65+
66+
correctnessGenericTest<byte> ((=) 0uy) exists (Array.create 1000 0uy)
8367
|> testPropertyWithConfig config (getCorrectnessTestName "byte zeros")
8468

85-
let containsNonZeroFloat =
69+
let exists =
8670
ClArray.exists context wgSize Predicates.isSome
8771

88-
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat (Array.create 1000 0.0)
72+
correctnessGenericTest<float> ((=) 0.0) exists
73+
|> testPropertyWithConfig config (getCorrectnessTestName "float")
74+
75+
correctnessGenericTest<float> ((=) 0.0) exists (Array.create 1000 0.0)
8976
|> testPropertyWithConfig config (getCorrectnessTestName "float zeros")
9077

91-
let containsNonZeroBool =
78+
let exists =
9279
ClArray.exists context wgSize Predicates.isSome
9380

94-
correctnessGenericTest<bool> ((=) false) containsNonZeroBool (Array.create 1000 false)
81+
correctnessGenericTest<bool> ((=) false) exists
82+
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
83+
84+
correctnessGenericTest<bool> ((=) false) exists (Array.create 1000 false)
9585
|> testPropertyWithConfig config (getCorrectnessTestName "bool zeros") ]
9686

9787
let tests =

tests/GraphBLAS-sharp.Tests/Common/PrefixSum.fs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ let logger = Log.create "ClArray.PrefixSum.Tests"
1212

1313
let context = defaultContext.ClContext
1414

15-
let makeTest (q: MailboxProcessor<_>) scan plus zero isEqual (filter: 'a [] -> 'a []) (array: 'a []) =
15+
let config = defaultConfig
16+
17+
let wgSize = 128
18+
19+
let q = defaultContext.Queue
20+
21+
let makeTest scan plus zero isEqual (array: 'a []) =
1622
if array.Length > 0 then
17-
let array = filter array
1823

1924
logger.debug (
20-
eventX "Filtered array is {array}\n"
25+
eventX $"Array is %A{array}\n"
2126
>> setField "array" (sprintf "%A" array)
2227
)
2328

@@ -49,42 +54,30 @@ let makeTest (q: MailboxProcessor<_>) scan plus zero isEqual (filter: 'a [] -> '
4954
>> setField "expected" (sprintf "%A" expected)
5055
)
5156

52-
"Lengths of arrays should be equal"
53-
|> Expect.equal actual.Length expected.Length
54-
5557
"Total sums should be equal"
5658
|> Expect.equal actualSum expectedSum
5759

58-
for i in 0 .. actual.Length - 1 do
59-
Expect.isTrue
60-
(isEqual actual.[i] expected.[i])
61-
(sprintf "Arrays should be the same. Actual is \n%A, expected \n%A, input is \n%A" actual expected array)
60+
"Arrays should be the same"
61+
|> compareArrays isEqual actual expected
6262

63-
let testFixtures config wgSize q plus plusQ zero isEqual filter name =
63+
let testFixtures plus plusQ zero isEqual name =
6464
let scan =
6565
ClArray.prefixSumIncludeInplace plusQ context wgSize
6666

67-
makeTest q scan plus zero isEqual filter
67+
makeTest scan plus zero isEqual
6868
|> testPropertyWithConfig config (sprintf "Correctness on %s" name)
6969

7070
let tests =
71-
let config = defaultConfig
72-
73-
let wgSize = 128
74-
let q = defaultContext.Queue
7571
q.Error.Add(fun e -> failwithf "%A" e)
7672

77-
let filterFloats =
78-
Array.filter (System.Double.IsNaN >> not)
79-
80-
[ testFixtures config wgSize q (+) <@ (+) @> 0 (=) id "int add"
81-
testFixtures config wgSize q (+) <@ (+) @> 0uy (=) id "byte add"
82-
testFixtures config wgSize q max <@ max @> 0 (=) id "int max"
83-
testFixtures config wgSize q max <@ max @> 0.0 (=) filterFloats "float max"
84-
testFixtures config wgSize q max <@ max @> 0uy (=) id "byte max"
85-
testFixtures config wgSize q min <@ min @> System.Int32.MaxValue (=) id "int min"
86-
testFixtures config wgSize q min <@ min @> System.Double.MaxValue (=) filterFloats "float min"
87-
testFixtures config wgSize q min <@ min @> System.Byte.MaxValue (=) id "byte min"
88-
testFixtures config wgSize q (||) <@ (||) @> false (=) id "bool logic-or"
89-
testFixtures config wgSize q (&&) <@ (&&) @> true (=) id "bool logic-and" ]
73+
[ testFixtures (+) <@ (+) @> 0 (=) "int add"
74+
testFixtures (+) <@ (+) @> 0uy (=) "byte add"
75+
testFixtures max <@ max @> 0 (=) "int max"
76+
testFixtures max <@ max @> 0.0 (=) "float max"
77+
testFixtures max <@ max @> 0uy (=) "byte max"
78+
testFixtures min <@ min @> System.Int32.MaxValue (=) "int min"
79+
testFixtures min <@ min @> System.Double.MaxValue (=) "float min"
80+
testFixtures min <@ min @> System.Byte.MaxValue (=) "byte min"
81+
testFixtures (||) <@ (||) @> false (=) "bool logic-or"
82+
testFixtures (&&) <@ (&&) @> true (=) "bool logic-and" ]
9083
|> testList "Backend.Common.PrefixSum tests"

tests/GraphBLAS-sharp.Tests/Common/Reduce.fs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ let logger = Log.create "Reduce.Tests"
1212

1313
let context = Context.defaultContext.ClContext
1414

15-
let makeTest
16-
(q: MailboxProcessor<_>)
17-
(reduce: MailboxProcessor<_> -> ClArray<'a> -> ClCell<'a>)
18-
plus
19-
zero
20-
(filter: 'a [] -> 'a [])
21-
(array: 'a [])
22-
=
15+
let config = defaultConfig
2316

24-
let array = filter array
17+
let wgSize = 32
18+
let q = Context.defaultContext.Queue
19+
20+
let makeTest (reduce: MailboxProcessor<_> -> ClArray<'a> -> ClCell<'a>) plus zero (array: 'a []) =
2521

2622
if array.Length > 0 then
2723
let reduce = reduce q
@@ -57,30 +53,25 @@ let makeTest
5753
"Total sums should be equal"
5854
|> Expect.equal actualSum expectedSum
5955

60-
61-
let testFixtures config wgSize q plus plusQ zero filter name =
56+
let testFixtures plus plusQ zero name =
6257
let reduce = Reduce.reduce context wgSize plusQ
6358

64-
makeTest q reduce plus zero filter
59+
makeTest reduce plus zero
6560
|> testPropertyWithConfig config (sprintf "Correctness on %s" name)
6661

6762
let tests =
68-
let config = defaultConfig
69-
70-
let wgSize = 32
71-
let q = Context.defaultContext.Queue
7263
q.Error.Add(fun e -> failwithf "%A" e)
7364

7465
let filterFloats = Array.filter System.Double.IsNormal
7566

76-
[ testFixtures config wgSize q (+) <@ (+) @> 0 id "int add"
77-
testFixtures config wgSize q (+) <@ (+) @> 0uy id "byte add"
78-
testFixtures config wgSize q max <@ max @> System.Int32.MinValue id "int max"
79-
testFixtures config wgSize q max <@ max @> System.Double.MinValue filterFloats "float max"
80-
testFixtures config wgSize q max <@ max @> System.Byte.MinValue id "byte max"
81-
testFixtures config wgSize q min <@ min @> System.Int32.MaxValue id "int min"
82-
testFixtures config wgSize q min <@ min @> System.Double.MaxValue filterFloats "float min"
83-
testFixtures config wgSize q min <@ min @> System.Byte.MaxValue id "byte min"
84-
testFixtures config wgSize q (||) <@ (||) @> false id "bool logic-or"
85-
testFixtures config wgSize q (&&) <@ (&&) @> true id "bool logic-and" ]
67+
[ testFixtures (+) <@ (+) @> 0 "int add"
68+
testFixtures (+) <@ (+) @> 0uy "byte add"
69+
testFixtures max <@ max @> System.Int32.MinValue "int max"
70+
testFixtures max <@ max @> System.Double.MinValue "float max"
71+
testFixtures max <@ max @> System.Byte.MinValue "byte max"
72+
testFixtures min <@ min @> System.Int32.MaxValue "int min"
73+
testFixtures min <@ min @> System.Double.MaxValue "float min"
74+
testFixtures min <@ min @> System.Byte.MaxValue "byte min"
75+
testFixtures (||) <@ (||) @> false "bool logic-or"
76+
testFixtures (&&) <@ (&&) @> true "bool logic-and" ]
8677
|> testList "Backend.Common.Reduce tests"

tests/GraphBLAS-sharp.Tests/Common/Scatter.fs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ open GraphBLAS.FSharp.Tests.Utils
1010
let logger = Log.create "Scatter.Tests"
1111

1212
let context = defaultContext.ClContext
13+
let config = { defaultConfig with endSize = 1000000 }
1314

14-
let makeTest (context: ClContext) (q: MailboxProcessor<_>) scatter (array: (int * 'a) []) (result: 'a []) =
15+
let wgSize = 32
16+
17+
let q = defaultContext.Queue
18+
19+
let makeTest scatter (array: (int * 'a) []) (result: 'a []) =
1520
if array.Length > 0 then
1621
let expected = Array.copy result
1722

@@ -41,22 +46,16 @@ let makeTest (context: ClContext) (q: MailboxProcessor<_>) scatter (array: (int
4146
(sprintf "Arrays should be equal. Actual is \n%A, expected \n%A" actual expected)
4247
|> compareArrays (=) actual expected
4348

44-
let testFixtures<'a when 'a: equality> config wgSize context q =
45-
let scatter: MailboxProcessor<_> -> ClArray<int> -> ClArray<'a> -> ClArray<'a> -> unit =
46-
Scatter.runInplace context wgSize
49+
let testFixtures<'a when 'a: equality> =
50+
let scatter = Scatter.runInplace<'a> context wgSize
4751

48-
makeTest context q scatter
52+
makeTest scatter
4953
|> testPropertyWithConfig config (sprintf "Correctness on %A" typeof<'a>)
5054

5155
let tests =
52-
let context = defaultContext.ClContext
53-
let config = { defaultConfig with endSize = 1000000 }
54-
55-
let wgSize = 32
56-
let q = defaultContext.Queue
5756
q.Error.Add(fun e -> failwithf "%A" e)
5857

59-
[ testFixtures<int> config wgSize context q
60-
testFixtures<byte> config wgSize context q
61-
testFixtures<bool> config wgSize context q ]
58+
[ testFixtures<int>
59+
testFixtures<byte>
60+
testFixtures<bool> ]
6261
|> testList "Backend.Common.Scatter tests"

0 commit comments

Comments
 (0)