2 // MonoTests.System.Collections.Generic.Test.DictionaryTest
5 // David Waite (mass@akuma.org)
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 // Copyright (C) 2005 David Waite (mass@akuma.org)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Collections.ObjectModel;
37 using System.Runtime.Serialization.Formatters.Binary;
40 using NUnit.Framework;
42 namespace MonoTests.System.Collections.Generic {
44 class GenericComparer<T> : IComparer<T> {
46 private bool called = false;
56 public int Compare (T x, T y)
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,
88 int [] _list1_contents;
94 // FIXME arrays currently do not support generic collection
96 _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
97 // _list1 = new List <int> (_list1_contents);
99 _list1 = new List <int> (8);
100 foreach (int i in _list1_contents)
104 [Test] // This was for bug #74980
105 public void InsertTest ()
107 List <string> test = new List <string> ();
108 test.Insert (0, "a");
109 test.Insert (0, "b");
110 test.Insert (1, "c");
112 Assert.AreEqual (3, test.Count);
113 Assert.AreEqual ("b", test [0]);
114 Assert.AreEqual ("c", test [1]);
115 Assert.AreEqual ("a", test [2]);
119 public void InsertRangeTest ()
121 int count = _list1.Count;
122 // FIXME arrays currently do not support generic collection
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)
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]);
137 newRange = new List <int> ();
138 List <int> li = new List <int> ();
140 newRange.InsertRange (0, li);
141 newRange.InsertRange (newRange.Count, li);
142 Assert.AreEqual (2, newRange.Count);
146 public void InsertSelfTest()
148 List <int> range = new List <int> (5);
149 for (int i = 0; i < 5; ++ i)
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]);
166 [Test, ExpectedException (typeof (ArgumentNullException))]
167 public void InsertRangeNullTest ()
169 IEnumerable <int> n = null;
170 _list1.InsertRange (0, n);
173 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
174 public void InsertRangeNegativeIndexTest ()
176 _list1.InsertRange (-1, _list1);
180 public void IndexOfTest ()
182 List <int> l = new List <int> ();
187 Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
190 [Test, ExpectedException(typeof (ArgumentException))]
191 public void IList_InsertInvalidType ()
193 IList list = _list1 as IList;
194 list.Insert(0, new object());
197 [Test, ExpectedException(typeof (ArgumentException))]
198 public void IList_AddInvalidType()
200 IList list = _list1 as IList;
201 list.Add(new object());
205 public void IList_RemoveInvalidType()
207 IList list = _list1 as IList;
208 int nCount = list.Count;
209 list.Remove(new object());
210 Assert.AreEqual(nCount, list.Count);
213 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
214 public void IndexOfOutOfRangeTest ()
216 List <int> l = new List <int> (4);
221 public void GetRangeTest ()
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]);
232 public void EnumeratorTest ()
234 List <int>.Enumerator e = _list1.GetEnumerator ();
235 for (int i = 0; i < _list1_contents.Length; i++)
237 Assert.IsTrue (e.MoveNext ());
238 Assert.AreEqual (_list1_contents [i], e.Current);
240 Assert.IsFalse (e.MoveNext ());
244 public void ConstructWithSizeTest ()
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);
250 Assert.AreEqual (1, l_1.Capacity);
251 Assert.AreEqual (50, l_2.Capacity);
252 Assert.AreEqual (0, l_3.Capacity);
255 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
256 public void ConstructWithInvalidSizeTest ()
258 List <int> l = new List <int> (-1);
262 public void ConstructWithCollectionTest ()
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]);
271 [Test, ExpectedException (typeof (ArgumentNullException))]
272 public void ConstructWithInvalidCollectionTest ()
275 List <int> l1 = new List <int> (n);
279 public void AddTest ()
281 int count = _list1.Count;
283 Assert.AreEqual (count + 1, _list1.Count);
284 Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
288 public void AddRangeTest ()
290 int count = _list1.Count;
291 // FIXME arrays currently do not support generic collection
293 int [] range = { -1, -2, -3 };
294 List <int> tmp = new List <int> (3);
295 foreach (int i in range)
297 // _list1.AddRange (range);
298 _list1.AddRange (tmp);
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]);
306 [Test, ExpectedException (typeof (ArgumentNullException))]
307 public void AddNullRangeTest ()
314 public void BinarySearchTest ()
316 List <int> l = new List <int> (_list1);
318 Assert.AreEqual (0, l.BinarySearch (22));
319 Assert.AreEqual (-2, l.BinarySearch (23));
320 Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
324 [Category ("TargetJvmNotWorking")]
325 public void SerializeTest ()
327 List <int> list = new List <int> ();
333 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
335 BinaryFormatter bf = new BinaryFormatter ();
337 MemoryStream ms = new MemoryStream ();
338 bf.Serialize (ms, list);
340 byte [] buffer = new byte [ms.Length];
342 ms.Read (buffer, 0, buffer.Length);
344 Assert.AreEqual (_serializedList, buffer);
348 [Category ("TargetJvmNotWorking")]
349 public void DeserializeTest ()
351 MemoryStream ms = new MemoryStream ();
352 ms.Write (_serializedList, 0, _serializedList.Length);
356 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
358 BinaryFormatter bf = new BinaryFormatter ();
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");
368 public void SortTest ()
370 List <int> l = new List <int> (_list1);
372 Assert.AreEqual (_list1.Count, l.Count);
373 Assert.AreEqual (22, l [0]);
377 Assert.IsTrue (minimum <= i);
383 public void ClearTest ()
385 int capacity = _list1.Capacity;
387 Assert.AreEqual (0, _list1.Count);
388 Assert.AreEqual (capacity, _list1.Capacity);
392 public void ContainsTest ()
394 Assert.IsTrue (_list1.Contains (22));
395 Assert.IsFalse (_list1.Contains (23));
398 private string StringConvert (int i)
400 return i.ToString ();
404 public void ConvertAllTest ()
406 List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
407 Assert.AreEqual (_list1.Count, s.Count);
408 Assert.AreEqual ("55", s [0]);
412 public void CopyToTest ()
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]);
419 int [] b = new int [_list1.Count + 1];
420 b [_list1.Count] = 555;
422 Assert.AreEqual (55, b [0]);
423 Assert.AreEqual (555, b [_list1.Count]);
426 _list1.CopyTo (b, 1);
427 Assert.AreEqual (888, b [0]);
428 Assert.AreEqual (55, b [1]);
431 [Test, ExpectedException (typeof (ArgumentNullException))]
432 public void CopyToNullTest ()
435 _list1.CopyTo (0, a, 0, 0);
438 static bool FindMultipleOfThree (int i)
443 static bool FindMultipleOfFour (int i)
448 static bool FindMultipleOfTwelve (int i)
450 return (i % 12) == 0;
454 public void FindTest ()
456 int i = _list1.Find (FindMultipleOfThree);
457 Assert.AreEqual (63, i);
459 i = _list1.Find (FindMultipleOfTwelve);
460 Assert.AreEqual (default (int), i);
463 [Test, ExpectedException (typeof (ArgumentNullException))]
464 public void FindNullTest ()
466 int i = _list1.Find (null);
470 public void FindAllSmallTest ()
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]);
479 findings = _list1.FindAll (FindMultipleOfTwelve);
480 Assert.IsNotNull (findings);
481 Assert.AreEqual (0, findings.Count);
485 public void FindAllMediumTest ()
487 List <int> integers = new List <int> (10000);
488 for (int i = 1; i <= 10000; i++)
491 List <int> results = integers.FindAll (FindMultipleOfFour);
493 Assert.IsNotNull (results);
494 Assert.AreEqual (2500, results.Count);
496 results = integers.FindAll (FindMultipleOfTwelve);
498 Assert.IsNotNull (results);
499 Assert.AreEqual (833, results.Count);
503 public void FindAllLargeTest ()
505 List <int> integers = new List <int> (70000);
506 for (int i = 1; i <= 80000; i++)
509 List <int> results = integers.FindAll (FindMultipleOfFour);
511 Assert.IsNotNull (results);
512 Assert.AreEqual (20000, results.Count);
514 results = integers.FindAll (FindMultipleOfTwelve);
516 Assert.IsNotNull (results);
517 Assert.AreEqual (6666, results.Count);
520 [Test, ExpectedException (typeof (ArgumentNullException))]
521 public void FindAllNullTest ()
523 List <int> findings = _list1.FindAll (null);
527 public void FindIndexTest ()
529 int i = _list1.FindIndex (FindMultipleOfThree);
530 Assert.AreEqual (7, i);
532 i = _list1.FindIndex (FindMultipleOfTwelve);
533 Assert.AreEqual (-1, i);
536 [Test, ExpectedException (typeof (ArgumentNullException))]
537 public void FindIndexNullTest ()
539 int i = _list1.FindIndex (null);
543 public void FindLastTest ()
545 int i = _list1.FindLast (FindMultipleOfFour);
546 Assert.AreEqual (40, i);
548 i = _list1.FindLast (FindMultipleOfTwelve);
549 Assert.AreEqual (default (int), i);
552 [Test, ExpectedException (typeof (ArgumentNullException))]
553 public void FindLastNullTest ()
555 int i = _list1.FindLast (null);
558 // FIXME currently generates Invalid IL Code error
561 public void ForEachTest ()
564 _list1.ForEach (delegate (int j) { i += j; });
566 Assert.AreEqual (418, i);
570 public void FindLastIndexTest ()
572 int i = _list1.FindLastIndex (FindMultipleOfFour);
573 Assert.AreEqual (6, i);
575 i = _list1.FindLastIndex (5, FindMultipleOfFour);
576 Assert.AreEqual (5, i);
578 i = _list1.FindIndex (FindMultipleOfTwelve);
579 Assert.AreEqual (-1, i);
582 [Test, ExpectedException (typeof (ArgumentNullException))]
583 public void FindLastIndexNullTest ()
585 int i = _list1.FindLastIndex (null);
589 public void RemoveTest ()
591 int count = _list1.Count;
592 bool result = _list1.Remove (22);
593 Assert.IsTrue (result);
594 Assert.AreEqual (count - 1, _list1.Count);
596 Assert.AreEqual (-1, _list1.IndexOf (22));
598 result = _list1.Remove (0);
599 Assert.IsFalse (result);
603 public void RemoveAllTest ()
605 int count = _list1.Count;
606 int removedCount = _list1.RemoveAll (FindMultipleOfFour);
607 Assert.AreEqual (4, removedCount);
608 Assert.AreEqual (count - 4, _list1.Count);
610 removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
611 Assert.AreEqual (0, removedCount);
612 Assert.AreEqual (count - 4, _list1.Count);
616 public void RemoveAtTest ()
618 int count = _list1.Count;
620 Assert.AreEqual (count - 1, _list1.Count);
621 Assert.AreEqual (50, _list1 [0]);
624 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
625 public void RemoveOutOfRangeTest ()
627 _list1.RemoveAt (_list1.Count);
631 public void RemoveRangeTest ()
633 int count = _list1.Count;
634 _list1.RemoveRange (1, 2);
635 Assert.AreEqual (count - 2, _list1.Count);
636 Assert.AreEqual (55, _list1 [0]);
637 Assert.AreEqual (80, _list1 [1]);
639 _list1.RemoveRange (0, 0);
640 Assert.AreEqual (count - 2, _list1.Count);
644 public void RemoveRangeFromEmptyListTest ()
646 List<int> l = new List<int> ();
647 l.RemoveRange (0, 0);
650 [Test, ExpectedException (typeof (ArgumentException))]
651 public void RemoveRangeOutOfRangeTest ()
653 _list1.RemoveRange (1, _list1.Count);
657 public void ReverseTest ()
659 int count = _list1.Count;
661 Assert.AreEqual (count, _list1.Count);
663 Assert.AreEqual (63, _list1 [0]);
664 Assert.AreEqual (55, _list1 [count - 1]);
666 _list1.Reverse (0, 2);
668 Assert.AreEqual (40, _list1 [0]);
669 Assert.AreEqual (63, _list1 [1]);
672 [Test, ExpectedException (typeof (ArgumentException))]
673 public void ReverseOutOfRangeTest ()
675 _list1.Reverse (1, _list1.Count);
679 public void ToArrayTest ()
681 int [] copiedContents = _list1.ToArray ();
682 Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
684 Assert.AreEqual (_list1.Count, copiedContents.Length);
685 Assert.AreEqual (_list1 [0], copiedContents [0]);
689 public void TrimExcessTest ()
691 List <string> l = new List <string> ();
694 Assert.IsTrue (l.Count < l.Capacity);
696 Assert.AreEqual (l.Count, l.Capacity);
699 bool IsPositive (int i)
705 public void TrueForAllTest ()
707 Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
708 Assert.IsTrue (_list1.TrueForAll (IsPositive));
711 [Test, ExpectedException (typeof (ArgumentNullException))]
712 public void TrueForAllNullTest ()
714 _list1.TrueForAll (null);
717 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
718 public void CapacityOutOfRangeTest ()
720 _list1.Capacity = _list1.Count - 1;
724 public void BinarySearch_EmptyList ()
726 GenericComparer<int> comparer = new GenericComparer<int> ();
727 List<int> l = new List<int> ();
728 Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
729 // bug 77030 - the comparer isn't called for an empty array/list
730 Assert.IsFalse (comparer.Called, "Called");
734 public void BinarySearch2_EmptyList ()
736 GenericComparer<int> comparer = new GenericComparer<int> ();
737 List<int> l = new List<int> ();
738 Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
739 // bug 77030 - the comparer isn't called for an empty array/list
740 Assert.IsFalse (comparer.Called, "Called");
744 public void AddRange_Bug77019 ()
746 List<int> l = new List<int> ();
747 Dictionary<string, int> d = new Dictionary<string, int> ();
748 l.AddRange (d.Values);
749 Assert.AreEqual (0, l.Count, "Count");
753 public void VersionCheck_Add ()
755 List<int> list = new List<int> ();
756 IEnumerator enumerator = list.GetEnumerator ();
760 enumerator.MoveNext ();
762 } catch (InvalidOperationException) {
768 } catch (InvalidOperationException) {
771 enumerator = list.GetEnumerator ();
772 enumerator.MoveNext ();
776 public void VersionCheck_AddRange ()
778 List<int> list = new List<int> ();
779 IEnumerator enumerator = list.GetEnumerator ();
780 list.AddRange (new int [] { 5, 7 });
783 enumerator.MoveNext ();
785 } catch (InvalidOperationException) {
791 } catch (InvalidOperationException) {
794 enumerator = list.GetEnumerator ();
795 enumerator.MoveNext ();
799 public void VersionCheck_Clear ()
801 List<int> list = new List<int> ();
802 IEnumerator enumerator = list.GetEnumerator ();
806 enumerator.MoveNext ();
808 } catch (InvalidOperationException) {
814 } catch (InvalidOperationException) {
817 enumerator = list.GetEnumerator ();
818 enumerator.MoveNext ();
822 public void VersionCheck_Insert ()
824 List<int> list = new List<int> ();
825 IEnumerator enumerator = list.GetEnumerator ();
829 enumerator.MoveNext ();
831 } catch (InvalidOperationException) {
837 } catch (InvalidOperationException) {
840 enumerator = list.GetEnumerator ();
841 enumerator.MoveNext ();
845 public void VersionCheck_InsertRange ()
847 List<int> list = new List<int> ();
848 IEnumerator enumerator = list.GetEnumerator ();
849 list.InsertRange (0, new int [] { 5, 7 });
852 enumerator.MoveNext ();
854 } catch (InvalidOperationException) {
860 } catch (InvalidOperationException) {
863 enumerator = list.GetEnumerator ();
864 enumerator.MoveNext ();
868 public void VersionCheck_Remove ()
870 List<int> list = new List<int> ();
872 IEnumerator enumerator = list.GetEnumerator ();
873 // version number is not incremented if item does not exist in list
875 enumerator.MoveNext ();
879 enumerator.MoveNext ();
881 } catch (InvalidOperationException) {
887 } catch (InvalidOperationException) {
890 enumerator = list.GetEnumerator ();
891 enumerator.MoveNext ();
895 public void VersionCheck_RemoveAll ()
897 List<int> list = new List<int> ();
899 IEnumerator enumerator = list.GetEnumerator ();
900 // version is not incremented if there are no items to remove
901 list.RemoveAll (FindMultipleOfFour);
902 enumerator.MoveNext ();
905 enumerator = list.GetEnumerator ();
906 list.RemoveAll (FindMultipleOfFour);
909 enumerator.MoveNext ();
911 } catch (InvalidOperationException) {
917 } catch (InvalidOperationException) {
920 enumerator = list.GetEnumerator ();
921 enumerator.MoveNext ();
925 public void VersionCheck_RemoveAt ()
927 List<int> list = new List<int> ();
929 IEnumerator enumerator = list.GetEnumerator ();
933 enumerator.MoveNext ();
935 } catch (InvalidOperationException) {
941 } catch (InvalidOperationException) {
944 enumerator = list.GetEnumerator ();
945 enumerator.MoveNext ();
949 public void VersionCheck_RemoveRange ()
951 List<int> list = new List<int> ();
953 IEnumerator enumerator = list.GetEnumerator ();
954 // version is not incremented if count is zero
955 list.RemoveRange (0, 0);
956 enumerator.MoveNext ();
958 list.RemoveRange (0, 1);
961 enumerator.MoveNext ();
963 } catch (InvalidOperationException) {
969 } catch (InvalidOperationException) {
972 enumerator = list.GetEnumerator ();
973 enumerator.MoveNext ();
977 public void VersionCheck_Reverse ()
979 List<int> list = new List<int> ();
980 IEnumerator enumerator = list.GetEnumerator ();
984 enumerator.MoveNext ();
986 } catch (InvalidOperationException) {
992 } catch (InvalidOperationException) {
995 enumerator = list.GetEnumerator ();
999 enumerator.MoveNext ();
1000 Assert.Fail ("#B1");
1001 } catch (InvalidOperationException) {
1005 enumerator.Reset ();
1006 Assert.Fail ("#B2");
1007 } catch (InvalidOperationException) {
1010 enumerator = list.GetEnumerator ();
1011 enumerator.MoveNext ();
1014 class SortTestComparer: IComparer<string> {
1016 public int Compare (string s1, string s2)
1018 return String.Compare (s1, s2);
1023 public void Sort_Bug76361 ()
1025 SortTestComparer comparer = new SortTestComparer ();
1026 List<string> l = new List<string> ();
1030 Assert.AreEqual ("bar", l[0], "0");
1031 Assert.AreEqual ("foo", l[1], "1");
1032 Assert.AreEqual (2, l.Count, "Count");
1035 // for bug #77039 test case
1036 class GenericIComparable: IComparable<GenericIComparable> {
1037 private int _NumberToSortOn;
1039 public int NumberToSortOn {
1040 get { return _NumberToSortOn; }
1041 set { _NumberToSortOn = value; }
1044 public GenericIComparable (int val)
1046 _NumberToSortOn = val;
1049 public int CompareTo (GenericIComparable other)
1051 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1056 public void Sort_GenericIComparable_Bug77039 ()
1058 List<GenericIComparable> l = new List<GenericIComparable> ();
1059 l.Add (new GenericIComparable (2));
1060 l.Add (new GenericIComparable (1));
1061 l.Add (new GenericIComparable (3));
1063 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1064 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1065 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1068 class NonGenericIComparable: IComparable {
1069 private int _NumberToSortOn;
1071 public int NumberToSortOn {
1072 get { return _NumberToSortOn; }
1073 set { _NumberToSortOn = value; }
1076 public NonGenericIComparable (int val)
1078 _NumberToSortOn = val;
1081 public int CompareTo (object obj)
1083 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1088 public void Sort_NonGenericIComparable ()
1090 List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
1091 l.Add (new NonGenericIComparable (2));
1092 l.Add (new NonGenericIComparable (1));
1093 l.Add (new NonGenericIComparable (3));
1095 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1096 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1097 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1100 class NonComparable {
1104 public void Sort_GenericNonIComparable ()
1106 List<NonComparable> l = new List<NonComparable> ();
1108 // no element -> no sort -> no exception
1109 l.Add (new NonComparable ());
1111 // one element -> no sort -> no exception
1115 [ExpectedException (typeof (InvalidOperationException))]
1116 public void Sort_GenericNonIComparable_2 ()
1118 List<NonComparable> l = new List<NonComparable> ();
1119 l.Add (new NonComparable ());
1120 l.Add (new NonComparable ());
1122 // two element -> sort -> exception!
1126 void IList_Contains_InvalidType()
1128 List<string> list = new List<string>();
1130 Assert.IsFalse (((IList)list).Contains(new object()));
1134 void IList_IndexOf_InvalidType()
1136 List<string> list = new List<string>();
1138 Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1141 // for bug #77277 test case
1143 public void Test_ContainsAndIndexOf_EquatableItem ()
1145 List<EquatableClass> list = new List<EquatableClass> ();
1146 EquatableClass item0 = new EquatableClass (0);
1147 EquatableClass item1 = new EquatableClass (1);
1153 Assert.AreEqual (true, list.Contains (item0), "#0");
1154 Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
1155 Assert.AreEqual (0, list.IndexOf (item0), "#2");
1156 Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
1157 Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
1158 Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
1161 // for bug #81387 test case
1163 public void Test_Contains_After_Remove ()
1165 List<int> list = new List<int> ();
1170 Assert.AreEqual (true, list.Contains (2), "#0");
1174 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1175 public void SetItem_OutOfRange()
1177 List<string> list = new List<string>();
1182 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1183 public void SetItem_IList_OutOfRange()
1185 IList<string> list = new List<string>();
1189 public class EquatableClass : IEquatable<EquatableClass>
1192 public EquatableClass (int x)
1197 public bool Equals (EquatableClass other)
1199 return this._x == other._x;