Skip to content

Commit 925cf50

Browse files
committed
Added additional unit-tests (failing)
1 parent fb93e7a commit 925cf50

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

src/NumSharp.Core/Selection/NDArray.Indexing.Masking.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public NDArray this[NDArray<bool> mask]
2121
get => retrieve_indices(this, np.nonzero(mask), null);
2222
set
2323
{
24-
throw new NotImplementedException("Setter is not implemnted yet");
24+
throw new NotImplementedException("Setter is not implemented yet");
2525
}
2626
}
2727
}

test/NumSharp.UnitTest/Selection/NDArray.Indexing.Test.cs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,5 +1323,159 @@ public void IndexNDArray_Ellipsis_Case3()
13231323
{
13241324
np.arange(8).reshape(2, 1, 2, 1, 2)[0, Slice.Ellipsis].Should().BeShaped(1, 2, 1, 2);
13251325
}
1326+
1327+
[TestMethod]
1328+
public void IndexNDArray_Set_Case1()
1329+
{
1330+
var a = np.arange(8).reshape(2,4);
1331+
a["1, :"] = np.arange(4);
1332+
a.Should().AllValuesBe(8).And.BeShaped(2,4).And.BeOfValues(0,1,2,3,0,1,2,3);
1333+
}
1334+
1335+
[TestMethod]
1336+
public void IndexNDArray_Set_Case2()
1337+
{
1338+
var a = np.arange(8).reshape(2, 4);
1339+
a[1, np.arange(4)] = np.arange(4);
1340+
a.Should().AllValuesBe(8).And.BeShaped(2, 4).And.BeOfValues(0, 1, 2, 3, 0, 1, 2, 3);
1341+
}
1342+
1343+
[TestMethod]
1344+
public void IndexNDArray_Set_Case3()
1345+
{
1346+
var a = np.arange(8).reshape(2, 4);
1347+
a[np.array(1), np.arange(4)] = np.arange(4);
1348+
a.Should().AllValuesBe(8).And.BeShaped(2, 4).And.BeOfValues(0, 1, 2, 3, 0, 1, 2, 3);
1349+
}
1350+
1351+
[TestMethod]
1352+
public void IndexNDArray_Set_Case4()
1353+
{
1354+
var a = np.arange(8).reshape(2, 4);
1355+
a["1:2", np.arange(4)] = np.arange(4);
1356+
a.Should().AllValuesBe(8).And.BeShaped(2, 4).And.BeOfValues(0, 1, 2, 3, 0, 1, 2, 3);
1357+
}
1358+
1359+
[TestMethod]
1360+
public void IndexNDArray_Set_Case5()
1361+
{
1362+
var a = np.arange(8).reshape(2, 4);
1363+
a["1:2", Slice.All] = np.arange(4);
1364+
a.Should().AllValuesBe(8).And.BeShaped(2, 4).And.BeOfValues(0, 1, 2, 3, 0, 1, 2, 3);
1365+
}
1366+
1367+
[TestMethod]
1368+
public void IndexNDArray_Set_Case6()
1369+
{
1370+
var a = np.arange(8).reshape(2, 4);
1371+
a[Slice.Index(1), Slice.All] = np.arange(4);
1372+
a.Should().AllValuesBe(8).And.BeShaped(2, 4).And.BeOfValues(0, 1, 2, 3, 0, 1, 2, 3);
1373+
}
1374+
1375+
[TestMethod]
1376+
public void IndexNDArray_Set_Case7_Boolean()
1377+
{
1378+
var a = np.arange(8);
1379+
a[true] = 1;
1380+
a.Should().AllValuesBe(8).And.BeShaped(8);
1381+
}
1382+
1383+
[TestMethod]
1384+
public void IndexNDArray_Set_Case8_Broadcasted()
1385+
{
1386+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1387+
1388+
new Action(() =>
1389+
{
1390+
a[Slice.Index(1), Slice.All] = np.arange(0, 4, -1);
1391+
//ValueError:
1392+
}).Should().Throw<NumSharpException>().WithMessage("assignment destination is read-only");
1393+
}
1394+
1395+
[TestMethod]
1396+
public void IndexNDArray_Get_Case1_Broadcasted()
1397+
{
1398+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1399+
a[0].Should().BeShaped(4).And.BeOfValues(0, 1, 2, 3);
1400+
a[1].Should().BeShaped(4).And.BeOfValues(0, 1, 2, 3);
1401+
}
1402+
1403+
[TestMethod]
1404+
public void IndexNDArray_Get_Case2_Broadcasted()
1405+
{
1406+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1407+
a["0:1"].Should().BeShaped(1, 4).And.BeOfValues(0, 1, 2, 3);
1408+
}
1409+
1410+
[TestMethod]
1411+
public void IndexNDArray_Get_Case3_Broadcasted()
1412+
{
1413+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1414+
a["0:1, :"].Should().BeShaped(1, 4).And.BeOfValues(0, 1, 2, 3);
1415+
}
1416+
1417+
[TestMethod]
1418+
public void IndexNDArray_Get_Case4_Broadcasted()
1419+
{
1420+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1421+
var g = a["0:1:-1, :"];
1422+
g.Should().BeShaped();
1423+
}
1424+
1425+
1426+
[TestMethod]
1427+
public void IndexNDArray_Get_Case5_Broadcasted()
1428+
{
1429+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1430+
a[Slice.Index(0)].Should().BeShaped(4).And.BeOfValues(0, 1, 2, 3);
1431+
}
1432+
1433+
[TestMethod]
1434+
public void IndexNDArray_Get_Case6_Broadcasted()
1435+
{
1436+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1437+
a[new Slice(0, 1), Slice.All].Should().BeShaped(1, 4).And.BeOfValues(0, 1, 2, 3);
1438+
}
1439+
1440+
[TestMethod]
1441+
public void IndexNDArray_Get_Case7_Broadcasted()
1442+
{
1443+
//TODO: this produces incorrect return shape
1444+
//a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4))
1445+
//a = a[[1], :]
1446+
//
1447+
//(1, 4)
1448+
//[[0 1 2 3]]
1449+
var a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4));
1450+
a[np.arange(1) + 1, Slice.All].Should().BeShaped(1, 4).And.BeOfValues(0, 1, 2, 3);
1451+
}
1452+
1453+
[TestMethod]
1454+
public void IndexNDArray_Get_Case7()
1455+
{
1456+
//TODO: this produces incorrect return shape
1457+
//a = np.broadcast_to(np.arange(8).reshape(1, 1, 8), (2, 1, 8)) # np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4))
1458+
//a = a[np.arange(1) + 1, :]
1459+
//
1460+
//(1, 1, 8)
1461+
//[[[0 1 2 3 4 5 6 7]]]
1462+
var a = np.broadcast_to(np.arange(8).reshape(1, 1, 8), (2, 1, 8));
1463+
print(a[np.arange(1) + 1, Slice.All]);
1464+
a[np.arange(1) + 1, Slice.All].Should().BeShaped(1, 1, 8).And.BeOfValues(0,1,2,3,4,5,6,7);
1465+
}
1466+
1467+
1468+
[TestMethod]
1469+
public void IndexNDArray_Get_Case8_Broadcasted()
1470+
{
1471+
//TODO: this produces incorrect return shape
1472+
//a = np.broadcast_to(np.arange(4).reshape(1, 4), (2, 4))
1473+
//a = a[[1], :]
1474+
//
1475+
//(1, 4)
1476+
//[[0 1 2 3]]
1477+
var a = np.broadcast_to(np.arange(8).reshape(1, 2, 4), (2, 2, 4));
1478+
a[np.arange(1) + 1, Slice.All].Should().BeShaped(1, 4).And.BeOfValues(0, 1, 2, 3);
1479+
}
13261480
}
13271481
}

0 commit comments

Comments
 (0)