update readme (#21797)
[mono-project.git] / mcs / class / corlib / Test / System / ArrayTest.cs
blob3c168093cc1e6c571bd5310bcb59c16a4eef2ead
1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
2 //
3 // David Brandt (bucky@keystreams.com)
4 // Eduardo Garcia (kiwnix@yahoo.es)
5 //
6 // (C) Ximian, Inc. http://www.ximian.com
7 // Copyright (C) 2004 Novell (http://www.novell.com)
8 //
10 using NUnit.Framework;
11 using System;
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
20 //Auxiliary Things
21 enum enua {hola,adios,mas,menos};
23 class AClass
25 public AClass()
31 class BClass : AClass
35 class CClass : AClass
39 struct AStruct
41 public string s;
42 public string a;
45 class DataEqual
47 public override bool Equals (object obj)
49 return true;
52 public override int GetHashCode ()
54 return 0;
58 //End Auxiliary Things
60 [TestFixture]
61 public class ArrayTest
63 char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
65 interface I
69 class C
73 class DC : C
77 class DI : I
81 [Test]
82 public void TestIsFixedSize() {
83 char[] a1 = {'a'};
84 Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
87 [Test]
88 public void TestIsReadOnly() {
89 char[] a1 = {'a'};
90 Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
93 [Test]
94 public void TestIsSynchronized() {
95 char[] a1 = {'a'};
96 Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
99 [Test]
100 public void TestLength() {
102 char[] a1 = { };
103 Assert.AreEqual (0, a1.Length, "Zero length array");
106 char[] a1 = {'c'};
107 Assert.AreEqual (1, a1.Length, "One-length array");
110 char[] a1 = {'c', 'c'};
111 Assert.AreEqual (2, a1.Length, "Two-length array");
115 [Test]
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");
127 [Test]
128 public void TestBinarySearch1() {
129 bool errorThrown = false;
130 try {
131 Array.BinarySearch(null, "blue");
132 } catch (ArgumentNullException) {
133 errorThrown = true;
135 Assert.IsTrue (errorThrown, "#B01");
136 errorThrown = false;
137 try {
138 char[,] c1 = new Char[2,2];
139 Array.BinarySearch(c1, "needle");
140 } catch (RankException) {
141 errorThrown = true;
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");
160 [Test]
161 public void TestBinarySearch2() {
162 bool errorThrown = false;
163 try {
164 Array.BinarySearch(null, 0, 1, "blue");
165 } catch (ArgumentNullException) {
166 errorThrown = true;
168 Assert.IsTrue (errorThrown, "#B20");
169 errorThrown = false;
170 try {
171 char[,] c1 = new Char[2,2];
172 Array.BinarySearch(c1, 0, 1, "needle");
173 } catch (RankException) {
174 errorThrown = true;
176 Assert.IsTrue (errorThrown, "#B21");
177 errorThrown = false;
178 try {
179 char[] c1 = {'a'};
180 Array.BinarySearch(c1, -1, 1, 'a');
181 } catch (ArgumentOutOfRangeException) {
182 errorThrown = true;
184 Assert.IsTrue (errorThrown, "#B22");
185 errorThrown = false;
186 try {
187 char[] c1 = {'a'};
188 Array.BinarySearch(c1, 0, -1, 'a');
189 } catch (ArgumentOutOfRangeException) {
190 errorThrown = true;
192 Assert.IsTrue (errorThrown, "#B23");
193 errorThrown = false;
194 try {
195 char[] c1 = {'a'};
196 Array.BinarySearch(c1, 0, 4, 'a');
197 } catch (ArgumentException) {
198 errorThrown = true;
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++)
222 array[i] = 10;
224 Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
227 [Test]
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)
247 if (y != 7)
248 throw new ApplicationException ();
250 return x.CompareTo (y);
254 [Test]
255 public void BinarySearch_WithComparer ()
257 var a = new int[] { 2, 6, 9 };
258 Assert.AreEqual (-3, Array.BinarySearch (a, 7, new TestComparer7 ()));
261 [Test]
262 public void TestClear() {
263 bool errorThrown = false;
264 try {
265 Array.Clear(null, 0, 1);
266 } catch (ArgumentNullException) {
267 errorThrown = true;
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");
314 [Test]
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");
331 d1[0][0] = 'z';
332 Assert.AreEqual (d1[0], d2[0], "#D07");
335 [Test]
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];
347 IList b = a;
348 try {
349 object c = b [-1];
350 Assert.Fail ("IList.this [-1] should throw");
351 } catch (IndexOutOfRangeException) {
352 // Good
353 } catch (Exception){
354 Assert.Fail ("Should have thrown an IndexOutOfRangeException");
358 [Test]
359 public void TestCopy() {
361 bool errorThrown = false;
362 try {
363 Char[] c1 = {};
364 Array.Copy(c1, null, 1);
365 } catch (ArgumentNullException) {
366 errorThrown = true;
368 Assert.IsTrue (errorThrown, "#E01");
371 bool errorThrown = false;
372 try {
373 Char[] c1 = {};
374 Array.Copy(null, c1, 1);
375 } catch (ArgumentNullException) {
376 errorThrown = true;
378 Assert.IsTrue (errorThrown, "#E02");
381 bool errorThrown = false;
382 try {
383 Char[] c1 = new Char[1];
384 Char[,] c2 = new Char[1,1];
385 Array.Copy(c1, c2, 1);
386 } catch (RankException) {
387 errorThrown = true;
389 Assert.IsTrue (errorThrown, "#E03");
392 bool errorThrown = false;
393 try {
394 Char[] c1 = new Char[1];
395 string[] s1 = new String[1];
396 Array.Copy(c1, s1, 1);
397 } catch (ArrayTypeMismatchException) {
398 errorThrown = true;
400 Assert.IsTrue (errorThrown, "#E04");
403 bool errorThrown = false;
404 try {
405 Char[] c1 = new Char[1];
406 Object[] o1 = new Object[1];
407 o1[0] = "hello";
408 Array.Copy(o1, c1, 1);
409 } catch (InvalidCastException) {
410 errorThrown = true;
412 Assert.IsTrue (errorThrown, "#E05");
415 bool errorThrown = false;
416 try {
417 Char[] c1 = new Char[1];
418 Char[] c2 = new Char[1];
419 Array.Copy(c1, c2, -1);
420 } catch (ArgumentOutOfRangeException) {
421 errorThrown = true;
423 Assert.IsTrue (errorThrown, "#E06");
426 bool errorThrown = false;
427 try {
428 Char[] c1 = new Char[1];
429 Char[] c2 = new Char[2];
430 Array.Copy(c1, c2, 2);
431 } catch (ArgumentException) {
432 errorThrown = true;
434 Assert.IsTrue (errorThrown, "#E07");
437 bool errorThrown = false;
438 try {
439 Char[] c1 = new Char[1];
440 Char[] c2 = new Char[2];
441 Array.Copy(c2, c1, 2);
442 } catch (ArgumentException) {
443 errorThrown = true;
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");
465 [Test]
466 public void TestCopy2() {
468 bool errorThrown = false;
469 try {
470 Char[] c1 = new Char[2];
471 Char[] c2 = new Char[2];
472 Array.Copy(c2, 1, c1, 0, 2);
473 } catch (ArgumentException) {
474 errorThrown = true;
476 Assert.IsTrue (errorThrown, "#E31");
479 bool errorThrown = false;
480 try {
481 Char[] c1 = new Char[2];
482 Char[] c2 = new Char[2];
483 Array.Copy(c2, 0, c1, 1, 2);
484 } catch (ArgumentException) {
485 errorThrown = true;
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");
505 [Test]
506 public void Copy_InvalidCast () {
507 object[] arr1 = new object [10];
508 Type[] arr2 = new Type [10];
509 arr1 [0] = new object ();
511 try {
512 Array.Copy (arr1, 0, arr2, 0, 10);
513 Assert.Fail ("#1");
514 } catch (InvalidCastException) {
517 var arr1_2 = new I [1] { new DI () };
518 var arr2_2 = new C [1] { new DC () };
519 try {
520 Array.Copy (arr2_2, arr1_2, 1);
521 Assert.Fail ("#2");
522 } catch (InvalidCastException) {
525 var arr1_3 = new float [5];
526 var arr2_3 = new object [1];
527 try {
528 Array.Copy (arr2_3, arr1_3, 1);
529 Assert.Fail ("#3");
530 } catch (InvalidCastException) {
534 [Test]
535 public void TestCopyTo() {
537 bool errorThrown = false;
538 try {
539 Char[] c1 = new Char[2];
540 c1.CopyTo(null, 2);
541 } catch (ArgumentNullException) {
542 errorThrown = true;
544 Assert.IsTrue (errorThrown, "#E61");
547 bool errorThrown = false;
548 try {
549 Char[] c1 = new Char[2];
550 Char[,] c2 = new Char[2,2];
551 c1.CopyTo(c2, 2);
552 } catch (ArgumentException) {
553 errorThrown = true;
555 Assert.IsTrue (errorThrown, "#E62");
558 bool errorThrown = false;
559 try {
560 Char[,] c1 = new Char[2,2];
561 Char[] c2 = new Char[2];
562 c1.CopyTo(c2, -1);
563 } catch (RankException) {
564 errorThrown = true;
566 Assert.IsTrue (errorThrown, "#E63");
569 bool errorThrown = false;
570 try {
571 Char[,] c1 = new Char[2,2];
572 Char[] c2 = new Char[2];
573 c1.CopyTo(c2, 2);
574 } catch (RankException) {
575 errorThrown = true;
577 Assert.IsTrue (errorThrown, "#E64");
580 bool errorThrown = false;
581 try {
582 Char[] c1 = new Char[2];
583 Char[] c2 = new Char[2];
584 c1.CopyTo(c2, -1);
585 } catch (ArgumentOutOfRangeException) {
586 errorThrown = true;
588 Assert.IsTrue (errorThrown, "#E65");
591 bool errorThrown = false;
592 try {
593 Char[] c1 = new Char[2];
594 Char[] c2 = new Char[2];
595 c1.CopyTo(c2, 3);
596 } catch (ArgumentException) {
597 errorThrown = true;
599 Assert.IsTrue (errorThrown, "#E66");
602 bool errorThrown = false;
603 try {
604 Char[] c1 = new Char[2];
605 Char[] c2 = new Char[2];
606 c1.CopyTo(c2, 1);
607 } catch (ArgumentException) {
608 errorThrown = true;
610 Assert.IsTrue (errorThrown, "#E67");
614 bool errorThrown = false;
615 try {
616 String[] c1 = new String[2];
617 // TODO: this crashes mono if there are null
618 // values in the array.
619 c1[1] = "hey";
620 c1[0] = "you";
621 Char[] c2 = new Char[2];
622 c2[1] = 'a';
623 c2[0] = 'z';
624 c1.CopyTo(c2, 0);
625 } catch (ArrayTypeMismatchException) {
626 errorThrown = true;
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;
649 try {
650 int[] src = new int [0];
651 int[] dest = new int [0];
652 src.CopyTo (dest, 0);
653 } catch (ArgumentException) {
654 errorThrown = true;
656 Assert.IsTrue (!errorThrown, "#E79");
660 // bug #38812
661 bool errorThrown = false;
662 try {
663 CClass[] src = new CClass [] { new CClass () };
664 BClass[] dest = new BClass [1];
666 src.CopyTo (dest, 0);
668 } catch (ArrayTypeMismatchException) {
669 errorThrown = true;
671 Assert.IsTrue (errorThrown, "#E80");
675 [Test]
676 public void TestCreateInstance() {
678 bool errorThrown = false;
679 try {
680 Array.CreateInstance(null, 12);
681 } catch (ArgumentNullException) {
682 errorThrown = true;
684 Assert.IsTrue (errorThrown, "#F01");
687 bool errorThrown = false;
688 try {
689 Array.CreateInstance(Type.GetType("System.Char"), -3);
690 } catch (ArgumentOutOfRangeException) {
691 errorThrown = true;
693 Assert.IsTrue (errorThrown, "#F02");
696 bool errorThrown = false;
697 try {
698 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
699 } catch (ArgumentNullException) {
700 errorThrown = true;
702 Assert.IsTrue (errorThrown, "#F03a");
706 bool errorThrown = false;
707 try {
708 Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
709 } catch (ArgumentNullException) {
710 errorThrown = true;
712 Assert.IsTrue (errorThrown, "#F03b");
715 bool errorThrown = false;
716 try {
717 Array.CreateInstance(Type.GetType("System.Char"), null, null);
718 } catch (ArgumentNullException) {
719 errorThrown = true;
721 Assert.IsTrue (errorThrown, "#F04");
724 bool errorThrown = false;
725 try {
726 int[] lengths = new int [0];
727 Array.CreateInstance(Type.GetType("System.Char"), lengths);
728 } catch (ArgumentException) {
729 errorThrown = true;
731 Assert.IsTrue (errorThrown, "#F05");
734 bool errorThrown = false;
735 try {
736 int[] lengths = new int [1];
737 int[] bounds = new int [2];
738 Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
739 errorThrown = true;
740 } catch (ArgumentException) {
741 errorThrown = true;
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 ());
777 [Test]
778 [ExpectedException (typeof (ArgumentNullException))]
779 public void TestCreateInstance2 ()
781 Array.CreateInstance (typeof (Int32), (int[])null);
784 [Test]
785 [ExpectedException (typeof (ArgumentNullException))]
786 public void TestCreateInstance2b ()
788 Array.CreateInstance (typeof (Int32), (long[])null);
791 [Test]
792 public void CreateInstanceVoid ()
794 Assert.Throws<NotSupportedException> (delegate () {
795 Array.CreateInstance (typeof (void), 1);
799 [Test]
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");
815 en.Reset ();
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");
825 [Test]
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");
841 en.Reset ();
842 Assert.IsTrue (en.MoveNext (), "#AA11");
843 Assert.AreEqual ("this", en.Current, "#AA12");
845 int[] idxs = {0,1};
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");
852 [Test]
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
873 string lastElement;
874 do {
875 lastElement = (string)en.Current;
876 } while (en.MoveNext());
877 Assert.AreEqual ("47", lastElement, "#AB06");
880 [Test]
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 );
886 try {
887 ((IList)myArray).Add ("can not");
888 Assert.Fail ("IList.Add should throw");
890 catch (NotSupportedException) {
891 return;
893 catch (Exception) {
894 Assert.Fail ("IList.Add threw wrong exception type");
897 Assert.Fail ("IList.Add shouldn't get this far");
900 [Test]
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 );
906 try {
907 ((IList)myArray).Insert (0, "can not");
908 Assert.Fail ("IList.Insert should throw");
910 catch (NotSupportedException) {
911 return;
913 catch (Exception) {
914 Assert.Fail ("IList.Insert threw wrong exception type");
917 Assert.Fail ("IList.Insert shouldn't get this far");
920 [Test]
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 );
926 try {
927 ((IList)myArray).Remove ("can not");
928 Assert.Fail ("IList.Remove should throw");
930 catch (NotSupportedException) {
931 return;
933 catch (Exception) {
934 Assert.Fail ("IList.Remove threw wrong exception type");
937 Assert.Fail ("IList.Remove shouldn't get this far");
940 [Test]
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 );
946 try {
947 ((IList)myArray).RemoveAt (0);
948 Assert.Fail ("IList.RemoveAt should throw");
950 catch (NotSupportedException) {
951 return;
953 catch (Exception) {
954 Assert.Fail ("IList.RemoveAt threw wrong exception type");
957 Assert.Fail ("IList.RemoveAt shouldn't get this far");
960 [Test]
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 );
967 try {
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");
979 return;
982 Assert.Fail ("Should not get here");
985 [Test]
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 );
992 try {
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");
1017 [Test]
1018 public void TestGetLength() {
1020 bool errorThrown = false;
1021 try {
1022 char[] c1 = {'a', 'b', 'c'};
1023 c1.GetLength(-1);
1024 } catch (IndexOutOfRangeException) {
1025 errorThrown = true;
1027 Assert.IsTrue (errorThrown, "#H01");
1030 bool errorThrown = false;
1031 try {
1032 char[] c1 = {'a', 'b', 'c'};
1033 c1.GetLength(1);
1034 } catch (IndexOutOfRangeException) {
1035 errorThrown = true;
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");
1048 [Test]
1049 public void TestGetLowerBound() {
1051 bool errorThrown = false;
1052 try {
1053 char[] c = {'a', 'b', 'c'};
1054 c.GetLowerBound(-1);
1055 } catch (IndexOutOfRangeException) {
1056 errorThrown = true;
1058 Assert.IsTrue (errorThrown, "#H31");
1061 bool errorThrown = false;
1062 try {
1063 char[] c = {'a', 'b', 'c'};
1064 c.GetLowerBound(1);
1065 } catch (IndexOutOfRangeException) {
1066 errorThrown = true;
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");
1079 [Test]
1080 public void TestGetUpperBound() {
1082 bool errorThrown = false;
1083 try {
1084 char[] c = {'a', 'b', 'c'};
1085 c.GetUpperBound(-1);
1086 } catch (IndexOutOfRangeException) {
1087 errorThrown = true;
1089 Assert.IsTrue (errorThrown, "#H61");
1092 bool errorThrown = false;
1093 try {
1094 char[] c = {'a', 'b', 'c'};
1095 c.GetUpperBound(1);
1096 } catch (IndexOutOfRangeException) {
1097 errorThrown = true;
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");
1110 [Test]
1111 public void TestGetValue1() {
1113 bool errorThrown = false;
1114 try {
1115 char[,] c = new Char[2,2];
1116 c.GetValue(1);
1117 } catch (ArgumentException) {
1118 errorThrown = true;
1120 Assert.IsTrue (errorThrown, "#I01");
1123 bool errorThrown = false;
1124 try {
1125 char[] c = {'a', 'b', 'c'};
1126 c.GetValue(-1);
1127 } catch (IndexOutOfRangeException) {
1128 errorThrown = true;
1130 Assert.IsTrue (errorThrown, "#I02");
1133 bool errorThrown = false;
1134 try {
1135 char[] c = {'a', 'b', 'c'};
1136 c.GetValue(4);
1137 } catch (IndexOutOfRangeException) {
1138 errorThrown = true;
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 + ")");
1149 [Test]
1150 public void TestGetValue2() {
1152 bool errorThrown = false;
1153 try {
1154 char[] c = new Char[2];
1155 c.GetValue(1,1);
1156 } catch (ArgumentException) {
1157 errorThrown = true;
1159 Assert.IsTrue (errorThrown, "#I21");
1162 bool errorThrown = false;
1163 try {
1164 char[,] c = new Char[2,2];
1165 c.GetValue(-1, 1);
1166 } catch (IndexOutOfRangeException) {
1167 errorThrown = true;
1169 Assert.IsTrue (errorThrown, "#I22");
1172 bool errorThrown = false;
1173 try {
1174 char[,] c = new Char[2,2];
1175 c.GetValue(4,1);
1176 } catch (IndexOutOfRangeException) {
1177 errorThrown = true;
1179 Assert.IsTrue (errorThrown, "#I23");
1182 char[,] c1 = new Char[4,6];
1183 for (int i = 0; i < 24; i++) {
1184 int first = i / 6;
1185 int second = i % 6;
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 + ")");
1195 [Test]
1196 public void TestGetValue3() {
1198 bool errorThrown = false;
1199 try {
1200 char[] c = new Char[2];
1201 c.GetValue(1,1,1);
1202 } catch (ArgumentException) {
1203 errorThrown = true;
1205 Assert.IsTrue (errorThrown, "#I41");
1208 bool errorThrown = false;
1209 try {
1210 char[,,] c = new Char[2,2,2];
1211 c.GetValue(-1, 1, 1);
1212 } catch (IndexOutOfRangeException) {
1213 errorThrown = true;
1215 Assert.IsTrue (errorThrown, "#I42");
1218 bool errorThrown = false;
1219 try {
1220 char[,,] c = new Char[2,2,2];
1221 c.GetValue(4,1,1);
1222 } catch (IndexOutOfRangeException) {
1223 errorThrown = true;
1225 Assert.IsTrue (errorThrown, "#I43");
1228 char[,,] c1 = new Char[4,2,3];
1229 for (int i = 0; i < 24; i++) {
1230 int first = i / 6;
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 + ")");
1245 [Test]
1246 [ExpectedException (typeof (ArgumentNullException))]
1247 public void TestGetValueLongArray ()
1249 char[] c = new Char[2];
1250 c.GetValue((long [])null);
1253 [Test]
1254 public void TestGetValueN() {
1256 bool errorThrown = false;
1257 try {
1258 char[] c = new Char[2];
1259 c.GetValue((int [])null);
1260 } catch (ArgumentNullException) {
1261 errorThrown = true;
1263 Assert.IsTrue (errorThrown, "#I61a");
1266 bool errorThrown = false;
1267 try {
1268 char[] c = new Char[2];
1269 int[] coords = {1, 1};
1270 c.GetValue(coords);
1271 } catch (ArgumentException) {
1272 errorThrown = true;
1274 Assert.IsTrue (errorThrown, "#I62");
1277 bool errorThrown = false;
1278 try {
1279 char[,] c = new Char[2,2];
1280 int[] coords = {-1, 1};
1281 c.GetValue(coords);
1282 } catch (IndexOutOfRangeException) {
1283 errorThrown = true;
1285 Assert.IsTrue (errorThrown, "#I63");
1288 bool errorThrown = false;
1289 try {
1290 char[,] c = new Char[2,2];
1291 int[] coords = {4, 1};
1292 c.GetValue(coords);
1293 } catch (IndexOutOfRangeException) {
1294 errorThrown = true;
1296 Assert.IsTrue (errorThrown, "#I64");
1299 char[,] c1 = new Char[4,6];
1300 for (int i = 0; i < 24; i++) {
1301 int first = i / 6;
1302 int second = i % 6;
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 + ")");
1313 [Test]
1314 public void TestIndexOf1() {
1316 bool errorThrown = false;
1317 try {
1318 Array.IndexOf(null, "huh?");
1319 } catch (ArgumentNullException) {
1320 errorThrown = true;
1322 Assert.IsTrue (errorThrown, "#J01");
1325 bool errorThrown = false;
1326 try {
1327 char[,] c = new Char[2,2];
1328 Array.IndexOf(c, "huh?");
1329 } catch (RankException) {
1330 errorThrown = true;
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");
1342 [Test]
1343 public void TestIndexOf2() {
1345 bool errorThrown = false;
1346 try {
1347 Array.IndexOf(null, "huh?", 0);
1348 } catch (ArgumentNullException) {
1349 errorThrown = true;
1351 Assert.IsTrue (errorThrown, "#J21");
1354 bool errorThrown = false;
1355 try {
1356 char[,] c = new Char[2,2];
1357 Array.IndexOf(c, "huh?", 0);
1358 } catch (RankException) {
1359 errorThrown = true;
1361 Assert.IsTrue (errorThrown, "#J22");
1364 bool errorThrown = false;
1365 try {
1366 char[] c = new Char[2];
1367 Array.IndexOf(c, "huh?", 3);
1368 } catch (ArgumentOutOfRangeException) {
1369 errorThrown = true;
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");
1382 [Test]
1383 public void TestIndexOf3() {
1385 bool errorThrown = false;
1386 try {
1387 Array.IndexOf(null, "huh?", 0, 1);
1388 } catch (ArgumentNullException) {
1389 errorThrown = true;
1391 Assert.IsTrue (errorThrown, "#J41");
1394 bool errorThrown = false;
1395 try {
1396 char[,] c = new Char[2,2];
1397 Array.IndexOf(c, "huh?", 0, 1);
1398 } catch (RankException) {
1399 errorThrown = true;
1401 Assert.IsTrue (errorThrown, "#J42");
1404 bool errorThrown = false;
1405 try {
1406 char[] c = new Char[2];
1407 Array.IndexOf(c, "huh?", 3, 1);
1408 } catch (ArgumentOutOfRangeException) {
1409 errorThrown = true;
1411 Assert.IsTrue (errorThrown, "#J43");
1414 bool errorThrown = false;
1415 try {
1416 char[] c = new Char[2];
1417 Array.IndexOf(c, "huh?", 0, 5);
1418 } catch (ArgumentOutOfRangeException) {
1419 errorThrown = true;
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");
1433 [Test]
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"));
1443 [Test]
1444 public void TestLastIndexOf1() {
1446 bool errorThrown = false;
1447 try {
1448 Array.LastIndexOf(null, "huh?");
1449 } catch (ArgumentNullException) {
1450 errorThrown = true;
1452 Assert.IsTrue (errorThrown, "#K01");
1455 bool errorThrown = false;
1456 try {
1457 char[,] c = new Char[2,2];
1458 Array.LastIndexOf(c, "huh?");
1459 } catch (RankException) {
1460 errorThrown = true;
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"));
1475 [Test]
1476 public void TestLastIndexOf2() {
1478 bool errorThrown = false;
1479 try {
1480 Array.LastIndexOf(null, "huh?", 0);
1481 } catch (ArgumentNullException) {
1482 errorThrown = true;
1484 Assert.IsTrue (errorThrown, "#K21");
1487 bool errorThrown = false;
1488 try {
1489 char[,] c = new Char[2,2];
1490 Array.LastIndexOf(c, "huh?", 0);
1491 } catch (RankException) {
1492 errorThrown = true;
1494 Assert.IsTrue (errorThrown, "#K22");
1497 bool errorThrown = false;
1498 try {
1499 char[] c = new Char[2];
1500 Array.LastIndexOf(c, "huh?", 3);
1501 } catch (ArgumentOutOfRangeException) {
1502 errorThrown = true;
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");
1515 [Test]
1516 public void TestLastIndexOf3() {
1518 bool errorThrown = false;
1519 try {
1520 Array.LastIndexOf(null, "huh?", 0, 1);
1521 } catch (ArgumentNullException) {
1522 errorThrown = true;
1524 Assert.IsTrue (errorThrown, "#K41");
1527 bool errorThrown = false;
1528 try {
1529 char[,] c = new Char[2,2];
1530 Array.LastIndexOf(c, "huh?", 0, 1);
1531 } catch (RankException) {
1532 errorThrown = true;
1534 Assert.IsTrue (errorThrown, "#K42");
1537 bool errorThrown = false;
1538 try {
1539 char[] c = new Char[2];
1540 Array.LastIndexOf(c, "huh?", 3, 1);
1541 } catch (ArgumentOutOfRangeException) {
1542 errorThrown = true;
1544 Assert.IsTrue (errorThrown, "#K43");
1547 bool errorThrown = false;
1548 try {
1549 char[] c = new Char[2];
1550 Array.LastIndexOf(c, "huh?", 0, 5);
1551 } catch (ArgumentOutOfRangeException) {
1552 errorThrown = true;
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");
1566 [Test]
1567 public void TestLastIndexOf4 ()
1569 short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
1570 try {
1571 Array.LastIndexOf (a, (object)16, -1);
1572 NUnit.Framework.Assert.Fail ("#1");
1573 } catch (ArgumentOutOfRangeException) { }
1575 try {
1576 Array.LastIndexOf<short> (a, 16, -1);
1577 NUnit.Framework.Assert.Fail ("#2");
1578 } catch (ArgumentOutOfRangeException) { }
1581 [Test]
1582 public void TestLastIndexOf5 ()
1584 char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
1585 string s;
1586 int retval;
1587 bool error = false;
1589 for (int i = a.Length - 1; i >= 0 ; i--) {
1590 s = i.ToString ();
1591 retval = Array.LastIndexOf(a, a [i], i, i + 1);
1592 if (retval != i)
1593 error = true;
1595 Assert.IsTrue (!error);
1598 [Test]
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);
1607 [Test]
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);
1616 [Test]
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);
1628 try {
1629 Array.LastIndexOf (array, (object) null, -1, 0);
1630 Assert.Fail ("#04");
1631 } catch (ArgumentOutOfRangeException e) {
1633 try {
1634 Array.LastIndexOf (array, (object) null, -1, 10);
1635 Assert.Fail ("#05");
1636 } catch (ArgumentOutOfRangeException e) {
1638 try {
1639 Array.LastIndexOf (array, (object) null, -100, 10);
1640 Assert.Fail ("#06");
1641 } catch (ArgumentOutOfRangeException e) {
1646 [Test]
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));
1653 [Test]
1654 public void FindIndex_Invalid ()
1656 var array = new int [] { 1, 2, 3, 4, 5 };
1658 try {
1659 Array.FindIndex (array, null);
1660 Assert.Fail ("#1");
1661 } catch (ArgumentNullException) {
1664 try {
1665 Array.FindIndex (array, -1, l => true);
1666 Assert.Fail ("#2");
1667 } catch (ArgumentOutOfRangeException) {
1670 try {
1671 Array.FindIndex (array, -1, 0, l => true);
1672 Assert.Fail ("#2b");
1673 } catch (ArgumentOutOfRangeException) {
1676 try {
1677 Array.FindIndex (array, 0, -1, l => true);
1678 Assert.Fail ("#3");
1679 } catch (ArgumentOutOfRangeException) {
1682 try {
1683 Array.FindIndex (array, 100, l => true);
1684 Assert.Fail ("#4");
1685 } catch (ArgumentOutOfRangeException) {
1688 try {
1689 Array.FindIndex (array, 100, 0, l => true);
1690 Assert.Fail ("#4b");
1691 } catch (ArgumentOutOfRangeException) {
1694 try {
1695 Array.FindIndex (array, 7, 2, l => true);
1696 Assert.Fail ("#5");
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);
1708 [Test]
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));
1718 [Test]
1719 public void FindLastIndex_Invalid ()
1721 var array = new int [] { 1, 2, 3, 4, 5 };
1722 try {
1723 Array.FindLastIndex (array, null);
1724 Assert.Fail ("#1");
1725 } catch (ArgumentNullException) {
1728 try {
1729 Array.FindLastIndex (array, -1, l => true);
1730 Assert.Fail ("#2");
1731 } catch (ArgumentOutOfRangeException) {
1734 try {
1735 Array.FindLastIndex (array, -1, 0, l => true);
1736 Assert.Fail ("#2b");
1737 } catch (ArgumentOutOfRangeException) {
1740 try {
1741 Array.FindLastIndex (array, 0, -1, l => true);
1742 Assert.Fail ("#3");
1743 } catch (ArgumentOutOfRangeException) {
1746 try {
1747 Array.FindLastIndex (array, 100, l => true);
1748 Assert.Fail ("#4");
1749 } catch (ArgumentOutOfRangeException) {
1752 try {
1753 Array.FindLastIndex (array, 100, 0, l => true);
1754 Assert.Fail ("#4b");
1755 } catch (ArgumentOutOfRangeException) {
1758 try {
1759 Array.FindLastIndex (array, 2, 4, l => true);
1760 Assert.Fail ("#5");
1761 } catch (ArgumentOutOfRangeException) {
1765 [Test]
1766 public void TestReverse() {
1768 bool errorThrown = false;
1769 try {
1770 Array.Reverse(null);
1771 } catch (ArgumentNullException) {
1772 errorThrown = true;
1774 Assert.IsTrue (errorThrown, "#L01");
1777 bool errorThrown = false;
1778 try {
1779 char[,] c = new Char[2,2];
1780 Array.Reverse(c);
1781 } catch (RankException) {
1782 errorThrown = true;
1784 Assert.IsTrue (errorThrown, "#L02");
1787 char[] c1 = {'a', 'b', 'c', 'd'};
1788 Array.Reverse(c1);
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;
1796 try {
1797 Array.Reverse(null, 0, 0);
1798 } catch (ArgumentNullException) {
1799 errorThrown = true;
1801 Assert.IsTrue (errorThrown, "#L07");
1804 bool errorThrown = false;
1805 try {
1806 char[,] c = new Char[2,2];
1807 Array.Reverse(c, 0, 0);
1808 } catch (RankException) {
1809 errorThrown = true;
1811 Assert.IsTrue (errorThrown, "#L08");
1814 //bool errorThrown = false;
1815 //try {
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;
1825 //try {
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");
1842 [Test]
1843 // #8904
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 };
1848 Array.Reverse (c3);
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);
1857 struct BStruct {
1858 public int i1, i2, i3;
1861 [Test]
1862 public void TestSetValue1() {
1864 bool errorThrown = false;
1865 try {
1866 char[,] c = new Char[2,2];
1867 c.SetValue("buh", 1);
1868 } catch (ArgumentException) {
1869 errorThrown = true;
1871 Assert.IsTrue (errorThrown, "#M01");
1874 bool errorThrown = false;
1875 try {
1876 char[] c = {'a', 'b', 'c'};
1877 c.SetValue("buh", -1);
1878 } catch (IndexOutOfRangeException) {
1879 errorThrown = true;
1881 Assert.IsTrue (errorThrown, "#M02");
1884 bool errorThrown = false;
1885 try {
1886 char[] c = {'a', 'b', 'c'};
1887 c.SetValue("buh", 4);
1888 } catch (IndexOutOfRangeException) {
1889 errorThrown = true;
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);
1909 try {
1910 c3.CopyTo (c4, 0);
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];
1920 try {
1921 c4.CopyTo (c5, 0);
1922 } catch (Exception e) {
1923 Assert.Fail ("c4.CopyTo(): e=" + e);
1926 try {
1927 c5.CopyTo (c6, 0);
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 + ")");
1935 [Test]
1936 public void TestSetValue2() {
1938 bool errorThrown = false;
1939 try {
1940 char[] c = new Char[2];
1941 c.SetValue("buh", 1,1);
1942 } catch (ArgumentException) {
1943 errorThrown = true;
1945 Assert.IsTrue (errorThrown, "#M21");
1948 bool errorThrown = false;
1949 try {
1950 char[,] c = new Char[2,2];
1951 c.SetValue("buh", -1, 1);
1952 } catch (IndexOutOfRangeException) {
1953 errorThrown = true;
1955 Assert.IsTrue (errorThrown, "#M22");
1958 bool errorThrown = false;
1959 try {
1960 char[,] c = new Char[2,2];
1961 c.SetValue("buh", 4,1);
1962 } catch (IndexOutOfRangeException) {
1963 errorThrown = true;
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++) {
1971 int first = i / 6;
1972 int second = i % 6;
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 + ")");
1983 [Test]
1984 public void TestSetValue3() {
1986 bool errorThrown = false;
1987 try {
1988 char[] c = new Char[2];
1989 c.SetValue("buh", 1,1,1);
1990 } catch (ArgumentException) {
1991 errorThrown = true;
1993 Assert.IsTrue (errorThrown, "#M41");
1996 bool errorThrown = false;
1997 try {
1998 char[,,] c = new Char[2,2,2];
1999 c.SetValue("buh", -1, 1, 1);
2000 } catch (IndexOutOfRangeException) {
2001 errorThrown = true;
2003 Assert.IsTrue (errorThrown, "#M42");
2006 bool errorThrown = false;
2007 try {
2008 char[,,] c = new Char[2,2,2];
2009 c.SetValue("buh", 4,1,1);
2010 } catch (IndexOutOfRangeException) {
2011 errorThrown = true;
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++) {
2019 int first = i / 6;
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 + " )");
2035 [Test]
2036 [ExpectedException (typeof (ArgumentNullException))]
2037 public void TestSetValueLongArray ()
2039 char[] c = new Char[2];
2040 c.SetValue("buh", (long [])null);
2043 [Test]
2044 public void TestSetValueN() {
2046 bool errorThrown = false;
2047 try {
2048 char[] c = new Char[2];
2049 c.SetValue("buh", (int [])null);
2050 } catch (ArgumentNullException) {
2051 errorThrown = true;
2053 Assert.IsTrue (errorThrown, "#M61a");
2056 bool errorThrown = false;
2057 try {
2058 char[] c = new Char[2];
2059 int[] coords = {1, 1};
2060 c.SetValue("buh", coords);
2061 } catch (ArgumentException) {
2062 errorThrown = true;
2064 Assert.IsTrue (errorThrown, "#M62");
2067 bool errorThrown = false;
2068 try {
2069 char[,] c = new Char[2,2];
2070 int[] coords = {-1, 1};
2071 c.SetValue("buh", coords);
2072 } catch (IndexOutOfRangeException) {
2073 errorThrown = true;
2075 Assert.IsTrue (errorThrown, "#M63");
2078 bool errorThrown = false;
2079 try {
2080 char[,] c = new Char[2,2];
2081 int[] coords = {4, 1};
2082 c.SetValue("buh", coords);
2083 } catch (IndexOutOfRangeException) {
2084 errorThrown = true;
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++) {
2092 int first = i / 6;
2093 int second = i % 6;
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 + ")");
2105 [Test]
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;
2123 try {
2124 c2.SetValue (c1 [0], 0);
2125 } catch (ArgumentException) {
2126 errorThrown = true;
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";
2155 c1[0] = test;
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];
2164 try {
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];
2172 try {
2173 c2.SetValue (c1 [0], 0);
2174 Assert.Fail ("#M89");
2175 } catch (ArgumentException) {}
2178 Type[] types = {
2179 typeof (Boolean),
2180 typeof (Byte),
2181 typeof (Char),
2182 typeof (Double),
2183 typeof (Int16),
2184 typeof (Int32),
2185 typeof (Int64),
2186 typeof (SByte),
2187 typeof (Single),
2188 typeof (UInt16),
2189 typeof (UInt32),
2190 typeof (UInt64)
2193 bool v1 = true;
2194 Byte v2 = 1;
2195 Char v3 = 'a';
2196 Double v4 = -1.2;
2197 Int16 v5 = -32;
2198 Int32 v6 = -234;
2199 Int64 v7 = -34523;
2200 SByte v8 = -1;
2201 Single v9 = -4.8F;
2202 UInt16 v10 = 24234;
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 };
2212 int[] arg_ex = {
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
2227 // SetValue
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;
2236 try {
2237 array.SetValue (value, 0);
2238 } catch (ArgumentException) {
2239 errorThrown = true;
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;
2254 try {
2255 array.SetValue (value, 0);
2256 } catch (InvalidCastException) {
2257 errorThrown = true;
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;
2269 try {
2270 array.SetValue (value, 0);
2271 } catch (InvalidCastException) {
2272 errorThrown = true;
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;
2284 try {
2285 array.SetValue (value, 0);
2286 } catch (InvalidCastException) {
2287 errorThrown = true;
2290 Assert.IsFalse (errorThrown, "#M93(" + types [i] + ")");
2293 // Copy
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;
2304 try {
2305 Array.Copy (source, array, 2);
2306 } catch (ArrayTypeMismatchException) {
2307 errorThrown = true;
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;
2324 try {
2325 Array.Copy (source, array, 2);
2326 } catch (ArrayTypeMismatchException) {
2327 errorThrown = true;
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;
2341 try {
2342 Array.Copy (source, array, 2);
2343 } catch (ArrayTypeMismatchException) {
2344 errorThrown = true;
2347 Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
2352 [Test]
2353 public void TestSort() {
2355 bool errorThrown = false;
2356 try {
2357 Array.Sort(null);
2358 } catch (ArgumentNullException) {
2359 errorThrown = true;
2361 Assert.IsTrue (errorThrown, "#N01");
2364 bool errorThrown = false;
2365 try {
2366 Array.Sort(null, 0, 1);
2367 } catch (ArgumentNullException) {
2368 errorThrown = true;
2370 Assert.IsTrue (errorThrown, "#N02");
2373 bool errorThrown = false;
2374 try {
2375 char[] c1 = new Char[2];
2376 Array.Sort(null, c1);
2377 } catch (ArgumentNullException) {
2378 errorThrown = true;
2380 Assert.IsTrue (errorThrown, "#N03");
2383 bool errorThrown = false;
2384 try {
2385 char[] c1 = new Char[2];
2386 Array.Sort(null, c1, 0, 1);
2387 } catch (ArgumentNullException) {
2388 errorThrown = true;
2390 Assert.IsTrue (errorThrown, "#N04");
2393 int tc = 5;
2394 char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
2396 try {
2397 Array.Sort (null, 0, 1);
2398 Assert.Fail ("#N" + tc.ToString ());
2400 catch (ArgumentException) {}
2401 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2402 tc++;
2404 try {
2405 Array.Sort (arr, -1, 3);
2406 Assert.Fail ("#N" + tc.ToString ());
2408 catch (ArgumentException) {}
2409 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2410 tc++;
2412 try {
2413 Array.Sort (arr, 1, -3);
2414 Assert.Fail ("#N" + tc.ToString ());
2416 catch (ArgumentException) {}
2417 catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
2418 tc++;
2420 try {
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();
2433 Array.Sort(c1);
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();
2444 Array.Sort(c1, i1);
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");
2487 // #648828
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),
2503 0x7FFFFFFFffffffff,
2507 Array a = arr;
2508 Array.Sort (a, names, null);
2509 Assert.AreEqual (0, a.GetValue (0));
2513 [Test]
2514 public void Sort_NullValues ()
2516 var s = new [] { "a", null, "b", null };
2517 Array.Sort (s, (a, b) => {
2518 if (a == null) {
2519 return b == null ? 0 : 1;
2522 if (b == null)
2523 return -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");
2534 [Test] // #616416
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];
2551 indices[0] = 0;
2552 indices[1] = 1;
2553 indices[2] = 2;
2554 indices[3] = 3;
2555 indices[4] = 4;
2556 indices[5] = 5;
2557 indices[6] = 6;
2558 indices[7] = 7;
2559 indices[8] = 8;
2560 indices[9] = 9;
2561 indices[10] = 10;
2563 Array.Sort ((Array)doubleValues, (Array)indices);
2564 Assert.AreEqual (4, indices [0]);
2567 [Test]
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
2581 readonly int val;
2583 public Comp (int a)
2585 val = a;
2588 int IComparable.CompareTo (object obj)
2590 return val.CompareTo ((obj as Comp).val);
2594 [Test]
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 };
2600 Array.Sort (arr);
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)
2610 return -1;
2614 [Test]
2615 public void TestInitializeEmpty()
2617 bool catched=false;
2618 int[] a = {};
2621 a.Initialize();
2623 catch(Exception)
2625 catched=true;
2627 Assert.IsTrue (!catched, "#TI01");
2630 [Test]
2631 public void TestInitializeInt()
2633 int[] a = {1,2,0};
2634 a.Initialize();
2635 int[] b = {1,2,0};
2636 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2638 Assert.AreEqual (a[i], b[i], "#TI02 " + i);
2642 [Test]
2643 public void TestInitializeDouble()
2645 double[] a = {1.0,2.0,0.0};
2646 a.Initialize();
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);
2654 [Test]
2655 public void TestInitializeFloat()
2657 float[] a = {1.0F,2.0F,0.0F};
2658 a.Initialize();
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);
2666 [Test]
2667 public void TestInitializeChar()
2669 char[] a = {'1','.','0','F','2','.','0','F'};
2670 a.Initialize();
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);
2678 [Test]
2679 public void TestInitializeString()
2681 string[] a = {"hola","adios","menos","mas"};
2682 a.Initialize();
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);
2690 [Test]
2691 public void TestInitializeEnum()
2693 enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
2694 a.Initialize();
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);
2702 [Test]
2703 public void TestInitializeIntNI()
2705 int[] a = new int[20];
2706 a.Initialize();
2707 foreach(int b in a)
2709 Assert.AreEqual (b, 0, "#TI08");
2713 [Test]
2714 public void TestInitializeCharNI()
2716 char[] a = new char[20];
2717 a.Initialize();
2718 foreach(char b in a)
2720 Assert.AreEqual (b, 0, "#TI09");
2724 [Test]
2725 public void TestInitializeDoubleNI()
2727 double[] a = new double[20];
2728 a.Initialize();
2729 foreach(double b in a)
2731 Assert.AreEqual (b, 0.0, "#TI09");
2735 [Test]
2736 public void TestInitializeStringNI()
2738 string[] a = new string[20];
2739 a.Initialize();
2740 foreach(string b in a)
2742 Assert.AreEqual (b, null, "#TI10");
2746 [Test]
2747 public void TestInitializeObjectNI()
2749 object[] a = new object[20];
2750 a.Initialize();
2751 foreach(object b in a)
2753 Assert.AreEqual (b, null, "#TI11");
2757 [Test]
2758 public void TestInitializeAClassNI()
2760 AClass[] a = new AClass[20];
2761 a.Initialize();
2762 foreach(AClass b in a)
2764 Assert.AreEqual (b, null, "#TI12");
2769 [Test]
2770 public void TestInitializeAStructNI()
2772 AStruct[] a = new AStruct[20];
2773 a.Initialize();
2774 foreach(AStruct b in a)
2776 Assert.AreEqual (b, new AStruct(), "#TI14");
2780 [Test]
2781 public void TestInitializeAStruct()
2783 AStruct[] a = new AStruct[3];
2784 a[1].a = "ADIOS";
2785 a[1].s = "HOLA";
2786 a.Initialize();
2787 AStruct[] b = new AStruct[3];
2788 b[1].a = "ADIOS";
2789 b[1].s = "HOLA";
2790 for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
2792 Assert.AreEqual (a[i], b[i], "#TI15 " + i);
2796 [Test]
2797 public void TestInitializeDateTimeNI()
2799 DateTime[] a = new DateTime[20];
2800 a.Initialize();
2801 foreach(DateTime b in a)
2803 Assert.AreEqual (b, new DateTime(), "#TI16");
2807 [Test]
2808 [ExpectedException (typeof (ArgumentNullException))]
2809 public void MoreSort1 ()
2811 Array.Sort (null, 0, 1);
2814 [Test]
2815 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2816 public void MoreSort2 ()
2818 Array.Sort (arrsort, -1, 3);
2821 [Test]
2822 [ExpectedException (typeof (ArgumentOutOfRangeException))]
2823 public void MoreSort3 ()
2825 Array.Sort (arrsort, 1, -3);
2828 [Test]
2829 [ExpectedException (typeof (ArgumentException))]
2830 public void MoreSort4 ()
2832 Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
2835 [Test]
2836 [ExpectedException (typeof (RankException))]
2837 public void MoreSort5 ()
2839 char [,] arr = new char [,] {{'a'}, {'b'}};
2840 Array.Sort (arr, 0, 1);
2843 [Test]
2844 public void MoreSort6 ()
2846 Array.Sort (arrsort, 0, 0);
2849 [Test]
2850 [ExpectedException (typeof (ArgumentException))]
2851 public void MoreSort7 ()
2853 Array.Sort (arrsort, arrsort.Length - 1, 2);
2856 [Test]
2857 [ExpectedException (typeof (ArgumentException))]
2858 public void MoreSort8 ()
2860 Array.Sort (arrsort, 0, arrsort.Length + 1);
2863 [Test]
2864 public void MoreSort9 ()
2866 Array.Sort (arrsort, null, 0, arrsort.Length, null);
2869 [Test]
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
2878 public void Sort ()
2880 double [] a = new double [2] { 0.9, 0.3 };
2881 uint [] b = new uint [2] { 4, 7 };
2882 Array.Sort (a, b);
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");
2889 [Test]
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++) {
2896 matrix [i][j] = 1;
2899 Array.Clear (matrix, 0, 8);
2900 for (int i=0; i < 8; i++) {
2901 Assert.IsNull (matrix [i], i.ToString ());
2905 [Test]
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");
2916 [Test]
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);
2924 [Test]
2925 [ExpectedException (typeof (IndexOutOfRangeException))]
2926 public void Clear_IndexOverflow ()
2928 byte[] array = new byte [16];
2929 Array.Clear (array, 4, Int32.MaxValue);
2932 [Test]
2933 [ExpectedException (typeof (IndexOutOfRangeException))]
2934 public void Clear_LengthOverflow ()
2936 byte[] array = new byte [16];
2937 Array.Clear (array, Int32.MaxValue, 4);
2940 [Test]
2941 [ExpectedException (typeof (ArgumentException))]
2942 public void Copy_SourceIndexOverflow ()
2944 byte[] array = new byte [16];
2945 Array.Copy (array, Int32.MaxValue, array, 8, 8);
2948 [Test]
2949 [ExpectedException (typeof (ArgumentException))]
2950 public void Copy_DestinationIndexOverflow ()
2952 byte[] array = new byte [16];
2953 Array.Copy (array, 8, array, Int32.MaxValue, 8);
2956 [Test]
2957 [ExpectedException (typeof (ArgumentException))]
2958 public void Copy_LengthOverflow ()
2960 byte[] array = new byte [16];
2961 Array.Copy (array, 8, array, 8, Int32.MaxValue);
2964 [Test]
2965 [ExpectedException (typeof (ArgumentException))]
2966 public void Reverse_IndexOverflow ()
2968 byte[] array = new byte [16];
2969 Array.Reverse (array, Int32.MaxValue, 8);
2972 [Test]
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 {
2981 public char c;
2983 public CharX (char c)
2985 this.c = c;
2988 public int CompareTo (object obj)
2990 if (obj is CharX)
2991 return c.CompareTo (((CharX) obj).c);
2992 else
2993 return c.CompareTo (obj);
2997 [Test]
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 {
3014 get {
3015 bool result = called;
3016 called = false;
3017 return called;
3021 public int Compare (object x, object y)
3023 called = true;
3024 return 0;
3028 [Test]
3029 public void BinarySearch1_EmptyList ()
3031 int[] array = new int[0];
3032 Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
3035 [Test]
3036 public void BinarySearch2_EmptyList ()
3038 int[] array = new int[0];
3039 Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
3042 [Test]
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");
3052 [Test]
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");
3062 [Test]
3063 [ExpectedException (typeof (ArgumentNullException))]
3064 public void AsReadOnly_NullArray ()
3066 Array.AsReadOnly <int> (null);
3069 [Test]
3070 public void ReadOnly_Count ()
3072 Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
3075 [Test]
3076 public void ReadOnly_Contains ()
3078 int[] arr = new int [2];
3079 arr [0] = 3;
3080 arr [1] = 5;
3081 IList<int> a = Array.AsReadOnly (arr);
3083 Assert.IsTrue (a.Contains (3));
3084 Assert.IsTrue (!a.Contains (6));
3087 [Test]
3088 public void ReadOnly_IndexOf ()
3090 int[] arr = new int [2];
3091 arr [0] = 3;
3092 arr [1] = 5;
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));
3100 [Test]
3101 public void ReadOnly_Indexer ()
3103 int[] arr = new int [2];
3104 arr [0] = 3;
3105 arr [1] = 5;
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 */
3112 arr [0] = 6;
3113 Assert.AreEqual (6, a [0]);
3116 [Test]
3117 public void ReadOnly_Enumerator ()
3119 int[] arr = new int [10];
3121 for (int i = 0; i < 10; ++i)
3122 arr [i] = i;
3124 int sum = 0;
3125 foreach (int i in Array.AsReadOnly (arr))
3126 sum += i;
3128 Assert.AreEqual (45, sum);
3131 [Test]
3132 public void ReadOnly_CopyTo ()
3134 int[] arr = new int [2];
3135 arr [0] = 3;
3136 arr [1] = 5;
3137 IList<int> a = Array.AsReadOnly (arr);
3139 int[] arr2 = new int [3];
3140 a.CopyTo (arr2, 1);
3142 Assert.AreEqual (0, arr2 [0]);
3143 Assert.AreEqual (3, arr2 [1]);
3144 Assert.AreEqual (5, arr2 [2]);
3147 [Test]
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");
3170 [Test]
3171 public void Resize_null ()
3173 int [] arr = null;
3174 Array.Resize (ref arr, 10);
3175 Assert.AreEqual (arr.Length, 10);
3178 [Test]
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>
3191 int _x;
3192 public EquatableClass (int x)
3194 _x = x;
3197 public bool Equals (EquatableClass other)
3199 return this._x == other._x;
3203 [Test]
3204 public void AsIList ()
3206 IList<int> arr = new int [10];
3207 arr [0] = 5;
3208 Assert.AreEqual (5, arr [0]);
3210 IList<FooStruct> arr2 = new FooStruct [10];
3211 FooStruct s = new FooStruct ();
3212 s.i = 11;
3213 s.j = 22;
3214 arr2 [5] = s;
3215 s = arr2 [5];
3216 Assert.AreEqual (11, s.i);
3217 Assert.AreEqual (22, s.j);
3219 IList<string> arr3 = new string [10];
3220 arr3 [5] = "ABC";
3221 Assert.AreEqual ("ABC", arr3 [5]);
3224 struct FooStruct {
3225 public int i, j;
3228 [Test]
3229 // From bug #80563
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");
3256 [Test]
3257 public void IListNull ()
3259 IList<object> test;
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");
3284 #region Bug 80299
3286 enum ByteEnum : byte {}
3287 enum IntEnum : int {}
3289 [Test]
3290 public void TestByteEnumArrayToByteArray ()
3292 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3293 byte[] b = new byte[a.Length];
3294 a.CopyTo (b, 0);
3297 [Test]
3298 public void TestByteEnumArrayToIntArray ()
3300 ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
3301 int[] b = new int[a.Length];
3302 a.CopyTo (b, 0);
3305 [Test]
3306 [ExpectedException (typeof (ArrayTypeMismatchException))]
3307 public void TestIntEnumArrayToByteArray ()
3309 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3310 byte[] b = new byte[a.Length];
3311 a.CopyTo (b, 0);
3314 [Test]
3315 public void TestIntEnumArrayToIntArray ()
3317 IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
3318 int[] b = new int[a.Length];
3319 a.CopyTo (b, 0);
3322 #endregion
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");
3334 e.Reset ();
3336 Assert.IsTrue (e.MoveNext (), "#C1");
3337 Assert.AreEqual (1, e.Current, "#C2");
3340 [Test]
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");
3353 try {
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");
3363 [Test]
3364 public void IEnumerator_Current_NotStarted ()
3366 int[] array = new int[] { 1, 2, 3 };
3367 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3369 try {
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");
3379 [Test]
3380 public void IEnumerator_Current_Reset ()
3382 int[] array = new int[] { 1, 2, 3 };
3383 IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
3384 e.MoveNext ();
3385 e.Reset ();
3387 try {
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");
3397 [Test]
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");
3404 en.Dispose ();
3405 Assert.IsFalse (en.MoveNext (), "#3");
3408 [Test]
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");
3420 [Test]
3421 public void ICollection_IsReadOnly() {
3422 ICollection<string> arr = new string [10];
3424 Assert.IsTrue (arr.IsReadOnly);
3427 [Test]
3428 [ExpectedException (typeof (NotSupportedException))]
3429 public void ArrayCreateInstanceOfVoid ()
3431 Array.CreateInstance (typeof (void), 42);
3434 class Foo<T> {}
3436 [Test]
3437 [ExpectedException (typeof (NotSupportedException))]
3438 public void ArrayCreateInstanceOfOpenGenericType ()
3440 Array.CreateInstance (typeof (Foo<>), 42);
3443 [Test]
3444 [ExpectedException (typeof (IndexOutOfRangeException))]
3445 public void ClearNegativeLength ()
3447 Array.Clear (new int [] { 1, 2 }, 0, -1);
3450 [Test]
3451 [ExpectedException (typeof (ArgumentException))]
3452 public void MultiDimension_IList_setItem ()
3454 IList array = new int [1, 1];
3455 array [0] = 2;
3458 [Test]
3459 [ExpectedException (typeof (ArgumentException))]
3460 public void MultiDimension_IList_getItem ()
3462 IList array = new int [1, 1];
3463 int a = (int) array [0];
3466 [Test]
3467 public void SetValue_Nullable () {
3468 Array array = Array.CreateInstance (typeof (int?), 7);
3470 object o = 42;
3472 array.SetValue (o, 0);
3473 Assert.AreEqual (42, array.GetValue (0));
3475 array.SetValue (null, 0);
3476 Assert.AreEqual (null, array.GetValue (0));
3479 [Test]
3480 public void SortNullsWithGenericVersion ()
3482 string[] s1 = new string[6]{
3483 "J",
3484 "M",
3485 null,
3486 "P",
3487 "T",
3488 "A"};
3490 string[] s2 = new string[]{null,
3491 "A",
3492 "J",
3493 "M",
3494 "P",
3495 "T"};
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
3505 // for bug #351638.
3507 // This tests the fix for: #622101
3509 [Test]
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);
3529 [Test] //bxc #11184
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);
3539 [Test]
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);
3547 [Test]
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);
3555 [Test]
3556 [ExpectedException (typeof (ArgumentException))]
3557 public void CompareToWithNonArrayType () {
3558 IStructuralComparable a = new int[] { 1, 2 };
3559 a.CompareTo (99, Comparer<object>.Default);
3562 [Test]
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);
3570 [Test]
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);
3578 [Test]
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);
3586 [Test]
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);
3594 [Test]
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);
3606 [Test]
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);
3619 [Test]
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);
3627 [Test]
3628 public void CompareToWithNullArray () {
3629 IStructuralComparable a = new int[] { 1, 2 };
3630 Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
3633 [Test]
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));
3644 [Test]
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));
3654 [Test]
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);
3663 [Test]
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);
3672 [Test]
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
3682 public int Counter;
3684 bool IEqualityComparer.Equals (object x, object y)
3686 throw new NotImplementedException ();
3689 public int GetHashCode (object obj)
3691 return Counter++;
3695 [Test]
3696 public void IStructuralEquatable_GetHashCode ()
3698 IStructuralEquatable a = new int[] { 1, 2, 9 };
3700 var c = new TestComparer_GetHashCode ();
3701 a.GetHashCode (c);
3702 Assert.AreEqual (3, c.Counter);
3705 [Test]
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));
3720 [Test]
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)
3728 ctor = c;
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);
3738 [Test]
3739 public unsafe void PointerArraysBoxing ()
3741 var x = new int*[10];
3742 var e = x.GetEnumerator ();
3743 e.MoveNext ();
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
3752 public struct J
3754 public int i;
3756 public J(int i_) { i = i_; }
3759 struct JComp : IComparer<J>
3761 public int Compare(J x, J y)
3763 int val = 0;
3764 Thread.Sleep (Timeout.Infinite);
3765 return val;
3769 class ArraySortAbortData {
3770 internal ManualResetEventSlim mre;
3771 internal bool threw;
3773 internal ArraySortAbortData () {
3774 mre = new ManualResetEventSlim ();
3775 threw = false;
3779 [Test]
3780 public void ArraySortAbort ()
3782 var d = new ArraySortAbortData();
3783 var t = new Thread(RunArraySort);
3784 t.Start(d);
3785 d.mre.Wait();
3786 Thread.Sleep(400);
3787 t.Abort();
3788 t.Join();
3789 Assert.IsFalse (d.threw);
3792 public static void RunArraySort(object data)
3794 var d = data as ArraySortAbortData;
3795 int n = 10;
3796 var a = new J[n];
3797 for (int i = 0; i < n; ++i)
3799 a[i] = new J(n - i);
3801 d.mre.Set();
3802 try {
3803 Array.Sort(a, 0, n, new JComp());
3804 } catch (InvalidOperationException) {
3805 // t.Abort in ArraySortAbort should _not_ end up here
3806 d.threw = true;
3809 #endif