1 // ArrayTest.cs - NUnit Test Cases for the System.Array class
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
8 using NUnit.Framework;
\r
10 using System.Collections;
\r
11 using System.Globalization;
\r
13 namespace MonoTests.System
\r
16 public class ArrayTest : TestCase
\r
18 public ArrayTest() : base ("MonoTests.System.ArrayTest testsuite") {}
\r
19 public ArrayTest(string name) : base(name) {}
\r
21 protected override void SetUp()
\r
25 protected override void TearDown()
\r
29 public static ITest Suite {
\r
31 return new TestSuite(typeof(ArrayTest));
\r
35 public void TestIsFixedSize() {
\r
37 Assert("All arrays are fixed", a1.IsFixedSize);
\r
40 public void TestIsReadOnly() {
\r
42 Assert("No array is readonly", !a1.IsReadOnly);
\r
45 public void TestIsSynchronized() {
\r
47 Assert("No array is synchronized", !a1.IsSynchronized);
\r
50 public void TestLength() {
\r
53 AssertEquals("Zero length array", 0, a1.Length);
\r
57 AssertEquals("One-length array", 1, a1.Length);
\r
60 char[] a1 = {'c', 'c'};
\r
61 AssertEquals("Two-length array", 2, a1.Length);
\r
65 public void TestRank() {
\r
66 char[] a1 = { 'c', 'd', 'e' };
\r
67 AssertEquals("Rank one", 1, a1.Rank);
\r
69 char[,] a2 = new Char[3,3];
\r
70 AssertEquals("Rank two", 2, a2.Rank);
\r
72 char[,,] a3 = new Char[3,3,3];
\r
73 AssertEquals("Rank three", 3, a3.Rank);
\r
76 public void TestBinarySearch1() {
\r
77 bool errorThrown = false;
\r
79 Array.BinarySearch(null, "blue");
\r
80 } catch (ArgumentNullException) {
\r
83 Assert("Error not thrown", errorThrown);
\r
84 errorThrown = false;
\r
86 char[,] c1 = new Char[2,2];
\r
87 Array.BinarySearch(c1, "needle");
\r
88 } catch (RankException) {
\r
91 Assert("Error not thrown", errorThrown);
\r
94 char[] bad = {'d', 'a', 'd', 'a', 'c', 'a'};
\r
95 AssertEquals("shouldn't find elem in badly-sorted array", -1, Array.BinarySearch(bad, 'c'));
\r
98 char[] bad = {'a', 'd', 'a', 'd', 'a', 'c', 'a'};
\r
99 AssertEquals("shouldn't find elem in badly-sorted array", -2, Array.BinarySearch(bad, 'c'));
\r
102 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
103 Assert("couldn't find elem",
\r
104 Array.BinarySearch(arr, 'c') >= 3);
\r
105 Assert("couldn't find elem",
\r
106 Array.BinarySearch(arr, 'c') < 6);
\r
109 char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
\r
110 AssertEquals("couldn't find next-higher elem",
\r
111 -4, Array.BinarySearch(arr, 'c'));
\r
114 char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
115 AssertEquals("couldn't find end",
\r
116 -9, Array.BinarySearch(arr, 'e'));
\r
119 public void TestBinarySearch2() {
\r
120 bool errorThrown = false;
\r
122 Array.BinarySearch(null, 0, 1, "blue");
\r
123 } catch (ArgumentNullException) {
\r
124 errorThrown = true;
\r
126 Assert("Error not thrown", errorThrown);
\r
127 errorThrown = false;
\r
129 char[,] c1 = new Char[2,2];
\r
130 Array.BinarySearch(c1, 0, 1, "needle");
\r
131 } catch (RankException) {
\r
132 errorThrown = true;
\r
134 Assert("Error not thrown", errorThrown);
\r
135 errorThrown = false;
\r
138 Array.BinarySearch(c1, -1, 1, 'a');
\r
139 } catch (ArgumentOutOfRangeException) {
\r
140 errorThrown = true;
\r
142 Assert("Error not thrown", errorThrown);
\r
143 errorThrown = false;
\r
146 Array.BinarySearch(c1, 0, -1, 'a');
\r
147 } catch (ArgumentOutOfRangeException) {
\r
148 errorThrown = true;
\r
150 Assert("Error not thrown", errorThrown);
\r
151 errorThrown = false;
\r
154 Array.BinarySearch(c1, 0, 4, 'a');
\r
155 } catch (ArgumentException) {
\r
156 errorThrown = true;
\r
158 Assert("Error not thrown", errorThrown);
\r
160 // FIXME - see commented-out tests in TestBinarySearch1, above
\r
163 char[] bad = {'z', 'z', 'd', 'a', 'd', 'a', 'c', 'a'};
\r
164 AssertEquals("shouldn't find elem in badly-sorted array", -3, Array.BinarySearch(bad, 2, 6, 'c'));
\r
167 char[] bad = {'z', 'z', 'a', 'd', 'a', 'd', 'a', 'c', 'a'};
\r
168 AssertEquals("shouldn't find elem in badly-sorted array", -4, Array.BinarySearch(bad, 2, 7, 'c'));
\r
171 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
172 Assert("couldn't find elem",
\r
173 Array.BinarySearch(arr, 2, 8, 'c') >= 5);
\r
174 Assert("couldn't find elem",
\r
175 Array.BinarySearch(arr, 2, 8, 'c') < 8);
\r
178 char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
\r
179 AssertEquals("couldn't find next-higher elem",
\r
180 -6, Array.BinarySearch(arr, 2, 8, 'c'));
\r
183 char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
\r
184 AssertEquals("couldn't find end",
\r
185 -11, Array.BinarySearch(arr, 2, 8, 'e'));
\r
189 // TODO - testBinarySearch with explicit IComparer args
\r
191 public void TestClear() {
\r
192 bool errorThrown = false;
\r
194 Array.Clear(null, 0, 1);
\r
195 } catch (ArgumentNullException) {
\r
196 errorThrown = true;
\r
198 Assert("error not thrown", errorThrown);
\r
200 int[] i1 = { 1, 2, 3, 4 };
\r
202 int[] compare = {1,2,3,4};
\r
203 AssertEquals("array match", compare[0], i1[0]);
\r
204 AssertEquals("array match", compare[1], i1[1]);
\r
205 AssertEquals("array match", compare[2], i1[2]);
\r
206 AssertEquals("array match", compare[3], i1[3]);
\r
208 Array.Clear(i1, 3, 1);
\r
210 int[] compare = {1,2,3,0};
\r
211 AssertEquals("array match", compare[0], i1[0]);
\r
212 AssertEquals("array match", compare[1], i1[1]);
\r
213 AssertEquals("array match", compare[2], i1[2]);
\r
214 AssertEquals("array match", compare[3], i1[3]);
\r
216 Array.Clear(i1, 1, 1);
\r
218 int[] compare = {1,0,3,0};
\r
219 AssertEquals("array match", compare[0], i1[0]);
\r
220 AssertEquals("array match", compare[1], i1[1]);
\r
221 AssertEquals("array match", compare[2], i1[2]);
\r
222 AssertEquals("array match", compare[3], i1[3]);
\r
224 Array.Clear(i1, 1, 3);
\r
226 int[] compare = {1,0,0,0};
\r
227 AssertEquals("array match", compare[0], i1[0]);
\r
228 AssertEquals("array match", compare[1], i1[1]);
\r
229 AssertEquals("array match", compare[2], i1[2]);
\r
230 AssertEquals("array match", compare[3], i1[3]);
\r
233 string[] s1 = { "red", "green", "blue" };
\r
234 Array.Clear(s1, 0, 3);
\r
236 string[] compare = {null, null, null};
\r
237 AssertEquals("array match", compare[0], s1[0]);
\r
238 AssertEquals("array match", compare[1], s1[1]);
\r
239 AssertEquals("array match", compare[2], s1[2]);
\r
243 public void TestClone() {
\r
244 char[] c1 = {'a', 'b', 'c'};
\r
245 char[] c2 = (char[])c1.Clone();
\r
246 AssertEquals("Array match", c1[0], c2[0]);
\r
247 AssertEquals("Array match", c1[1], c2[1]);
\r
248 AssertEquals("Array match", c1[2], c2[2]);
\r
250 char[] d10 = {'a', 'b'};
\r
251 char[] d11 = {'a', 'c'};
\r
252 char[] d12 = {'b', 'c'};
\r
253 char[][] d1 = {d10, d11, d12};
\r
254 char[][] d2 = (char[][])d1.Clone();
\r
255 AssertEquals("Array match", d1[0], d2[0]);
\r
256 AssertEquals("Array match", d1[1], d2[1]);
\r
257 AssertEquals("Array match", d1[2], d2[2]);
\r
260 AssertEquals("shallow copy", d1[0], d2[0]);
\r
263 public void TestCopy() {
\r
265 bool errorThrown = false;
\r
268 Array.Copy(c1, null, 1);
\r
269 } catch (ArgumentNullException) {
\r
270 errorThrown = true;
\r
272 Assert("error not thrown", errorThrown);
\r
275 bool errorThrown = false;
\r
278 Array.Copy(null, c1, 1);
\r
279 } catch (ArgumentNullException) {
\r
280 errorThrown = true;
\r
282 Assert("error not thrown", errorThrown);
\r
285 bool errorThrown = false;
\r
287 Char[] c1 = new Char[1];
\r
288 Char[,] c2 = new Char[1,1];
\r
289 Array.Copy(c1, c2, 1);
\r
290 } catch (RankException) {
\r
291 errorThrown = true;
\r
293 Assert("error not thrown", errorThrown);
\r
296 bool errorThrown = false;
\r
298 Char[] c1 = new Char[1];
\r
299 string[] s1 = new String[1];
\r
300 Array.Copy(c1, s1, 1);
\r
301 } catch (ArrayTypeMismatchException) {
\r
302 errorThrown = true;
\r
304 Assert("error not thrown", errorThrown);
\r
307 bool errorThrown = false;
\r
309 Char[] c1 = new Char[1];
\r
310 Object[] o1 = new Object[1];
\r
312 Array.Copy(o1, c1, 1);
\r
313 } catch (InvalidCastException) {
\r
314 errorThrown = true;
\r
316 Assert("error not thrown", errorThrown);
\r
319 bool errorThrown = false;
\r
321 Char[] c1 = new Char[1];
\r
322 Char[] c2 = new Char[1];
\r
323 Array.Copy(c1, c2, -1);
\r
324 } catch (ArgumentOutOfRangeException) {
\r
325 errorThrown = true;
\r
327 Assert("error not thrown", errorThrown);
\r
330 bool errorThrown = false;
\r
332 Char[] c1 = new Char[1];
\r
333 Char[] c2 = new Char[2];
\r
334 Array.Copy(c1, c2, 2);
\r
335 } catch (ArgumentException) {
\r
336 errorThrown = true;
\r
338 Assert("error not thrown", errorThrown);
\r
341 bool errorThrown = false;
\r
343 Char[] c1 = new Char[1];
\r
344 Char[] c2 = new Char[2];
\r
345 Array.Copy(c2, c1, 2);
\r
346 } catch (ArgumentException) {
\r
347 errorThrown = true;
\r
349 Assert("error not thrown", errorThrown);
\r
352 char[] orig = {'a', 'b', 'd', 'a'};
\r
353 char[] copy = new Char[4];
\r
354 Array.Copy(orig, copy, 4);
\r
355 for (int i = 0; i < orig.Length; i++) {
\r
356 AssertEquals("copy unsuccessful " + i,
\r
359 Array.Clear(copy, 0, copy.Length);
\r
360 for (int i = 0; i < orig.Length; i++) {
\r
361 AssertEquals("clear unsuccessful " + i,
\r
364 Array.Copy(orig, copy, 2);
\r
365 AssertEquals("copy unsuccessful 1", orig[0], copy[0]);
\r
366 AssertEquals("copy unsuccessful 2", orig[1], copy[1]);
\r
367 Assert("copy unsuccessful 3", orig[2] != copy[2]);
\r
368 Assert("copy unsuccessful 4", orig[3] != copy[3]);
\r
370 public void TestCopy2() {
\r
372 bool errorThrown = false;
\r
374 Char[] c1 = new Char[2];
\r
375 Char[] c2 = new Char[2];
\r
376 Array.Copy(c2, 1, c1, 0, 2);
\r
377 } catch (ArgumentException) {
\r
378 errorThrown = true;
\r
380 Assert("error not thrown", errorThrown);
\r
383 bool errorThrown = false;
\r
385 Char[] c1 = new Char[2];
\r
386 Char[] c2 = new Char[2];
\r
387 Array.Copy(c2, 0, c1, 1, 2);
\r
388 } catch (ArgumentException) {
\r
389 errorThrown = true;
\r
391 Assert("error not thrown", errorThrown);
\r
394 char[] orig = {'a', 'b', 'd', 'a'};
\r
395 char[] copy = new Char[4];
\r
396 Array.Copy(orig, 1, copy, 1, 3);
\r
397 Assert("copy unsuccessful", copy[0] != orig[0]);
\r
398 for (int i = 1; i < orig.Length; i++) {
\r
399 AssertEquals("copy unsuccessful " + i,
\r
402 Array.Clear(copy, 0, copy.Length);
\r
403 Array.Copy(orig, 1, copy, 0, 2);
\r
404 AssertEquals("copy unsuccessful", orig[1], copy[0]);
\r
405 AssertEquals("copy unsuccessful", orig[2], copy[1]);
\r
406 Assert("copy unsuccessful", copy[2] != orig[2]);
\r
407 Assert("copy unsuccessful", copy[3] != orig[3]);
\r
410 public void TestCopyTo() {
\r
412 bool errorThrown = false;
\r
414 Char[] c1 = new Char[2];
\r
415 c1.CopyTo(null, 2);
\r
416 } catch (ArgumentNullException) {
\r
417 errorThrown = true;
\r
419 Assert("error not thrown", errorThrown);
\r
422 bool errorThrown = false;
\r
424 Char[] c1 = new Char[2];
\r
425 Char[,] c2 = new Char[2,2];
\r
427 } catch (ArgumentException) {
\r
428 errorThrown = true;
\r
430 Assert("error not thrown", errorThrown);
\r
433 bool errorThrown = false;
\r
435 Char[,] c1 = new Char[2,2];
\r
436 Char[] c2 = new Char[2];
\r
438 } catch (RankException) {
\r
439 errorThrown = true;
\r
441 Assert("error not thrown", errorThrown);
\r
444 bool errorThrown = false;
\r
446 Char[] c1 = new Char[2];
\r
447 Char[] c2 = new Char[2];
\r
449 } catch (ArgumentOutOfRangeException) {
\r
450 errorThrown = true;
\r
452 Assert("error not thrown", errorThrown);
\r
455 bool errorThrown = false;
\r
457 Char[] c1 = new Char[2];
\r
458 Char[] c2 = new Char[2];
\r
460 } catch (ArgumentException) {
\r
461 errorThrown = true;
\r
463 Assert("error not thrown", errorThrown);
\r
466 bool errorThrown = false;
\r
468 Char[] c1 = new Char[2];
\r
469 Char[] c2 = new Char[2];
\r
471 } catch (ArgumentException) {
\r
472 errorThrown = true;
\r
474 Assert("error not thrown", errorThrown);
\r
477 bool errorThrown = false;
\r
479 String[] c1 = new String[2];
\r
480 Char[] c2 = new Char[2];
\r
482 } catch (ArrayTypeMismatchException) {
\r
483 errorThrown = true;
\r
485 Assert("error not thrown", errorThrown);
\r
488 Char[] orig = {'a', 'b', 'c', 'd'};
\r
489 Char[] copy = new Char[10];
\r
490 Array.Clear(copy, 0, copy.Length);
\r
491 orig.CopyTo(copy, 3);
\r
492 AssertEquals("Wrong CopyTo 0", (char)0, copy[0]);
\r
493 AssertEquals("Wrong CopyTo 0", (char)0, copy[1]);
\r
494 AssertEquals("Wrong CopyTo 0", (char)0, copy[2]);
\r
495 AssertEquals("Wrong CopyTo 0", orig[0], copy[3]);
\r
496 AssertEquals("Wrong CopyTo 0", orig[1], copy[4]);
\r
497 AssertEquals("Wrong CopyTo 0", orig[2], copy[5]);
\r
498 AssertEquals("Wrong CopyTo 0", orig[3], copy[6]);
\r
499 AssertEquals("Wrong CopyTo 0", (char)0, copy[7]);
\r
500 AssertEquals("Wrong CopyTo 0", (char)0, copy[8]);
\r
501 AssertEquals("Wrong CopyTo 0", (char)0, copy[9]);
\r
504 public void TestCreateInstance() {
\r
506 bool errorThrown = false;
\r
508 Array.CreateInstance(null, 12);
\r
509 } catch (ArgumentNullException) {
\r
510 errorThrown = true;
\r
512 Assert("error not thrown", errorThrown);
\r
515 bool errorThrown = false;
\r
517 Array.CreateInstance(Type.GetType("System.Char"), -3);
\r
518 } catch (ArgumentOutOfRangeException) {
\r
519 errorThrown = true;
\r
521 Assert("error not thrown", errorThrown);
\r
524 char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
\r
525 AssertEquals("Array wrong size", 12, c1.Length);
\r
528 public void TestGetEnumerator() {
\r
529 String[] s1 = {"this", "is", "a", "test"};
\r
530 IEnumerator en = s1.GetEnumerator();
\r
531 AssertNotNull("No enumerator", en);
\r
533 for (int i = 0; i < s1.Length; i++) {
\r
535 AssertEquals("Not enumerating", s1[i], en.Current);
\r
539 public void TestGetLength() {
\r
541 bool errorThrown = false;
\r
543 char[] c1 = {'a', 'b', 'c'};
\r
545 } catch (IndexOutOfRangeException) {
\r
546 errorThrown = true;
\r
548 Assert("error not thrown", errorThrown);
\r
551 bool errorThrown = false;
\r
553 char[] c1 = {'a', 'b', 'c'};
\r
555 } catch (IndexOutOfRangeException) {
\r
556 errorThrown = true;
\r
558 Assert("error not thrown", errorThrown);
\r
561 char[] c2 = new Char[5];
\r
562 AssertEquals("wrong single dimension length",
\r
563 5, c2.GetLength(0));
\r
565 char[,] c3 = new Char[6,7];
\r
566 AssertEquals("wrong single dimension length",
\r
567 6, c3.GetLength(0));
\r
568 AssertEquals("wrong single dimension length",
\r
569 7, c3.GetLength(1));
\r
572 public void TestGetLowerBound() {
\r
573 // I have no idea what the point of this function is.
\r
575 bool errorThrown = false;
\r
577 char[] c = {'a', 'b', 'c'};
\r
578 c.GetLowerBound(-1);
\r
579 } catch (IndexOutOfRangeException) {
\r
580 errorThrown = true;
\r
582 Assert("error not thrown", errorThrown);
\r
585 bool errorThrown = false;
\r
587 char[] c = {'a', 'b', 'c'};
\r
588 c.GetLowerBound(1);
\r
589 } catch (IndexOutOfRangeException) {
\r
590 errorThrown = true;
\r
592 Assert("error not thrown", errorThrown);
\r
595 char[] c1 = new Char[5];
\r
596 AssertEquals("single-dimensional lower bound",
\r
597 0, c1.GetLowerBound(0));
\r
599 char[,] c2 = new Char[4,4];
\r
600 AssertEquals("multiple-dimensional lower bound",
\r
601 0, c2.GetLowerBound(0));
\r
602 AssertEquals("multiple-dimensional lower bound",
\r
603 0, c2.GetLowerBound(1));
\r
606 public void TestGetUpperBound() {
\r
608 bool errorThrown = false;
\r
610 char[] c = {'a', 'b', 'c'};
\r
611 c.GetUpperBound(-1);
\r
612 } catch (IndexOutOfRangeException) {
\r
613 errorThrown = true;
\r
615 Assert("error not thrown", errorThrown);
\r
618 bool errorThrown = false;
\r
620 char[] c = {'a', 'b', 'c'};
\r
621 c.GetUpperBound(1);
\r
622 } catch (IndexOutOfRangeException) {
\r
623 errorThrown = true;
\r
625 Assert("error not thrown", errorThrown);
\r
628 char[] c1 = new Char[5];
\r
629 AssertEquals("single-dimensional lower bound",
\r
630 4, c1.GetUpperBound(0));
\r
632 char[,] c2 = new Char[4,6];
\r
633 AssertEquals("multiple-dimensional lower bound",
\r
634 3, c2.GetUpperBound(0));
\r
635 AssertEquals("multiple-dimensional lower bound",
\r
636 5, c2.GetUpperBound(1));
\r
639 public void TestGetValue1() {
\r
641 bool errorThrown = false;
\r
643 char[,] c = new Char[2,2];
\r
645 } catch (ArgumentException) {
\r
646 errorThrown = true;
\r
648 Assert("error not thrown", errorThrown);
\r
651 bool errorThrown = false;
\r
653 char[] c = {'a', 'b', 'c'};
\r
655 } catch (IndexOutOfRangeException) {
\r
656 errorThrown = true;
\r
658 Assert("error not thrown", errorThrown);
\r
661 bool errorThrown = false;
\r
663 char[] c = {'a', 'b', 'c'};
\r
665 } catch (IndexOutOfRangeException) {
\r
666 errorThrown = true;
\r
668 Assert("error not thrown", errorThrown);
\r
671 char[] c1 = {'a', 'b', 'c', 'd'};
\r
672 for (int i = 0; i < c1.Length; i++) {
\r
673 AssertEquals("Bad GetValue", c1[i], c1.GetValue(i));
\r
676 public void TestGetValue2() {
\r
678 bool errorThrown = false;
\r
680 char[] c = new Char[2];
\r
682 } catch (ArgumentException) {
\r
683 errorThrown = true;
\r
685 Assert("error not thrown", errorThrown);
\r
688 bool errorThrown = false;
\r
690 char[,] c = new Char[2,2];
\r
692 } catch (IndexOutOfRangeException) {
\r
693 errorThrown = true;
\r
695 Assert("error not thrown", errorThrown);
\r
698 bool errorThrown = false;
\r
700 char[,] c = new Char[2,2];
\r
702 } catch (IndexOutOfRangeException) {
\r
703 errorThrown = true;
\r
705 Assert("error not thrown", errorThrown);
\r
708 char[,] c1 = new Char[4,6];
\r
709 for (int i = 0; i < 24; i++) {
\r
711 int second = i % 6;
\r
712 c1[first,second] = (char)(((int)'a')+i);
\r
714 for (int i = 0; i < c1.GetLength(0); i++) {
\r
715 for (int j = 0; j < c1.GetLength(1); j++) {
\r
716 AssertEquals("Bad GetValue",
\r
717 c1[i,j], c1.GetValue(i, j));
\r
721 public void TestGetValue3() {
\r
723 bool errorThrown = false;
\r
725 char[] c = new Char[2];
\r
727 } catch (ArgumentException) {
\r
728 errorThrown = true;
\r
730 Assert("error not thrown", errorThrown);
\r
733 bool errorThrown = false;
\r
735 char[,,] c = new Char[2,2,2];
\r
736 c.GetValue(-1, 1, 1);
\r
737 } catch (IndexOutOfRangeException) {
\r
738 errorThrown = true;
\r
740 Assert("error not thrown", errorThrown);
\r
743 bool errorThrown = false;
\r
745 char[,,] c = new Char[2,2,2];
\r
747 } catch (IndexOutOfRangeException) {
\r
748 errorThrown = true;
\r
750 Assert("error not thrown", errorThrown);
\r
753 char[,,] c1 = new Char[4,2,3];
\r
754 for (int i = 0; i < 24; i++) {
\r
756 int remains = i % 6;
\r
757 int second = remains / 3;
\r
758 int third = remains % 3;
\r
759 c1[first,second, third] = (char)(((int)'a')+i);
\r
761 for (int i = 0; i < c1.GetLength(0); i++) {
\r
762 for (int j = 0; j < c1.GetLength(1); j++) {
\r
763 for (int k = 0; k < c1.GetLength(2); k++) {
\r
764 AssertEquals("Bad GetValue",
\r
765 c1[i,j,k], c1.GetValue(i,j,k));
\r
770 public void TestGetValueN() {
\r
772 bool errorThrown = false;
\r
774 char[] c = new Char[2];
\r
776 } catch (ArgumentNullException) {
\r
777 errorThrown = true;
\r
779 Assert("error not thrown", errorThrown);
\r
782 bool errorThrown = false;
\r
784 char[] c = new Char[2];
\r
785 int[] coords = {1, 1};
\r
786 c.GetValue(coords);
\r
787 } catch (ArgumentException) {
\r
788 errorThrown = true;
\r
790 Assert("error not thrown", errorThrown);
\r
793 bool errorThrown = false;
\r
795 char[,] c = new Char[2,2];
\r
796 int[] coords = {-1, 1};
\r
797 c.GetValue(coords);
\r
798 } catch (IndexOutOfRangeException) {
\r
799 errorThrown = true;
\r
801 Assert("error not thrown", errorThrown);
\r
804 bool errorThrown = false;
\r
806 char[,] c = new Char[2,2];
\r
807 int[] coords = {4, 1};
\r
808 c.GetValue(coords);
\r
809 } catch (IndexOutOfRangeException) {
\r
810 errorThrown = true;
\r
812 Assert("error not thrown", errorThrown);
\r
815 char[,] c1 = new Char[4,6];
\r
816 for (int i = 0; i < 24; i++) {
\r
818 int second = i % 6;
\r
819 c1[first,second] = (char)(((int)'a')+i);
\r
821 for (int i = 0; i < c1.GetLength(0); i++) {
\r
822 for (int j = 0; j < c1.GetLength(1); j++) {
\r
823 int[] coords = {i, j};
\r
824 AssertEquals("Bad GetValue",
\r
825 c1[i,j], c1.GetValue(coords));
\r
830 public void TestIndexOf1() {
\r
832 bool errorThrown = false;
\r
834 Array.IndexOf(null, "huh?");
\r
835 } catch (ArgumentNullException) {
\r
836 errorThrown = true;
\r
838 Assert("error not thrown", errorThrown);
\r
841 bool errorThrown = false;
\r
843 char[,] c = new Char[2,2];
\r
844 Array.IndexOf(c, "huh?");
\r
845 } catch (RankException) {
\r
846 errorThrown = true;
\r
848 Assert("error not thrown", errorThrown);
\r
851 String[] s1 = {"this", "is", "a", "test"};
\r
852 AssertEquals("No null here", -1, Array.IndexOf(s1, null));
\r
853 AssertEquals("No nothing here", -1, Array.IndexOf(s1, "nothing"));
\r
854 AssertEquals("Found first", 0, Array.IndexOf(s1, "this"));
\r
855 AssertEquals("Found last", 3, Array.IndexOf(s1, "test"));
\r
857 public void TestIndexOf2() {
\r
859 bool errorThrown = false;
\r
861 Array.IndexOf(null, "huh?", 0);
\r
862 } catch (ArgumentNullException) {
\r
863 errorThrown = true;
\r
865 Assert("error not thrown", errorThrown);
\r
868 bool errorThrown = false;
\r
870 char[,] c = new Char[2,2];
\r
871 Array.IndexOf(c, "huh?", 0);
\r
872 } catch (RankException) {
\r
873 errorThrown = true;
\r
875 Assert("error not thrown", errorThrown);
\r
878 bool errorThrown = false;
\r
880 char[] c = new Char[2];
\r
881 Array.IndexOf(c, "huh?", 3);
\r
882 } catch (ArgumentOutOfRangeException) {
\r
883 errorThrown = true;
\r
885 Assert("error not thrown", errorThrown);
\r
888 String[] s1 = {"this", "is", "really", "a", "test"};
\r
889 AssertEquals("No null here", -1, Array.IndexOf(s1, null, 1));
\r
890 AssertEquals("No nothing here", -1, Array.IndexOf(s1, "nothing", 1));
\r
891 AssertEquals("Didn't find first", -1, Array.IndexOf(s1, "this", 1));
\r
892 AssertEquals("Found first", 1, Array.IndexOf(s1, "is", 1));
\r
893 AssertEquals("Found last", 4, Array.IndexOf(s1, "test", 1));
\r
895 public void TestIndexOf3() {
\r
897 bool errorThrown = false;
\r
899 Array.IndexOf(null, "huh?", 0, 1);
\r
900 } catch (ArgumentNullException) {
\r
901 errorThrown = true;
\r
903 Assert("error not thrown", errorThrown);
\r
906 bool errorThrown = false;
\r
908 char[,] c = new Char[2,2];
\r
909 Array.IndexOf(c, "huh?", 0, 1);
\r
910 } catch (RankException) {
\r
911 errorThrown = true;
\r
913 Assert("error not thrown", errorThrown);
\r
916 bool errorThrown = false;
\r
918 char[] c = new Char[2];
\r
919 Array.IndexOf(c, "huh?", 3, 1);
\r
920 } catch (ArgumentOutOfRangeException) {
\r
921 errorThrown = true;
\r
923 Assert("error not thrown", errorThrown);
\r
926 bool errorThrown = false;
\r
928 char[] c = new Char[2];
\r
929 Array.IndexOf(c, "huh?", 0, 5);
\r
930 } catch (ArgumentOutOfRangeException) {
\r
931 errorThrown = true;
\r
933 Assert("error not thrown", errorThrown);
\r
936 String[] s1 = {"this", "is", "really", "a", "test"};
\r
937 AssertEquals("No null here", -1, Array.IndexOf(s1, null, 1, 3));
\r
938 AssertEquals("No nothing here", -1, Array.IndexOf(s1, "nothing", 1, 3));
\r
939 AssertEquals("Didn't find first", -1, Array.IndexOf(s1, "this", 1, 3));
\r
940 AssertEquals("Found first", 1, Array.IndexOf(s1, "is", 1, 3));
\r
941 AssertEquals("Didn't find last", -1, Array.IndexOf(s1, "test", 1, 3));
\r
942 AssertEquals("Found last", 3, Array.IndexOf(s1, "a", 1, 3));
\r
945 public void TestLastIndexOf1() {
\r
947 bool errorThrown = false;
\r
949 Array.LastIndexOf(null, "huh?");
\r
950 } catch (ArgumentNullException) {
\r
951 errorThrown = true;
\r
953 Assert("error not thrown", errorThrown);
\r
956 bool errorThrown = false;
\r
958 char[,] c = new Char[2,2];
\r
959 Array.LastIndexOf(c, "huh?");
\r
960 } catch (RankException) {
\r
961 errorThrown = true;
\r
963 Assert("error not thrown", errorThrown);
\r
966 String[] s1 = {"this", "is", "a", "a", "test"};
\r
967 AssertEquals("No null here", -1, Array.LastIndexOf(s1, null));
\r
968 AssertEquals("No nothing here", -1, Array.LastIndexOf(s1, "nothing"));
\r
969 AssertEquals("Found first", 0, Array.LastIndexOf(s1, "this"));
\r
970 AssertEquals("Found last", 4, Array.LastIndexOf(s1, "test"));
\r
971 AssertEquals("Found repeat", 3, Array.LastIndexOf(s1, "a"));
\r
973 public void TestLastIndexOf2() {
\r
975 bool errorThrown = false;
\r
977 Array.LastIndexOf(null, "huh?", 0);
\r
978 } catch (ArgumentNullException) {
\r
979 errorThrown = true;
\r
981 Assert("error not thrown", errorThrown);
\r
984 bool errorThrown = false;
\r
986 char[,] c = new Char[2,2];
\r
987 Array.LastIndexOf(c, "huh?", 0);
\r
988 } catch (RankException) {
\r
989 errorThrown = true;
\r
991 Assert("error not thrown", errorThrown);
\r
994 bool errorThrown = false;
\r
996 char[] c = new Char[2];
\r
997 Array.LastIndexOf(c, "huh?", 3);
\r
998 } catch (ArgumentOutOfRangeException) {
\r
999 errorThrown = true;
\r
1001 Assert("error not thrown", errorThrown);
\r
1004 String[] s1 = {"this", "is", "really", "a", "test"};
\r
1005 AssertEquals("No null here", -1, Array.LastIndexOf(s1, null, 3));
\r
1006 AssertEquals("No nothing here", -1, Array.LastIndexOf(s1, "nothing", 3));
\r
1007 AssertEquals("Didn't find larst", -1, Array.LastIndexOf(s1, "test", 3));
\r
1008 AssertEquals("Found last", 3, Array.LastIndexOf(s1, "a", 3));
\r
1009 AssertEquals("Found first", 0, Array.LastIndexOf(s1, "this", 3));
\r
1011 public void TestLastIndexOf3() {
\r
1013 bool errorThrown = false;
\r
1015 Array.LastIndexOf(null, "huh?", 0, 1);
\r
1016 } catch (ArgumentNullException) {
\r
1017 errorThrown = true;
\r
1019 Assert("error not thrown", errorThrown);
\r
1022 bool errorThrown = false;
\r
1024 char[,] c = new Char[2,2];
\r
1025 Array.LastIndexOf(c, "huh?", 0, 1);
\r
1026 } catch (RankException) {
\r
1027 errorThrown = true;
\r
1029 Assert("error not thrown", errorThrown);
\r
1032 bool errorThrown = false;
\r
1034 char[] c = new Char[2];
\r
1035 Array.LastIndexOf(c, "huh?", 3, 1);
\r
1036 } catch (ArgumentOutOfRangeException) {
\r
1037 errorThrown = true;
\r
1039 Assert("error not thrown", errorThrown);
\r
1042 bool errorThrown = false;
\r
1044 char[] c = new Char[2];
\r
1045 Array.LastIndexOf(c, "huh?", 0, 5);
\r
1046 } catch (ArgumentOutOfRangeException) {
\r
1047 errorThrown = true;
\r
1049 Assert("error not thrown", errorThrown);
\r
1052 String[] s1 = {"this", "is", "really", "a", "test"};
\r
1053 AssertEquals("No null here",
\r
1054 -1, Array.LastIndexOf(s1, null, 3, 3));
\r
1055 AssertEquals("No nothing here",
\r
1056 -1, Array.LastIndexOf(s1, "nothing", 3, 3));
\r
1057 AssertEquals("Didn't find first",
\r
1058 -1, Array.LastIndexOf(s1, "this", 3, 3));
\r
1059 AssertEquals("Found first",
\r
1060 1, Array.LastIndexOf(s1, "is", 3, 3));
\r
1061 AssertEquals("Didn't find last",
\r
1062 -1, Array.LastIndexOf(s1, "test", 3, 3));
\r
1063 AssertEquals("Found last",
\r
1064 3, Array.LastIndexOf(s1, "a", 3, 3));
\r
1067 public void TestReverse() {
\r
1069 bool errorThrown = false;
\r
1071 Array.Reverse(null);
\r
1072 } catch (ArgumentNullException) {
\r
1073 errorThrown = true;
\r
1075 Assert("error not thrown", errorThrown);
\r
1078 bool errorThrown = false;
\r
1080 char[,] c = new Char[2,2];
\r
1082 } catch (RankException) {
\r
1083 errorThrown = true;
\r
1085 Assert("error not thrown", errorThrown);
\r
1088 char[] c1 = {'a', 'b', 'c', 'd'};
\r
1089 Array.Reverse(c1);
\r
1090 AssertEquals("Reverse not working", 'd', c1[0]);
\r
1091 AssertEquals("Reverse not working", 'c', c1[1]);
\r
1092 AssertEquals("Reverse not working", 'b', c1[2]);
\r
1093 AssertEquals("Reverse not working", 'a', c1[3]);
\r
1096 bool errorThrown = false;
\r
1098 Array.Reverse(null, 0, 0);
\r
1099 } catch (ArgumentNullException) {
\r
1100 errorThrown = true;
\r
1102 Assert("error not thrown", errorThrown);
\r
1105 bool errorThrown = false;
\r
1107 char[,] c = new Char[2,2];
\r
1108 Array.Reverse(c, 0, 0);
\r
1109 } catch (RankException) {
\r
1110 errorThrown = true;
\r
1112 Assert("error not thrown", errorThrown);
\r
1115 //bool errorThrown = false;
\r
1117 // char[] c = new Char[2];
\r
1118 // Array.Reverse(c, 0, 3);
\r
1119 //} catch (ArgumentOutOfRangeException) {
\r
1120 // errorThrown = true;
\r
1122 //Assert("error not thrown", errorThrown);
\r
1125 //bool errorThrown = false;
\r
1127 // char[] c = new Char[2];
\r
1128 // Array.Reverse(c, 3, 0);
\r
1129 //} catch (ArgumentOutOfRangeException) {
\r
1130 // errorThrown = true;
\r
1132 //Assert("error not thrown", errorThrown);
\r
1135 char[] c2 = { 'a', 'b', 'c', 'd'};
\r
1136 Array.Reverse(c2, 1, 2);
\r
1137 AssertEquals("Reverse not working", 'a', c2[0]);
\r
1138 AssertEquals("Reverse not working", 'c', c2[1]);
\r
1139 AssertEquals("Reverse not working", 'b', c2[2]);
\r
1140 AssertEquals("Reverse not working", 'd', c2[3]);
\r
1143 public void TestSetValue1() {
\r
1145 bool errorThrown = false;
\r
1147 char[,] c = new Char[2,2];
\r
1148 c.SetValue("buh", 1);
\r
1149 } catch (ArgumentException) {
\r
1150 errorThrown = true;
\r
1152 Assert("error not thrown", errorThrown);
\r
1155 bool errorThrown = false;
\r
1157 char[] c = {'a', 'b', 'c'};
\r
1158 c.SetValue("buh", -1);
\r
1159 } catch (IndexOutOfRangeException) {
\r
1160 errorThrown = true;
\r
1162 Assert("error not thrown", errorThrown);
\r
1165 bool errorThrown = false;
\r
1167 char[] c = {'a', 'b', 'c'};
\r
1168 c.SetValue("buh", 4);
\r
1169 } catch (IndexOutOfRangeException) {
\r
1170 errorThrown = true;
\r
1172 Assert("error not thrown", errorThrown);
\r
1175 char[] c1 = {'a', 'b', 'c', 'd'};
\r
1176 char[] c2 = new char[4];
\r
1177 for (int i = 0; i < c1.Length; i++) {
\r
1178 c2.SetValue(c1[i], i);
\r
1180 for (int i = 0; i < c1.Length; i++) {
\r
1181 AssertEquals("Bad SetValue", c1[i], c2[i]);
\r
1184 public void TestSetValue2() {
\r
1186 bool errorThrown = false;
\r
1188 char[] c = new Char[2];
\r
1189 c.SetValue("buh", 1,1);
\r
1190 } catch (ArgumentException) {
\r
1191 errorThrown = true;
\r
1193 Assert("error not thrown", errorThrown);
\r
1196 bool errorThrown = false;
\r
1198 char[,] c = new Char[2,2];
\r
1199 c.SetValue("buh", -1, 1);
\r
1200 } catch (IndexOutOfRangeException) {
\r
1201 errorThrown = true;
\r
1203 Assert("error not thrown", errorThrown);
\r
1206 bool errorThrown = false;
\r
1208 char[,] c = new Char[2,2];
\r
1209 c.SetValue("buh", 4,1);
\r
1210 } catch (IndexOutOfRangeException) {
\r
1211 errorThrown = true;
\r
1213 Assert("error not thrown", errorThrown);
\r
1216 char[,] c1 = new Char[4,6];
\r
1217 char[,] c2 = new Char[4,6];
\r
1218 for (int i = 0; i < 24; i++) {
\r
1219 int first = i / 6;
\r
1220 int second = i % 6;
\r
1221 c1[first,second] = (char)(((int)'a')+i);
\r
1222 c2.SetValue(c1[first,second], first, second);
\r
1224 for (int i = 0; i < c1.GetLength(0); i++) {
\r
1225 for (int j = 0; j < c1.GetLength(1); j++) {
\r
1226 AssertEquals("Bad SetValue",
\r
1227 c1[i,j], c2[i, j]);
\r
1231 public void TestSetValue3() {
\r
1233 bool errorThrown = false;
\r
1235 char[] c = new Char[2];
\r
1236 c.SetValue("buh", 1,1,1);
\r
1237 } catch (ArgumentException) {
\r
1238 errorThrown = true;
\r
1240 Assert("error not thrown", errorThrown);
\r
1243 bool errorThrown = false;
\r
1245 char[,,] c = new Char[2,2,2];
\r
1246 c.SetValue("buh", -1, 1, 1);
\r
1247 } catch (IndexOutOfRangeException) {
\r
1248 errorThrown = true;
\r
1250 Assert("error not thrown", errorThrown);
\r
1253 bool errorThrown = false;
\r
1255 char[,,] c = new Char[2,2,2];
\r
1256 c.SetValue("buh", 4,1,1);
\r
1257 } catch (IndexOutOfRangeException) {
\r
1258 errorThrown = true;
\r
1260 Assert("error not thrown", errorThrown);
\r
1263 char[,,] c1 = new Char[4,2,3];
\r
1264 char[,,] c2 = new Char[4,2,3];
\r
1265 for (int i = 0; i < 24; i++) {
\r
1266 int first = i / 6;
\r
1267 int remains = i % 6;
\r
1268 int second = remains / 3;
\r
1269 int third = remains % 3;
\r
1270 c1[first,second, third] = (char)(((int)'a')+i);
\r
1271 c2.SetValue(c1[first, second, third], first, second, third);
\r
1273 for (int i = 0; i < c1.GetLength(0); i++) {
\r
1274 for (int j = 0; j < c1.GetLength(1); j++) {
\r
1275 for (int k = 0; k < c1.GetLength(2); k++) {
\r
1276 AssertEquals("Bad SetValue",
\r
1277 c1[i,j,k], c2[i,j,k]);
\r
1282 public void TestSetValueN() {
\r
1284 bool errorThrown = false;
\r
1286 char[] c = new Char[2];
\r
1287 c.SetValue("buh", null);
\r
1288 } catch (ArgumentNullException) {
\r
1289 errorThrown = true;
\r
1291 Assert("error not thrown", errorThrown);
\r
1294 bool errorThrown = false;
\r
1296 char[] c = new Char[2];
\r
1297 int[] coords = {1, 1};
\r
1298 c.SetValue("buh", coords);
\r
1299 } catch (ArgumentException) {
\r
1300 errorThrown = true;
\r
1302 Assert("error not thrown", errorThrown);
\r
1305 bool errorThrown = false;
\r
1307 char[,] c = new Char[2,2];
\r
1308 int[] coords = {-1, 1};
\r
1309 c.SetValue("buh", coords);
\r
1310 } catch (IndexOutOfRangeException) {
\r
1311 errorThrown = true;
\r
1313 Assert("error not thrown", errorThrown);
\r
1316 bool errorThrown = false;
\r
1318 char[,] c = new Char[2,2];
\r
1319 int[] coords = {4, 1};
\r
1320 c.SetValue("buh", coords);
\r
1321 } catch (IndexOutOfRangeException) {
\r
1322 errorThrown = true;
\r
1324 Assert("error not thrown", errorThrown);
\r
1327 char[,] c1 = new Char[4,6];
\r
1328 char[,] c2 = new Char[4,6];
\r
1329 for (int i = 0; i < 24; i++) {
\r
1330 int first = i / 6;
\r
1331 int second = i % 6;
\r
1332 c1[first,second] = (char)(((int)'a')+i);
\r
1333 int[] coords = {first, second};
\r
1334 c2.SetValue(c1[first,second], coords);
\r
1336 for (int i = 0; i < c1.GetLength(0); i++) {
\r
1337 for (int j = 0; j < c1.GetLength(1); j++) {
\r
1338 AssertEquals("Bad SetValue",
\r
1339 c1[i,j], c2[i,j]);
\r
1344 public void TestSort() {
\r
1346 bool errorThrown = false;
\r
1349 } catch (ArgumentNullException) {
\r
1350 errorThrown = true;
\r
1352 Assert("error not thrown 1", errorThrown);
\r
1355 bool errorThrown = false;
\r
1357 Array.Sort(null, 0, 1);
\r
1358 } catch (ArgumentNullException) {
\r
1359 errorThrown = true;
\r
1361 Assert("error not thrown 2", errorThrown);
\r
1364 bool errorThrown = false;
\r
1366 char[] c1 = new Char[2];
\r
1367 Array.Sort(null, c1);
\r
1368 } catch (ArgumentNullException) {
\r
1369 errorThrown = true;
\r
1371 Assert("error not thrown 5", errorThrown);
\r
1374 bool errorThrown = false;
\r
1376 char[] c1 = new Char[2];
\r
1377 Array.Sort(null, c1, 0, 1);
\r
1378 } catch (ArgumentNullException) {
\r
1379 errorThrown = true;
\r
1381 Assert("error not thrown 6", errorThrown);
\r
1384 // note: null second array => just sort first array
\r
1385 char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
\r
1386 int[] starter1 = {1,2,3,4,5,6};
\r
1388 char[] c1 = (char[])starter.Clone();
\r
1390 AssertEquals("Basic sort problem", 'a', c1[0]);
\r
1391 AssertEquals("Basic sort problem", 'b', c1[1]);
\r
1392 AssertEquals("Basic sort problem", 'c', c1[2]);
\r
1393 AssertEquals("Basic sort problem", 'd', c1[3]);
\r
1394 AssertEquals("Basic sort problem", 'e', c1[4]);
\r
1395 AssertEquals("Basic sort problem", 'f', c1[5]);
\r
1398 char[] c1 = (char[])starter.Clone();
\r
1399 int[] i1 = (int[])starter1.Clone();
\r
1400 Array.Sort(c1, i1);
\r
1401 AssertEquals("Keyed sort problem", 'a', c1[0]);
\r
1402 AssertEquals("Keyed sort problem", 'b', c1[1]);
\r
1403 AssertEquals("Keyed sort problem", 'c', c1[2]);
\r
1404 AssertEquals("Keyed sort problem", 'd', c1[3]);
\r
1405 AssertEquals("Keyed sort problem", 'e', c1[4]);
\r
1406 AssertEquals("Keyed sort problem", 'f', c1[5]);
\r
1407 AssertEquals("Keyed sort problem", 5, i1[0]);
\r
1408 AssertEquals("Keyed sort problem", 2, i1[1]);
\r
1409 AssertEquals("Keyed sort problem", 6, i1[2]);
\r
1410 AssertEquals("Keyed sort problem", 1, i1[3]);
\r
1411 AssertEquals("Keyed sort problem", 4, i1[4]);
\r
1412 AssertEquals("Keyed sort problem", 3, i1[5]);
\r
1415 char[] c1 = (char[])starter.Clone();
\r
1416 Array.Sort(c1, 1, 4);
\r
1417 AssertEquals("Basic sort chunk problem", 'd', c1[0]);
\r
1418 AssertEquals("Basic sort chunk problem", 'a', c1[1]);
\r
1419 AssertEquals("Basic sort chunk problem", 'b', c1[2]);
\r
1420 AssertEquals("Basic sort chunk problem", 'e', c1[3]);
\r
1421 AssertEquals("Basic sort chunk problem", 'f', c1[4]);
\r
1422 AssertEquals("Basic sort chunk problem", 'c', c1[5]);
\r
1425 char[] c1 = (char[])starter.Clone();
\r
1426 int[] i1 = (int[])starter1.Clone();
\r
1427 Array.Sort(c1, i1, 1, 4);
\r
1428 AssertEquals("Keyed sort chunk problem", 'd', c1[0]);
\r
1429 AssertEquals("Keyed sort chunk problem", 'a', c1[1]);
\r
1430 AssertEquals("Keyed sort chunk problem", 'b', c1[2]);
\r
1431 AssertEquals("Keyed sort chunk problem", 'e', c1[3]);
\r
1432 AssertEquals("Keyed sort chunk problem", 'f', c1[4]);
\r
1433 AssertEquals("Keyed sort chunk problem", 'c', c1[5]);
\r
1434 AssertEquals("Keyed sort chunk problem", 1, i1[0]);
\r
1435 AssertEquals("Keyed sort chunk problem", 5, i1[1]);
\r
1436 AssertEquals("Keyed sort chunk problem", 2, i1[2]);
\r
1437 AssertEquals("Keyed sort chunk problem", 4, i1[3]);
\r
1438 AssertEquals("Keyed sort chunk problem", 3, i1[4]);
\r
1439 AssertEquals("Keyed sort chunk problem", 6, i1[5]);
\r
1443 // TODO - TestSort passed-in IComparable versions
\r