21
21
// MODULES //
22
22
23
23
var tape = require ( 'tape' ) ;
24
+ var DataType = require ( '@stdlib/ndarray/dtype-ctor' ) ;
25
+ var structFactory = require ( '@stdlib/dstructs/struct' ) ;
24
26
var Complex128 = require ( '@stdlib/complex/float64/ctor' ) ;
25
27
var isScalarMostlySafeCompatible = require ( './../lib' ) ; // eslint-disable-line id-length
26
28
@@ -33,7 +35,7 @@ tape( 'main export is a function', function test( t ) {
33
35
t . end ( ) ;
34
36
} ) ;
35
37
36
- tape ( 'the function throws an error if provided a second argument which is not a supported data type' , function test ( t ) {
38
+ tape ( 'the function returns `false` if provided a second argument which is not a supported data type' , function test ( t ) {
37
39
var values ;
38
40
var i ;
39
41
@@ -50,15 +52,9 @@ tape( 'the function throws an error if provided a second argument which is not a
50
52
function noop ( ) { }
51
53
] ;
52
54
for ( i = 0 ; i < values . length ; i ++ ) {
53
- t . throws ( badValue ( values [ i ] ) , TypeError , 'throws an error when provided ' + values [ i ] ) ;
55
+ t . strictEqual ( isScalarMostlySafeCompatible ( 3.14 , values [ i ] ) , false , 'returns expected value for ' + values [ i ] ) ;
54
56
}
55
57
t . end ( ) ;
56
-
57
- function badValue ( value ) {
58
- return function badValue ( ) {
59
- isScalarMostlySafeCompatible ( 3.14 , value ) ;
60
- } ;
61
- }
62
58
} ) ;
63
59
64
60
tape ( 'the function returns a boolean indicating if a value can be cast to an ndarray data type (generic)' , function test ( t ) {
@@ -90,8 +86,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
90
86
for ( i = 0 ; i < values . length ; i ++ ) {
91
87
v = values [ i ] [ 0 ] ;
92
88
expected = values [ i ] [ 1 ] ;
89
+
93
90
actual = isScalarMostlySafeCompatible ( v , 'generic' ) ;
94
91
t . strictEqual ( actual , expected , 'returns expected value' ) ;
92
+
93
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'generic' ) ) ;
94
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
95
95
}
96
96
t . end ( ) ;
97
97
} ) ;
@@ -125,8 +125,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
125
125
for ( i = 0 ; i < values . length ; i ++ ) {
126
126
v = values [ i ] [ 0 ] ;
127
127
expected = values [ i ] [ 1 ] ;
128
+
128
129
actual = isScalarMostlySafeCompatible ( v , 'binary' ) ;
129
130
t . strictEqual ( actual , expected , 'returns expected value' ) ;
131
+
132
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'binary' ) ) ;
133
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
130
134
}
131
135
t . end ( ) ;
132
136
} ) ;
@@ -160,8 +164,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
160
164
for ( i = 0 ; i < values . length ; i ++ ) {
161
165
v = values [ i ] [ 0 ] ;
162
166
expected = values [ i ] [ 1 ] ;
167
+
163
168
actual = isScalarMostlySafeCompatible ( v , 'bool' ) ;
164
169
t . strictEqual ( actual , expected , 'returns expected value' ) ;
170
+
171
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'bool' ) ) ;
172
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
165
173
}
166
174
t . end ( ) ;
167
175
} ) ;
@@ -195,8 +203,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
195
203
for ( i = 0 ; i < values . length ; i ++ ) {
196
204
v = values [ i ] [ 0 ] ;
197
205
expected = values [ i ] [ 1 ] ;
206
+
198
207
actual = isScalarMostlySafeCompatible ( v , 'float64' ) ;
199
208
t . strictEqual ( actual , expected , 'returns expected value' ) ;
209
+
210
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'float64' ) ) ;
211
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
200
212
}
201
213
t . end ( ) ;
202
214
} ) ;
@@ -230,8 +242,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
230
242
for ( i = 0 ; i < values . length ; i ++ ) {
231
243
v = values [ i ] [ 0 ] ;
232
244
expected = values [ i ] [ 1 ] ;
245
+
233
246
actual = isScalarMostlySafeCompatible ( v , 'complex128' ) ;
234
247
t . strictEqual ( actual , expected , 'returns expected value' ) ;
248
+
249
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'complex128' ) ) ;
250
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
235
251
}
236
252
t . end ( ) ;
237
253
} ) ;
@@ -265,8 +281,12 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
265
281
for ( i = 0 ; i < values . length ; i ++ ) {
266
282
v = values [ i ] [ 0 ] ;
267
283
expected = values [ i ] [ 1 ] ;
284
+
268
285
actual = isScalarMostlySafeCompatible ( v , 'int32' ) ;
269
286
t . strictEqual ( actual , expected , 'returns expected value' ) ;
287
+
288
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'int32' ) ) ;
289
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
270
290
}
271
291
t . end ( ) ;
272
292
} ) ;
@@ -300,8 +320,76 @@ tape( 'the function returns a boolean indicating if a value can be cast to an nd
300
320
for ( i = 0 ; i < values . length ; i ++ ) {
301
321
v = values [ i ] [ 0 ] ;
302
322
expected = values [ i ] [ 1 ] ;
323
+
303
324
actual = isScalarMostlySafeCompatible ( v , 'uint32' ) ;
304
325
t . strictEqual ( actual , expected , 'returns expected value' ) ;
326
+
327
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( 'uint32' ) ) ;
328
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
329
+ }
330
+ t . end ( ) ;
331
+ } ) ;
332
+
333
+ tape ( 'the function returns a boolean indicating if a value can be cast to an ndarray data type (struct)' , function test ( t ) {
334
+ var expected ;
335
+ var Struct1 ;
336
+ var Struct2 ;
337
+ var values ;
338
+ var actual ;
339
+ var schema ;
340
+ var data ;
341
+ var v ;
342
+ var i ;
343
+
344
+ schema = [
345
+ {
346
+ 'name' : 'value' ,
347
+ 'type' : 'float64'
348
+ }
349
+ ] ;
350
+ Struct1 = structFactory ( schema ) ;
351
+
352
+ schema = [
353
+ {
354
+ 'name' : 'value' ,
355
+ 'type' : 'float32'
356
+ }
357
+ ] ;
358
+ Struct2 = structFactory ( schema ) ;
359
+
360
+ data = {
361
+ 'data' : 3.0
362
+ } ;
363
+ values = [
364
+ [ new Struct1 ( data ) , true ] ,
365
+ [ new Struct2 ( data ) , false ] ,
366
+ [ 5 , false ] ,
367
+ [ - 5 , false ] ,
368
+ [ 3.14 , false ] ,
369
+ [ - 3.14 , false ] ,
370
+ [ 127 , false ] , // int8
371
+ [ 255 , false ] , // uint8
372
+ [ 32767 , false ] , // int16
373
+ [ 65535 , false ] , // uint16
374
+ [ 2147483647 , false ] , // int32
375
+ [ 4294967295 , false ] , // uint32
376
+ [ '5' , false ] ,
377
+ [ true , false ] ,
378
+ [ false , false ] ,
379
+ [ null , false ] ,
380
+ [ { } , false ] ,
381
+ [ new Complex128 ( 3.0 , 5.0 ) , false ]
382
+ ] ;
383
+
384
+ for ( i = 0 ; i < values . length ; i ++ ) {
385
+ v = values [ i ] [ 0 ] ;
386
+ expected = values [ i ] [ 1 ] ;
387
+
388
+ actual = isScalarMostlySafeCompatible ( v , Struct1 ) ;
389
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
390
+
391
+ actual = isScalarMostlySafeCompatible ( v , new DataType ( Struct1 ) ) ;
392
+ t . strictEqual ( actual , expected , 'returns expected value' ) ;
305
393
}
306
394
t . end ( ) ;
307
395
} ) ;
0 commit comments