Skip to content

Commit e348e20

Browse files
committed
test: add casting tests
1 parent 152ee45 commit e348e20

1 file changed

Lines changed: 110 additions & 6 deletions

File tree

  • lib/node_modules/@stdlib/ndarray/base/slice-assign/test

lib/node_modules/@stdlib/ndarray/base/slice-assign/test/test.js

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424

2525
var tape = require( 'tape' );
2626
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
27+
var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
2728
var MultiSlice = require( '@stdlib/slice/multi' );
2829
var Slice = require( '@stdlib/slice/ctor' );
30+
var Complex64 = require( '@stdlib/complex/float32' );
31+
var Complex128 = require( '@stdlib/complex/float64' );
2932
var real = require( '@stdlib/complex/real' );
3033
var imag = require( '@stdlib/complex/imag' );
3134
var zeroTo = require( '@stdlib/array/base/zero-to' );
@@ -464,7 +467,7 @@ tape( 'if all output array dimensions are reduced, the function supports assigni
464467
t.end();
465468
});
466469

467-
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=1)', function test( t ) {
470+
tape( 'the function assigns input array element values to corresponding elements in an output array view (ndims=1)', function test( t ) {
468471
var expected;
469472
var actual;
470473
var xbuf;
@@ -558,7 +561,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen
558561
t.end();
559562
});
560563

561-
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=2)', function test( t ) {
564+
tape( 'the function assigns input array element values to corresponding elements in an output array view (ndims=2)', function test( t ) {
562565
var expected;
563566
var actual;
564567
var xbuf;
@@ -663,7 +666,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen
663666
t.end();
664667
});
665668

666-
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=2, partial reduction)', function test( t ) {
669+
tape( 'the function assigns input array element values to corresponding elements in an output array view (ndims=2, partial reduction)', function test( t ) {
667670
var expected;
668671
var actual;
669672
var xbuf;
@@ -768,7 +771,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen
768771
t.end();
769772
});
770773

771-
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=3)', function test( t ) {
774+
tape( 'the function assigns input array element values to corresponding elements in an output array view (ndims=3)', function test( t ) {
772775
var expected;
773776
var actual;
774777
var xbuf;
@@ -909,7 +912,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen
909912
t.end();
910913
});
911914

912-
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=3, partial reduction)', function test( t ) {
915+
tape( 'the function assigns input array element values to corresponding elements in an output array view (ndims=3, partial reduction)', function test( t ) {
913916
var expected;
914917
var actual;
915918
var xbuf;
@@ -1363,4 +1366,105 @@ tape( 'the function supports broadcasting (ndims=3)', function test( t ) {
13631366
t.end();
13641367
});
13651368

1366-
// TODO: add dtype casting tests (including complex numbers)
1369+
tape( 'the function supports safely casting input array elements to the data type of the output array', function test( t ) {
1370+
var expected;
1371+
var values;
1372+
var actual;
1373+
var x;
1374+
var s;
1375+
var v;
1376+
var e;
1377+
var i;
1378+
var j;
1379+
1380+
s = new MultiSlice( null );
1381+
1382+
x = [
1383+
scalar2ndarray( 10, 'float32', 'row-major' ),
1384+
scalar2ndarray( 10, 'int8', 'row-major' ),
1385+
scalar2ndarray( 10, 'uint16', 'row-major' ),
1386+
scalar2ndarray( 10, 'float64', 'row-major' ),
1387+
scalar2ndarray( new Complex64( 3.0, 5.0 ), 'complex64', 'row-major' )
1388+
];
1389+
values = [
1390+
zeros( [ 2 ], { 'dtype': 'float64' } ),
1391+
zeros( [ 2 ], { 'dtype': 'int16' } ),
1392+
zeros( [ 2 ], { 'dtype': 'uint32' } ),
1393+
zeros( [ 2 ], { 'dtype': 'complex128' } ),
1394+
zeros( [ 2 ], { 'dtype': 'complex128' } )
1395+
];
1396+
expected = [
1397+
[ 10, 10 ],
1398+
[ 10, 10 ],
1399+
[ 10, 10 ],
1400+
[ 10, 10, 10, 10 ],
1401+
[ 3, 5, 3, 5 ]
1402+
];
1403+
for ( i = 0; i < expected.length; i++ ) {
1404+
actual = sliceAssign( x[ i ], values[ i ], s, true );
1405+
1406+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1407+
t.strictEqual( actual, values[ i ], 'returns expected value' );
1408+
1409+
v = actual.data;
1410+
e = expected[ i ];
1411+
if ( isComplexDataType( actual.dtype ) ) {
1412+
for ( j = 0; j < v.legnth; j++ ) {
1413+
t.strictEqual( real( v[ j ] ), e[ j*2 ], 'returns expected value' );
1414+
t.strictEqual( imag( v[ j ] ), e[ (j*2)+1 ], 'returns expected value' );
1415+
}
1416+
} else {
1417+
for ( j = 0; j < v.length; j++ ) {
1418+
t.strictEqual( v[ j ], e[ j ], 'returns expected value' );
1419+
}
1420+
}
1421+
}
1422+
t.end();
1423+
});
1424+
1425+
tape( 'the function supports downcasting floating-point input array elements to an output array data type of the same kind', function test( t ) {
1426+
var expected;
1427+
var values;
1428+
var actual;
1429+
var x;
1430+
var s;
1431+
var v;
1432+
var e;
1433+
var i;
1434+
var j;
1435+
1436+
s = new MultiSlice( null );
1437+
1438+
x = [
1439+
scalar2ndarray( 10, 'float64', 'row-major' ),
1440+
scalar2ndarray( new Complex128( 3.0, 5.0 ), 'complex128', 'row-major' )
1441+
];
1442+
values = [
1443+
zeros( [ 2 ], { 'dtype': 'float32' } ),
1444+
zeros( [ 2 ], { 'dtype': 'complex64' } )
1445+
];
1446+
expected = [
1447+
[ 10, 10 ],
1448+
[ 3, 5, 3, 5 ]
1449+
];
1450+
for ( i = 0; i < expected.length; i++ ) {
1451+
actual = sliceAssign( x[ i ], values[ i ], s, true );
1452+
1453+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1454+
t.strictEqual( actual, values[ i ], 'returns expected value' );
1455+
1456+
v = actual.data;
1457+
e = expected[ i ];
1458+
if ( isComplexDataType( actual.dtype ) ) {
1459+
for ( j = 0; j < v.legnth; j++ ) {
1460+
t.strictEqual( real( v[ j ] ), e[ j*2 ], 'returns expected value' );
1461+
t.strictEqual( imag( v[ j ] ), e[ (j*2)+1 ], 'returns expected value' );
1462+
}
1463+
} else {
1464+
for ( j = 0; j < v.length; j++ ) {
1465+
t.strictEqual( v[ j ], e[ j ], 'returns expected value' );
1466+
}
1467+
}
1468+
}
1469+
t.end();
1470+
});

0 commit comments

Comments
 (0)