remove TARGET_JVM
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / ListTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.DictionaryTest
3 //
4 // Authors:
5 //      David Waite (mass@akuma.org)
6 //
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 // Copyright (C) 2005 David Waite (mass@akuma.org)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
36 using System.IO;
37 using System.Runtime.Serialization.Formatters.Binary;
38 using System.Text;
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Collections.Generic {
43
44         class GenericComparer<T> : IComparer<T> {
45
46                 private bool called = false;
47
48                 public bool Called {
49                         get {
50                                 bool result = called;
51                                 called = false;
52                                 return called;
53                         }
54                 }
55
56                 public int Compare (T x, T y)
57                 {
58                         called = true;
59                         return 0;
60                 }
61         }
62
63         [TestFixture]
64         public class ListTest
65         {
66                 static byte [] _serializedList = new byte [] {
67                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
68                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
69                         0x7e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
70                         0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65,
71                         0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x60,
72                         0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
73                         0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72,
74                         0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
75                         0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20,
76                         0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75,
77                         0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69,
78                         0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62,
79                         0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34,
80                         0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00, 0x00, 0x00, 0x06,
81                         0x5f, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a,
82                         0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x07,
83                         0x00, 0x00, 0x08, 0x08, 0x08, 0x09, 0x02, 0x00, 0x00, 0x00, 0x03,
84                         0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x02, 0x00, 0x00,
85                         0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00,
86                         0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87                         0x0b };
88                 int [] _list1_contents;
89                 List <int> _list1;
90
91                 [SetUp]
92                 public void SetUp ()
93                 {
94                         // FIXME arrays currently do not support generic collection
95                         // interfaces
96                         _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
97                         // _list1 = new List <int> (_list1_contents);
98                         
99                         _list1 = new List <int> (8);
100                         foreach (int i in _list1_contents)
101                                 _list1.Add (i);
102                 }
103
104                 [Test]  // This was for bug #74980
105                 public void InsertTest ()
106                 {
107                         List <string> test = new List <string> ();
108                         test.Insert (0, "a");
109                         test.Insert (0, "b");
110                         test.Insert (1, "c");
111
112                         Assert.AreEqual (3, test.Count);
113                         Assert.AreEqual ("b", test [0]);
114                         Assert.AreEqual ("c", test [1]);
115                         Assert.AreEqual ("a", test [2]);
116                 }
117
118                 [Test]
119                 public void InsertRangeTest ()
120                 {
121                         int count = _list1.Count;
122                         // FIXME arrays currently do not support generic collection 
123                         // interfaces
124                         int [] items = {1, 2, 3};
125                         // List <int> newRange = new List <int> (items);
126                         List <int> newRange = new List <int> (3);
127                         foreach (int i in items)
128                                 newRange.Add (i);
129                         _list1.InsertRange (1, newRange);
130                         Assert.AreEqual (count + 3, _list1.Count);
131                         Assert.AreEqual (55, _list1 [0]);
132                         Assert.AreEqual (1, _list1 [1]);
133                         Assert.AreEqual (2, _list1 [2]);
134                         Assert.AreEqual (3, _list1 [3]);
135                         Assert.AreEqual (50, _list1 [4]);
136
137                         newRange = new List <int> ();
138                         List <int> li = new List <int> ();
139                         li.Add (1);
140                         newRange.InsertRange (0, li);
141                         newRange.InsertRange (newRange.Count, li);
142                         Assert.AreEqual (2, newRange.Count);
143                 }
144                 
145                 [Test]
146                 public void InsertSelfTest()
147                 {
148                         List <int> range = new List <int> (5);
149                         for (int i = 0; i < 5; ++ i)
150                                 range.Add (i);
151                         
152                         range.InsertRange(2, range);
153                         Assert.AreEqual (10, range.Count);
154                         Assert.AreEqual (0, range [0]);
155                         Assert.AreEqual (1, range [1]);
156                         Assert.AreEqual (0, range [2]);
157                         Assert.AreEqual (1, range [3]);
158                         Assert.AreEqual (2, range [4]);
159                         Assert.AreEqual (3, range [5]);
160                         Assert.AreEqual (4, range [6]);
161                         Assert.AreEqual (2, range [7]);
162                         Assert.AreEqual (3, range [8]);
163                         Assert.AreEqual (4, range [9]);
164                 }
165
166                 [Test, ExpectedException (typeof (ArgumentNullException))]
167                 public void InsertRangeNullTest ()
168                 {
169                         IEnumerable <int> n = null;
170                         _list1.InsertRange (0, n);
171                 }
172
173                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
174                 public void InsertRangeNegativeIndexTest ()
175                 {
176                         _list1.InsertRange (-1, _list1);
177                 }
178
179                 [Test]
180                 public void IndexOfTest ()
181                 {
182                         List <int> l = new List <int> ();
183
184                         l.Add (100);
185                         l.Add (200);
186
187                         Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
188                 }
189                 
190                 [Test, ExpectedException(typeof (ArgumentException))]
191                 public void IList_InsertInvalidType ()
192                 {
193                         IList list = _list1 as IList;
194                         list.Insert(0, new object());
195                 }
196                 
197                 [Test, ExpectedException(typeof (ArgumentException))]
198                 public void IList_AddInvalidType()
199                 {
200                         IList list = _list1 as IList;
201                         list.Add(new object());
202                 }
203                 
204                 [Test]
205                 public void IList_RemoveInvalidType()
206                 {
207                         IList list = _list1 as IList;
208                         int nCount = list.Count;
209                         list.Remove(new object());
210                         Assert.AreEqual(nCount, list.Count);
211                 }
212
213                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
214                 public void IndexOfOutOfRangeTest ()
215                 {
216                         List <int> l = new List <int> (4);
217                         l.IndexOf (0, 0, 4);
218                 }
219
220                 [Test]
221                 public void GetRangeTest ()
222                 {
223                         List <int> r = _list1.GetRange (2, 4);
224                         Assert.AreEqual (4, r.Count);
225                         Assert.AreEqual (22, r [0]);
226                         Assert.AreEqual (80, r [1]);
227                         Assert.AreEqual (56, r [2]);
228                         Assert.AreEqual (52, r [3]);
229                 }
230
231                 [Test]
232                 public void EnumeratorTest ()
233                 {
234                         List <int>.Enumerator e = _list1.GetEnumerator ();
235                         for (int i = 0; i < _list1_contents.Length; i++)
236                         {
237                                 Assert.IsTrue (e.MoveNext ());
238                                 Assert.AreEqual (_list1_contents [i], e.Current);
239                         }
240                         Assert.IsFalse (e.MoveNext ());
241                 }
242
243                 [Test]
244                 public void ConstructWithSizeTest ()
245                 {
246                         List <object> l_1 = new List <object> (1);
247                         List <object> l_2 = new List <object> (50);
248                         List <object> l_3 = new List <object> (0);
249
250                         Assert.AreEqual (1, l_1.Capacity);
251                         Assert.AreEqual (50, l_2.Capacity);
252                         Assert.AreEqual (0, l_3.Capacity);
253                 }
254
255                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
256                 public void ConstructWithInvalidSizeTest ()
257                 {
258                         List <int> l = new List <int> (-1);
259                 }
260
261                 [Test]
262                 public void ConstructWithCollectionTest ()
263                 {
264                         List <int> l1 = new List <int> (_list1);
265                         Assert.AreEqual (_list1.Count, l1.Count);
266                         Assert.AreEqual (l1.Count, l1.Capacity);
267                         for (int i = 0; i < l1.Count; i++)
268                                 Assert.AreEqual (_list1 [i], l1 [i]);
269                 }
270
271                 [Test, ExpectedException (typeof (ArgumentNullException))]
272                 public void ConstructWithInvalidCollectionTest ()
273                 {
274                         List <int> n = null;
275                         List <int> l1 = new List <int> (n);
276                 }
277
278                 [Test]
279                 public void AddTest ()
280                 {
281                         int count = _list1.Count;
282                         _list1.Add (-1);
283                         Assert.AreEqual (count + 1, _list1.Count);
284                         Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
285                 }
286
287                 [Test]
288                 public void AddRangeTest ()
289                 {
290                         int count = _list1.Count;
291                         // FIXME arrays currently do not support generic collection
292                         // interfaces
293                         int [] range = { -1, -2, -3 };
294                         List <int> tmp = new List <int> (3);
295                         foreach (int i in range)
296                                 tmp.Add (i);
297                         // _list1.AddRange (range);
298                         _list1.AddRange (tmp);
299                         
300                         Assert.AreEqual (count + 3, _list1.Count);
301                         Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
302                         Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
303                         Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
304                 }
305
306                 [Test, ExpectedException (typeof (ArgumentNullException))]
307                 public void AddNullRangeTest ()
308                 {
309                         int [] n = null;
310                         _list1.AddRange (n);
311                 }
312
313                 [Test]
314                 public void BinarySearchTest ()
315                 {
316                         List <int> l = new List <int> (_list1);
317                         l.Sort ();
318                         Assert.AreEqual (0, l.BinarySearch (22));
319                         Assert.AreEqual (-2, l.BinarySearch (23));
320                         Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
321                 }
322
323                 [Test]
324                 public void SerializeTest ()
325                 {
326                         List <int> list = new List <int> ();
327                         list.Add (5);
328                         list.Add (0);
329                         list.Add (7);
330
331                         BinaryFormatter bf = new BinaryFormatter ();
332                         MemoryStream ms = new MemoryStream ();
333                         bf.Serialize (ms, list);
334
335                         byte [] buffer = new byte [ms.Length];
336                         ms.Position = 0;
337                         ms.Read (buffer, 0, buffer.Length);
338
339                         Assert.AreEqual (_serializedList, buffer);
340                 }
341
342                 [Test]
343                 public void DeserializeTest ()
344                 {
345                         MemoryStream ms = new MemoryStream ();
346                         ms.Write (_serializedList, 0, _serializedList.Length);
347                         ms.Position = 0;
348
349                         BinaryFormatter bf = new BinaryFormatter ();
350                         List<int> list = (List<int>) bf.Deserialize (ms);
351                         Assert.AreEqual (3, list.Count, "#1");
352                         Assert.AreEqual (5, list [0], "#2");
353                         Assert.AreEqual (0, list [1], "#3");
354                         Assert.AreEqual (7, list [2], "#4");
355                 }
356
357                 [Test]
358                 public void SortTest ()
359                 {
360                         List <int> l = new List <int> (_list1);
361                         l.Sort ();
362                         Assert.AreEqual (_list1.Count, l.Count);
363                         Assert.AreEqual (22, l [0]);
364                         int minimum = 22;
365                         foreach (int i in l)
366                         {
367                                 Assert.IsTrue (minimum <= i);
368                                 minimum = i;
369                         }
370                 }
371
372                 [Test]
373                 public void ClearTest ()
374                 {
375                         int capacity = _list1.Capacity;
376                         _list1.Clear ();
377                         Assert.AreEqual (0, _list1.Count);
378                         Assert.AreEqual (capacity, _list1.Capacity);
379                 }
380
381                 [Test]
382                 public void ContainsTest ()
383                 {
384                         Assert.IsTrue (_list1.Contains (22));
385                         Assert.IsFalse (_list1.Contains (23));
386                 }
387
388                 private string StringConvert (int i)
389                 {
390                         return i.ToString ();
391                 }
392                 
393                 [Test]
394                 public void ConvertAllTest ()
395                 {
396                         List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
397                         Assert.AreEqual (_list1.Count, s.Count);
398                         Assert.AreEqual ("55", s [0]);
399                 }
400
401                 [Test]
402                 public void CopyToTest ()
403                 {
404                         int [] a = new int [2];
405                         _list1.CopyTo (1, a, 0, 2);
406                         Assert.AreEqual (50, a [0]);
407                         Assert.AreEqual (22, a [1]);
408
409                         int [] b = new int [_list1.Count + 1];
410                         b [_list1.Count] = 555;
411                         _list1.CopyTo (b);
412                         Assert.AreEqual (55, b [0]);
413                         Assert.AreEqual (555, b [_list1.Count]);
414
415                         b [0] = 888;
416                         _list1.CopyTo (b, 1);
417                         Assert.AreEqual (888, b [0]);
418                         Assert.AreEqual (55, b [1]);
419                 }
420
421                 [Test, ExpectedException (typeof (ArgumentNullException))]
422                 public void CopyToNullTest ()
423                 {
424                         int [] a = null;
425                         _list1.CopyTo (0, a, 0, 0);
426                 }
427
428                 static bool FindMultipleOfThree (int i)
429                 {
430                         return (i % 3) == 0;
431                 }
432
433                 static bool FindMultipleOfFour (int i)
434                 {
435                         return (i % 4) == 0;
436                 }
437
438                 static bool FindMultipleOfTwelve (int i)
439                 {
440                         return (i % 12) == 0;
441                 }
442
443                 [Test]
444                 public void FindTest ()
445                 {
446                         int i = _list1.Find (FindMultipleOfThree);
447                         Assert.AreEqual (63, i);
448
449                         i = _list1.Find (FindMultipleOfTwelve);
450                         Assert.AreEqual (default (int), i);
451                 }
452
453                 [Test, ExpectedException (typeof (ArgumentNullException))]
454                 public void FindNullTest ()
455                 {
456                         int i = _list1.Find (null);
457                 }
458
459                 [Test]
460                 public void FindAllTest ()
461                 {
462                         List <int> findings = _list1.FindAll (FindMultipleOfFour);
463                         Assert.AreEqual (4, findings.Count);
464                         Assert.AreEqual (80, findings [0]);
465                         Assert.AreEqual (56, findings [1]);
466                         Assert.AreEqual (52, findings [2]);
467                         Assert.AreEqual (40, findings [3]);
468
469                         findings = _list1.FindAll (FindMultipleOfTwelve);
470                         Assert.IsNotNull (findings);
471                         Assert.AreEqual (0, findings.Count);
472                 }
473
474                 [Test, ExpectedException (typeof (ArgumentNullException))]
475                 public void FindAllNullTest ()
476                 {
477                         List <int> findings = _list1.FindAll (null);
478                 }
479
480                 [Test]
481                 public void FindIndexTest ()
482                 {
483                         int i = _list1.FindIndex (FindMultipleOfThree);
484                         Assert.AreEqual (7, i);
485
486                         i = _list1.FindIndex (FindMultipleOfTwelve);
487                         Assert.AreEqual (-1, i);
488                 }
489
490                 [Test, ExpectedException (typeof (ArgumentNullException))]
491                 public void FindIndexNullTest ()
492                 {
493                         int i = _list1.FindIndex (null);
494                 }
495
496                 [Test]
497                 public void FindLastTest ()
498                 {
499                         int i = _list1.FindLast (FindMultipleOfFour);
500                         Assert.AreEqual (40, i);
501
502                         i = _list1.FindLast (FindMultipleOfTwelve);
503                         Assert.AreEqual (default (int), i);
504                 }
505
506                 [Test, ExpectedException (typeof (ArgumentNullException))]
507                 public void FindLastNullTest ()
508                 {
509                         int i = _list1.FindLast (null);
510                 }
511
512                 // FIXME currently generates Invalid IL Code error
513                 /*
514                 [Test]
515                 public void ForEachTest ()
516                 {
517                         int i = 0;
518                         _list1.ForEach (delegate (int j) { i += j; });
519
520                         Assert.AreEqual (418, i);
521                 }
522                 */
523                 [Test]
524                 public void FindLastIndexTest ()
525                 {
526                         int i = _list1.FindLastIndex (FindMultipleOfFour);
527                         Assert.AreEqual (6, i);
528
529                         i = _list1.FindLastIndex (5, FindMultipleOfFour);
530                         Assert.AreEqual (5, i);
531
532                         i = _list1.FindIndex (FindMultipleOfTwelve);
533                         Assert.AreEqual (-1, i);
534                 }
535
536                 [Test, ExpectedException (typeof (ArgumentNullException))]
537                 public void FindLastIndexNullTest ()
538                 {
539                         int i = _list1.FindLastIndex (null);
540                 }
541
542                 [Test]
543                 public void RemoveTest ()
544                 {
545                         int count = _list1.Count;
546                         bool result = _list1.Remove (22);
547                         Assert.IsTrue (result);
548                         Assert.AreEqual (count - 1, _list1.Count);
549
550                         Assert.AreEqual (-1, _list1.IndexOf (22));
551
552                         result = _list1.Remove (0);
553                         Assert.IsFalse (result);
554                 }
555
556                 [Test]
557                 public void RemoveAllTest ()
558                 {
559                         int count = _list1.Count;
560                         int removedCount = _list1.RemoveAll (FindMultipleOfFour);
561                         Assert.AreEqual (4, removedCount);
562                         Assert.AreEqual (count - 4, _list1.Count);
563
564                         removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
565                         Assert.AreEqual (0, removedCount);
566                         Assert.AreEqual (count - 4, _list1.Count);
567                 }
568
569                 [Test]
570                 public void RemoveAtTest ()
571                 {
572                         int count = _list1.Count;
573                         _list1.RemoveAt (0);
574                         Assert.AreEqual (count - 1, _list1.Count);
575                         Assert.AreEqual (50, _list1 [0]);
576                 }
577
578                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
579                 public void RemoveOutOfRangeTest ()
580                 {
581                         _list1.RemoveAt (_list1.Count);
582                 }
583
584                 [Test]
585                 public void RemoveRangeTest ()
586                 {
587                         int count = _list1.Count;
588                         _list1.RemoveRange (1, 2);
589                         Assert.AreEqual (count - 2, _list1.Count);
590                         Assert.AreEqual (55, _list1 [0]);
591                         Assert.AreEqual (80, _list1 [1]);
592
593                         _list1.RemoveRange (0, 0);
594                         Assert.AreEqual (count - 2, _list1.Count);
595                 }
596
597                 [Test]
598                 public void RemoveRangeFromEmptyListTest ()
599                 {
600                         List<int> l = new List<int> ();
601                         l.RemoveRange (0, 0);
602                 }
603
604                 [Test, ExpectedException (typeof (ArgumentException))]
605                 public void RemoveRangeOutOfRangeTest ()
606                 {
607                         _list1.RemoveRange (1, _list1.Count);
608                 }
609
610                 [Test]
611                 public void ReverseTest ()
612                 {
613                         int count = _list1.Count;
614                         _list1.Reverse ();
615                         Assert.AreEqual (count, _list1.Count);
616
617                         Assert.AreEqual (63, _list1 [0]);
618                         Assert.AreEqual (55, _list1 [count - 1]);
619
620                         _list1.Reverse (0, 2);
621
622                         Assert.AreEqual (40, _list1 [0]);
623                         Assert.AreEqual (63, _list1 [1]);
624                 }
625
626                 [Test, ExpectedException (typeof (ArgumentException))]
627                 public void ReverseOutOfRangeTest ()
628                 {
629                         _list1.Reverse (1, _list1.Count);
630                 }
631
632                 [Test]
633                 public void ToArrayTest ()
634                 {
635                         int [] copiedContents = _list1.ToArray ();
636                         Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
637
638                         Assert.AreEqual (_list1.Count, copiedContents.Length);
639                         Assert.AreEqual (_list1 [0], copiedContents [0]);
640                 }
641
642                 [Test]
643                 public void TrimExcessTest ()
644                 {
645                         List <string> l = new List <string> ();
646                         l.Add ("foo");
647
648                         Assert.IsTrue (l.Count < l.Capacity);
649                         l.TrimExcess ();
650                         Assert.AreEqual (l.Count, l.Capacity);
651                 }
652
653                 bool IsPositive (int i)
654                 {
655                         return i >= 0;
656                 }
657
658                 [Test]
659                 public void TrueForAllTest ()
660                 {
661                         Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
662                         Assert.IsTrue (_list1.TrueForAll (IsPositive));
663                 }
664
665                 [Test, ExpectedException (typeof (ArgumentNullException))]
666                 public void TrueForAllNullTest ()
667                 {
668                         _list1.TrueForAll (null);
669                 }
670
671                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
672                 public void CapacityOutOfRangeTest ()
673                 {
674                         _list1.Capacity = _list1.Count - 1;
675                 }
676
677                 [Test]
678                 public void BinarySearch_EmptyList ()
679                 {
680                         GenericComparer<int> comparer = new GenericComparer<int> ();
681                         List<int> l = new List<int> ();
682                         Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
683                         // bug 77030 - the comparer isn't called for an empty array/list
684                         Assert.IsFalse (comparer.Called, "Called");
685                 }
686
687                 [Test]
688                 public void BinarySearch2_EmptyList ()
689                 {
690                         GenericComparer<int> comparer = new GenericComparer<int> ();
691                         List<int> l = new List<int> ();
692                         Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
693                         // bug 77030 - the comparer isn't called for an empty array/list
694                         Assert.IsFalse (comparer.Called, "Called");
695                 }
696
697                 [Test]
698                 public void AddRange_Bug77019 ()
699                 {
700                         List<int> l = new List<int> ();
701                         Dictionary<string, int> d = new Dictionary<string, int> ();
702                         l.AddRange (d.Values);
703                         Assert.AreEqual (0, l.Count, "Count");
704                 }
705
706                 [Test]
707                 public void VersionCheck_Add ()
708                 {
709                         List<int> list = new List<int> ();
710                         IEnumerator enumerator = list.GetEnumerator ();
711                         list.Add (5);
712
713                         try {
714                                 enumerator.MoveNext ();
715                                 Assert.Fail ("#1");
716                         } catch (InvalidOperationException) {
717                         }
718
719                         try {
720                                 enumerator.Reset ();
721                                 Assert.Fail ("#2");
722                         } catch (InvalidOperationException) {
723                         }
724
725                         enumerator = list.GetEnumerator ();
726                         enumerator.MoveNext ();
727                 }
728
729                 [Test]
730                 public void VersionCheck_AddRange ()
731                 {
732                         List<int> list = new List<int> ();
733                         IEnumerator enumerator = list.GetEnumerator ();
734                         list.AddRange (new int [] { 5, 7 });
735
736                         try {
737                                 enumerator.MoveNext ();
738                                 Assert.Fail ("#1");
739                         } catch (InvalidOperationException) {
740                         }
741
742                         try {
743                                 enumerator.Reset ();
744                                 Assert.Fail ("#2");
745                         } catch (InvalidOperationException) {
746                         }
747
748                         enumerator = list.GetEnumerator ();
749                         enumerator.MoveNext ();
750                 }
751
752                 [Test]
753                 public void VersionCheck_Clear ()
754                 {
755                         List<int> list = new List<int> ();
756                         IEnumerator enumerator = list.GetEnumerator ();
757                         list.Clear ();
758
759                         try {
760                                 enumerator.MoveNext ();
761                                 Assert.Fail ("#1");
762                         } catch (InvalidOperationException) {
763                         }
764
765                         try {
766                                 enumerator.Reset ();
767                                 Assert.Fail ("#2");
768                         } catch (InvalidOperationException) {
769                         }
770
771                         enumerator = list.GetEnumerator ();
772                         enumerator.MoveNext ();
773                 }
774
775                 [Test]
776                 public void VersionCheck_Insert ()
777                 {
778                         List<int> list = new List<int> ();
779                         IEnumerator enumerator = list.GetEnumerator ();
780                         list.Insert (0, 7);
781
782                         try {
783                                 enumerator.MoveNext ();
784                                 Assert.Fail ("#1");
785                         } catch (InvalidOperationException) {
786                         }
787
788                         try {
789                                 enumerator.Reset ();
790                                 Assert.Fail ("#2");
791                         } catch (InvalidOperationException) {
792                         }
793
794                         enumerator = list.GetEnumerator ();
795                         enumerator.MoveNext ();
796                 }
797
798                 [Test]
799                 public void VersionCheck_InsertRange ()
800                 {
801                         List<int> list = new List<int> ();
802                         IEnumerator enumerator = list.GetEnumerator ();
803                         list.InsertRange (0, new int [] { 5, 7 });
804
805                         try {
806                                 enumerator.MoveNext ();
807                                 Assert.Fail ("#1");
808                         } catch (InvalidOperationException) {
809                         }
810
811                         try {
812                                 enumerator.Reset ();
813                                 Assert.Fail ("#2");
814                         } catch (InvalidOperationException) {
815                         }
816
817                         enumerator = list.GetEnumerator ();
818                         enumerator.MoveNext ();
819                 }
820
821                 [Test]
822                 public void VersionCheck_Remove ()
823                 {
824                         List<int> list = new List<int> ();
825                         list.Add (5);
826                         IEnumerator enumerator = list.GetEnumerator ();
827                         // version number is not incremented if item does not exist in list
828                         list.Remove (7);
829                         enumerator.MoveNext ();
830                         list.Remove (5);
831
832                         try {
833                                 enumerator.MoveNext ();
834                                 Assert.Fail ("#1");
835                         } catch (InvalidOperationException) {
836                         }
837
838                         try {
839                                 enumerator.Reset ();
840                                 Assert.Fail ("#2");
841                         } catch (InvalidOperationException) {
842                         }
843
844                         enumerator = list.GetEnumerator ();
845                         enumerator.MoveNext ();
846                 }
847
848                 [Test]
849                 public void VersionCheck_RemoveAll ()
850                 {
851                         List<int> list = new List<int> ();
852                         list.Add (5);
853                         IEnumerator enumerator = list.GetEnumerator ();
854                         // version is not incremented if there are no items to remove
855                         list.RemoveAll (FindMultipleOfFour);
856                         enumerator.MoveNext ();
857                         list.Add (4);
858
859                         enumerator = list.GetEnumerator ();
860                         list.RemoveAll (FindMultipleOfFour);
861
862                         try {
863                                 enumerator.MoveNext ();
864                                 Assert.Fail ("#1");
865                         } catch (InvalidOperationException) {
866                         }
867
868                         try {
869                                 enumerator.Reset ();
870                                 Assert.Fail ("#2");
871                         } catch (InvalidOperationException) {
872                         }
873
874                         enumerator = list.GetEnumerator ();
875                         enumerator.MoveNext ();
876                 }
877
878                 [Test]
879                 public void VersionCheck_RemoveAt ()
880                 {
881                         List<int> list = new List<int> ();
882                         list.Add (5);
883                         IEnumerator enumerator = list.GetEnumerator ();
884                         list.RemoveAt (0);
885
886                         try {
887                                 enumerator.MoveNext ();
888                                 Assert.Fail ("#1");
889                         } catch (InvalidOperationException) {
890                         }
891
892                         try {
893                                 enumerator.Reset ();
894                                 Assert.Fail ("#2");
895                         } catch (InvalidOperationException) {
896                         }
897
898                         enumerator = list.GetEnumerator ();
899                         enumerator.MoveNext ();
900                 }
901
902                 [Test]
903                 public void VersionCheck_RemoveRange ()
904                 {
905                         List<int> list = new List<int> ();
906                         list.Add (5);
907                         IEnumerator enumerator = list.GetEnumerator ();
908                         // version is not incremented if count is zero
909                         list.RemoveRange (0, 0);
910                         enumerator.MoveNext ();
911                         enumerator.Reset ();
912                         list.RemoveRange (0, 1);
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_Reverse ()
932                 {
933                         List<int> list = new List<int> ();
934                         IEnumerator enumerator = list.GetEnumerator ();
935                         list.Reverse ();
936
937                         try {
938                                 enumerator.MoveNext ();
939                                 Assert.Fail ("#A1");
940                         } catch (InvalidOperationException) {
941                         }
942
943                         try {
944                                 enumerator.Reset ();
945                                 Assert.Fail ("#A2");
946                         } catch (InvalidOperationException) {
947                         }
948
949                         enumerator = list.GetEnumerator ();
950                         list.Reverse (0, 0);
951
952                         try {
953                                 enumerator.MoveNext ();
954                                 Assert.Fail ("#B1");
955                         } catch (InvalidOperationException) {
956                         }
957
958                         try {
959                                 enumerator.Reset ();
960                                 Assert.Fail ("#B2");
961                         } catch (InvalidOperationException) {
962                         }
963
964                         enumerator = list.GetEnumerator ();
965                         enumerator.MoveNext ();
966                 }
967
968                 class SortTestComparer: IComparer<string> {
969
970                         public int Compare (string s1, string s2)
971                         {
972                                 return String.Compare (s1, s2);
973                         }
974                 }
975
976                 [Test]
977                 public void Sort_Bug76361 ()
978                 {
979                         SortTestComparer comparer = new SortTestComparer ();
980                         List<string> l = new List<string> ();
981                         l.Add ("foo");
982                         l.Add ("bar");
983                         l.Sort (comparer);
984                         Assert.AreEqual ("bar", l[0], "0");
985                         Assert.AreEqual ("foo", l[1], "1");
986                         Assert.AreEqual (2, l.Count, "Count");
987                 }
988
989                 // for bug #77039 test case
990                 class GenericIComparable: IComparable<GenericIComparable> {
991                         private int _NumberToSortOn;
992
993                         public int NumberToSortOn {
994                                 get { return _NumberToSortOn; }
995                                 set { _NumberToSortOn = value; }
996                         }
997
998                         public GenericIComparable (int val)
999                         {
1000                                 _NumberToSortOn = val;
1001                         }
1002
1003                         public int CompareTo (GenericIComparable other)
1004                         {
1005                                 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1006                         }
1007                 }
1008
1009                 [Test]
1010                 public void Sort_GenericIComparable_Bug77039 ()
1011                 {
1012                         List<GenericIComparable> l = new List<GenericIComparable> ();
1013                         l.Add (new GenericIComparable (2));
1014                         l.Add (new GenericIComparable (1));
1015                         l.Add (new GenericIComparable (3));
1016                         l.Sort ();
1017                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1018                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1019                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1020                 }
1021
1022                 class NonGenericIComparable: IComparable {
1023                         private int _NumberToSortOn;
1024
1025                         public int NumberToSortOn {
1026                                 get { return _NumberToSortOn; }
1027                                 set { _NumberToSortOn = value; }
1028                         }
1029
1030                         public NonGenericIComparable (int val)
1031                         {
1032                                 _NumberToSortOn = val;
1033                         }
1034
1035                         public int CompareTo (object obj)
1036                         {
1037                                 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1038                         }
1039                 }
1040
1041                 [Test]
1042                 public void Sort_NonGenericIComparable ()
1043                 {
1044                         List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1045                         l.Add (new NonGenericIComparable (2));
1046                         l.Add (new NonGenericIComparable (1));
1047                         l.Add (new NonGenericIComparable (3));
1048                         l.Sort ();
1049                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1050                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1051                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1052                 }
1053
1054                 class NonComparable {
1055                 }
1056
1057                 [Test]
1058                 public void Sort_GenericNonIComparable ()
1059                 {
1060                         List<NonComparable> l = new List<NonComparable> ();
1061                         l.Sort ();
1062                         // no element -> no sort -> no exception
1063                         l.Add (new NonComparable ());
1064                         l.Sort ();
1065                         // one element -> no sort -> no exception
1066                 }
1067
1068                 [Test]
1069                 [ExpectedException (typeof (InvalidOperationException))]
1070                 public void Sort_GenericNonIComparable_2 ()
1071                 {
1072                         List<NonComparable> l = new List<NonComparable> ();
1073                         l.Add (new NonComparable ());
1074                         l.Add (new NonComparable ());
1075                         l.Sort ();
1076                         // two element -> sort -> exception!
1077                 }
1078                 
1079                 [Test]
1080                 void IList_Contains_InvalidType()
1081                 {
1082                         List<string> list = new List<string>();
1083                         list.Add("foo");
1084                         Assert.IsFalse (((IList)list).Contains(new object()));
1085                 }
1086                 
1087                 [Test]
1088                 void IList_IndexOf_InvalidType()
1089                 {
1090                         List<string> list = new List<string>();
1091                         list.Add("foo");
1092                         Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1093                 }
1094
1095                 // for bug #77277 test case
1096                 [Test]
1097                 public void Test_ContainsAndIndexOf_EquatableItem ()
1098                 {
1099                         List<EquatableClass> list = new List<EquatableClass> ();
1100                         EquatableClass item0 = new EquatableClass (0);
1101                         EquatableClass item1 = new EquatableClass (1);
1102
1103                         list.Add (item0);
1104                         list.Add (item1);
1105                         list.Add (item0);
1106
1107                         Assert.AreEqual (true, list.Contains (item0), "#0");
1108                         Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1109                         Assert.AreEqual (0, list.IndexOf (item0), "#2");
1110                         Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1111                         Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1112                         Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1113                 }
1114                 
1115                 [Test]
1116                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1117                 public void SetItem_OutOfRange()
1118                 {
1119                         List<string> list = new List<string>();
1120                         list[0] = "foo";
1121                 }
1122                 
1123                 [Test]
1124                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1125                 public void SetItem_IList_OutOfRange()
1126                 {
1127                         IList<string> list = new List<string>();
1128                         list[0] = "foo";
1129                 }
1130
1131                 public class EquatableClass : IEquatable<EquatableClass>
1132                 {
1133                         int _x;
1134                         public EquatableClass (int x)
1135                         {
1136                                 _x = x;
1137                         }
1138
1139                         public bool Equals (EquatableClass other)
1140                         {
1141                                 return this._x == other._x;
1142                         }
1143                 }
1144         }
1145 }
1146 #endif
1147