1 // ArrayListTest.cs - NUnit Test Cases for the System.Collections.ArrayList class
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc.  http://www.ximian.com
6 // Copyright (C) 2005 Novell (http://www.novell.com)
7 //
8 
9 using System;
10 using System.Collections;
11 
12 using NUnit.Framework;
13 
14 namespace MonoTests.System.Collections
15 {
16 	[TestFixture]
17 	public class ArrayListTest
18 	{
19 		[Test]
TestCtor()20 		public void TestCtor ()
21 		{
22 			{
23 				ArrayList al1 = new ArrayList ();
24 				Assert.IsNotNull (al1, "no basic ArrayList");
25 			}
26 			{
27 				bool errorThrown = false;
28 				try {
29 					ArrayList a = new ArrayList (null);
30 				} catch (ArgumentNullException) {
31 					errorThrown = true;
32 				}
33 				Assert.IsTrue (errorThrown, "null icollection error not thrown");
34 			}
35 			{
36 				// what can I say?  I like chars.  [--DB]
37 				char [] coll = { 'a', 'b', 'c', 'd' };
38 				ArrayList al1 = new ArrayList (coll);
39 				Assert.IsNotNull (al1, "no icollection ArrayList");
40 				for (int i = 0; i < coll.Length; i++) {
41 					Assert.AreEqual (coll [i], al1 [i], i + " not ctor'ed properly.");
42 				}
43 			}
44 			{
45 				try {
46 					Char [,] c1 = new Char [2, 2];
47 					ArrayList al1 = new ArrayList (c1);
48 					Assert.Fail ("Should fail with multi-dimensional array in constructor.");
49 				} catch (RankException) {
50 				}
51 			}
52 
53 			{
54 				bool errorThrown = false;
55 				try {
56 					ArrayList a = new ArrayList (-1);
57 				} catch (ArgumentOutOfRangeException) {
58 					errorThrown = true;
59 				}
60 				Assert.IsTrue (errorThrown, "negative capacity error not thrown");
61 			}
62 		}
63 
64 		[Test]
TestCapacity()65 		public void TestCapacity ()
66 		{
67 		int default_capacity = 4;
68 		int unspecified_capacity = 0;
69 			for (int i = 1; i < 100; i++) {
70 				ArrayList al1 = new ArrayList (i);
71 				Assert.AreEqual (i, al1.Capacity, "Bad capacity of " + i);
72 			}
73 			{
74 				ArrayList al1 = new ArrayList (0);
75 				// LAMESPEC:
76 				// Assert.AreEqual (//	     16, al1.Capacity, "Bad capacity when set to 0");
77 				al1.Add ("?");
78 				Assert.AreEqual (default_capacity, al1.Capacity, "Bad capacity when set to 0");
79 			}
80 			{
81 				ArrayList al1 = new ArrayList ();
82 				Assert.AreEqual (unspecified_capacity, al1.Capacity, "Bad default capacity");
83 			}
84 		}
85 
86 		[Test]
TestCount()87 		public void TestCount ()
88 		{
89 			{
90 				ArrayList al1 = new ArrayList ();
91 				Assert.AreEqual (0, al1.Count, "Bad initial count");
92 				for (int i = 1; i <= 100; i++) {
93 					al1.Add (i);
94 					Assert.AreEqual (i, al1.Count, "Bad count " + i);
95 				}
96 			}
97 			for (int i = 0; i < 100; i++) {
98 				char [] coll = new Char [i];
99 				ArrayList al1 = new ArrayList (coll);
100 				Assert.AreEqual (i, al1.Count, "Bad count for " + i);
101 			}
102 		}
103 
104 		[Test]
TestIsFixed()105 		public void TestIsFixed ()
106 		{
107 			ArrayList al1 = new ArrayList ();
108 			Assert.IsTrue (!al1.IsFixedSize, "should not be fixed by default");
109 			ArrayList al2 = ArrayList.FixedSize (al1);
110 			Assert.IsTrue (al2.IsFixedSize, "fixed-size wrapper not working");
111 		}
112 
113 		[Test]
TestIsReadOnly()114 		public void TestIsReadOnly ()
115 		{
116 			ArrayList al1 = new ArrayList ();
117 			Assert.IsTrue (!al1.IsReadOnly, "should not be ReadOnly by default");
118 			ArrayList al2 = ArrayList.ReadOnly (al1);
119 			Assert.IsTrue (al2.IsReadOnly, "read-only wrapper not working");
120 		}
121 
122 		[Test]
TestIsSynchronized()123 		public void TestIsSynchronized ()
124 		{
125 			ArrayList al1 = new ArrayList ();
126 			Assert.IsTrue (!al1.IsSynchronized, "should not be synchronized by default");
127 			ArrayList al2 = ArrayList.Synchronized (al1);
128 			Assert.IsTrue (al2.IsSynchronized, "synchronized wrapper not working");
129 		}
130 
131 		[Test]
TestItem()132 		public void TestItem ()
133 		{
134 			ArrayList al1 = new ArrayList ();
135 			{
136 				bool errorThrown = false;
137 				try {
138 					object o = al1 [-1];
139 				} catch (ArgumentOutOfRangeException) {
140 					errorThrown = true;
141 				}
142 				Assert.IsTrue (errorThrown, "negative item error not thrown");
143 			}
144 			{
145 				bool errorThrown = false;
146 				try {
147 					object o = al1 [1];
148 				} catch (ArgumentOutOfRangeException) {
149 					errorThrown = true;
150 				}
151 				Assert.IsTrue (errorThrown, "past-end item error not thrown");
152 			}
153 			for (int i = 0; i <= 100; i++) {
154 				al1.Add (i);
155 			}
156 			for (int i = 0; i <= 100; i++) {
157 				Assert.AreEqual (i, al1 [i], "item not fetched for " + i);
158 			}
159 		}
160 
161 		[Test]
TestAdapter()162 		public void TestAdapter ()
163 		{
164 			{
165 				bool errorThrown = false;
166 				try {
167 					ArrayList al1 = ArrayList.Adapter (null);
168 				} catch (ArgumentNullException) {
169 					errorThrown = true;
170 				} catch (Exception e) {
171 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
172 				}
173 				Assert.IsTrue (errorThrown, "null adapter error not thrown");
174 			}
175 			{
176 				char [] list = { 'a', 'b', 'c', 'd' };
177 				ArrayList al1 = ArrayList.Adapter (list);
178 				Assert.IsNotNull (al1, "Couldn't get an adapter");
179 				for (int i = 0; i < list.Length; i++) {
180 					Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
181 				}
182 				list [0] = 'z';
183 				for (int i = 0; i < list.Length; i++) {
184 					Assert.AreEqual (list [i], al1 [i], "adapter not adapting");
185 				}
186 			}
187 			// Test Binary Search
188 			{
189 				bool errorThrown = false;
190 				try {
191 
192 					String [] s1 = { "This", "is", "a", "test" };
193 					ArrayList al1 = ArrayList.Adapter (s1);
194 					al1.BinarySearch (42);
195 				} catch (InvalidOperationException) {
196 					// this is what .NET throws
197 					errorThrown = true;
198 				} catch (ArgumentException) {
199 					// this is what the docs say it should throw
200 					errorThrown = true;
201 				} catch (Exception e) {
202 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
203 				}
204 				Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
205 			}
206 
207 			{
208 				char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
209 				ArrayList al1 = ArrayList.Adapter (arr);
210 				Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
211 				Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
212 			}
213 			{
214 				char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
215 				ArrayList al1 = ArrayList.Adapter (arr);
216 				Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
217 			}
218 			{
219 				char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
220 				ArrayList al1 = ArrayList.Adapter (arr);
221 				Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
222 			}
223 			// Sort
224 			{
225 				char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
226 				ArrayList al1 = ArrayList.Adapter (starter);
227 				al1.Sort ();
228 				Assert.AreEqual ('a', al1 [0], "Should be sorted");
229 				Assert.AreEqual ('b', al1 [1], "Should be sorted");
230 				Assert.AreEqual ('c', al1 [2], "Should be sorted");
231 				Assert.AreEqual ('d', al1 [3], "Should be sorted");
232 				Assert.AreEqual ('e', al1 [4], "Should be sorted");
233 				Assert.AreEqual ('f', al1 [5], "Should be sorted");
234 			}
235 
236 			// TODO - test other adapter types?
237 		}
238 
239 		[Test]
TestAdd()240 		public void TestAdd ()
241 		{
242 			{
243 				bool errorThrown = false;
244 				try {
245 					ArrayList al1 =
246 						ArrayList.FixedSize (new ArrayList ());
247 					al1.Add ("Hi!");
248 				} catch (NotSupportedException) {
249 					errorThrown = true;
250 				} catch (Exception e) {
251 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
252 				}
253 				Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
254 			}
255 			{
256 				bool errorThrown = false;
257 				try {
258 					ArrayList al1 =
259 						ArrayList.ReadOnly (new ArrayList ());
260 					al1.Add ("Hi!");
261 				} catch (NotSupportedException) {
262 					errorThrown = true;
263 				} catch (Exception e) {
264 					Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
265 				}
266 				Assert.IsTrue (errorThrown, "add to read only error not thrown");
267 			}
268 			{
269 				ArrayList al1 = new ArrayList ();
270 				for (int i = 1; i <= 100; i++) {
271 					al1.Add (i);
272 					Assert.AreEqual (i, al1.Count, "add failed " + i);
273 					Assert.AreEqual (i, al1 [i - 1], "add failed " + i);
274 
275 				}
276 			}
277 			{
278 				string [] strArray = new string [] { };
279 				ArrayList al1 = new ArrayList (strArray);
280 				al1.Add ("Hi!");
281 				al1.Add ("Hi!");
282 				Assert.AreEqual (2, al1.Count, "add failed");
283 			}
284 		}
285 
286 		[Test]
TestAddRange()287 		public void TestAddRange ()
288 		{
289 			{
290 				bool errorThrown = false;
291 				try {
292 					ArrayList al1 =
293 						ArrayList.FixedSize (new ArrayList ());
294 					String [] s1 = { "Hi!" };
295 					al1.AddRange (s1);
296 				} catch (NotSupportedException) {
297 					errorThrown = true;
298 				} catch (Exception e) {
299 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
300 				}
301 				Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
302 			}
303 			{
304 				bool errorThrown = false;
305 				try {
306 					ArrayList al1 =
307 						ArrayList.ReadOnly (new ArrayList ());
308 					String [] s1 = { "Hi!" };
309 					al1.AddRange (s1);
310 				} catch (NotSupportedException) {
311 					errorThrown = true;
312 				} catch (Exception e) {
313 					Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
314 				}
315 				Assert.IsTrue (errorThrown, "add to read only error not thrown");
316 			}
317 			{
318 				bool errorThrown = false;
319 				try {
320 					ArrayList al1 = new ArrayList ();
321 					al1.AddRange (null);
322 				} catch (ArgumentNullException) {
323 					errorThrown = true;
324 				} catch (Exception e) {
325 					Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
326 				}
327 				Assert.IsTrue (errorThrown, "add to read only error not thrown");
328 			}
329 
330 			{
331 				ArrayList a1 = new ArrayList ();
332 				Assert.AreEqual (0, a1.Count, "ArrayList should start empty");
333 				char [] coll = { 'a', 'b', 'c' };
334 				a1.AddRange (coll);
335 				Assert.AreEqual (3, a1.Count, "ArrayList has wrong elements");
336 				a1.AddRange (coll);
337 				Assert.AreEqual (6, a1.Count, "ArrayList has wrong elements");
338 			}
339 
340 			{
341 				ArrayList list = new ArrayList ();
342 
343 				for (int i = 0; i < 100; i++) {
344 					list.Add (1);
345 				}
346 
347 				Assert.AreEqual (49, list.BinarySearch (1), "BinarySearch off-by-one bug");
348 			}
349 		}
350 
351 		[Test]
TestBinarySearch()352 		public void TestBinarySearch ()
353 		{
354 			{
355 				bool errorThrown = false;
356 				try {
357 					ArrayList al1 = new ArrayList ();
358 					String [] s1 = { "This", "is", "a", "test" };
359 					al1.AddRange (s1);
360 					al1.BinarySearch (42);
361 				} catch (InvalidOperationException) {
362 					// this is what .NET throws
363 					errorThrown = true;
364 				} catch (ArgumentException) {
365 					// this is what the docs say it should throw
366 					errorThrown = true;
367 				} catch (Exception e) {
368 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
369 				}
370 				Assert.IsTrue (errorThrown, "search-for-wrong-type error not thrown");
371 			}
372 
373 			{
374 				char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
375 				ArrayList al1 = new ArrayList (arr);
376 				Assert.IsTrue (al1.BinarySearch ('c') >= 3, "couldn't find elem #1");
377 				Assert.IsTrue (al1.BinarySearch ('c') < 6, "couldn't find elem #2");
378 			}
379 			{
380 				char [] arr = { 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e' };
381 				ArrayList al1 = new ArrayList (arr);
382 				Assert.AreEqual (-4, al1.BinarySearch ('c'), "couldn't find next-higher elem");
383 			}
384 			{
385 				char [] arr = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
386 				ArrayList al1 = new ArrayList (arr);
387 				Assert.AreEqual (-9, al1.BinarySearch ('e'), "couldn't find end");
388 			}
389 
390 		}
391 
392 		[Test]
393 		[ExpectedException (typeof (ArgumentException))]
BinarySearch_IndexOverflow()394 		public void BinarySearch_IndexOverflow ()
395 		{
396 			ArrayList al = new ArrayList ();
397 			al.Add (this);
398 			al.BinarySearch (Int32.MaxValue, 1, this, null);
399 		}
400 
401 		[Test]
402 		[ExpectedException (typeof (ArgumentException))]
BinarySearch_CountOverflow()403 		public void BinarySearch_CountOverflow ()
404 		{
405 			ArrayList al = new ArrayList ();
406 			al.Add (this);
407 			al.BinarySearch (1, Int32.MaxValue, this, null);
408 		}
409 
410 		[Test]
BinarySearch_Null()411 		public void BinarySearch_Null ()
412 		{
413 			ArrayList al = new ArrayList ();
414 			al.Add (this);
415 			Assert.AreEqual (-1, al.BinarySearch (null), "null");
416 		}
417 
418 		// TODO - BinarySearch with IComparer
419 
420 		[Test]
TestClear()421 		public void TestClear ()
422 		{
423 			{
424 				bool errorThrown = false;
425 				try {
426 					ArrayList al1 =
427 						ArrayList.FixedSize (new ArrayList ());
428 					al1.Clear ();
429 				} catch (NotSupportedException) {
430 					errorThrown = true;
431 				}
432 				Assert.IsTrue (errorThrown, "add to fixed size error not thrown");
433 			}
434 			{
435 				bool errorThrown = false;
436 				try {
437 					ArrayList al1 =
438 						ArrayList.ReadOnly (new ArrayList ());
439 					al1.Clear ();
440 				} catch (NotSupportedException) {
441 					errorThrown = true;
442 				}
443 				Assert.IsTrue (errorThrown, "add to read only error not thrown");
444 			}
445 			{
446 				ArrayList al1 = new ArrayList ();
447 				al1.Add ('c');
448 				Assert.AreEqual (1, al1.Count, "should have one element");
449 				al1.Clear ();
450 				Assert.AreEqual (0, al1.Count, "should be empty");
451 			}
452 			{
453 				int [] i1 = { 1, 2, 3, 4 };
454 				ArrayList al1 = new ArrayList (i1);
455 				Assert.AreEqual (i1.Length, al1.Count, "should have elements");
456 				int capacity = al1.Capacity;
457 				al1.Clear ();
458 				Assert.AreEqual (0, al1.Count, "should be empty again");
459 				Assert.AreEqual (capacity, al1.Capacity, "capacity shouldn't have changed");
460 			}
461 		}
462 
463 		[Test]
TestClone()464 		public void TestClone ()
465 		{
466 			{
467 				char [] c1 = { 'a', 'b', 'c' };
468 				ArrayList al1 = new ArrayList (c1);
469 				ArrayList al2 = (ArrayList) al1.Clone ();
470 				Assert.AreEqual (al1 [0], al2 [0], "ArrayList match");
471 				Assert.AreEqual (al1 [1], al2 [1], "ArrayList match");
472 				Assert.AreEqual (al1 [2], al2 [2], "ArrayList match");
473 			}
474 			{
475 				char [] d10 = { 'a', 'b' };
476 				char [] d11 = { 'a', 'c' };
477 				char [] d12 = { 'b', 'c' };
478 				char [] [] d1 = { d10, d11, d12 };
479 				ArrayList al1 = new ArrayList (d1);
480 				ArrayList al2 = (ArrayList) al1.Clone ();
481 				Assert.AreEqual (al1 [0], al2 [0], "Array match");
482 				Assert.AreEqual (al1 [1], al2 [1], "Array match");
483 				Assert.AreEqual (al1 [2], al2 [2], "Array match");
484 
485 				((char []) al1 [0]) [0] = 'z';
486 				Assert.AreEqual (al1 [0], al2 [0], "shallow copy");
487 			}
488 		}
489 
490 		[Test]
TestContains()491 		public void TestContains ()
492 		{
493 			char [] c1 = { 'a', 'b', 'c' };
494 			ArrayList al1 = new ArrayList (c1);
495 			Assert.IsTrue (!al1.Contains (null), "never find a null");
496 			Assert.IsTrue (al1.Contains ('b'), "can't find value");
497 			Assert.IsTrue (!al1.Contains ('?'), "shouldn't find value");
498 		}
499 
500 		[Test]
TestCopyTo()501 		public void TestCopyTo ()
502 		{
503 			{
504 				bool errorThrown = false;
505 				try {
506 					Char [] c1 = new Char [2];
507 					ArrayList al1 = new ArrayList (c1);
508 					al1.CopyTo (null, 2);
509 				} catch (ArgumentNullException) {
510 					errorThrown = true;
511 				} catch (Exception e) {
512 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
513 				}
514 				Assert.IsTrue (errorThrown, "error not thrown 1");
515 			}
516 			{
517 				bool errorThrown = false;
518 				try {
519 					Char [] c1 = new Char [2];
520 					ArrayList al1 = new ArrayList (c1);
521 					Char [,] c2 = new Char [2, 2];
522 					al1.CopyTo (c2, 2);
523 				} catch (ArgumentException) {
524 					errorThrown = true;
525 				} catch (Exception e) {
526 					Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
527 				}
528 				Assert.IsTrue (errorThrown, "error not thrown 2");
529 			}
530 			{
531 				bool errorThrown = false;
532 				try {
533 					// This appears to be a bug in the ArrayList Constructor.
534 					// It throws a RankException if a multidimensional Array
535 					// is passed. The docs imply that an IEnumerator is used
536 					// to retrieve the items from the collection, so this should
537 					// work.  In anycase this test is for CopyTo, so use what
538 					// works on both platforms.
539 					//Char[,] c1 = new Char[2,2];
540 					Char [] c1 = new Char [2];
541 					ArrayList al1 = new ArrayList (c1);
542 					Char [] c2 = new Char [2];
543 					al1.CopyTo (c2, 2);
544 				} catch (ArgumentException) {
545 					errorThrown = true;
546 				} catch (Exception e) {
547 					Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
548 				}
549 				Assert.IsTrue (errorThrown, "error not thrown 3");
550 			}
551 			{
552 				bool errorThrown = false;
553 				try {
554 					Char [] c1 = new Char [2];
555 					ArrayList al1 = new ArrayList (c1);
556 					Char [] c2 = new Char [2];
557 					al1.CopyTo (c2, -1);
558 				} catch (ArgumentOutOfRangeException) {
559 					errorThrown = true;
560 				} catch (Exception e) {
561 					Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
562 				}
563 				Assert.IsTrue (errorThrown, "error not thrown 4");
564 			}
565 			{
566 				bool errorThrown = false;
567 				try {
568 					Char [] c1 = new Char [2];
569 					ArrayList al1 = new ArrayList (c1);
570 					Char [] c2 = new Char [2];
571 					al1.CopyTo (c2, 3);
572 				} catch (ArgumentException) {
573 					errorThrown = true;
574 				} catch (Exception e) {
575 					Assert.Fail ("Incorrect exception thrown at 5: " + e.ToString ());
576 				}
577 				Assert.IsTrue (errorThrown, "error not thrown 5");
578 			}
579 			{
580 				bool errorThrown = false;
581 				try {
582 					Char [] c1 = new Char [2];
583 					ArrayList al1 = new ArrayList (c1);
584 					Char [] c2 = new Char [2];
585 					al1.CopyTo (c2, 1);
586 				} catch (ArgumentException) {
587 					errorThrown = true;
588 				} catch (Exception e) {
589 					Assert.Fail ("Incorrect exception thrown at 6: " + e.ToString ());
590 				}
591 				Assert.IsTrue (errorThrown, "error not thrown 6");
592 			}
593 			{
594 				bool errorThrown = false;
595 				try {
596 					String [] c1 = { "String", "array" };
597 					ArrayList al1 = new ArrayList (c1);
598 					Char [] c2 = new Char [2];
599 					al1.CopyTo (c2, 0);
600 				} catch (InvalidCastException) {
601 					errorThrown = true;
602 				} catch (Exception e) {
603 					Assert.Fail ("Incorrect exception thrown at 7: " + e.ToString ());
604 				}
605 				Assert.IsTrue (errorThrown, "error not thrown 7");
606 			}
607 
608 			Char [] orig = { 'a', 'b', 'c', 'd' };
609 			ArrayList al = new ArrayList (orig);
610 			Char [] copy = new Char [10];
611 			Array.Clear (copy, 0, copy.Length);
612 			al.CopyTo (copy, 3);
613 			Assert.AreEqual ((char) 0, copy [0], "Wrong CopyTo 0");
614 			Assert.AreEqual ((char) 0, copy [1], "Wrong CopyTo 1");
615 			Assert.AreEqual ((char) 0, copy [2], "Wrong CopyTo 2");
616 			Assert.AreEqual (orig [0], copy [3], "Wrong CopyTo 3");
617 			Assert.AreEqual (orig [1], copy [4], "Wrong CopyTo 4");
618 			Assert.AreEqual (orig [2], copy [5], "Wrong CopyTo 5");
619 			Assert.AreEqual (orig [3], copy [6], "Wrong CopyTo 6");
620 			Assert.AreEqual ((char) 0, copy [7], "Wrong CopyTo 7");
621 			Assert.AreEqual ((char) 0, copy [8], "Wrong CopyTo 8");
622 			Assert.AreEqual ((char) 0, copy [9], "Wrong CopyTo 9");
623 		}
624 
625 		[Test]
626 		[ExpectedException (typeof (ArgumentException))]
CopyTo_IndexOverflow()627 		public void CopyTo_IndexOverflow ()
628 		{
629 			ArrayList al = new ArrayList ();
630 			al.Add (this);
631 			al.CopyTo (Int32.MaxValue, new byte [2], 0, 0);
632 		}
633 
634 		[Test]
635 		[ExpectedException (typeof (ArgumentException))]
CopyTo_ArrayIndexOverflow()636 		public void CopyTo_ArrayIndexOverflow ()
637 		{
638 			ArrayList al = new ArrayList ();
639 			al.Add (this);
640 			al.CopyTo (0, new byte [2], Int32.MaxValue, 0);
641 		}
642 
643 		[Test]
644 		[ExpectedException (typeof (ArgumentException))]
CopyTo_CountOverflow()645 		public void CopyTo_CountOverflow ()
646 		{
647 			ArrayList al = new ArrayList ();
648 			al.Add (this);
649 			al.CopyTo (0, new byte [2], 0, Int32.MaxValue);
650 		}
651 
652 		[Test]
TestFixedSize()653 		public void TestFixedSize ()
654 		{
655 			{
656 				bool errorThrown = false;
657 				try {
658 					ArrayList al1 = ArrayList.FixedSize (null);
659 				} catch (ArgumentNullException) {
660 					errorThrown = true;
661 				}
662 				Assert.IsTrue (errorThrown, "null arg error not thrown");
663 			}
664 			{
665 				ArrayList al1 = new ArrayList ();
666 				Assert.AreEqual (false, al1.IsFixedSize, "arrays start un-fixed.");
667 				ArrayList al2 = ArrayList.FixedSize (al1);
668 				Assert.AreEqual (true, al2.IsFixedSize, "should be fixed.");
669 			}
670 		}
671 
672 		[Test]
TestEnumerator()673 		public void TestEnumerator ()
674 		{
675 			String [] s1 = { "this", "is", "a", "test" };
676 			ArrayList al1 = new ArrayList (s1);
677 			IEnumerator en = al1.GetEnumerator ();
678 			en.MoveNext ();
679 			al1.Add ("something");
680 			try {
681 				en.MoveNext ();
682 				Assert.Fail ("Add() didn't invalidate the enumerator");
683 			} catch (InvalidOperationException) {
684 				// do nothing...this is what we expect
685 			}
686 
687 			en = al1.GetEnumerator ();
688 			en.MoveNext ();
689 			al1.AddRange (al1);
690 			try {
691 				en.MoveNext ();
692 				Assert.Fail ("AddRange() didn't invalidate the enumerator");
693 			} catch (InvalidOperationException) {
694 				// do nothing...this is what we expect
695 			}
696 
697 			en = al1.GetEnumerator ();
698 			en.MoveNext ();
699 			al1.Clear ();
700 			try {
701 				en.MoveNext ();
702 				Assert.Fail ("Clear() didn't invalidate the enumerator");
703 			} catch (InvalidOperationException) {
704 				// do nothing...this is what we expect
705 			}
706 
707 			al1 = new ArrayList (s1);
708 			en = al1.GetEnumerator ();
709 			en.MoveNext ();
710 			al1.Insert (0, "new first");
711 			try {
712 				en.MoveNext ();
713 				Assert.Fail ("Insert() didn't invalidate the enumerator");
714 			} catch (InvalidOperationException) {
715 				// do nothing...this is what we expect
716 			}
717 
718 			en = al1.GetEnumerator ();
719 			en.MoveNext ();
720 			al1.InsertRange (0, al1);
721 			try {
722 				en.MoveNext ();
723 				Assert.Fail ("InsertRange() didn't invalidate the enumerator");
724 			} catch (InvalidOperationException) {
725 				// do nothing...this is what we expect
726 			}
727 
728 			en = al1.GetEnumerator ();
729 			en.MoveNext ();
730 			al1.Remove ("this");
731 			try {
732 				en.MoveNext ();
733 				Assert.Fail ("Remove() didn't invalidate the enumerator");
734 			} catch (InvalidOperationException) {
735 				// do nothing...this is what we expect
736 			}
737 
738 			en = al1.GetEnumerator ();
739 			en.MoveNext ();
740 			al1.RemoveAt (2);
741 			try {
742 				en.MoveNext ();
743 				Assert.Fail ("RemoveAt() didn't invalidate the enumerator");
744 			} catch (InvalidOperationException) {
745 				// do nothing...this is what we expect
746 			}
747 
748 			en = al1.GetEnumerator ();
749 			en.MoveNext ();
750 			al1.RemoveRange (1, 1);
751 			try {
752 				en.MoveNext ();
753 				Assert.Fail ("RemoveRange() didn't invalidate the enumerator");
754 			} catch (InvalidOperationException) {
755 				// do nothing...this is what we expect
756 			}
757 
758 			en = al1.GetEnumerator ();
759 			en.MoveNext ();
760 			al1.Reverse ();
761 			try {
762 				en.MoveNext ();
763 				Assert.Fail ("Reverse() didn't invalidate the enumerator");
764 			} catch (InvalidOperationException) {
765 				// do nothing...this is what we expect
766 			}
767 
768 			en = al1.GetEnumerator ();
769 			en.MoveNext ();
770 			al1.Sort ();
771 			try {
772 				en.MoveNext ();
773 				Assert.Fail ("Sort() didn't invalidate the enumerator");
774 			} catch (InvalidOperationException) {
775 				// do nothing...this is what we expect
776 			}
777 		}
778 
779 		[Test]
TestGetEnumerator()780 		public void TestGetEnumerator ()
781 		{
782 			{
783 				bool errorThrown = false;
784 				try {
785 					ArrayList a = new ArrayList ();
786 					IEnumerator en = a.GetEnumerator (-1, 1);
787 				} catch (ArgumentOutOfRangeException) {
788 					errorThrown = true;
789 				}
790 				Assert.IsTrue (errorThrown, "negative index error not thrown");
791 			}
792 			{
793 				bool errorThrown = false;
794 				try {
795 					ArrayList a = new ArrayList ();
796 					IEnumerator en = a.GetEnumerator (1, -1);
797 				} catch (ArgumentOutOfRangeException) {
798 					errorThrown = true;
799 				}
800 				Assert.IsTrue (errorThrown, "negative index error not thrown");
801 			}
802 			{
803 				bool errorThrown = false;
804 				try {
805 					ArrayList a = new ArrayList ();
806 					IEnumerator en = a.GetEnumerator (1, 1);
807 				} catch (ArgumentException) {
808 					errorThrown = true;
809 				}
810 				Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
811 			}
812 			{
813 				String [] s1 = { "this", "is", "a", "test" };
814 				ArrayList al1 = new ArrayList (s1);
815 				IEnumerator en = al1.GetEnumerator ();
816 				Assert.IsNotNull (en, "No enumerator");
817 
818 				for (int i = 0; i < s1.Length; i++) {
819 					en.MoveNext ();
820 					Assert.AreEqual (al1 [i], en.Current, "Not enumerating");
821 				}
822 			}
823 			{
824 				String [] s1 = { "this", "is", "a", "test" };
825 				ArrayList al1 = new ArrayList (s1);
826 				IEnumerator en = al1.GetEnumerator (1, 2);
827 				Assert.IsNotNull (en, "No enumerator");
828 
829 				for (int i = 0; i < 2; i++) {
830 					en.MoveNext ();
831 					Assert.AreEqual (al1 [i + 1], en.Current, "Not enumerating");
832 				}
833 			}
834 		}
835 
836 		[Test]
837 		[ExpectedException (typeof (ArgumentException))]
GetEnumerator_IndexOverflow()838 		public void GetEnumerator_IndexOverflow ()
839 		{
840 			ArrayList al = new ArrayList ();
841 			al.Add (this);
842 			al.GetEnumerator (Int32.MaxValue, 0);
843 		}
844 
845 		[Test]
846 		[ExpectedException (typeof (ArgumentException))]
GetEnumerator_CountOverflow()847 		public void GetEnumerator_CountOverflow ()
848 		{
849 			ArrayList al = new ArrayList ();
850 			al.Add (this);
851 			al.GetEnumerator (0, Int32.MaxValue);
852 		}
853 
854 		[Test]
TestGetRange()855 		public void TestGetRange ()
856 		{
857 			{
858 				bool errorThrown = false;
859 				try {
860 					ArrayList a = new ArrayList ();
861 					ArrayList b = a.GetRange (-1, 1);
862 				} catch (ArgumentOutOfRangeException) {
863 					errorThrown = true;
864 				}
865 				Assert.IsTrue (errorThrown, "negative index error not thrown");
866 			}
867 			{
868 				bool errorThrown = false;
869 				try {
870 					ArrayList a = new ArrayList ();
871 					ArrayList b = a.GetRange (1, -1);
872 				} catch (ArgumentOutOfRangeException) {
873 					errorThrown = true;
874 				}
875 				Assert.IsTrue (errorThrown, "negative index error not thrown");
876 			}
877 			{
878 				bool errorThrown = false;
879 				try {
880 					ArrayList a = new ArrayList ();
881 					ArrayList b = a.GetRange (1, 1);
882 				} catch (ArgumentException) {
883 					errorThrown = true;
884 				}
885 				Assert.IsTrue (errorThrown, "out-of-range index error not thrown");
886 			}
887 			{
888 				char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
889 				ArrayList a = new ArrayList (chars);
890 				ArrayList b = a.GetRange (1, 3);
891 				Assert.AreEqual (3, b.Count, "GetRange returned wrong size ArrayList");
892 				for (int i = 0; i < b.Count; i++) {
893 					Assert.AreEqual (chars [i + 1], b [i], "range didn't work");
894 				}
895 
896 				a [2] = '?'; // should screw up ArrayList b.
897 				bool errorThrown = false;
898 				try {
899 					int i = b.Count;
900 				} catch (InvalidOperationException) {
901 					errorThrown = true;
902 				}
903 				Assert.AreEqual (true, errorThrown, "Munging 'a' should mess up 'b'");
904 			}
905 			{
906 				char [] chars = { 'a', 'b', 'c', 'd', 'e', 'f' };
907 				ArrayList a = new ArrayList (chars);
908 				ArrayList b = a.GetRange (3, 3);
909 				object [] obj_chars = b.ToArray ();
910 				for (int i = 0; i < 3; i++) {
911 					char c = (char) obj_chars [i];
912 					Assert.AreEqual (chars [i + 3], c, "range.ToArray didn't work");
913 				}
914 				char [] new_chars = (char []) b.ToArray (typeof (char));
915 				for (int i = 0; i < 3; i++) {
916 					Assert.AreEqual (chars [i + 3], new_chars [i], "range.ToArray with type didn't work");
917 				}
918 			}
919 		}
920 
921 		[Test]
922 		[ExpectedException (typeof (ArgumentException))]
GetRange_IndexOverflow()923 		public void GetRange_IndexOverflow ()
924 		{
925 			ArrayList al = new ArrayList ();
926 			al.Add (this);
927 			al.GetRange (Int32.MaxValue, 0);
928 		}
929 
930 		[Test]
931 		[ExpectedException (typeof (ArgumentException))]
GetRange_CountOverflow()932 		public void GetRange_CountOverflow ()
933 		{
934 			ArrayList al = new ArrayList ();
935 			al.Add (this);
936 			al.GetRange (0, Int32.MaxValue);
937 		}
938 
939 		[Test]
TestIndexOf()940 		public void TestIndexOf ()
941 		{
942 			{
943 				bool errorThrown = false;
944 				try {
945 					ArrayList a = new ArrayList (1);
946 					int i = a.IndexOf ('a', -1);
947 				} catch (ArgumentOutOfRangeException) {
948 					errorThrown = true;
949 				}
950 				Assert.IsTrue (errorThrown, "negative indexof error not thrown");
951 			}
952 			{
953 				bool errorThrown = false;
954 				try {
955 					ArrayList a = new ArrayList (1);
956 					int i = a.IndexOf ('a', 2);
957 				} catch (ArgumentOutOfRangeException) {
958 					errorThrown = true;
959 				}
960 				Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
961 			}
962 			{
963 				bool errorThrown = false;
964 				try {
965 					ArrayList a = new ArrayList (1);
966 					int i = a.IndexOf ('a', 0, -1);
967 				} catch (ArgumentOutOfRangeException) {
968 					errorThrown = true;
969 				}
970 				Assert.IsTrue (errorThrown, "negative indexof error not thrown");
971 			}
972 			{
973 				bool errorThrown = false;
974 				try {
975 					ArrayList a = new ArrayList (1);
976 					int i = a.IndexOf ('a', 0, 2);
977 				} catch (ArgumentOutOfRangeException) {
978 					errorThrown = true;
979 				}
980 				Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
981 			}
982 			{
983 				bool errorThrown = false;
984 				try {
985 					ArrayList a = new ArrayList (2);
986 					int i = a.IndexOf ('a', 1, 2);
987 				} catch (ArgumentOutOfRangeException) {
988 					errorThrown = true;
989 				}
990 				Assert.IsTrue (errorThrown, "past-end indexof error not thrown");
991 			}
992 			{
993 				char [] c = { 'a', 'b', 'c', 'd', 'e' };
994 				ArrayList a = new ArrayList (c);
995 				Assert.AreEqual (-1, a.IndexOf (null), "never find null");
996 				Assert.AreEqual (-1, a.IndexOf (null, 0), "never find null");
997 				Assert.AreEqual (-1, a.IndexOf (null, 0, 5), "never find null");
998 				Assert.AreEqual (2, a.IndexOf ('c'), "can't find elem");
999 				Assert.AreEqual (2, a.IndexOf ('c', 2), "can't find elem");
1000 				Assert.AreEqual (2, a.IndexOf ('c', 2, 2), "can't find elem");
1001 				Assert.AreEqual (-1, a.IndexOf ('c', 3, 2), "shouldn't find elem");
1002 				Assert.AreEqual (-1, a.IndexOf ('?'), "shouldn't find");
1003 				Assert.AreEqual (-1, a.IndexOf (3), "shouldn't find");
1004 			}
1005 		}
1006 
1007 		[Test]
1008 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
IndexOf_StartIndexOverflow()1009 		public void IndexOf_StartIndexOverflow ()
1010 		{
1011 			ArrayList al = new ArrayList ();
1012 			al.Add (this);
1013 			al.IndexOf ('a', Int32.MaxValue, 1);
1014 		}
1015 
1016 		[Test]
1017 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
IndexOf_CountOverflow()1018 		public void IndexOf_CountOverflow ()
1019 		{
1020 			ArrayList al = new ArrayList ();
1021 			al.Add (this);
1022 			al.IndexOf ('a', 1, Int32.MaxValue);
1023 		}
1024 
1025 		[Test]
TestInsert()1026 		public void TestInsert ()
1027 		{
1028 			{
1029 				bool errorThrown = false;
1030 				try {
1031 					ArrayList al1 =
1032 						ArrayList.FixedSize (new ArrayList ());
1033 					al1.Insert (0, "Hi!");
1034 				} catch (NotSupportedException) {
1035 					errorThrown = true;
1036 				}
1037 				Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
1038 			}
1039 			{
1040 				bool errorThrown = false;
1041 				try {
1042 					ArrayList al1 =
1043 						ArrayList.ReadOnly (new ArrayList ());
1044 					al1.Insert (0, "Hi!");
1045 				} catch (NotSupportedException) {
1046 					errorThrown = true;
1047 				}
1048 				Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1049 			}
1050 			{
1051 				bool errorThrown = false;
1052 				try {
1053 					ArrayList al1 = new ArrayList (3);
1054 					al1.Insert (-1, "Hi!");
1055 				} catch (ArgumentOutOfRangeException) {
1056 					errorThrown = true;
1057 				}
1058 				Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1059 			}
1060 			{
1061 				bool errorThrown = false;
1062 				try {
1063 					ArrayList al1 = new ArrayList (3);
1064 					al1.Insert (4, "Hi!");
1065 				} catch (ArgumentOutOfRangeException) {
1066 					errorThrown = true;
1067 				}
1068 				Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1069 			}
1070 			{
1071 				ArrayList al1 = new ArrayList ();
1072 				Assert.AreEqual (0, al1.Count, "arraylist starts empty");
1073 				al1.Insert (0, 'a');
1074 				al1.Insert (1, 'b');
1075 				al1.Insert (0, 'c');
1076 				Assert.AreEqual (3, al1.Count, "arraylist needs stuff");
1077 				Assert.AreEqual ('c', al1 [0], "arraylist got stuff");
1078 				Assert.AreEqual ('a', al1 [1], "arraylist got stuff");
1079 				Assert.AreEqual ('b', al1 [2], "arraylist got stuff");
1080 			}
1081 		}
1082 
1083 		[Test]
TestInsertRange()1084 		public void TestInsertRange ()
1085 		{
1086 			{
1087 				bool errorThrown = false;
1088 				try {
1089 					ArrayList al1 =
1090 						ArrayList.FixedSize (new ArrayList ());
1091 					string [] s = { "Hi!" };
1092 					al1.InsertRange (0, s);
1093 				} catch (NotSupportedException) {
1094 					errorThrown = true;
1095 				}
1096 				Assert.IsTrue (errorThrown, "insert to fixed size error not thrown");
1097 			}
1098 			{
1099 				bool errorThrown = false;
1100 				try {
1101 					ArrayList al1 =
1102 						ArrayList.ReadOnly (new ArrayList ());
1103 					string [] s = { "Hi!" };
1104 					al1.InsertRange (0, s);
1105 				} catch (NotSupportedException) {
1106 					errorThrown = true;
1107 				}
1108 				Assert.IsTrue (errorThrown, "insert to read only error not thrown");
1109 			}
1110 			{
1111 				bool errorThrown = false;
1112 				try {
1113 					ArrayList al1 = new ArrayList (3);
1114 					string [] s = { "Hi!" };
1115 					al1.InsertRange (-1, s);
1116 				} catch (ArgumentOutOfRangeException) {
1117 					errorThrown = true;
1118 				}
1119 				Assert.IsTrue (errorThrown, "negative index insert error not thrown");
1120 			}
1121 			{
1122 				bool errorThrown = false;
1123 				try {
1124 					ArrayList al1 = new ArrayList (3);
1125 					string [] s = { "Hi!" };
1126 					al1.InsertRange (4, s);
1127 				} catch (ArgumentOutOfRangeException) {
1128 					errorThrown = true;
1129 				}
1130 				Assert.IsTrue (errorThrown, "out-of-range insert error not thrown");
1131 			}
1132 			{
1133 				bool errorThrown = false;
1134 				try {
1135 					ArrayList al1 = new ArrayList (3);
1136 					al1.InsertRange (0, null);
1137 				} catch (ArgumentNullException) {
1138 					errorThrown = true;
1139 				}
1140 				Assert.IsTrue (errorThrown, "null insert error not thrown");
1141 			}
1142 			{
1143 				char [] c = { 'a', 'b', 'c' };
1144 				ArrayList a = new ArrayList (c);
1145 				a.InsertRange (1, c);
1146 				Assert.AreEqual ('a', a [0], "bad insert 1");
1147 				Assert.AreEqual ('a', a [1], "bad insert 2");
1148 				Assert.AreEqual ('b', a [2], "bad insert 3");
1149 				Assert.AreEqual ('c', a [3], "bad insert 4");
1150 				Assert.AreEqual ('b', a [4], "bad insert 5");
1151 				Assert.AreEqual ('c', a [5], "bad insert 6");
1152 			}
1153 		}
1154 
1155 		[Test]
TestLastIndexOf()1156 		public void TestLastIndexOf ()
1157 		{
1158 			//{
1159 			//bool errorThrown = false;
1160 			//try {
1161 			//ArrayList a = new ArrayList(1);
1162 			//int i = a.LastIndexOf('a', -1);
1163 			//} catch (ArgumentOutOfRangeException) {
1164 			//errorThrown = true;
1165 			//}
1166 			//Assert.IsTrue (//errorThrown, "first negative lastindexof error not thrown");
1167 			//}
1168 			{
1169 				bool errorThrown = false;
1170 				try {
1171 					ArrayList a = new ArrayList (1);
1172 					int i = a.LastIndexOf ('a', 2);
1173 				} catch (ArgumentOutOfRangeException) {
1174 					errorThrown = true;
1175 				}
1176 				Assert.IsTrue (errorThrown, "past-end lastindexof error not thrown");
1177 			}
1178 			//{
1179 			//bool errorThrown = false;
1180 			//try {
1181 			//ArrayList a = new ArrayList(1);
1182 			//int i = a.LastIndexOf('a', 0, -1);
1183 			//} catch (ArgumentOutOfRangeException) {
1184 			//errorThrown = true;
1185 			//}
1186 			//Assert.IsTrue (//errorThrown, "second negative lastindexof error not thrown");
1187 			//}
1188 			//{
1189 			//bool errorThrown = false;
1190 			//try {
1191 			//ArrayList a = new ArrayList(1);
1192 			//int i = a.LastIndexOf('a', 0, 2);
1193 			//} catch (ArgumentOutOfRangeException) {
1194 			//errorThrown = true;
1195 			//}
1196 			//Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
1197 			//}
1198 			//{
1199 			//bool errorThrown = false;
1200 			//try {
1201 			//ArrayList a = new ArrayList(2);
1202 			//int i = a.LastIndexOf('a', 0, 2);
1203 			//} catch (ArgumentOutOfRangeException) {
1204 			//errorThrown = true;
1205 			//}
1206 			//Assert.IsTrue (//errorThrown, "past-end lastindexof error not thrown");
1207 			//}
1208 			int iTest = 0;
1209 			try {
1210 				char [] c = { 'a', 'b', 'c', 'd', 'e' };
1211 				ArrayList a = new ArrayList (c);
1212 				Assert.AreEqual (-1, a.LastIndexOf (null), "never find null");
1213 				iTest++;
1214 				Assert.AreEqual (-1, a.LastIndexOf (null, 4), "never find null");
1215 				iTest++;
1216 				Assert.AreEqual (-1, a.LastIndexOf (null, 4, 5), "never find null");
1217 				iTest++;
1218 				Assert.AreEqual (2, a.LastIndexOf ('c'), "can't find elem");
1219 				iTest++;
1220 				Assert.AreEqual (2, a.LastIndexOf ('c', 4), "can't find elem");
1221 				iTest++;
1222 				Assert.AreEqual (2, a.LastIndexOf ('c', 3, 2), "can't find elem");
1223 				iTest++;
1224 				Assert.AreEqual (-1, a.LastIndexOf ('c', 4, 2), "shouldn't find elem");
1225 				iTest++;
1226 				Assert.AreEqual (-1, a.LastIndexOf ('?'), "shouldn't find");
1227 				iTest++;
1228 				Assert.AreEqual (-1, a.LastIndexOf (1), "shouldn't find");
1229 			} catch (Exception e) {
1230 				Assert.Fail ("Unexpected exception caught when iTest=" + iTest + ". e=" + e);
1231 			}
1232 		}
1233 
1234 		[Test]
1235 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
LastIndexOf_StartIndexOverflow()1236 		public void LastIndexOf_StartIndexOverflow ()
1237 		{
1238 			ArrayList al = new ArrayList ();
1239 			al.Add (this);
1240 			al.LastIndexOf ('a', Int32.MaxValue, 1);
1241 		}
1242 
1243 		[Test]
1244 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
LastIndexOf_CountOverflow()1245 		public void LastIndexOf_CountOverflow ()
1246 		{
1247 			ArrayList al = new ArrayList ();
1248 			al.Add (this);
1249 			al.LastIndexOf ('a', 1, Int32.MaxValue);
1250 		}
1251 
1252 		[Test]
TestReadOnly()1253 		public void TestReadOnly ()
1254 		{
1255 			{
1256 				bool errorThrown = false;
1257 				try {
1258 					ArrayList al1 = ArrayList.ReadOnly (null);
1259 				} catch (ArgumentNullException) {
1260 					errorThrown = true;
1261 				}
1262 				Assert.IsTrue (errorThrown, "null arg error not thrown");
1263 			}
1264 			{
1265 				ArrayList al1 = new ArrayList ();
1266 				Assert.AreEqual (false, al1.IsReadOnly, "arrays start writeable.");
1267 				ArrayList al2 = ArrayList.ReadOnly (al1);
1268 				Assert.AreEqual (true, al2.IsReadOnly, "should be readonly.");
1269 			}
1270 		}
1271 
1272 		[Test]
TestRemove()1273 		public void TestRemove ()
1274 		{
1275 			{
1276 				bool errorThrown = false;
1277 				try {
1278 					ArrayList al1 =
1279 						ArrayList.FixedSize (new ArrayList (3));
1280 					al1.Remove (1);
1281 				} catch (NotSupportedException) {
1282 					errorThrown = true;
1283 				}
1284 				Assert.IsTrue (errorThrown, "remove fixed size error not thrown");
1285 			}
1286 			{
1287 				bool errorThrown = false;
1288 				try {
1289 					ArrayList al1 =
1290 						ArrayList.ReadOnly (new ArrayList (3));
1291 					al1.Remove (1);
1292 				} catch (NotSupportedException) {
1293 					errorThrown = true;
1294 				}
1295 				Assert.IsTrue (errorThrown, "remove read only error not thrown");
1296 			}
1297 			{
1298 				char [] c = { 'a', 'b', 'c' };
1299 				ArrayList a = new ArrayList (c);
1300 				a.Remove (1);
1301 				a.Remove ('?');
1302 				Assert.AreEqual (c.Length, a.Count, "should be unchanged");
1303 				a.Remove ('a');
1304 				Assert.AreEqual (2, a.Count, "should be changed");
1305 				Assert.AreEqual ('b', a [0], "should have shifted");
1306 				Assert.AreEqual ('c', a [1], "should have shifted");
1307 			}
1308 		}
1309 
1310 		[Test]
TestRemoveAt()1311 		public void TestRemoveAt ()
1312 		{
1313 			{
1314 				bool errorThrown = false;
1315 				try {
1316 					ArrayList al1 =
1317 						ArrayList.FixedSize (new ArrayList (3));
1318 					al1.RemoveAt (1);
1319 				} catch (NotSupportedException) {
1320 					errorThrown = true;
1321 				}
1322 				Assert.IsTrue (errorThrown, "remove from fixed size error not thrown");
1323 			}
1324 			{
1325 				bool errorThrown = false;
1326 				try {
1327 					ArrayList al1 =
1328 						ArrayList.ReadOnly (new ArrayList (3));
1329 					al1.RemoveAt (1);
1330 				} catch (NotSupportedException) {
1331 					errorThrown = true;
1332 				}
1333 				Assert.IsTrue (errorThrown, "remove from read only error not thrown");
1334 			}
1335 			{
1336 				bool errorThrown = false;
1337 				try {
1338 					ArrayList al1 = new ArrayList (3);
1339 					al1.RemoveAt (-1);
1340 				} catch (ArgumentOutOfRangeException) {
1341 					errorThrown = true;
1342 				}
1343 				Assert.IsTrue (errorThrown, "remove at negative index error not thrown");
1344 			}
1345 			{
1346 				bool errorThrown = false;
1347 				try {
1348 					ArrayList al1 = new ArrayList (3);
1349 					al1.RemoveAt (4);
1350 				} catch (ArgumentOutOfRangeException) {
1351 					errorThrown = true;
1352 				}
1353 				Assert.IsTrue (errorThrown, "remove at out-of-range index error not thrown");
1354 			}
1355 			{
1356 				char [] c = { 'a', 'b', 'c' };
1357 				ArrayList a = new ArrayList (c);
1358 				a.RemoveAt (0);
1359 				Assert.AreEqual (2, a.Count, "should be changed");
1360 				Assert.AreEqual ('b', a [0], "should have shifted");
1361 				Assert.AreEqual ('c', a [1], "should have shifted");
1362 			}
1363 		}
1364 
1365 		[Test]
TestRemoveRange()1366 		public void TestRemoveRange ()
1367 		{
1368 			{
1369 				bool errorThrown = false;
1370 				try {
1371 					ArrayList al1 =
1372 						ArrayList.FixedSize (new ArrayList (3));
1373 					al1.RemoveRange (0, 1);
1374 				} catch (NotSupportedException) {
1375 					errorThrown = true;
1376 				}
1377 				Assert.IsTrue (errorThrown, "removerange from fixed size error not thrown");
1378 			}
1379 			{
1380 				bool errorThrown = false;
1381 				try {
1382 					ArrayList al1 =
1383 						ArrayList.ReadOnly (new ArrayList (3));
1384 					al1.RemoveRange (0, 1);
1385 				} catch (NotSupportedException) {
1386 					errorThrown = true;
1387 				}
1388 				Assert.IsTrue (errorThrown, "removerange from read only error not thrown");
1389 			}
1390 			{
1391 				bool errorThrown = false;
1392 				try {
1393 					ArrayList al1 = new ArrayList (3);
1394 					al1.RemoveRange (-1, 1);
1395 				} catch (ArgumentOutOfRangeException) {
1396 					errorThrown = true;
1397 				}
1398 				Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
1399 			}
1400 			{
1401 				bool errorThrown = false;
1402 				try {
1403 					ArrayList al1 = new ArrayList (3);
1404 					al1.RemoveRange (0, -1);
1405 				} catch (ArgumentOutOfRangeException) {
1406 					errorThrown = true;
1407 				}
1408 				Assert.IsTrue (errorThrown, "removerange at negative index error not thrown");
1409 			}
1410 			{
1411 				bool errorThrown = false;
1412 				try {
1413 					ArrayList al1 = new ArrayList (3);
1414 					al1.RemoveRange (2, 3);
1415 				} catch (ArgumentException) {
1416 					errorThrown = true;
1417 				}
1418 				Assert.IsTrue (errorThrown, "removerange at bad range error not thrown");
1419 			}
1420 			{
1421 				char [] c = { 'a', 'b', 'c' };
1422 				ArrayList a = new ArrayList (c);
1423 				a.RemoveRange (1, 2);
1424 				Assert.AreEqual (1, a.Count, "should be changed");
1425 				Assert.AreEqual ('a', a [0], "should have shifted");
1426 			}
1427 		}
1428 
1429 		[Test]
1430 		[ExpectedException (typeof (ArgumentException))]
RemoveRange_IndexOverflow()1431 		public void RemoveRange_IndexOverflow ()
1432 		{
1433 			ArrayList al = new ArrayList ();
1434 			al.Add (this);
1435 			al.RemoveRange (Int32.MaxValue, 1);
1436 		}
1437 
1438 		[Test]
1439 		[ExpectedException (typeof (ArgumentException))]
RemoveRange_CountOverflow()1440 		public void RemoveRange_CountOverflow ()
1441 		{
1442 			ArrayList al = new ArrayList ();
1443 			al.Add (this);
1444 			al.RemoveRange (1, Int32.MaxValue);
1445 		}
1446 
1447 		[Test]
TestRepeat()1448 		public void TestRepeat ()
1449 		{
1450 			{
1451 				bool errorThrown = false;
1452 				try {
1453 					ArrayList al1 = ArrayList.Repeat ('c', -1);
1454 				} catch (ArgumentOutOfRangeException) {
1455 					errorThrown = true;
1456 				}
1457 				Assert.IsTrue (errorThrown, "repeat negative copies error not thrown");
1458 			}
1459 			{
1460 				ArrayList al1 = ArrayList.Repeat ("huh?", 0);
1461 				Assert.AreEqual (0, al1.Count, "should be nothing in array");
1462 			}
1463 			{
1464 				ArrayList al1 = ArrayList.Repeat ("huh?", 3);
1465 				Assert.AreEqual (3, al1.Count, "should be something in array");
1466 				Assert.AreEqual ("huh?", al1 [0], "array elem doesn't check");
1467 				Assert.AreEqual ("huh?", al1 [1], "array elem doesn't check");
1468 				Assert.AreEqual ("huh?", al1 [2], "array elem doesn't check");
1469 			}
1470 		}
1471 
1472 		[Test]
TestReverse()1473 		public void TestReverse ()
1474 		{
1475 			{
1476 				bool errorThrown = false;
1477 				try {
1478 					ArrayList al1 =
1479 						ArrayList.ReadOnly (new ArrayList ());
1480 					al1.Reverse ();
1481 				} catch (NotSupportedException) {
1482 					errorThrown = true;
1483 				}
1484 				Assert.IsTrue (errorThrown, "reverse on read only error not thrown");
1485 			}
1486 			{
1487 				bool errorThrown = false;
1488 				try {
1489 					char [] c = new Char [2];
1490 					ArrayList al1 = new ArrayList (c);
1491 					al1.Reverse (0, 3);
1492 				} catch (ArgumentException) {
1493 					errorThrown = true;
1494 				}
1495 				Assert.IsTrue (errorThrown, "error not thrown");
1496 			}
1497 			{
1498 				bool errorThrown = false;
1499 				try {
1500 					char [] c = new Char [2];
1501 					ArrayList al1 = new ArrayList (c);
1502 					al1.Reverse (3, 0);
1503 				} catch (ArgumentException) {
1504 					errorThrown = true;
1505 				}
1506 				Assert.IsTrue (errorThrown, "error not thrown");
1507 			}
1508 			{
1509 				char [] c = { 'a', 'b', 'c', 'd', 'e' };
1510 				ArrayList al1 = new ArrayList (c);
1511 				al1.Reverse (2, 1);
1512 				for (int i = 0; i < al1.Count; i++) {
1513 					Assert.AreEqual (c [i], al1 [i], "Should be no change yet");
1514 				}
1515 				al1.Reverse ();
1516 				for (int i = 0; i < al1.Count; i++) {
1517 					Assert.AreEqual (c [i], al1 [4 - i], "Should be reversed");
1518 				}
1519 				al1.Reverse ();
1520 				for (int i = 0; i < al1.Count; i++) {
1521 					Assert.AreEqual (c [i], al1 [i], "Should be back to normal");
1522 				}
1523 				al1.Reverse (1, 3);
1524 				Assert.AreEqual (c [0], al1 [0], "Should be back to normal");
1525 				Assert.AreEqual (c [3], al1 [1], "Should be back to normal");
1526 				Assert.AreEqual (c [2], al1 [2], "Should be back to normal");
1527 				Assert.AreEqual (c [1], al1 [3], "Should be back to normal");
1528 				Assert.AreEqual (c [4], al1 [4], "Should be back to normal");
1529 			}
1530 		}
1531 
1532 		[Test]
1533 		[ExpectedException (typeof (ArgumentException))]
Reverse_IndexOverflow()1534 		public void Reverse_IndexOverflow ()
1535 		{
1536 			ArrayList al = new ArrayList ();
1537 			al.Add (this);
1538 			al.Reverse (Int32.MaxValue, 1);
1539 		}
1540 
1541 		[Test]
1542 		[ExpectedException (typeof (ArgumentException))]
Reverse_CountOverflow()1543 		public void Reverse_CountOverflow ()
1544 		{
1545 			ArrayList al = new ArrayList ();
1546 			al.Add (this);
1547 			al.Reverse (1, Int32.MaxValue);
1548 		}
1549 
1550 		[Test]
TestSetRange()1551 		public void TestSetRange ()
1552 		{
1553 			{
1554 				bool errorThrown = false;
1555 				try {
1556 					char [] c = { 'a', 'b', 'c' };
1557 					ArrayList al1 =
1558 						ArrayList.ReadOnly (new ArrayList (3));
1559 					al1.SetRange (0, c);
1560 				} catch (NotSupportedException) {
1561 					errorThrown = true;
1562 				} catch (Exception e) {
1563 					Assert.Fail ("Incorrect exception thrown at 1: " + e.ToString ());
1564 				}
1565 				Assert.IsTrue (errorThrown, "setrange on read only error not thrown");
1566 			}
1567 			{
1568 				bool errorThrown = false;
1569 				try {
1570 					ArrayList al1 = new ArrayList (3);
1571 					al1.SetRange (0, null);
1572 				} catch (ArgumentNullException) {
1573 					errorThrown = true;
1574 				} catch (ArgumentOutOfRangeException) {
1575 					errorThrown = true;
1576 				} catch (Exception e) {
1577 					Assert.Fail ("Incorrect exception thrown at 2: " + e.ToString ());
1578 				}
1579 				Assert.IsTrue (errorThrown, "setrange with null error not thrown");
1580 			}
1581 			{
1582 				bool errorThrown = false;
1583 				try {
1584 					char [] c = { 'a', 'b', 'c' };
1585 					ArrayList al1 = new ArrayList (3);
1586 					al1.SetRange (-1, c);
1587 				} catch (ArgumentOutOfRangeException) {
1588 					errorThrown = true;
1589 				} catch (Exception e) {
1590 					Assert.Fail ("Incorrect exception thrown at 3: " + e.ToString ());
1591 				}
1592 				Assert.IsTrue (errorThrown, "setrange with negative index error not thrown");
1593 			}
1594 			{
1595 				bool errorThrown = false;
1596 				try {
1597 					char [] c = { 'a', 'b', 'c' };
1598 					ArrayList al1 = new ArrayList (3);
1599 					al1.SetRange (2, c);
1600 				} catch (ArgumentOutOfRangeException) {
1601 					errorThrown = true;
1602 				} catch (Exception e) {
1603 					Assert.Fail ("Incorrect exception thrown at 4: " + e.ToString ());
1604 				}
1605 				Assert.IsTrue (errorThrown, "setrange with too much error not thrown");
1606 			}
1607 
1608 			{
1609 				char [] c = { 'a', 'b', 'c' };
1610 				ArrayList al1 = ArrayList.Repeat ('?', 3);
1611 				Assert.IsTrue (c [0] != (char) al1 [0], "no match yet");
1612 				Assert.IsTrue (c [1] != (char) al1 [1], "no match yet");
1613 				Assert.IsTrue (c [2] != (char) al1 [2], "no match yet");
1614 				al1.SetRange (0, c);
1615 				Assert.AreEqual (c [0], al1 [0], "should match");
1616 				Assert.AreEqual (c [1], al1 [1], "should match");
1617 				Assert.AreEqual (c [2], al1 [2], "should match");
1618 			}
1619 		}
1620 
1621 		[Test]
1622 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
SetRange_Overflow()1623 		public void SetRange_Overflow ()
1624 		{
1625 			ArrayList al = new ArrayList ();
1626 			al.Add (this);
1627 			al.SetRange (Int32.MaxValue, new ArrayList ());
1628 		}
1629 
1630 		[Test]
TestInsertRange_this()1631 		public void TestInsertRange_this ()
1632 		{
1633 			String [] s1 = { "this", "is", "a", "test" };
1634 			ArrayList al = new ArrayList (s1);
1635 			al.InsertRange (2, al);
1636 			String [] s2 = { "this", "is", "this", "is", "a", "test", "a", "test" };
1637 			for (int i = 0; i < al.Count; i++) {
1638 				Assert.AreEqual (s2 [i], al [i], "at i=" + i);
1639 			}
1640 		}
1641 
1642 		[Test]
TestSort()1643 		public void TestSort ()
1644 		{
1645 			{
1646 				bool errorThrown = false;
1647 				try {
1648 					ArrayList al1 =
1649 						ArrayList.ReadOnly (new ArrayList ());
1650 					al1.Sort ();
1651 				} catch (NotSupportedException) {
1652 					errorThrown = true;
1653 				}
1654 				Assert.IsTrue (errorThrown, "sort on read only error not thrown");
1655 			}
1656 			{
1657 				char [] starter = { 'd', 'b', 'f', 'e', 'a', 'c' };
1658 				ArrayList al1 = new ArrayList (starter);
1659 				al1.Sort ();
1660 				Assert.AreEqual ('a', al1 [0], "Should be sorted");
1661 				Assert.AreEqual ('b', al1 [1], "Should be sorted");
1662 				Assert.AreEqual ('c', al1 [2], "Should be sorted");
1663 				Assert.AreEqual ('d', al1 [3], "Should be sorted");
1664 				Assert.AreEqual ('e', al1 [4], "Should be sorted");
1665 				Assert.AreEqual ('f', al1 [5], "Should be sorted");
1666 			}
1667 			{
1668 				ArrayList al1 = new ArrayList ();
1669 				al1.Add (null);
1670 				al1.Add (null);
1671 				al1.Add (32);
1672 				al1.Add (33);
1673 				al1.Add (null);
1674 				al1.Add (null);
1675 
1676 				al1.Sort ();
1677 				Assert.AreEqual (null, al1 [0], "Should be null (0)");
1678 				Assert.AreEqual (null, al1 [1], "Should be null (1)");
1679 				Assert.AreEqual (null, al1 [2], "Should be null (2)");
1680 				Assert.AreEqual (null, al1 [3], "Should be null (3)");
1681 				Assert.AreEqual (32, al1 [4], "Should be 32");
1682 				Assert.AreEqual (33, al1 [5], "Should be 33");
1683 			}
1684 		}
1685 
1686 		[Test]
1687 		[ExpectedException (typeof (ArgumentException))]
Sort_IndexOverflow()1688 		public void Sort_IndexOverflow ()
1689 		{
1690 			ArrayList al = new ArrayList ();
1691 			al.Add (this);
1692 			al.Sort (Int32.MaxValue, 1, null);
1693 		}
1694 
1695 		[Test]
1696 		[ExpectedException (typeof (ArgumentException))]
Sort_CountOverflow()1697 		public void Sort_CountOverflow ()
1698 		{
1699 			ArrayList al = new ArrayList ();
1700 			al.Add (this);
1701 			al.Sort (1, Int32.MaxValue, null);
1702 		}
1703 
1704 		// TODO - Sort with IComparers
1705 
1706 		// TODO - Synchronize
1707 
1708 		[Test]
TestToArray()1709 		public void TestToArray ()
1710 		{
1711 			{
1712 				bool errorThrown = false;
1713 				try {
1714 					ArrayList al1 = new ArrayList (3);
1715 					al1.ToArray (null);
1716 				} catch (ArgumentNullException) {
1717 					errorThrown = true;
1718 				}
1719 				Assert.IsTrue (errorThrown, "toarray with null error not thrown");
1720 			}
1721 			{
1722 				bool errorThrown = false;
1723 				try {
1724 					char [] c = { 'a', 'b', 'c' };
1725 					string s = "huh?";
1726 					ArrayList al1 = new ArrayList (c);
1727 					al1.ToArray (s.GetType ());
1728 				} catch (InvalidCastException) {
1729 					errorThrown = true;
1730 				}
1731 				Assert.IsTrue (errorThrown, "toarray with bad type error not thrown");
1732 			}
1733 			{
1734 				char [] c1 = { 'a', 'b', 'c', 'd', 'e' };
1735 				ArrayList al1 = new ArrayList (c1);
1736 				object [] o2 = al1.ToArray ();
1737 				for (int i = 0; i < c1.Length; i++) {
1738 					Assert.AreEqual (c1 [i], o2 [i], "should be copy");
1739 				}
1740 				Array c2 = al1.ToArray (c1 [0].GetType ());
1741 				for (int i = 0; i < c1.Length; i++) {
1742 					Assert.AreEqual (c1 [i], c2.GetValue (i), "should be copy");
1743 				}
1744 			}
1745 		}
1746 
1747 		[Test]
1748 		[ExpectedException (typeof (NotSupportedException))]
TrimToSize_ReadOnly()1749 		public void TrimToSize_ReadOnly ()
1750 		{
1751 			ArrayList al1 = ArrayList.ReadOnly (new ArrayList ());
1752 			al1.TrimToSize ();
1753 		}
1754 
1755 		[Test]
TrimToSize()1756 		public void TrimToSize ()
1757 		{
1758 			ArrayList al1 = new ArrayList ();
1759 		// Capacity is 0 under 2.0
1760 		int capacity = 4;
1761 			int size = capacity / 2;
1762 			for (int i = 1; i <= size; i++) {
1763 				al1.Add ('?');
1764 			}
1765 			al1.RemoveAt (0);
1766 			al1.TrimToSize ();
1767 			Assert.AreEqual (size - 1, al1.Capacity, "no capacity match");
1768 
1769 			al1.Clear ();
1770 			al1.TrimToSize ();
1771 			Assert.AreEqual (capacity, al1.Capacity, "no default capacity");
1772 		}
1773 
1774 		class Comparer : IComparer
1775 		{
1776 
1777 			private bool called = false;
1778 
1779 			public bool Called
1780 			{
1781 				get
1782 				{
1783 					bool result = called;
1784 					called = false;
1785 					return called;
1786 				}
1787 			}
1788 
Compare(object x, object y)1789 			public int Compare (object x, object y)
1790 			{
1791 				called = true;
1792 				return 0;
1793 			}
1794 		}
1795 
1796 		[Test]
BinarySearch1_EmptyList()1797 		public void BinarySearch1_EmptyList ()
1798 		{
1799 			ArrayList list = new ArrayList ();
1800 			Assert.AreEqual (-1, list.BinarySearch (0), "BinarySearch");
1801 		}
1802 
1803 		[Test]
BinarySearch2_EmptyList()1804 		public void BinarySearch2_EmptyList ()
1805 		{
1806 			Comparer comparer = new Comparer ();
1807 			ArrayList list = new ArrayList ();
1808 			Assert.AreEqual (-1, list.BinarySearch (0, comparer), "BinarySearch");
1809 			// bug 77030 - the comparer isn't called for an empty array/list
1810 			Assert.IsTrue (!comparer.Called, "Called");
1811 		}
1812 
1813 		[Test]
BinarySearch3_EmptyList()1814 		public void BinarySearch3_EmptyList ()
1815 		{
1816 			Comparer comparer = new Comparer ();
1817 			ArrayList list = new ArrayList ();
1818 			Assert.AreEqual (-1, list.BinarySearch (0, 0, 0, comparer), "BinarySearch");
1819 			// bug 77030 - the comparer isn't called for an empty array/list
1820 			Assert.IsTrue (!comparer.Called, "Called");
1821 		}
1822 
1823 		[Test]
AddRange_GetRange()1824 		public void AddRange_GetRange ()
1825 		{
1826 			ArrayList source = ArrayList.Adapter (new object [] { "1", "2" });
1827 			Assert.AreEqual (2, source.Count, "#1");
1828 			Assert.AreEqual ("1", source [0], "#2");
1829 			Assert.AreEqual ("2", source [1], "#3");
1830 			ArrayList range = source.GetRange (1, 1);
1831 			Assert.AreEqual (1, range.Count, "#4");
1832 			Assert.AreEqual ("2", range [0], "#5");
1833 			ArrayList target = new ArrayList ();
1834 			target.AddRange (range);
1835 			Assert.AreEqual (1, target.Count, "#6");
1836 			Assert.AreEqual ("2", target [0], "#7");
1837 		}
1838 
1839 		[Test]
IterateSelf()1840 		public void IterateSelf ()
1841 		{
1842 			ArrayList list = new ArrayList ();
1843 			list.Add (list);
1844 			IEnumerator enumerator = list.GetEnumerator ();
1845 			Assert.IsTrue (enumerator.MoveNext (), "#1");
1846 			Assert.IsTrue (object.ReferenceEquals (list, enumerator.Current), "#2");
1847 			Assert.IsTrue (!enumerator.MoveNext (), "#3");
1848 		}
1849 	}
1850 }
1851