a6951d9fe6ccb24f640e32c37a3d7ee92dff03ea
[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 FindAllTest ()
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, ExpectedException (typeof (ArgumentNullException))]
485                 public void FindAllNullTest ()
486                 {
487                         List <int> findings = _list1.FindAll (null);
488                 }
489
490                 [Test]
491                 public void FindIndexTest ()
492                 {
493                         int i = _list1.FindIndex (FindMultipleOfThree);
494                         Assert.AreEqual (7, i);
495
496                         i = _list1.FindIndex (FindMultipleOfTwelve);
497                         Assert.AreEqual (-1, i);
498                 }
499
500                 [Test, ExpectedException (typeof (ArgumentNullException))]
501                 public void FindIndexNullTest ()
502                 {
503                         int i = _list1.FindIndex (null);
504                 }
505
506                 [Test]
507                 public void FindLastTest ()
508                 {
509                         int i = _list1.FindLast (FindMultipleOfFour);
510                         Assert.AreEqual (40, i);
511
512                         i = _list1.FindLast (FindMultipleOfTwelve);
513                         Assert.AreEqual (default (int), i);
514                 }
515
516                 [Test, ExpectedException (typeof (ArgumentNullException))]
517                 public void FindLastNullTest ()
518                 {
519                         int i = _list1.FindLast (null);
520                 }
521
522                 // FIXME currently generates Invalid IL Code error
523                 /*
524                 [Test]
525                 public void ForEachTest ()
526                 {
527                         int i = 0;
528                         _list1.ForEach (delegate (int j) { i += j; });
529
530                         Assert.AreEqual (418, i);
531                 }
532                 */
533                 [Test]
534                 public void FindLastIndexTest ()
535                 {
536                         int i = _list1.FindLastIndex (FindMultipleOfFour);
537                         Assert.AreEqual (6, i);
538
539                         i = _list1.FindLastIndex (5, FindMultipleOfFour);
540                         Assert.AreEqual (5, i);
541
542                         i = _list1.FindIndex (FindMultipleOfTwelve);
543                         Assert.AreEqual (-1, i);
544                 }
545
546                 [Test, ExpectedException (typeof (ArgumentNullException))]
547                 public void FindLastIndexNullTest ()
548                 {
549                         int i = _list1.FindLastIndex (null);
550                 }
551
552                 [Test]
553                 public void RemoveTest ()
554                 {
555                         int count = _list1.Count;
556                         bool result = _list1.Remove (22);
557                         Assert.IsTrue (result);
558                         Assert.AreEqual (count - 1, _list1.Count);
559
560                         Assert.AreEqual (-1, _list1.IndexOf (22));
561
562                         result = _list1.Remove (0);
563                         Assert.IsFalse (result);
564                 }
565
566                 [Test]
567                 public void RemoveAllTest ()
568                 {
569                         int count = _list1.Count;
570                         int removedCount = _list1.RemoveAll (FindMultipleOfFour);
571                         Assert.AreEqual (4, removedCount);
572                         Assert.AreEqual (count - 4, _list1.Count);
573
574                         removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
575                         Assert.AreEqual (0, removedCount);
576                         Assert.AreEqual (count - 4, _list1.Count);
577                 }
578
579                 [Test]
580                 public void RemoveAtTest ()
581                 {
582                         int count = _list1.Count;
583                         _list1.RemoveAt (0);
584                         Assert.AreEqual (count - 1, _list1.Count);
585                         Assert.AreEqual (50, _list1 [0]);
586                 }
587
588                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
589                 public void RemoveOutOfRangeTest ()
590                 {
591                         _list1.RemoveAt (_list1.Count);
592                 }
593
594                 [Test]
595                 public void RemoveRangeTest ()
596                 {
597                         int count = _list1.Count;
598                         _list1.RemoveRange (1, 2);
599                         Assert.AreEqual (count - 2, _list1.Count);
600                         Assert.AreEqual (55, _list1 [0]);
601                         Assert.AreEqual (80, _list1 [1]);
602
603                         _list1.RemoveRange (0, 0);
604                         Assert.AreEqual (count - 2, _list1.Count);
605                 }
606
607                 [Test]
608                 public void RemoveRangeFromEmptyListTest ()
609                 {
610                         List<int> l = new List<int> ();
611                         l.RemoveRange (0, 0);
612                 }
613
614                 [Test, ExpectedException (typeof (ArgumentException))]
615                 public void RemoveRangeOutOfRangeTest ()
616                 {
617                         _list1.RemoveRange (1, _list1.Count);
618                 }
619
620                 [Test]
621                 public void ReverseTest ()
622                 {
623                         int count = _list1.Count;
624                         _list1.Reverse ();
625                         Assert.AreEqual (count, _list1.Count);
626
627                         Assert.AreEqual (63, _list1 [0]);
628                         Assert.AreEqual (55, _list1 [count - 1]);
629
630                         _list1.Reverse (0, 2);
631
632                         Assert.AreEqual (40, _list1 [0]);
633                         Assert.AreEqual (63, _list1 [1]);
634                 }
635
636                 [Test, ExpectedException (typeof (ArgumentException))]
637                 public void ReverseOutOfRangeTest ()
638                 {
639                         _list1.Reverse (1, _list1.Count);
640                 }
641
642                 [Test]
643                 public void ToArrayTest ()
644                 {
645                         int [] copiedContents = _list1.ToArray ();
646                         Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
647
648                         Assert.AreEqual (_list1.Count, copiedContents.Length);
649                         Assert.AreEqual (_list1 [0], copiedContents [0]);
650                 }
651
652                 [Test]
653                 public void TrimExcessTest ()
654                 {
655                         List <string> l = new List <string> ();
656                         l.Add ("foo");
657
658                         Assert.IsTrue (l.Count < l.Capacity);
659                         l.TrimExcess ();
660                         Assert.AreEqual (l.Count, l.Capacity);
661                 }
662
663                 bool IsPositive (int i)
664                 {
665                         return i >= 0;
666                 }
667
668                 [Test]
669                 public void TrueForAllTest ()
670                 {
671                         Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
672                         Assert.IsTrue (_list1.TrueForAll (IsPositive));
673                 }
674
675                 [Test, ExpectedException (typeof (ArgumentNullException))]
676                 public void TrueForAllNullTest ()
677                 {
678                         _list1.TrueForAll (null);
679                 }
680
681                 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
682                 public void CapacityOutOfRangeTest ()
683                 {
684                         _list1.Capacity = _list1.Count - 1;
685                 }
686
687                 [Test]
688                 public void BinarySearch_EmptyList ()
689                 {
690                         GenericComparer<int> comparer = new GenericComparer<int> ();
691                         List<int> l = new List<int> ();
692                         Assert.AreEqual (-1, l.BinarySearch (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 BinarySearch2_EmptyList ()
699                 {
700                         GenericComparer<int> comparer = new GenericComparer<int> ();
701                         List<int> l = new List<int> ();
702                         Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
703                         // bug 77030 - the comparer isn't called for an empty array/list
704                         Assert.IsFalse (comparer.Called, "Called");
705                 }
706
707                 [Test]
708                 public void AddRange_Bug77019 ()
709                 {
710                         List<int> l = new List<int> ();
711                         Dictionary<string, int> d = new Dictionary<string, int> ();
712                         l.AddRange (d.Values);
713                         Assert.AreEqual (0, l.Count, "Count");
714                 }
715
716                 [Test]
717                 public void VersionCheck_Add ()
718                 {
719                         List<int> list = new List<int> ();
720                         IEnumerator enumerator = list.GetEnumerator ();
721                         list.Add (5);
722
723                         try {
724                                 enumerator.MoveNext ();
725                                 Assert.Fail ("#1");
726                         } catch (InvalidOperationException) {
727                         }
728
729                         try {
730                                 enumerator.Reset ();
731                                 Assert.Fail ("#2");
732                         } catch (InvalidOperationException) {
733                         }
734
735                         enumerator = list.GetEnumerator ();
736                         enumerator.MoveNext ();
737                 }
738
739                 [Test]
740                 public void VersionCheck_AddRange ()
741                 {
742                         List<int> list = new List<int> ();
743                         IEnumerator enumerator = list.GetEnumerator ();
744                         list.AddRange (new int [] { 5, 7 });
745
746                         try {
747                                 enumerator.MoveNext ();
748                                 Assert.Fail ("#1");
749                         } catch (InvalidOperationException) {
750                         }
751
752                         try {
753                                 enumerator.Reset ();
754                                 Assert.Fail ("#2");
755                         } catch (InvalidOperationException) {
756                         }
757
758                         enumerator = list.GetEnumerator ();
759                         enumerator.MoveNext ();
760                 }
761
762                 [Test]
763                 public void VersionCheck_Clear ()
764                 {
765                         List<int> list = new List<int> ();
766                         IEnumerator enumerator = list.GetEnumerator ();
767                         list.Clear ();
768
769                         try {
770                                 enumerator.MoveNext ();
771                                 Assert.Fail ("#1");
772                         } catch (InvalidOperationException) {
773                         }
774
775                         try {
776                                 enumerator.Reset ();
777                                 Assert.Fail ("#2");
778                         } catch (InvalidOperationException) {
779                         }
780
781                         enumerator = list.GetEnumerator ();
782                         enumerator.MoveNext ();
783                 }
784
785                 [Test]
786                 public void VersionCheck_Insert ()
787                 {
788                         List<int> list = new List<int> ();
789                         IEnumerator enumerator = list.GetEnumerator ();
790                         list.Insert (0, 7);
791
792                         try {
793                                 enumerator.MoveNext ();
794                                 Assert.Fail ("#1");
795                         } catch (InvalidOperationException) {
796                         }
797
798                         try {
799                                 enumerator.Reset ();
800                                 Assert.Fail ("#2");
801                         } catch (InvalidOperationException) {
802                         }
803
804                         enumerator = list.GetEnumerator ();
805                         enumerator.MoveNext ();
806                 }
807
808                 [Test]
809                 public void VersionCheck_InsertRange ()
810                 {
811                         List<int> list = new List<int> ();
812                         IEnumerator enumerator = list.GetEnumerator ();
813                         list.InsertRange (0, new int [] { 5, 7 });
814
815                         try {
816                                 enumerator.MoveNext ();
817                                 Assert.Fail ("#1");
818                         } catch (InvalidOperationException) {
819                         }
820
821                         try {
822                                 enumerator.Reset ();
823                                 Assert.Fail ("#2");
824                         } catch (InvalidOperationException) {
825                         }
826
827                         enumerator = list.GetEnumerator ();
828                         enumerator.MoveNext ();
829                 }
830
831                 [Test]
832                 public void VersionCheck_Remove ()
833                 {
834                         List<int> list = new List<int> ();
835                         list.Add (5);
836                         IEnumerator enumerator = list.GetEnumerator ();
837                         // version number is not incremented if item does not exist in list
838                         list.Remove (7);
839                         enumerator.MoveNext ();
840                         list.Remove (5);
841
842                         try {
843                                 enumerator.MoveNext ();
844                                 Assert.Fail ("#1");
845                         } catch (InvalidOperationException) {
846                         }
847
848                         try {
849                                 enumerator.Reset ();
850                                 Assert.Fail ("#2");
851                         } catch (InvalidOperationException) {
852                         }
853
854                         enumerator = list.GetEnumerator ();
855                         enumerator.MoveNext ();
856                 }
857
858                 [Test]
859                 public void VersionCheck_RemoveAll ()
860                 {
861                         List<int> list = new List<int> ();
862                         list.Add (5);
863                         IEnumerator enumerator = list.GetEnumerator ();
864                         // version is not incremented if there are no items to remove
865                         list.RemoveAll (FindMultipleOfFour);
866                         enumerator.MoveNext ();
867                         list.Add (4);
868
869                         enumerator = list.GetEnumerator ();
870                         list.RemoveAll (FindMultipleOfFour);
871
872                         try {
873                                 enumerator.MoveNext ();
874                                 Assert.Fail ("#1");
875                         } catch (InvalidOperationException) {
876                         }
877
878                         try {
879                                 enumerator.Reset ();
880                                 Assert.Fail ("#2");
881                         } catch (InvalidOperationException) {
882                         }
883
884                         enumerator = list.GetEnumerator ();
885                         enumerator.MoveNext ();
886                 }
887
888                 [Test]
889                 public void VersionCheck_RemoveAt ()
890                 {
891                         List<int> list = new List<int> ();
892                         list.Add (5);
893                         IEnumerator enumerator = list.GetEnumerator ();
894                         list.RemoveAt (0);
895
896                         try {
897                                 enumerator.MoveNext ();
898                                 Assert.Fail ("#1");
899                         } catch (InvalidOperationException) {
900                         }
901
902                         try {
903                                 enumerator.Reset ();
904                                 Assert.Fail ("#2");
905                         } catch (InvalidOperationException) {
906                         }
907
908                         enumerator = list.GetEnumerator ();
909                         enumerator.MoveNext ();
910                 }
911
912                 [Test]
913                 public void VersionCheck_RemoveRange ()
914                 {
915                         List<int> list = new List<int> ();
916                         list.Add (5);
917                         IEnumerator enumerator = list.GetEnumerator ();
918                         // version is not incremented if count is zero
919                         list.RemoveRange (0, 0);
920                         enumerator.MoveNext ();
921                         enumerator.Reset ();
922                         list.RemoveRange (0, 1);
923
924                         try {
925                                 enumerator.MoveNext ();
926                                 Assert.Fail ("#1");
927                         } catch (InvalidOperationException) {
928                         }
929
930                         try {
931                                 enumerator.Reset ();
932                                 Assert.Fail ("#2");
933                         } catch (InvalidOperationException) {
934                         }
935
936                         enumerator = list.GetEnumerator ();
937                         enumerator.MoveNext ();
938                 }
939
940                 [Test]
941                 public void VersionCheck_Reverse ()
942                 {
943                         List<int> list = new List<int> ();
944                         IEnumerator enumerator = list.GetEnumerator ();
945                         list.Reverse ();
946
947                         try {
948                                 enumerator.MoveNext ();
949                                 Assert.Fail ("#A1");
950                         } catch (InvalidOperationException) {
951                         }
952
953                         try {
954                                 enumerator.Reset ();
955                                 Assert.Fail ("#A2");
956                         } catch (InvalidOperationException) {
957                         }
958
959                         enumerator = list.GetEnumerator ();
960                         list.Reverse (0, 0);
961
962                         try {
963                                 enumerator.MoveNext ();
964                                 Assert.Fail ("#B1");
965                         } catch (InvalidOperationException) {
966                         }
967
968                         try {
969                                 enumerator.Reset ();
970                                 Assert.Fail ("#B2");
971                         } catch (InvalidOperationException) {
972                         }
973
974                         enumerator = list.GetEnumerator ();
975                         enumerator.MoveNext ();
976                 }
977
978                 class SortTestComparer: IComparer<string> {
979
980                         public int Compare (string s1, string s2)
981                         {
982                                 return String.Compare (s1, s2);
983                         }
984                 }
985
986                 [Test]
987                 public void Sort_Bug76361 ()
988                 {
989                         SortTestComparer comparer = new SortTestComparer ();
990                         List<string> l = new List<string> ();
991                         l.Add ("foo");
992                         l.Add ("bar");
993                         l.Sort (comparer);
994                         Assert.AreEqual ("bar", l[0], "0");
995                         Assert.AreEqual ("foo", l[1], "1");
996                         Assert.AreEqual (2, l.Count, "Count");
997                 }
998
999                 // for bug #77039 test case
1000                 class GenericIComparable: IComparable<GenericIComparable> {
1001                         private int _NumberToSortOn;
1002
1003                         public int NumberToSortOn {
1004                                 get { return _NumberToSortOn; }
1005                                 set { _NumberToSortOn = value; }
1006                         }
1007
1008                         public GenericIComparable (int val)
1009                         {
1010                                 _NumberToSortOn = val;
1011                         }
1012
1013                         public int CompareTo (GenericIComparable other)
1014                         {
1015                                 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1016                         }
1017                 }
1018
1019                 [Test]
1020                 public void Sort_GenericIComparable_Bug77039 ()
1021                 {
1022                         List<GenericIComparable> l = new List<GenericIComparable> ();
1023                         l.Add (new GenericIComparable (2));
1024                         l.Add (new GenericIComparable (1));
1025                         l.Add (new GenericIComparable (3));
1026                         l.Sort ();
1027                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1028                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1029                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1030                 }
1031
1032                 class NonGenericIComparable: IComparable {
1033                         private int _NumberToSortOn;
1034
1035                         public int NumberToSortOn {
1036                                 get { return _NumberToSortOn; }
1037                                 set { _NumberToSortOn = value; }
1038                         }
1039
1040                         public NonGenericIComparable (int val)
1041                         {
1042                                 _NumberToSortOn = val;
1043                         }
1044
1045                         public int CompareTo (object obj)
1046                         {
1047                                 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1048                         }
1049                 }
1050
1051                 [Test]
1052                 public void Sort_NonGenericIComparable ()
1053                 {
1054                         List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1055                         l.Add (new NonGenericIComparable (2));
1056                         l.Add (new NonGenericIComparable (1));
1057                         l.Add (new NonGenericIComparable (3));
1058                         l.Sort ();
1059                         Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1060                         Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1061                         Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1062                 }
1063
1064                 class NonComparable {
1065                 }
1066
1067                 [Test]
1068                 public void Sort_GenericNonIComparable ()
1069                 {
1070                         List<NonComparable> l = new List<NonComparable> ();
1071                         l.Sort ();
1072                         // no element -> no sort -> no exception
1073                         l.Add (new NonComparable ());
1074                         l.Sort ();
1075                         // one element -> no sort -> no exception
1076                 }
1077
1078                 [Test]
1079                 [ExpectedException (typeof (InvalidOperationException))]
1080                 public void Sort_GenericNonIComparable_2 ()
1081                 {
1082                         List<NonComparable> l = new List<NonComparable> ();
1083                         l.Add (new NonComparable ());
1084                         l.Add (new NonComparable ());
1085                         l.Sort ();
1086                         // two element -> sort -> exception!
1087                 }
1088                 
1089                 [Test]
1090                 void IList_Contains_InvalidType()
1091                 {
1092                         List<string> list = new List<string>();
1093                         list.Add("foo");
1094                         Assert.IsFalse (((IList)list).Contains(new object()));
1095                 }
1096                 
1097                 [Test]
1098                 void IList_IndexOf_InvalidType()
1099                 {
1100                         List<string> list = new List<string>();
1101                         list.Add("foo");
1102                         Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1103                 }
1104
1105                 // for bug #77277 test case
1106                 [Test]
1107                 public void Test_ContainsAndIndexOf_EquatableItem ()
1108                 {
1109                         List<EquatableClass> list = new List<EquatableClass> ();
1110                         EquatableClass item0 = new EquatableClass (0);
1111                         EquatableClass item1 = new EquatableClass (1);
1112
1113                         list.Add (item0);
1114                         list.Add (item1);
1115                         list.Add (item0);
1116
1117                         Assert.AreEqual (true, list.Contains (item0), "#0");
1118                         Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1119                         Assert.AreEqual (0, list.IndexOf (item0), "#2");
1120                         Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1121                         Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1122                         Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1123                 }
1124                 
1125                 [Test]
1126                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1127                 public void SetItem_OutOfRange()
1128                 {
1129                         List<string> list = new List<string>();
1130                         list[0] = "foo";
1131                 }
1132                 
1133                 [Test]
1134                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1135                 public void SetItem_IList_OutOfRange()
1136                 {
1137                         IList<string> list = new List<string>();
1138                         list[0] = "foo";
1139                 }
1140
1141                 public class EquatableClass : IEquatable<EquatableClass>
1142                 {
1143                         int _x;
1144                         public EquatableClass (int x)
1145                         {
1146                                 _x = x;
1147                         }
1148
1149                         public bool Equals (EquatableClass other)
1150                         {
1151                                 return this._x == other._x;
1152                         }
1153                 }
1154         }
1155 }
1156 #endif
1157