-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtestfns.apln
More file actions
170 lines (145 loc) · 6.87 KB
/
testfns.apln
File metadata and controls
170 lines (145 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
:Namespace testfns
Assert←#.unittest.Assert
⍝ Run the operand function and return either (1 Rslt) or (0 (⎕DMX.EN Message))
runOrErr←{
0::⊂0 ⎕DMX.EN ⍝ We should care about the ⎕DMX message, but that is hard to model, so we don't.
⍺←⊢
⊂1(⍺ ⍺⍺ ⍵)
}
⍝ isCurrentlyRunning checks what test is running and returns a 1
⍝ if the test mentioned in ⍵ is running, to simplify the if case
⍝ in _RunVariations_ to skip part of the test for certain primitives.
isCurrentlyRunning←{∨/⊃,/{∨/⍵⍷↑#.unittest.tests}¨⍵}
⍝ tests that don't use this are:
⍝ use a different runvariations:
⍝ - unique
⍝ - uniquemask
⍝ - indexof
⍝ - membership
⍝ Run Variations of each test with normal, empty and multiple shaped data
∇ {tRes}←tData(model _RunVariations_ op)exp;actualR;actualRE;actualRS;actualRSW0;expectedR;larg;nlarg;nrarg;randr;rarg;shape;shapeW0;skipRandom;tCmt;tID;trimmedrarg;val;⎕CT;⎕DCT;⎕DIV;⎕FR;⎕IO
:If 3≡≢exp ⍝ dyadic op
val←2 ⍝ valence
(expectedR larg rarg)←exp
:Else ⍝ monadic op
val←1
(expectedR rarg)←exp
:EndIf
(tID tCmt quadparams)←tData
⎕CT ⎕DCT ⎕FR ⎕IO ⎕DIV←quadparams
tRes←⍬
⍝ normal
actualR←{val≡2:larg op rarg ⋄ op rarg}⍬
tRes,←(tID tCmt)Assert expectedR≡actualR
:If val≡1
⍝ scalar
randr←?≢rarg
trimmedrarg←randr↓rarg ⍝ so that not always the first is selected
shape←⍬ ⍝ scalar shape
actualRS←op shape⍴trimmedrarg
tRes,←('Scalar',tID)tCmt Assert(shape⍴randr↓expectedR)≡actualRS
:EndIf
⍝ empty
actualRE←{val≡2:(0⍴larg)op(0⍴rarg) ⋄ op(0⍴rarg)}⍬ ⍝ 0 in the shape means we have no elements in the array, i.e. it's empty.
tRes,←('EmptyL',tID)tCmt Assert ⍬≡actualRE ⍝ empty array is expectedR
⍝ different shapes
shape←?(?4)/4
actualRS←{val≡2:(shape⍴larg)op(shape⍴rarg) ⋄ op(shape⍴rarg)}⍬
tRes,←('Multiple',tID)tCmt Assert(shape⍴expectedR)≡actualRS
⍝ different shapes with 0 in shape
shapeW0←(0@(?(≢shape)))shape
actualRSW0←{val≡2:(shapeW0⍴larg)op(shapeW0⍴rarg) ⋄ op(shapeW0⍴rarg)}⍬
tRes,←('ShapeW0',tID)tCmt Assert(shapeW0⍴0)≡actualRSW0
⍝ testing larg and rarg specifically from model
tRes,←('ModelTest',tID)tCmt Assert({val≡2:(larg model rarg)≡larg op rarg ⋄ (model¨rarg)≡op rarg}⍬)
⍝ This test creates a random element based on the datatype of the rarg and larg
⍝ it is then tested using models of the function.
⍝ The purpose of this test is to use the entire range of the datatype so a dataset
⍝ is generated in the longer run to test more data.
⍝
⍝ skipping big rand test for mentioned primitives because of issues with the model
skipRandom←0
skipRandom∨←isCurrentlyRunning'residue' 'floor' 'multiply' 'divide'
skipRandom∨←(1289 1287∊⍨⎕DR rarg)∧isCurrentlyRunning'magnitude'
:If ~skipRandom
nrarg←#.random.VariationOf rarg
:If val=2
nlarg←#.random.VariationOf larg
:EndIf
:If val=2
actualR←nlarg(op runOrErr)nrarg
expectedR←nlarg(model runOrErr)nrarg
:Else
actualR←(op runOrErr)nrarg
expectedR←(model runOrErr)nrarg
:EndIf
tRes,←('RandModelTest',tID)tCmt Assert expectedR≡actualR
:EndIf
⍝ todo:
⍝ new variations needs more tests of:
⍝ lengths of arrays from 1 to 10000
⍝ and
⍝ of kind
⍝ scalar
⍝ vector
⍝ tall matrix, 11-column matrix with 10*0 1 2 4 rows
⍝ wide matrix, 11-row matrix with 10*0 1 2 4 columns
⍝ square matrix with ⌈10*0 1 2 4×0.6 (1 4 16 252) rows/columns
∇
⍝ Run Variations of each test with normal, empty and multiple shaped data.
⍝ This operator is similar to _RunVariations_, but runs the model function and compares
⍝ the produced result/error messages with the primitive.
∇ {tRes}←spec(rawModel _RunVariationsWithModel_ rawOp)args;ChkEqual;Eval;quadparams;shape;tCmt;tID;⎕CT;⎕DCT;⎕DIV;⎕FR;⎕IO
tRes←⍬
(tID tCmt quadparams)←spec
⎕CT ⎕DCT ⎕FR ⎕IO ⎕DIV←quadparams
⍝ Run the operand function either monadically or dyadically, depending on the
⍝ argument to _RunVariationsWithModel_
Eval←{
2=≢args:⍺ ⍺⍺ ⍵
1=≢args:⍺⍺ ⍵
⎕SIGNAL 5 ⍝ length error
}
⍝ run a transformation function (⍺⍺) on both arguments,
⍝ and then compare the result produced by the model and
⍝ the primitive, when the transformed arguments are applied.
⍝ This checks that the model produces the expected results
⍝ and the expected error messages.
ChkEqual←{
(larg rarg)←2⍴⍺⍺¨args
model←rawModel runOrErr
op←rawOp runOrErr
expected←larg model Eval rarg
actual←larg op Eval rarg
tRes,←(tID,' - ',⍵)tCmt Assert expected≡actual
}
⍝ Base test: check that the function behaves as the model with the provided data
{⍵}ChkEqual'Base'
⍝ scalar test: Pick a random item and check.
{⍵[⊂?⍴⍵]}ChkEqual'RandomScalar'
⍝ Empty test: check that the function behaves as the model when given an empty vector of the given type.
{0⍴⍵}ChkEqual'Empty'
⍝ High rank test: generate a random shape, and check.
shape←{⎕IO←1 ⋄ ?(?⍵)/⍵}4
{shape⍴⍵}ChkEqual'RandomHighRank'
⍝ High rank with 0 in shape test
shape[?≢shape]←0
{shape⍴⍵}ChkEqual'RandomEmptyHighRank'
⍝ Random data test:
⍝ This test creates a random element based on the datatype of the rarg and larg
⍝ it is then tested using models of the function.
⍝ The purpose of this test is to use the entire range of the datatype so a dataset
⍝ is generated in the longer run to test more data.
#.random.VariationOf ChkEqual'RandModelTest'
⍝ todo:
⍝ new variations needs more tests of:
⍝ lengths of arrays from 1 to 10000
⍝ and
⍝ of kind
⍝ scalar
⍝ vector
⍝ tall matrix, 11-column matrix with 10*0 1 2 4 rows
⍝ wide matrix, 11-row matrix with 10*0 1 2 4 columns
⍝ square matrix with ⌈10*0 1 2 4×0.6 (1 4 16 252) rows/columns
∇
:EndNamespace