forked from CommunityToolkit/dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Span2D{T}.cs
More file actions
1140 lines (890 loc) · 33.3 KB
/
Copy pathTest_Span2D{T}.cs
File metadata and controls
1140 lines (890 loc) · 33.3 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Enumerables;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CommunityToolkit.HighPerformance.UnitTests;
[TestClass]
public class Test_Span2DT
{
[TestMethod]
public void Test_Span2DT_Empty()
{
// Like in the tests for Memory2D<T>, here we validate a number of empty spans
Span2D<int> empty1 = default;
Assert.IsTrue(empty1.IsEmpty);
Assert.AreEqual(empty1.Length, 0);
Assert.AreEqual(empty1.Width, 0);
Assert.AreEqual(empty1.Height, 0);
Span2D<string> empty2 = Span2D<string>.Empty;
Assert.IsTrue(empty2.IsEmpty);
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 0);
Span2D<int> empty3 = new int[4, 0];
Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 0);
Assert.AreEqual(empty3.Height, 4);
Span2D<int> empty4 = new int[0, 7];
Assert.IsTrue(empty4.IsEmpty);
Assert.AreEqual(empty4.Length, 0);
Assert.AreEqual(empty4.Width, 7);
Assert.AreEqual(empty4.Height, 0);
}
#if NETCOREAPP
[TestMethod]
public unsafe void Test_Span2DT_RefConstructor()
{
Span<int> span = stackalloc[]
{
1,
2,
3,
4,
5,
6
};
// Test for a Span2D<T> instance created from a target reference. This is only supported
// on runtimes with fast Span<T> support (as we need the API to power this with just a ref).
Span2D<int> span2d = Span2D<int>.DangerousCreate(ref span[0], 2, 3, 0);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 6);
Assert.AreEqual(span2d.Width, 3);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 0] = 99;
span2d[1, 2] = 101;
// Validate that those values were mapped to the right spot in the target span
Assert.AreEqual(span[0], 99);
Assert.AreEqual(span[5], 101);
// A few cases with invalid indices
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => Span2D<int>.DangerousCreate(ref Unsafe.AsRef<int>(null), -1, 0, 0));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => Span2D<int>.DangerousCreate(ref Unsafe.AsRef<int>(null), 1, -2, 0));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => Span2D<int>.DangerousCreate(ref Unsafe.AsRef<int>(null), 1, 0, -5));
}
#endif
[TestMethod]
public unsafe void Test_Span2DT_PtrConstructor()
{
int* ptr = stackalloc[]
{
1,
2,
3,
4,
5,
6
};
// Same as above, but creating a Span2D<T> from a raw pointer
Span2D<int> span2d = new(ptr, 2, 3, 0);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 6);
Assert.AreEqual(span2d.Width, 3);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 0] = 99;
span2d[1, 2] = 101;
Assert.AreEqual(ptr[0], 99);
Assert.AreEqual(ptr[5], 101);
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>((void*)0, -1, 0, 0));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>((void*)0, 1, -2, 0));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>((void*)0, 1, 0, -5));
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<string>((void*)0, 2, 2, 0));
}
[TestMethod]
public void Test_Span2DT_Array1DConstructor()
{
int[] array =
{
1, 2, 3, 4, 5, 6
};
// Same as above, but wrapping a 1D array with data in row-major order
Span2D<int> span2d = new(array, 1, 2, 2, 1);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 4);
Assert.AreEqual(span2d.Width, 2);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 0] = 99;
span2d[1, 1] = 101;
Assert.AreEqual(array[1], 99);
Assert.AreEqual(array[5], 101);
// The first check fails due to the array covariance test mentioned in the Memory2D<T> tests.
// The others just validate a number of cases with invalid arguments (eg. out of range).
_ = Assert.ThrowsException<ArrayTypeMismatchException>(() => new Span2D<object>(new string[1], 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, -99, 1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 0, -10, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 0, 1, 1, -1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 0, 1, -100, 1));
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array, 0, 10, 1, 120));
}
[TestMethod]
public void Test_Span2DT_Array2DConstructor_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but directly wrapping a 2D array
Span2D<int> span2d = new(array);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 6);
Assert.AreEqual(span2d.Width, 3);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 1] = 99;
span2d[1, 2] = 101;
Assert.AreEqual(array[0, 1], 99);
Assert.AreEqual(array[1, 2], 101);
_ = Assert.ThrowsException<ArrayTypeMismatchException>(() => new Span2D<object>(new string[1, 2]));
}
[TestMethod]
public void Test_Span2DT_Array2DConstructor_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but with a custom slicing over the target 2D array
Span2D<int> span2d = new(array, 0, 1, 2, 2);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 4);
Assert.AreEqual(span2d.Width, 2);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 0] = 99;
span2d[1, 1] = 101;
Assert.AreEqual(array[0, 1], 99);
Assert.AreEqual(array[1, 2], 101);
_ = Assert.ThrowsException<ArrayTypeMismatchException>(() => new Span2D<object>(new string[1, 2], 0, 0, 2, 2));
}
[TestMethod]
public void Test_Span2DT_Array3DConstructor_1()
{
int[,,] array =
{
{
{ 1, 2, 3 },
{ 4, 5, 6 }
},
{
{ 10, 20, 30 },
{ 40, 50, 60 }
}
};
// Here we wrap a layer in a 3D array instead, the rest is the same
Span2D<int> span2d = new(array, 1);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 6);
Assert.AreEqual(span2d.Width, 3);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 1] = 99;
span2d[1, 2] = 101;
Assert.AreEqual(span2d[0, 0], 10);
Assert.AreEqual(array[1, 0, 1], 99);
Assert.AreEqual(array[1, 1, 2], 101);
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, -1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 20));
}
[TestMethod]
public void Test_Span2DT_Array3DConstructor_2()
{
int[,,] array =
{
{
{ 1, 2, 3 },
{ 4, 5, 6 }
},
{
{ 10, 20, 30 },
{ 40, 50, 60 }
}
};
// Same as above, but also slicing a target 2D area in the 3D array layer
Span2D<int> span2d = new(array, 1, 0, 1, 2, 2);
Assert.IsFalse(span2d.IsEmpty);
Assert.AreEqual(span2d.Length, 4);
Assert.AreEqual(span2d.Width, 2);
Assert.AreEqual(span2d.Height, 2);
span2d[0, 1] = 99;
span2d[1, 1] = 101;
Assert.AreEqual(span2d[0, 0], 20);
Assert.AreEqual(array[1, 0, 2], 99);
Assert.AreEqual(array[1, 1, 2], 101);
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, -1, 1, 1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 1, -1, 1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 1, 1, -1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 1, 1, 1, -1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array, 1, 1, 1, 1, -1));
}
[TestMethod]
public void Test_Span2DT_FillAndClear_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Tests for the Fill and Clear APIs for Span2D<T>. These should fill
// or clear the entire wrapped 2D array (just like eg. Span<T>.Fill).
Span2D<int> span2d = new(array);
span2d.Fill(42);
Assert.IsTrue(array.Cast<int>().All(n => n == 42));
span2d.Clear();
Assert.IsTrue(array.Cast<int>().All(n => n == 0));
}
[TestMethod]
public void Test_Span2DT_Fill_Empty()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but with an initial slicing as well to ensure
// these method work correctly with different internal offsets
Span2D<int> span2d = new(array, 0, 0, 0, 0);
span2d.Fill(42);
CollectionAssert.AreEqual(array, array);
span2d.Clear();
CollectionAssert.AreEqual(array, array);
}
[TestMethod]
public void Test_Span2DT_FillAndClear_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, just with different slicing to a target smaller 2D area
Span2D<int> span2d = new(array, 0, 1, 2, 2);
span2d.Fill(42);
int[,] filled =
{
{ 1, 42, 42 },
{ 4, 42, 42 }
};
CollectionAssert.AreEqual(array, filled);
span2d.Clear();
int[,] cleared =
{
{ 1, 0, 0 },
{ 4, 0, 0 }
};
CollectionAssert.AreEqual(array, cleared);
}
[TestMethod]
public void Test_Span2DT_CopyTo_Empty()
{
Span2D<int> span2d = Span2D<int>.Empty;
int[] target = new int[0];
// Copying an empty Span2D<T> to an empty array is just a no-op
span2d.CopyTo(target);
}
[TestMethod]
public void Test_Span2DT_CopyTo_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
int[] target = new int[array.Length];
// Here we copy a Span2D<T> to a target Span<T> mapping an array.
// This is valid, and the data will just be copied in row-major order.
span2d.CopyTo(target);
CollectionAssert.AreEqual(array, target);
// Exception due to the target span being too small for the source Span2D<T> instance
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array).CopyTo(Span<int>.Empty));
}
[TestMethod]
public void Test_Span2DT_CopyTo_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but with different initial slicing
Span2D<int> span2d = new(array, 0, 1, 2, 2);
int[] target = new int[4];
span2d.CopyTo(target);
int[] expected = { 2, 3, 5, 6 };
CollectionAssert.AreEqual(target, expected);
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array).CopyTo(Span<int>.Empty));
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array, 0, 1, 2, 2).CopyTo(Span<int>.Empty));
}
[TestMethod]
public void Test_Span2DT_CopyTo2D_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
int[,] target = new int[2, 3];
// Same as above, but copying to a target Span2D<T> instead. Note
// that this method uses the implicit T[,] to Span2D<T> conversion.
span2d.CopyTo(target);
CollectionAssert.AreEqual(array, target);
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array).CopyTo(Span2D<int>.Empty));
}
[TestMethod]
public void Test_Span2DT_CopyTo2D_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but with extra initial slicing
Span2D<int> span2d = new(array, 0, 1, 2, 2);
int[,] target = new int[2, 2];
span2d.CopyTo(target);
int[,] expected =
{
{ 2, 3 },
{ 5, 6 }
};
CollectionAssert.AreEqual(target, expected);
_ = Assert.ThrowsException<ArgumentException>(() => new Span2D<int>(array).CopyTo(new Span2D<int>(target)));
}
[TestMethod]
public void Test_Span2DT_TryCopyTo()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
int[] target = new int[array.Length];
// Here we test the safe TryCopyTo method, which will fail gracefully
Assert.IsTrue(span2d.TryCopyTo(target));
Assert.IsFalse(span2d.TryCopyTo(Span<int>.Empty));
int[] expected = { 1, 2, 3, 4, 5, 6 };
CollectionAssert.AreEqual(target, expected);
}
[TestMethod]
public void Test_Span2DT_TryCopyTo2D()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but copying to a 2D array with the safe TryCopyTo method
Span2D<int> span2d = new(array);
int[,] target = new int[2, 3];
Assert.IsTrue(span2d.TryCopyTo(target));
Assert.IsFalse(span2d.TryCopyTo(Span2D<int>.Empty));
int[,] expected =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
CollectionAssert.AreEqual(target, expected);
}
[TestMethod]
public unsafe void Test_Span2DT_GetPinnableReference()
{
// Here we test that a ref from an empty Span2D<T> returns a null ref
Assert.IsTrue(Unsafe.AreSame(
ref Unsafe.AsRef<int>(null),
ref Span2D<int>.Empty.GetPinnableReference()));
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
ref int r0 = ref span2d.GetPinnableReference();
// Here we test that GetPinnableReference returns a ref to the first array element
Assert.IsTrue(Unsafe.AreSame(ref r0, ref array[0, 0]));
}
[TestMethod]
public unsafe void Test_Span2DT_DangerousGetReference()
{
// Same as above, but using DangerousGetReference instead (faster, no conditional check)
Assert.IsTrue(Unsafe.AreSame(
ref Unsafe.AsRef<int>(null),
ref Span2D<int>.Empty.DangerousGetReference()));
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
ref int r0 = ref span2d.DangerousGetReference();
Assert.IsTrue(Unsafe.AreSame(ref r0, ref array[0, 0]));
}
#if NETCOREAPP3_1_OR_GREATER
[TestMethod]
public unsafe void Test_Span2DT_Index_Indexer_1()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
ref int arrayRef = ref array[1, 3];
ref int span2dRef = ref span2d[1, ^1];
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref span2dRef));
}
[TestMethod]
public unsafe void Test_Span2DT_Index_Indexer_2()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
ref int arrayRef = ref array[2, 1];
ref int span2dRef = ref span2d[^2, ^3];
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref span2dRef));
}
[TestMethod]
[ExpectedException(typeof(IndexOutOfRangeException))]
public unsafe void Test_Span2DT_Index_Indexer_Fail()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
ref int span2dRef = ref span2d[^6, 2];
}
[TestMethod]
public unsafe void Test_Span2DT_Range_Indexer_1()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
Span2D<int> slice = span2d[1.., 1..];
Assert.AreEqual(slice.Length, 9);
Assert.IsTrue(Unsafe.AreSame(ref array[1, 1], ref slice[0, 0]));
Assert.IsTrue(Unsafe.AreSame(ref array[3, 3], ref slice[2, 2]));
}
[TestMethod]
public unsafe void Test_Span2DT_Range_Indexer_2()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
Span2D<int> slice = span2d[0..^2, 1..^1];
Assert.AreEqual(slice.Length, 4);
Assert.IsTrue(Unsafe.AreSame(ref array[0, 1], ref slice[0, 0]));
Assert.IsTrue(Unsafe.AreSame(ref array[1, 2], ref slice[1, 1]));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public unsafe void Test_Span2DT_Range_Indexer_Fail()
{
int[,] array = new int[4, 4];
Span2D<int> span2d = new(array);
_ = span2d[0..6, 2..^1];
Assert.Fail();
}
#endif
[TestMethod]
public void Test_Span2DT_Slice_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Here we have a number of tests that just take an initial 2D array, create a Span2D<T>,
// perform a number of slicing operations and then validate the parameters for the resulting
// instances, and that the indexer works correctly and maps to the right original elements.
Span2D<int> span2d = new(array);
Span2D<int> slice1 = span2d.Slice(1, 1, 1, 2);
Assert.AreEqual(slice1.Length, 2);
Assert.AreEqual(slice1.Height, 1);
Assert.AreEqual(slice1.Width, 2);
Assert.AreEqual(slice1[0, 0], 5);
Assert.AreEqual(slice1[0, 1], 6);
Span2D<int> slice2 = span2d.Slice(0, 1, 2, 2);
Assert.AreEqual(slice2.Length, 4);
Assert.AreEqual(slice2.Height, 2);
Assert.AreEqual(slice2.Width, 2);
Assert.AreEqual(slice2[0, 0], 2);
Assert.AreEqual(slice2[1, 0], 5);
Assert.AreEqual(slice2[1, 1], 6);
// Some checks for invalid arguments
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(-1, 1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(1, -1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(1, 1, 1, -1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(1, 1, -1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(10, 1, 1, 1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(1, 12, 1, 12));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).Slice(1, 1, 55, 1));
}
[TestMethod]
public void Test_Span2DT_Slice_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// Same as above, but with some different slicing
Span2D<int> slice1 = span2d.Slice(0, 0, 2, 2);
Assert.AreEqual(slice1.Length, 4);
Assert.AreEqual(slice1.Height, 2);
Assert.AreEqual(slice1.Width, 2);
Assert.AreEqual(slice1[0, 0], 1);
Assert.AreEqual(slice1[1, 1], 5);
Span2D<int> slice2 = slice1.Slice(1, 0, 1, 2);
Assert.AreEqual(slice2.Length, 2);
Assert.AreEqual(slice2.Height, 1);
Assert.AreEqual(slice2.Width, 2);
Assert.AreEqual(slice2[0, 0], 4);
Assert.AreEqual(slice2[0, 1], 5);
Span2D<int> slice3 = slice2.Slice(0, 1, 1, 1);
Assert.AreEqual(slice3.Length, 1);
Assert.AreEqual(slice3.Height, 1);
Assert.AreEqual(slice3.Width, 1);
Assert.AreEqual(slice3[0, 0], 5);
}
#if NETCOREAPP
[TestMethod]
public void Test_Span2DT_GetRowSpan()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// Here we create a Span2D<T> from a 2D array and want to get a Span<T> from
// a specific row. This is only supported on runtimes with fast Span<T> support
// for the same reason mentioned in the Memory2D<T> tests (we need the Span<T>
// constructor that only takes a target ref). Then we just get some references
// to items in this span and compare them against references into the original
// 2D array to ensure they match and point to the correct elements from there.
Span<int> span = span2d.GetRowSpan(1);
Assert.IsTrue(Unsafe.AreSame(
ref span[0],
ref array[1, 0]));
Assert.IsTrue(Unsafe.AreSame(
ref span[2],
ref array[1, 2]));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).GetRowSpan(-1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).GetRowSpan(5));
}
#endif
[TestMethod]
public void Test_Span2DT_TryGetSpan_From1DArray_1()
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Span2D<int> span2d = new(array, 3, 3);
bool success = span2d.TryGetSpan(out Span<int> span);
Assert.IsTrue(success);
Assert.AreEqual(span.Length, span2d.Length);
Assert.IsTrue(Unsafe.AreSame(ref array[0], ref span[0]));
}
[TestMethod]
public void Test_Span2DT_TryGetSpan_From1DArray_2()
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Span2D<int> span2d = new Span2D<int>(array, 3, 3).Slice(1, 0, 2, 3);
bool success = span2d.TryGetSpan(out Span<int> span);
Assert.IsTrue(success);
Assert.AreEqual(span.Length, span2d.Length);
Assert.IsTrue(Unsafe.AreSame(ref array[3], ref span[0]));
}
[TestMethod]
public void Test_Span2DT_TryGetSpan_From1DArray_3()
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Span2D<int> span2d = new Span2D<int>(array, 3, 3).Slice(0, 1, 3, 2);
bool success = span2d.TryGetSpan(out Span<int> span);
Assert.IsFalse(success);
Assert.AreEqual(span.Length, 0);
}
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3947
[TestMethod]
public void Test_Span2DT_TryGetSpan_From1DArray_4()
{
int[] array = new int[128];
Span2D<int> span2d = new(array, 8, 16);
bool success = span2d.TryGetSpan(out Span<int> span);
Assert.IsTrue(success);
Assert.AreEqual(span.Length, span2d.Length);
Assert.IsTrue(Unsafe.AreSame(ref array[0], ref span[0]));
}
[TestMethod]
public void Test_Span2DT_TryGetSpan_From2DArray_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// This API tries to get a Span<T> for the entire contents of Span2D<T>.
// This only works on runtimes if the underlying data is contiguous
// and of a size that can fit into a single Span<T>. In this specific test,
// this is not expected to work on .NET Standard 2.0 because it can't create a
// Span<T> from a 2D array (reasons explained in the comments for the test above).
bool success = span2d.TryGetSpan(out Span<int> span);
#if NETFRAMEWORK
// Can't get a Span<T> over a T[,] array on .NET Standard 2.0
Assert.IsFalse(success);
Assert.AreEqual(span.Length, 0);
#else
Assert.IsTrue(success);
Assert.AreEqual(span.Length, span2d.Length);
#endif
}
[TestMethod]
public void Test_Span2DT_TryGetSpan_From2DArray_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but this will always fail because we're creating
// a Span2D<T> wrapping non contiguous data (the pitch is not 0).
Span2D<int> span2d = new(array, 0, 0, 2, 2);
bool success = span2d.TryGetSpan(out Span<int> span);
Assert.IsFalse(success);
Assert.IsTrue(span.IsEmpty);
}
[TestMethod]
public void Test_Span2DT_ToArray_1()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Here we create a Span2D<T> and verify that ToArray() produces
// a 2D array that is identical to the original one being wrapped.
Span2D<int> span2d = new(array);
int[,] copy = span2d.ToArray();
Assert.AreEqual(copy.GetLength(0), array.GetLength(0));
Assert.AreEqual(copy.GetLength(1), array.GetLength(1));
CollectionAssert.AreEqual(array, copy);
}
[TestMethod]
public void Test_Span2DT_ToArray_2()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Same as above, but with extra initial slicing
Span2D<int> span2d = new(array, 0, 0, 2, 2);
int[,] copy = span2d.ToArray();
Assert.AreEqual(copy.GetLength(0), 2);
Assert.AreEqual(copy.GetLength(1), 2);
int[,] expected =
{
{ 1, 2 },
{ 4, 5 }
};
CollectionAssert.AreEqual(expected, copy);
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Test_Span2DT_Equals()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// Span2D<T>.Equals always throw (this mirrors the behavior of Span<T>.Equals)
_ = span2d.Equals(null);
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Test_Span2DT_GetHashCode()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// Same as above, this always throws
_ = span2d.GetHashCode();
}
[TestMethod]
public void Test_Span2DT_ToString()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
Span2D<int> span2d = new(array);
// Verify that we get the nicely formatted string
string text = span2d.ToString();
const string expected = "CommunityToolkit.HighPerformance.Span2D<System.Int32>[2, 3]";
Assert.AreEqual(text, expected);
}
[TestMethod]
public void Test_Span2DT_opEquals()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Create two Span2D<T> instances wrapping the same array with the same
// parameters, and verify that the equality operators work correctly.
Span2D<int> span2d_1 = new(array);
Span2D<int> span2d_2 = new(array);
Assert.IsTrue(span2d_1 == span2d_2);
Assert.IsFalse(span2d_1 == Span2D<int>.Empty);
Assert.IsTrue(Span2D<int>.Empty == Span2D<int>.Empty);
// Same as above, but verify that a sliced span is not reported as equal
Span2D<int> span2d_3 = new(array, 0, 0, 2, 2);
Assert.IsFalse(span2d_1 == span2d_3);
Assert.IsFalse(span2d_3 == Span2D<int>.Empty);
}
[TestMethod]
public void Test_Span2DT_ImplicitCast()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Verify that an explicit constructor and the implicit conversion
// operator generate an identical Span2D<T> instance from the array.
Span2D<int> span2d_1 = array;
Span2D<int> span2d_2 = new(array);
Assert.IsTrue(span2d_1 == span2d_2);
}
[TestMethod]
public void Test_Span2DT_GetRow()
{
int[,] array =
{
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Get a target row and verify the contents match with our data
RefEnumerable<int> enumerable = new Span2D<int>(array).GetRow(1);
int[] expected = { 4, 5, 6 };
CollectionAssert.AreEqual(enumerable.ToArray(), expected);
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).GetRow(-1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).GetRow(2));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Span2D<int>(array).GetRow(1000));
}
[TestMethod]
public unsafe void Test_Span2DT_Pointer_GetRow()
{