2 // MonoTests.System.Collections.Generic.Test.ListTest
5 // David Waite (mass@akuma.org)
6 // Andres G. Aragoneses (andres.aragoneses@7digital.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright (C) 2005 David Waite (mass@akuma.org)
10 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Copyright 2012 7digital Ltd (http://www.7digital.com).
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.Collections.ObjectModel;
38 using System.Runtime.Serialization.Formatters.Binary;
41 using NUnit.Framework;
43 namespace MonoTests.System.Collections.Generic {
45 class GenericComparer<T> : IComparer<T> {
47 private bool called = false;
57 public int Compare (T x, T y)
67 static byte [] _serializedList = new byte [] {
68 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
70 0x7e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
71 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65,
72 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x60,
73 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
74 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72,
75 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
76 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20,
77 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75,
78 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69,
79 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62,
80 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34,
81 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00, 0x00, 0x00, 0x06,
82 0x5f, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a,
83 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x07,
84 0x00, 0x00, 0x08, 0x08, 0x08, 0x09, 0x02, 0x00, 0x00, 0x00, 0x03,
85 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x02, 0x00, 0x00,
86 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 int [] _list1_contents;
95 // FIXME arrays currently do not support generic collection
97 _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
98 // _list1 = new List <int> (_list1_contents);
100 _list1 = new List <int> (8);
101 foreach (int i in _list1_contents)
105 [Test] // This was for bug #74980
106 public void InsertTest ()
108 List <string> test = new List <string> ();
109 test.Insert (0, "a");
110 test.Insert (0, "b");
111 test.Insert (1, "c");
113 Assert.AreEqual (3, test.Count);
114 Assert.AreEqual ("b", test [0]);
115 Assert.AreEqual ("c", test [1]);
116 Assert.AreEqual ("a", test [2]);
120 public void InsertRangeTest ()
122 int count = _list1.Count;
123 // FIXME arrays currently do not support generic collection
125 int [] items = {1, 2, 3};
126 // List <int> newRange = new List <int> (items);
127 List <int> newRange = new List <int> (3);
128 foreach (int i in items)
130 _list1.InsertRange (1, newRange);
131 Assert.AreEqual (count + 3, _list1.Count);
132 Assert.AreEqual (55, _list1 [0]);
133 Assert.AreEqual (1, _list1 [1]);
134 Assert.AreEqual (2, _list1 [2]);
135 Assert.AreEqual (3, _list1 [3]);
136 Assert.AreEqual (50, _list1 [4]);
138 newRange = new List <int> ();
139 List <int> li = new List <int> ();
141 newRange.InsertRange (0, li);
142 newRange.InsertRange (newRange.Count, li);
143 Assert.AreEqual (2, newRange.Count);
147 public void InsertSelfTest()
149 List <int> range = new List <int> (5);
150 for (int i = 0; i < 5; ++ i)
153 range.InsertRange(2, range);
154 Assert.AreEqual (10, range.Count);
155 Assert.AreEqual (0, range [0]);
156 Assert.AreEqual (1, range [1]);
157 Assert.AreEqual (0, range [2]);
158 Assert.AreEqual (1, range [3]);
159 Assert.AreEqual (2, range [4]);
160 Assert.AreEqual (3, range [5]);
161 Assert.AreEqual (4, range [6]);
162 Assert.AreEqual (2, range [7]);
163 Assert.AreEqual (3, range [8]);
164 Assert.AreEqual (4, range [9]);
167 [Test, ExpectedException (typeof (ArgumentNullException))]
168 public void InsertRangeNullTest ()
170 IEnumerable <int> n = null;
171 _list1.InsertRange (0, n);
174 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
175 public void InsertRangeNegativeIndexTest ()
177 _list1.InsertRange (-1, _list1);
181 public void IndexOfTest ()
183 List <int> l = new List <int> ();
188 Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
191 [Test, ExpectedException(typeof (ArgumentException))]
192 public void IList_InsertInvalidType1 ()
194 IList list = _list1 as IList;
195 list.Insert(0, new object());
198 [Test, ExpectedException(typeof (ArgumentException))]
199 public void IList_InsertInvalidType2 ()
201 IList list = _list1 as IList;
202 list.Insert(0, null);
205 [Test, ExpectedException(typeof (ArgumentException))]
206 public void IList_AddInvalidType1()
208 IList list = _list1 as IList;
209 list.Add(new object());
212 [Test, ExpectedException(typeof (ArgumentException))]
213 public void IList_AddInvalidType2()
215 IList list = _list1 as IList;
220 public void IList_RemoveInvalidType()
222 IList list = _list1 as IList;
223 int nCount = list.Count;
224 list.Remove(new object());
225 Assert.AreEqual(nCount, list.Count);
228 Assert.AreEqual(nCount, list.Count);
231 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
232 public void IndexOfOutOfRangeTest ()
234 List <int> l = new List <int> (4);
239 public void GetRangeTest ()
241 List <int> r = _list1.GetRange (2, 4);
242 Assert.AreEqual (4, r.Count);
243 Assert.AreEqual (22, r [0]);
244 Assert.AreEqual (80, r [1]);
245 Assert.AreEqual (56, r [2]);
246 Assert.AreEqual (52, r [3]);
250 public void EnumeratorTest ()
252 List <int>.Enumerator e = _list1.GetEnumerator ();
253 for (int i = 0; i < _list1_contents.Length; i++)
255 Assert.IsTrue (e.MoveNext ());
256 Assert.AreEqual (_list1_contents [i], e.Current);
258 Assert.IsFalse (e.MoveNext ());
262 public void ConstructWithSizeTest ()
264 List <object> l_1 = new List <object> (1);
265 List <object> l_2 = new List <object> (50);
266 List <object> l_3 = new List <object> (0);
268 Assert.AreEqual (1, l_1.Capacity);
269 Assert.AreEqual (50, l_2.Capacity);
270 Assert.AreEqual (0, l_3.Capacity);
273 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
274 public void ConstructWithInvalidSizeTest ()
276 List <int> l = new List <int> (-1);
280 public void ConstructWithCollectionTest ()
282 List <int> l1 = new List <int> (_list1);
283 Assert.AreEqual (_list1.Count, l1.Count);
284 Assert.AreEqual (l1.Count, l1.Capacity);
285 for (int i = 0; i < l1.Count; i++)
286 Assert.AreEqual (_list1 [i], l1 [i]);
288 var input = new [] { "a", "b", "c" };
289 var l2 = new List<string>(input);
290 Assert.AreEqual (3, l2.Capacity);
293 [Test, ExpectedException (typeof (ArgumentNullException))]
294 public void ConstructWithInvalidCollectionTest ()
297 List <int> l1 = new List <int> (n);
301 public void AddTest ()
303 int count = _list1.Count;
305 Assert.AreEqual (count + 1, _list1.Count);
306 Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
310 public void AddRangeTest ()
312 int count = _list1.Count;
313 // FIXME arrays currently do not support generic collection
315 int [] range = { -1, -2, -3 };
316 List <int> tmp = new List <int> (3);
317 foreach (int i in range)
319 // _list1.AddRange (range);
320 _list1.AddRange (tmp);
322 Assert.AreEqual (count + 3, _list1.Count);
323 Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
324 Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
325 Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
328 [Test, ExpectedException (typeof (ArgumentNullException))]
329 public void AddNullRangeTest ()
336 public void BinarySearchTest ()
338 List <int> l = new List <int> (_list1);
340 Assert.AreEqual (0, l.BinarySearch (22));
341 Assert.AreEqual (-2, l.BinarySearch (23));
342 Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
345 #if !NET_4_0 // FIXME: the blob contains the 2.0 mscorlib version
348 [Category ("TargetJvmNotWorking")]
349 public void SerializeTest ()
351 List <int> list = new List <int> ();
357 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
359 BinaryFormatter bf = new BinaryFormatter ();
361 MemoryStream ms = new MemoryStream ();
362 bf.Serialize (ms, list);
364 byte [] buffer = new byte [ms.Length];
366 ms.Read (buffer, 0, buffer.Length);
368 Assert.AreEqual (_serializedList, buffer);
374 [Category ("TargetJvmNotWorking")]
375 public void DeserializeTest ()
377 MemoryStream ms = new MemoryStream ();
378 ms.Write (_serializedList, 0, _serializedList.Length);
382 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
384 BinaryFormatter bf = new BinaryFormatter ();
386 List<int> list = (List<int>) bf.Deserialize (ms);
387 Assert.AreEqual (3, list.Count, "#1");
388 Assert.AreEqual (5, list [0], "#2");
389 Assert.AreEqual (0, list [1], "#3");
390 Assert.AreEqual (7, list [2], "#4");
394 public void SortTest ()
396 List <int> l = new List <int> (_list1);
398 Assert.AreEqual (_list1.Count, l.Count);
399 Assert.AreEqual (22, l [0]);
403 Assert.IsTrue (minimum <= i);
409 public void ClearTest ()
411 int capacity = _list1.Capacity;
413 Assert.AreEqual (0, _list1.Count);
414 Assert.AreEqual (capacity, _list1.Capacity);
418 public void ContainsTest ()
420 Assert.IsTrue (_list1.Contains (22));
421 Assert.IsFalse (_list1.Contains (23));
424 private string StringConvert (int i)
426 return i.ToString ();
430 public void ConvertAllTest ()
432 List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
433 Assert.AreEqual (_list1.Count, s.Count);
434 Assert.AreEqual ("55", s [0]);
438 public void CopyToTest ()
440 int [] a = new int [2];
441 _list1.CopyTo (1, a, 0, 2);
442 Assert.AreEqual (50, a [0]);
443 Assert.AreEqual (22, a [1]);
445 int [] b = new int [_list1.Count + 1];
446 b [_list1.Count] = 555;
448 Assert.AreEqual (55, b [0]);
449 Assert.AreEqual (555, b [_list1.Count]);
452 _list1.CopyTo (b, 1);
453 Assert.AreEqual (888, b [0]);
454 Assert.AreEqual (55, b [1]);
457 [Test, ExpectedException (typeof (ArgumentNullException))]
458 public void CopyToNullTest ()
461 _list1.CopyTo (0, a, 0, 0);
464 static bool FindMultipleOfThree (int i)
469 static bool FindMultipleOfFour (int i)
474 static bool FindMultipleOfTwelve (int i)
476 return (i % 12) == 0;
480 public void FindTest ()
482 int i = _list1.Find (FindMultipleOfThree);
483 Assert.AreEqual (63, i);
485 i = _list1.Find (FindMultipleOfTwelve);
486 Assert.AreEqual (default (int), i);
489 [Test, ExpectedException (typeof (ArgumentNullException))]
490 public void FindNullTest ()
492 int i = _list1.Find (null);
496 public void FindAllSmallTest ()
498 List <int> findings = _list1.FindAll (FindMultipleOfFour);
499 Assert.AreEqual (4, findings.Count);
500 Assert.AreEqual (80, findings [0]);
501 Assert.AreEqual (56, findings [1]);
502 Assert.AreEqual (52, findings [2]);
503 Assert.AreEqual (40, findings [3]);
505 findings = _list1.FindAll (FindMultipleOfTwelve);
506 Assert.IsNotNull (findings);
507 Assert.AreEqual (0, findings.Count);
511 public void FindAllMediumTest ()
513 List <int> integers = new List <int> (10000);
514 for (int i = 1; i <= 10000; i++)
517 List <int> results = integers.FindAll (FindMultipleOfFour);
519 Assert.IsNotNull (results);
520 Assert.AreEqual (2500, results.Count);
522 results = integers.FindAll (FindMultipleOfTwelve);
524 Assert.IsNotNull (results);
525 Assert.AreEqual (833, results.Count);
529 public void FindAllLargeTest ()
531 List <int> integers = new List <int> (70000);
532 for (int i = 1; i <= 80000; i++)
535 List <int> results = integers.FindAll (FindMultipleOfFour);
537 Assert.IsNotNull (results);
538 Assert.AreEqual (20000, results.Count);
540 results = integers.FindAll (FindMultipleOfTwelve);
542 Assert.IsNotNull (results);
543 Assert.AreEqual (6666, results.Count);
546 [Test, ExpectedException (typeof (ArgumentNullException))]
547 public void FindAllNullTest ()
549 List <int> findings = _list1.FindAll (null);
553 public void FindIndexTest ()
555 int i = _list1.FindIndex (FindMultipleOfThree);
556 Assert.AreEqual (7, i);
558 i = _list1.FindIndex (FindMultipleOfTwelve);
559 Assert.AreEqual (-1, i);
561 var a = new List<int> () { 2, 2, 2, 3, 2 };
562 Assert.AreEqual (2, a.FindIndex (2, 2, l => true));
566 public void FindIndex_Invalid ()
569 _list1.FindIndex (null);
571 } catch (ArgumentNullException) {
575 _list1.FindIndex (-1, l => true);
577 } catch (ArgumentOutOfRangeException) {
581 _list1.FindIndex (-1, 0, l => true);
583 } catch (ArgumentOutOfRangeException) {
587 _list1.FindIndex (0, -1, l => true);
589 } catch (ArgumentOutOfRangeException) {
593 _list1.FindIndex (100, l => true);
595 } catch (ArgumentOutOfRangeException) {
599 _list1.FindIndex (100, 0, l => true);
601 } catch (ArgumentOutOfRangeException) {
605 _list1.FindIndex (7, 2, l => true);
607 } catch (ArgumentOutOfRangeException) {
612 public void FindLastTest ()
614 int i = _list1.FindLast (FindMultipleOfFour);
615 Assert.AreEqual (40, i);
617 i = _list1.FindLast (FindMultipleOfTwelve);
618 Assert.AreEqual (default (int), i);
621 [Test, ExpectedException (typeof (ArgumentNullException))]
622 public void FindLastNullTest ()
624 int i = _list1.FindLast (null);
628 public void ForEachTest ()
631 _list1.ForEach (delegate (int j) { i += j; });
633 Assert.AreEqual (418, i);
637 public void FindLastIndexTest ()
639 int i = _list1.FindLastIndex (FindMultipleOfFour);
640 Assert.AreEqual (6, i);
642 i = _list1.FindLastIndex (5, FindMultipleOfFour);
643 Assert.AreEqual (5, i);
645 i = _list1.FindIndex (FindMultipleOfTwelve);
646 Assert.AreEqual (-1, i);
648 Assert.AreEqual (2, _list1.FindLastIndex (2, 3, l => true));
649 Assert.AreEqual (2, _list1.FindLastIndex (2, 2, l => true));
650 Assert.AreEqual (1, _list1.FindLastIndex (1, 2, l => true));
654 public void FindLastIndex_Invalid ()
657 _list1.FindLastIndex (null);
659 } catch (ArgumentNullException) {
663 _list1.FindLastIndex (-1, l => true);
665 } catch (ArgumentOutOfRangeException) {
669 _list1.FindLastIndex (-1, 0, l => true);
671 } catch (ArgumentOutOfRangeException) {
675 _list1.FindLastIndex (0, -1, l => true);
677 } catch (ArgumentOutOfRangeException) {
681 _list1.FindLastIndex (100, l => true);
683 } catch (ArgumentOutOfRangeException) {
687 _list1.FindLastIndex (100, 0, l => true);
689 } catch (ArgumentOutOfRangeException) {
693 _list1.FindLastIndex (2, 4, l => true);
695 } catch (ArgumentOutOfRangeException) {
700 public void RemoveTest ()
702 int count = _list1.Count;
703 bool result = _list1.Remove (22);
704 Assert.IsTrue (result);
705 Assert.AreEqual (count - 1, _list1.Count);
707 Assert.AreEqual (-1, _list1.IndexOf (22));
709 result = _list1.Remove (0);
710 Assert.IsFalse (result);
714 public void RemoveAllTest ()
716 int count = _list1.Count;
717 int removedCount = _list1.RemoveAll (FindMultipleOfFour);
718 Assert.AreEqual (4, removedCount);
719 Assert.AreEqual (count - 4, _list1.Count);
721 removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
722 Assert.AreEqual (0, removedCount);
723 Assert.AreEqual (count - 4, _list1.Count);
727 public void RemoveAtTest ()
729 int count = _list1.Count;
731 Assert.AreEqual (count - 1, _list1.Count);
732 Assert.AreEqual (50, _list1 [0]);
735 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
736 public void RemoveOutOfRangeTest ()
738 _list1.RemoveAt (_list1.Count);
742 public void RemoveRangeTest ()
744 int count = _list1.Count;
745 _list1.RemoveRange (1, 2);
746 Assert.AreEqual (count - 2, _list1.Count);
747 Assert.AreEqual (55, _list1 [0]);
748 Assert.AreEqual (80, _list1 [1]);
750 _list1.RemoveRange (0, 0);
751 Assert.AreEqual (count - 2, _list1.Count);
755 public void RemoveRangeFromEmptyListTest ()
757 List<int> l = new List<int> ();
758 l.RemoveRange (0, 0);
761 [Test, ExpectedException (typeof (ArgumentException))]
762 public void RemoveRangeOutOfRangeTest ()
764 _list1.RemoveRange (1, _list1.Count);
768 public void ReverseTest ()
770 int count = _list1.Count;
772 Assert.AreEqual (count, _list1.Count);
774 Assert.AreEqual (63, _list1 [0]);
775 Assert.AreEqual (55, _list1 [count - 1]);
777 _list1.Reverse (0, 2);
779 Assert.AreEqual (40, _list1 [0]);
780 Assert.AreEqual (63, _list1 [1]);
783 [Test, ExpectedException (typeof (ArgumentException))]
784 public void ReverseOutOfRangeTest ()
786 _list1.Reverse (1, _list1.Count);
790 public void ToArrayTest ()
792 int [] copiedContents = _list1.ToArray ();
793 Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
795 Assert.AreEqual (_list1.Count, copiedContents.Length);
796 Assert.AreEqual (_list1 [0], copiedContents [0]);
800 public void TrimExcessTest ()
802 List <string> l = new List <string> ();
805 Assert.IsTrue (l.Count < l.Capacity);
807 Assert.AreEqual (l.Count, l.Capacity);
810 bool IsPositive (int i)
816 public void TrueForAllTest ()
818 Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
819 Assert.IsTrue (_list1.TrueForAll (IsPositive));
822 [Test, ExpectedException (typeof (ArgumentNullException))]
823 public void TrueForAllNullTest ()
825 _list1.TrueForAll (null);
828 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
829 public void CapacityOutOfRangeTest ()
831 _list1.Capacity = _list1.Count - 1;
835 public void BinarySearch_EmptyList ()
837 GenericComparer<int> comparer = new GenericComparer<int> ();
838 List<int> l = new List<int> ();
839 Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
840 // bug 77030 - the comparer isn't called for an empty array/list
841 Assert.IsFalse (comparer.Called, "Called");
845 public void BinarySearch2_EmptyList ()
847 GenericComparer<int> comparer = new GenericComparer<int> ();
848 List<int> l = new List<int> ();
849 Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
850 // bug 77030 - the comparer isn't called for an empty array/list
851 Assert.IsFalse (comparer.Called, "Called");
855 public void AddRange_Bug77019 ()
857 List<int> l = new List<int> ();
858 Dictionary<string, int> d = new Dictionary<string, int> ();
859 l.AddRange (d.Values);
860 Assert.AreEqual (0, l.Count, "Count");
864 public void VersionCheck_Add ()
866 List<int> list = new List<int> ();
867 IEnumerator enumerator = list.GetEnumerator ();
871 enumerator.MoveNext ();
873 } catch (InvalidOperationException) {
879 } catch (InvalidOperationException) {
882 enumerator = list.GetEnumerator ();
883 enumerator.MoveNext ();
887 public void VersionCheck_AddRange ()
889 List<int> list = new List<int> ();
890 IEnumerator enumerator = list.GetEnumerator ();
891 list.AddRange (new int [] { 5, 7 });
894 enumerator.MoveNext ();
896 } catch (InvalidOperationException) {
902 } catch (InvalidOperationException) {
905 enumerator = list.GetEnumerator ();
906 enumerator.MoveNext ();
910 public void VersionCheck_Clear ()
912 List<int> list = new List<int> ();
913 IEnumerator enumerator = list.GetEnumerator ();
917 enumerator.MoveNext ();
919 } catch (InvalidOperationException) {
925 } catch (InvalidOperationException) {
928 enumerator = list.GetEnumerator ();
929 enumerator.MoveNext ();
933 public void VersionCheck_Insert ()
935 List<int> list = new List<int> ();
936 IEnumerator enumerator = list.GetEnumerator ();
940 enumerator.MoveNext ();
942 } catch (InvalidOperationException) {
948 } catch (InvalidOperationException) {
951 enumerator = list.GetEnumerator ();
952 enumerator.MoveNext ();
956 public void VersionCheck_InsertRange ()
958 List<int> list = new List<int> ();
959 IEnumerator enumerator = list.GetEnumerator ();
960 list.InsertRange (0, new int [] { 5, 7 });
963 enumerator.MoveNext ();
965 } catch (InvalidOperationException) {
971 } catch (InvalidOperationException) {
974 enumerator = list.GetEnumerator ();
975 enumerator.MoveNext ();
979 public void VersionCheck_Remove ()
981 List<int> list = new List<int> ();
983 IEnumerator enumerator = list.GetEnumerator ();
984 // version number is not incremented if item does not exist in list
986 enumerator.MoveNext ();
990 enumerator.MoveNext ();
992 } catch (InvalidOperationException) {
998 } catch (InvalidOperationException) {
1001 enumerator = list.GetEnumerator ();
1002 enumerator.MoveNext ();
1006 public void VersionCheck_RemoveAll ()
1008 List<int> list = new List<int> ();
1010 IEnumerator enumerator = list.GetEnumerator ();
1011 // version is not incremented if there are no items to remove
1012 list.RemoveAll (FindMultipleOfFour);
1013 enumerator.MoveNext ();
1016 enumerator = list.GetEnumerator ();
1017 list.RemoveAll (FindMultipleOfFour);
1020 enumerator.MoveNext ();
1022 } catch (InvalidOperationException) {
1026 enumerator.Reset ();
1028 } catch (InvalidOperationException) {
1031 enumerator = list.GetEnumerator ();
1032 enumerator.MoveNext ();
1036 public void VersionCheck_RemoveAt ()
1038 List<int> list = new List<int> ();
1040 IEnumerator enumerator = list.GetEnumerator ();
1044 enumerator.MoveNext ();
1046 } catch (InvalidOperationException) {
1050 enumerator.Reset ();
1052 } catch (InvalidOperationException) {
1055 enumerator = list.GetEnumerator ();
1056 enumerator.MoveNext ();
1060 public void VersionCheck_RemoveRange ()
1062 List<int> list = new List<int> ();
1064 IEnumerator enumerator = list.GetEnumerator ();
1065 // version is not incremented if count is zero
1066 list.RemoveRange (0, 0);
1067 enumerator.MoveNext ();
1068 enumerator.Reset ();
1069 list.RemoveRange (0, 1);
1072 enumerator.MoveNext ();
1074 } catch (InvalidOperationException) {
1078 enumerator.Reset ();
1080 } catch (InvalidOperationException) {
1083 enumerator = list.GetEnumerator ();
1084 enumerator.MoveNext ();
1087 [Test, ExpectedException (typeof (InvalidOperationException))] // #699182
1088 public void VersionCheck_Indexer ()
1090 var list = new List<int> () { 0, 2, 3 };
1091 var enumerator = list.GetEnumerator ();
1095 enumerator.MoveNext ();
1099 public void VersionCheck_Reverse ()
1101 List<int> list = new List<int> ();
1102 IEnumerator enumerator = list.GetEnumerator ();
1106 enumerator.MoveNext ();
1107 Assert.Fail ("#A1");
1108 } catch (InvalidOperationException) {
1112 enumerator.Reset ();
1113 Assert.Fail ("#A2");
1114 } catch (InvalidOperationException) {
1117 enumerator = list.GetEnumerator ();
1118 list.Reverse (0, 0);
1121 enumerator.MoveNext ();
1122 Assert.Fail ("#B1");
1123 } catch (InvalidOperationException) {
1127 enumerator.Reset ();
1128 Assert.Fail ("#B2");
1129 } catch (InvalidOperationException) {
1132 enumerator = list.GetEnumerator ();
1133 enumerator.MoveNext ();
1136 class SortTestComparer: IComparer<string> {
1138 public int Compare (string s1, string s2)
1140 return String.Compare (s1, s2);
1145 public void Sort_Bug76361 ()
1147 SortTestComparer comparer = new SortTestComparer ();
1148 List<string> l = new List<string> ();
1152 Assert.AreEqual ("bar", l[0], "0");
1153 Assert.AreEqual ("foo", l[1], "1");
1154 Assert.AreEqual (2, l.Count, "Count");
1157 // for bug #77039 test case
1158 class GenericIComparable: IComparable<GenericIComparable> {
1159 private int _NumberToSortOn;
1161 public int NumberToSortOn {
1162 get { return _NumberToSortOn; }
1163 set { _NumberToSortOn = value; }
1166 public GenericIComparable (int val)
1168 _NumberToSortOn = val;
1171 public int CompareTo (GenericIComparable other)
1173 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1178 public void Sort_GenericIComparable_Bug77039 ()
1180 List<GenericIComparable> l = new List<GenericIComparable> ();
1181 l.Add (new GenericIComparable (2));
1182 l.Add (new GenericIComparable (1));
1183 l.Add (new GenericIComparable (3));
1185 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1186 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1187 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1190 class NonGenericIComparable: IComparable {
1191 private int _NumberToSortOn;
1193 public int NumberToSortOn {
1194 get { return _NumberToSortOn; }
1195 set { _NumberToSortOn = value; }
1198 public NonGenericIComparable (int val)
1200 _NumberToSortOn = val;
1203 public int CompareTo (object obj)
1205 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1210 public void Sort_NonGenericIComparable ()
1212 List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1213 l.Add (new NonGenericIComparable (2));
1214 l.Add (new NonGenericIComparable (1));
1215 l.Add (new NonGenericIComparable (3));
1217 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1218 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1219 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1222 class NonComparable {
1226 public void Sort_GenericNonIComparable ()
1228 List<NonComparable> l = new List<NonComparable> ();
1230 // no element -> no sort -> no exception
1231 l.Add (new NonComparable ());
1233 // one element -> no sort -> no exception
1237 [ExpectedException (typeof (InvalidOperationException))]
1238 public void Sort_GenericNonIComparable_2 ()
1240 List<NonComparable> l = new List<NonComparable> ();
1241 l.Add (new NonComparable ());
1242 l.Add (new NonComparable ());
1244 // two element -> sort -> exception!
1248 public void IList_Contains_InvalidType()
1250 List<string> list = new List<string>();
1252 Assert.IsFalse (((IList)list).Contains(new object()));
1254 Assert.IsFalse (((IList)_list1).Contains(null));
1258 public void IList_IndexOf_InvalidType()
1260 List<string> list = new List<string>();
1262 Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1264 Assert.AreEqual (-1, ((IList)_list1).IndexOf(null));
1267 // for bug #77277 test case
1269 public void Test_ContainsAndIndexOf_EquatableItem ()
1271 List<EquatableClass> list = new List<EquatableClass> ();
1272 EquatableClass item0 = new EquatableClass (0);
1273 EquatableClass item1 = new EquatableClass (1);
1279 Assert.AreEqual (true, list.Contains (item0), "#0");
1280 Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1281 Assert.AreEqual (0, list.IndexOf (item0), "#2");
1282 Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1283 Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1284 Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1287 // for bug #81387 test case
1289 public void Test_Contains_After_Remove ()
1291 List<int> list = new List<int> ();
1296 Assert.AreEqual (false, list.Contains (2), "#0");
1300 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1301 public void SetItem_OutOfRange()
1303 List<string> list = new List<string>();
1308 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1309 public void SetItem_IList_OutOfRange()
1311 IList<string> list = new List<string>();
1315 public class EquatableClass : IEquatable<EquatableClass>
1318 public EquatableClass (int x)
1323 public bool Equals (EquatableClass other)
1325 return this._x == other._x;
1341 // based on #491858, #517415
1342 public void Enumerator_Current ()
1344 var e1 = new List<int>.Enumerator ();
1345 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1347 var d = new List<int> ();
1348 var e2 = d.GetEnumerator ();
1349 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1351 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1353 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1355 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1356 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1358 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1360 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1362 var e4 = ((IEnumerable) d).GetEnumerator ();
1363 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1365 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1366 ((IDisposable) e4).Dispose ();
1367 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1371 public void Enumerator_Reset ()
1373 var l = new List<int> () {
1377 IEnumerator<int> e = l.GetEnumerator ();
1378 Assert.IsTrue (e.MoveNext (), "#1");
1379 Assert.AreEqual (4, e.Current, "#2");
1381 Assert.AreEqual (0, e.Current, "#3");
1384 [Test] //bug #672907
1385 public void ICollectionCopyToExceptions ()
1387 var l = new List <int> ();
1392 } catch (Exception e) {
1393 Assert.IsTrue (e is ArgumentNullException, "#2");
1397 x.CopyTo (new int [10], -1);
1399 } catch (Exception e) {
1400 Assert.IsTrue (e is ArgumentOutOfRangeException, "#4");
1404 x.CopyTo (new int [10, 1], 0);
1406 } catch (Exception e) {
1407 Assert.IsTrue (e is ArgumentException, "#6");
1411 x.CopyTo (Array.CreateInstance (typeof (int), new int [] { 10 }, new int[] { 1 }), 0);
1413 } catch (Exception e) {
1414 Assert.IsTrue (e is ArgumentException, "#8");
1417 l.Add (10); l.Add (20);
1419 x.CopyTo (new int [1], 0);
1421 } catch (Exception e) {
1422 Assert.IsTrue (e is ArgumentException, "#10");
1427 public void LastIndexOfEmpty_2558 () {
1428 var l = new List<int> ();
1429 Assert.AreEqual (-1, l.IndexOf (-1));
1433 #region Enumerator mutability
1439 class Foo : IEnumerable<Bar>
1445 enumerator = new Baz ();
1448 public IEnumerator<Bar> GetEnumerator ()
1453 IEnumerator IEnumerable.GetEnumerator ()
1459 class Baz : IEnumerator<Bar>
1461 public bool DisposeWasCalled = false;
1463 public void Dispose ()
1465 DisposeWasCalled = true;
1468 public bool MoveNext ()
1470 return false; //assume empty collection
1473 public void Reset ()
1479 get { return null; }
1482 object IEnumerator.Current
1484 get { return Current; }
1489 public void PremiseAboutDisposeBeingCalledWhenLooping ()
1491 Foo enumerable = new Foo ();
1492 Baz enumerator = enumerable.GetEnumerator () as Baz;
1493 Assert.IsNotNull (enumerator);
1494 Assert.AreEqual (false, enumerator.DisposeWasCalled);
1495 foreach (var element in enumerable) ; //sic
1496 Assert.AreEqual (true, enumerator.DisposeWasCalled);
1500 public void TwoEnumeratorsOfTwoDifferentListsAreDifferent ()
1502 var twoThree = new List<int> { 2, 3 };
1503 var oneTwo = new List<int> { 2, 4 };
1504 Assert.IsFalse (oneTwo.GetEnumerator ().Equals (twoThree.GetEnumerator ()));
1508 public void TwoEnumeratorsOfTwoDifferentListsWithSameElementsAreDifferent ()
1510 var twoThree = new List<int> { 2, 3 };
1511 var anotherTwoThree = new List<int> { 2, 3 };
1512 Assert.IsFalse(twoThree.GetEnumerator ().Equals (anotherTwoThree.GetEnumerator ()));
1516 public void EnumeratorIsSameInSameListAfterSubsequentCalls ()
1518 var enumerable = new List<Bar> ();
1519 var enumerator = enumerable.GetEnumerator ();
1520 var enumerator2 = enumerable.GetEnumerator ();
1522 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1524 Assert.IsTrue (enumerator2.Equals (enumerator));
1528 [Test] // was bug in Mono 2.10.9
1529 public void EnumeratorIsStillSameInSubsequentCallsEvenHavingADisposalInBetween ()
1531 var enumerable = new List<Bar> ();
1532 var enumerator = enumerable.GetEnumerator ();
1533 enumerator.Dispose ();
1534 var enumerator2 = enumerable.GetEnumerator ();
1536 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1538 Assert.IsTrue (enumerator2.Equals (enumerator));
1542 public void EnumeratorIsObviouslyDifferentAfterListChanges ()
1544 var enumerable = new List<Bar> ();
1545 var enumerator = enumerable.GetEnumerator ();
1546 enumerable.Add (new Bar ());
1547 var enumerator2 = enumerable.GetEnumerator ();
1549 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1551 Assert.IsFalse (enumerator2.Equals (enumerator));
1554 [Test] // was bug in Mono 2.10.9
1555 public void DotNetDoesntThrowObjectDisposedExceptionAfterSubsequentDisposes()
1557 var enumerable = new List<Bar> ();
1558 var enumerator = enumerable.GetEnumerator ();
1559 Assert.AreEqual (false, enumerator.MoveNext ());
1560 enumerator.Dispose();
1561 Assert.AreEqual (false, enumerator.MoveNext ());