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 FindAllTest ()
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);
484 [Test, ExpectedException (typeof (ArgumentNullException))]
485 public void FindAllNullTest ()
487 List <int> findings = _list1.FindAll (null);
491 public void FindIndexTest ()
493 int i = _list1.FindIndex (FindMultipleOfThree);
494 Assert.AreEqual (7, i);
496 i = _list1.FindIndex (FindMultipleOfTwelve);
497 Assert.AreEqual (-1, i);
500 [Test, ExpectedException (typeof (ArgumentNullException))]
501 public void FindIndexNullTest ()
503 int i = _list1.FindIndex (null);
507 public void FindLastTest ()
509 int i = _list1.FindLast (FindMultipleOfFour);
510 Assert.AreEqual (40, i);
512 i = _list1.FindLast (FindMultipleOfTwelve);
513 Assert.AreEqual (default (int), i);
516 [Test, ExpectedException (typeof (ArgumentNullException))]
517 public void FindLastNullTest ()
519 int i = _list1.FindLast (null);
522 // FIXME currently generates Invalid IL Code error
525 public void ForEachTest ()
528 _list1.ForEach (delegate (int j) { i += j; });
530 Assert.AreEqual (418, i);
534 public void FindLastIndexTest ()
536 int i = _list1.FindLastIndex (FindMultipleOfFour);
537 Assert.AreEqual (6, i);
539 i = _list1.FindLastIndex (5, FindMultipleOfFour);
540 Assert.AreEqual (5, i);
542 i = _list1.FindIndex (FindMultipleOfTwelve);
543 Assert.AreEqual (-1, i);
546 [Test, ExpectedException (typeof (ArgumentNullException))]
547 public void FindLastIndexNullTest ()
549 int i = _list1.FindLastIndex (null);
553 public void RemoveTest ()
555 int count = _list1.Count;
556 bool result = _list1.Remove (22);
557 Assert.IsTrue (result);
558 Assert.AreEqual (count - 1, _list1.Count);
560 Assert.AreEqual (-1, _list1.IndexOf (22));
562 result = _list1.Remove (0);
563 Assert.IsFalse (result);
567 public void RemoveAllTest ()
569 int count = _list1.Count;
570 int removedCount = _list1.RemoveAll (FindMultipleOfFour);
571 Assert.AreEqual (4, removedCount);
572 Assert.AreEqual (count - 4, _list1.Count);
574 removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
575 Assert.AreEqual (0, removedCount);
576 Assert.AreEqual (count - 4, _list1.Count);
580 public void RemoveAtTest ()
582 int count = _list1.Count;
584 Assert.AreEqual (count - 1, _list1.Count);
585 Assert.AreEqual (50, _list1 [0]);
588 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
589 public void RemoveOutOfRangeTest ()
591 _list1.RemoveAt (_list1.Count);
595 public void RemoveRangeTest ()
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]);
603 _list1.RemoveRange (0, 0);
604 Assert.AreEqual (count - 2, _list1.Count);
608 public void RemoveRangeFromEmptyListTest ()
610 List<int> l = new List<int> ();
611 l.RemoveRange (0, 0);
614 [Test, ExpectedException (typeof (ArgumentException))]
615 public void RemoveRangeOutOfRangeTest ()
617 _list1.RemoveRange (1, _list1.Count);
621 public void ReverseTest ()
623 int count = _list1.Count;
625 Assert.AreEqual (count, _list1.Count);
627 Assert.AreEqual (63, _list1 [0]);
628 Assert.AreEqual (55, _list1 [count - 1]);
630 _list1.Reverse (0, 2);
632 Assert.AreEqual (40, _list1 [0]);
633 Assert.AreEqual (63, _list1 [1]);
636 [Test, ExpectedException (typeof (ArgumentException))]
637 public void ReverseOutOfRangeTest ()
639 _list1.Reverse (1, _list1.Count);
643 public void ToArrayTest ()
645 int [] copiedContents = _list1.ToArray ();
646 Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
648 Assert.AreEqual (_list1.Count, copiedContents.Length);
649 Assert.AreEqual (_list1 [0], copiedContents [0]);
653 public void TrimExcessTest ()
655 List <string> l = new List <string> ();
658 Assert.IsTrue (l.Count < l.Capacity);
660 Assert.AreEqual (l.Count, l.Capacity);
663 bool IsPositive (int i)
669 public void TrueForAllTest ()
671 Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
672 Assert.IsTrue (_list1.TrueForAll (IsPositive));
675 [Test, ExpectedException (typeof (ArgumentNullException))]
676 public void TrueForAllNullTest ()
678 _list1.TrueForAll (null);
681 [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
682 public void CapacityOutOfRangeTest ()
684 _list1.Capacity = _list1.Count - 1;
688 public void BinarySearch_EmptyList ()
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");
698 public void BinarySearch2_EmptyList ()
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");
708 public void AddRange_Bug77019 ()
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");
717 public void VersionCheck_Add ()
719 List<int> list = new List<int> ();
720 IEnumerator enumerator = list.GetEnumerator ();
724 enumerator.MoveNext ();
726 } catch (InvalidOperationException) {
732 } catch (InvalidOperationException) {
735 enumerator = list.GetEnumerator ();
736 enumerator.MoveNext ();
740 public void VersionCheck_AddRange ()
742 List<int> list = new List<int> ();
743 IEnumerator enumerator = list.GetEnumerator ();
744 list.AddRange (new int [] { 5, 7 });
747 enumerator.MoveNext ();
749 } catch (InvalidOperationException) {
755 } catch (InvalidOperationException) {
758 enumerator = list.GetEnumerator ();
759 enumerator.MoveNext ();
763 public void VersionCheck_Clear ()
765 List<int> list = new List<int> ();
766 IEnumerator enumerator = list.GetEnumerator ();
770 enumerator.MoveNext ();
772 } catch (InvalidOperationException) {
778 } catch (InvalidOperationException) {
781 enumerator = list.GetEnumerator ();
782 enumerator.MoveNext ();
786 public void VersionCheck_Insert ()
788 List<int> list = new List<int> ();
789 IEnumerator enumerator = list.GetEnumerator ();
793 enumerator.MoveNext ();
795 } catch (InvalidOperationException) {
801 } catch (InvalidOperationException) {
804 enumerator = list.GetEnumerator ();
805 enumerator.MoveNext ();
809 public void VersionCheck_InsertRange ()
811 List<int> list = new List<int> ();
812 IEnumerator enumerator = list.GetEnumerator ();
813 list.InsertRange (0, new int [] { 5, 7 });
816 enumerator.MoveNext ();
818 } catch (InvalidOperationException) {
824 } catch (InvalidOperationException) {
827 enumerator = list.GetEnumerator ();
828 enumerator.MoveNext ();
832 public void VersionCheck_Remove ()
834 List<int> list = new List<int> ();
836 IEnumerator enumerator = list.GetEnumerator ();
837 // version number is not incremented if item does not exist in list
839 enumerator.MoveNext ();
843 enumerator.MoveNext ();
845 } catch (InvalidOperationException) {
851 } catch (InvalidOperationException) {
854 enumerator = list.GetEnumerator ();
855 enumerator.MoveNext ();
859 public void VersionCheck_RemoveAll ()
861 List<int> list = new List<int> ();
863 IEnumerator enumerator = list.GetEnumerator ();
864 // version is not incremented if there are no items to remove
865 list.RemoveAll (FindMultipleOfFour);
866 enumerator.MoveNext ();
869 enumerator = list.GetEnumerator ();
870 list.RemoveAll (FindMultipleOfFour);
873 enumerator.MoveNext ();
875 } catch (InvalidOperationException) {
881 } catch (InvalidOperationException) {
884 enumerator = list.GetEnumerator ();
885 enumerator.MoveNext ();
889 public void VersionCheck_RemoveAt ()
891 List<int> list = new List<int> ();
893 IEnumerator enumerator = list.GetEnumerator ();
897 enumerator.MoveNext ();
899 } catch (InvalidOperationException) {
905 } catch (InvalidOperationException) {
908 enumerator = list.GetEnumerator ();
909 enumerator.MoveNext ();
913 public void VersionCheck_RemoveRange ()
915 List<int> list = new List<int> ();
917 IEnumerator enumerator = list.GetEnumerator ();
918 // version is not incremented if count is zero
919 list.RemoveRange (0, 0);
920 enumerator.MoveNext ();
922 list.RemoveRange (0, 1);
925 enumerator.MoveNext ();
927 } catch (InvalidOperationException) {
933 } catch (InvalidOperationException) {
936 enumerator = list.GetEnumerator ();
937 enumerator.MoveNext ();
941 public void VersionCheck_Reverse ()
943 List<int> list = new List<int> ();
944 IEnumerator enumerator = list.GetEnumerator ();
948 enumerator.MoveNext ();
950 } catch (InvalidOperationException) {
956 } catch (InvalidOperationException) {
959 enumerator = list.GetEnumerator ();
963 enumerator.MoveNext ();
965 } catch (InvalidOperationException) {
971 } catch (InvalidOperationException) {
974 enumerator = list.GetEnumerator ();
975 enumerator.MoveNext ();
978 class SortTestComparer: IComparer<string> {
980 public int Compare (string s1, string s2)
982 return String.Compare (s1, s2);
987 public void Sort_Bug76361 ()
989 SortTestComparer comparer = new SortTestComparer ();
990 List<string> l = new List<string> ();
994 Assert.AreEqual ("bar", l[0], "0");
995 Assert.AreEqual ("foo", l[1], "1");
996 Assert.AreEqual (2, l.Count, "Count");
999 // for bug #77039 test case
1000 class GenericIComparable: IComparable<GenericIComparable> {
1001 private int _NumberToSortOn;
1003 public int NumberToSortOn {
1004 get { return _NumberToSortOn; }
1005 set { _NumberToSortOn = value; }
1008 public GenericIComparable (int val)
1010 _NumberToSortOn = val;
1013 public int CompareTo (GenericIComparable other)
1015 return NumberToSortOn.CompareTo (other.NumberToSortOn);
1020 public void Sort_GenericIComparable_Bug77039 ()
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));
1027 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1028 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1029 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1032 class NonGenericIComparable: IComparable {
1033 private int _NumberToSortOn;
1035 public int NumberToSortOn {
1036 get { return _NumberToSortOn; }
1037 set { _NumberToSortOn = value; }
1040 public NonGenericIComparable (int val)
1042 _NumberToSortOn = val;
1045 public int CompareTo (object obj)
1047 return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
1052 public void Sort_NonGenericIComparable ()
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));
1059 Assert.AreEqual (1, l[0].NumberToSortOn, "0");
1060 Assert.AreEqual (2, l[1].NumberToSortOn, "1");
1061 Assert.AreEqual (3, l[2].NumberToSortOn, "2");
1064 class NonComparable {
1068 public void Sort_GenericNonIComparable ()
1070 List<NonComparable> l = new List<NonComparable> ();
1072 // no element -> no sort -> no exception
1073 l.Add (new NonComparable ());
1075 // one element -> no sort -> no exception
1079 [ExpectedException (typeof (InvalidOperationException))]
1080 public void Sort_GenericNonIComparable_2 ()
1082 List<NonComparable> l = new List<NonComparable> ();
1083 l.Add (new NonComparable ());
1084 l.Add (new NonComparable ());
1086 // two element -> sort -> exception!
1090 void IList_Contains_InvalidType()
1092 List<string> list = new List<string>();
1094 Assert.IsFalse (((IList)list).Contains(new object()));
1098 void IList_IndexOf_InvalidType()
1100 List<string> list = new List<string>();
1102 Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
1105 // for bug #77277 test case
1107 public void Test_ContainsAndIndexOf_EquatableItem ()
1109 List<EquatableClass> list = new List<EquatableClass> ();
1110 EquatableClass item0 = new EquatableClass (0);
1111 EquatableClass item1 = new EquatableClass (1);
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");
1126 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1127 public void SetItem_OutOfRange()
1129 List<string> list = new List<string>();
1134 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1135 public void SetItem_IList_OutOfRange()
1137 IList<string> list = new List<string>();
1141 public class EquatableClass : IEquatable<EquatableClass>
1144 public EquatableClass (int x)
1149 public bool Equals (EquatableClass other)
1151 return this._x == other._x;