Repository navigation
Expand file tree
/
Copy pathShape.Order.Tests.cs
More file actions
295 lines (254 loc) · 10.4 KB
/
Copy pathShape.Order.Tests.cs
File metadata and controls
295 lines (254 loc) · 10.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
namespace NumSharp.Tests.View
{
/// <summary>
/// Tests for multi-order memory layout support: C/F physical, A/K logical.
///
/// PYTHON VERIFICATION (NumPy 2.4.2):
/// Behavior matches NumPy's flags['C_CONTIGUOUS'] / flags['F_CONTIGUOUS']
/// and order resolution semantics for np.empty / np.copy.
/// </summary>
[TestClass]
public class ShapeOrderTests
{
// ================================================================
// Detection: IsContiguous / IsFContiguous
// ================================================================
[TestMethod]
public void Scalar_IsBothCAndFContiguous()
{
// NumPy: np.array(42).flags -> C=True, F=True
var scalar = new Shape();
scalar.IsContiguous.Should().BeTrue("scalars are C-contig by definition");
scalar.IsFContiguous.Should().BeTrue("scalars are F-contig by definition");
}
[TestMethod]
public void OneDimensional_IsBothCAndFContiguous()
{
// NumPy: np.arange(5).flags -> C=True, F=True
var shape = new Shape(5);
shape.IsContiguous.Should().BeTrue();
shape.IsFContiguous.Should().BeTrue("1-D contiguous arrays are both C and F contig");
}
[TestMethod]
public void CContiguous2D_IsCOnly()
{
// NumPy: np.zeros((3,4)).flags -> C=True, F=False
var shape = new Shape(3L, 4L);
shape.IsContiguous.Should().BeTrue();
shape.IsFContiguous.Should().BeFalse("multi-dim C-contig is not F-contig");
}
[TestMethod]
public void TransposeOfCContig_IsFContiguous()
{
// NumPy: arr = np.arange(24).reshape(2,3,4); arr.T.flags -> C=False, F=True
var arr = np.arange(24).reshape(2, 3, 4);
var transposed = arr.T;
transposed.Shape.IsContiguous.Should().BeFalse();
transposed.Shape.IsFContiguous.Should().BeTrue(
"transpose of C-contig produces F-contig memory layout");
}
[TestMethod]
public void Shape_WithFOrder_ProducesFContigStrides()
{
// F-order strides for (3,4): strides[0]=1, strides[1]=3
var shape = new Shape(new long[] { 3, 4 }, 'F');
shape.IsFContiguous.Should().BeTrue();
shape.IsContiguous.Should().BeFalse();
shape.strides[0].Should().Be(1);
shape.strides[1].Should().Be(3);
}
[TestMethod]
public void Shape_WithCOrder_ProducesCContigStrides()
{
// C-order strides for (3,4): strides[0]=4, strides[1]=1
var shape = new Shape(new long[] { 3, 4 }, 'C');
shape.IsContiguous.Should().BeTrue();
shape.IsFContiguous.Should().BeFalse();
shape.strides[0].Should().Be(4);
shape.strides[1].Should().Be(1);
}
[TestMethod]
public void Shape_WithInvalidOrder_Throws()
{
// Direct Shape constructor only accepts physical orders (C/F); A/K must be resolved first.
Action act = () => new Shape(new long[] { 3, 4 }, 'A');
act.Should().Throw<ArgumentException>();
Action act2 = () => new Shape(new long[] { 3, 4 }, 'X');
act2.Should().Throw<ArgumentException>();
}
[TestMethod]
public void Shape_3D_FOrder_HasExpectedStrides()
{
// F-order (2,3,4): strides = (1, 2, 6)
var shape = new Shape(new long[] { 2, 3, 4 }, 'F');
shape.IsFContiguous.Should().BeTrue();
shape.strides.Should().Equal(new long[] { 1, 2, 6 });
}
// ================================================================
// OrderResolver: Logical -> Physical mapping
// ================================================================
[TestMethod]
public void OrderResolver_C_ReturnsC()
{
OrderResolver.Resolve('C').Should().Be('C');
OrderResolver.Resolve('c').Should().Be('C');
}
[TestMethod]
public void OrderResolver_F_ReturnsF()
{
OrderResolver.Resolve('F').Should().Be('F');
OrderResolver.Resolve('f').Should().Be('F');
}
[TestMethod]
public void OrderResolver_A_WithoutSource_Throws()
{
// NumPy: np.empty((3,4), order='A') -> "only 'C' or 'F' order is permitted"
Action act = () => OrderResolver.Resolve('A');
act.Should().Throw<ArgumentException>()
.WithMessage("*only 'C' or 'F'*");
}
[TestMethod]
public void OrderResolver_K_WithoutSource_Throws()
{
Action act = () => OrderResolver.Resolve('K');
act.Should().Throw<ArgumentException>()
.WithMessage("*only 'C' or 'F'*");
}
[TestMethod]
public void OrderResolver_A_WithCSource_ReturnsC()
{
var cSource = new Shape(new long[] { 3, 4 }, 'C');
OrderResolver.Resolve('A', cSource).Should().Be('C');
}
[TestMethod]
public void OrderResolver_A_WithFSource_ReturnsF()
{
// NumPy: np.copy(f_arr, order='A') with F-contig (not C) source -> F-contig output
var fSource = new Shape(new long[] { 3, 4 }, 'F');
OrderResolver.Resolve('A', fSource).Should().Be('F');
}
[TestMethod]
public void OrderResolver_K_WithCSource_ReturnsC()
{
var cSource = new Shape(new long[] { 3, 4 }, 'C');
OrderResolver.Resolve('K', cSource).Should().Be('C');
}
[TestMethod]
public void OrderResolver_K_WithFSource_ReturnsF()
{
var fSource = new Shape(new long[] { 3, 4 }, 'F');
OrderResolver.Resolve('K', fSource).Should().Be('F');
}
[TestMethod]
public void OrderResolver_InvalidChar_Throws()
{
Action act = () => OrderResolver.Resolve('X');
act.Should().Throw<ArgumentException>()
.WithMessage("*'C', 'F', 'A', 'K'*");
}
// ================================================================
// np.empty integration — all 4 orders
// ================================================================
[TestMethod]
public void NpEmpty_COrder_ProducesCContig()
{
var arr = np.empty(new Shape(3L, 4L), order: 'C');
arr.Shape.IsContiguous.Should().BeTrue();
arr.Shape.IsFContiguous.Should().BeFalse();
}
[TestMethod]
public void NpEmpty_FOrder_ProducesFContig()
{
var arr = np.empty(new Shape(3L, 4L), order: 'F');
arr.Shape.IsContiguous.Should().BeFalse();
arr.Shape.IsFContiguous.Should().BeTrue();
}
[TestMethod]
public void NpEmpty_AOrder_Throws()
{
// NumPy: np.empty(shape, order='A') -> ValueError
Action act = () => np.empty(new Shape(3L, 4L), order: 'A');
act.Should().Throw<ArgumentException>();
}
[TestMethod]
public void NpEmpty_KOrder_Throws()
{
// NumPy: np.empty(shape, order='K') -> ValueError
Action act = () => np.empty(new Shape(3L, 4L), order: 'K');
act.Should().Throw<ArgumentException>();
}
// ================================================================
// Flags integration
// ================================================================
[TestMethod]
public void Flags_FContig_ExposesFContiguousBit()
{
var fShape = new Shape(new long[] { 3, 4 }, 'F');
(fShape.Flags & ArrayFlags.F_CONTIGUOUS).Should().Be(ArrayFlags.F_CONTIGUOUS);
(fShape.Flags & ArrayFlags.C_CONTIGUOUS).Should().Be(ArrayFlags.None);
}
[TestMethod]
public void Flags_CContig_ExposesCContiguousBit()
{
var cShape = new Shape(new long[] { 3, 4 }, 'C');
(cShape.Flags & ArrayFlags.C_CONTIGUOUS).Should().Be(ArrayFlags.C_CONTIGUOUS);
(cShape.Flags & ArrayFlags.F_CONTIGUOUS).Should().Be(ArrayFlags.None);
}
[TestMethod]
public void Flags_1D_ExposesBothContiguousBits()
{
// 1-D arrays satisfy both C and F contiguity conditions
var shape = new Shape(5L);
(shape.Flags & ArrayFlags.C_CONTIGUOUS).Should().Be(ArrayFlags.C_CONTIGUOUS);
(shape.Flags & ArrayFlags.F_CONTIGUOUS).Should().Be(ArrayFlags.F_CONTIGUOUS);
}
// ================================================================
// Shape.Order property — derives from actual contiguity flags
// ================================================================
[TestMethod]
public void Order_CContig_ReportsC()
{
var shape = new Shape(new long[] { 3, 4 }, 'C');
shape.Order.Should().Be('C');
}
[TestMethod]
public void Order_FContig_ReportsF()
{
var shape = new Shape(new long[] { 3, 4 }, 'F');
shape.Order.Should().Be('F');
}
[TestMethod]
public void Order_Transpose_ReportsF()
{
// Transpose of C-contig produces F-contig memory; Order should reflect that
var arr = np.arange(24).reshape(2, 3, 4);
arr.Shape.Order.Should().Be('C');
arr.T.Shape.Order.Should().Be('F');
}
[TestMethod]
public void Order_1D_ReportsC()
{
// 1-D is both C and F contig; default to 'C'
var shape = new Shape(5L);
shape.Order.Should().Be('C');
}
[TestMethod]
public void Order_Scalar_ReportsC()
{
var scalar = new Shape();
scalar.Order.Should().Be('C');
}
// ================================================================
// Empty arrays (any dim == 0) are trivially both C and F contig
// ================================================================
[TestMethod]
public void EmptyArray_IsBothCAndFContiguous()
{
// NumPy: np.empty((2, 0, 3)).flags -> C=True, F=True (any dim=0)
var shape = new Shape(2L, 0L, 3L);
shape.IsContiguous.Should().BeTrue();
shape.IsFContiguous.Should().BeTrue();
}
}
}