Skip to content

Commit b34bfec

Browse files
committed
NDArray<T>: added a constructor overload and test cases
1 parent 61dc35a commit b34bfec

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/NumSharp.Core/Generics/NDArrayGeneric.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public NDArray(Shape shape) : this()
3939
Storage = new NDStorage(typeof(T));
4040
Storage.Allocate(this.dtype, shape);
4141
}
42+
43+
public NDArray(Array array, Shape shape) : this(shape)
44+
{
45+
Storage.SetData(array);
46+
}
4247
/// <summary>
4348
/// indexing of generic - overridden on purpose
4449
/// </summary>
@@ -57,4 +62,3 @@ public NDArray(Shape shape) : this()
5762
}
5863
}
5964
}
60-
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Numerics;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Linq;
7+
using NumSharp;
8+
using NumSharp.Generic;
9+
10+
namespace NumSharp.UnitTest.Generic
11+
{
12+
[TestClass]
13+
public class NDArrayGenericTest
14+
{
15+
16+
[TestMethod]
17+
public void Generic1DBool_NDArray()
18+
{
19+
var np1 = new NDArray<bool>(new [] { true, true, false, false }, new Shape(4));
20+
var np2 = new NDArray<bool>(new Shape(2));
21+
var np3 = new NDArray<bool>();
22+
Assert.IsTrue(Enumerable.SequenceEqual(new [] { true, true, false, false }, np1.Storage.GetData<bool>()));
23+
Assert.AreEqual(4, np1.size);
24+
Assert.AreEqual(1, np1.ndim);
25+
Assert.IsTrue(Enumerable.SequenceEqual(new[] { false, false }, np2.Storage.GetData<bool>()));
26+
Assert.AreEqual(2, np2.size);
27+
Assert.AreEqual(1, np2.ndim);
28+
Assert.IsTrue(Enumerable.SequenceEqual(new[] { false }, np3.Storage.GetData<bool>()));
29+
Assert.AreEqual(1, np3.size);
30+
Assert.AreEqual(1, np3.ndim);
31+
}
32+
33+
[TestMethod]
34+
public void Generic2DBool_NDArrayOR()
35+
{
36+
var np1 = new NDArray<bool>(new Shape(2, 3));
37+
np1.Storage.SetData(new bool[] { true, true, false, false, true, false });
38+
Assert.IsTrue(Enumerable.SequenceEqual(new[] { true, true, false, false, true, false }, np1.Storage.GetData<bool>()));
39+
Assert.AreEqual(6, np1.size);
40+
Assert.AreEqual(2, np1.ndim);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)