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.
36 using System.Collections;
37 using System.Collections.Generic;
38 using System.Collections.ObjectModel;
40 using System.Runtime.Serialization.Formatters.Binary;
43 using NUnit.Framework;
45 namespace MonoTests.System.Collections.Generic {
47 class GenericComparer<T> : IComparer<T> {
49 private bool called = false;
59 public int Compare (T x, T y)
69 static byte [] _serializedList = new byte [] {
70 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
72 0x7e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
73 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65,
74 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x60,
75 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
76 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72,
77 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
78 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20,
79 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75,
80 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69,
81 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62,
82 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34,
83 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00, 0x00, 0x00, 0x06,
84 0x5f, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a,
85 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x07,
86 0x00, 0x00, 0x08, 0x08, 0x08, 0x09, 0x02, 0x00, 0x00, 0x00, 0x03,
87 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x02, 0x00, 0x00,
88 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 int [] _list1_contents;
97 // FIXME arrays currently do not support generic collection
99 _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
100 // _list1 = new List <int> (_list1_contents);
102 _list1 = new List <int> (8);
103 foreach (int i in _list1_contents)
107 [Test] // This was for bug #74980
108 public void InsertTest ()
110 List <string> test = new List <string> ();
111 test.Insert (0, "a");
112 test.Insert (0, "b");
113 test.Insert (1, "c");
115 Assert.AreEqual (3, test.Count);
116 Assert.AreEqual ("b", test [0]);
117 Assert.AreEqual ("c", test [1]);
118 Assert.AreEqual ("a", test [2]);
122 public void InsertRangeTest ()
124 int count = _list1.Count;
125 // FIXME arrays currently do not support generic collection
127 int [] items = {1, 2, 3};
128 // List <int> newRange = new List <int> (items);
129 List <int> newRange = new List <int> (3);
130 foreach (int i in items)
132 _list1.InsertRange (1, newRange);
133 Assert.AreEqual (count + 3, _list1.Count);
134 Assert.AreEqual (55, _list1 [0]);
135 Assert.AreEqual (1, _list1 [1]);
136 Assert.AreEqual (2, _list1 [2]);
137 Assert.AreEqual (3, _list1 [3]);
138 Assert.AreEqual (50, _list1 [4]);
140 newRange = new List <int> ();
141 List <int> li = new List <int> ();
143 newRange.InsertRange (0, li);
144 newRange.InsertRange (newRange.Count, li);
145 Assert.AreEqual (2, newRange.Count);
149 public void InsertSelfTest()
151 List <int> range = new List <int> (5);
152 for (int i = 0; i < 5; ++ i)
155 range.InsertRange(2, range);
156 Assert.AreEqual (10, range.Count);
157 Assert.AreEqual (0, range [0]);
158 Assert.AreEqual (1, range [1]);
159 Assert.AreEqual (0, range [2]);
160 Assert.AreEqual (1, range [3]);
161 Assert.AreEqual (2, range [4]);
162 Assert.AreEqual (3, range [5]);
163 Assert.AreEqual (4, range [6]);
164 Assert.AreEqual (2, range [7]);
165 Assert.AreEqual (3, range [8]);
166 Assert.AreEqual (4, range [9]);
169 [Test, ExpectedException (typeof (ArgumentNullException))]
170 public void InsertRangeNullTest ()
172 IEnumerable <int> n = null;
173 _list1.InsertRange (0, n);
176 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
177 public void InsertRangeNegativeIndexTest ()
179 _list1.InsertRange (-1, _list1);
183 public void IndexOfTest ()
185 List <int> l = new List <int> ();
190 Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
193 [Test, ExpectedException(typeof (ArgumentException))]
194 public void IList_InsertInvalidType1 ()
196 IList list = _list1 as IList;
197 list.Insert(0, new object());
200 [Test, ExpectedException(typeof (ArgumentException))]
201 public void IList_InsertInvalidType2 ()
203 IList list = _list1 as IList;
204 list.Insert(0, null);
207 [Test, ExpectedException(typeof (ArgumentException))]
208 public void IList_AddInvalidType1()
210 IList list = _list1 as IList;
211 list.Add(new object());
214 [Test, ExpectedException(typeof (ArgumentException))]
215 public void IList_AddInvalidType2()
217 IList list = _list1 as IList;
222 public void IList_RemoveInvalidType()
224 IList list = _list1 as IList;
225 int nCount = list.Count;
226 list.Remove(new object());
227 Assert.AreEqual(nCount, list.Count);
230 Assert.AreEqual(nCount, list.Count);
233 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
234 public void IndexOfOutOfRangeTest ()
236 List <int> l = new List <int> (4);
241 public void GetRangeTest ()
243 List <int> r = _list1.GetRange (2, 4);
244 Assert.AreEqual (4, r.Count);
245 Assert.AreEqual (22, r [0]);
246 Assert.AreEqual (80, r [1]);
247 Assert.AreEqual (56, r [2]);
248 Assert.AreEqual (52, r [3]);
252 public void EnumeratorTest ()
254 List <int>.Enumerator e = _list1.GetEnumerator ();
255 for (int i = 0; i < _list1_contents.Length; i++)
257 Assert.IsTrue (e.MoveNext ());
258 Assert.AreEqual (_list1_contents [i], e.Current);
260 Assert.IsFalse (e.MoveNext ());
264 public void ConstructWithSizeTest ()
266 List <object> l_1 = new List <object> (1);
267 List <object> l_2 = new List <object> (50);
268 List <object> l_3 = new List <object> (0);
270 Assert.AreEqual (1, l_1.Capacity);
271 Assert.AreEqual (50, l_2.Capacity);
272 Assert.AreEqual (0, l_3.Capacity);
275 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
276 public void ConstructWithInvalidSizeTest ()
278 List <int> l = new List <int> (-1);
282 public void ConstructWithCollectionTest ()
284 List <int> l1 = new List <int> (_list1);
285 Assert.AreEqual (_list1.Count, l1.Count);
286 Assert.AreEqual (l1.Count, l1.Capacity);
287 for (int i = 0; i < l1.Count; i++)
288 Assert.AreEqual (_list1 [i], l1 [i]);
291 [Test, ExpectedException (typeof (ArgumentNullException))]
292 public void ConstructWithInvalidCollectionTest ()
295 List <int> l1 = new List <int> (n);
299 public void AddTest ()
301 int count = _list1.Count;
303 Assert.AreEqual (count + 1, _list1.Count);
304 Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
308 public void AddRangeTest ()
310 int count = _list1.Count;
311 // FIXME arrays currently do not support generic collection
313 int [] range = { -1, -2, -3 };
314 List <int> tmp = new List <int> (3);
315 foreach (int i in range)
317 // _list1.AddRange (range);
318 _list1.AddRange (tmp);
320 Assert.AreEqual (count + 3, _list1.Count);
321 Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
322 Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
323 Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
326 [Test, ExpectedException (typeof (ArgumentNullException))]
327 public void AddNullRangeTest ()
334 public void BinarySearchTest ()
336 List <int> l = new List <int> (_list1);
338 Assert.AreEqual (0, l.BinarySearch (22));
339 Assert.AreEqual (-2, l.BinarySearch (23));
340 Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
343 #if !NET_4_0 // FIXME: the blob contains the 2.0 mscorlib version
346 [Category ("TargetJvmNotWorking")]
347 public void SerializeTest ()
349 List <int> list = new List <int> ();
355 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
357 BinaryFormatter bf = new BinaryFormatter ();
359 MemoryStream ms = new MemoryStream ();
360 bf.Serialize (ms, list);
362 byte [] buffer = new byte [ms.Length];
364 ms.Read (buffer, 0, buffer.Length);
366 Assert.AreEqual (_serializedList, buffer);
372 [Category ("TargetJvmNotWorking")]
373 public void DeserializeTest ()
375 MemoryStream ms = new MemoryStream ();
376 ms.Write (_serializedList, 0, _serializedList.Length);
380 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
382 BinaryFormatter bf = new BinaryFormatter ();
384 List<int> list = (List<int>) bf.Deserialize (ms);
385 Assert.AreEqual (3, list.Count, "#1");
386 Assert.AreEqual (5, list [0], "#2");
387 Assert.AreEqual (0, list [1], "#3");
388 Assert.AreEqual (7, list [2], "#4");
392 public void SortTest ()
394 List <int> l = new List <int> (_list1);
396 Assert.AreEqual (_list1.Count, l.Count);
397 Assert.AreEqual (22, l [0]);
401 Assert.IsTrue (minimum <= i);
407 public void ClearTest ()
409 int capacity = _list1.Capacity;
411 Assert.AreEqual (0, _list1.Count);
412 Assert.AreEqual (capacity, _list1.Capacity);
416 public void ContainsTest ()
418 Assert.IsTrue (_list1.Contains (22));
419 Assert.IsFalse (_list1.Contains (23));
422 private string StringConvert (int i)
424 return i.ToString ();
428 public void ConvertAllTest ()
430 List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
431 Assert.AreEqual (_list1.Count, s.Count);
432 Assert.AreEqual ("55", s [0]);
436 public void CopyToTest ()
438 int [] a = new int [2];
439 _list1.CopyTo (1, a, 0, 2);
440 Assert.AreEqual (50, a [0]);
441 Assert.AreEqual (22, a [1]);
443 int [] b = new int [_list1.Count + 1];
444 b [_list1.Count] = 555;
446 Assert.AreEqual (55, b [0]);
447 Assert.AreEqual (555, b [_list1.Count]);
450 _list1.CopyTo (b, 1);
451 Assert.AreEqual (888, b [0]);
452 Assert.AreEqual (55, b [1]);
455 [Test, ExpectedException (typeof (ArgumentNullException))]
456 public void CopyToNullTest ()
459 _list1.CopyTo (0, a, 0, 0);
462 static bool FindMultipleOfThree (int i)
467 static bool FindMultipleOfFour (int i)
472 static bool FindMultipleOfTwelve (int i)
474 return (i % 12) == 0;
478 public void FindTest ()
480 int i = _list1.Find (FindMultipleOfThree);
481 Assert.AreEqual (63, i);
483 i = _list1.Find (FindMultipleOfTwelve);
484 Assert.AreEqual (default (int), i);
487 [Test, ExpectedException (typeof (ArgumentNullException))]
488 public void FindNullTest ()
490 int i = _list1.Find (null);
494 public void FindAllSmallTest ()
496 List <int> findings = _list1.FindAll (FindMultipleOfFour);
497 Assert.AreEqual (4, findings.Count);
498 Assert.AreEqual (80, findings [0]);
499 Assert.AreEqual (56, findings [1]);
500 Assert.AreEqual (52, findings [2]);
501 Assert.AreEqual (40, findings [3]);
503 findings = _list1.FindAll (FindMultipleOfTwelve);
504 Assert.IsNotNull (findings);
505 Assert.AreEqual (0, findings.Count);
509 public void FindAllMediumTest ()
511 List <int> integers = new List <int> (10000);
512 for (int i = 1; i <= 10000; i++)
515 List <int> results = integers.FindAll (FindMultipleOfFour);
517 Assert.IsNotNull (results);
518 Assert.AreEqual (2500, results.Count);
520 results = integers.FindAll (FindMultipleOfTwelve);
522 Assert.IsNotNull (results);
523 Assert.AreEqual (833, results.Count);
527 public void FindAllLargeTest ()
529 List <int> integers = new List <int> (70000);
530 for (int i = 1; i <= 80000; i++)
533 List <int> results = integers.FindAll (FindMultipleOfFour);
535 Assert.IsNotNull (results);
536 Assert.AreEqual (20000, results.Count);
538 results = integers.FindAll (FindMultipleOfTwelve);
540 Assert.IsNotNull (results);
541 Assert.AreEqual (6666, results.Count);
544 [Test, ExpectedException (typeof (ArgumentNullException))]
545 public void FindAllNullTest ()
547 List <int> findings = _list1.FindAll (null);
551 public void FindIndexTest ()
553 int i = _list1.FindIndex (FindMultipleOfThree);
554 Assert.AreEqual (7, i);
556 i = _list1.FindIndex (FindMultipleOfTwelve);
557 Assert.AreEqual (-1, i);
559 var a = new List<int> () { 2, 2, 2, 3, 2 };
560 Assert.AreEqual (2, a.FindIndex (2, 2, l => true));
564 public void FindIndex_Invalid ()
567 _list1.FindIndex (null);
569 } catch (ArgumentNullException) {
573 _list1.FindIndex (-1, l => true);
575 } catch (ArgumentOutOfRangeException) {
579 _list1.FindIndex (-1, 0, l => true);
581 } catch (ArgumentOutOfRangeException) {
585 _list1.FindIndex (0, -1, l => true);
587 } catch (ArgumentOutOfRangeException) {
591 _list1.FindIndex (100, l => true);
593 } catch (ArgumentOutOfRangeException) {
597 _list1.FindIndex (100, 0, l => true);
599 } catch (ArgumentOutOfRangeException) {
603 _list1.FindIndex (7, 2, l => true);
605 } catch (ArgumentOutOfRangeException) {
610 public void FindLastTest ()
612 int i = _list1.FindLast (FindMultipleOfFour);
613 Assert.AreEqual (40, i);
615 i = _list1.FindLast (FindMultipleOfTwelve);
616 Assert.AreEqual (default (int), i);
619 [Test, ExpectedException (typeof (ArgumentNullException))]
620 public void FindLastNullTest ()
622 int i = _list1.FindLast (null);
626 public void ForEachTest ()
629 _list1.ForEach (delegate (int j) { i += j; });
631 Assert.AreEqual (418, i);
635 public void FindLastIndexTest ()
637 int i = _list1.FindLastIndex (FindMultipleOfFour);
638 Assert.AreEqual (6, i);
640 i = _list1.FindLastIndex (5, FindMultipleOfFour);
641 Assert.AreEqual (5, i);
643 i = _list1.FindIndex (FindMultipleOfTwelve);
644 Assert.AreEqual (-1, i);
646 Assert.AreEqual (2, _list1.FindLastIndex (2, 3, l => true));
647 Assert.AreEqual (2, _list1.FindLastIndex (2, 2, l => true));
648 Assert.AreEqual (1, _list1.FindLastIndex (1, 2, l => true));
652 public void FindLastIndex_Invalid ()
655 _list1.FindLastIndex (null);
657 } catch (ArgumentNullException) {
661 _list1.FindLastIndex (-1, l => true);
663 } catch (ArgumentOutOfRangeException) {
667 _list1.FindLastIndex (-1, 0, l => true);
669 } catch (ArgumentOutOfRangeException) {
673 _list1.FindLastIndex (0, -1, l => true);
675 } catch (ArgumentOutOfRangeException) {
679 _list1.FindLastIndex (100, l => true);
681 } catch (ArgumentOutOfRangeException) {
685 _list1.FindLastIndex (100, 0, l => true);
687 } catch (ArgumentOutOfRangeException) {
691 _list1.FindLastIndex (2, 4, l => true);
693 } catch (ArgumentOutOfRangeException) {
698 public void RemoveTest ()
700 int count = _list1.Count;
701 bool result = _list1.Remove (22);
702 Assert.IsTrue (result);
703 Assert.AreEqual (count - 1, _list1.Count);
705 Assert.AreEqual (-1, _list1.IndexOf (22));
707 result = _list1.Remove (0);
708 Assert.IsFalse (result);
712 public void RemoveAllTest ()
714 int count = _list1.Count;
715 int removedCount = _list1.RemoveAll (FindMultipleOfFour);
716 Assert.AreEqual (4, removedCount);
717 Assert.AreEqual (count - 4, _list1.Count);
719 removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
720 Assert.AreEqual (0, removedCount);
721 Assert.AreEqual (count - 4, _list1.Count);
725 public void RemoveAtTest ()
727 int count = _list1.Count;
729 Assert.AreEqual (count - 1, _list1.Count);
730 Assert.AreEqual (50, _list1 [0]);
733 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
734 public void RemoveOutOfRangeTest ()
736 _list1.RemoveAt (_list1.Count);
740 public void RemoveRangeTest ()
742 int count = _list1.Count;
743 _list1.RemoveRange (1, 2);
744 Assert.AreEqual (count - 2, _list1.Count);
745 Assert.AreEqual (55, _list1 [0]);
746 Assert.AreEqual (80, _list1 [1]);
748 _list1.RemoveRange (0, 0);
749 Assert.AreEqual (count - 2, _list1.Count);
753 public void RemoveRangeFromEmptyListTest ()
755 List<int> l = new List<int> ();
756 l.RemoveRange (0, 0);
759 [Test, ExpectedException (typeof (ArgumentException))]
760 public void RemoveRangeOutOfRangeTest ()
762 _list1.RemoveRange (1, _list1.Count);
766 public void ReverseTest ()
768 int count = _list1.Count;
770 Assert.AreEqual (count, _list1.Count);
772 Assert.AreEqual (63, _list1 [0]);
773 Assert.AreEqual (55, _list1 [count - 1]);
775 _list1.Reverse (0, 2);
777 Assert.AreEqual (40, _list1 [0]);
778 Assert.AreEqual (63, _list1 [1]);
781 [Test, ExpectedException (typeof (ArgumentException))]
782 public void ReverseOutOfRangeTest ()
784 _list1.Reverse (1, _list1.Count);
788 public void ToArrayTest ()
790 int [] copiedContents = _list1.ToArray ();
791 Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
793 Assert.AreEqual (_list1.Count, copiedContents.Length);
794 Assert.AreEqual (_list1 [0], copiedContents [0]);
798 public void TrimExcessTest ()
800 List <string> l = new List <string> ();
803 Assert.IsTrue (l.Count < l.Capacity);
805 Assert.AreEqual (l.Count, l.Capacity);
808 bool IsPositive (int i)
814 public void TrueForAllTest ()
816 Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
817 Assert.IsTrue (_list1.TrueForAll (IsPositive));
820 [Test, ExpectedException (typeof (ArgumentNullException))]
821 public void TrueForAllNullTest ()
823 _list1.TrueForAll (null);
826 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
827 public void CapacityOutOfRangeTest ()
829 _list1.Capacity = _list1.Count - 1;
833 public void BinarySearch_EmptyList ()
835 GenericComparer<int> comparer = new GenericComparer<int> ();
836 List<int> l = new List<int> ();
837 Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
838 // bug 77030 - the comparer isn't called for an empty array/list
839 Assert.IsFalse (comparer.Called, "Called");
843 public void BinarySearch2_EmptyList ()
845 GenericComparer<int> comparer = new GenericComparer<int> ();
846 List<int> l = new List<int> ();
847 Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
848 // bug 77030 - the comparer isn't called for an empty array/list
849 Assert.IsFalse (comparer.Called, "Called");
853 public void AddRange_Bug77019 ()
855 List<int> l = new List<int> ();
856 Dictionary<string, int> d = new Dictionary<string, int> ();
857 l.AddRange (d.Values);
858 Assert.AreEqual (0, l.Count, "Count");
862 public void VersionCheck_Add ()
864 List<int> list = new List<int> ();
865 IEnumerator enumerator = list.GetEnumerator ();
869 enumerator.MoveNext ();
871 } catch (InvalidOperationException) {
877 } catch (InvalidOperationException) {
880 enumerator = list.GetEnumerator ();
881 enumerator.MoveNext ();
885 public void VersionCheck_AddRange ()
887 List<int> list = new List<int> ();
888 IEnumerator enumerator = list.GetEnumerator ();
889 list.AddRange (new int [] { 5, 7 });
892 enumerator.MoveNext ();
894 } catch (InvalidOperationException) {
900 } catch (InvalidOperationException) {
903 enumerator = list.GetEnumerator ();
904 enumerator.MoveNext ();
908 public void VersionCheck_Clear ()
910 List<int> list = new List<int> ();
911 IEnumerator enumerator = list.GetEnumerator ();
915 enumerator.MoveNext ();
917 } catch (InvalidOperationException) {
923 } catch (InvalidOperationException) {
926 enumerator = list.GetEnumerator ();
927 enumerator.MoveNext ();
931 public void VersionCheck_Insert ()
933 List<int> list = new List<int> ();
934 IEnumerator enumerator = list.GetEnumerator ();
938 enumerator.MoveNext ();
940 } catch (InvalidOperationException) {
946 } catch (InvalidOperationException) {
949 enumerator = list.GetEnumerator ();
950 enumerator.MoveNext ();
954 public void VersionCheck_InsertRange ()
956 List<int> list = new List<int> ();
957 IEnumerator enumerator = list.GetEnumerator ();
958 list.InsertRange (0, new int [] { 5, 7 });
961 enumerator.MoveNext ();
963 } catch (InvalidOperationException) {
969 } catch (InvalidOperationException) {
972 enumerator = list.GetEnumerator ();
973 enumerator.MoveNext ();
977 public void VersionCheck_Remove ()
979 List<int> list = new List<int> ();
981 IEnumerator enumerator = list.GetEnumerator ();
982 // version number is not incremented if item does not exist in list
984 enumerator.MoveNext ();
988 enumerator.MoveNext ();
990 } catch (InvalidOperationException) {
996 } catch (InvalidOperationException) {
999 enumerator = list.GetEnumerator ();
1000 enumerator.MoveNext ();
1004 public void VersionCheck_RemoveAll ()
1006 List<int> list = new List<int> ();
1008 IEnumerator enumerator = list.GetEnumerator ();
1009 // version is not incremented if there are no items to remove
1010 list.RemoveAll (FindMultipleOfFour);
1011 enumerator.MoveNext ();
1014 enumerator = list.GetEnumerator ();
1015 list.RemoveAll (FindMultipleOfFour);
1018 enumerator.MoveNext ();
1020 } catch (InvalidOperationException) {
1024 enumerator.Reset ();
1026 } catch (InvalidOperationException) {
1029 enumerator = list.GetEnumerator ();
1030 enumerator.MoveNext ();
1034 public void VersionCheck_RemoveAt ()
1036 List<int> list = new List<int> ();
1038 IEnumerator enumerator = list.GetEnumerator ();
1042 enumerator.MoveNext ();
1044 } catch (InvalidOperationException) {
1048 enumerator.Reset ();
1050 } catch (InvalidOperationException) {
1053 enumerator = list.GetEnumerator ();
1054 enumerator.MoveNext ();
1058 public void VersionCheck_RemoveRange ()
1060 List<int> list = new List<int> ();
1062 IEnumerator enumerator = list.GetEnumerator ();
1063 // version is not incremented if count is zero
1064 list.RemoveRange (0, 0);
1065 enumerator.MoveNext ();
1066 enumerator.Reset ();
1067 list.RemoveRange (0, 1);
1070 enumerator.MoveNext ();
1072 } catch (InvalidOperationException) {
1076 enumerator.Reset ();
1078 } catch (InvalidOperationException) {
1081 enumerator = list.GetEnumerator ();
1082 enumerator.MoveNext ();
1085 [Test, ExpectedException (typeof (InvalidOperationException))] // #699182
1086 public void VersionCheck_Indexer ()
1088 var list = new List<int> () { 0, 2, 3 };
1089 var enumerator = list.GetEnumerator ();
1093 enumerator.MoveNext ();
1097 public void VersionCheck_Reverse ()
1099 List<int> list = new List<int> ();
1100 IEnumerator enumerator = list.GetEnumerator ();
1104 enumerator.MoveNext ();
1105 Assert.Fail ("#A1");
1106 } catch (InvalidOperationException) {
1110 enumerator.Reset ();
1111 Assert.Fail ("#A2");
1112 } catch (InvalidOperationException) {
1115 enumerator = list.GetEnumerator ();
1116 list.Reverse (0, 0);
1119 enumerator.MoveNext ();
1120 Assert.Fail ("#B1");
1121 } catch (InvalidOperationException) {
1125 enumerator.Reset ();
1126 Assert.Fail ("#B2");
1127 } catch (InvalidOperationException) {
1130 enumerator = list.GetEnumerator ();
1131 enumerator.MoveNext ();
1134 class SortTestComparer: IComparer<string> {
1136 public int Compare (string s1, string s2)
1138 return String.Compare (s1, s2);
1143 public void Sort_Bug76361 ()
1145 SortTestComparer comparer = new SortTestComparer ();
1146 List<string> l = new List<string> ();
1150 Assert.AreEqual ("bar", l[0], "0");
1151 Assert.AreEqual ("foo", l[1], "1");
1152 Assert.AreEqual (2, l.Count, "Count");
1155 // for bug #77039 test case
1156 class GenericIComparable: IComparable<GenericIComparable> {
1157 private int _NumberToSortOn;
1159 public int NumberToSortOn {
1160 get { return _NumberToSortOn; }
1161 set { _NumberToSortOn = value; }
1164 public GenericIComparable (int val)
1166 _NumberToSortOn = val;
1169 public int CompareTo (GenericIComparable other)
1171 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1176 public void Sort_GenericIComparable_Bug77039 ()
1178 List<GenericIComparable> l = new List<GenericIComparable> ();
1179 l.Add (new GenericIComparable (2));
1180 l.Add (new GenericIComparable (1));
1181 l.Add (new GenericIComparable (3));
1183 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1184 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1185 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1188 class NonGenericIComparable: IComparable {
1189 private int _NumberToSortOn;
1191 public int NumberToSortOn {
1192 get { return _NumberToSortOn; }
1193 set { _NumberToSortOn = value; }
1196 public NonGenericIComparable (int val)
1198 _NumberToSortOn = val;
1201 public int CompareTo (object obj)
1203 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1208 public void Sort_NonGenericIComparable ()
1210 List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1211 l.Add (new NonGenericIComparable (2));
1212 l.Add (new NonGenericIComparable (1));
1213 l.Add (new NonGenericIComparable (3));
1215 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1216 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1217 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1220 class NonComparable {
1224 public void Sort_GenericNonIComparable ()
1226 List<NonComparable> l = new List<NonComparable> ();
1228 // no element -> no sort -> no exception
1229 l.Add (new NonComparable ());
1231 // one element -> no sort -> no exception
1235 [ExpectedException (typeof (InvalidOperationException))]
1236 public void Sort_GenericNonIComparable_2 ()
1238 List<NonComparable> l = new List<NonComparable> ();
1239 l.Add (new NonComparable ());
1240 l.Add (new NonComparable ());
1242 // two element -> sort -> exception!
1246 public void IList_Contains_InvalidType()
1248 List<string> list = new List<string>();
1250 Assert.IsFalse (((IList)list).Contains(new object()));
1252 Assert.IsFalse (((IList)_list1).Contains(null));
1256 public void IList_IndexOf_InvalidType()
1258 List<string> list = new List<string>();
1260 Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1262 Assert.AreEqual (-1, ((IList)_list1).IndexOf(null));
1265 // for bug #77277 test case
1267 public void Test_ContainsAndIndexOf_EquatableItem ()
1269 List<EquatableClass> list = new List<EquatableClass> ();
1270 EquatableClass item0 = new EquatableClass (0);
1271 EquatableClass item1 = new EquatableClass (1);
1277 Assert.AreEqual (true, list.Contains (item0), "#0");
1278 Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1279 Assert.AreEqual (0, list.IndexOf (item0), "#2");
1280 Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1281 Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1282 Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1285 // for bug #81387 test case
1287 public void Test_Contains_After_Remove ()
1289 List<int> list = new List<int> ();
1294 Assert.AreEqual (false, list.Contains (2), "#0");
1298 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1299 public void SetItem_OutOfRange()
1301 List<string> list = new List<string>();
1306 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1307 public void SetItem_IList_OutOfRange()
1309 IList<string> list = new List<string>();
1313 public class EquatableClass : IEquatable<EquatableClass>
1316 public EquatableClass (int x)
1321 public bool Equals (EquatableClass other)
1323 return this._x == other._x;
1339 // based on #491858, #517415
1340 public void Enumerator_Current ()
1342 var e1 = new List<int>.Enumerator ();
1343 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1345 var d = new List<int> ();
1346 var e2 = d.GetEnumerator ();
1347 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1349 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1351 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1353 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1354 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1356 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1358 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1360 var e4 = ((IEnumerable) d).GetEnumerator ();
1361 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1363 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1364 ((IDisposable) e4).Dispose ();
1365 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1368 [Test] //bug #672907
1369 public void ICollectionCopyToExceptions ()
1371 var l = new List <int> ();
1376 } catch (Exception e) {
1377 Assert.IsTrue (e is ArgumentNullException, "#2");
1381 x.CopyTo (new int [10], -1);
1383 } catch (Exception e) {
1384 Assert.IsTrue (e is ArgumentOutOfRangeException, "#4");
1388 x.CopyTo (new int [10, 1], 0);
1390 } catch (Exception e) {
1391 Assert.IsTrue (e is ArgumentException, "#6");
1395 x.CopyTo (Array.CreateInstance (typeof (int), new int [] { 10 }, new int[] { 1 }), 0);
1397 } catch (Exception e) {
1398 Assert.IsTrue (e is ArgumentException, "#8");
1401 l.Add (10); l.Add (20);
1403 x.CopyTo (new int [1], 0);
1405 } catch (Exception e) {
1406 Assert.IsTrue (e is ArgumentException, "#10");
1411 public void LastIndexOfEmpty_2558 () {
1412 var l = new List<int> ();
1413 Assert.AreEqual (-1, l.IndexOf (-1));
1417 #region Enumerator mutability
1423 class Foo : IEnumerable<Bar>
1429 enumerator = new Baz ();
1432 public IEnumerator<Bar> GetEnumerator ()
1437 IEnumerator IEnumerable.GetEnumerator ()
1443 class Baz : IEnumerator<Bar>
1445 public bool DisposeWasCalled = false;
1447 public void Dispose ()
1449 DisposeWasCalled = true;
1452 public bool MoveNext ()
1454 return false; //assume empty collection
1457 public void Reset ()
1463 get { return null; }
1466 object IEnumerator.Current
1468 get { return Current; }
1473 public void PremiseAboutDisposeBeingCalledWhenLooping ()
1475 Foo enumerable = new Foo ();
1476 Baz enumerator = enumerable.GetEnumerator () as Baz;
1477 Assert.IsNotNull (enumerator);
1478 Assert.AreEqual (false, enumerator.DisposeWasCalled);
1479 foreach (var element in enumerable) ; //sic
1480 Assert.AreEqual (true, enumerator.DisposeWasCalled);
1484 public void TwoEnumeratorsOfTwoDifferentListsAreDifferent ()
1486 var twoThree = new List<int> { 2, 3 };
1487 var oneTwo = new List<int> { 2, 4 };
1488 Assert.IsFalse (oneTwo.GetEnumerator ().Equals (twoThree.GetEnumerator ()));
1492 public void TwoEnumeratorsOfTwoDifferentListsWithSameElementsAreDifferent ()
1494 var twoThree = new List<int> { 2, 3 };
1495 var anotherTwoThree = new List<int> { 2, 3 };
1496 Assert.IsFalse(twoThree.GetEnumerator ().Equals (anotherTwoThree.GetEnumerator ()));
1500 public void EnumeratorIsSameInSameListAfterSubsequentCalls ()
1502 var enumerable = new List<Bar> ();
1503 var enumerator = enumerable.GetEnumerator ();
1504 var enumerator2 = enumerable.GetEnumerator ();
1506 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1508 Assert.IsTrue (enumerator2.Equals (enumerator));
1512 [Test] // was bug in Mono 2.10.9
1513 public void EnumeratorIsStillSameInSubsequentCallsEvenHavingADisposalInBetween ()
1515 var enumerable = new List<Bar> ();
1516 var enumerator = enumerable.GetEnumerator ();
1517 enumerator.Dispose ();
1518 var enumerator2 = enumerable.GetEnumerator ();
1520 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1522 Assert.IsTrue (enumerator2.Equals (enumerator));
1526 public void EnumeratorIsObviouslyDifferentAfterListChanges ()
1528 var enumerable = new List<Bar> ();
1529 var enumerator = enumerable.GetEnumerator ();
1530 enumerable.Add (new Bar ());
1531 var enumerator2 = enumerable.GetEnumerator ();
1533 Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
1535 Assert.IsFalse (enumerator2.Equals (enumerator));
1538 [Test] // was bug in Mono 2.10.9
1539 public void DotNetDoesntThrowObjectDisposedExceptionAfterSubsequentDisposes()
1541 var enumerable = new List<Bar> ();
1542 var enumerator = enumerable.GetEnumerator ();
1543 Assert.AreEqual (false, enumerator.MoveNext ());
1544 enumerator.Dispose();
1545 Assert.AreEqual (false, enumerator.MoveNext ());