Skip to content

Commit 152ee45

Browse files
committed
test: add broadcasting tests
1 parent c90e739 commit 152ee45

1 file changed

Lines changed: 317 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: 317 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ tape( 'if all output array dimensions are reduced, the function supports assigni
464464
t.end();
465465
});
466466

467-
tape( 'the function returns a view of a provided input array (ndims=1)', function test( t ) {
467+
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=1)', function test( t ) {
468468
var expected;
469469
var actual;
470470
var xbuf;
@@ -558,7 +558,7 @@ tape( 'the function returns a view of a provided input array (ndims=1)', functio
558558
t.end();
559559
});
560560

561-
tape( 'the function returns a view of a provided input array (ndims=2)', function test( t ) {
561+
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=2)', function test( t ) {
562562
var expected;
563563
var actual;
564564
var xbuf;
@@ -663,7 +663,7 @@ tape( 'the function returns a view of a provided input array (ndims=2)', functio
663663
t.end();
664664
});
665665

666-
tape( 'the function returns a view of a provided input array (ndims=2, partial reduction)', function test( t ) {
666+
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=2, partial reduction)', function test( t ) {
667667
var expected;
668668
var actual;
669669
var xbuf;
@@ -768,7 +768,7 @@ tape( 'the function returns a view of a provided input array (ndims=2, partial r
768768
t.end();
769769
});
770770

771-
tape( 'the function returns a view of a provided input array (ndims=3)', function test( t ) {
771+
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=3)', function test( t ) {
772772
var expected;
773773
var actual;
774774
var xbuf;
@@ -909,7 +909,7 @@ tape( 'the function returns a view of a provided input array (ndims=3)', functio
909909
t.end();
910910
});
911911

912-
tape( 'the function returns a view of a provided input array (ndims=3, partial reduction)', function test( t ) {
912+
tape( 'the function assigns input ndarray element values to corresponding elements in an output ndarray view (ndims=3, partial reduction)', function test( t ) {
913913
var expected;
914914
var actual;
915915
var xbuf;
@@ -1050,6 +1050,317 @@ tape( 'the function returns a view of a provided input array (ndims=3, partial r
10501050
t.end();
10511051
});
10521052

1053-
// TODO: add broadcasting tests
1053+
tape( 'the function supports broadcasting (ndims=1)', function test( t ) {
1054+
var expected;
1055+
var actual;
1056+
var ybuf;
1057+
var x;
1058+
var y;
1059+
var s;
1060+
1061+
x = scalar2ndarray( 10.0, 'float64', 'row-major' );
1062+
1063+
// Full slice:
1064+
ybuf = azeros( 30, x.dtype );
1065+
y = new ctor( x.dtype, ybuf, [ 6 ], [ 2 ], 4, 'row-major' );
1066+
1067+
s = new MultiSlice( null );
1068+
actual = sliceAssign( x, y, s, true );
1069+
1070+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1071+
t.strictEqual( actual, y, 'returns expected value' );
1072+
1073+
expected = [ 10, 10, 10, 10, 10, 10 ];
1074+
actual = ndarray2array( actual );
1075+
t.deepEqual( actual, expected, 'returns expected value' );
1076+
1077+
// Reverse order and skip every other element:
1078+
ybuf = azeros( 30, x.dtype );
1079+
y = new ctor( x.dtype, ybuf, [ 6 ], [ 2 ], 4, 'row-major' );
1080+
1081+
s = new MultiSlice( new Slice( null, null, -2 ) );
1082+
actual = sliceAssign( x, y, s, true );
1083+
1084+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1085+
t.strictEqual( actual, y, 'returns expected value' );
1086+
1087+
expected = [ 0, 10, 0, 10, 0, 10 ];
1088+
actual = ndarray2array( actual );
1089+
t.deepEqual( actual, expected, 'returns expected value' );
1090+
1091+
// Reverse order and skip every other element, starting from second-to-last element:
1092+
ybuf = azeros( 30, x.dtype );
1093+
y = new ctor( x.dtype, ybuf, [ 6 ], [ 2 ], 4, 'row-major' );
1094+
1095+
s = new MultiSlice( new Slice( 4, null, -2 ) );
1096+
actual = sliceAssign( x, y, s, true );
1097+
1098+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1099+
t.strictEqual( actual, y, 'returns expected value' );
1100+
1101+
expected = [ 10, 0, 10, 0, 10, 0 ];
1102+
actual = ndarray2array( actual );
1103+
t.deepEqual( actual, expected, 'returns expected value' );
1104+
1105+
// Skip every three elements, starting from second element:
1106+
ybuf = azeros( 30, x.dtype );
1107+
y = new ctor( x.dtype, ybuf, [ 6 ], [ 2 ], 4, 'row-major' );
1108+
1109+
s = new MultiSlice( new Slice( 1, null, 3 ) );
1110+
actual = sliceAssign( x, y, s, true );
1111+
1112+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1113+
t.strictEqual( actual, y, 'returns expected value' );
1114+
1115+
expected = [ 0, 10, 0, 0, 10, 0 ];
1116+
actual = ndarray2array( actual );
1117+
t.deepEqual( actual, expected, 'returns expected value' );
1118+
1119+
// Set a sub-array:
1120+
ybuf = azeros( 30, x.dtype );
1121+
y = new ctor( x.dtype, ybuf, [ 6 ], [ 2 ], 4, 'row-major' );
1122+
1123+
s = new MultiSlice( new Slice( 4, 1, -1 ) );
1124+
actual = sliceAssign( x, y, s, true );
1125+
1126+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1127+
t.strictEqual( actual, y, 'returns expected value' );
1128+
1129+
expected = [ 0, 0, 10, 10, 10, 0 ];
1130+
actual = ndarray2array( actual );
1131+
t.deepEqual( actual, expected, 'returns expected value' );
1132+
1133+
t.end();
1134+
});
1135+
1136+
tape( 'the function supports broadcasting (ndims=2)', function test( t ) {
1137+
var expected;
1138+
var actual;
1139+
var ybuf;
1140+
var s0;
1141+
var s1;
1142+
var x;
1143+
var y;
1144+
var s;
1145+
1146+
x = scalar2ndarray( 10.0, 'float64', 'row-major' );
1147+
1148+
// Full slice:
1149+
ybuf = azeros( 30, x.dtype );
1150+
y = new ctor( x.dtype, ybuf, [ 4, 3 ], [ 6, 2 ], 4, 'row-major' );
1151+
1152+
s = new MultiSlice( null, null );
1153+
actual = sliceAssign( x, y, s, true );
1154+
1155+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1156+
t.strictEqual( actual, y, 'returns expected value' );
1157+
1158+
expected = [
1159+
[ 10, 10, 10 ],
1160+
[ 10, 10, 10 ],
1161+
[ 10, 10, 10 ],
1162+
[ 10, 10, 10 ]
1163+
];
1164+
actual = ndarray2array( actual );
1165+
t.deepEqual( actual, expected, 'returns expected value' );
1166+
1167+
// Reverse order and skip every other element:
1168+
ybuf = azeros( 30, x.dtype );
1169+
y = new ctor( x.dtype, ybuf, [ 4, 3 ], [ 6, 2 ], 4, 'row-major' );
1170+
1171+
s0 = new Slice( null, null, -2 );
1172+
s1 = new Slice( null, null, -2 );
1173+
s = new MultiSlice( s0, s1 );
1174+
actual = sliceAssign( x, y, s, true );
1175+
1176+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1177+
t.strictEqual( actual, y, 'returns expected value' );
1178+
1179+
expected = [
1180+
[ 0, 0, 0 ],
1181+
[ 10, 0, 10 ],
1182+
[ 0, 0, 0 ],
1183+
[ 10, 0, 10 ]
1184+
];
1185+
actual = ndarray2array( actual );
1186+
t.deepEqual( actual, expected, 'returns expected value' );
1187+
1188+
// Reverse order and skip every other row, starting from second-to-last row:
1189+
ybuf = azeros( 30, x.dtype );
1190+
y = new ctor( x.dtype, ybuf, [ 4, 3 ], [ 6, 2 ], 4, 'row-major' );
1191+
1192+
s0 = new Slice( 2, null, -2 );
1193+
s1 = new Slice( 1, null, -1 );
1194+
s = new MultiSlice( s0, s1 );
1195+
actual = sliceAssign( x, y, s, true );
1196+
1197+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1198+
t.strictEqual( actual, y, 'returns expected value' );
1199+
1200+
expected = [
1201+
[ 10, 10, 0 ],
1202+
[ 0, 0, 0 ],
1203+
[ 10, 10, 0 ],
1204+
[ 0, 0, 0 ]
1205+
];
1206+
actual = ndarray2array( actual );
1207+
t.deepEqual( actual, expected, 'returns expected value' );
1208+
1209+
// Set a sub-array:
1210+
ybuf = azeros( 30, x.dtype );
1211+
y = new ctor( x.dtype, ybuf, [ 4, 3 ], [ 6, 2 ], 4, 'row-major' );
1212+
1213+
s0 = new Slice( 2, 0, -1 );
1214+
s1 = new Slice( 0, 2, 1 );
1215+
s = new MultiSlice( s0, s1 );
1216+
actual = sliceAssign( x, y, s, true );
1217+
1218+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1219+
t.strictEqual( actual, y, 'returns expected value' );
1220+
1221+
expected = [
1222+
[ 0, 0, 0 ],
1223+
[ 10, 10, 0 ],
1224+
[ 10, 10, 0 ],
1225+
[ 0, 0, 0 ]
1226+
];
1227+
actual = ndarray2array( actual );
1228+
t.deepEqual( actual, expected, 'returns expected value' );
1229+
t.end();
1230+
});
1231+
1232+
tape( 'the function supports broadcasting (ndims=3)', function test( t ) {
1233+
var expected;
1234+
var actual;
1235+
var xbuf;
1236+
var ybuf;
1237+
var s0;
1238+
var s1;
1239+
var s2;
1240+
var x;
1241+
var y;
1242+
var s;
1243+
1244+
xbuf = typedarray( [ 10.0 ], 'float64' );
1245+
x = new ctor( 'float64', xbuf, [ 1, 1 ], [ 1, 1 ], 0, 'row-major' );
1246+
1247+
// Full slice:
1248+
ybuf = azeros( 100, x.dtype );
1249+
y = new ctor( x.dtype, ybuf, [ 2, 4, 3 ], [ 24, 6, 2 ], 4, 'row-major' );
1250+
1251+
s = new MultiSlice( null, null, null );
1252+
actual = sliceAssign( x, y, s, true );
1253+
1254+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1255+
t.strictEqual( actual, y, 'returns expected value' );
1256+
1257+
expected = [
1258+
[
1259+
[ 10, 10, 10 ],
1260+
[ 10, 10, 10 ],
1261+
[ 10, 10, 10 ],
1262+
[ 10, 10, 10 ]
1263+
],
1264+
[
1265+
[ 10, 10, 10 ],
1266+
[ 10, 10, 10 ],
1267+
[ 10, 10, 10 ],
1268+
[ 10, 10, 10 ]
1269+
]
1270+
];
1271+
actual = ndarray2array( actual );
1272+
t.deepEqual( actual, expected, 'returns expected value' );
1273+
1274+
// Reverse order and skip every other element:
1275+
ybuf = azeros( 100, x.dtype );
1276+
y = new ctor( x.dtype, ybuf, [ 2, 4, 3 ], [ 24, 6, 2 ], 4, 'row-major' );
1277+
1278+
s0 = new Slice( null, null, -1 );
1279+
s1 = new Slice( null, null, -2 );
1280+
s2 = new Slice( null, null, -2 );
1281+
s = new MultiSlice( s0, s1, s2 );
1282+
actual = sliceAssign( x, y, s, true );
1283+
1284+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1285+
t.strictEqual( actual, y, 'returns expected value' );
1286+
1287+
expected = [
1288+
[
1289+
[ 0, 0, 0 ],
1290+
[ 10, 0, 10 ],
1291+
[ 0, 0, 0 ],
1292+
[ 10, 0, 10 ]
1293+
],
1294+
[
1295+
[ 0, 0, 0 ],
1296+
[ 10, 0, 10 ],
1297+
[ 0, 0, 0 ],
1298+
[ 10, 0, 10 ]
1299+
]
1300+
];
1301+
actual = ndarray2array( actual );
1302+
t.deepEqual( actual, expected, 'returns expected value' );
1303+
1304+
// Reverse order and skip elements, starting from specified elements:
1305+
ybuf = azeros( 100, x.dtype );
1306+
y = new ctor( x.dtype, ybuf, [ 2, 4, 3 ], [ 24, 6, 2 ], 4, 'row-major' );
1307+
1308+
s0 = new Slice( null, null, 1 );
1309+
s1 = new Slice( 2, null, -2 );
1310+
s2 = new Slice( 1, null, -1 );
1311+
s = new MultiSlice( s0, s1, s2 );
1312+
actual = sliceAssign( x, y, s, true );
1313+
1314+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1315+
t.strictEqual( actual, y, 'returns expected value' );
1316+
1317+
expected = [
1318+
[
1319+
[ 10, 10, 0 ],
1320+
[ 0, 0, 0 ],
1321+
[ 10, 10, 0 ],
1322+
[ 0, 0, 0 ]
1323+
],
1324+
[
1325+
[ 10, 10, 0 ],
1326+
[ 0, 0, 0 ],
1327+
[ 10, 10, 0 ],
1328+
[ 0, 0, 0 ]
1329+
]
1330+
];
1331+
actual = ndarray2array( actual );
1332+
t.deepEqual( actual, expected, 'returns expected value' );
1333+
1334+
// Set a sub-array:
1335+
ybuf = azeros( 100, x.dtype );
1336+
y = new ctor( x.dtype, ybuf, [ 2, 4, 3 ], [ 24, 6, 2 ], 4, 'row-major' );
1337+
1338+
s0 = new Slice( 0, 1, 1 );
1339+
s1 = new Slice( 2, 0, -1 );
1340+
s2 = new Slice( 0, 2, 1 );
1341+
s = new MultiSlice( s0, s1, s2 );
1342+
actual = sliceAssign( x, y, s, true );
1343+
1344+
t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' );
1345+
t.strictEqual( actual, y, 'returns expected value' );
1346+
1347+
expected = [
1348+
[
1349+
[ 0, 0, 0 ],
1350+
[ 10, 10, 0 ],
1351+
[ 10, 10, 0 ],
1352+
[ 0, 0, 0 ]
1353+
],
1354+
[
1355+
[ 0, 0, 0 ],
1356+
[ 0, 0, 0 ],
1357+
[ 0, 0, 0 ],
1358+
[ 0, 0, 0 ]
1359+
]
1360+
];
1361+
actual = ndarray2array( actual );
1362+
t.deepEqual( actual, expected, 'returns expected value' );
1363+
t.end();
1364+
});
10541365

10551366
// TODO: add dtype casting tests (including complex numbers)

0 commit comments

Comments
 (0)