@@ -19,10 +19,19 @@ public class ImmutableArrayTest : SimpleElementImmutablesTestBase
19
19
private static readonly ImmutableArray < GenericParameterHelper > s_oneElementRefType = ImmutableArray . Create ( new GenericParameterHelper ( 1 ) ) ;
20
20
private static readonly ImmutableArray < string > s_twoElementRefTypeWithNull = ImmutableArray . Create ( "1" , null ) ;
21
21
22
+ [ Fact ]
23
+ public void Clear ( )
24
+ {
25
+ Assert . Equal ( ImmutableArray < int > . Empty , ImmutableArray . Create < int > ( ) . Clear ( ) ) ;
26
+ Assert . Equal ( ImmutableArray < int > . Empty , ImmutableArray . Create < int > ( 1 ) . Clear ( ) ) ;
27
+ Assert . Equal ( ImmutableArray < int > . Empty , ImmutableArray . Create < int > ( 1 , 2 , 3 ) . Clear ( ) ) ;
28
+ }
29
+
22
30
[ Fact ]
23
31
public void CreateEmpty ( )
24
32
{
25
- Assert . Equal ( ImmutableArray . Create < int > ( ) , ImmutableArray < int > . Empty ) ;
33
+ Assert . Equal ( ImmutableArray < int > . Empty , ImmutableArray . Create < int > ( ) ) ;
34
+ Assert . Equal ( ImmutableArray < int > . Empty , ImmutableArray . Create < int > ( new int [ 0 ] ) ) ;
26
35
}
27
36
28
37
[ Fact ]
@@ -534,12 +543,17 @@ public void ObjectEnumerator()
534
543
enumerator = ( ( IEnumerable < int > ) s_manyElements ) . GetEnumerator ( ) ;
535
544
Assert . Throws < InvalidOperationException > ( ( ) => enumerator . Current ) ;
536
545
537
- Assert . True ( enumerator . MoveNext ( ) ) ;
538
- Assert . Equal ( 1 , enumerator . Current ) ;
539
- Assert . True ( enumerator . MoveNext ( ) ) ;
540
- Assert . Equal ( 2 , enumerator . Current ) ;
541
- Assert . True ( enumerator . MoveNext ( ) ) ;
542
- Assert . Equal ( 3 , enumerator . Current ) ;
546
+ for ( int i = 0 ; i < 2 ; i ++ )
547
+ {
548
+ Assert . True ( enumerator . MoveNext ( ) ) ;
549
+ Assert . Equal ( 1 , enumerator . Current ) ;
550
+ Assert . True ( enumerator . MoveNext ( ) ) ;
551
+ Assert . Equal ( 2 , enumerator . Current ) ;
552
+ Assert . True ( enumerator . MoveNext ( ) ) ;
553
+ Assert . Equal ( 3 , enumerator . Current ) ;
554
+ if ( i == 0 )
555
+ enumerator . Reset ( ) ;
556
+ }
543
557
544
558
Assert . False ( enumerator . MoveNext ( ) ) ;
545
559
Assert . Throws < InvalidOperationException > ( ( ) => enumerator . Current ) ;
@@ -641,6 +655,9 @@ public void AddRange()
641
655
642
656
Assert . Equal ( new [ ] { 1 , 2 , 3 , 4 } , s_manyElements . AddRange ( new [ ] { 4 } ) ) ;
643
657
Assert . Equal ( new [ ] { 1 , 2 , 3 , 4 , 5 } , s_manyElements . AddRange ( new [ ] { 4 , 5 } ) ) ;
658
+
659
+ Assert . Equal ( new [ ] { 1 , 2 , 3 , 4 } , s_manyElements . AddRange ( ImmutableArray . Create ( 4 ) ) ) ;
660
+ Assert . Equal ( new [ ] { 1 , 2 , 3 , 4 , 5 } , s_manyElements . AddRange ( ImmutableArray . Create ( 4 , 5 ) ) ) ;
644
661
}
645
662
646
663
[ Fact ]
@@ -727,6 +744,8 @@ public void InsertRangeEmpty()
727
744
Assert . Equal ( s_manyElements , s_manyElements . InsertRange ( 0 , Enumerable . Empty < int > ( ) ) ) ;
728
745
Assert . Throws < ArgumentOutOfRangeException > ( ( ) => s_empty . InsertRange ( 1 , s_oneElement ) ) ;
729
746
Assert . Throws < ArgumentOutOfRangeException > ( ( ) => s_empty . InsertRange ( - 1 , s_oneElement ) ) ;
747
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => s_empty . InsertRange ( 1 , ( IEnumerable < int > ) s_oneElement ) ) ;
748
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => s_empty . InsertRange ( - 1 , ( IEnumerable < int > ) s_oneElement ) ) ;
730
749
}
731
750
732
751
[ Fact ]
@@ -795,6 +814,14 @@ public void InsertRangeRight()
795
814
Assert . Equal ( new [ ] { 1 , 2 , 3 , 7 , 8 } , s_manyElements . InsertRange ( 3 , new [ ] { 7 , 8 } ) ) ;
796
815
}
797
816
817
+ [ Fact ]
818
+ public void InsertRangeImmutableArray ( )
819
+ {
820
+ Assert . Equal ( new [ ] { 7 , 8 , 1 , 2 , 3 } , s_manyElements . InsertRange ( 0 , ImmutableArray . Create ( 7 , 8 ) ) ) ;
821
+ Assert . Equal ( new [ ] { 1 , 7 , 2 , 3 } , s_manyElements . InsertRange ( 1 , ImmutableArray . Create ( 7 ) ) ) ;
822
+ Assert . Equal ( new [ ] { 1 , 2 , 3 , 7 } , s_manyElements . InsertRange ( 3 , ImmutableArray . Create ( 7 ) ) ) ;
823
+ }
824
+
798
825
[ Fact ]
799
826
public void RemoveAt ( )
800
827
{
@@ -960,6 +987,20 @@ public void CopyToArray()
960
987
}
961
988
}
962
989
990
+ [ Fact ]
991
+ public void CopyToArrayInt ( )
992
+ {
993
+ var source = ImmutableArray . Create ( 1 , 2 , 3 ) ;
994
+ var target = new int [ 4 ] ;
995
+ source . CopyTo ( target , 1 ) ;
996
+ Assert . Equal ( new [ ] { 0 , 1 , 2 , 3 } , target ) ;
997
+
998
+ Assert . Throws < NullReferenceException > ( ( ) => s_emptyDefault . CopyTo ( target , 0 ) ) ;
999
+ Assert . Throws < ArgumentNullException > ( ( ) => source . CopyTo ( null , 0 ) ) ;
1000
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => source . CopyTo ( target , - 1 ) ) ;
1001
+ Assert . Throws < ArgumentException > ( ( ) => source . CopyTo ( target , 2 ) ) ;
1002
+ }
1003
+
963
1004
[ Fact ]
964
1005
public void CopyToIntArrayIntInt ( )
965
1006
{
@@ -1068,6 +1109,7 @@ public void SortRange()
1068
1109
{
1069
1110
var array = ImmutableArray . Create ( 2 , 4 , 1 , 3 ) ;
1070
1111
Assert . Throws < ArgumentOutOfRangeException > ( ( ) => array . Sort ( - 1 , 2 , Comparer < int > . Default ) ) ;
1112
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => array . Sort ( 1 , - 1 , Comparer < int > . Default ) ) ;
1071
1113
Assert . Throws < ArgumentOutOfRangeException > ( ( ) => array . Sort ( 1 , 4 , Comparer < int > . Default ) ) ;
1072
1114
Assert . Equal ( new int [ ] { 2 , 4 , 1 , 3 } , array . Sort ( array . Length , 0 , Comparer < int > . Default ) ) ;
1073
1115
Assert . Equal ( new [ ] { 2 , 1 , 4 , 3 } , array . Sort ( 1 , 2 , Comparer < int > . Default ) ) ;
@@ -1221,13 +1263,40 @@ public void StructuralComparableArrayInterop()
1221
1263
Assert . Equal ( array . CompareTo ( equalArray , Comparer < int > . Default ) , immArray . CompareTo ( equalArray , Comparer < int > . Default ) ) ;
1222
1264
}
1223
1265
1224
- [ Fact ]
1225
- public void BinarySearch ( )
1266
+ [ Theory ]
1267
+ [ InlineData ( new int [ 0 ] , 5 ) ]
1268
+ [ InlineData ( new int [ ] { 3 } , 5 ) ]
1269
+ [ InlineData ( new int [ ] { 5 } , 5 ) ]
1270
+ [ InlineData ( new int [ ] { 1 , 2 , 3 } , 1 ) ]
1271
+ [ InlineData ( new int [ ] { 1 , 2 , 3 } , 2 ) ]
1272
+ [ InlineData ( new int [ ] { 1 , 2 , 3 } , 3 ) ]
1273
+ [ InlineData ( new int [ ] { 1 , 2 , 3 , 4 } , 4 ) ]
1274
+ public void BinarySearch ( int [ ] array , int value )
1226
1275
{
1227
- Assert . Throws < ArgumentNullException > ( ( ) => Assert . Equal ( Array . BinarySearch ( new int [ 0 ] , 5 ) , ImmutableArray . BinarySearch ( default ( ImmutableArray < int > ) , 5 ) ) ) ;
1228
- Assert . Equal ( Array . BinarySearch ( new int [ 0 ] , 5 ) , ImmutableArray . BinarySearch ( ImmutableArray . Create < int > ( ) , 5 ) ) ;
1229
- Assert . Equal ( Array . BinarySearch ( new int [ ] { 3 } , 5 ) , ImmutableArray . BinarySearch ( ImmutableArray . Create ( 3 ) , 5 ) ) ;
1230
- Assert . Equal ( Array . BinarySearch ( new int [ ] { 5 } , 5 ) , ImmutableArray . BinarySearch ( ImmutableArray . Create ( 5 ) , 5 ) ) ;
1276
+ Assert . Throws < ArgumentNullException > ( ( ) => ImmutableArray . BinarySearch ( default ( ImmutableArray < int > ) , value ) ) ;
1277
+
1278
+ Assert . Equal (
1279
+ Array . BinarySearch ( array , value ) ,
1280
+ ImmutableArray . BinarySearch ( ImmutableArray . Create ( array ) , value ) ) ;
1281
+
1282
+ Assert . Equal (
1283
+ Array . BinarySearch ( array , value , Comparer < int > . Default ) ,
1284
+ ImmutableArray . BinarySearch ( ImmutableArray . Create ( array ) , value , Comparer < int > . Default ) ) ;
1285
+
1286
+ Assert . Equal (
1287
+ Array . BinarySearch ( array , 0 , array . Length , value ) ,
1288
+ ImmutableArray . BinarySearch ( ImmutableArray . Create ( array ) , 0 , array . Length , value ) ) ;
1289
+
1290
+ if ( array . Length > 0 )
1291
+ {
1292
+ Assert . Equal (
1293
+ Array . BinarySearch ( array , 1 , array . Length - 1 , value ) ,
1294
+ ImmutableArray . BinarySearch ( ImmutableArray . Create ( array ) , 1 , array . Length - 1 , value ) ) ;
1295
+ }
1296
+
1297
+ Assert . Equal (
1298
+ Array . BinarySearch ( array , 0 , array . Length , value , Comparer < int > . Default ) ,
1299
+ ImmutableArray . BinarySearch ( ImmutableArray . Create ( array ) , 0 , array . Length , value , Comparer < int > . Default ) ) ;
1231
1300
}
1232
1301
1233
1302
[ Fact ]
0 commit comments