forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.Test.cs
More file actions
219 lines (168 loc) · 8.25 KB
/
Storage.Test.cs
File metadata and controls
219 lines (168 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using NumSharp.Core;
using System;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace NumSharp.UnitTest
{
[TestClass]
public class StorageTester
{
public NDStorage strg1D;
public NDStorage strg2D;
public NDStorage strg2DNonFull;
public NDStorage strg3D;
public NDStorage strg3DNonFull;
public StorageTester()
{
strg1D = new NDStorage(np.float64);
strg1D.Allocate(np.float64,new Shape(10),1);
strg1D.SetData(new double[]{0,1,2,3,4,5,6,7,8,9});
strg2D = new NDStorage(np.int64);
strg2D.Allocate(np.int64,new Shape(3,3),1);
strg2D.SetData(new Int64[]{0,1,2,3,4,5,6,7,8});
strg2DNonFull = new NDStorage(np.float32);
strg2DNonFull.Allocate(np.float32,new Shape(5,2),1);
strg2DNonFull.SetData(new float[]{0,1,2,3,4,5,6,7,8,9});
strg3D = new NDStorage(typeof(Complex));
strg3D.Allocate(typeof(Complex),new Shape(2,2,2));
strg3D.SetData(new Complex[]{1,2,3,4,5,6,7,8});
strg3DNonFull = new NDStorage(typeof(Complex));
strg3DNonFull.Allocate(typeof(Complex),new Shape(2,3,4));
var puffer = new Complex[24];
for(int idx = 1;idx < 25;idx++)
puffer[idx-1] = new Complex(idx,0);
strg3DNonFull.SetData(puffer);
}
[TestMethod]
public void Creation()
{
Assert.IsNotNull(strg1D);
Assert.IsNotNull(strg2D);
Assert.IsNotNull(strg2DNonFull);
Assert.IsNotNull(strg3D);
}
[TestMethod]
public void InternalArrayCheck()
{
Assert.IsTrue(strg1D.GetData().Length == 10);
Assert.IsTrue(strg2D.GetData().Length == 9);
Assert.IsTrue(strg2DNonFull.GetData().Length == 10);
}
//[TestMethod]
public void IndexingCheck()
{
var element1D = strg1D.GetData<double>(0);
Assert.IsTrue(element1D == 0);
for (int idx = 1; idx < 10;idx++)
{
element1D = strg1D.GetData<double>(idx);
Assert.IsTrue(element1D == idx);
}
var element2D = strg2D.GetData<Int64>(0,0);
Assert.IsTrue(element2D == 0);
element2D = strg2D.GetData<Int64>(1,0);
Assert.IsTrue(element2D == 1);
element2D = strg2D.GetData<Int64>(2,0);
Assert.IsTrue(element2D == 2);
element2D = strg2D.GetData<Int64>(0,1);
Assert.IsTrue(element2D == 3);
element2D = strg2D.GetData<Int64>(1,1);
Assert.IsTrue(element2D == 4);
element2D = strg2D.GetData<Int64>(2,1);
Assert.IsTrue(element2D == 5);
element2D = strg2D.GetData<Int64>(0,2);
Assert.IsTrue(element2D == 6);
element2D = strg2D.GetData<Int64>(1,2);
Assert.IsTrue(element2D == 7);
element2D = strg2D.GetData<Int64>(2,2);
Assert.IsTrue(element2D == 8);
var element3d = strg3D.GetData<Complex>(0,0,0);
element3d = strg3D.GetData<Complex>(1,0,0);
element3d = strg3D.GetData<Complex>(0,1,0);
element3d = strg3D.GetData<Complex>(1,1,0);
element3d = strg3D.GetData<Complex>(0,0,1);
element3d = strg3D.GetData<Complex>(1,0,1);
element3d = strg3D.GetData<Complex>(0,1,1);
element3d = strg3D.GetData<Complex>(1,1,1);
}
[TestMethod]
public void CloneCheck()
{
var strg1DCpy = (NDStorage) strg1D.Clone();
Assert.IsTrue(strg1DCpy.DType == strg1DCpy.GetData().GetType().GetElementType());
Assert.IsFalse(strg1D.GetData() == strg1DCpy.GetData());
Assert.IsTrue(strg1D.GetData().Length == strg1DCpy.GetData().Length);
Assert.IsTrue(Enumerable.SequenceEqual(strg1DCpy.GetData<double>(),strg1D.GetData<double>()));
}
[TestMethod]
public void CastingViaGet()
{
double[] arr1 = strg1D.GetData<double>();
}
//[TestMethod]
public void CheckChangeTensorLayout2D()
{
var strg2DCpy = (NDStorage) strg2D.Clone();
Assert.IsTrue(strg2DCpy.TensorLayout == 1);
strg2DCpy.ChangeTensorLayout(2);
Assert.IsTrue(strg2DCpy.TensorLayout == 2);
Assert.IsTrue(strg2DCpy.Shape.TensorLayout == 2);
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions,new int[]{3,3}));
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData<Int64>(), new Int64[]{0,3,6,1,4,7,2,5,8} ));
strg2DCpy.ChangeTensorLayout(1);
Assert.IsTrue(strg2DCpy.TensorLayout == 1);
Assert.IsTrue(strg2DCpy.Shape.TensorLayout == 1);
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions,new int[]{3,3}));
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData<Int64>(), strg2D.GetData<Int64>() ));
strg2DCpy = (NDStorage) strg2DNonFull.Clone();
Assert.IsTrue(strg2DCpy.TensorLayout == 1);
strg2DCpy.ChangeTensorLayout(2);
Assert.IsTrue(strg2DCpy.TensorLayout == 2);
Assert.IsTrue(strg2DCpy.Shape.TensorLayout == 2);
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions,new int[]{5,2}));
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData<Int64>(), new Int64[]{0,5,1,6,2,7,3,8,4,9} ));
strg2DCpy.ChangeTensorLayout(1);
Assert.IsTrue(strg2DCpy.TensorLayout == 1);
Assert.IsTrue(strg2DCpy.Shape.TensorLayout == 1);
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.Shape.Dimensions,new int[]{5,2}));
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData<Int64>(), strg2DNonFull.GetData<Int64>() ));
strg2DCpy = new NDStorage();
strg2DCpy.Allocate(typeof(Int64),new Shape(5,2),2);
strg2DCpy.SetData(strg2DNonFull.GetData());
strg2DCpy.ChangeTensorLayout(1);
Assert.IsTrue(Enumerable.SequenceEqual(strg2DCpy.GetData<Int64>(),new Int64[]{0,2,4,6,8,1,3,5,7,9}));
}
//[TestMethod]
public void CheckChangeTensorLayout3D()
{
var strg3DCpy = (NDStorage) strg3D.Clone();
Assert.IsTrue(strg3DCpy.TensorLayout == 1);
strg3DCpy.ChangeTensorLayout(2);
Assert.IsTrue(strg3DCpy.TensorLayout == 2);
Assert.IsTrue(strg3DCpy.Shape.TensorLayout == 2);
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.Shape.Dimensions,new int[]{2,2,2}));
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.GetData<Complex>(), new Complex[]{1,5,3,7,2,6,4,8} ));
strg3DCpy.ChangeTensorLayout(1);
Assert.IsTrue(strg3DCpy.TensorLayout == 1);
Assert.IsTrue(strg3DCpy.Shape.TensorLayout == 1);
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.Shape.Dimensions,new int[]{2,2,2}));
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.GetData<Complex>(), strg3D.GetData<Complex>() ));
strg3DCpy = (NDStorage) strg3DNonFull.Clone();
Assert.IsTrue(strg3DCpy.TensorLayout == 1);
strg3DCpy.ChangeTensorLayout(2);
Assert.IsTrue(strg3DCpy.TensorLayout == 2);
Assert.IsTrue(strg3DCpy.Shape.TensorLayout == 2);
var expectedValues = new Complex[]{1,7,13,19, 3,9,15,21, 5,11,17,23, 2,8,14,20, 4,10,16,22, 6,12,18,24 };
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.Shape.Dimensions,new int[]{2,3,4}));
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.GetData<Complex>(), expectedValues ));
strg3DCpy.ChangeTensorLayout(1);
Assert.IsTrue(strg3DCpy.TensorLayout == 1);
Assert.IsTrue(strg3DCpy.Shape.TensorLayout == 1);
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.Shape.Dimensions,new int[]{2,3,4}));
Assert.IsTrue(Enumerable.SequenceEqual(strg3DCpy.GetData<Complex>(), strg3DNonFull.GetData<Complex>() ));
}
}
}