Skip to content

Commit 25b14d7

Browse files
committed
refactor: separate Vector.assignByMask tests
1 parent 1858c02 commit 25b14d7

3 files changed

Lines changed: 114 additions & 52 deletions

File tree

tests/GraphBLAS-sharp.Tests/Helpers.fs

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ module Generators =
5555

5656
let rec normalFloat32Generator (random: System.Random) =
5757
gen {
58-
let buffer = Array.zeroCreate<byte> 4
59-
random.NextBytes buffer
60-
61-
let result = System.BitConverter.ToSingle(buffer, 0)
58+
let result = random.NextSingle()
6259

6360
if System.Single.IsNormal result then
6461
return result
@@ -75,7 +72,7 @@ module Generators =
7572
let! sparsity = sparsityGen
7673

7774
logger.debug (
78-
eventX "Sparcity is {sp} of {ms}"
75+
eventX "Sparsity is {sp} of {ms}"
7976
>> setField "sp" sparsity
8077
>> setField "ms" maxSparsity
8178
)
@@ -145,10 +142,10 @@ module Generators =
145142
type SingleSymmetricalMatrix() =
146143
static let matrixGenerator (valuesGenerator: Gen<'a>) =
147144
gen {
148-
let! nrows, _ = dimension2DGenerator
149-
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, nrows)
145+
let! nRows, _ = dimension2DGenerator
146+
let! matrix = valuesGenerator |> Gen.array2DOfDim (nRows, nRows)
150147

151-
for row in 1 .. nrows - 1 do
148+
for row in 1 .. nRows - 1 do
152149
for col in 0 .. row - 1 do
153150
matrix.[row, col] <- matrix.[col, row]
154151

@@ -202,9 +199,16 @@ module Generators =
202199
type PairOfSparseMatricesOfEqualSize() =
203200
static let pairOfMatricesOfEqualSizeGenerator (valuesGenerator: Gen<'a>) =
204201
gen {
205-
let! nrows, ncols = dimension2DGenerator
206-
let! matrixA = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
207-
let! matrixB = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
202+
let! nRows, nColumns = dimension2DGenerator
203+
204+
let! matrixA =
205+
valuesGenerator
206+
|> Gen.array2DOfDim (nRows, nColumns)
207+
208+
let! matrixB =
209+
valuesGenerator
210+
|> Gen.array2DOfDim (nRows, nColumns)
211+
208212
return (matrixA, matrixB)
209213
}
210214

@@ -255,10 +259,14 @@ module Generators =
255259
type PairOfSparseMatrixOAndVectorsCompatibleSize() =
256260
static let pairOfMatrixAndVectorOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
257261
gen {
258-
let! nrows, ncols = dimension2DGenerator
259-
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
260-
let! vector = valuesGenerator |> Gen.arrayOfLength ncols
261-
let! mask = Arb.generate<bool> |> Gen.arrayOfLength nrows
262+
let! nRows, nColumns = dimension2DGenerator
263+
264+
let! matrix =
265+
valuesGenerator
266+
|> Gen.array2DOfDim (nRows, nColumns)
267+
268+
let! vector = valuesGenerator |> Gen.arrayOfLength nColumns
269+
let! mask = Arb.generate<bool> |> Gen.arrayOfLength nRows
262270
return (matrix, vector, mask)
263271
}
264272

@@ -316,10 +324,14 @@ module Generators =
316324
type PairOfSparseVectorAndMatrixOfCompatibleSize() =
317325
static let pairOfVectorAndMatrixOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
318326
gen {
319-
let! nrows, ncols = dimension2DGenerator
320-
let! vector = valuesGenerator |> Gen.arrayOfLength nrows
321-
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
322-
let! mask = Arb.generate<bool> |> Gen.arrayOfLength ncols
327+
let! nRows, nColumns = dimension2DGenerator
328+
let! vector = valuesGenerator |> Gen.arrayOfLength nRows
329+
330+
let! matrix =
331+
valuesGenerator
332+
|> Gen.array2DOfDim (nRows, nColumns)
333+
334+
let! mask = Arb.generate<bool> |> Gen.arrayOfLength nColumns
323335
return (vector, matrix, mask)
324336
}
325337

@@ -370,15 +382,15 @@ module Generators =
370382
type PairOfMatricesOfCompatibleSize() =
371383
static let pairOfMatricesOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
372384
gen {
373-
let! nrowsA, ncolsA, ncolsB = dimension3DGenerator
385+
let! nRowsA, nColsA, nColsB = dimension3DGenerator
374386

375387
let! matrixA =
376388
valuesGenerator
377-
|> Gen.array2DOfDim (nrowsA, ncolsA)
389+
|> Gen.array2DOfDim (nRowsA, nColsA)
378390

379391
let! matrixB =
380392
valuesGenerator
381-
|> Gen.array2DOfDim (ncolsA, ncolsB)
393+
|> Gen.array2DOfDim (nColsA, nColsB)
382394

383395
return (matrixA, matrixB)
384396
}
@@ -430,19 +442,19 @@ module Generators =
430442
type PairOfMatricesOfCompatibleSizeWithMask() =
431443
static let pairOfMatricesOfCompatibleSizeWithMaskGenerator (valuesGenerator: Gen<'a>) =
432444
gen {
433-
let! nrowsA, ncolsA, ncolsB = dimension3DGenerator
445+
let! nRowsA, nColumnsA, nColumnsB = dimension3DGenerator
434446

435447
let! matrixA =
436448
valuesGenerator
437-
|> Gen.array2DOfDim (nrowsA, ncolsA)
449+
|> Gen.array2DOfDim (nRowsA, nColumnsA)
438450

439451
let! matrixB =
440452
valuesGenerator
441-
|> Gen.array2DOfDim (ncolsA, ncolsB)
453+
|> Gen.array2DOfDim (nColumnsA, nColumnsB)
442454

443455
let! mask =
444456
(genericSparseGenerator false Arb.generate<bool>)
445-
<| Gen.array2DOfDim (nrowsA, ncolsB)
457+
<| Gen.array2DOfDim (nRowsA, nColumnsB)
446458

447459
return (matrixA, matrixB, mask)
448460
}
@@ -598,7 +610,7 @@ module Generators =
598610
arrayOfAscendingKeysGenerator <| Arb.generate<int>
599611
|> Arb.fromGen
600612

601-
static member FloatType() = // (float32Generator <| System.Random())
613+
static member FloatType() =
602614
arrayOfAscendingKeysGenerator
603615
<| (Arb.Default.NormalFloat()
604616
|> Arb.toGen
@@ -778,7 +790,9 @@ module Utils =
778790
abs (x - y) < Accuracy.medium.absolute
779791
|| x.Equals y
780792

781-
let float32IsEqual x y = abs (x - y) < 0.01f || x.Equals y
793+
let inline float32IsEqual x y =
794+
float (abs (x - y)) < Accuracy.medium.absolute
795+
|| x.Equals y
782796

783797
let vectorToDenseVector =
784798
function
@@ -830,17 +844,13 @@ module Utils =
830844
array
831845

832846
let compareArrays areEqual (actual: 'a []) (expected: 'a []) message =
833-
sprintf "%s. Lengths should be equal. Actual is %A, expected %A" message actual expected
847+
$"%s{message}. Lengths should be equal. Actual is %A{actual}, expected %A{expected}"
834848
|> Expect.equal actual.Length expected.Length
835849

836850
for i in 0 .. actual.Length - 1 do
837851
if not (areEqual actual.[i] expected.[i]) then
838-
sprintf "%s. Arrays differ at position %A of %A. Actual value is %A, expected %A"
839-
<| message
840-
<| i
841-
<| (actual.Length - 1)
842-
<| actual.[i]
843-
<| expected.[i]
852+
$"%s{message}. Arrays differ at position %A{i} of %A{actual.Length - 1}.
853+
Actual value is %A{actual.[i]}, expected %A{expected.[i]}"
844854
|> failtestf "%s"
845855

846856
let listOfUnionCases<'a> =

tests/GraphBLAS-sharp.Tests/Matrix/Map2.fs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,6 @@ let testFixturesEWiseAddAtLeastOneToCOO case =
252252
|> correctnessGenericTest 0.0f (+) float32Add floatToCOO Utils.float32IsEqual q
253253
|> testPropertyWithConfig config (getCorrectnessTestName "float32")
254254

255-
let float32Add =
256-
Matrix.map2AtLeastOneToCOO context ArithmeticOperations.float32SumAtLeastOne wgSize
257-
258-
let floatToCOO = Matrix.toCOO context wgSize
259-
260-
case
261-
|> correctnessGenericTest 0.0f (+) float32Add floatToCOO Utils.float32IsEqual q
262-
|> testPropertyWithConfig config (getCorrectnessTestName "float32")
263-
264255
let byteAdd =
265256
Matrix.map2AtLeastOneToCOO context ArithmeticOperations.byteSumAtLeastOne wgSize
266257

tests/GraphBLAS-sharp.Tests/Vector/AssignByMask.fs

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ let config = Utils.defaultConfig
2222

2323
let wgSize = 32
2424

25+
let getCorrectnessTestName case datatype =
26+
$"Correctness on %s{datatype}, vector: %A{case.Format}"
27+
2528
let checkResult isZero isComplemented (actual: Vector<'a>) (vector: 'a []) (mask: 'a []) (value: 'a) =
2629

2730
let expectedArray = Array.zeroCreate vector.Length
@@ -96,12 +99,74 @@ let makeTest<'a when 'a: struct and 'a: equality>
9699
| ex when ex.Message = "InvalidBufferSize" -> ()
97100
| ex -> raise ex
98101

99-
let testFixtures isComplemented case =
100-
let getCorrectnessTestName datatype =
101-
$"Correctness on %s{datatype}, vector: %A{case.Format}"
102+
let testFixtures case =
103+
let config = Utils.defaultConfig
104+
105+
let context = case.TestContext.ClContext
106+
107+
let getCorrectnessTestName = getCorrectnessTestName case
108+
109+
let isComplemented = false
110+
111+
[ let intFill =
112+
Vector.assignByMask context Mask.assign wgSize
113+
114+
let intToCoo = Vector.toDense context wgSize
115+
116+
case
117+
|> makeTest ((=) 0) alwaysTrue intToCoo intFill isComplemented
118+
|> testPropertyWithConfig config (getCorrectnessTestName "int")
119+
120+
if Utils.isFloat64Available context.ClDevice then
121+
let floatFill =
122+
Vector.assignByMask context Mask.assign wgSize
123+
124+
let floatToCoo = Vector.toDense context wgSize
125+
126+
case
127+
|> makeTest (Utils.floatIsEqual 0.0) Double.IsNormal floatToCoo floatFill isComplemented
128+
|> testPropertyWithConfig config (getCorrectnessTestName "float")
129+
130+
let float32Fill =
131+
Vector.assignByMask context Mask.assign wgSize
132+
133+
let float32ToCoo = Vector.toDense context wgSize
134+
135+
case
136+
|> makeTest (Utils.float32IsEqual 0.0f) Single.IsNormal float32ToCoo float32Fill isComplemented
137+
|> testPropertyWithConfig config (getCorrectnessTestName "float32")
138+
139+
let byteFill =
140+
Vector.assignByMask context Mask.assign wgSize
141+
142+
let byteToCoo = Vector.toDense context wgSize
143+
144+
case
145+
|> makeTest ((=) 0uy) alwaysTrue byteToCoo byteFill isComplemented
146+
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
147+
148+
let boolFill =
149+
Vector.assignByMask context Mask.assign wgSize
150+
151+
let boolToCoo = Vector.toDense context wgSize
152+
153+
case
154+
|> makeTest ((=) false) alwaysTrue boolToCoo boolFill isComplemented
155+
|> testPropertyWithConfig config (getCorrectnessTestName "bool") ]
156+
157+
158+
let tests =
159+
operationGPUTests "Backend.Vector.assignByMask tests"
160+
<| testFixtures
161+
162+
let testFixturesComplemented case =
163+
164+
let getCorrectnessTestName = getCorrectnessTestName case
102165

103166
let context = case.TestContext.ClContext
104167

168+
let isComplemented = true
169+
105170
[ let intFill =
106171
Vector.assignByMaskComplemented context Mask.assign wgSize
107172

@@ -148,10 +213,6 @@ let testFixtures isComplemented case =
148213
|> makeTest ((=) false) alwaysTrue boolToCoo boolFill isComplemented
149214
|> testPropertyWithConfig config (getCorrectnessTestName "bool") ]
150215

151-
let tests =
152-
operationGPUTests "Backend.Vector.assignByMask tests"
153-
<| testFixtures false
154-
155216
let complementedTests =
156217
operationGPUTests "Backend.Vector.assignByMaskComplemented tests"
157-
<| testFixtures true
218+
<| testFixturesComplemented

0 commit comments

Comments
 (0)