|
24 | 24 |
|
25 | 25 | var tape = require( 'tape' ); |
26 | 26 | var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); |
| 27 | +var isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' ); |
27 | 28 | var MultiSlice = require( '@stdlib/slice/multi' ); |
28 | 29 | var Slice = require( '@stdlib/slice/ctor' ); |
| 30 | +var Complex64 = require( '@stdlib/complex/float32' ); |
| 31 | +var Complex128 = require( '@stdlib/complex/float64' ); |
29 | 32 | var real = require( '@stdlib/complex/real' ); |
30 | 33 | var imag = require( '@stdlib/complex/imag' ); |
31 | 34 | var zeroTo = require( '@stdlib/array/base/zero-to' ); |
@@ -464,7 +467,7 @@ tape( 'if all output array dimensions are reduced, the function supports assigni |
464 | 467 | t.end(); |
465 | 468 | }); |
466 | 469 |
|
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 ) { |
468 | 471 | var expected; |
469 | 472 | var actual; |
470 | 473 | var xbuf; |
@@ -558,7 +561,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen |
558 | 561 | t.end(); |
559 | 562 | }); |
560 | 563 |
|
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 ) { |
562 | 565 | var expected; |
563 | 566 | var actual; |
564 | 567 | var xbuf; |
@@ -663,7 +666,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen |
663 | 666 | t.end(); |
664 | 667 | }); |
665 | 668 |
|
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 ) { |
667 | 670 | var expected; |
668 | 671 | var actual; |
669 | 672 | var xbuf; |
@@ -768,7 +771,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen |
768 | 771 | t.end(); |
769 | 772 | }); |
770 | 773 |
|
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 ) { |
772 | 775 | var expected; |
773 | 776 | var actual; |
774 | 777 | var xbuf; |
@@ -909,7 +912,7 @@ tape( 'the function assigns input ndarray element values to corresponding elemen |
909 | 912 | t.end(); |
910 | 913 | }); |
911 | 914 |
|
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 ) { |
913 | 916 | var expected; |
914 | 917 | var actual; |
915 | 918 | var xbuf; |
@@ -1363,4 +1366,105 @@ tape( 'the function supports broadcasting (ndims=3)', function test( t ) { |
1363 | 1366 | t.end(); |
1364 | 1367 | }); |
1365 | 1368 |
|
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