Merge pull request #799 from kebby/master
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / ListTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.ListTest
3 //
4 // Authors:
5 //      David Waite (mass@akuma.org)
6 //      Andres G. Aragoneses (andres.aragoneses@7digital.com)
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2005 David Waite (mass@akuma.org)
10 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Copyright 2012 7digital Ltd (http://www.7digital.com).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34
35 using System;
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Collections.ObjectModel;
39 using System.IO;
40 using System.Runtime.Serialization.Formatters.Binary;
41 using System.Text;
42
43 using NUnit.Framework;
44
45 namespace MonoTests.System.Collections.Generic {
46
47         class GenericComparer<T> : IComparer<T> {
48
49                 private bool called = false;
50
51                 public bool Called {
52                         get {
53                                 bool result = called;
54                                 called = false;
55                                 return called;
56                         }
57                 }
58
59                 public int Compare (T x, T y)
60                 {
61                         called = true;
62                         return 0;
63                 }
64         }
65
66         [TestFixture]
67         public class ListTest
68         {
69                 static byte [] _serializedList = new byte [] {
70                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
71                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
72                         0x7e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
73                         0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65,
74                         0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x60,
75                         0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
76                         0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72,
77                         0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
78                         0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20,
79                         0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75,
80                         0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69,
81                         0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62,
82                         0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34,
83                         0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00, 0x00, 0x00, 0x06,
84                         0x5f, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a,
85                         0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x07,
86                         0x00, 0x00, 0x08, 0x08, 0x08, 0x09, 0x02, 0x00, 0x00, 0x00, 0x03,
87                         0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x02, 0x00, 0x00,
88                         0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00,
89                         0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90                         0x0b };
91                 int [] _list1_contents;
92                 List <int> _list1;
93
94                 [SetUp]
95                 public void SetUp ()
96                 {
97                         // FIXME arrays currently do not support generic collection
98                         // interfaces
99                         _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
100                         // _list1 = new List <int> (_list1_contents);
101                         
102                         _list1 = new List <int> (8);
103                         foreach (int i in _list1_contents)
104                                 _list1.Add (i);
105                 }
106
107                 [Test]  // This was for bug #74980
108                 public void InsertTest ()
109                 {
110                         List <string> test = new List <string> ();
111                         test.Insert (0, "a");
112                         test.Insert (0, "b");
113                         test.Insert (1, "c");
114
115                         Assert.AreEqual (3, test.Count);
116                         Assert.AreEqual ("b", test [0]);
117                         Assert.AreEqual ("c", test [1]);
118                         Assert.AreEqual ("a", test [2]);
119                 }
120
121                 [Test]
122                 public void InsertRangeTest ()
123                 {
124                         int count = _list1.Count;
125                         // FIXME arrays currently do not support generic collection 
126                         // interfaces
127                         int [] items = {1, 2, 3};
128                         // List <int> newRange = new List <int> (items);
129                         List <int> newRange = new List <int> (3);
130                         foreach (int i in items)
131                                 newRange.Add (i);
132                         _list1.InsertRange (1, newRange);
133                         Assert.AreEqual (count + 3, _list1.Count);
134                         Assert.AreEqual (55, _list1 [0]);
135                         Assert.AreEqual (1, _list1 [1]);
136                         Assert.AreEqual (2, _list1 [2]);
137                         Assert.AreEqual (3, _list1 [3]);
138                         Assert.AreEqual (50, _list1 [4]);
139
140                         newRange = new List <int> ();
141                         List <int> li = new List <int> ();
142                         li.Add (1);
143                         newRange.InsertRange (0, li);
144                         newRange.InsertRange (newRange.Count, li);
145                         Assert.AreEqual (2, newRange.Count);
146                 }
147                 
148                 [Test]
149                 public void InsertSelfTest()
150                 {
151                         List <int> range = new List <int> (5);
152                         for (int i = 0; i < 5; ++ i)
153                                 range.Add (i);
154                         
155                         range.InsertRange(2, range);
156                         Assert.AreEqual (10, range.Count);
157                         Assert.AreEqual (0, range [0]);
158                         Assert.AreEqual (1, range [1]);
159                         Assert.AreEqual (0, range [2]);
160                         Assert.AreEqual (1, range [3]);
161                         Assert.AreEqual (2, range [4]);
162                         Assert.AreEqual (3, range [5]);
163                         Assert.AreEqual (4, range [6]);
164                         Assert.AreEqual (2, range [7]);
165                         Assert.AreEqual (3, range [8]);
166                         Assert.AreEqual (4, range [9]);
167                 }
168
169                 [Test, ExpectedException (typeof (ArgumentNullException))]
170                 public void InsertRangeNullTest ()
171                 {
172                         IEnumerable <int> n = null;
173                         _list1.InsertRange (0, n);
174                 }
175
176                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
177                 public void InsertRangeNegativeIndexTest ()
178                 {
179                         _list1.InsertRange (-1, _list1);
180                 }
181
182                 [Test]
183                 public void IndexOfTest ()
184                 {
185                         List <int> l = new List <int> ();
186
187                         l.Add (100);
188                         l.Add (200);
189
190                         Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
191                 }
192                 
193                 [Test, ExpectedException(typeof (ArgumentException))]
194                 public void IList_InsertInvalidType1 ()
195                 {
196                         IList list = _list1 as IList;
197                         list.Insert(0, new object());
198                 }
199
200                 [Test, ExpectedException(typeof (ArgumentException))]
201                 public void IList_InsertInvalidType2 ()
202                 {
203                         IList list = _list1 as IList;
204                         list.Insert(0, null);
205                 }
206                 
207                 [Test, ExpectedException(typeof (ArgumentException))]
208                 public void IList_AddInvalidType1()
209                 {
210                         IList list = _list1 as IList;
211                         list.Add(new object());
212                 }
213
214                 [Test, ExpectedException(typeof (ArgumentException))]
215                 public void IList_AddInvalidType2()
216                 {
217                         IList list = _list1 as IList;
218                         list.Add(null);
219                 }
220                 
221                 [Test]
222                 public void IList_RemoveInvalidType()
223                 {
224                         IList list = _list1 as IList;
225                         int nCount = list.Count;
226                         list.Remove(new object());
227                         Assert.AreEqual(nCount, list.Count);
228
229                         list.Remove(null);
230                         Assert.AreEqual(nCount, list.Count);
231                 }
232
233                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
234                 public void IndexOfOutOfRangeTest ()
235                 {
236                         List <int> l = new List <int> (4);
237                         l.IndexOf (0, 0, 4);
238                 }
239
240                 [Test]
241                 public void GetRangeTest ()
242                 {
243                         List <int> r = _list1.GetRange (2, 4);
244                         Assert.AreEqual (4, r.Count);
245                         Assert.AreEqual (22, r [0]);
246                         Assert.AreEqual (80, r [1]);
247                         Assert.AreEqual (56, r [2]);
248                         Assert.AreEqual (52, r [3]);
249                 }
250
251                 [Test]
252                 public void EnumeratorTest ()
253                 {
254                         List <int>.Enumerator e = _list1.GetEnumerator ();
255                         for (int i = 0; i < _list1_contents.Length; i++)
256                         {
257                                 Assert.IsTrue (e.MoveNext ());
258                                 Assert.AreEqual (_list1_contents [i], e.Current);
259                         }
260                         Assert.IsFalse (e.MoveNext ());
261                 }
262
263                 [Test]
264                 public void ConstructWithSizeTest ()
265                 {
266                         List <object> l_1 = new List <object> (1);
267                         List <object> l_2 = new List <object> (50);
268                         List <object> l_3 = new List <object> (0);
269
270                         Assert.AreEqual (1, l_1.Capacity);
271                         Assert.AreEqual (50, l_2.Capacity);
272                         Assert.AreEqual (0, l_3.Capacity);
273                 }
274
275                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
276                 public void ConstructWithInvalidSizeTest ()
277                 {
278                         List <int> l = new List <int> (-1);
279                 }
280
281                 [Test]
282                 public void ConstructWithCollectionTest ()
283                 {
284                         List <int> l1 = new List <int> (_list1);
285                         Assert.AreEqual (_list1.Count, l1.Count);
286                         Assert.AreEqual (l1.Count, l1.Capacity);
287                         for (int i = 0; i < l1.Count; i++)
288                                 Assert.AreEqual (_list1 [i], l1 [i]);
289                 }
290
291                 [Test, ExpectedException (typeof (ArgumentNullException))]
292                 public void ConstructWithInvalidCollectionTest ()
293                 {
294                         List <int> n = null;
295                         List <int> l1 = new List <int> (n);
296                 }
297
298                 [Test]
299                 public void AddTest ()
300                 {
301                         int count = _list1.Count;
302                         _list1.Add (-1);
303                         Assert.AreEqual (count + 1, _list1.Count);
304                         Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
305                 }
306
307                 [Test]
308                 public void AddRangeTest ()
309                 {
310                         int count = _list1.Count;
311                         // FIXME arrays currently do not support generic collection
312                         // interfaces
313                         int [] range = { -1, -2, -3 };
314                         List <int> tmp = new List <int> (3);
315                         foreach (int i in range)
316                                 tmp.Add (i);
317                         // _list1.AddRange (range);
318                         _list1.AddRange (tmp);
319                         
320                         Assert.AreEqual (count + 3, _list1.Count);
321                         Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
322                         Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
323                         Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
324                 }
325
326                 [Test, ExpectedException (typeof (ArgumentNullException))]
327                 public void AddNullRangeTest ()
328                 {
329                         int [] n = null;
330                         _list1.AddRange (n);
331                 }
332
333                 [Test]
334                 public void BinarySearchTest ()
335                 {
336                         List <int> l = new List <int> (_list1);
337                         l.Sort ();
338                         Assert.AreEqual (0, l.BinarySearch (22));
339                         Assert.AreEqual (-2, l.BinarySearch (23));
340                         Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
341                 }
342
343 #if !NET_4_0 // FIXME: the blob contains the 2.0 mscorlib version
344
345                 [Test]
346                 [Category ("TargetJvmNotWorking")]
347                 public void SerializeTest ()
348                 {
349                         List <int> list = new List <int> ();
350                         list.Add (5);
351                         list.Add (0);
352                         list.Add (7);
353
354 #if TARGET_JVM
355                         BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
356 #else
357                         BinaryFormatter bf = new BinaryFormatter ();
358 #endif // TARGET_JVM
359                         MemoryStream ms = new MemoryStream ();
360                         bf.Serialize (ms, list);
361
362                         byte [] buffer = new byte [ms.Length];
363                         ms.Position = 0;
364                         ms.Read (buffer, 0, buffer.Length);
365
366                         Assert.AreEqual (_serializedList, buffer);
367                 }
368
369 #endif
370
371                 [Test]
372                 [Category ("TargetJvmNotWorking")]
373                 public void DeserializeTest ()
374                 {
375                         MemoryStream ms = new MemoryStream ();
376                         ms.Write (_serializedList, 0, _serializedList.Length);
377                         ms.Position = 0;
378
379 #if TARGET_JVM
380                         BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
381 #else
382                         BinaryFormatter bf = new BinaryFormatter ();
383 #endif // TARGET_JVM
384                         List<int> list = (List<int>) bf.Deserialize (ms);
385                         Assert.AreEqual (3, list.Count, "#1");
386                         Assert.AreEqual (5, list [0], "#2");
387                         Assert.AreEqual (0, list [1], "#3");
388                         Assert.AreEqual (7, list [2], "#4");
389                 }
390
391                 [Test]
392                 public void SortTest ()
393                 {
394                         List <int> l = new List <int> (_list1);
395                         l.Sort ();
396                         Assert.AreEqual (_list1.Count, l.Count);
397                         Assert.AreEqual (22, l [0]);
398                         int minimum = 22;
399                         foreach (int i in l)
400                         {
401                                 Assert.IsTrue (minimum <= i);
402                                 minimum = i;
403                         }
404                 }
405
406                 [Test]
407                 public void ClearTest ()
408                 {
409                         int capacity = _list1.Capacity;
410                         _list1.Clear ();
411                         Assert.AreEqual (0, _list1.Count);
412                         Assert.AreEqual (capacity, _list1.Capacity);
413                 }
414
415                 [Test]
416                 public void ContainsTest ()
417                 {
418                         Assert.IsTrue (_list1.Contains (22));
419                         Assert.IsFalse (_list1.Contains (23));
420                 }
421
422                 private string StringConvert (int i)
423                 {
424                         return i.ToString ();
425                 }
426                 
427                 [Test]
428                 public void ConvertAllTest ()
429                 {
430                         List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
431                         Assert.AreEqual (_list1.Count, s.Count);
432                         Assert.AreEqual ("55", s [0]);
433                 }
434
435                 [Test]
436                 public void CopyToTest ()
437                 {
438                         int [] a = new int [2];
439                         _list1.CopyTo (1, a, 0, 2);
440                         Assert.AreEqual (50, a [0]);
441                         Assert.AreEqual (22, a [1]);
442
443                         int [] b = new int [_list1.Count + 1];
444                         b [_list1.Count] = 555;
445                         _list1.CopyTo (b);
446                         Assert.AreEqual (55, b [0]);
447                         Assert.AreEqual (555, b [_list1.Count]);
448
449                         b [0] = 888;
450                         _list1.CopyTo (b, 1);
451                         Assert.AreEqual (888, b [0]);
452                         Assert.AreEqual (55, b [1]);
453                 }
454
455                 [Test, ExpectedException (typeof (ArgumentNullException))]
456                 public void CopyToNullTest ()
457                 {
458                         int [] a = null;
459                         _list1.CopyTo (0, a, 0, 0);
460                 }
461
462                 static bool FindMultipleOfThree (int i)
463                 {
464                         return (i % 3) == 0;
465                 }
466
467                 static bool FindMultipleOfFour (int i)
468                 {
469                         return (i % 4) == 0;
470                 }
471
472                 static bool FindMultipleOfTwelve (int i)
473                 {
474                         return (i % 12) == 0;
475                 }
476
477                 [Test]
478                 public void FindTest ()
479                 {
480                         int i = _list1.Find (FindMultipleOfThree);
481                         Assert.AreEqual (63, i);
482
483                         i = _list1.Find (FindMultipleOfTwelve);
484                         Assert.AreEqual (default (int), i);
485                 }
486
487                 [Test, ExpectedException (typeof (ArgumentNullException))]
488                 public void FindNullTest ()
489                 {
490                         int i = _list1.Find (null);
491                 }
492
493                 [Test]
494                 public void FindAllSmallTest ()
495                 {
496                         List <int> findings = _list1.FindAll (FindMultipleOfFour);
497                         Assert.AreEqual (4, findings.Count);
498                         Assert.AreEqual (80, findings [0]);
499                         Assert.AreEqual (56, findings [1]);
500                         Assert.AreEqual (52, findings [2]);
501                         Assert.AreEqual (40, findings [3]);
502
503                         findings = _list1.FindAll (FindMultipleOfTwelve);
504                         Assert.IsNotNull (findings);
505                         Assert.AreEqual (0, findings.Count);
506                 }
507                 
508                 [Test]
509                 public void FindAllMediumTest ()
510                 {
511                         List <int> integers = new List <int> (10000);
512                         for (int i = 1; i <= 10000; i++)
513                                 integers.Add (i);
514                         
515                         List <int> results = integers.FindAll (FindMultipleOfFour);
516                         
517                         Assert.IsNotNull (results);
518                         Assert.AreEqual (2500, results.Count);
519                         
520                         results = integers.FindAll (FindMultipleOfTwelve);
521                         
522                         Assert.IsNotNull (results);
523                         Assert.AreEqual (833, results.Count);
524                 }
525                 
526                 [Test]
527                 public void FindAllLargeTest ()
528                 {
529                         List <int> integers = new List <int> (70000);
530                         for (int i = 1; i <= 80000; i++)
531                                 integers.Add (i);
532                         
533                         List <int> results = integers.FindAll (FindMultipleOfFour);
534                         
535                         Assert.IsNotNull (results);
536                         Assert.AreEqual (20000, results.Count);
537                         
538                         results = integers.FindAll (FindMultipleOfTwelve);
539                         
540                         Assert.IsNotNull (results);
541                         Assert.AreEqual (6666, results.Count);
542                 }
543
544                 [Test, ExpectedException (typeof (ArgumentNullException))]
545                 public void FindAllNullTest ()
546                 {
547                         List <int> findings = _list1.FindAll (null);
548                 }
549
550                 [Test]
551                 public void FindIndexTest ()
552                 {
553                         int i = _list1.FindIndex (FindMultipleOfThree);
554                         Assert.AreEqual (7, i);
555
556                         i = _list1.FindIndex (FindMultipleOfTwelve);
557                         Assert.AreEqual (-1, i);
558
559                         var a = new List<int> () { 2, 2, 2, 3, 2 };
560                         Assert.AreEqual (2, a.FindIndex (2, 2, l => true));
561                 }
562
563                 [Test]
564                 public void FindIndex_Invalid ()
565                 {
566                         try {
567                                 _list1.FindIndex (null);
568                                 Assert.Fail ("#1");
569                         } catch (ArgumentNullException) {
570                         }
571
572                         try {
573                                 _list1.FindIndex (-1, l => true);
574                                 Assert.Fail ("#2");
575                         } catch (ArgumentOutOfRangeException) {
576                         }
577
578                         try {
579                                 _list1.FindIndex (-1, 0, l => true);
580                                 Assert.Fail ("#2b");
581                         } catch (ArgumentOutOfRangeException) {
582                         }
583
584                         try {
585                                 _list1.FindIndex (0, -1, l => true);
586                                 Assert.Fail ("#3");
587                         } catch (ArgumentOutOfRangeException) {
588                         }
589
590                         try {
591                                 _list1.FindIndex (100, l => true);
592                                 Assert.Fail ("#4");
593                         } catch (ArgumentOutOfRangeException) {
594                         }
595
596                         try {
597                                 _list1.FindIndex (100, 0, l => true);
598                                 Assert.Fail ("#4b");
599                         } catch (ArgumentOutOfRangeException) {
600                         }
601
602                         try {
603                                 _list1.FindIndex (7, 2, l => true);
604                                 Assert.Fail ("#5");
605                         } catch (ArgumentOutOfRangeException) {
606                         }
607                 }
608
609                 [Test]
610                 public void FindLastTest ()
611                 {
612                         int i = _list1.FindLast (FindMultipleOfFour);
613                         Assert.AreEqual (40, i);
614
615                         i = _list1.FindLast (FindMultipleOfTwelve);
616                         Assert.AreEqual (default (int), i);
617                 }
618
619                 [Test, ExpectedException (typeof (ArgumentNullException))]
620                 public void FindLastNullTest ()
621                 {
622                         int i = _list1.FindLast (null);
623                 }
624
625                 [Test]
626                 public void ForEachTest ()
627                 {
628                         int i = 0;
629                         _list1.ForEach (delegate (int j) { i += j; });
630
631                         Assert.AreEqual (418, i);
632                 }
633
634                 [Test]
635                 public void FindLastIndexTest ()
636                 {
637                         int i = _list1.FindLastIndex (FindMultipleOfFour);
638                         Assert.AreEqual (6, i);
639
640                         i = _list1.FindLastIndex (5, FindMultipleOfFour);
641                         Assert.AreEqual (5, i);
642
643                         i = _list1.FindIndex (FindMultipleOfTwelve);
644                         Assert.AreEqual (-1, i);
645
646                         Assert.AreEqual (2, _list1.FindLastIndex (2, 3, l => true));
647                         Assert.AreEqual (2, _list1.FindLastIndex (2, 2, l => true));
648                         Assert.AreEqual (1, _list1.FindLastIndex (1, 2, l => true));
649                 }
650
651                 [Test]
652                 public void FindLastIndex_Invalid ()
653                 {
654                         try {
655                                 _list1.FindLastIndex (null);
656                                 Assert.Fail ("#1");
657                         } catch (ArgumentNullException) {
658                         }
659
660                         try {
661                                 _list1.FindLastIndex (-1, l => true);
662                                 Assert.Fail ("#2");
663                         } catch (ArgumentOutOfRangeException) {
664                         }
665
666                         try {
667                                 _list1.FindLastIndex (-1, 0, l => true);
668                                 Assert.Fail ("#2b");
669                         } catch (ArgumentOutOfRangeException) {
670                         }
671
672                         try {
673                                 _list1.FindLastIndex (0, -1, l => true);
674                                 Assert.Fail ("#3");
675                         } catch (ArgumentOutOfRangeException) {
676                         }
677
678                         try {
679                                 _list1.FindLastIndex (100, l => true);
680                                 Assert.Fail ("#4");
681                         } catch (ArgumentOutOfRangeException) {
682                         }
683
684                         try {
685                                 _list1.FindLastIndex (100, 0, l => true);
686                                 Assert.Fail ("#4b");
687                         } catch (ArgumentOutOfRangeException) {
688                         }
689
690                         try {
691                                 _list1.FindLastIndex (2, 4, l => true);
692                                 Assert.Fail ("#5");
693                         } catch (ArgumentOutOfRangeException) {
694                         }
695                 }
696
697                 [Test]
698                 public void RemoveTest ()
699                 {
700                         int count = _list1.Count;
701                         bool result = _list1.Remove (22);
702                         Assert.IsTrue (result);
703                         Assert.AreEqual (count - 1, _list1.Count);
704
705                         Assert.AreEqual (-1, _list1.IndexOf (22));
706
707                         result = _list1.Remove (0);
708                         Assert.IsFalse (result);
709                 }
710
711                 [Test]
712                 public void RemoveAllTest ()
713                 {
714                         int count = _list1.Count;
715                         int removedCount = _list1.RemoveAll (FindMultipleOfFour);
716                         Assert.AreEqual (4, removedCount);
717                         Assert.AreEqual (count - 4, _list1.Count);
718
719                         removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
720                         Assert.AreEqual (0, removedCount);
721                         Assert.AreEqual (count - 4, _list1.Count);
722                 }
723
724                 [Test]
725                 public void RemoveAtTest ()
726                 {
727                         int count = _list1.Count;
728                         _list1.RemoveAt (0);
729                         Assert.AreEqual (count - 1, _list1.Count);
730                         Assert.AreEqual (50, _list1 [0]);
731                 }
732
733                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
734                 public void RemoveOutOfRangeTest ()
735                 {
736                         _list1.RemoveAt (_list1.Count);
737                 }
738
739                 [Test]
740                 public void RemoveRangeTest ()
741                 {
742                         int count = _list1.Count;
743                         _list1.RemoveRange (1, 2);
744                         Assert.AreEqual (count - 2, _list1.Count);
745                         Assert.AreEqual (55, _list1 [0]);
746                         Assert.AreEqual (80, _list1 [1]);
747
748                         _list1.RemoveRange (0, 0);
749                         Assert.AreEqual (count - 2, _list1.Count);
750                 }
751
752                 [Test]
753                 public void RemoveRangeFromEmptyListTest ()
754                 {
755                         List<int> l = new List<int> ();
756                         l.RemoveRange (0, 0);
757                 }
758
759                 [Test, ExpectedException (typeof (ArgumentException))]
760                 public void RemoveRangeOutOfRangeTest ()
761                 {
762                         _list1.RemoveRange (1, _list1.Count);
763                 }
764
765                 [Test]
766                 public void ReverseTest ()
767                 {
768                         int count = _list1.Count;
769                         _list1.Reverse ();
770                         Assert.AreEqual (count, _list1.Count);
771
772                         Assert.AreEqual (63, _list1 [0]);
773                         Assert.AreEqual (55, _list1 [count - 1]);
774
775                         _list1.Reverse (0, 2);
776
777                         Assert.AreEqual (40, _list1 [0]);
778                         Assert.AreEqual (63, _list1 [1]);
779                 }
780
781                 [Test, ExpectedException (typeof (ArgumentException))]
782                 public void ReverseOutOfRangeTest ()
783                 {
784                         _list1.Reverse (1, _list1.Count);
785                 }
786
787                 [Test]
788                 public void ToArrayTest ()
789                 {
790                         int [] copiedContents = _list1.ToArray ();
791                         Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
792
793                         Assert.AreEqual (_list1.Count, copiedContents.Length);
794                         Assert.AreEqual (_list1 [0], copiedContents [0]);
795                 }
796
797                 [Test]
798                 public void TrimExcessTest ()
799                 {
800                         List <string> l = new List <string> ();
801                         l.Add ("foo");
802
803                         Assert.IsTrue (l.Count < l.Capacity);
804                         l.TrimExcess ();
805                         Assert.AreEqual (l.Count, l.Capacity);
806                 }
807
808                 bool IsPositive (int i)
809                 {
810                         return i >= 0;
811                 }
812
813                 [Test]
814                 public void TrueForAllTest ()
815                 {
816                         Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
817                         Assert.IsTrue (_list1.TrueForAll (IsPositive));
818                 }
819
820                 [Test, ExpectedException (typeof (ArgumentNullException))]
821                 public void TrueForAllNullTest ()
822                 {
823                         _list1.TrueForAll (null);
824                 }
825
826                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
827                 public void CapacityOutOfRangeTest ()
828                 {
829                         _list1.Capacity = _list1.Count - 1;
830                 }
831
832                 [Test]
833                 public void BinarySearch_EmptyList ()
834                 {
835                         GenericComparer<int> comparer = new GenericComparer<int> ();
836                         List<int> l = new List<int> ();
837                         Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
838                         // bug 77030 - the comparer isn't called for an empty array/list
839                         Assert.IsFalse (comparer.Called, "Called");
840                 }
841
842                 [Test]
843                 public void BinarySearch2_EmptyList ()
844                 {
845                         GenericComparer<int> comparer = new GenericComparer<int> ();
846                         List<int> l = new List<int> ();
847                         Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
848                         // bug 77030 - the comparer isn't called for an empty array/list
849                         Assert.IsFalse (comparer.Called, "Called");
850                 }
851
852                 [Test]
853                 public void AddRange_Bug77019 ()
854                 {
855                         List<int> l = new List<int> ();
856                         Dictionary<string, int> d = new Dictionary<string, int> ();
857                         l.AddRange (d.Values);
858                         Assert.AreEqual (0, l.Count, "Count");
859                 }
860
861                 [Test]
862                 public void VersionCheck_Add ()
863                 {
864                         List<int> list = new List<int> ();
865                         IEnumerator enumerator = list.GetEnumerator ();
866                         list.Add (5);
867
868                         try {
869                                 enumerator.MoveNext ();
870                                 Assert.Fail ("#1");
871                         } catch (InvalidOperationException) {
872                         }
873
874                         try {
875                                 enumerator.Reset ();
876                                 Assert.Fail ("#2");
877                         } catch (InvalidOperationException) {
878                         }
879
880                         enumerator = list.GetEnumerator ();
881                         enumerator.MoveNext ();
882                 }
883
884                 [Test]
885                 public void VersionCheck_AddRange ()
886                 {
887                         List<int> list = new List<int> ();
888                         IEnumerator enumerator = list.GetEnumerator ();
889                         list.AddRange (new int [] { 5, 7 });
890
891                         try {
892                                 enumerator.MoveNext ();
893                                 Assert.Fail ("#1");
894                         } catch (InvalidOperationException) {
895                         }
896
897                         try {
898                                 enumerator.Reset ();
899                                 Assert.Fail ("#2");
900                         } catch (InvalidOperationException) {
901                         }
902
903                         enumerator = list.GetEnumerator ();
904                         enumerator.MoveNext ();
905                 }
906
907                 [Test]
908                 public void VersionCheck_Clear ()
909                 {
910                         List<int> list = new List<int> ();
911                         IEnumerator enumerator = list.GetEnumerator ();
912                         list.Clear ();
913
914                         try {
915                                 enumerator.MoveNext ();
916                                 Assert.Fail ("#1");
917                         } catch (InvalidOperationException) {
918                         }
919
920                         try {
921                                 enumerator.Reset ();
922                                 Assert.Fail ("#2");
923                         } catch (InvalidOperationException) {
924                         }
925
926                         enumerator = list.GetEnumerator ();
927                         enumerator.MoveNext ();
928                 }
929
930                 [Test]
931                 public void VersionCheck_Insert ()
932                 {
933                         List<int> list = new List<int> ();
934                         IEnumerator enumerator = list.GetEnumerator ();
935                         list.Insert (0, 7);
936
937                         try {
938                                 enumerator.MoveNext ();
939                                 Assert.Fail ("#1");
940                         } catch (InvalidOperationException) {
941                         }
942
943                         try {
944                                 enumerator.Reset ();
945                                 Assert.Fail ("#2");
946                         } catch (InvalidOperationException) {
947                         }
948
949                         enumerator = list.GetEnumerator ();
950                         enumerator.MoveNext ();
951                 }
952
953                 [Test]
954                 public void VersionCheck_InsertRange ()
955                 {
956                         List<int> list = new List<int> ();
957                         IEnumerator enumerator = list.GetEnumerator ();
958                         list.InsertRange (0, new int [] { 5, 7 });
959
960                         try {
961                                 enumerator.MoveNext ();
962                                 Assert.Fail ("#1");
963                         } catch (InvalidOperationException) {
964                         }
965
966                         try {
967                                 enumerator.Reset ();
968                                 Assert.Fail ("#2");
969                         } catch (InvalidOperationException) {
970                         }
971
972                         enumerator = list.GetEnumerator ();
973                         enumerator.MoveNext ();
974                 }
975
976                 [Test]
977                 public void VersionCheck_Remove ()
978                 {
979                         List<int> list = new List<int> ();
980                         list.Add (5);
981                         IEnumerator enumerator = list.GetEnumerator ();
982                         // version number is not incremented if item does not exist in list
983                         list.Remove (7);
984                         enumerator.MoveNext ();
985                         list.Remove (5);
986
987                         try {
988                                 enumerator.MoveNext ();
989                                 Assert.Fail ("#1");
990                         } catch (InvalidOperationException) {
991                         }
992
993                         try {
994                                 enumerator.Reset ();
995                                 Assert.Fail ("#2");
996                         } catch (InvalidOperationException) {
997                         }
998
999                         enumerator = list.GetEnumerator ();
1000                         enumerator.MoveNext ();
1001                 }
1002
1003                 [Test]
1004                 public void VersionCheck_RemoveAll ()
1005                 {
1006                         List<int> list = new List<int> ();
1007                         list.Add (5);
1008                         IEnumerator enumerator = list.GetEnumerator ();
1009                         // version is not incremented if there are no items to remove
1010                         list.RemoveAll (FindMultipleOfFour);
1011                         enumerator.MoveNext ();
1012                         list.Add (4);
1013
1014                         enumerator = list.GetEnumerator ();
1015                         list.RemoveAll (FindMultipleOfFour);
1016
1017                         try {
1018                                 enumerator.MoveNext ();
1019                                 Assert.Fail ("#1");
1020                         } catch (InvalidOperationException) {
1021                         }
1022
1023                         try {
1024                                 enumerator.Reset ();
1025                                 Assert.Fail ("#2");
1026                         } catch (InvalidOperationException) {
1027                         }
1028
1029                         enumerator = list.GetEnumerator ();
1030                         enumerator.MoveNext ();
1031                 }
1032
1033                 [Test]
1034                 public void VersionCheck_RemoveAt ()
1035                 {
1036                         List<int> list = new List<int> ();
1037                         list.Add (5);
1038                         IEnumerator enumerator = list.GetEnumerator ();
1039                         list.RemoveAt (0);
1040
1041                         try {
1042                                 enumerator.MoveNext ();
1043                                 Assert.Fail ("#1");
1044                         } catch (InvalidOperationException) {
1045                         }
1046
1047                         try {
1048                                 enumerator.Reset ();
1049                                 Assert.Fail ("#2");
1050                         } catch (InvalidOperationException) {
1051                         }
1052
1053                         enumerator = list.GetEnumerator ();
1054                         enumerator.MoveNext ();
1055                 }
1056
1057                 [Test]
1058                 public void VersionCheck_RemoveRange ()
1059                 {
1060                         List<int> list = new List<int> ();
1061                         list.Add (5);
1062                         IEnumerator enumerator = list.GetEnumerator ();
1063                         // version is not incremented if count is zero
1064                         list.RemoveRange (0, 0);
1065                         enumerator.MoveNext ();
1066                         enumerator.Reset ();
1067                         list.RemoveRange (0, 1);
1068
1069                         try {
1070                                 enumerator.MoveNext ();
1071                                 Assert.Fail ("#1");
1072                         } catch (InvalidOperationException) {
1073                         }
1074
1075                         try {
1076                                 enumerator.Reset ();
1077                                 Assert.Fail ("#2");
1078                         } catch (InvalidOperationException) {
1079                         }
1080
1081                         enumerator = list.GetEnumerator ();
1082                         enumerator.MoveNext ();
1083                 }
1084
1085                 [Test, ExpectedException (typeof (InvalidOperationException))] // #699182
1086                 public void VersionCheck_Indexer ()
1087                 {
1088                         var list = new List<int> () { 0, 2, 3 };
1089                         var enumerator = list.GetEnumerator ();
1090
1091                         list [0] = 1;
1092
1093                         enumerator.MoveNext ();
1094                 }
1095
1096                 [Test]
1097                 public void VersionCheck_Reverse ()
1098                 {
1099                         List<int> list = new List<int> ();
1100                         IEnumerator enumerator = list.GetEnumerator ();
1101                         list.Reverse ();
1102
1103                         try {
1104                                 enumerator.MoveNext ();
1105                                 Assert.Fail ("#A1");
1106                         } catch (InvalidOperationException) {
1107                         }
1108
1109                         try {
1110                                 enumerator.Reset ();
1111                                 Assert.Fail ("#A2");
1112                         } catch (InvalidOperationException) {
1113                         }
1114
1115                         enumerator = list.GetEnumerator ();
1116                         list.Reverse (0, 0);
1117
1118                         try {
1119                                 enumerator.MoveNext ();
1120                                 Assert.Fail ("#B1");
1121                         } catch (InvalidOperationException) {
1122                         }
1123
1124                         try {
1125                                 enumerator.Reset ();
1126                                 Assert.Fail ("#B2");
1127                         } catch (InvalidOperationException) {
1128                         }
1129
1130                         enumerator = list.GetEnumerator ();
1131                         enumerator.MoveNext ();
1132                 }
1133
1134                 class SortTestComparer: IComparer<string> {
1135
1136                         public int Compare (string s1, string s2)
1137                         {
1138                                 return String.Compare (s1, s2);
1139                         }
1140                 }
1141
1142                 [Test]
1143                 public void Sort_Bug76361 ()
1144                 {
1145                         SortTestComparer comparer = new SortTestComparer ();
1146                         List<string> l = new List<string> ();
1147                         l.Add ("foo");
1148                         l.Add ("bar");
1149                         l.Sort (comparer);
1150                         Assert.AreEqual ("bar", l[0], "0");
1151                         Assert.AreEqual ("foo", l[1], "1");
1152                         Assert.AreEqual (2, l.Count, "Count");
1153                 }
1154
1155                 // for bug #77039 test case
1156                 class GenericIComparable: IComparable<GenericIComparable> {
1157                         private int _NumberToSortOn;
1158
1159                         public int NumberToSortOn {
1160                                 get { return _NumberToSortOn; }
1161                                 set { _NumberToSortOn = value; }
1162                         }
1163
1164                         public GenericIComparable (int val)
1165                         {
1166                                 _NumberToSortOn = val;
1167                         }
1168
1169                         public int CompareTo (GenericIComparable other)
1170                         {
1171                                 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1172                         }
1173                 }
1174
1175                 [Test]
1176                 public void Sort_GenericIComparable_Bug77039 ()
1177                 {
1178                         List<GenericIComparable> l = new List<GenericIComparable> ();
1179                         l.Add (new GenericIComparable (2));
1180                         l.Add (new GenericIComparable (1));
1181                         l.Add (new GenericIComparable (3));
1182                         l.Sort ();
1183                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1184                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1185                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1186                 }
1187
1188                 class NonGenericIComparable: IComparable {
1189                         private int _NumberToSortOn;
1190
1191                         public int NumberToSortOn {
1192                                 get { return _NumberToSortOn; }
1193                                 set { _NumberToSortOn = value; }
1194                         }
1195
1196                         public NonGenericIComparable (int val)
1197                         {
1198                                 _NumberToSortOn = val;
1199                         }
1200
1201                         public int CompareTo (object obj)
1202                         {
1203                                 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1204                         }
1205                 }
1206
1207                 [Test]
1208                 public void Sort_NonGenericIComparable ()
1209                 {
1210                         List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1211                         l.Add (new NonGenericIComparable (2));
1212                         l.Add (new NonGenericIComparable (1));
1213                         l.Add (new NonGenericIComparable (3));
1214                         l.Sort ();
1215                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1216                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1217                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1218                 }
1219
1220                 class NonComparable {
1221                 }
1222
1223                 [Test]
1224                 public void Sort_GenericNonIComparable ()
1225                 {
1226                         List<NonComparable> l = new List<NonComparable> ();
1227                         l.Sort ();
1228                         // no element -> no sort -> no exception
1229                         l.Add (new NonComparable ());
1230                         l.Sort ();
1231                         // one element -> no sort -> no exception
1232                 }
1233
1234                 [Test]
1235                 [ExpectedException (typeof (InvalidOperationException))]
1236                 public void Sort_GenericNonIComparable_2 ()
1237                 {
1238                         List<NonComparable> l = new List<NonComparable> ();
1239                         l.Add (new NonComparable ());
1240                         l.Add (new NonComparable ());
1241                         l.Sort ();
1242                         // two element -> sort -> exception!
1243                 }
1244                 
1245                 [Test]
1246                 public void IList_Contains_InvalidType()
1247                 {
1248                         List<string> list = new List<string>();
1249                         list.Add("foo");
1250                         Assert.IsFalse (((IList)list).Contains(new object()));
1251
1252                         Assert.IsFalse (((IList)_list1).Contains(null));
1253                 }
1254                 
1255                 [Test]
1256                 public void IList_IndexOf_InvalidType()
1257                 {
1258                         List<string> list = new List<string>();
1259                         list.Add("foo");
1260                         Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1261
1262                         Assert.AreEqual (-1, ((IList)_list1).IndexOf(null));
1263                 }
1264
1265                 // for bug #77277 test case
1266                 [Test]
1267                 public void Test_ContainsAndIndexOf_EquatableItem ()
1268                 {
1269                         List<EquatableClass> list = new List<EquatableClass> ();
1270                         EquatableClass item0 = new EquatableClass (0);
1271                         EquatableClass item1 = new EquatableClass (1);
1272
1273                         list.Add (item0);
1274                         list.Add (item1);
1275                         list.Add (item0);
1276
1277                         Assert.AreEqual (true, list.Contains (item0), "#0");
1278                         Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1279                         Assert.AreEqual (0, list.IndexOf (item0), "#2");
1280                         Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1281                         Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1282                         Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1283                 }
1284
1285                 // for bug #81387 test case
1286                 [Test]
1287                 public void Test_Contains_After_Remove ()
1288                 {
1289                         List<int> list = new List<int> ();
1290             list.Add (2);
1291
1292             list.Remove (2);
1293
1294                         Assert.AreEqual (false, list.Contains (2), "#0");
1295                 }
1296                 
1297                 [Test]
1298                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1299                 public void SetItem_OutOfRange()
1300                 {
1301                         List<string> list = new List<string>();
1302                         list[0] = "foo";
1303                 }
1304                 
1305                 [Test]
1306                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1307                 public void SetItem_IList_OutOfRange()
1308                 {
1309                         IList<string> list = new List<string>();
1310                         list[0] = "foo";
1311                 }
1312
1313                 public class EquatableClass : IEquatable<EquatableClass>
1314                 {
1315                         int _x;
1316                         public EquatableClass (int x)
1317                         {
1318                                 _x = x;
1319                         }
1320
1321                         public bool Equals (EquatableClass other)
1322                         {
1323                                 return this._x == other._x;
1324                         }
1325                 }
1326
1327                 delegate void D ();
1328                 bool Throws (D d)
1329                 {
1330                         try {
1331                                 d ();
1332                                 return false;
1333                         } catch {
1334                                 return true;
1335                         }
1336                 }
1337
1338                 [Test]
1339                 // based on #491858, #517415
1340                 public void Enumerator_Current ()
1341                 {
1342                         var e1 = new List<int>.Enumerator ();
1343                         Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1344
1345                         var d = new List<int> ();
1346                         var e2 = d.GetEnumerator ();
1347                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1348                         e2.MoveNext ();
1349                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1350                         e2.Dispose ();
1351                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1352
1353                         var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1354                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1355                         e3.MoveNext ();
1356                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1357                         e3.Dispose ();
1358                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1359
1360                         var e4 = ((IEnumerable) d).GetEnumerator ();
1361                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1362                         e4.MoveNext ();
1363                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1364                         ((IDisposable) e4).Dispose ();
1365                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1366                 }
1367
1368                 [Test] //bug #672907
1369                 public void ICollectionCopyToExceptions ()
1370                 {
1371                         var l = new List <int> ();
1372                         ICollection x = l;
1373                         try {
1374                                 x.CopyTo (null, 0);
1375                                 Assert.Fail ("#1");
1376                         } catch (Exception e) {
1377                                 Assert.IsTrue (e is ArgumentNullException, "#2");
1378                         }
1379
1380                         try {
1381                                 x.CopyTo (new int [10], -1);
1382                                 Assert.Fail ("#3");
1383                         } catch (Exception e) {
1384                                 Assert.IsTrue (e is ArgumentOutOfRangeException, "#4");
1385                         }
1386
1387                         try {
1388                                 x.CopyTo (new int [10, 1], 0);
1389                                 Assert.Fail ("#5");
1390                         } catch (Exception e) {
1391                                 Assert.IsTrue (e is ArgumentException, "#6");
1392                         }
1393
1394                         try {
1395                                 x.CopyTo (Array.CreateInstance (typeof (int), new int [] { 10 }, new int[] { 1 }), 0);
1396                                 Assert.Fail ("#7");
1397                         } catch (Exception e) {
1398                                 Assert.IsTrue (e is ArgumentException, "#8");
1399                         }
1400
1401                         l.Add (10); l.Add (20);
1402                         try {
1403                                 x.CopyTo (new int [1], 0);
1404                                 Assert.Fail ("#9");
1405                         } catch (Exception e) {
1406                                 Assert.IsTrue (e is ArgumentException, "#10");
1407                         }
1408                 }
1409
1410                 [Test]
1411                 public void LastIndexOfEmpty_2558 () {
1412                         var l = new List<int> ();
1413                         Assert.AreEqual (-1, l.IndexOf (-1));
1414                 }
1415
1416
1417 #region Enumerator mutability
1418
1419                 class Bar
1420                 {
1421                 }
1422
1423                 class Foo : IEnumerable<Bar>
1424                 {
1425                         Baz enumerator;
1426
1427                         public Foo ()
1428                         {
1429                                 enumerator = new Baz ();
1430                         }
1431
1432                         public IEnumerator<Bar> GetEnumerator ()
1433                         {
1434                                 return enumerator;
1435                         }
1436
1437                         IEnumerator IEnumerable.GetEnumerator ()
1438                         {
1439                                 return enumerator;
1440                         }
1441                 }
1442
1443                 class Baz : IEnumerator<Bar>
1444                 {
1445                         public bool DisposeWasCalled = false;
1446
1447                         public void Dispose ()
1448                         {
1449                                 DisposeWasCalled = true;
1450                         }
1451
1452                         public bool MoveNext ()
1453                         {
1454                                 return false; //assume empty collection
1455                         }
1456
1457                         public void Reset ()
1458                         {
1459                         }
1460
1461                         public Bar Current
1462                         {
1463                                 get { return null; }
1464                         }
1465
1466                         object IEnumerator.Current
1467                         {
1468                                 get { return Current; }
1469                         }
1470                 }
1471
1472                 [Test]
1473                 public void PremiseAboutDisposeBeingCalledWhenLooping ()
1474                 {
1475                         Foo enumerable = new Foo ();
1476                         Baz enumerator = enumerable.GetEnumerator () as Baz;
1477                         Assert.IsNotNull (enumerator);
1478                         Assert.AreEqual (false, enumerator.DisposeWasCalled);
1479                         foreach (var element in enumerable) ; //sic
1480                         Assert.AreEqual (true, enumerator.DisposeWasCalled);
1481                 }
1482
1483                 [Test]
1484                 public void TwoEnumeratorsOfTwoDifferentListsAreDifferent ()
1485                 {
1486                         var twoThree = new List<int> { 2, 3 };
1487                         var oneTwo = new List<int> { 2, 4 };
1488                         Assert.IsFalse (oneTwo.GetEnumerator ().Equals (twoThree.GetEnumerator ()));
1489                 }
1490
1491                 [Test]
1492                 public void TwoEnumeratorsOfTwoDifferentListsWithSameElementsAreDifferent ()
1493                 {
1494                         var twoThree = new List<int> { 2, 3 };
1495                         var anotherTwoThree = new List<int> { 2, 3 };
1496                         Assert.IsFalse(twoThree.GetEnumerator ().Equals (anotherTwoThree.GetEnumerator ()));
1497                 }
1498
1499                 [Test]
1500                 public void EnumeratorIsSameInSameListAfterSubsequentCalls ()
1501                 {
1502                         var enumerable = new List<Bar> ();
1503                         var enumerator = enumerable.GetEnumerator ();
1504                         var enumerator2 = enumerable.GetEnumerator ();
1505
1506                         Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1507
1508                         Assert.IsTrue (enumerator2.Equals (enumerator));
1509                 }
1510
1511
1512                 [Test] // was bug in Mono 2.10.9
1513                 public void EnumeratorIsStillSameInSubsequentCallsEvenHavingADisposalInBetween ()
1514                 {
1515                         var enumerable = new List<Bar> ();
1516                         var enumerator = enumerable.GetEnumerator ();
1517                         enumerator.Dispose ();
1518                         var enumerator2 = enumerable.GetEnumerator ();
1519
1520                         Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1521
1522                         Assert.IsTrue (enumerator2.Equals (enumerator));
1523                 }
1524
1525                 [Test]
1526                 public void EnumeratorIsObviouslyDifferentAfterListChanges ()
1527                 {
1528                         var enumerable = new List<Bar> ();
1529                         var enumerator = enumerable.GetEnumerator ();
1530                         enumerable.Add (new Bar ());
1531                         var enumerator2 = enumerable.GetEnumerator ();
1532
1533                         Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1534
1535                         Assert.IsFalse (enumerator2.Equals (enumerator));
1536                 }
1537
1538                 [Test] // was bug in Mono 2.10.9
1539                 public void DotNetDoesntThrowObjectDisposedExceptionAfterSubsequentDisposes()
1540                 {
1541                         var enumerable = new List<Bar> ();
1542                         var enumerator = enumerable.GetEnumerator ();
1543                         Assert.AreEqual (false, enumerator.MoveNext ());
1544                         enumerator.Dispose();
1545                         Assert.AreEqual (false, enumerator.MoveNext ());
1546                 }
1547 #endregion
1548
1549
1550         }
1551 }
1552 #endif
1553