@@ -191,6 +191,31 @@ tape( 'the function throws an error if provided an invalid `order` option', func
191
191
}
192
192
} ) ;
193
193
194
+ tape ( 'the function throws an error if provided a callback argument which is not a function' , function test ( t ) {
195
+ var values ;
196
+ var i ;
197
+
198
+ values = [
199
+ '5' ,
200
+ 5 ,
201
+ NaN ,
202
+ true ,
203
+ false ,
204
+ null ,
205
+ void 0
206
+ ] ;
207
+ for ( i = 0 ; i < values . length ; i ++ ) {
208
+ t . throws ( badValue ( values [ i ] ) , TypeError , 'throws an error when provided ' + values [ i ] ) ;
209
+ }
210
+ t . end ( ) ;
211
+
212
+ function badValue ( value ) {
213
+ return function badValue ( ) {
214
+ flattenBy ( zeros ( [ 2 , 2 , 2 ] ) , { } , value ) ;
215
+ } ;
216
+ }
217
+ } ) ;
218
+
194
219
tape ( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, contiguous)' , function test ( t ) {
195
220
var expected ;
196
221
var xbuf ;
@@ -291,6 +316,65 @@ tape( 'the function supports specifying the callback execution context (row-majo
291
316
}
292
317
} ) ;
293
318
319
+ tape ( 'the function supports specifying the callback execution context (row-major, contiguous, options)' , function test ( t ) {
320
+ var expected ;
321
+ var xbuf ;
322
+ var opts ;
323
+ var ord ;
324
+ var ctx ;
325
+ var sh ;
326
+ var st ;
327
+ var dt ;
328
+ var o ;
329
+ var x ;
330
+ var y ;
331
+
332
+ dt = 'float64' ;
333
+ ord = 'row-major' ;
334
+ sh = [ 2 , 2 , 2 ] ;
335
+ st = shape2strides ( sh , ord ) ;
336
+ o = strides2offset ( sh , st ) ;
337
+
338
+ /*
339
+ * [
340
+ * [
341
+ * [ 1.0, 2.0 ],
342
+ * [ 3.0, 4.0 ]
343
+ * ],
344
+ * [
345
+ * [ 5.0, 6.0 ],
346
+ * [ 7.0, 8.0 ]
347
+ * ]
348
+ * ]
349
+ */
350
+ xbuf = new Float64Array ( [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] ) ;
351
+ x = new ndarray ( dt , xbuf , sh , st , o , ord ) ;
352
+ ctx = {
353
+ 'count' : 1
354
+ } ;
355
+ opts = {
356
+ 'depth' : 2
357
+ } ;
358
+
359
+ y = flattenBy ( x , opts , clbk , ctx ) ;
360
+ expected = new Float64Array ( [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] ) ;
361
+
362
+ t . notEqual ( y , x , 'returns expected value' ) ;
363
+ t . notEqual ( getData ( y ) , xbuf , 'returns expected value' ) ;
364
+ t . strictEqual ( isSameFloat64Array ( getData ( y ) , expected ) , true , 'returns expected value' ) ;
365
+ t . deepEqual ( getShape ( y ) , [ 8 ] , 'returns expected value' ) ;
366
+ t . strictEqual ( getDType ( y ) , dt , 'returns expected value' ) ;
367
+ t . strictEqual ( getOrder ( y ) , 'row-major' , 'returns expected value' ) ;
368
+ t . strictEqual ( ctx . count , 9 , 'returns expected value' ) ;
369
+
370
+ t . end ( ) ;
371
+
372
+ function clbk ( v ) {
373
+ this . count += 1 ; // eslint-disable-line no-invalid-this
374
+ return v ;
375
+ }
376
+ } ) ;
377
+
294
378
tape ( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (row-major, non-contiguous)' , function test ( t ) {
295
379
var expected ;
296
380
var xbuf ;
@@ -436,6 +520,65 @@ tape( 'the function supports specifying the callback execution context (column-m
436
520
}
437
521
} ) ;
438
522
523
+ tape ( 'the function supports specifying the callback execution context (column-major, contiguous, options)' , function test ( t ) {
524
+ var expected ;
525
+ var xbuf ;
526
+ var opts ;
527
+ var ord ;
528
+ var ctx ;
529
+ var sh ;
530
+ var st ;
531
+ var dt ;
532
+ var o ;
533
+ var x ;
534
+ var y ;
535
+
536
+ dt = 'float64' ;
537
+ ord = 'column-major' ;
538
+ sh = [ 2 , 2 , 2 ] ;
539
+ st = shape2strides ( sh , ord ) ;
540
+ o = strides2offset ( sh , st ) ;
541
+
542
+ /*
543
+ * [
544
+ * [
545
+ * [ 1.0, 2.0 ],
546
+ * [ 3.0, 4.0 ]
547
+ * ],
548
+ * [
549
+ * [ 5.0, 6.0 ],
550
+ * [ 7.0, 8.0 ]
551
+ * ]
552
+ * ]
553
+ */
554
+ xbuf = new Float64Array ( [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 ] ) ;
555
+ x = new ndarray ( dt , xbuf , sh , st , o , ord ) ;
556
+ ctx = {
557
+ 'count' : 1
558
+ } ;
559
+ opts = {
560
+ 'depth' : 2
561
+ } ;
562
+
563
+ y = flattenBy ( x , opts , clbk , ctx ) ;
564
+ expected = new Float64Array ( [ 1.0 , 5.0 , 3.0 , 7.0 , 2.0 , 6.0 , 4.0 , 8.0 ] ) ;
565
+
566
+ t . notEqual ( y , x , 'returns expected value' ) ;
567
+ t . notEqual ( getData ( y ) , xbuf , 'returns expected value' ) ;
568
+ t . strictEqual ( isSameFloat64Array ( getData ( y ) , expected ) , true , 'returns expected value' ) ;
569
+ t . deepEqual ( getShape ( y ) , [ 8 ] , 'returns expected value' ) ;
570
+ t . strictEqual ( getDType ( y ) , dt , 'returns expected value' ) ;
571
+ t . strictEqual ( getOrder ( y ) , 'row-major' , 'returns expected value' ) ;
572
+ t . strictEqual ( ctx . count , 9 , 'returns expected value' ) ;
573
+
574
+ t . end ( ) ;
575
+
576
+ function clbk ( v ) {
577
+ this . count += 1 ; // eslint-disable-line no-invalid-this
578
+ return v ;
579
+ }
580
+ } ) ;
581
+
439
582
tape ( 'by default, the function flattens all dimensions of a provided input ndarray in lexicographic order (column-major, non-contiguous)' , function test ( t ) {
440
583
var expected ;
441
584
var xbuf ;
0 commit comments