@@ -59,6 +59,44 @@ tape( 'the function calculates the population variance of a strided array', func
59
59
t . end ( ) ;
60
60
} ) ;
61
61
62
+ tape ( 'the function calculates the population variance of a strided array (accessors)' , function test ( t ) {
63
+ var x ;
64
+ var v ;
65
+
66
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 0.0 , 3.0 ] ;
67
+ v = variancetk ( x . length , 0 , toAccessorArray ( x ) , 1 ) ;
68
+ t . strictEqual ( v , 53.5 / x . length , 'returns expected value' ) ;
69
+
70
+ x = [ - 4.0 , - 4.0 ] ;
71
+ v = variancetk ( x . length , 0 , toAccessorArray ( x ) , 1 ) ;
72
+ t . strictEqual ( v , 0.0 , 'returns expected value' ) ;
73
+
74
+ x = [ NaN , 4.0 ] ;
75
+ v = variancetk ( x . length , 0 , toAccessorArray ( x ) , 1 ) ;
76
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
77
+
78
+ t . end ( ) ;
79
+ } ) ;
80
+
81
+ tape ( 'the function calculates the sample variance of a strided array' , function test ( t ) {
82
+ var x ;
83
+ var v ;
84
+
85
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 0.0 , 3.0 ] ;
86
+ v = variancetk ( x . length , 1 , x , 1 ) ;
87
+ t . strictEqual ( v , 53.5 / ( x . length - 1 ) , 'returns expected value' ) ;
88
+
89
+ x = [ - 4.0 , - 4.0 ] ;
90
+ v = variancetk ( x . length , 1 , x , 1 ) ;
91
+ t . strictEqual ( v , 0.0 , 'returns expected value' ) ;
92
+
93
+ x = [ NaN , 4.0 ] ;
94
+ v = variancetk ( x . length , 1 , x , 1 ) ;
95
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
96
+
97
+ t . end ( ) ;
98
+ } ) ;
99
+
62
100
tape ( 'the function calculates the sample variance of a strided array (accessors)' , function test ( t ) {
63
101
var x ;
64
102
var v ;
@@ -93,6 +131,21 @@ tape( 'if provided an `N` parameter less than or equal to `0`, the function retu
93
131
t . end ( ) ;
94
132
} ) ;
95
133
134
+ tape ( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)' , function test ( t ) {
135
+ var x ;
136
+ var v ;
137
+
138
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 3.0 ] ;
139
+
140
+ v = variancetk ( 0 , 1 , toAccessorArray ( x ) , 1 ) ;
141
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
142
+
143
+ v = variancetk ( - 1 , 1 , toAccessorArray ( x ) , 1 ) ;
144
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
145
+
146
+ t . end ( ) ;
147
+ } ) ;
148
+
96
149
tape ( 'if provided an `N` parameter equal to `1`, the function returns a population variance of `0`' , function test ( t ) {
97
150
var x ;
98
151
var v ;
@@ -105,6 +158,18 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns a populat
105
158
t . end ( ) ;
106
159
} ) ;
107
160
161
+ tape ( 'if provided an `N` parameter equal to `1`, the function returns a population variance of `0` (accessors)' , function test ( t ) {
162
+ var x ;
163
+ var v ;
164
+
165
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 3.0 ] ;
166
+
167
+ v = variancetk ( 1 , 0 , toAccessorArray ( x ) , 1 ) ;
168
+ t . strictEqual ( v , 0.0 , 'returns expected value' ) ;
169
+
170
+ t . end ( ) ;
171
+ } ) ;
172
+
108
173
tape ( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`' , function test ( t ) {
109
174
var x ;
110
175
var v ;
@@ -120,6 +185,21 @@ tape( 'if provided a `correction` parameter yielding `N-correction` less than or
120
185
t . end ( ) ;
121
186
} ) ;
122
187
188
+ tape ( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)' , function test ( t ) {
189
+ var x ;
190
+ var v ;
191
+
192
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 3.0 ] ;
193
+
194
+ v = variancetk ( x . length , x . length , toAccessorArray ( x ) , 1 ) ;
195
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
196
+
197
+ v = variancetk ( x . length , x . length + 1 , toAccessorArray ( x ) , 1 ) ;
198
+ t . strictEqual ( isnan ( v ) , true , 'returns expected value' ) ;
199
+
200
+ t . end ( ) ;
201
+ } ) ;
202
+
123
203
tape ( 'the function supports a `stride` parameter' , function test ( t ) {
124
204
var x ;
125
205
var v ;
@@ -216,6 +296,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns `0`',
216
296
t . end ( ) ;
217
297
} ) ;
218
298
299
+ tape ( 'if provided a `stride` parameter equal to `0`, the function returns `0` (accessors)' , function test ( t ) {
300
+ var x ;
301
+ var v ;
302
+
303
+ x = [ 1.0 , - 2.0 , - 4.0 , 5.0 , 3.0 ] ;
304
+
305
+ v = variancetk ( x . length , 1 , toAccessorArray ( x ) , 0 ) ;
306
+ t . strictEqual ( v , 0.0 , 'returns expected value' ) ;
307
+
308
+ t . end ( ) ;
309
+ } ) ;
310
+
219
311
tape ( 'the function supports view offsets' , function test ( t ) {
220
312
var x0 ;
221
313
var x1 ;
@@ -240,3 +332,28 @@ tape( 'the function supports view offsets', function test( t ) {
240
332
241
333
t . end ( ) ;
242
334
} ) ;
335
+
336
+ tape ( 'the function supports view offsets (accessors)' , function test ( t ) {
337
+ var x0 ;
338
+ var x1 ;
339
+ var v ;
340
+
341
+ x0 = new Float64Array ( [
342
+ 2.0 ,
343
+ 1.0 , // 0
344
+ 2.0 ,
345
+ - 2.0 , // 1
346
+ - 2.0 ,
347
+ 2.0 , // 2
348
+ 3.0 ,
349
+ 4.0 , // 3
350
+ 6.0
351
+ ] ) ;
352
+
353
+ x1 = new Float64Array ( x0 . buffer , x0 . BYTES_PER_ELEMENT * 1 ) ; // start at 2nd element
354
+
355
+ v = variancetk ( 4 , 1 , toAccessorArray ( x1 ) , 2 ) ;
356
+ t . strictEqual ( v , 6.25 , 'returns expected value' ) ;
357
+
358
+ t . end ( ) ;
359
+ } ) ;
0 commit comments