@@ -88,6 +88,11 @@ public void Test_ArrayExtensions_2D_AsSpan2DAndFillArrayMid()
88
88
{
89
89
bool [ , ] test = new bool [ 4 , 5 ] ;
90
90
91
+ // To fill an array we now go through the Span2D<T> type, which includes all
92
+ // the necessary logic to perform the operation. In these tests we just create
93
+ // one through the extension, slice it and then fill it. For instance in this
94
+ // one, we're creating a Span2D<bool> from coordinates (1, 1), with a height of
95
+ // 2 and a width of 2, and then filling it. Then we just compare the results.
91
96
test . AsSpan2D ( 1 , 1 , 2 , 3 ) . Fill ( true ) ;
92
97
93
98
var expected = new [ , ]
@@ -173,12 +178,16 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle()
173
178
{ 9 , 10 , 11 , 12 }
174
179
} ;
175
180
181
+ // Here we use the enumerator on the RefEnumerator<T> type to traverse items in a row
182
+ // by reference. For each one, we check that the reference does in fact point to the
183
+ // item we expect in the underlying array (in this case, items on row 1).
176
184
int j = 0 ;
177
185
foreach ( ref int value in array . GetRow ( 1 ) )
178
186
{
179
187
Assert . IsTrue ( Unsafe . AreSame ( ref value , ref array [ 1 , j ++ ] ) ) ;
180
188
}
181
189
190
+ // Check that RefEnumerable<T>.ToArray() works correctly
182
191
CollectionAssert . AreEqual ( array . GetRow ( 1 ) . ToArray ( ) , new [ ] { 5 , 6 , 7 , 8 } ) ;
183
192
184
193
// Test an empty array
@@ -189,12 +198,40 @@ public void Test_ArrayExtensions_2D_GetRow_Rectangle()
189
198
Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetRow ( 20 ) ) ;
190
199
}
191
200
201
+ [ TestCategory ( "ArrayExtensions" ) ]
202
+ [ TestMethod ]
203
+ [ SuppressMessage ( "StyleCop.CSharp.NamingRules" , "SA1312" , Justification = "Dummy loop variable" ) ]
204
+ [ SuppressMessage ( "StyleCop.CSharp.LayoutRules" , "SA1501" , Justification = "Empty test loop" ) ]
205
+ public void Test_ArrayExtensions_2D_GetColumn_Rectangle ( )
206
+ {
207
+ int [ , ] array =
208
+ {
209
+ { 1 , 2 , 3 , 4 } ,
210
+ { 5 , 6 , 7 , 8 } ,
211
+ { 9 , 10 , 11 , 12 }
212
+ } ;
213
+
214
+ // Same as above, but this time we iterate a column instead (so non contiguous items)
215
+ int i = 0 ;
216
+ foreach ( ref int value in array . GetColumn ( 1 ) )
217
+ {
218
+ Assert . IsTrue ( Unsafe . AreSame ( ref value , ref array [ i ++ , 1 ] ) ) ;
219
+ }
220
+
221
+ CollectionAssert . AreEqual ( array . GetColumn ( 1 ) . ToArray ( ) , new [ ] { 2 , 6 , 10 } ) ;
222
+
223
+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetColumn ( - 1 ) ) ;
224
+
225
+ Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetColumn ( 20 ) ) ;
226
+ }
227
+
192
228
[ TestCategory ( "ArrayExtensions" ) ]
193
229
[ TestMethod ]
194
230
public void Test_ArrayExtensions_2D_GetRow_Empty ( )
195
231
{
196
232
int [ , ] array = new int [ 0 , 0 ] ;
197
233
234
+ // Try to get a row from an empty array (the row index isn't in range)
198
235
Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetRow ( 0 ) . ToArray ( ) ) ;
199
236
}
200
237
@@ -212,6 +249,8 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
212
249
{ 13 , 14 , 15 , 16 }
213
250
} ;
214
251
252
+ // Get a row and test the Clear method. Note that the Span2D<T> here is sliced
253
+ // starting from the second column, so this method should clear the row from index 1.
215
254
array . AsSpan2D ( 1 , 1 , 3 , 3 ) . GetRow ( 0 ) . Clear ( ) ;
216
255
217
256
int [ , ] expected =
@@ -224,6 +263,7 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
224
263
225
264
CollectionAssert . AreEqual ( array , expected ) ;
226
265
266
+ // Same as before, but this time we fill a column with a value
227
267
array . GetColumn ( 2 ) . Fill ( 42 ) ;
228
268
229
269
expected = new [ , ]
@@ -238,31 +278,36 @@ public void Test_ArrayExtensions_2D_GetRowOrColumn_Helpers()
238
278
239
279
int [ ] copy = new int [ 4 ] ;
240
280
281
+ // Get a row and copy items to a target span (in this case, wrapping an array)
241
282
array . GetRow ( 2 ) . CopyTo ( copy ) ;
242
283
243
284
int [ ] result = { 9 , 10 , 42 , 12 } ;
244
285
245
286
CollectionAssert . AreEqual ( copy , result ) ;
246
287
288
+ // Same as above, but copying from a column (so we test non contiguous sequences too)
247
289
array . GetColumn ( 1 ) . CopyTo ( copy ) ;
248
290
249
291
result = new [ ] { 2 , 0 , 10 , 14 } ;
250
292
251
293
CollectionAssert . AreEqual ( copy , result ) ;
252
294
295
+ // Some invalid attempts to copy to an empty span or sequence
253
296
Assert . ThrowsException < ArgumentException > ( ( ) => array . GetRow ( 0 ) . CopyTo ( default ( RefEnumerable < int > ) ) ) ;
254
297
Assert . ThrowsException < ArgumentException > ( ( ) => array . GetRow ( 0 ) . CopyTo ( default ( Span < int > ) ) ) ;
255
298
256
299
Assert . ThrowsException < ArgumentException > ( ( ) => array . GetColumn ( 0 ) . CopyTo ( default ( RefEnumerable < int > ) ) ) ;
257
300
Assert . ThrowsException < ArgumentException > ( ( ) => array . GetColumn ( 0 ) . CopyTo ( default ( Span < int > ) ) ) ;
258
301
302
+ // Same as CopyTo, but this will fail gracefully with an invalid target
259
303
Assert . IsTrue ( array . GetRow ( 2 ) . TryCopyTo ( copy ) ) ;
260
304
Assert . IsFalse ( array . GetRow ( 0 ) . TryCopyTo ( default ( Span < int > ) ) ) ;
261
305
262
306
result = new [ ] { 9 , 10 , 42 , 12 } ;
263
307
264
308
CollectionAssert . AreEqual ( copy , result ) ;
265
309
310
+ // Also fill a row and then further down clear a column (trying out all possible combinations)
266
311
array . GetRow ( 2 ) . Fill ( 99 ) ;
267
312
268
313
expected = new [ , ]
@@ -302,6 +347,9 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers()
302
347
{ 13 , 14 , 15 , 16 }
303
348
} ;
304
349
350
+ // This test pretty much does the same things as the method above, but this time
351
+ // using a source ReadOnlySpan2D<T>, so that the sequence type being tested is
352
+ // ReadOnlyRefEnumerable<T> instead (which shares most features but is separate).
305
353
ReadOnlySpan2D < int > span2D = array ;
306
354
307
355
int [ ] copy = new int [ 4 ] ;
@@ -332,32 +380,6 @@ public void Test_ArrayExtensions_2D_ReadOnlyGetRowOrColumn_Helpers()
332
380
CollectionAssert . AreEqual ( copy , result ) ;
333
381
}
334
382
335
- [ TestCategory ( "ArrayExtensions" ) ]
336
- [ TestMethod ]
337
- [ SuppressMessage ( "StyleCop.CSharp.NamingRules" , "SA1312" , Justification = "Dummy loop variable" ) ]
338
- [ SuppressMessage ( "StyleCop.CSharp.LayoutRules" , "SA1501" , Justification = "Empty test loop" ) ]
339
- public void Test_ArrayExtensions_2D_GetColumn_Rectangle ( )
340
- {
341
- int [ , ] array =
342
- {
343
- { 1 , 2 , 3 , 4 } ,
344
- { 5 , 6 , 7 , 8 } ,
345
- { 9 , 10 , 11 , 12 }
346
- } ;
347
-
348
- int i = 0 ;
349
- foreach ( ref int value in array . GetColumn ( 1 ) )
350
- {
351
- Assert . IsTrue ( Unsafe . AreSame ( ref value , ref array [ i ++ , 1 ] ) ) ;
352
- }
353
-
354
- CollectionAssert . AreEqual ( array . GetColumn ( 1 ) . ToArray ( ) , new [ ] { 2 , 6 , 10 } ) ;
355
-
356
- Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetColumn ( - 1 ) ) ;
357
-
358
- Assert . ThrowsException < ArgumentOutOfRangeException > ( ( ) => array . GetColumn ( 20 ) ) ;
359
- }
360
-
361
383
[ TestCategory ( "ArrayExtensions" ) ]
362
384
[ TestMethod ]
363
385
[ SuppressMessage ( "StyleCop.CSharp.NamingRules" , "SA1312" , Justification = "Dummy loop variable" ) ]
@@ -432,6 +454,7 @@ public void Test_ArrayExtensions_2D_AsSpan_Empty()
432
454
433
455
Span < int > span = array . AsSpan ( ) ;
434
456
457
+ // Check that the empty array was loaded properly
435
458
Assert . AreEqual ( span . Length , array . Length ) ;
436
459
Assert . IsTrue ( span . IsEmpty ) ;
437
460
}
@@ -449,11 +472,14 @@ public void Test_ArrayExtensions_2D_AsSpan_Populated()
449
472
450
473
Span < int > span = array . AsSpan ( ) ;
451
474
475
+ // Test the total length of the span
452
476
Assert . AreEqual ( span . Length , array . Length ) ;
453
477
454
478
ref int r0 = ref array [ 0 , 0 ] ;
455
479
ref int r1 = ref span [ 0 ] ;
456
480
481
+ // Similarly to the top methods, here we compare a given reference to
482
+ // ensure they point to the right element back in the original array.
457
483
Assert . IsTrue ( Unsafe . AreSame ( ref r0 , ref r1 ) ) ;
458
484
}
459
485
#endif
0 commit comments