1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
10 using NUnit
.Framework
;
12 using System
.Collections
;
13 using System
.Globalization
;
14 using System
.Reflection
;
15 using System
.Threading
;
16 using System
.Collections
.Generic
;
18 namespace MonoTests
.System
21 enum enua {hola,adios,mas,menos}
;
47 public override bool Equals (object obj
)
52 public override int GetHashCode ()
58 //End Auxiliary Things
61 public class ArrayTest
63 char [] arrsort
= {'d', 'b', 'f', 'e', 'a', 'c'}
;
82 public void TestIsFixedSize() {
84 Assert
.IsTrue (a1
.IsFixedSize
, "All arrays are fixed");
88 public void TestIsReadOnly() {
90 Assert
.IsTrue (!a1
.IsReadOnly
, "No array is readonly");
94 public void TestIsSynchronized() {
96 Assert
.IsTrue (!a1
.IsSynchronized
, "No array is synchronized");
100 public void TestLength() {
103 Assert
.AreEqual (0, a1
.Length
, "Zero length array");
107 Assert
.AreEqual (1, a1
.Length
, "One-length array");
110 char[] a1
= {'c', 'c'}
;
111 Assert
.AreEqual (2, a1
.Length
, "Two-length array");
116 public void TestRank() {
117 char[] a1
= { 'c', 'd', 'e' }
;
118 Assert
.AreEqual (1, a1
.Rank
, "Rank one");
120 char[,] a2
= new Char
[3,3];
121 Assert
.AreEqual (2, a2
.Rank
, "Rank two");
123 char[,,] a3
= new Char
[3,3,3];
124 Assert
.AreEqual (3, a3
.Rank
, "Rank three");
128 public void TestBinarySearch1() {
129 bool errorThrown
= false;
131 Array
.BinarySearch(null, "blue");
132 } catch (ArgumentNullException
) {
135 Assert
.IsTrue (errorThrown
, "#B01");
138 char[,] c1
= new Char
[2,2];
139 Array
.BinarySearch(c1
, "needle");
140 } catch (RankException
) {
143 Assert
.IsTrue (errorThrown
, "#B02");
146 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
147 Assert
.IsTrue (Array
.BinarySearch(arr
, 'c') >= 3, "#B05");
148 Assert
.IsTrue (Array
.BinarySearch(arr
, 'c') < 6, "#B06");
151 char[] arr
= {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}
;
152 Assert
.AreEqual (-4, Array
.BinarySearch(arr
, 'c'), "#B07");
155 char[] arr
= {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
156 Assert
.AreEqual (-9, Array
.BinarySearch(arr
, 'e'), "#B08");
161 public void TestBinarySearch2() {
162 bool errorThrown
= false;
164 Array
.BinarySearch(null, 0, 1, "blue");
165 } catch (ArgumentNullException
) {
168 Assert
.IsTrue (errorThrown
, "#B20");
171 char[,] c1
= new Char
[2,2];
172 Array
.BinarySearch(c1
, 0, 1, "needle");
173 } catch (RankException
) {
176 Assert
.IsTrue (errorThrown
, "#B21");
180 Array
.BinarySearch(c1
, -1, 1, 'a');
181 } catch (ArgumentOutOfRangeException
) {
184 Assert
.IsTrue (errorThrown
, "#B22");
188 Array
.BinarySearch(c1
, 0, -1, 'a');
189 } catch (ArgumentOutOfRangeException
) {
192 Assert
.IsTrue (errorThrown
, "#B23");
196 Array
.BinarySearch(c1
, 0, 4, 'a');
197 } catch (ArgumentException
) {
200 Assert
.IsTrue (errorThrown
, "#B24");
203 char[] arr
= {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
204 Assert
.IsTrue (Array
.BinarySearch(arr
, 2, 8, 'c') >= 5, "#B26");
205 Assert
.IsTrue (Array
.BinarySearch(arr
, 2, 8, 'c') < 8, "#B27");
208 char[] arr
= {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'}
;
209 Assert
.AreEqual (-6, Array
.BinarySearch(arr
, 2, 8, 'c'), "#B28");
212 char[] arr
= {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'}
;
213 Assert
.AreEqual (-11, Array
.BinarySearch(arr
, 2, 8, 'e'), "#B29");
217 public void TestBinarySearch3()
219 int[] array
= new int[100];
221 for (int i
= 0; i
< 100; i
++)
224 Assert
.AreEqual (49, Array
.BinarySearch(array
, 10), "#B30");
228 public void BinarySearch_NullValue ()
230 int[] array
= new int[1];
231 Assert
.AreEqual (-1, Array
.BinarySearch (array
, null), "I=a,o");
232 Assert
.AreEqual (-1, Array
.BinarySearch (array
, null, null), "I=a,o,c");
233 Assert
.AreEqual (-1, Array
.BinarySearch (array
, 0, 1, null), "I=a,i,i,o");
234 Assert
.AreEqual (-1, Array
.BinarySearch (array
, 0, 1, null, null), "I=a,i,i,o,c");
236 object[] o
= new object [3] { this, this, null }
;
237 Assert
.AreEqual (-1, Array
.BinarySearch (o
, null), "O=a,o");
238 Assert
.AreEqual (-1, Array
.BinarySearch (o
, null, null), "O=a,o,c");
239 Assert
.AreEqual (-1, Array
.BinarySearch (o
, 0, 3, null), "O=a,i,i,o");
240 Assert
.AreEqual (-1, Array
.BinarySearch (o
, 0, 3, null, null), "O=a,i,i,o,c");
243 class TestComparer7
: IComparer
<int>
245 public int Compare (int x
, int y
)
248 throw new ApplicationException ();
250 return x
.CompareTo (y
);
255 public void BinarySearch_WithComparer ()
257 var a
= new int[] { 2, 6, 9 }
;
258 Assert
.AreEqual (-3, Array
.BinarySearch (a
, 7, new TestComparer7 ()));
262 public void TestClear() {
263 bool errorThrown
= false;
265 Array
.Clear(null, 0, 1);
266 } catch (ArgumentNullException
) {
269 Assert
.IsTrue (errorThrown
, "#C01");
271 int[] i1
= { 1, 2, 3, 4 }
;
273 int[] compare
= {1,2,3,4}
;
274 Assert
.AreEqual (compare
[0], i1
[0], "#C02");
275 Assert
.AreEqual (compare
[1], i1
[1], "#C03");
276 Assert
.AreEqual (compare
[2], i1
[2], "#C04");
277 Assert
.AreEqual (compare
[3], i1
[3], "#C05");
279 Array
.Clear(i1
, 3, 1);
281 int[] compare
= {1,2,3,0}
;
282 Assert
.AreEqual (compare
[0], i1
[0], "#C06");
283 Assert
.AreEqual (compare
[1], i1
[1], "#C07");
284 Assert
.AreEqual (compare
[2], i1
[2], "#C08");
285 Assert
.AreEqual (compare
[3], i1
[3], "#C09");
287 Array
.Clear(i1
, 1, 1);
289 int[] compare
= {1,0,3,0}
;
290 Assert
.AreEqual (compare
[0], i1
[0], "#C10");
291 Assert
.AreEqual (compare
[1], i1
[1], "#C11");
292 Assert
.AreEqual (compare
[2], i1
[2], "#C12");
293 Assert
.AreEqual (compare
[3], i1
[3], "#C13");
295 Array
.Clear(i1
, 1, 3);
297 int[] compare
= {1,0,0,0}
;
298 Assert
.AreEqual (compare
[0], i1
[0], "#C14");
299 Assert
.AreEqual (compare
[1], i1
[1], "#C15");
300 Assert
.AreEqual (compare
[2], i1
[2], "#C16");
301 Assert
.AreEqual (compare
[3], i1
[3], "#C17");
304 string[] s1
= { "red", "green", "blue" }
;
305 Array
.Clear(s1
, 0, 3);
307 string[] compare
= {null, null, null}
;
308 Assert
.AreEqual (compare
[0], s1
[0], "#C18");
309 Assert
.AreEqual (compare
[1], s1
[1], "#C19");
310 Assert
.AreEqual (compare
[2], s1
[2], "#C20");
315 public void TestClone() {
316 char[] c1
= {'a', 'b', 'c'}
;
317 char[] c2
= (char[])c1
.Clone();
318 Assert
.AreEqual (c1
[0], c2
[0], "#D01");
319 Assert
.AreEqual (c1
[1], c2
[1], "#D02");
320 Assert
.AreEqual (c1
[2], c2
[2], "#D03");
322 char[] d10
= {'a', 'b'}
;
323 char[] d11
= {'a', 'c'}
;
324 char[] d12
= {'b', 'c'}
;
325 char[][] d1
= {d10, d11, d12}
;
326 char[][] d2
= (char[][])d1
.Clone();
327 Assert
.AreEqual (d1
[0], d2
[0], "#D04");
328 Assert
.AreEqual (d1
[1], d2
[1], "#D05");
329 Assert
.AreEqual (d1
[2], d2
[2], "#D06");
332 Assert
.AreEqual (d1
[0], d2
[0], "#D07");
336 public void TestMemberwiseClone () {
337 int[] array
= new int[] { 1, 2, 3 }
;
338 MethodBase mi
= array
.GetType ().GetMethod("MemberwiseClone",
339 BindingFlags
.Instance
| BindingFlags
.NonPublic
);
340 int[] res
= (int[])mi
.Invoke (array
, null);
341 Assert
.AreEqual (3, res
.Length
);
344 [Test
] public void TestIndexer ()
346 int [] a
= new int [10];
350 Assert
.Fail ("IList.this [-1] should throw");
351 } catch (IndexOutOfRangeException
) {
354 Assert
.Fail ("Should have thrown an IndexOutOfRangeException");
359 public void TestCopy() {
361 bool errorThrown
= false;
364 Array
.Copy(c1
, null, 1);
365 } catch (ArgumentNullException
) {
368 Assert
.IsTrue (errorThrown
, "#E01");
371 bool errorThrown
= false;
374 Array
.Copy(null, c1
, 1);
375 } catch (ArgumentNullException
) {
378 Assert
.IsTrue (errorThrown
, "#E02");
381 bool errorThrown
= false;
383 Char
[] c1
= new Char
[1];
384 Char
[,] c2
= new Char
[1,1];
385 Array
.Copy(c1
, c2
, 1);
386 } catch (RankException
) {
389 Assert
.IsTrue (errorThrown
, "#E03");
392 bool errorThrown
= false;
394 Char
[] c1
= new Char
[1];
395 string[] s1
= new String
[1];
396 Array
.Copy(c1
, s1
, 1);
397 } catch (ArrayTypeMismatchException
) {
400 Assert
.IsTrue (errorThrown
, "#E04");
403 bool errorThrown
= false;
405 Char
[] c1
= new Char
[1];
406 Object
[] o1
= new Object
[1];
408 Array
.Copy(o1
, c1
, 1);
409 } catch (InvalidCastException
) {
412 Assert
.IsTrue (errorThrown
, "#E05");
415 bool errorThrown
= false;
417 Char
[] c1
= new Char
[1];
418 Char
[] c2
= new Char
[1];
419 Array
.Copy(c1
, c2
, -1);
420 } catch (ArgumentOutOfRangeException
) {
423 Assert
.IsTrue (errorThrown
, "#E06");
426 bool errorThrown
= false;
428 Char
[] c1
= new Char
[1];
429 Char
[] c2
= new Char
[2];
430 Array
.Copy(c1
, c2
, 2);
431 } catch (ArgumentException
) {
434 Assert
.IsTrue (errorThrown
, "#E07");
437 bool errorThrown
= false;
439 Char
[] c1
= new Char
[1];
440 Char
[] c2
= new Char
[2];
441 Array
.Copy(c2
, c1
, 2);
442 } catch (ArgumentException
) {
445 Assert
.IsTrue (errorThrown
, "#E08");
448 char[] orig
= {'a', 'b', 'd', 'a'}
;
449 char[] copy
= new Char
[4];
450 Array
.Copy(orig
, copy
, 4);
451 for (int i
= 0; i
< orig
.Length
; i
++) {
452 Assert
.AreEqual (orig
[i
], copy
[i
], "#E09(" + i
+ ")");
454 Array
.Clear(copy
, 0, copy
.Length
);
455 for (int i
= 0; i
< orig
.Length
; i
++) {
456 Assert
.AreEqual ((char)0, copy
[i
], "#E10(" + i
+ ")");
458 Array
.Copy(orig
, copy
, 2);
459 Assert
.AreEqual (orig
[0], copy
[0], "#E11");
460 Assert
.AreEqual (orig
[1], copy
[1], "#E12");
461 Assert
.IsTrue (orig
[2] != copy
[2], "#E13");
462 Assert
.IsTrue (orig
[3] != copy
[3], "#E14");
466 public void TestCopy2() {
468 bool errorThrown
= false;
470 Char
[] c1
= new Char
[2];
471 Char
[] c2
= new Char
[2];
472 Array
.Copy(c2
, 1, c1
, 0, 2);
473 } catch (ArgumentException
) {
476 Assert
.IsTrue (errorThrown
, "#E31");
479 bool errorThrown
= false;
481 Char
[] c1
= new Char
[2];
482 Char
[] c2
= new Char
[2];
483 Array
.Copy(c2
, 0, c1
, 1, 2);
484 } catch (ArgumentException
) {
487 Assert
.IsTrue (errorThrown
, "#E32");
490 char[] orig
= {'a', 'b', 'd', 'a'}
;
491 char[] copy
= new Char
[4];
492 Array
.Copy(orig
, 1, copy
, 1, 3);
493 Assert
.IsTrue (copy
[0] != orig
[0], "#E33");
494 for (int i
= 1; i
< orig
.Length
; i
++) {
495 Assert
.AreEqual (orig
[i
], copy
[i
], "#E34(" + i
+ ")");
497 Array
.Clear(copy
, 0, copy
.Length
);
498 Array
.Copy(orig
, 1, copy
, 0, 2);
499 Assert
.AreEqual (orig
[1], copy
[0], "#E35");
500 Assert
.AreEqual (orig
[2], copy
[1], "#E36");
501 Assert
.IsTrue (copy
[2] != orig
[2], "#E37");
502 Assert
.IsTrue (copy
[3] != orig
[3], "#E38");
506 public void Copy_InvalidCast () {
507 object[] arr1
= new object [10];
508 Type
[] arr2
= new Type
[10];
509 arr1
[0] = new object ();
512 Array
.Copy (arr1
, 0, arr2
, 0, 10);
514 } catch (InvalidCastException
) {
517 var arr1_2
= new I
[1] { new DI () }
;
518 var arr2_2
= new C
[1] { new DC () }
;
520 Array
.Copy (arr2_2
, arr1_2
, 1);
522 } catch (InvalidCastException
) {
525 var arr1_3
= new float [5];
526 var arr2_3
= new object [1];
528 Array
.Copy (arr2_3
, arr1_3
, 1);
530 } catch (InvalidCastException
) {
535 public void TestCopyTo() {
537 bool errorThrown
= false;
539 Char
[] c1
= new Char
[2];
541 } catch (ArgumentNullException
) {
544 Assert
.IsTrue (errorThrown
, "#E61");
547 bool errorThrown
= false;
549 Char
[] c1
= new Char
[2];
550 Char
[,] c2
= new Char
[2,2];
552 } catch (ArgumentException
) {
555 Assert
.IsTrue (errorThrown
, "#E62");
558 bool errorThrown
= false;
560 Char
[,] c1
= new Char
[2,2];
561 Char
[] c2
= new Char
[2];
563 } catch (RankException
) {
566 Assert
.IsTrue (errorThrown
, "#E63");
569 bool errorThrown
= false;
571 Char
[,] c1
= new Char
[2,2];
572 Char
[] c2
= new Char
[2];
574 } catch (RankException
) {
577 Assert
.IsTrue (errorThrown
, "#E64");
580 bool errorThrown
= false;
582 Char
[] c1
= new Char
[2];
583 Char
[] c2
= new Char
[2];
585 } catch (ArgumentOutOfRangeException
) {
588 Assert
.IsTrue (errorThrown
, "#E65");
591 bool errorThrown
= false;
593 Char
[] c1
= new Char
[2];
594 Char
[] c2
= new Char
[2];
596 } catch (ArgumentException
) {
599 Assert
.IsTrue (errorThrown
, "#E66");
602 bool errorThrown
= false;
604 Char
[] c1
= new Char
[2];
605 Char
[] c2
= new Char
[2];
607 } catch (ArgumentException
) {
610 Assert
.IsTrue (errorThrown
, "#E67");
614 bool errorThrown
= false;
616 String
[] c1
= new String
[2];
617 // TODO: this crashes mono if there are null
618 // values in the array.
621 Char
[] c2
= new Char
[2];
625 } catch (ArrayTypeMismatchException
) {
628 Assert
.IsTrue (errorThrown
, "#E68");
631 Char
[] orig
= {'a', 'b', 'c', 'd'}
;
632 Char
[] copy
= new Char
[10];
633 Array
.Clear(copy
, 0, copy
.Length
);
634 orig
.CopyTo(copy
, 3);
635 Assert
.AreEqual ((char)0, copy
[0], "#E69");
636 Assert
.AreEqual ((char)0, copy
[1], "#E70");
637 Assert
.AreEqual ((char)0, copy
[2], "#E71");
638 Assert
.AreEqual (orig
[0], copy
[3], "#E72");
639 Assert
.AreEqual (orig
[1], copy
[4], "#E73");
640 Assert
.AreEqual (orig
[2], copy
[5], "#E74");
641 Assert
.AreEqual (orig
[3], copy
[6], "#E75");
642 Assert
.AreEqual ((char)0, copy
[7], "#E76");
643 Assert
.AreEqual ((char)0, copy
[8], "#E77");
644 Assert
.AreEqual ((char)0, copy
[9], "#E78");
647 // The following is valid and must not throw an exception.
648 bool errorThrown
= false;
650 int[] src
= new int [0];
651 int[] dest
= new int [0];
652 src
.CopyTo (dest
, 0);
653 } catch (ArgumentException
) {
656 Assert
.IsTrue (!errorThrown
, "#E79");
661 bool errorThrown
= false;
663 CClass
[] src
= new CClass
[] { new CClass () }
;
664 BClass
[] dest
= new BClass
[1];
666 src
.CopyTo (dest
, 0);
668 } catch (ArrayTypeMismatchException
) {
671 Assert
.IsTrue (errorThrown
, "#E80");
676 public void TestCreateInstance() {
678 bool errorThrown
= false;
680 Array
.CreateInstance(null, 12);
681 } catch (ArgumentNullException
) {
684 Assert
.IsTrue (errorThrown
, "#F01");
687 bool errorThrown
= false;
689 Array
.CreateInstance(Type
.GetType("System.Char"), -3);
690 } catch (ArgumentOutOfRangeException
) {
693 Assert
.IsTrue (errorThrown
, "#F02");
696 bool errorThrown
= false;
698 Array
.CreateInstance(Type
.GetType("System.Char"), (int [])null);
699 } catch (ArgumentNullException
) {
702 Assert
.IsTrue (errorThrown
, "#F03a");
706 bool errorThrown
= false;
708 Array
.CreateInstance(Type
.GetType("System.Char"), (int [])null);
709 } catch (ArgumentNullException
) {
712 Assert
.IsTrue (errorThrown
, "#F03b");
715 bool errorThrown
= false;
717 Array
.CreateInstance(Type
.GetType("System.Char"), null, null);
718 } catch (ArgumentNullException
) {
721 Assert
.IsTrue (errorThrown
, "#F04");
724 bool errorThrown
= false;
726 int[] lengths
= new int [0];
727 Array
.CreateInstance(Type
.GetType("System.Char"), lengths
);
728 } catch (ArgumentException
) {
731 Assert
.IsTrue (errorThrown
, "#F05");
734 bool errorThrown
= false;
736 int[] lengths
= new int [1];
737 int[] bounds
= new int [2];
738 Array
.CreateInstance(Type
.GetType("System.Char"), lengths
, bounds
);
740 } catch (ArgumentException
) {
743 Assert
.IsTrue (errorThrown
, "#F06");
746 char[] c1
= (char[])Array
.CreateInstance(Type
.GetType("System.Char"), 12);
747 Assert
.AreEqual (12, c1
.Length
, "#F07");
749 Array c2
= Array
.CreateInstance(Type
.GetType("System.Char"), 12, 5);
750 Assert
.AreEqual (2, c2
.Rank
, "#F08");
751 Assert
.AreEqual (60, c2
.Length
, "#F09");
755 int[] lengths
= { 3 }
;
756 int[] bounds
= { 5 }
;
757 int[] src
= { 512, 718, 912 }
;
758 Array array
= Array
.CreateInstance(typeof(int), lengths
, bounds
);
760 Assert
.AreEqual (3, array
.Length
, "#F10");
761 Assert
.AreEqual (5, array
.GetLowerBound(0), "#F11");
762 Assert
.AreEqual (7, array
.GetUpperBound(0), "#F12");
764 src
.CopyTo (array
, 5);
766 for (int i
= 0; i
< src
.Length
; i
++)
767 Assert
.AreEqual (src
[i
], array
.GetValue(i
+5), "#F13(" + i
+ ")");
770 // Test that a 1 dimensional array with 0 lower bound is the
771 // same as an szarray
772 Type szarrayType
= new int [10].GetType ();
773 Assert
.IsTrue (szarrayType
== (Array
.CreateInstance (typeof (int), new int[] {1}
, new int[] {0}
)).GetType ());
774 Assert
.IsTrue (szarrayType
!= (Array
.CreateInstance (typeof (int), new int[] {1}
, new int[] {1}
)).GetType ());
778 [ExpectedException (typeof (ArgumentNullException
))]
779 public void TestCreateInstance2 ()
781 Array
.CreateInstance (typeof (Int32
), (int[])null);
785 [ExpectedException (typeof (ArgumentNullException
))]
786 public void TestCreateInstance2b ()
788 Array
.CreateInstance (typeof (Int32
), (long[])null);
792 public void CreateInstanceVoid ()
794 Assert
.Throws
<NotSupportedException
> (delegate () {
795 Array
.CreateInstance (typeof (void), 1);
800 public void TestGetEnumerator() {
801 String
[] s1
= {"this", "is", "a", "test"}
;
802 IEnumerator en
= s1
.GetEnumerator ();
803 Assert
.IsNotNull (en
, "#G01");
805 Assert
.IsTrue (en
.MoveNext (), "#G02");
806 Assert
.AreEqual ("this", en
.Current
, "#G03");
807 Assert
.IsTrue (en
.MoveNext (), "#G04");
808 Assert
.AreEqual ("is", en
.Current
, "#G05");
809 Assert
.IsTrue (en
.MoveNext (), "#G06");
810 Assert
.AreEqual ("a", en
.Current
, "#G07");
811 Assert
.IsTrue (en
.MoveNext (), "#G08");
812 Assert
.AreEqual ("test", en
.Current
, "#G09");
813 Assert
.IsTrue (!en
.MoveNext (), "#G10");
816 Assert
.IsTrue (en
.MoveNext (), "#G11");
817 Assert
.AreEqual ("this", en
.Current
, "#G12");
819 // mutation does not invalidate array enumerator!
820 s1
.SetValue ("change", 1);
821 Assert
.IsTrue (en
.MoveNext (), "#G13");
822 Assert
.AreEqual ("change", en
.Current
, "#G14");
826 public void TestGetEnumeratorMultipleDimension() {
827 String
[,] s1
= {{"this", "is"}
, {"a", "test"}}
;
828 IEnumerator en
= s1
.GetEnumerator ();
829 Assert
.IsNotNull (en
, "#AA01");
831 Assert
.IsTrue (en
.MoveNext (), "#AA02");
832 Assert
.AreEqual ("this", en
.Current
, "#AA03");
833 Assert
.IsTrue (en
.MoveNext (), "#AA04");
834 Assert
.AreEqual ("is", en
.Current
, "#AA05");
835 Assert
.IsTrue (en
.MoveNext (), "#AA06");
836 Assert
.AreEqual ("a", en
.Current
, "#AA07");
837 Assert
.IsTrue (en
.MoveNext (), "#AA08");
838 Assert
.AreEqual ("test", en
.Current
, "#AA09");
839 Assert
.IsTrue (!en
.MoveNext (), "#AA10");
842 Assert
.IsTrue (en
.MoveNext (), "#AA11");
843 Assert
.AreEqual ("this", en
.Current
, "#AA12");
846 // mutation does not invalidate array enumerator!
847 s1
.SetValue ("change", idxs
);
848 Assert
.IsTrue (en
.MoveNext (), "#AA13");
849 Assert
.AreEqual ("change", en
.Current
, "#AA14");
853 public void TestGetEnumeratorNonZeroLowerBounds() {
854 int[] myLengthsArray
= new int[2] { 3, 5 }
;
855 int[] myBoundsArray
= new int[2] { 2, 3 }
;
857 Array myArray
=Array
.CreateInstance( typeof(String
), myLengthsArray
, myBoundsArray
);
858 for ( int i
= myArray
.GetLowerBound(0); i
<= myArray
.GetUpperBound(0); i
++ )
859 for ( int j
= myArray
.GetLowerBound(1); j
<= myArray
.GetUpperBound(1); j
++ ) {
860 int[] myIndicesArray
= new int[2] { i, j }
;
861 myArray
.SetValue( Convert
.ToString(i
) + j
, myIndicesArray
);
863 IEnumerator en
= myArray
.GetEnumerator ();
864 Assert
.IsNotNull (en
, "#AB01");
866 // check the first couple of values
867 Assert
.IsTrue (en
.MoveNext (), "#AB02");
868 Assert
.AreEqual ("23", en
.Current
, "#AB03");
869 Assert
.IsTrue (en
.MoveNext (), "#AB04");
870 Assert
.AreEqual ("24", en
.Current
, "#AB05");
872 // then check the last element's value
875 lastElement
= (string)en
.Current
;
876 } while (en
.MoveNext());
877 Assert
.AreEqual ("47", lastElement
, "#AB06");
881 public void TestIList_Add () {
882 int[] myLengthsArray
= new int[2] { 3, 5 }
;
883 int[] myBoundsArray
= new int[2] { 2, 3 }
;
885 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
887 ((IList
)myArray
).Add ("can not");
888 Assert
.Fail ("IList.Add should throw");
890 catch (NotSupportedException
) {
894 Assert
.Fail ("IList.Add threw wrong exception type");
897 Assert
.Fail ("IList.Add shouldn't get this far");
901 public void TestIList_Insert () {
902 int[] myLengthsArray
= new int[2] { 3, 5 }
;
903 int[] myBoundsArray
= new int[2] { 2, 3 }
;
905 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
907 ((IList
)myArray
).Insert (0, "can not");
908 Assert
.Fail ("IList.Insert should throw");
910 catch (NotSupportedException
) {
914 Assert
.Fail ("IList.Insert threw wrong exception type");
917 Assert
.Fail ("IList.Insert shouldn't get this far");
921 public void TestIList_Remove () {
922 int[] myLengthsArray
= new int[2] { 3, 5 }
;
923 int[] myBoundsArray
= new int[2] { 2, 3 }
;
925 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
927 ((IList
)myArray
).Remove ("can not");
928 Assert
.Fail ("IList.Remove should throw");
930 catch (NotSupportedException
) {
934 Assert
.Fail ("IList.Remove threw wrong exception type");
937 Assert
.Fail ("IList.Remove shouldn't get this far");
941 public void TestIList_RemoveAt () {
942 int[] myLengthsArray
= new int[2] { 3, 5 }
;
943 int[] myBoundsArray
= new int[2] { 2, 3 }
;
945 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
947 ((IList
)myArray
).RemoveAt (0);
948 Assert
.Fail ("IList.RemoveAt should throw");
950 catch (NotSupportedException
) {
954 Assert
.Fail ("IList.RemoveAt threw wrong exception type");
957 Assert
.Fail ("IList.RemoveAt shouldn't get this far");
961 public void TestIList_Contains () {
962 int[] myLengthsArray
= new int[2] { 3, 5 }
;
963 int[] myBoundsArray
= new int[2] { 2, 3 }
;
965 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
968 bool b
= ((IList
)myArray
).Contains ("23");
969 Assert
.Fail ("IList.Contains should throw with multi-dimensional arrays");
971 catch (RankException
) {
972 int[] iArr
= new int[3] { 1, 2, 3}
;
973 // check the first and last items
974 Assert
.IsTrue (((IList
)iArr
).Contains (1), "AC01");
975 Assert
.IsTrue (((IList
)iArr
).Contains (3), "AC02");
977 // and one that is definately not there
978 Assert
.IsTrue (!((IList
)iArr
).Contains (42), "AC03");
982 Assert
.Fail ("Should not get here");
986 public void TestIList_IndexOf () {
987 int[] myLengthsArray
= new int[2] { 3, 5 }
;
988 int[] myBoundsArray
= new int[2] { 2, 3 }
;
990 Array myArray
=Array
.CreateInstance ( typeof(String
), myLengthsArray
, myBoundsArray
);
993 bool b
= ((IList
)myArray
).Contains ("23");
994 Assert
.Fail ("IList.Contains should throw with multi-dimensional arrays");
996 catch (RankException
) {
997 int[] iArr
= new int[3] { 1, 2, 3}
;
998 // check the first and last items
999 Assert
.AreEqual (0, ((IList
)iArr
).IndexOf (1), "AD01");
1000 Assert
.AreEqual (2, ((IList
)iArr
).IndexOf (3), "AD02");
1002 // and one that is definately not there
1003 Assert
.AreEqual (-1, ((IList
)iArr
).IndexOf (42), "AD03");
1005 catch (Exception e
) {
1006 Assert
.Fail ("Unexpected exception: " + e
.ToString());
1009 // check that wierd case whem lowerbound is Int32.MinValue,
1010 // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
1011 int[] myLengthArray
= new int[1] { 3 }
;
1012 int[] myBoundArray
= new int[1] { Int32.MinValue }
;
1013 Array myExtremeArray
=Array
.CreateInstance ( typeof(String
), myLengthArray
, myBoundArray
);
1014 Assert
.AreEqual (Int32
.MaxValue
, ((IList
)myExtremeArray
).IndexOf (42), "AD04");
1018 public void TestGetLength() {
1020 bool errorThrown
= false;
1022 char[] c1
= {'a', 'b', 'c'}
;
1024 } catch (IndexOutOfRangeException
) {
1027 Assert
.IsTrue (errorThrown
, "#H01");
1030 bool errorThrown
= false;
1032 char[] c1
= {'a', 'b', 'c'}
;
1034 } catch (IndexOutOfRangeException
) {
1037 Assert
.IsTrue (errorThrown
, "#H02");
1040 char[] c2
= new Char
[5];
1041 Assert
.AreEqual (5, c2
.GetLength(0), "#H03");
1043 char[,] c3
= new Char
[6,7];
1044 Assert
.AreEqual (6, c3
.GetLength(0), "#H04");
1045 Assert
.AreEqual (7, c3
.GetLength(1), "#H05");
1049 public void TestGetLowerBound() {
1051 bool errorThrown
= false;
1053 char[] c
= {'a', 'b', 'c'}
;
1054 c
.GetLowerBound(-1);
1055 } catch (IndexOutOfRangeException
) {
1058 Assert
.IsTrue (errorThrown
, "#H31");
1061 bool errorThrown
= false;
1063 char[] c
= {'a', 'b', 'c'}
;
1065 } catch (IndexOutOfRangeException
) {
1068 Assert
.IsTrue (errorThrown
, "#H32");
1071 char[] c1
= new Char
[5];
1072 Assert
.AreEqual (0, c1
.GetLowerBound(0), "#H33");
1074 char[,] c2
= new Char
[4,4];
1075 Assert
.AreEqual (0, c2
.GetLowerBound(0), "#H34");
1076 Assert
.AreEqual (0, c2
.GetLowerBound(1), "#H35");
1080 public void TestGetUpperBound() {
1082 bool errorThrown
= false;
1084 char[] c
= {'a', 'b', 'c'}
;
1085 c
.GetUpperBound(-1);
1086 } catch (IndexOutOfRangeException
) {
1089 Assert
.IsTrue (errorThrown
, "#H61");
1092 bool errorThrown
= false;
1094 char[] c
= {'a', 'b', 'c'}
;
1096 } catch (IndexOutOfRangeException
) {
1099 Assert
.IsTrue (errorThrown
, "#H62");
1102 char[] c1
= new Char
[5];
1103 Assert
.AreEqual (4, c1
.GetUpperBound(0), "#H63");
1105 char[,] c2
= new Char
[4,6];
1106 Assert
.AreEqual (3, c2
.GetUpperBound(0), "#H64");
1107 Assert
.AreEqual (5, c2
.GetUpperBound(1), "#H65");
1111 public void TestGetValue1() {
1113 bool errorThrown
= false;
1115 char[,] c
= new Char
[2,2];
1117 } catch (ArgumentException
) {
1120 Assert
.IsTrue (errorThrown
, "#I01");
1123 bool errorThrown
= false;
1125 char[] c
= {'a', 'b', 'c'}
;
1127 } catch (IndexOutOfRangeException
) {
1130 Assert
.IsTrue (errorThrown
, "#I02");
1133 bool errorThrown
= false;
1135 char[] c
= {'a', 'b', 'c'}
;
1137 } catch (IndexOutOfRangeException
) {
1140 Assert
.IsTrue (errorThrown
, "#I03");
1143 char[] c1
= {'a', 'b', 'c', 'd'}
;
1144 for (int i
= 0; i
< c1
.Length
; i
++) {
1145 Assert
.AreEqual (c1
[i
], c1
.GetValue(i
), "#I04(" + i
+ ")");
1150 public void TestGetValue2() {
1152 bool errorThrown
= false;
1154 char[] c
= new Char
[2];
1156 } catch (ArgumentException
) {
1159 Assert
.IsTrue (errorThrown
, "#I21");
1162 bool errorThrown
= false;
1164 char[,] c
= new Char
[2,2];
1166 } catch (IndexOutOfRangeException
) {
1169 Assert
.IsTrue (errorThrown
, "#I22");
1172 bool errorThrown
= false;
1174 char[,] c
= new Char
[2,2];
1176 } catch (IndexOutOfRangeException
) {
1179 Assert
.IsTrue (errorThrown
, "#I23");
1182 char[,] c1
= new Char
[4,6];
1183 for (int i
= 0; i
< 24; i
++) {
1186 c1
[first
,second
] = (char)(((int)'a')+i
);
1188 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
1189 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
1190 Assert
.AreEqual (c1
[i
, j
], c1
.GetValue(i
, j
), "#I24(" + i
+ "," + j
+ ")");
1196 public void TestGetValue3() {
1198 bool errorThrown
= false;
1200 char[] c
= new Char
[2];
1202 } catch (ArgumentException
) {
1205 Assert
.IsTrue (errorThrown
, "#I41");
1208 bool errorThrown
= false;
1210 char[,,] c
= new Char
[2,2,2];
1211 c
.GetValue(-1, 1, 1);
1212 } catch (IndexOutOfRangeException
) {
1215 Assert
.IsTrue (errorThrown
, "#I42");
1218 bool errorThrown
= false;
1220 char[,,] c
= new Char
[2,2,2];
1222 } catch (IndexOutOfRangeException
) {
1225 Assert
.IsTrue (errorThrown
, "#I43");
1228 char[,,] c1
= new Char
[4,2,3];
1229 for (int i
= 0; i
< 24; i
++) {
1231 int remains
= i
% 6;
1232 int second
= remains
/ 3;
1233 int third
= remains
% 3;
1234 c1
[first
,second
, third
] = (char)(((int)'a')+i
);
1236 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
1237 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
1238 for (int k
= 0; k
< c1
.GetLength(2); k
++) {
1239 Assert
.AreEqual (c1
[i
, j
, k
], c1
.GetValue(i
, j
, k
), "#I44(" + i
+ "," + j
+ ")");
1246 [ExpectedException (typeof (ArgumentNullException
))]
1247 public void TestGetValueLongArray ()
1249 char[] c
= new Char
[2];
1250 c
.GetValue((long [])null);
1254 public void TestGetValueN() {
1256 bool errorThrown
= false;
1258 char[] c
= new Char
[2];
1259 c
.GetValue((int [])null);
1260 } catch (ArgumentNullException
) {
1263 Assert
.IsTrue (errorThrown
, "#I61a");
1266 bool errorThrown
= false;
1268 char[] c
= new Char
[2];
1269 int[] coords
= {1, 1}
;
1271 } catch (ArgumentException
) {
1274 Assert
.IsTrue (errorThrown
, "#I62");
1277 bool errorThrown
= false;
1279 char[,] c
= new Char
[2,2];
1280 int[] coords
= {-1, 1}
;
1282 } catch (IndexOutOfRangeException
) {
1285 Assert
.IsTrue (errorThrown
, "#I63");
1288 bool errorThrown
= false;
1290 char[,] c
= new Char
[2,2];
1291 int[] coords
= {4, 1}
;
1293 } catch (IndexOutOfRangeException
) {
1296 Assert
.IsTrue (errorThrown
, "#I64");
1299 char[,] c1
= new Char
[4,6];
1300 for (int i
= 0; i
< 24; i
++) {
1303 c1
[first
,second
] = (char)(((int)'a')+i
);
1305 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
1306 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
1307 int[] coords
= {i, j}
;
1308 Assert
.AreEqual (c1
[i
, j
], c1
.GetValue(coords
), "#I65(" + i
+ "," + j
+ ")");
1314 public void TestIndexOf1() {
1316 bool errorThrown
= false;
1318 Array
.IndexOf(null, "huh?");
1319 } catch (ArgumentNullException
) {
1322 Assert
.IsTrue (errorThrown
, "#J01");
1325 bool errorThrown
= false;
1327 char[,] c
= new Char
[2,2];
1328 Array
.IndexOf(c
, "huh?");
1329 } catch (RankException
) {
1332 Assert
.IsTrue (errorThrown
, "#J02");
1335 String
[] s1
= {"this", "is", "a", "test"}
;
1336 Assert
.AreEqual (-1, Array
.IndexOf(s1
, null), "#J03");
1337 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "nothing"), "#J04");
1338 Assert
.AreEqual (0, Array
.IndexOf(s1
, "this"), "#J05");
1339 Assert
.AreEqual (3, Array
.IndexOf(s1
, "test"), "#J06");
1343 public void TestIndexOf2() {
1345 bool errorThrown
= false;
1347 Array
.IndexOf(null, "huh?", 0);
1348 } catch (ArgumentNullException
) {
1351 Assert
.IsTrue (errorThrown
, "#J21");
1354 bool errorThrown
= false;
1356 char[,] c
= new Char
[2,2];
1357 Array
.IndexOf(c
, "huh?", 0);
1358 } catch (RankException
) {
1361 Assert
.IsTrue (errorThrown
, "#J22");
1364 bool errorThrown
= false;
1366 char[] c
= new Char
[2];
1367 Array
.IndexOf(c
, "huh?", 3);
1368 } catch (ArgumentOutOfRangeException
) {
1371 Assert
.IsTrue (errorThrown
, "#J23");
1374 String
[] s1
= {"this", "is", "really", "a", "test"}
;
1375 Assert
.AreEqual (-1, Array
.IndexOf(s1
, null, 1), "#J24");
1376 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "nothing", 1), "#J25");
1377 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "this", 1), "#J26");
1378 Assert
.AreEqual (1, Array
.IndexOf(s1
, "is", 1), "#J27");
1379 Assert
.AreEqual (4, Array
.IndexOf(s1
, "test", 1), "#J28");
1383 public void TestIndexOf3() {
1385 bool errorThrown
= false;
1387 Array
.IndexOf(null, "huh?", 0, 1);
1388 } catch (ArgumentNullException
) {
1391 Assert
.IsTrue (errorThrown
, "#J41");
1394 bool errorThrown
= false;
1396 char[,] c
= new Char
[2,2];
1397 Array
.IndexOf(c
, "huh?", 0, 1);
1398 } catch (RankException
) {
1401 Assert
.IsTrue (errorThrown
, "#J42");
1404 bool errorThrown
= false;
1406 char[] c
= new Char
[2];
1407 Array
.IndexOf(c
, "huh?", 3, 1);
1408 } catch (ArgumentOutOfRangeException
) {
1411 Assert
.IsTrue (errorThrown
, "#J43");
1414 bool errorThrown
= false;
1416 char[] c
= new Char
[2];
1417 Array
.IndexOf(c
, "huh?", 0, 5);
1418 } catch (ArgumentOutOfRangeException
) {
1421 Assert
.IsTrue (errorThrown
, "#J44");
1424 String
[] s1
= {"this", "is", "really", "a", "test"}
;
1425 Assert
.AreEqual (-1, Array
.IndexOf(s1
, null, 1, 3), "#J45");
1426 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "nothing", 1, 3), "#J46");
1427 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "this", 1, 3), "#J47");
1428 Assert
.AreEqual (1, Array
.IndexOf(s1
, "is", 1, 3), "#J48");
1429 Assert
.AreEqual (-1, Array
.IndexOf(s1
, "test", 1, 3), "#J49");
1430 Assert
.AreEqual (3, Array
.IndexOf(s1
, "a", 1, 3), "#J50");
1434 public void TestIndexOf_CustomEqual ()
1436 DataEqual
[] test
= new DataEqual
[] { new DataEqual () }
;
1437 Assert
.AreEqual (0, Array
.IndexOf (test
, "asdfas", 0));
1439 IList array
= (IList
)test
;
1440 Assert
.AreEqual (0, array
.IndexOf ("asdfas"));
1444 public void TestLastIndexOf1() {
1446 bool errorThrown
= false;
1448 Array
.LastIndexOf(null, "huh?");
1449 } catch (ArgumentNullException
) {
1452 Assert
.IsTrue (errorThrown
, "#K01");
1455 bool errorThrown
= false;
1457 char[,] c
= new Char
[2,2];
1458 Array
.LastIndexOf(c
, "huh?");
1459 } catch (RankException
) {
1462 Assert
.IsTrue (errorThrown
, "#K02");
1465 String
[] s1
= {"this", "is", "a", "a", "test"}
;
1466 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, null), "#K03");
1467 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "nothing"), "#K04");
1468 Assert
.AreEqual (0, Array
.LastIndexOf(s1
, "this"), "#K05");
1469 Assert
.AreEqual (4, Array
.LastIndexOf(s1
, "test"), "#K06");
1470 Assert
.AreEqual (3, Array
.LastIndexOf(s1
, "a"), "#K07");
1472 Assert
.AreEqual (-1, Array
.LastIndexOf (new String
[0], "foo"));
1476 public void TestLastIndexOf2() {
1478 bool errorThrown
= false;
1480 Array
.LastIndexOf(null, "huh?", 0);
1481 } catch (ArgumentNullException
) {
1484 Assert
.IsTrue (errorThrown
, "#K21");
1487 bool errorThrown
= false;
1489 char[,] c
= new Char
[2,2];
1490 Array
.LastIndexOf(c
, "huh?", 0);
1491 } catch (RankException
) {
1494 Assert
.IsTrue (errorThrown
, "#K22");
1497 bool errorThrown
= false;
1499 char[] c
= new Char
[2];
1500 Array
.LastIndexOf(c
, "huh?", 3);
1501 } catch (ArgumentOutOfRangeException
) {
1504 Assert
.IsTrue (errorThrown
, "#K23");
1507 String
[] s1
= {"this", "is", "really", "a", "test"}
;
1508 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, null, 3), "#K24");
1509 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "nothing", 3), "#K25");
1510 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "test", 3), "#K26");
1511 Assert
.AreEqual (3, Array
.LastIndexOf(s1
, "a", 3), "#K27");
1512 Assert
.AreEqual (0, Array
.LastIndexOf(s1
, "this", 3), "#K28");
1516 public void TestLastIndexOf3() {
1518 bool errorThrown
= false;
1520 Array
.LastIndexOf(null, "huh?", 0, 1);
1521 } catch (ArgumentNullException
) {
1524 Assert
.IsTrue (errorThrown
, "#K41");
1527 bool errorThrown
= false;
1529 char[,] c
= new Char
[2,2];
1530 Array
.LastIndexOf(c
, "huh?", 0, 1);
1531 } catch (RankException
) {
1534 Assert
.IsTrue (errorThrown
, "#K42");
1537 bool errorThrown
= false;
1539 char[] c
= new Char
[2];
1540 Array
.LastIndexOf(c
, "huh?", 3, 1);
1541 } catch (ArgumentOutOfRangeException
) {
1544 Assert
.IsTrue (errorThrown
, "#K43");
1547 bool errorThrown
= false;
1549 char[] c
= new Char
[2];
1550 Array
.LastIndexOf(c
, "huh?", 0, 5);
1551 } catch (ArgumentOutOfRangeException
) {
1554 Assert
.IsTrue (errorThrown
, "#K44");
1557 String
[] s1
= {"this", "is", "really", "a", "test"}
;
1558 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, null, 3, 3), "#K45");
1559 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "nothing", 3, 3), "#K46");
1560 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "this", 3, 3), "#K47");
1561 Assert
.AreEqual (1, Array
.LastIndexOf(s1
, "is", 3, 3), "#K48");
1562 Assert
.AreEqual (-1, Array
.LastIndexOf(s1
, "test", 3, 3), "#K49");
1563 Assert
.AreEqual (3, Array
.LastIndexOf(s1
, "a", 3, 3), "#K50");
1567 public void TestLastIndexOf4 ()
1569 short [] a
= new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238}
;
1571 Array
.LastIndexOf (a
, (object)16, -1);
1572 NUnit
.Framework
.Assert
.Fail ("#1");
1573 } catch (ArgumentOutOfRangeException
) { }
1576 Array
.LastIndexOf
<short> (a
, 16, -1);
1577 NUnit
.Framework
.Assert
.Fail ("#2");
1578 } catch (ArgumentOutOfRangeException
) { }
1582 public void TestLastIndexOf5 ()
1584 char [] a
= new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'}
;
1589 for (int i
= a
.Length
- 1; i
>= 0 ; i
--) {
1591 retval
= Array
.LastIndexOf(a
, a
[i
], i
, i
+ 1);
1595 Assert
.IsTrue (!error
);
1599 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1600 public void LastIndexOf_StartIndexOverflow ()
1602 // legal - no exception
1603 byte[] array
= new byte [16];
1604 Array
.LastIndexOf (array
, this, Int32
.MaxValue
, 1);
1608 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
1609 public void LastIndexOf_CountOverflow ()
1611 // legal - no exception
1612 byte[] array
= new byte [16];
1613 Array
.LastIndexOf (array
, this, 1, Int32
.MaxValue
);
1617 public void LastIndexOf_0LengthArray ()
1619 Array array
= Array
.CreateInstance (typeof (char), 0);
1620 int idx
= Array
.LastIndexOf (array
, (object) null, -1, 0);
1621 Assert
.IsTrue (idx
== -1, "#01");
1622 idx
= Array
.LastIndexOf (array
, (object) null, -1, 10);
1623 Assert
.IsTrue (idx
== -1, "#02");
1624 idx
= Array
.LastIndexOf (array
, (object) null, -100, 10);
1625 Assert
.IsTrue (idx
== -1, "#02");
1627 array
= Array
.CreateInstance (typeof (char), 1);
1629 Array
.LastIndexOf (array
, (object) null, -1, 0);
1630 Assert
.Fail ("#04");
1631 } catch (ArgumentOutOfRangeException e
) {
1634 Array
.LastIndexOf (array
, (object) null, -1, 10);
1635 Assert
.Fail ("#05");
1636 } catch (ArgumentOutOfRangeException e
) {
1639 Array
.LastIndexOf (array
, (object) null, -100, 10);
1640 Assert
.Fail ("#06");
1641 } catch (ArgumentOutOfRangeException e
) {
1647 public void FindIndexTest ()
1649 var a
= new int[] { 2, 2, 2, 3, 2 }
;
1650 Assert
.AreEqual (2, Array
.FindIndex (a
, 2, 2, l
=> true));
1654 public void FindIndex_Invalid ()
1656 var array
= new int [] { 1, 2, 3, 4, 5 }
;
1659 Array
.FindIndex (array
, null);
1661 } catch (ArgumentNullException
) {
1665 Array
.FindIndex (array
, -1, l
=> true);
1667 } catch (ArgumentOutOfRangeException
) {
1671 Array
.FindIndex (array
, -1, 0, l
=> true);
1672 Assert
.Fail ("#2b");
1673 } catch (ArgumentOutOfRangeException
) {
1677 Array
.FindIndex (array
, 0, -1, l
=> true);
1679 } catch (ArgumentOutOfRangeException
) {
1683 Array
.FindIndex (array
, 100, l
=> true);
1685 } catch (ArgumentOutOfRangeException
) {
1689 Array
.FindIndex (array
, 100, 0, l
=> true);
1690 Assert
.Fail ("#4b");
1691 } catch (ArgumentOutOfRangeException
) {
1695 Array
.FindIndex (array
, 7, 2, l
=> true);
1697 } catch (ArgumentOutOfRangeException
) {
1701 [Test
, ExpectedException (typeof (ArgumentNullException
))]
1702 public void FindLastNullTest ()
1704 var array
= new int [] { 1, 2, 3, 4, 5 }
;
1705 Array
.FindLast (array
, null);
1709 public void FindLastIndexTest ()
1711 var array
= new int [] { 1, 2, 3, 4, 5 }
;
1713 Assert
.AreEqual (2, Array
.FindLastIndex (array
, 2, 3, l
=> true));
1714 Assert
.AreEqual (2, Array
.FindLastIndex (array
, 2, 2, l
=> true));
1715 Assert
.AreEqual (1, Array
.FindLastIndex (array
, 1, 2, l
=> true));
1719 public void FindLastIndex_Invalid ()
1721 var array
= new int [] { 1, 2, 3, 4, 5 }
;
1723 Array
.FindLastIndex (array
, null);
1725 } catch (ArgumentNullException
) {
1729 Array
.FindLastIndex (array
, -1, l
=> true);
1731 } catch (ArgumentOutOfRangeException
) {
1735 Array
.FindLastIndex (array
, -1, 0, l
=> true);
1736 Assert
.Fail ("#2b");
1737 } catch (ArgumentOutOfRangeException
) {
1741 Array
.FindLastIndex (array
, 0, -1, l
=> true);
1743 } catch (ArgumentOutOfRangeException
) {
1747 Array
.FindLastIndex (array
, 100, l
=> true);
1749 } catch (ArgumentOutOfRangeException
) {
1753 Array
.FindLastIndex (array
, 100, 0, l
=> true);
1754 Assert
.Fail ("#4b");
1755 } catch (ArgumentOutOfRangeException
) {
1759 Array
.FindLastIndex (array
, 2, 4, l
=> true);
1761 } catch (ArgumentOutOfRangeException
) {
1766 public void TestReverse() {
1768 bool errorThrown
= false;
1770 Array
.Reverse(null);
1771 } catch (ArgumentNullException
) {
1774 Assert
.IsTrue (errorThrown
, "#L01");
1777 bool errorThrown
= false;
1779 char[,] c
= new Char
[2,2];
1781 } catch (RankException
) {
1784 Assert
.IsTrue (errorThrown
, "#L02");
1787 char[] c1
= {'a', 'b', 'c', 'd'}
;
1789 Assert
.AreEqual ('d', c1
[0], "#L03");
1790 Assert
.AreEqual ('c', c1
[1], "#L04");
1791 Assert
.AreEqual ('b', c1
[2], "#L05");
1792 Assert
.AreEqual ('a', c1
[3], "#L06");
1795 bool errorThrown
= false;
1797 Array
.Reverse(null, 0, 0);
1798 } catch (ArgumentNullException
) {
1801 Assert
.IsTrue (errorThrown
, "#L07");
1804 bool errorThrown
= false;
1806 char[,] c
= new Char
[2,2];
1807 Array
.Reverse(c
, 0, 0);
1808 } catch (RankException
) {
1811 Assert
.IsTrue (errorThrown
, "#L08");
1814 //bool errorThrown = false;
1816 // char[] c = new Char[2];
1817 // Array.Reverse(c, 0, 3);
1818 //} catch (ArgumentOutOfRangeException) {
1819 // errorThrown = true;
1821 //Assert.IsTrue (errorThrown, "#L09");
1824 //bool errorThrown = false;
1826 // char[] c = new Char[2];
1827 // Array.Reverse(c, 3, 0);
1828 //} catch (ArgumentOutOfRangeException) {
1829 // errorThrown = true;
1831 //Assert.IsTrue (errorThrown, "#L10");
1834 char[] c2
= { 'a', 'b', 'c', 'd'}
;
1835 Array
.Reverse(c2
, 1, 2);
1836 Assert
.AreEqual ('a', c2
[0], "#L11");
1837 Assert
.AreEqual ('c', c2
[1], "#L12");
1838 Assert
.AreEqual ('b', c2
[2], "#L13");
1839 Assert
.AreEqual ('d', c2
[3], "#L14");
1844 public void ReverseStruct () {
1845 BStruct
[] c3
= new BStruct
[2];
1846 c3
[0] = new BStruct () { i1 = 1, i2 = 2, i3 = 3 }
;
1847 c3
[1] = new BStruct () { i1 = 4, i2 = 5, i3 = 6 }
;
1849 Assert
.AreEqual (4, c3
[0].i1
);
1850 Assert
.AreEqual (5, c3
[0].i2
);
1851 Assert
.AreEqual (6, c3
[0].i3
);
1852 Assert
.AreEqual (1, c3
[1].i1
);
1853 Assert
.AreEqual (2, c3
[1].i2
);
1854 Assert
.AreEqual (3, c3
[1].i3
);
1858 public int i1
, i2
, i3
;
1862 public void TestSetValue1() {
1864 bool errorThrown
= false;
1866 char[,] c
= new Char
[2,2];
1867 c
.SetValue("buh", 1);
1868 } catch (ArgumentException
) {
1871 Assert
.IsTrue (errorThrown
, "#M01");
1874 bool errorThrown
= false;
1876 char[] c
= {'a', 'b', 'c'}
;
1877 c
.SetValue("buh", -1);
1878 } catch (IndexOutOfRangeException
) {
1881 Assert
.IsTrue (errorThrown
, "#M02");
1884 bool errorThrown
= false;
1886 char[] c
= {'a', 'b', 'c'}
;
1887 c
.SetValue("buh", 4);
1888 } catch (IndexOutOfRangeException
) {
1891 Assert
.IsTrue (errorThrown
, "#M03");
1894 char[] c1
= {'a', 'b', 'c', 'd'}
;
1895 char[] c2
= new char[4];
1896 for (int i
= 0; i
< c1
.Length
; i
++) {
1897 c2
.SetValue(c1
[i
], i
);
1899 for (int i
= 0; i
< c1
.Length
; i
++) {
1900 Assert
.AreEqual (c1
[i
], c2
[i
], "#M04(" + i
+ ")");
1903 int[] c3
= { 1, 2, 3 }
;
1904 long[] c4
= new long [3];
1906 for (int i
= 0; i
< c3
.Length
; i
++)
1907 c4
.SetValue (c3
[i
], i
);
1911 } catch (Exception e
) {
1912 Assert
.Fail ("c3.CopyTo(): e=" + e
);
1914 for (int i
= 0; i
< c3
.Length
; i
++)
1915 Assert
.IsTrue (c3
[i
] == c4
[i
], "#M05(" + i
+ ")");
1917 Object
[] c5
= new Object
[3];
1918 long[] c6
= new long [3];
1922 } catch (Exception e
) {
1923 Assert
.Fail ("c4.CopyTo(): e=" + e
);
1928 } catch (Exception e
) {
1929 Assert
.Fail ("c5.CopyTo(): e=" + e
);
1931 // for (int i = 0; i < c5.Length; i++)
1932 // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
1936 public void TestSetValue2() {
1938 bool errorThrown
= false;
1940 char[] c
= new Char
[2];
1941 c
.SetValue("buh", 1,1);
1942 } catch (ArgumentException
) {
1945 Assert
.IsTrue (errorThrown
, "#M21");
1948 bool errorThrown
= false;
1950 char[,] c
= new Char
[2,2];
1951 c
.SetValue("buh", -1, 1);
1952 } catch (IndexOutOfRangeException
) {
1955 Assert
.IsTrue (errorThrown
, "#M22");
1958 bool errorThrown
= false;
1960 char[,] c
= new Char
[2,2];
1961 c
.SetValue("buh", 4,1);
1962 } catch (IndexOutOfRangeException
) {
1965 Assert
.IsTrue (errorThrown
, "#M23");
1968 char[,] c1
= new Char
[4,6];
1969 char[,] c2
= new Char
[4,6];
1970 for (int i
= 0; i
< 24; i
++) {
1973 c1
[first
,second
] = (char)(((int)'a')+i
);
1974 c2
.SetValue(c1
[first
,second
], first
, second
);
1976 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
1977 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
1978 Assert
.AreEqual (c1
[i
, j
], c2
[i
, j
], "#M24(" + i
+ "," + j
+ ")");
1984 public void TestSetValue3() {
1986 bool errorThrown
= false;
1988 char[] c
= new Char
[2];
1989 c
.SetValue("buh", 1,1,1);
1990 } catch (ArgumentException
) {
1993 Assert
.IsTrue (errorThrown
, "#M41");
1996 bool errorThrown
= false;
1998 char[,,] c
= new Char
[2,2,2];
1999 c
.SetValue("buh", -1, 1, 1);
2000 } catch (IndexOutOfRangeException
) {
2003 Assert
.IsTrue (errorThrown
, "#M42");
2006 bool errorThrown
= false;
2008 char[,,] c
= new Char
[2,2,2];
2009 c
.SetValue("buh", 4,1,1);
2010 } catch (IndexOutOfRangeException
) {
2013 Assert
.IsTrue (errorThrown
, "#M43");
2016 char[,,] c1
= new Char
[4,2,3];
2017 char[,,] c2
= new Char
[4,2,3];
2018 for (int i
= 0; i
< 24; i
++) {
2020 int remains
= i
% 6;
2021 int second
= remains
/ 3;
2022 int third
= remains
% 3;
2023 c1
[first
,second
, third
] = (char)(((int)'a')+i
);
2024 c2
.SetValue(c1
[first
, second
, third
], first
, second
, third
);
2026 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
2027 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
2028 for (int k
= 0; k
< c1
.GetLength(2); k
++) {
2029 Assert
.AreEqual (c1
[i
, j
, k
], c2
[i
, j
, k
], "#M44(" + i
+ "," + j
+ " )");
2036 [ExpectedException (typeof (ArgumentNullException
))]
2037 public void TestSetValueLongArray ()
2039 char[] c
= new Char
[2];
2040 c
.SetValue("buh", (long [])null);
2044 public void TestSetValueN() {
2046 bool errorThrown
= false;
2048 char[] c
= new Char
[2];
2049 c
.SetValue("buh", (int [])null);
2050 } catch (ArgumentNullException
) {
2053 Assert
.IsTrue (errorThrown
, "#M61a");
2056 bool errorThrown
= false;
2058 char[] c
= new Char
[2];
2059 int[] coords
= {1, 1}
;
2060 c
.SetValue("buh", coords
);
2061 } catch (ArgumentException
) {
2064 Assert
.IsTrue (errorThrown
, "#M62");
2067 bool errorThrown
= false;
2069 char[,] c
= new Char
[2,2];
2070 int[] coords
= {-1, 1}
;
2071 c
.SetValue("buh", coords
);
2072 } catch (IndexOutOfRangeException
) {
2075 Assert
.IsTrue (errorThrown
, "#M63");
2078 bool errorThrown
= false;
2080 char[,] c
= new Char
[2,2];
2081 int[] coords
= {4, 1}
;
2082 c
.SetValue("buh", coords
);
2083 } catch (IndexOutOfRangeException
) {
2086 Assert
.IsTrue (errorThrown
, "#M64");
2089 char[,] c1
= new Char
[4,6];
2090 char[,] c2
= new Char
[4,6];
2091 for (int i
= 0; i
< 24; i
++) {
2094 c1
[first
,second
] = (char)(((int)'a')+i
);
2095 int[] coords
= {first, second}
;
2096 c2
.SetValue(c1
[first
,second
], coords
);
2098 for (int i
= 0; i
< c1
.GetLength(0); i
++) {
2099 for (int j
= 0; j
< c1
.GetLength(1); j
++) {
2100 Assert
.AreEqual (c1
[i
, j
], c2
[i
, j
], "#M65(" + i
+ "," + j
+ ")");
2106 public void TestSetValue4() {
2108 int[] c1
= { 1, 2, 3 }
;
2109 long[] c2
= new long [3];
2111 for (int i
= 0; i
< c1
.Length
; i
++)
2112 c2
.SetValue (c1
[i
], i
);
2114 for (int i
= 0; i
< c1
.Length
; i
++) {
2115 Assert
.IsTrue (c1
[i
] == c2
[i
], "#M81(" + i
+ ")");
2116 Assert
.AreEqual (typeof (long), c2
[i
].GetType (), "#M82(" + i
+ ")");
2120 long[] c1
= { 1, 2, 3 }
;
2121 int[] c2
= new int [3];
2122 bool errorThrown
= false;
2124 c2
.SetValue (c1
[0], 0);
2125 } catch (ArgumentException
) {
2128 Assert
.IsTrue (errorThrown
, "#M83");
2131 int[] c1
= { 1, 2, 3 }
;
2132 Object
[] c2
= new Object
[3];
2134 for (int i
= 0; i
< c1
.Length
; i
++)
2135 c2
.SetValue (c1
[i
], i
);
2137 for (int i
= 0; i
< c1
.Length
; i
++)
2138 Assert
.AreEqual (c1
[i
], Convert
.ToInt32 (c2
[i
]), "#M84(" + i
+ ")");
2141 Object
[] c1
= new Object
[3];
2142 Object
[] c2
= new Object
[3];
2143 c1
[0] = new Object ();
2145 for (int i
= 0; i
< c1
.Length
; i
++)
2146 c2
.SetValue (c1
[i
], i
);
2148 for (int i
= 0; i
< c1
.Length
; i
++)
2149 Assert
.AreEqual (c1
[i
], c2
[i
], "#M85(" + i
+ ")");
2152 Object
[] c1
= new Object
[3];
2153 string[] c2
= new String
[3];
2154 string test
= "hello";
2157 c2
.SetValue (c1
[0], 0);
2158 Assert
.AreEqual (c1
[0], c2
[0], "#M86");
2159 Assert
.AreEqual ("hello", c2
[0], "#M87");
2162 char[] c1
= { 'a', 'b', 'c' }
;
2163 string[] c2
= new string [3];
2165 c2
.SetValue (c1
[0], 0);
2166 Assert
.Fail ("#M88");
2167 } catch (InvalidCastException
) {}
2170 Single
[] c1
= { 1.2F, 2.3F, 3.4F, 4.5F }
;
2171 long[] c2
= new long [3];
2173 c2
.SetValue (c1
[0], 0);
2174 Assert
.Fail ("#M89");
2175 } catch (ArgumentException
) {}
2203 UInt32 v11
= 235354;
2204 UInt64 v12
= 234552;
2206 Object
[] va1
= { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 }
;
2207 Object
[] va2
= { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
2208 "-4.8F", "24234", "235354", "234552" };
2210 Object
[][] vt
= { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 }
;
2213 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2214 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
2215 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2216 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
2217 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
2218 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
2219 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
2220 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
2221 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
2222 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
2223 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
2224 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
2229 for (int i
= 0; i
< types
.Length
; i
++) {
2230 for (int j
= 0; j
< types
.Length
; j
++) {
2231 Array array
= Array
.CreateInstance (types
[j
], 2);
2233 Object
value = vt
[j
][i
];
2235 bool errorThrown
= false;
2237 array
.SetValue (value, 0);
2238 } catch (ArgumentException
) {
2242 int ex_index
= (i
* types
.Length
) + j
;
2244 Assert
.AreEqual (errorThrown
, arg_ex
[ex_index
] == 1, "#M90(" + types
[i
] + "," + types
[j
] + ")");
2248 for (int i
= 0; i
< types
.Length
; i
++) {
2249 String
[] array
= new String
[2];
2251 Object
value = va1
[i
];
2253 bool errorThrown
= false;
2255 array
.SetValue (value, 0);
2256 } catch (InvalidCastException
) {
2260 Assert
.IsTrue (errorThrown
, "#M91(" + types
[i
] + ")");
2263 for (int i
= 0; i
< types
.Length
; i
++) {
2264 Array array
= Array
.CreateInstance (types
[i
], 2);
2266 Object
value = va2
[i
];
2268 bool errorThrown
= false;
2270 array
.SetValue (value, 0);
2271 } catch (InvalidCastException
) {
2275 Assert
.IsTrue (errorThrown
, "#M92(" + types
[i
] + ")");
2278 for (int i
= 0; i
< types
.Length
; i
++) {
2279 Array array
= Array
.CreateInstance (types
[i
], 2);
2281 Object
value = null;
2283 bool errorThrown
= false;
2285 array
.SetValue (value, 0);
2286 } catch (InvalidCastException
) {
2290 Assert
.IsFalse (errorThrown
, "#M93(" + types
[i
] + ")");
2295 for (int i
= 0; i
< types
.Length
; i
++) {
2296 for (int j
= 0; j
< types
.Length
; j
++) {
2297 Array source
= Array
.CreateInstance (types
[i
], 2);
2298 Array array
= Array
.CreateInstance (types
[j
], 2);
2300 source
.SetValue (vt
[j
][i
], 0);
2301 source
.SetValue (vt
[j
][i
], 1);
2303 bool errorThrown
= false;
2305 Array
.Copy (source
, array
, 2);
2306 } catch (ArrayTypeMismatchException
) {
2310 int ex_index
= (i
* types
.Length
) + j
;
2312 Assert
.AreEqual (errorThrown
, arg_ex
[ex_index
] == 1, "#M94(" + types
[i
] + "," + types
[j
] + ")");
2316 for (int i
= 0; i
< types
.Length
; i
++) {
2317 Array source
= Array
.CreateInstance (types
[i
], 2);
2318 String
[] array
= new String
[2];
2320 source
.SetValue (va1
[i
], 0);
2321 source
.SetValue (va1
[i
], 1);
2323 bool errorThrown
= false;
2325 Array
.Copy (source
, array
, 2);
2326 } catch (ArrayTypeMismatchException
) {
2330 Assert
.IsTrue (errorThrown
, "#M95(" + types
[i
] + ")");
2333 for (int i
= 0; i
< types
.Length
; i
++) {
2334 String
[] source
= new String
[2];
2335 Array array
= Array
.CreateInstance (types
[i
], 2);
2337 source
.SetValue (va2
[i
], 0);
2338 source
.SetValue (va2
[i
], 1);
2340 bool errorThrown
= false;
2342 Array
.Copy (source
, array
, 2);
2343 } catch (ArrayTypeMismatchException
) {
2347 Assert
.IsTrue (errorThrown
, "#M96(" + types
[i
] + ")");
2353 public void TestSort() {
2355 bool errorThrown
= false;
2358 } catch (ArgumentNullException
) {
2361 Assert
.IsTrue (errorThrown
, "#N01");
2364 bool errorThrown
= false;
2366 Array
.Sort(null, 0, 1);
2367 } catch (ArgumentNullException
) {
2370 Assert
.IsTrue (errorThrown
, "#N02");
2373 bool errorThrown
= false;
2375 char[] c1
= new Char
[2];
2376 Array
.Sort(null, c1
);
2377 } catch (ArgumentNullException
) {
2380 Assert
.IsTrue (errorThrown
, "#N03");
2383 bool errorThrown
= false;
2385 char[] c1
= new Char
[2];
2386 Array
.Sort(null, c1
, 0, 1);
2387 } catch (ArgumentNullException
) {
2390 Assert
.IsTrue (errorThrown
, "#N04");
2394 char[] arr
= {'d', 'b', 'f', 'e', 'a', 'c'}
;
2397 Array
.Sort (null, 0, 1);
2398 Assert
.Fail ("#N" + tc
.ToString ());
2400 catch (ArgumentException
) {}
2401 catch (Exception
) { Assert.Fail ("#N" + tc.ToString ()); }
2405 Array
.Sort (arr
, -1, 3);
2406 Assert
.Fail ("#N" + tc
.ToString ());
2408 catch (ArgumentException
) {}
2409 catch (Exception
) { Assert.Fail ("#N" + tc.ToString ()); }
2413 Array
.Sort (arr
, 1, -3);
2414 Assert
.Fail ("#N" + tc
.ToString ());
2416 catch (ArgumentException
) {}
2417 catch (Exception
) { Assert.Fail ("#N" + tc.ToString ()); }
2421 Array
.Sort (arr
, arr
.Length
, arr
.Length
+ 2);
2422 Assert
.Fail ("#N" + tc
.ToString ());
2424 catch (ArgumentException
) {}
2425 catch (Exception
) { Assert.Fail ("#N" + tc.ToString ()); }
2428 // note: null second array => just sort first array
2429 char[] starter
= {'d', 'b', 'f', 'e', 'a', 'c'}
;
2430 int[] starter1
= {1,2,3,4,5,6}
;
2432 char[] c1
= (char[])starter
.Clone();
2434 Assert
.AreEqual ('a', c1
[0], "#N21");
2435 Assert
.AreEqual ('b', c1
[1], "#N22");
2436 Assert
.AreEqual ('c', c1
[2], "#N23");
2437 Assert
.AreEqual ('d', c1
[3], "#N24");
2438 Assert
.AreEqual ('e', c1
[4], "#N25");
2439 Assert
.AreEqual ('f', c1
[5], "#N26");
2442 char[] c1
= (char[])starter
.Clone();
2443 int[] i1
= (int[])starter1
.Clone();
2445 Assert
.AreEqual ('a', c1
[0], "#N41");
2446 Assert
.AreEqual ('b', c1
[1], "#N42");
2447 Assert
.AreEqual ('c', c1
[2], "#N43");
2448 Assert
.AreEqual ('d', c1
[3], "#N44");
2449 Assert
.AreEqual ('e', c1
[4], "#N45");
2450 Assert
.AreEqual ('f', c1
[5], "#N46");
2451 Assert
.AreEqual (5, i1
[0], "#N47");
2452 Assert
.AreEqual (2, i1
[1], "#N48");
2453 Assert
.AreEqual (6, i1
[2], "#N49");
2454 Assert
.AreEqual (1, i1
[3], "#N50");
2455 Assert
.AreEqual (4, i1
[4], "#N51");
2456 Assert
.AreEqual (3, i1
[5], "#N52");
2459 char[] c1
= (char[])starter
.Clone();
2460 Array
.Sort(c1
, 1, 4);
2461 Assert
.AreEqual ('d', c1
[0], "#N61");
2462 Assert
.AreEqual ('a', c1
[1], "#N62");
2463 Assert
.AreEqual ('b', c1
[2], "#N63");
2464 Assert
.AreEqual ('e', c1
[3], "#N64");
2465 Assert
.AreEqual ('f', c1
[4], "#N65");
2466 Assert
.AreEqual ('c', c1
[5], "#N66");
2469 char[] c1
= (char[])starter
.Clone();
2470 int[] i1
= (int[])starter1
.Clone();
2471 Array
.Sort(c1
, i1
, 1, 4);
2472 Assert
.AreEqual ('d', c1
[0], "#N81");
2473 Assert
.AreEqual ('a', c1
[1], "#N82");
2474 Assert
.AreEqual ('b', c1
[2], "#N83");
2475 Assert
.AreEqual ('e', c1
[3], "#N84");
2476 Assert
.AreEqual ('f', c1
[4], "#N85");
2477 Assert
.AreEqual ('c', c1
[5], "#N86");
2478 Assert
.AreEqual (1, i1
[0], "#N87");
2479 Assert
.AreEqual (5, i1
[1], "#N88");
2480 Assert
.AreEqual (2, i1
[2], "#N89");
2481 Assert
.AreEqual (4, i1
[3], "#N90");
2482 Assert
.AreEqual (3, i1
[4], "#N91");
2483 Assert
.AreEqual (6, i1
[5], "#N92");
2488 double[] a
= new double[115];
2489 int[] b
= new int[256];
2490 Array
.Sort
<double, int> (a
, b
, 0, 115);
2493 /* Check that ulong[] is not sorted as long[] */
2495 string[] names
= new string[] {
2496 "A", "B", "C", "D", "E"
2499 ulong[] arr
= new ulong [] {
2501 unchecked((ulong)0xffffFFFF00000000),
2508 Array
.Sort (a
, names
, null);
2509 Assert
.AreEqual (0, a
.GetValue (0));
2514 public void Sort_NullValues ()
2516 var s
= new [] { "a", null, "b", null }
;
2517 Array
.Sort (s
, (a
, b
) => {
2519 return b
== null ? 0 : 1;
2525 return a
.CompareTo (b
);
2528 Assert
.AreEqual ("a", s
[0], "#1");
2529 Assert
.AreEqual ("b", s
[1], "#2");
2530 Assert
.IsNull (s
[2], "#3");
2531 Assert
.IsNull (s
[3], "#4");
2535 public void SortNonGenericDoubleItems () {
2536 double[] doubleValues
= new double[11];
2538 doubleValues
[0] = 0.221788066253601;
2539 doubleValues
[1] = 0.497278285809481;
2540 doubleValues
[2] = 0.100565033883643;
2541 doubleValues
[3] = 0.0433309347749905;
2542 doubleValues
[4] = 0.00476726438463812;
2543 doubleValues
[5] = 0.1354609735456;
2544 doubleValues
[6] = 0.57690356588135;
2545 doubleValues
[7] = 0.466239434334826;
2546 doubleValues
[8] = 0.409741461978934;
2547 doubleValues
[9] = 0.0112412763949565;
2548 doubleValues
[10] = 0.668704347674307;
2550 int[] indices
= new int[11];
2563 Array
.Sort ((Array
)doubleValues
, (Array
)indices
);
2564 Assert
.AreEqual (4, indices
[0]);
2568 public void TestSortComparable()
2570 int[] source
= { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
;
2571 int[] expected
= { 6, 5, 4, 3, 2, 1, 7, 8, 9 }
;
2572 Comp
[] c
= { new Comp (100), new Comp (16), new Comp (11), new Comp (9), new Comp (0), new Comp (-100) }
;
2573 IComparer
<Comp
> comp
= null;
2574 Array
.Sort
<Comp
, int> (c
, source
, comp
);
2576 Assert
.AreEqual (expected
, source
);
2579 class Comp
: IComparable
2588 int IComparable
.CompareTo (object obj
)
2590 return val
.CompareTo ((obj
as Comp
).val
);
2595 public void TestSortComparableMixed()
2597 var m
= new TestSortComparableMixed_Comparer ();
2598 var arr
= new object [] { 1, 2, m, 4, 5, 6, 7, 8, 9, 10 }
;
2602 var expected
= new object [] { m, 1, 2, 4, 5, 6, 7, 8, 9, 10 }
;
2603 Assert
.AreEqual (expected
, arr
);
2606 class TestSortComparableMixed_Comparer
: IComparable
2608 public int CompareTo (object other
)
2615 public void TestInitializeEmpty()
2627 Assert
.IsTrue (!catched
, "#TI01");
2631 public void TestInitializeInt()
2636 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2638 Assert
.AreEqual (a
[i
], b
[i
], "#TI02 " + i
);
2643 public void TestInitializeDouble()
2645 double[] a
= {1.0,2.0,0.0}
;
2647 double[] b
= {1.0,2.0,0.0}
;
2648 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2650 Assert
.AreEqual (a
[i
], b
[i
], "#TI03 " + i
);
2655 public void TestInitializeFloat()
2657 float[] a
= {1.0F,2.0F,0.0F}
;
2659 float[] b
= {1.0F,2.0F,0.0F}
;
2660 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2662 Assert
.AreEqual (a
[i
], b
[i
], "#TI04 " + i
);
2667 public void TestInitializeChar()
2669 char[] a
= {'1','.','0','F','2','.','0','F'}
;
2671 char[] b
= {'1','.','0','F','2','.','0','F'}
;
2672 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2674 Assert
.AreEqual (a
[i
], b
[i
], "#TI05 " + i
);
2679 public void TestInitializeString()
2681 string[] a
= {"hola","adios","menos","mas"}
;
2683 string[] b
= {"hola","adios","menos","mas"}
;
2684 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2686 Assert
.AreEqual (a
[i
], b
[i
], "#TI06 " + i
);
2691 public void TestInitializeEnum()
2693 enua
[] a
= {enua.hola,enua.adios,enua.menos,enua.mas}
;
2695 enua
[] b
= {enua.hola,enua.adios,enua.menos,enua.mas}
;
2696 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2698 Assert
.AreEqual (a
[i
], b
[i
], "#TI07 " + i
);
2703 public void TestInitializeIntNI()
2705 int[] a
= new int[20];
2709 Assert
.AreEqual (b
, 0, "#TI08");
2714 public void TestInitializeCharNI()
2716 char[] a
= new char[20];
2718 foreach(char b
in a
)
2720 Assert
.AreEqual (b
, 0, "#TI09");
2725 public void TestInitializeDoubleNI()
2727 double[] a
= new double[20];
2729 foreach(double b
in a
)
2731 Assert
.AreEqual (b
, 0.0, "#TI09");
2736 public void TestInitializeStringNI()
2738 string[] a
= new string[20];
2740 foreach(string b
in a
)
2742 Assert
.AreEqual (b
, null, "#TI10");
2747 public void TestInitializeObjectNI()
2749 object[] a
= new object[20];
2751 foreach(object b
in a
)
2753 Assert
.AreEqual (b
, null, "#TI11");
2758 public void TestInitializeAClassNI()
2760 AClass
[] a
= new AClass
[20];
2762 foreach(AClass b
in a
)
2764 Assert
.AreEqual (b
, null, "#TI12");
2770 public void TestInitializeAStructNI()
2772 AStruct
[] a
= new AStruct
[20];
2774 foreach(AStruct b
in a
)
2776 Assert
.AreEqual (b
, new AStruct(), "#TI14");
2781 public void TestInitializeAStruct()
2783 AStruct
[] a
= new AStruct
[3];
2787 AStruct
[] b
= new AStruct
[3];
2790 for(int i
=a
.GetLowerBound(0);i
<=a
.GetUpperBound(0);i
++)
2792 Assert
.AreEqual (a
[i
], b
[i
], "#TI15 " + i
);
2797 public void TestInitializeDateTimeNI()
2799 DateTime
[] a
= new DateTime
[20];
2801 foreach(DateTime b
in a
)
2803 Assert
.AreEqual (b
, new DateTime(), "#TI16");
2808 [ExpectedException (typeof (ArgumentNullException
))]
2809 public void MoreSort1 ()
2811 Array
.Sort (null, 0, 1);
2815 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
2816 public void MoreSort2 ()
2818 Array
.Sort (arrsort
, -1, 3);
2822 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
2823 public void MoreSort3 ()
2825 Array
.Sort (arrsort
, 1, -3);
2829 [ExpectedException (typeof (ArgumentException
))]
2830 public void MoreSort4 ()
2832 Array
.Sort (arrsort
, arrsort
.Length
, arrsort
.Length
+ 2);
2836 [ExpectedException (typeof (RankException
))]
2837 public void MoreSort5 ()
2839 char [,] arr
= new char [,] {{'a'}
, {'b'}}
;
2840 Array
.Sort (arr
, 0, 1);
2844 public void MoreSort6 ()
2846 Array
.Sort (arrsort
, 0, 0);
2850 [ExpectedException (typeof (ArgumentException
))]
2851 public void MoreSort7 ()
2853 Array
.Sort (arrsort
, arrsort
.Length
- 1, 2);
2857 [ExpectedException (typeof (ArgumentException
))]
2858 public void MoreSort8 ()
2860 Array
.Sort (arrsort
, 0, arrsort
.Length
+ 1);
2864 public void MoreSort9 ()
2866 Array
.Sort (arrsort
, null, 0, arrsort
.Length
, null);
2870 [ExpectedException (typeof (InvalidOperationException
))]
2871 public void MoreSort10 ()
2873 object [] array
= {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"}
;
2874 Array
.Sort (array
, (IComparer
) null);
2877 [Test
] // bug #81941
2880 double [] a
= new double [2] { 0.9, 0.3 }
;
2881 uint [] b
= new uint [2] { 4, 7 }
;
2883 Assert
.AreEqual (0.3, a
[0], "#1");
2884 Assert
.AreEqual (0.9, a
[1], "#2");
2885 Assert
.AreEqual (7, b
[0], "#3");
2886 Assert
.AreEqual (4, b
[1], "#4");
2890 public void ClearJaggedArray ()
2892 byte[][] matrix
= new byte [8][];
2893 for (int i
=0; i
< 8; i
++) {
2894 matrix
[i
] = new byte [8];
2895 for (int j
=0; j
< 8; j
++) {
2899 Array
.Clear (matrix
, 0, 8);
2900 for (int i
=0; i
< 8; i
++) {
2901 Assert
.IsNull (matrix
[i
], i
.ToString ());
2906 public void ClearMultidimentionalArray ()
2908 byte[,] matrix
= new byte [2,2] { {1, 1}
, {2, 2}
};
2909 Array
.Clear (matrix
, 0, 2);
2910 Assert
.AreEqual (0, matrix
[0, 0], "0,0");
2911 Assert
.AreEqual (0, matrix
[0, 1], "0,1");
2912 Assert
.AreEqual (2, matrix
[1, 0], "1,0");
2913 Assert
.AreEqual (2, matrix
[1, 1], "1,1");
2917 [ExpectedException (typeof (IndexOutOfRangeException
))]
2918 public void ClearOutsideMultidimentionalArray ()
2920 byte[,] matrix
= new byte [2,2] { {1, 1}
, {2, 2}
};
2921 Array
.Clear (matrix
, 0, 5);
2925 [ExpectedException (typeof (IndexOutOfRangeException
))]
2926 public void Clear_IndexOverflow ()
2928 byte[] array
= new byte [16];
2929 Array
.Clear (array
, 4, Int32
.MaxValue
);
2933 [ExpectedException (typeof (IndexOutOfRangeException
))]
2934 public void Clear_LengthOverflow ()
2936 byte[] array
= new byte [16];
2937 Array
.Clear (array
, Int32
.MaxValue
, 4);
2941 [ExpectedException (typeof (ArgumentException
))]
2942 public void Copy_SourceIndexOverflow ()
2944 byte[] array
= new byte [16];
2945 Array
.Copy (array
, Int32
.MaxValue
, array
, 8, 8);
2949 [ExpectedException (typeof (ArgumentException
))]
2950 public void Copy_DestinationIndexOverflow ()
2952 byte[] array
= new byte [16];
2953 Array
.Copy (array
, 8, array
, Int32
.MaxValue
, 8);
2957 [ExpectedException (typeof (ArgumentException
))]
2958 public void Copy_LengthOverflow ()
2960 byte[] array
= new byte [16];
2961 Array
.Copy (array
, 8, array
, 8, Int32
.MaxValue
);
2965 [ExpectedException (typeof (ArgumentException
))]
2966 public void Reverse_IndexOverflow ()
2968 byte[] array
= new byte [16];
2969 Array
.Reverse (array
, Int32
.MaxValue
, 8);
2973 [ExpectedException (typeof (ArgumentException
))]
2974 public void Reverse_LengthOverflow ()
2976 byte[] array
= new byte [16];
2977 Array
.Reverse (array
, 8, Int32
.MaxValue
);
2980 public struct CharX
: IComparable
{
2983 public CharX (char c
)
2988 public int CompareTo (object obj
)
2991 return c
.CompareTo (((CharX
) obj
).c
);
2993 return c
.CompareTo (obj
);
2998 public void BinarySearch_ArgPassingOrder ()
3001 // This tests that arguments are passed to the comprer in the correct
3002 // order. The IComparable of the *array* elements must get called, not
3003 // that of the search object.
3005 CharX
[] x
= { new CharX ('a'), new CharX ('b'), new CharX ('c') }
;
3006 Assert
.AreEqual (1, Array
.BinarySearch (x
, 'b'));
3009 class Comparer
: IComparer
{
3011 private bool called
= false;
3013 public bool Called
{
3015 bool result
= called
;
3021 public int Compare (object x
, object y
)
3029 public void BinarySearch1_EmptyList ()
3031 int[] array
= new int[0];
3032 Assert
.AreEqual (- 1, Array
.BinarySearch (array
, 0), "BinarySearch");
3036 public void BinarySearch2_EmptyList ()
3038 int[] array
= new int[0];
3039 Assert
.AreEqual (-1, Array
.BinarySearch (array
, 0, 0, 0), "BinarySearch");
3043 public void BinarySearch3_EmptyList ()
3045 Comparer comparer
= new Comparer ();
3046 int[] array
= new int[0];
3047 Assert
.AreEqual (-1, Array
.BinarySearch (array
, 0, comparer
), "BinarySearch");
3048 // bug 77030 - the comparer isn't called for an empty array/list
3049 Assert
.IsTrue (!comparer
.Called
, "Called");
3053 public void BinarySearch4_EmptyList ()
3055 Comparer comparer
= new Comparer ();
3056 int[] array
= new int[0];
3057 Assert
.AreEqual (-1, Array
.BinarySearch (array
, 0, 0, comparer
), "BinarySearch");
3058 // bug 77030 - the comparer isn't called for an empty array/list
3059 Assert
.IsTrue (!comparer
.Called
, "Called");
3063 [ExpectedException (typeof (ArgumentNullException
))]
3064 public void AsReadOnly_NullArray ()
3066 Array
.AsReadOnly
<int> (null);
3070 public void ReadOnly_Count ()
3072 Assert
.AreEqual (10, Array
.AsReadOnly (new int [10]).Count
);
3076 public void ReadOnly_Contains ()
3078 int[] arr
= new int [2];
3081 IList
<int> a
= Array
.AsReadOnly (arr
);
3083 Assert
.IsTrue (a
.Contains (3));
3084 Assert
.IsTrue (!a
.Contains (6));
3088 public void ReadOnly_IndexOf ()
3090 int[] arr
= new int [2];
3093 IList
<int> a
= Array
.AsReadOnly (arr
);
3095 Assert
.AreEqual (0, a
.IndexOf (3));
3096 Assert
.AreEqual (1, a
.IndexOf (5));
3097 Assert
.AreEqual (-1, a
.IndexOf (6));
3101 public void ReadOnly_Indexer ()
3103 int[] arr
= new int [2];
3106 IList
<int> a
= Array
.AsReadOnly (arr
);
3108 Assert
.AreEqual (3, a
[0]);
3109 Assert
.AreEqual (5, a
[1]);
3111 /* Check that modifications to the original array are visible */
3113 Assert
.AreEqual (6, a
[0]);
3117 public void ReadOnly_Enumerator ()
3119 int[] arr
= new int [10];
3121 for (int i
= 0; i
< 10; ++i
)
3125 foreach (int i
in Array
.AsReadOnly (arr
))
3128 Assert
.AreEqual (45, sum
);
3132 public void ReadOnly_CopyTo ()
3134 int[] arr
= new int [2];
3137 IList
<int> a
= Array
.AsReadOnly (arr
);
3139 int[] arr2
= new int [3];
3142 Assert
.AreEqual (0, arr2
[0]);
3143 Assert
.AreEqual (3, arr2
[1]);
3144 Assert
.AreEqual (5, arr2
[2]);
3148 public void Resize ()
3150 int [] arr
= new int [] { 1, 3, 5 }
;
3151 Array
.Resize
<int> (ref arr
, 3);
3152 Assert
.AreEqual (3, arr
.Length
, "#A1");
3153 Assert
.AreEqual (1, arr
[0], "#A2");
3154 Assert
.AreEqual (3, arr
[1], "#A3");
3155 Assert
.AreEqual (5, arr
[2], "#A4");
3157 Array
.Resize
<int> (ref arr
, 2);
3158 Assert
.AreEqual (2, arr
.Length
, "#B1");
3159 Assert
.AreEqual (1, arr
[0], "#B2");
3160 Assert
.AreEqual (3, arr
[1], "#B3");
3162 Array
.Resize
<int> (ref arr
, 4);
3163 Assert
.AreEqual (4, arr
.Length
, "#C1");
3164 Assert
.AreEqual (1, arr
[0], "#C2");
3165 Assert
.AreEqual (3, arr
[1], "#C3");
3166 Assert
.AreEqual (0, arr
[2], "#C4");
3167 Assert
.AreEqual (0, arr
[3], "#C5");
3171 public void Resize_null ()
3174 Array
.Resize (ref arr
, 10);
3175 Assert
.AreEqual (arr
.Length
, 10);
3179 public void Test_ContainsAndIndexOf_EquatableItem ()
3181 EquatableClass
[] list
= new EquatableClass
[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)}
;
3183 Assert
.AreEqual (0, Array
.IndexOf
<EquatableClass
> (list
, list
[0]), "#0");
3184 Assert
.AreEqual (0, Array
.IndexOf
<EquatableClass
> (list
, new EquatableClass (0)), "#1");
3185 Assert
.AreEqual (2, Array
.LastIndexOf
<EquatableClass
> (list
, list
[0]), "#2");
3186 Assert
.AreEqual (2, Array
.LastIndexOf
<EquatableClass
> (list
, new EquatableClass (0)), "#3");
3189 public class EquatableClass
: IEquatable
<EquatableClass
>
3192 public EquatableClass (int x
)
3197 public bool Equals (EquatableClass other
)
3199 return this._x
== other
._x
;
3204 public void AsIList ()
3206 IList
<int> arr
= new int [10];
3208 Assert
.AreEqual (5, arr
[0]);
3210 IList
<FooStruct
> arr2
= new FooStruct
[10];
3211 FooStruct s
= new FooStruct ();
3216 Assert
.AreEqual (11, s
.i
);
3217 Assert
.AreEqual (22, s
.j
);
3219 IList
<string> arr3
= new string [10];
3221 Assert
.AreEqual ("ABC", arr3
[5]);
3230 public void ICollectionNull ()
3232 ICollection
<object> test
;
3234 test
= new List
<object>();
3235 Assert
.AreEqual (test
.Contains (null), false, "list<o>");
3237 test
= new object[] {};
3238 Assert
.AreEqual (test
.Contains (null), false, "empty array");
3240 test
= new object[] {null}
;
3241 Assert
.AreEqual (test
.Contains (null), true, "array with null");
3243 test
= new object[] { 1, null}
;
3244 Assert
.IsTrue (test
.Contains (null), "array with last null");
3246 test
= new List
<object>(test
);
3247 Assert
.AreEqual (test
.Contains (null), true, "List<object> with test");
3249 test
= new object[] {new object()}
;
3250 Assert
.AreEqual (test
.Contains (null), false, "array with object");
3252 test
= new List
<object>(test
);
3253 Assert
.AreEqual (test
.Contains (null), false, "array with test");
3257 public void IListNull ()
3261 test
= new List
<object>();
3262 Assert
.AreEqual (-1, test
.IndexOf (null), "list<o>");
3264 test
= new object[] {};
3265 Assert
.AreEqual (-1, test
.IndexOf (null), "empty array");
3267 test
= new object[] {null}
;
3268 Assert
.AreEqual (0, test
.IndexOf (null), "array with null");
3270 test
= new object[] { 1, null}
;
3271 Assert
.AreEqual (1, test
.IndexOf (null), "array with last null");
3273 test
= new List
<object>(test
);
3274 Assert
.AreEqual (1, test
.IndexOf (null), "List<object> with test");
3276 test
= new object[] {new object()}
;
3277 Assert
.AreEqual (-1, test
.IndexOf (null), "array with object");
3279 test
= new List
<object>(test
);
3280 Assert
.AreEqual (-1, test
.IndexOf (null), "array with test");
3286 enum ByteEnum
: byte {}
3287 enum IntEnum
: int {}
3290 public void TestByteEnumArrayToByteArray ()
3292 ByteEnum
[] a
= new ByteEnum
[] {(ByteEnum) 1, (ByteEnum) 2}
;
3293 byte[] b
= new byte[a
.Length
];
3298 public void TestByteEnumArrayToIntArray ()
3300 ByteEnum
[] a
= new ByteEnum
[] {(ByteEnum) 1, (ByteEnum) 2}
;
3301 int[] b
= new int[a
.Length
];
3306 [ExpectedException (typeof (ArrayTypeMismatchException
))]
3307 public void TestIntEnumArrayToByteArray ()
3309 IntEnum
[] a
= new IntEnum
[] {(IntEnum) 1, (IntEnum) 2}
;
3310 byte[] b
= new byte[a
.Length
];
3315 public void TestIntEnumArrayToIntArray ()
3317 IntEnum
[] a
= new IntEnum
[] {(IntEnum) 1, (IntEnum) 2}
;
3318 int[] b
= new int[a
.Length
];
3324 [Test
] // bug #322248
3325 public void IEnumerator_Reset ()
3327 int[] array
= new int[] { 1, 2, 3}
;
3328 IEnumerator
<int> e
= ((IEnumerable
<int>)array
).GetEnumerator ();
3329 Assert
.IsTrue (e
.MoveNext (), "#A1");
3330 Assert
.AreEqual (1, e
.Current
, "#A2");
3331 Assert
.IsTrue (e
.MoveNext (), "#A3");
3332 Assert
.AreEqual (2, e
.Current
, "#A4");
3336 Assert
.IsTrue (e
.MoveNext (), "#C1");
3337 Assert
.AreEqual (1, e
.Current
, "#C2");
3341 public void IEnumerator_Current_Finished ()
3343 int[] array
= new int[] { 1, 2, 3 }
;
3344 IEnumerator
<int> e
= ((IEnumerable
<int>)array
).GetEnumerator ();
3345 Assert
.IsTrue (e
.MoveNext (), "#A1");
3346 Assert
.AreEqual (1, e
.Current
, "#A2");
3347 Assert
.IsTrue (e
.MoveNext (), "#A3");
3348 Assert
.AreEqual (2, e
.Current
, "#A4");
3349 Assert
.IsTrue (e
.MoveNext (), "#A5");
3350 Assert
.AreEqual (3, e
.Current
, "#A6");
3351 Assert
.IsTrue (!e
.MoveNext (), "#A6");
3354 Assert
.Fail ("#B1:" + e
.Current
);
3355 } catch (InvalidOperationException ex
) {
3356 // Enumeration already finished
3357 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#B2");
3358 Assert
.IsNull (ex
.InnerException
, "#B3");
3359 Assert
.IsNotNull (ex
.Message
, "#B4");
3364 public void IEnumerator_Current_NotStarted ()
3366 int[] array
= new int[] { 1, 2, 3 }
;
3367 IEnumerator
<int> e
= ((IEnumerable
<int>)array
).GetEnumerator ();
3370 Assert
.Fail ("#A1:" + e
.Current
);
3371 } catch (InvalidOperationException ex
) {
3372 // Enumeration has not started. Call MoveNext
3373 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#A2");
3374 Assert
.IsNull (ex
.InnerException
, "#A3");
3375 Assert
.IsNotNull (ex
.Message
, "#A4");
3380 public void IEnumerator_Current_Reset ()
3382 int[] array
= new int[] { 1, 2, 3 }
;
3383 IEnumerator
<int> e
= ((IEnumerable
<int>)array
).GetEnumerator ();
3388 Assert
.Fail ("#B1:" + e
.Current
);
3389 } catch (InvalidOperationException ex
) {
3390 // Enumeration has not started. Call MoveNext
3391 Assert
.AreEqual (typeof (InvalidOperationException
), ex
.GetType (), "#B2");
3392 Assert
.IsNull (ex
.InnerException
, "#B3");
3393 Assert
.IsNotNull (ex
.Message
, "#B4");
3398 public void IEnumerator_Dispose ()
3400 IEnumerable
<int> e
= new int[] { 1 }
;
3401 var en
= e
.GetEnumerator ();
3402 Assert
.IsTrue (en
.MoveNext (), "#1");
3403 Assert
.IsFalse (en
.MoveNext (), "#2");
3405 Assert
.IsFalse (en
.MoveNext (), "#3");
3409 public void IEnumerator_ZeroSize ()
3411 IEnumerable
<int> e
= Array
.Empty
<int> ();
3412 var en
= e
.GetEnumerator ();
3413 Assert
.IsFalse (en
.MoveNext (), "#1");
3415 e
= Array
.Empty
<int> ();
3416 en
= e
.GetEnumerator ();
3417 Assert
.IsFalse (en
.MoveNext (), "#2");
3421 public void ICollection_IsReadOnly() {
3422 ICollection
<string> arr
= new string [10];
3424 Assert
.IsTrue (arr
.IsReadOnly
);
3428 [ExpectedException (typeof (NotSupportedException
))]
3429 public void ArrayCreateInstanceOfVoid ()
3431 Array
.CreateInstance (typeof (void), 42);
3437 [ExpectedException (typeof (NotSupportedException
))]
3438 public void ArrayCreateInstanceOfOpenGenericType ()
3440 Array
.CreateInstance (typeof (Foo
<>), 42);
3444 [ExpectedException (typeof (IndexOutOfRangeException
))]
3445 public void ClearNegativeLength ()
3447 Array
.Clear (new int [] { 1, 2 }
, 0, -1);
3451 [ExpectedException (typeof (ArgumentException
))]
3452 public void MultiDimension_IList_setItem ()
3454 IList array
= new int [1, 1];
3459 [ExpectedException (typeof (ArgumentException
))]
3460 public void MultiDimension_IList_getItem ()
3462 IList array
= new int [1, 1];
3463 int a
= (int) array
[0];
3467 public void SetValue_Nullable () {
3468 Array array
= Array
.CreateInstance (typeof (int?), 7);
3472 array
.SetValue (o
, 0);
3473 Assert
.AreEqual (42, array
.GetValue (0));
3475 array
.SetValue (null, 0);
3476 Assert
.AreEqual (null, array
.GetValue (0));
3480 public void SortNullsWithGenericVersion ()
3482 string[] s1
= new string[6]{
3490 string[] s2
= new string[]{null,
3497 Array
.Sort
<string> (s1
);
3498 for (int i
= 0; i
< 6; i
++) {
3499 Assert
.AreEqual (s1
[i
], s2
[i
], "At:" + i
);
3504 // This is a test case for the case that was broken by the code contributed
3507 // This tests the fix for: #622101
3510 public void SortActuallyWorks ()
3512 string[] data
= new string[9]{"Foo", "Bar", "Dingus", null, "Dingu4", "123", "Iam", null, "NotNull"}
;
3513 IComparer comparer
= new NullAtEndComparer ();
3514 Array
.Sort (data
, comparer
);
3516 Assert
.AreEqual (data
[7], null);
3517 Assert
.AreNotEqual (data
[0], null);
3520 class NullAtEndComparer
: IComparer
{
3521 public int Compare(object x
, object y
)
3523 if (x
== null) return 1;
3524 if (y
== null) return -1;
3525 return ((string)x
).CompareTo((string)y
);
3530 public void UnalignedArrayClear ()
3532 byte[] input
= new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
;
3533 byte[] expected
= new byte[] { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
;
3534 Array
.Clear (input
, 5, 11);
3536 Assert
.AreEqual (input
, expected
);
3540 [ExpectedException (typeof (ArgumentException
))]
3541 public void CompareToWithJaggedArray () {
3542 IStructuralComparable a
= new int[][] { new int [] { 1,2 }
, new int [] { 3,4 }}
;
3543 IStructuralComparable b
= new int[][] { new int [] { 1,2 }
, new int [] { 3,4 }}
;
3544 a
.CompareTo (b
, Comparer
<object>.Default
);
3548 [ExpectedException (typeof (ArgumentException
))]
3549 public void CompareToWithArrayOfTheWrongKind () {
3550 IStructuralComparable a
= new int[] { 1, 2 }
;
3551 IStructuralComparable b
= new double[] { 1, 2 }
;
3552 a
.CompareTo (b
, Comparer
<object>.Default
);
3556 [ExpectedException (typeof (ArgumentException
))]
3557 public void CompareToWithNonArrayType () {
3558 IStructuralComparable a
= new int[] { 1, 2 }
;
3559 a
.CompareTo (99, Comparer
<object>.Default
);
3563 [ExpectedException (typeof (ArgumentException
))]
3564 public void CompareToWithNonArrayOfDifferentSize () {
3565 IStructuralComparable a
= new int[] { 1, 2 }
;
3566 IStructuralComparable b
= new int[] { 1, 2, 3 }
;
3567 a
.CompareTo (b
, Comparer
<object>.Default
);
3571 [ExpectedException (typeof (ArgumentException
))]
3572 public void CompareToWithMultiDimArray1 () {
3573 IStructuralComparable a
= new int [2,2] { {10, 10 }
, { 10, 10 }
};
3574 IStructuralComparable b
= new int [2,2] { {10, 10 }
, { 10, 10 }
};
3575 a
.CompareTo (b
, Comparer
<object>.Default
);
3579 [ExpectedException (typeof (ArgumentException
))]
3580 public void CompareToWithMultiDimArray2 () {
3581 IStructuralComparable a
= new int [2] { 10, 10 }
;
3582 IStructuralComparable b
= new int [2,2] { {10, 10 }
, { 10, 10 }
};
3583 a
.CompareTo (b
, Comparer
<object>.Default
);
3587 [ExpectedException (typeof (ArgumentException
))]
3588 public void CompareToWithMultiDimArray3 () {
3589 IStructuralComparable a
= new int [4] { 10, 10, 10, 10 }
;
3590 IStructuralComparable b
= new int [2,2] { {10, 10 }
, { 10, 10 }
};
3591 a
.CompareTo (b
, Comparer
<object>.Default
);
3595 [ExpectedException (typeof (IndexOutOfRangeException
))]
3596 public void CompareToWithBoundedArray1 () {
3597 IStructuralComparable a
= new int [2] { 10, 10 }
;
3598 Array ab
= Array
.CreateInstance (typeof (int), new int[] { 2 }
, new int [] { 5 }
);
3599 IStructuralComparable b
= ab
;
3600 ab
.SetValue (10, 5);
3601 ab
.SetValue (10, 6);
3603 a
.CompareTo (b
, Comparer
<object>.Default
);
3607 [ExpectedException (typeof (IndexOutOfRangeException
))]
3608 public void CompareToWithBoundedArray2 () {
3609 IStructuralComparable a
= new int [2] { 10, 10 }
;
3610 Array ab
= Array
.CreateInstance (typeof (int), new int[] { 2 }
, new int [] { 5 }
);
3611 IStructuralComparable b
= ab
;
3612 ab
.SetValue (10, 5);
3613 ab
.SetValue (10, 6);
3615 //Yes, CompareTo simply doesn't work with bounded arrays!
3616 b
.CompareTo (b
, Comparer
<object>.Default
);
3620 [ExpectedException (typeof (NullReferenceException
))]
3621 public void CompareToWithNullComparer () {
3622 IStructuralComparable a
= new int[] { 1, 2 }
;
3623 IStructuralComparable b
= new int[] { 1, 2 }
;
3624 a
.CompareTo (b
, null);
3628 public void CompareToWithNullArray () {
3629 IStructuralComparable a
= new int[] { 1, 2 }
;
3630 Assert
.AreEqual (1, a
.CompareTo (null, Comparer
<object>.Default
));
3634 public void CompareToWithGoodArrays () {
3635 IStructuralComparable a
= new int[] { 10, 20 }
;
3636 Assert
.AreEqual (0, a
.CompareTo (a
, Comparer
<object>.Default
));
3637 Assert
.AreEqual (0, a
.CompareTo (new int [] { 10, 20 }
, Comparer
<object>.Default
));
3638 Assert
.AreEqual (-1, a
.CompareTo (new int [] { 11, 20 }
, Comparer
<object>.Default
));
3639 Assert
.AreEqual (-1, a
.CompareTo (new int [] { 10, 21 }
, Comparer
<object>.Default
));
3640 Assert
.AreEqual (1, a
.CompareTo (new int [] { 9, 20 }
, Comparer
<object>.Default
));
3641 Assert
.AreEqual (1, a
.CompareTo (new int [] { 10, 19 }
, Comparer
<object>.Default
));
3645 public void IStructuralEquatable_Equals ()
3647 IStructuralEquatable array
= new int[] {1, 2, 3}
;
3648 IStructuralEquatable array2
= new int[] {1, 2, 3}
;
3649 Assert
.AreEqual (false, array
.Equals (null, null));
3650 Assert
.AreEqual (true, array
.Equals (array
, null));
3651 Assert
.AreEqual (true, array
.Equals (array2
, EqualityComparer
<int>.Default
));
3655 [ExpectedException (typeof (NullReferenceException
))]
3656 public void IStructuralEquatable_Equals_NoComparer ()
3658 IStructuralEquatable array
= new int[] {1, 2, 3}
;
3659 IStructuralComparable array2
= new int[] {1, 2, 3}
;
3660 array
.Equals (array2
, null);
3664 [ExpectedException (typeof (ArgumentException
))]
3665 public void IStructuralEquatable_Equals_ComparerThrows ()
3667 IStructuralEquatable array
= new int[] {1, 2, 3}
;
3668 IStructuralComparable array2
= new int[] {1, 2, 3}
;
3669 array
.Equals (array2
, EqualityComparer
<long>.Default
);
3673 [ExpectedException (typeof (ArgumentNullException
))]
3674 public void IStructuralEquatable_GetHashCode_NullComparer ()
3676 IStructuralEquatable a
= new int[] { 1, 2 }
;
3677 a
.GetHashCode (null);
3680 class TestComparer_GetHashCode
: IEqualityComparer
3684 bool IEqualityComparer
.Equals (object x
, object y
)
3686 throw new NotImplementedException ();
3689 public int GetHashCode (object obj
)
3696 public void IStructuralEquatable_GetHashCode ()
3698 IStructuralEquatable a
= new int[] { 1, 2, 9 }
;
3700 var c
= new TestComparer_GetHashCode ();
3702 Assert
.AreEqual (3, c
.Counter
);
3706 public void EnumeratorsEquality ()
3708 int [] normalBase
= new int [0];
3709 IEnumerable
<int> specialBase
= new int [0];
3711 var firstSpecial
= specialBase
.GetEnumerator ();
3712 var secondSpecial
= specialBase
.GetEnumerator ();
3713 var firstNormal
= normalBase
.GetEnumerator ();
3714 var secondNormal
= normalBase
.GetEnumerator ();
3716 Assert
.IsFalse (object.ReferenceEquals (firstNormal
, secondNormal
));
3717 Assert
.IsTrue (object.ReferenceEquals (firstSpecial
, secondSpecial
));
3721 public void JaggedArrayCtor ()
3723 var type
= Type
.GetType ("System.Object[][]");
3725 ConstructorInfo ctor
= null;
3726 foreach (var c
in type
.GetConstructors ()) {
3727 if (c
.GetParameters ().Length
== 2)
3730 Assert
.IsNotNull (ctor
);
3731 var arr
= (object[])ctor
.Invoke (new object [] { 4, 10 }
);
3732 for (int i
= 0; i
< 4; ++i
) {
3733 Assert
.IsNotNull (arr
[i
]);
3734 Assert
.AreEqual (10, ((object[])arr
[i
]).Length
);
3739 public unsafe void PointerArraysBoxing ()
3741 var x
= new int*[10];
3742 var e
= x
.GetEnumerator ();
3745 Assert
.Throws
<NotSupportedException
> (() => { var _ = e.Current; }
, "#1");
3746 Assert
.Throws
<NotSupportedException
> (() => { var _ = x.GetValue (0); }
, "#2");
3747 Assert
.Throws
<NotSupportedException
> (() => { x.SetValue (0, 0); }
, "#3");
3751 #if MONO_FEATURE_THREAD_ABORT
3756 public J(int i_
) { i = i_; }
3759 struct JComp
: IComparer
<J
>
3761 public int Compare(J x
, J y
)
3764 Thread
.Sleep (Timeout
.Infinite
);
3769 class ArraySortAbortData
{
3770 internal ManualResetEventSlim mre
;
3771 internal bool threw
;
3773 internal ArraySortAbortData () {
3774 mre
= new ManualResetEventSlim ();
3780 public void ArraySortAbort ()
3782 var d
= new ArraySortAbortData();
3783 var t
= new Thread(RunArraySort
);
3789 Assert
.IsFalse (d
.threw
);
3792 public static void RunArraySort(object data
)
3794 var d
= data
as ArraySortAbortData
;
3797 for (int i
= 0; i
< n
; ++i
)
3799 a
[i
] = new J(n
- i
);
3803 Array
.Sort(a
, 0, n
, new JComp());
3804 } catch (InvalidOperationException
) {
3805 // t.Abort in ArraySortAbort should _not_ end up here