9ef5f84098c2236fe52b2a4f927795b9a27e724a
[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                 [Category ("TargetJvmNotWorking")]
325                 public void SerializeTest ()
326                 {
327                         List <int> list = new List <int> ();
328                         list.Add (5);
329                         list.Add (0);
330                         list.Add (7);
331
332 #if TARGET_JVM
333                         BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
334 #else
335                         BinaryFormatter bf = new BinaryFormatter ();
336 #endif // TARGET_JVM
337                         MemoryStream ms = new MemoryStream ();
338                         bf.Serialize (ms, list);
339
340                         byte [] buffer = new byte [ms.Length];
341                         ms.Position = 0;
342                         ms.Read (buffer, 0, buffer.Length);
343
344                         Assert.AreEqual (_serializedList, buffer);
345                 }
346
347                 [Test]
348                 [Category ("TargetJvmNotWorking")]
349                 public void DeserializeTest ()
350                 {
351                         MemoryStream ms = new MemoryStream ();
352                         ms.Write (_serializedList, 0, _serializedList.Length);
353                         ms.Position = 0;
354
355 #if TARGET_JVM
356                         BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
357 #else
358                         BinaryFormatter bf = new BinaryFormatter ();
359 #endif // TARGET_JVM
360                         List<int> list = (List<int>) bf.Deserialize (ms);
361                         Assert.AreEqual (3, list.Count, "#1");
362                         Assert.AreEqual (5, list [0], "#2");
363                         Assert.AreEqual (0, list [1], "#3");
364                         Assert.AreEqual (7, list [2], "#4");
365                 }
366
367                 [Test]
368                 public void SortTest ()
369                 {
370                         List <int> l = new List <int> (_list1);
371                         l.Sort ();
372                         Assert.AreEqual (_list1.Count, l.Count);
373                         Assert.AreEqual (22, l [0]);
374                         int minimum = 22;
375                         foreach (int i in l)
376                         {
377                                 Assert.IsTrue (minimum <= i);
378                                 minimum = i;
379                         }
380                 }
381
382                 [Test]
383                 public void ClearTest ()
384                 {
385                         int capacity = _list1.Capacity;
386                         _list1.Clear ();
387                         Assert.AreEqual (0, _list1.Count);
388                         Assert.AreEqual (capacity, _list1.Capacity);
389                 }
390
391                 [Test]
392                 public void ContainsTest ()
393                 {
394                         Assert.IsTrue (_list1.Contains (22));
395                         Assert.IsFalse (_list1.Contains (23));
396                 }
397
398                 private string StringConvert (int i)
399                 {
400                         return i.ToString ();
401                 }
402                 
403                 [Test]
404                 public void ConvertAllTest ()
405                 {
406                         List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
407                         Assert.AreEqual (_list1.Count, s.Count);
408                         Assert.AreEqual ("55", s [0]);
409                 }
410
411                 [Test]
412                 public void CopyToTest ()
413                 {
414                         int [] a = new int [2];
415                         _list1.CopyTo (1, a, 0, 2);
416                         Assert.AreEqual (50, a [0]);
417                         Assert.AreEqual (22, a [1]);
418
419                         int [] b = new int [_list1.Count + 1];
420                         b [_list1.Count] = 555;
421                         _list1.CopyTo (b);
422                         Assert.AreEqual (55, b [0]);
423                         Assert.AreEqual (555, b [_list1.Count]);
424
425                         b [0] = 888;
426                         _list1.CopyTo (b, 1);
427                         Assert.AreEqual (888, b [0]);
428                         Assert.AreEqual (55, b [1]);
429                 }
430
431                 [Test, ExpectedException (typeof (ArgumentNullException))]
432                 public void CopyToNullTest ()
433                 {
434                         int [] a = null;
435                         _list1.CopyTo (0, a, 0, 0);
436                 }
437
438                 static bool FindMultipleOfThree (int i)
439                 {
440                         return (i % 3) == 0;
441                 }
442
443                 static bool FindMultipleOfFour (int i)
444                 {
445                         return (i % 4) == 0;
446                 }
447
448                 static bool FindMultipleOfTwelve (int i)
449                 {
450                         return (i % 12) == 0;
451                 }
452
453                 [Test]
454                 public void FindTest ()
455                 {
456                         int i = _list1.Find (FindMultipleOfThree);
457                         Assert.AreEqual (63, i);
458
459                         i = _list1.Find (FindMultipleOfTwelve);
460                         Assert.AreEqual (default (int), i);
461                 }
462
463                 [Test, ExpectedException (typeof (ArgumentNullException))]
464                 public void FindNullTest ()
465                 {
466                         int i = _list1.Find (null);
467                 }
468
469                 [Test]
470                 public void FindAllSmallTest ()
471                 {
472                         List <int> findings = _list1.FindAll (FindMultipleOfFour);
473                         Assert.AreEqual (4, findings.Count);
474                         Assert.AreEqual (80, findings [0]);
475                         Assert.AreEqual (56, findings [1]);
476                         Assert.AreEqual (52, findings [2]);
477                         Assert.AreEqual (40, findings [3]);
478
479                         findings = _list1.FindAll (FindMultipleOfTwelve);
480                         Assert.IsNotNull (findings);
481                         Assert.AreEqual (0, findings.Count);
482                 }
483                 
484                 [Test]
485                 public void FindAllMediumTest ()
486                 {
487                         List <int> integers = new List <int> (10000);
488                         for (int i = 1; i <= 10000; i++)
489                                 integers.Add (i);
490                         
491                         List <int> results = integers.FindAll (FindMultipleOfFour);
492                         
493                         Assert.IsNotNull (results);
494                         Assert.AreEqual (2500, results.Count);
495                         
496                         results = integers.FindAll (FindMultipleOfTwelve);
497                         
498                         Assert.IsNotNull (results);
499                         Assert.AreEqual (833, results.Count);
500                 }
501                 
502                 [Test]
503                 public void FindAllLargeTest ()
504                 {
505                         List <int> integers = new List <int> (70000);
506                         for (int i = 1; i <= 80000; i++)
507                                 integers.Add (i);
508                         
509                         List <int> results = integers.FindAll (FindMultipleOfFour);
510                         
511                         Assert.IsNotNull (results);
512                         Assert.AreEqual (20000, results.Count);
513                         
514                         results = integers.FindAll (FindMultipleOfTwelve);
515                         
516                         Assert.IsNotNull (results);
517                         Assert.AreEqual (6666, results.Count);
518                 }
519
520                 [Test, ExpectedException (typeof (ArgumentNullException))]
521                 public void FindAllNullTest ()
522                 {
523                         List <int> findings = _list1.FindAll (null);
524                 }
525
526                 [Test]
527                 public void FindIndexTest ()
528                 {
529                         int i = _list1.FindIndex (FindMultipleOfThree);
530                         Assert.AreEqual (7, i);
531
532                         i = _list1.FindIndex (FindMultipleOfTwelve);
533                         Assert.AreEqual (-1, i);
534                 }
535
536                 [Test, ExpectedException (typeof (ArgumentNullException))]
537                 public void FindIndexNullTest ()
538                 {
539                         int i = _list1.FindIndex (null);
540                 }
541
542                 [Test]
543                 public void FindLastTest ()
544                 {
545                         int i = _list1.FindLast (FindMultipleOfFour);
546                         Assert.AreEqual (40, i);
547
548                         i = _list1.FindLast (FindMultipleOfTwelve);
549                         Assert.AreEqual (default (int), i);
550                 }
551
552                 [Test, ExpectedException (typeof (ArgumentNullException))]
553                 public void FindLastNullTest ()
554                 {
555                         int i = _list1.FindLast (null);
556                 }
557
558                 // FIXME currently generates Invalid IL Code error
559                 /*
560                 [Test]
561                 public void ForEachTest ()
562                 {
563                         int i = 0;
564                         _list1.ForEach (delegate (int j) { i += j; });
565
566                         Assert.AreEqual (418, i);
567                 }
568                 */
569                 [Test]
570                 public void FindLastIndexTest ()
571                 {
572                         int i = _list1.FindLastIndex (FindMultipleOfFour);
573                         Assert.AreEqual (6, i);
574
575                         i = _list1.FindLastIndex (5, FindMultipleOfFour);
576                         Assert.AreEqual (5, i);
577
578                         i = _list1.FindIndex (FindMultipleOfTwelve);
579                         Assert.AreEqual (-1, i);
580                 }
581
582                 [Test, ExpectedException (typeof (ArgumentNullException))]
583                 public void FindLastIndexNullTest ()
584                 {
585                         int i = _list1.FindLastIndex (null);
586                 }
587
588                 [Test]
589                 public void RemoveTest ()
590                 {
591                         int count = _list1.Count;
592                         bool result = _list1.Remove (22);
593                         Assert.IsTrue (result);
594                         Assert.AreEqual (count - 1, _list1.Count);
595
596                         Assert.AreEqual (-1, _list1.IndexOf (22));
597
598                         result = _list1.Remove (0);
599                         Assert.IsFalse (result);
600                 }
601
602                 [Test]
603                 public void RemoveAllTest ()
604                 {
605                         int count = _list1.Count;
606                         int removedCount = _list1.RemoveAll (FindMultipleOfFour);
607                         Assert.AreEqual (4, removedCount);
608                         Assert.AreEqual (count - 4, _list1.Count);
609
610                         removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
611                         Assert.AreEqual (0, removedCount);
612                         Assert.AreEqual (count - 4, _list1.Count);
613                 }
614
615                 [Test]
616                 public void RemoveAtTest ()
617                 {
618                         int count = _list1.Count;
619                         _list1.RemoveAt (0);
620                         Assert.AreEqual (count - 1, _list1.Count);
621                         Assert.AreEqual (50, _list1 [0]);
622                 }
623
624                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
625                 public void RemoveOutOfRangeTest ()
626                 {
627                         _list1.RemoveAt (_list1.Count);
628                 }
629
630                 [Test]
631                 public void RemoveRangeTest ()
632                 {
633                         int count = _list1.Count;
634                         _list1.RemoveRange (1, 2);
635                         Assert.AreEqual (count - 2, _list1.Count);
636                         Assert.AreEqual (55, _list1 [0]);
637                         Assert.AreEqual (80, _list1 [1]);
638
639                         _list1.RemoveRange (0, 0);
640                         Assert.AreEqual (count - 2, _list1.Count);
641                 }
642
643                 [Test]
644                 public void RemoveRangeFromEmptyListTest ()
645                 {
646                         List<int> l = new List<int> ();
647                         l.RemoveRange (0, 0);
648                 }
649
650                 [Test, ExpectedException (typeof (ArgumentException))]
651                 public void RemoveRangeOutOfRangeTest ()
652                 {
653                         _list1.RemoveRange (1, _list1.Count);
654                 }
655
656                 [Test]
657                 public void ReverseTest ()
658                 {
659                         int count = _list1.Count;
660                         _list1.Reverse ();
661                         Assert.AreEqual (count, _list1.Count);
662
663                         Assert.AreEqual (63, _list1 [0]);
664                         Assert.AreEqual (55, _list1 [count - 1]);
665
666                         _list1.Reverse (0, 2);
667
668                         Assert.AreEqual (40, _list1 [0]);
669                         Assert.AreEqual (63, _list1 [1]);
670                 }
671
672                 [Test, ExpectedException (typeof (ArgumentException))]
673                 public void ReverseOutOfRangeTest ()
674                 {
675                         _list1.Reverse (1, _list1.Count);
676                 }
677
678                 [Test]
679                 public void ToArrayTest ()
680                 {
681                         int [] copiedContents = _list1.ToArray ();
682                         Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
683
684                         Assert.AreEqual (_list1.Count, copiedContents.Length);
685                         Assert.AreEqual (_list1 [0], copiedContents [0]);
686                 }
687
688                 [Test]
689                 public void TrimExcessTest ()
690                 {
691                         List <string> l = new List <string> ();
692                         l.Add ("foo");
693
694                         Assert.IsTrue (l.Count < l.Capacity);
695                         l.TrimExcess ();
696                         Assert.AreEqual (l.Count, l.Capacity);
697                 }
698
699                 bool IsPositive (int i)
700                 {
701                         return i >= 0;
702                 }
703
704                 [Test]
705                 public void TrueForAllTest ()
706                 {
707                         Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
708                         Assert.IsTrue (_list1.TrueForAll (IsPositive));
709                 }
710
711                 [Test, ExpectedException (typeof (ArgumentNullException))]
712                 public void TrueForAllNullTest ()
713                 {
714                         _list1.TrueForAll (null);
715                 }
716
717                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
718                 public void CapacityOutOfRangeTest ()
719                 {
720                         _list1.Capacity = _list1.Count - 1;
721                 }
722
723                 [Test]
724                 public void BinarySearch_EmptyList ()
725                 {
726                         GenericComparer<int> comparer = new GenericComparer<int> ();
727                         List<int> l = new List<int> ();
728                         Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
729                         // bug 77030 - the comparer isn't called for an empty array/list
730                         Assert.IsFalse (comparer.Called, "Called");
731                 }
732
733                 [Test]
734                 public void BinarySearch2_EmptyList ()
735                 {
736                         GenericComparer<int> comparer = new GenericComparer<int> ();
737                         List<int> l = new List<int> ();
738                         Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
739                         // bug 77030 - the comparer isn't called for an empty array/list
740                         Assert.IsFalse (comparer.Called, "Called");
741                 }
742
743                 [Test]
744                 public void AddRange_Bug77019 ()
745                 {
746                         List<int> l = new List<int> ();
747                         Dictionary<string, int> d = new Dictionary<string, int> ();
748                         l.AddRange (d.Values);
749                         Assert.AreEqual (0, l.Count, "Count");
750                 }
751
752                 [Test]
753                 public void VersionCheck_Add ()
754                 {
755                         List<int> list = new List<int> ();
756                         IEnumerator enumerator = list.GetEnumerator ();
757                         list.Add (5);
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_AddRange ()
777                 {
778                         List<int> list = new List<int> ();
779                         IEnumerator enumerator = list.GetEnumerator ();
780                         list.AddRange (new int [] { 5, 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_Clear ()
800                 {
801                         List<int> list = new List<int> ();
802                         IEnumerator enumerator = list.GetEnumerator ();
803                         list.Clear ();
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_Insert ()
823                 {
824                         List<int> list = new List<int> ();
825                         IEnumerator enumerator = list.GetEnumerator ();
826                         list.Insert (0, 7);
827
828                         try {
829                                 enumerator.MoveNext ();
830                                 Assert.Fail ("#1");
831                         } catch (InvalidOperationException) {
832                         }
833
834                         try {
835                                 enumerator.Reset ();
836                                 Assert.Fail ("#2");
837                         } catch (InvalidOperationException) {
838                         }
839
840                         enumerator = list.GetEnumerator ();
841                         enumerator.MoveNext ();
842                 }
843
844                 [Test]
845                 public void VersionCheck_InsertRange ()
846                 {
847                         List<int> list = new List<int> ();
848                         IEnumerator enumerator = list.GetEnumerator ();
849                         list.InsertRange (0, new int [] { 5, 7 });
850
851                         try {
852                                 enumerator.MoveNext ();
853                                 Assert.Fail ("#1");
854                         } catch (InvalidOperationException) {
855                         }
856
857                         try {
858                                 enumerator.Reset ();
859                                 Assert.Fail ("#2");
860                         } catch (InvalidOperationException) {
861                         }
862
863                         enumerator = list.GetEnumerator ();
864                         enumerator.MoveNext ();
865                 }
866
867                 [Test]
868                 public void VersionCheck_Remove ()
869                 {
870                         List<int> list = new List<int> ();
871                         list.Add (5);
872                         IEnumerator enumerator = list.GetEnumerator ();
873                         // version number is not incremented if item does not exist in list
874                         list.Remove (7);
875                         enumerator.MoveNext ();
876                         list.Remove (5);
877
878                         try {
879                                 enumerator.MoveNext ();
880                                 Assert.Fail ("#1");
881                         } catch (InvalidOperationException) {
882                         }
883
884                         try {
885                                 enumerator.Reset ();
886                                 Assert.Fail ("#2");
887                         } catch (InvalidOperationException) {
888                         }
889
890                         enumerator = list.GetEnumerator ();
891                         enumerator.MoveNext ();
892                 }
893
894                 [Test]
895                 public void VersionCheck_RemoveAll ()
896                 {
897                         List<int> list = new List<int> ();
898                         list.Add (5);
899                         IEnumerator enumerator = list.GetEnumerator ();
900                         // version is not incremented if there are no items to remove
901                         list.RemoveAll (FindMultipleOfFour);
902                         enumerator.MoveNext ();
903                         list.Add (4);
904
905                         enumerator = list.GetEnumerator ();
906                         list.RemoveAll (FindMultipleOfFour);
907
908                         try {
909                                 enumerator.MoveNext ();
910                                 Assert.Fail ("#1");
911                         } catch (InvalidOperationException) {
912                         }
913
914                         try {
915                                 enumerator.Reset ();
916                                 Assert.Fail ("#2");
917                         } catch (InvalidOperationException) {
918                         }
919
920                         enumerator = list.GetEnumerator ();
921                         enumerator.MoveNext ();
922                 }
923
924                 [Test]
925                 public void VersionCheck_RemoveAt ()
926                 {
927                         List<int> list = new List<int> ();
928                         list.Add (5);
929                         IEnumerator enumerator = list.GetEnumerator ();
930                         list.RemoveAt (0);
931
932                         try {
933                                 enumerator.MoveNext ();
934                                 Assert.Fail ("#1");
935                         } catch (InvalidOperationException) {
936                         }
937
938                         try {
939                                 enumerator.Reset ();
940                                 Assert.Fail ("#2");
941                         } catch (InvalidOperationException) {
942                         }
943
944                         enumerator = list.GetEnumerator ();
945                         enumerator.MoveNext ();
946                 }
947
948                 [Test]
949                 public void VersionCheck_RemoveRange ()
950                 {
951                         List<int> list = new List<int> ();
952                         list.Add (5);
953                         IEnumerator enumerator = list.GetEnumerator ();
954                         // version is not incremented if count is zero
955                         list.RemoveRange (0, 0);
956                         enumerator.MoveNext ();
957                         enumerator.Reset ();
958                         list.RemoveRange (0, 1);
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_Reverse ()
978                 {
979                         List<int> list = new List<int> ();
980                         IEnumerator enumerator = list.GetEnumerator ();
981                         list.Reverse ();
982
983                         try {
984                                 enumerator.MoveNext ();
985                                 Assert.Fail ("#A1");
986                         } catch (InvalidOperationException) {
987                         }
988
989                         try {
990                                 enumerator.Reset ();
991                                 Assert.Fail ("#A2");
992                         } catch (InvalidOperationException) {
993                         }
994
995                         enumerator = list.GetEnumerator ();
996                         list.Reverse (0, 0);
997
998                         try {
999                                 enumerator.MoveNext ();
1000                                 Assert.Fail ("#B1");
1001                         } catch (InvalidOperationException) {
1002                         }
1003
1004                         try {
1005                                 enumerator.Reset ();
1006                                 Assert.Fail ("#B2");
1007                         } catch (InvalidOperationException) {
1008                         }
1009
1010                         enumerator = list.GetEnumerator ();
1011                         enumerator.MoveNext ();
1012                 }
1013
1014                 class SortTestComparer: IComparer<string> {
1015
1016                         public int Compare (string s1, string s2)
1017                         {
1018                                 return String.Compare (s1, s2);
1019                         }
1020                 }
1021
1022                 [Test]
1023                 public void Sort_Bug76361 ()
1024                 {
1025                         SortTestComparer comparer = new SortTestComparer ();
1026                         List<string> l = new List<string> ();
1027                         l.Add ("foo");
1028                         l.Add ("bar");
1029                         l.Sort (comparer);
1030                         Assert.AreEqual ("bar", l[0], "0");
1031                         Assert.AreEqual ("foo", l[1], "1");
1032                         Assert.AreEqual (2, l.Count, "Count");
1033                 }
1034
1035                 // for bug #77039 test case
1036                 class GenericIComparable: IComparable<GenericIComparable> {
1037                         private int _NumberToSortOn;
1038
1039                         public int NumberToSortOn {
1040                                 get { return _NumberToSortOn; }
1041                                 set { _NumberToSortOn = value; }
1042                         }
1043
1044                         public GenericIComparable (int val)
1045                         {
1046                                 _NumberToSortOn = val;
1047                         }
1048
1049                         public int CompareTo (GenericIComparable other)
1050                         {
1051                                 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1052                         }
1053                 }
1054
1055                 [Test]
1056                 public void Sort_GenericIComparable_Bug77039 ()
1057                 {
1058                         List<GenericIComparable> l = new List<GenericIComparable> ();
1059                         l.Add (new GenericIComparable (2));
1060                         l.Add (new GenericIComparable (1));
1061                         l.Add (new GenericIComparable (3));
1062                         l.Sort ();
1063                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1064                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1065                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1066                 }
1067
1068                 class NonGenericIComparable: IComparable {
1069                         private int _NumberToSortOn;
1070
1071                         public int NumberToSortOn {
1072                                 get { return _NumberToSortOn; }
1073                                 set { _NumberToSortOn = value; }
1074                         }
1075
1076                         public NonGenericIComparable (int val)
1077                         {
1078                                 _NumberToSortOn = val;
1079                         }
1080
1081                         public int CompareTo (object obj)
1082                         {
1083                                 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1084                         }
1085                 }
1086
1087                 [Test]
1088                 public void Sort_NonGenericIComparable ()
1089                 {
1090                         List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1091                         l.Add (new NonGenericIComparable (2));
1092                         l.Add (new NonGenericIComparable (1));
1093                         l.Add (new NonGenericIComparable (3));
1094                         l.Sort ();
1095                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1096                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1097                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1098                 }
1099
1100                 class NonComparable {
1101                 }
1102
1103                 [Test]
1104                 public void Sort_GenericNonIComparable ()
1105                 {
1106                         List<NonComparable> l = new List<NonComparable> ();
1107                         l.Sort ();
1108                         // no element -> no sort -> no exception
1109                         l.Add (new NonComparable ());
1110                         l.Sort ();
1111                         // one element -> no sort -> no exception
1112                 }
1113
1114                 [Test]
1115                 [ExpectedException (typeof (InvalidOperationException))]
1116                 public void Sort_GenericNonIComparable_2 ()
1117                 {
1118                         List<NonComparable> l = new List<NonComparable> ();
1119                         l.Add (new NonComparable ());
1120                         l.Add (new NonComparable ());
1121                         l.Sort ();
1122                         // two element -> sort -> exception!
1123                 }
1124                 
1125                 [Test]
1126                 void IList_Contains_InvalidType()
1127                 {
1128                         List<string> list = new List<string>();
1129                         list.Add("foo");
1130                         Assert.IsFalse (((IList)list).Contains(new object()));
1131                 }
1132                 
1133                 [Test]
1134                 void IList_IndexOf_InvalidType()
1135                 {
1136                         List<string> list = new List<string>();
1137                         list.Add("foo");
1138                         Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1139                 }
1140
1141                 // for bug #77277 test case
1142                 [Test]
1143                 public void Test_ContainsAndIndexOf_EquatableItem ()
1144                 {
1145                         List<EquatableClass> list = new List<EquatableClass> ();
1146                         EquatableClass item0 = new EquatableClass (0);
1147                         EquatableClass item1 = new EquatableClass (1);
1148
1149                         list.Add (item0);
1150                         list.Add (item1);
1151                         list.Add (item0);
1152
1153                         Assert.AreEqual (true, list.Contains (item0), "#0");
1154                         Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1155                         Assert.AreEqual (0, list.IndexOf (item0), "#2");
1156                         Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1157                         Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1158                         Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1159                 }
1160
1161                 // for bug #81387 test case
1162                 [Test]
1163                 public void Test_Contains_After_Remove ()
1164                 {
1165                         List<int> list = new List<int> ();
1166             list.Add (2);
1167
1168             list.Remove (2);
1169
1170                         Assert.AreEqual (true, list.Contains (2), "#0");
1171                 }
1172                 
1173                 [Test]
1174                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1175                 public void SetItem_OutOfRange()
1176                 {
1177                         List<string> list = new List<string>();
1178                         list[0] = "foo";
1179                 }
1180                 
1181                 [Test]
1182                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1183                 public void SetItem_IList_OutOfRange()
1184                 {
1185                         IList<string> list = new List<string>();
1186                         list[0] = "foo";
1187                 }
1188
1189                 public class EquatableClass : IEquatable<EquatableClass>
1190                 {
1191                         int _x;
1192                         public EquatableClass (int x)
1193                         {
1194                                 _x = x;
1195                         }
1196
1197                         public bool Equals (EquatableClass other)
1198                         {
1199                                 return this._x == other._x;
1200                         }
1201                 }
1202         }
1203 }
1204 #endif
1205