-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestfns.apln
141 lines (121 loc) · 6.81 KB
/
testfns.apln
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
:Namespace testfns
Assert←#.unittest.Assert
numOrChar←#.utils.numOrChar
change0sto1←#.utils.change0sto1
giveBound←{
type←(181⌶)⍵
type=11:0 1 ⍝ 11 : 1 bit boolean arrays
type=80:⎕UCS 0 255 ⍝ 80 : 8 bits character
type=83:¯128 127 ⍝ 83 : 8 bits signed integer
type=160:⎕UCS 0 65535 ⍝ 160 : 16 bits character
type=163:¯32768 32767 ⍝ 163 : 16 bits signed integer
type=320:⎕UCS 0 1114111 ⍝ 320 : 32 bits character
type=323:¯2147483648 2147483647 ⍝ 323 : 32 bits signed integer
type=645:(⌈/⍬)(⌊/⍬) ⍝ 645 : 64 bits floating
⎕FR←1287 ⋄ type=1287:(⌈/⍬)(⌊/⍬) ⋄ ⎕FR←645 ⍝ 1287: 128 bits decimal
type=1289:{(-⍵),⍵,(+⍵),(-+⍵)}(⌈/⍬)(⊣+¯11○⊢)(⌊/⍬) ⍝ 1289: 128 bits complex
}
⍝ shouldNotRandTest checks what test is running and returns a 0
⍝ if the test mentioned in ⍵ are running to simplify the if case
⍝ in RandModelTest to skip the test for certain primitives
shouldNotRandTest←{~∨/⊃,/{∨/⍵⍷↑#.unittest.tests}¨⍵}
⍝ -------------------------
⍝ Generating Random Numbers
⍝ -------------------------
⍝ generating big random numbers upto 1E6144
rollLimit←{⎕fr←1287 ⋄ ⍵<¯1+2*63 ⋄⎕fr←645} ⍝ limit of the roll(X?Y) primitive
⍝ generate 3 to 18 random numbers smaller than 1E17 and multiply them to get
⍝ a random number between 1E49 to 1E306
bigRand308←{×/?(2+?16)/1E17}
⍝ takes a number from bigRand308 and picks 3 to 20 random numbers and multiplies
⍝ all of them to generate a random number between 1E308 to 1E6144
bigRand308to6144←{×/change0sto1(2+?18)↑}
⍝ rarg bigRand bound
⍝ uses bigRand308to6144 and bigRand308 to use the required random number generator
⍝ to generate a appropriate random number based on the bounds and the datatype
⍝
⍝ <---cmplx---> <-----decimal numbers running with 128 bit arithmetic-----> <numbers under ? limit> <floating point nos>
bigRand←{1289≡⎕dr ⍵:⍺⋄(1287≡⎕dr ⍵) ∧ ⎕fr≡1287: {bigRand308to6144 bigRand308¨⍳20}¨⍺ ⋄ ⍺{rollLimit ⍵:?(≢⍺)/⍵ ⋄ (bigRand308¨⍺)}⍵}
⍝ todo: add this for characters when this runs for it
⍝ ⎕UCS ⍵ bigRand (⎕UCS 2⊃⍺)
⍝ normal cmplx number random:
⍝ {1⊃⍵ (⊣+¯11○⊢) 2⊃⍵}¨?⌊|(9 11○⊂)¨rarg
⍝ uses the appropriate random number generator for mixed, numeric or char arrays
⍝ ⍝ mixed array ⍝ numeric ⍝ char
getRand←{¯1≡numOrChar ⍺:⍺ ⋄ ⍺{numOrChar ⍵: ⍺ bigRand 2⊃⍵ ⋄ ⍺}⍵}
⍝ -----------------------------
⍝ Generating Random Numbers End
⍝ -----------------------------
⍝ 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;⎕CT;⎕DCT;⎕FR;⎕IO;⎕DIV;expectedR;larg;rarg;tID;tCmt;actualR;actualRE;shape;actualRS;shapeW0;actualRSW0;val;randr;trimmedrarg
: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
:If (shouldNotRandTest 'residue' 'floor' 'multiply')
boundR←giveBound rarg ⍝ get boundaries of rarg
⎕fr←645
nrarg←rarg getRand boundR ⍝ generate a random element in range of rarg
⎕fr←3⊃quadparams
:If val=2
boundL←giveBound larg
⎕fr←645
nlarg←larg getRand boundL
⎕fr←3⊃quadparams
:EndIf
tRes,←('RandModelTest',tID) tCmt Assert ({val≡2:(nlarg model nrarg)≡nlarg op nrarg ⋄ (model¨ nrarg)≡op nrarg}⍬)
: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
∇
:EndNamespace