1 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class
5 // Duncan Mak (duncan@ximian.com)
7 // Thanks go to David Brandt (bucky@keystreams.com),
8 // because this file is based on his ArrayListTest.cs
10 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 // main TODO: additional tests for functions affected by
14 // fixedsize and read-only properties
18 using System.Collections;
20 using System.Runtime.Serialization.Formatters;
21 using System.Runtime.Serialization.Formatters.Binary;
23 using NUnit.Framework;
25 namespace MonoTests.System.Collections
27 public class SortedListComparer : IComparer
29 public int Compare (object x, object y)
31 return x.GetHashCode () - y.GetHashCode ();
36 public class SortedListTest
38 protected SortedList sl1;
39 protected SortedList sl2;
40 protected SortedList emptysl;
41 protected const int icap = 16;
43 public void TestConstructor1 ()
45 SortedList temp1 = new SortedList ();
46 Assert.IsNotNull (temp1, "#1");
50 public void TestConstructor2 ()
52 Comparer c = Comparer.Default;
53 SortedList temp1 = new SortedList (c);
54 Assert.IsNotNull (temp1, "#1");
58 public void TestConstructor3 ()
60 Hashtable d = new Hashtable ();
61 d.Add ("one", "Mircosoft");
62 d.Add ("two", "will");
63 d.Add ("three", "rule");
64 d.Add ("four", "the world");
66 SortedList temp1 = new SortedList (d);
67 Assert.IsNotNull (temp1, "#A1");
68 Assert.AreEqual (4, temp1.Capacity, "#A2");
69 Assert.AreEqual (4, temp1.Count, "#A3");
72 new SortedList ((Hashtable) null);
74 } catch (ArgumentNullException) {
79 d.Add ("one", "Mircosoft");
80 d.Add ("two", "will");
81 d.Add ("three", "rule");
82 d.Add ("four", "the world");
86 } catch (InvalidOperationException) {
91 public void TestConstructor4 ()
93 SortedList temp1 = new SortedList (17);
94 Assert.IsNotNull (temp1, "#A1");
95 Assert.AreEqual (17, temp1.Capacity, "#A2");
100 } catch (ArgumentOutOfRangeException) {
103 temp1 = new SortedList (0);
104 Assert.IsNotNull (temp1, "#C");
108 public void TestConstructor5 ()
110 Comparer c = Comparer.Default;
111 SortedList temp1 = new SortedList (c, 27);
112 Assert.IsNotNull (temp1, "#A1");
113 Assert.AreEqual (27, temp1.Capacity, "#A2");
116 new SortedList (-12);
118 } catch (ArgumentOutOfRangeException) {
124 public void TestIsSynchronized ()
126 SortedList sl1 = new SortedList ();
127 Assert.IsFalse (sl1.IsSynchronized, "#1");
128 SortedList sl2 = SortedList.Synchronized (sl1);
129 Assert.IsTrue (sl2.IsSynchronized, "#2");
133 public void TestCapacity ()
135 for (int i = 0; i < 100; i++) {
136 SortedList sl1 = new SortedList (i);
137 Assert.AreEqual (i, sl1.Capacity, i.ToString ());
142 public void TestCapacity2 ()
144 SortedList list = new SortedList ();
147 Assert.AreEqual (5, list.Capacity, "#1");
149 SortedList sync = SortedList.Synchronized (list);
150 Assert.AreEqual (5, sync.Capacity, "#2");
153 Assert.AreEqual (20, list.Capacity, "#3");
154 Assert.AreEqual (20, sync.Capacity, "#4");
158 public void TestCapacity3 ()
160 int new_capacity = 5;
161 SortedList list = new SortedList (1000);
162 list.Capacity = new_capacity;
164 Assert.AreEqual (new_capacity, list.Capacity);
168 public void Capacity_BackTo0 ()
170 SortedList list = new SortedList (42);
171 Assert.AreEqual (42, list.Capacity, "#1");
175 // This doesn't fail on 64 bit systems
178 [ExpectedException (typeof (OutOfMemoryException))]
179 public void TestCapacity4 ()
181 SortedList list = new SortedList ();
182 list.Capacity = Int32.MaxValue;
187 public void TestCount ()
189 SortedList sl1 = new SortedList ();
190 Assert.AreEqual (0, sl1.Count, "#1");
191 for (int i = 1; i <= 100; i++) {
192 sl1.Add ("" + i, "" + i);
193 Assert.AreEqual (i, sl1.Count, "#2:" + i);
198 public void TestIsFixed ()
200 SortedList sl1 = new SortedList ();
201 Assert.IsFalse (sl1.IsFixedSize);
205 public void TestIsReadOnly ()
207 SortedList sl1 = new SortedList ();
208 Assert.IsFalse (sl1.IsReadOnly);
212 public void TestItem ()
214 SortedList sl1 = new SortedList ();
217 Assert.IsNull (o, "#A");
220 o = sl1 [(string) null];
222 } catch (ArgumentNullException) {
225 for (int i = 0; i <= 100; i++)
226 sl1.Add ("kala " + i, i);
227 for (int i = 0; i <= 100; i++)
228 Assert.AreEqual (i, sl1 ["kala " + i], "#C:" + i);
232 public void TestSyncRoot ()
234 SortedList sl1 = new SortedList ();
235 Assert.IsNotNull (sl1.SyncRoot);
237 lock( sl1.SyncRoot ) {
238 foreach ( Object item in sl1 ) {
240 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
247 public void TestValues ()
249 SortedList sl1 = new SortedList ();
250 ICollection ic1 = sl1.Values;
251 for (int i = 0; i <= 100; i++) {
252 sl1.Add ("kala " + i, i);
253 Assert.AreEqual (ic1.Count, sl1.Count);
258 // TODO: Add with IComparer
260 public void TestAdd ()
262 // seems SortedList cannot be set fixedsize or readonly
263 SortedList sl1 = new SortedList ();
266 sl1.Add ((string) null, "kala");
268 } catch (ArgumentNullException) {
271 for (int i = 1; i <= 100; i++) {
272 sl1.Add ("kala " + i, i);
273 Assert.AreEqual (i, sl1.Count, "#B1:" + i);
274 Assert.AreEqual (i, sl1 ["kala " + i], "#B2:" + i);
278 sl1.Add ("kala", 10);
279 sl1.Add ("kala", 11);
281 } catch (ArgumentException) {
286 public void TestClear ()
288 SortedList sl1 = new SortedList (10);
289 sl1.Add ("kala", 'c');
290 sl1.Add ("kala2", 'd');
291 Assert.AreEqual (10, sl1.Capacity, "#A1");
292 Assert.AreEqual (2, sl1.Count, "#A2");
294 Assert.AreEqual (0, sl1.Count, "#B1");
299 public void ClearDoesNotTouchCapacity ()
301 SortedList sl = new SortedList ();
302 // according to MSDN docs Clear () does not change capacity
303 for (int i = 0; i < 18; i++) {
306 int capacityBeforeClear = sl.Capacity;
308 int capacityAfterClear = sl.Capacity;
309 Assert.AreEqual (capacityBeforeClear, capacityAfterClear);
313 public void TestClone ()
316 SortedList sl1 = new SortedList (10);
317 for (int i = 0; i <= 50; i++)
318 sl1.Add ("kala " + i, i);
319 SortedList sl2 = (SortedList) sl1.Clone ();
320 for (int i = 0; i <= 50; i++)
321 Assert.AreEqual (sl1 ["kala " + i], sl2 ["kala " + i], "#A:" + i);
324 char [] d10 = { 'a', 'b' };
325 char [] d11 = { 'a', 'c' };
326 char [] d12 = { 'b', 'c' };
327 //char[][] d1 = {d10, d11, d12};
328 SortedList sl1 = new SortedList ();
332 SortedList sl2 = (SortedList) sl1.Clone ();
333 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B1");
334 Assert.AreEqual (sl1 ["d2"], sl2 ["d2"], "#B2");
335 Assert.AreEqual (sl1 ["d3"], sl2 ["d3"], "#B3");
336 ((char []) sl1 ["d1"]) [0] = 'z';
337 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B4");
342 public void TestContains ()
344 SortedList sl1 = new SortedList (55);
345 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
348 if (sl1.Contains (null)) {
351 } catch (ArgumentNullException) {
354 Assert.IsTrue (sl1.Contains ("kala 17"), "#B1");
355 Assert.IsFalse (sl1.Contains ("ohoo"), "#B2");
359 public void TestContainsKey ()
361 SortedList sl1 = new SortedList (55);
362 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
365 if (sl1.ContainsKey (null)) {
368 } catch (ArgumentNullException) {
371 Assert.IsTrue (sl1.ContainsKey ("kala 17"), "#B1");
372 Assert.IsFalse (sl1.ContainsKey ("ohoo"), "#B2");
376 public void TestContainsValue ()
378 SortedList sl1 = new SortedList (55);
382 sl1.Add (3, "three");
385 Assert.IsTrue (sl1.ContainsValue ("zero"), "#1");
386 Assert.IsFalse (sl1.ContainsValue ("ohoo"), "#2");
387 Assert.IsFalse (sl1.ContainsValue (null), "#3");
391 public void TestCopyTo ()
393 SortedList sl1 = new SortedList ();
394 for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
397 sl1.CopyTo (null, 2);
398 Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
399 } catch (ArgumentNullException) {
404 Char [,] c2 = new Char [2, 2];
406 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
407 } catch (ArgumentException) {
412 Char [] c1 = new Char [2];
414 Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
415 } catch (ArgumentOutOfRangeException) {
420 Char [] c1 = new Char [2];
422 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
423 } catch (ArgumentException) {
428 Char [] c1 = new Char [2];
430 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
431 } catch (ArgumentException) {
436 Char [] c2 = new Char [15];
438 Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
439 } catch (InvalidCastException) {
443 // CopyTo function does not work well with SortedList
444 // even example at MSDN gave InvalidCastException
445 // thus, it is NOT tested here
448 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
449 Char[] copy = new Char[15];
450 Array.Clear(copy,0,copy.Length);
451 copy.SetValue( "The", 0 );
452 copy.SetValue( "quick", 1 );
453 copy.SetValue( "brown", 2 );
454 copy.SetValue( "fox", 3 );
455 copy.SetValue( "jumped", 4 );
456 copy.SetValue( "over", 5 );
457 copy.SetValue( "the", 6 );
458 copy.SetValue( "lazy", 7 );
459 copy.SetValue( "dog", 8 );
461 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
462 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
463 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
464 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
468 public SortedList DefaultSL ()
470 SortedList sl1 = new SortedList ();
471 sl1.Add (1.0, "The");
472 sl1.Add (1.1, "quick");
473 sl1.Add (34.0, "brown");
474 sl1.Add (-100.75, "fox");
475 sl1.Add (1.4, "jumped");
476 sl1.Add (1.5, "over");
477 sl1.Add (1.6, "the");
478 sl1.Add (1.7, "lazy");
479 sl1.Add (1.8, "dog");
483 public IList DefaultValues ()
485 IList il = new ArrayList ();
499 public void TestGetByIndex ()
501 SortedList sl1 = DefaultSL ();
502 Assert.AreEqual ("over", sl1.GetByIndex (4), "#A1");
503 Assert.AreEqual ("brown", sl1.GetByIndex (8), "#A2");
508 } catch (ArgumentOutOfRangeException) {
512 sl1.GetByIndex (100);
514 } catch (ArgumentOutOfRangeException) {
519 public void GetEnumerator ()
521 SortedList sl1 = DefaultSL ();
522 IDictionaryEnumerator e = sl1.GetEnumerator ();
523 Assert.IsNotNull (e, "#1");
524 Assert.IsTrue (e.MoveNext (), "#2");
525 Assert.IsNotNull (e.Current, "#3");
526 Assert.IsTrue ((e is ICloneable), "#4");
527 Assert.IsTrue ((e is IDictionaryEnumerator), "#5");
528 Assert.IsTrue ((e is IEnumerator), "#6");
532 public void TestGetKey ()
534 SortedList sl1 = DefaultSL ();
535 Assert.AreEqual (1.5, sl1.GetKey (4), "#A1");
536 Assert.AreEqual (34.0, sl1.GetKey (8), "#A2");
541 } catch (ArgumentOutOfRangeException) {
547 } catch (ArgumentOutOfRangeException) {
552 public void TestGetKeyList ()
554 SortedList sl1 = DefaultSL ();
555 IList keys = sl1.GetKeyList ();
556 Assert.IsNotNull (keys, "#A1");
557 Assert.IsTrue (keys.IsReadOnly, "#A2");
558 Assert.AreEqual (9, keys.Count, "#A3");
559 Assert.AreEqual (1.4, keys [3], "#A4");
560 sl1.Add (33.9, "ehhe");
561 Assert.AreEqual (10, keys.Count, "#B1");
562 Assert.AreEqual (33.9, keys [8], "#B2");
566 public void TestGetValueList ()
568 SortedList sl1 = DefaultSL ();
569 IList originalvals = DefaultValues ();
570 IList vals = sl1.GetValueList ();
571 Assert.IsNotNull (vals, "#A1");
572 Assert.IsTrue (vals.IsReadOnly, "#A2");
573 Assert.AreEqual (vals.Count, sl1.Count, "#A3");
575 for (int i = 0; i < sl1.Count; i++) {
576 Assert.AreEqual (vals [i], originalvals [i], "#A4:" + i);
579 sl1.Add (0.01, "ehhe");
580 Assert.AreEqual (10, vals.Count, "#B1");
581 Assert.AreEqual ("dog", vals [8], "#B2");
584 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
586 public void TestIEnumerable_GetEnumerator() {
587 SortedList sl1 = DefaultSL();
588 IEnumerator e = sl1.IEnumerable.GetEnumerator();
589 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
590 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
591 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
596 public void TestIndexOfKey ()
598 SortedList sl1 = new SortedList (24);
600 for (int i = 0; i <= 50; i++) {
601 string s = string.Format ("{0:D2}", i);
602 sl1.Add ("kala " + s, i);
604 Assert.AreEqual (-1, sl1.IndexOfKey ("kala "), "#A");
607 sl1.IndexOfKey ((string) null);
609 } catch (ArgumentNullException) {
615 } catch (InvalidOperationException) {
618 for (int i = 0; i <= 50; i++) {
619 string s = string.Format ("{0:D2}", i);
620 Assert.AreEqual (i, sl1.IndexOfKey ("kala " + s), "#D:" + i);
625 public void TestIndexOfValue ()
627 SortedList sl1 = new SortedList (24);
629 for (int i = 0; i < 50; i++) {
630 s = string.Format ("{0:D2}", i);
631 sl1.Add ("kala " + s, 100 + i * i);
633 for (int i = 0; i < 50; i++) {
634 s = string.Format ("{0:D2}", i + 50);
635 sl1.Add ("kala " + s, 100 + i * i);
637 Assert.AreEqual (-1, sl1.IndexOfValue (102), "#1");
638 Assert.AreEqual (-1, sl1.IndexOfValue (null), "#2");
639 for (int i = 0; i < 50; i++)
640 Assert.AreEqual (i, sl1.IndexOfValue (100 + i * i), "#3:" + i);
644 public void TestIndexOfValue2 ()
646 SortedList list = new SortedList ();
647 list.Add ("key0", "la la");
648 list.Add ("key1", "value");
649 list.Add ("key2", "value");
651 int i = list.IndexOfValue ("value");
652 Assert.AreEqual (1, i);
656 public void TestIndexOfValue3 ()
658 SortedList list = new SortedList ();
659 int i = list.IndexOfValue ((string) null);
660 Assert.AreEqual (1, -i);
664 public void TestIndexer ()
666 SortedList list = new SortedList ();
668 list.Add (1, new Queue ());
669 list.Add (2, new Hashtable ());
670 list.Add (3, new Stack ());
672 Assert.AreEqual (typeof (Queue), list [1].GetType (), "#1");
673 Assert.AreEqual (typeof (Hashtable), list [2].GetType (), "#2");
674 Assert.AreEqual (typeof (Stack), list [3].GetType (), "#3");
678 public void TestEnumerator ()
680 SortedList list = new SortedList ();
682 list.Add (1, new Queue ());
683 list.Add (2, new Hashtable ());
684 list.Add (3, new Stack ());
686 foreach (DictionaryEntry d in list) {
688 int key = (int) d.Key;
689 Type value = d.Value.GetType ();
693 Assert.AreEqual (typeof (Queue), value, "#1");
697 Assert.AreEqual (typeof (Hashtable), value, "#2");
701 Assert.AreEqual (typeof (Stack), value, "#3");
705 Assert.Fail ("#4:" + value.FullName);
712 public void TestRemove ()
714 SortedList sl1 = new SortedList (24);
717 for (int i = 0; i < 50; i++) sl1.Add ("kala " + i, i);
722 } catch (ArgumentNullException) {
726 sl1.Remove ("kala ");
727 Assert.AreEqual (k, sl1.Count, "#B");
732 } catch (InvalidOperationException) {
735 for (int i = 15; i < 20; i++)
736 sl1.Remove ("kala " + i);
737 for (int i = 45; i < 55; i++)
738 sl1.Remove ("kala " + i);
740 Assert.AreEqual (40, sl1.Count, "#D1");
741 for (int i = 45; i < 55; i++)
742 Assert.IsNull (sl1 ["kala " + i], "#D2:" + i);
746 public void TestRemoveAt ()
748 SortedList sl1 = new SortedList (24);
751 for (int i = 0; i < 50; i++) {
752 string s = string.Format ("{0:D2}", i);
753 sl1.Add ("kala " + s, i);
759 } catch (ArgumentOutOfRangeException) {
765 } catch (ArgumentOutOfRangeException) {
770 for (int i = 0; i < 20; i++)
773 Assert.AreEqual (30, sl1.Count, 30, "#C1");
774 for (int i = 0; i < 9; i++)
775 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C2:" + i);
776 for (int i = 9; i < 29; i++)
777 Assert.IsNull (sl1 ["kala " + string.Format ("{0:D2}", i)], "#C3:" + i);
778 for (int i = 29; i < 50; i++)
779 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C4:" + i);
783 public void TestSetByIndex ()
785 SortedList sl1 = new SortedList (24);
786 for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i);
789 sl1.SetByIndex (-1, 77);
791 } catch (ArgumentOutOfRangeException) {
795 sl1.SetByIndex (100, 88);
797 } catch (ArgumentOutOfRangeException) {
800 for (int i = 5; i < 25; i++)
801 sl1.SetByIndex (i, -1);
802 for (int i = 0; i < 5; i++)
803 Assert.AreEqual (i, sl1 [100 + i], "#C1");
804 for (int i = 5; i < 25; i++)
805 Assert.AreEqual (-1, sl1 [100 + i], "#C2");
806 for (int i = 25; i < 50; i++)
807 Assert.AreEqual (i, sl1 [100 + i], "#C3");
811 public void TestTrimToSize ()
813 SortedList sl1 = new SortedList (24);
817 for (int i = 72; i >= 0; i--)
818 sl1.Add (100 + i, i);
823 public void SerializeTest ()
825 SortedList sl1 = new SortedList ();
830 BinaryFormatter bf = new BinaryFormatter ();
831 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
832 MemoryStream ms = new MemoryStream ();
833 bf.Serialize (ms, sl1);
836 SortedList sl2 = (SortedList) bf.Deserialize (ms);
837 Assert.IsNotNull (sl2, "#1");
838 Assert.AreEqual (3, sl2.Count, "#2");
839 Assert.AreEqual (sl1 [0], sl2 [0], "#3");
840 Assert.AreEqual (sl1 [1], sl2 [1], "#4");
841 Assert.AreEqual (sl1 [2], sl2 [2], "#5");
845 public void Keys_Serialize ()
847 SortedList sl = new SortedList ();
852 IList keys1 = (IList) sl.Keys;
853 BinaryFormatter bf = new BinaryFormatter ();
854 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
855 MemoryStream ms = new MemoryStream ();
856 bf.Serialize (ms, keys1);
859 IList keys2 = (IList) bf.Deserialize (ms);
860 Assert.IsNotNull (keys2, "#1");
861 Assert.AreEqual (3, keys2.Count, "#2");
862 Assert.AreEqual (keys1 [0], keys2 [0], "#3");
863 Assert.AreEqual (keys1 [1], keys2 [1], "#4");
864 Assert.AreEqual (keys1 [2], keys2 [2], "#5");
868 public void Values_Serialize ()
870 SortedList sl = new SortedList ();
875 IList values1 = (IList) sl.Values;
876 BinaryFormatter bf = new BinaryFormatter ();
877 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
878 MemoryStream ms = new MemoryStream ();
879 bf.Serialize (ms, values1);
882 IList values2 = (IList) bf.Deserialize (ms);
883 Assert.IsNotNull (values2, "#1");
884 Assert.AreEqual (3, values2.Count, "#2");
885 Assert.AreEqual (values1 [0], values2 [0], "#3");
886 Assert.AreEqual (values1 [1], values2 [1], "#4");
887 Assert.AreEqual (values1 [2], values2 [2], "#5");
891 [Category ("NotWorking")]
892 public void Values_Deserialize ()
894 BinaryFormatter bf = new BinaryFormatter ();
896 MemoryStream ms = new MemoryStream ();
897 ms.Write (_serializedValues, 0, _serializedValues.Length);
900 IList values = (IList) bf.Deserialize (ms);
901 Assert.AreEqual (3, values.Count, "#1");
902 Assert.AreEqual ("B", values [0], "#2");
903 Assert.AreEqual ("A", values [1], "#3");
904 Assert.AreEqual ("C", values [2], "#4");
908 [ExpectedException (typeof (InvalidOperationException))]
909 public void SetIdenticalObjectException ()
911 // Even though the key/value pair being set are identical to
912 // the existing one, it causes snapshot out of sync.
913 SortedList sl = new SortedList ();
915 foreach (string s in sl.Keys)
920 public void Ctor_IComparer ()
922 SortedList sl = new SortedList (new SortedListComparer ());
923 sl.Add (new object (), new object ());
927 public void Ctor_IComparer_Null ()
929 SortedList sl = new SortedList ((IComparer) null);
930 sl.Add (new object (), new object ());
934 public void Ctor_IDictionary_IComparer_Before ()
936 Hashtable ht = new Hashtable ();
939 // adding a non-IComparable in Hashtable
940 ht.Add (new object (), "c");
941 SortedList sl = new SortedList (ht, new SortedListComparer ());
942 Assert.AreEqual (3, sl.Count);
946 [ExpectedException (typeof (InvalidOperationException))]
947 public void Ctor_IDictionary_DefaultInvariant_Before ()
949 Hashtable ht = new Hashtable ();
952 // adding a non-IComparable in Hashtable
953 ht.Add (new object (), "c");
954 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
955 Assert.AreEqual (3, sl.Count);
959 public void Ctor_IDictionary_IComparer_Null_Before_1item ()
961 Hashtable ht = new Hashtable ();
962 // adding a non-IComparable in Hashtable
963 ht.Add (new object (), "c");
964 SortedList sl = new SortedList (ht, null);
965 Assert.AreEqual (1, sl.Count);
969 [ExpectedException (typeof (InvalidOperationException))]
970 public void Ctor_IDictionary_IComparer_Null_Before_2items ()
972 Hashtable ht = new Hashtable ();
974 // adding a non-IComparable in Hashtable
975 ht.Add (new object (), "c");
976 SortedList sl = new SortedList (ht, null);
977 Assert.AreEqual (2, sl.Count);
981 public void Ctor_IDictionary_IComparer_After ()
983 Hashtable ht = new Hashtable ();
986 SortedList sl = new SortedList (ht, new SortedListComparer ());
987 Assert.AreEqual (2, sl.Count);
988 // adding a non-IComparable in SortedList
989 sl.Add (new object (), "c");
993 [ExpectedException (typeof (InvalidOperationException))]
994 public void Ctor_IDictionary_DefaultInvariant_After ()
996 Hashtable ht = new Hashtable ();
999 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1000 Assert.AreEqual (2, sl.Count);
1001 // adding a non-IComparable in SortedList
1002 sl.Add (new object (), "c");
1006 public void Ctor_IDictionary_IComparer_Null_After_1item ()
1008 SortedList sl = new SortedList (new Hashtable (), null);
1009 sl.Add (new object (), "b");
1013 [ExpectedException (typeof (InvalidOperationException))]
1014 public void Ctor_IDictionary_IComparer_Null_After_2items ()
1016 SortedList sl = new SortedList (new Hashtable (), null);
1018 sl.Add (new object (), "b");
1022 public void IComparer_Clone ()
1024 SortedList sl = new SortedList (new SortedListComparer ());
1025 sl.Add (new object (), new object ());
1026 SortedList clone = (SortedList) sl.Clone ();
1030 public void IComparer_Null_Clone ()
1032 SortedList sl = new SortedList ((IComparer) null);
1033 sl.Add (new object (), new object ());
1034 SortedList clone = (SortedList) sl.Clone ();
1037 sealed class StartsWithComparator : IComparer {
1038 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1040 public int Compare(object p, object w)
1042 string part = (string) p;
1043 string whole = (string) w;
1044 // let the default string comparer deal with null or when part is not smaller then whole
1045 if (part == null || whole == null || part.Length >= whole.Length)
1046 return String.Compare (part, whole);
1048 // loop through all characters that part and whole have in common
1052 match = (part[pos] == whole[pos]);
1053 } while (match && ++pos < part.Length);
1055 // return result of last comparison
1056 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1060 sealed class StartsWithComparatorPartWholeCheck : IComparer
1062 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1064 public int Compare(object p, object w)
1066 string part = (string) p;
1067 string whole = (string) w;
1068 Assert.IsTrue(part == "Part", "#PWC0");
1069 Assert.IsTrue(whole == "Whole", "#PWC1");
1071 // let the default string comparer deal with null or when part is not smaller then whole
1072 if (part == null || whole == null || part.Length >= whole.Length)
1073 return String.Compare(part, whole);
1075 // loop through all characters that part and whole have in common
1079 match = (part[pos] == whole[pos]);
1080 } while (match && ++pos < part.Length);
1082 // return result of last comparison
1083 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1088 public void ComparatorUsageTest()
1090 SortedList sl = new SortedList(StartsWithComparator.Instance);
1092 sl.Add("Apples", "Value-Apples");
1093 sl.Add("Bananas", "Value-Bananas");
1094 sl.Add("Oranges", "Value-Oranges");
1096 // Ensure 3 objects exist in the collection
1097 Assert.IsTrue(sl.Count == 3, "Count");
1099 // Complete Match Test Set
1100 Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
1101 Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
1102 Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
1104 // Partial Match Test Set
1105 Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
1106 Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
1107 Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
1109 // Reversed Match Test Set
1110 Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
1113 Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
1114 Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
1115 Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
1120 public void ComparatorPartWholeCheck()
1122 SortedList sl = new SortedList (StartsWithComparatorPartWholeCheck.Instance);
1123 sl.Add("Part", "Value-Part");
1124 Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
1128 public void NonComparatorStringCheck()
1130 SortedList sl = new SortedList ();
1132 sl.Add("Oranges", "Value-Oranges");
1133 sl.Add("Apples", "Value-Apples");
1134 sl.Add("Bananas", "Value-Bananas");
1137 Assert.IsTrue(sl.Count == 3, "NCSC #A0");
1139 Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
1140 Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
1141 Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
1143 Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
1144 Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
1145 Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
1147 string [] keys = new string [sl.Keys.Count];
1148 sl.Keys.CopyTo (keys, 0);
1149 Assert.IsTrue(keys [0] == "Apples", "NCSC #D1");
1150 Assert.IsTrue(keys [1] == "Bananas", "NCSC #D2");
1151 Assert.IsTrue(keys [2] == "Oranges", "NCSC #D3");
1154 private static byte [] _serializedValues = new byte [] {
1155 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
1156 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
1157 0x01, 0x00, 0x00, 0x00, 0x27, 0x53, 0x79, 0x73, 0x74,
1158 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63,
1159 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72,
1160 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56,
1161 0x61, 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x01,
1162 0x00, 0x00, 0x00, 0x0a, 0x73, 0x6f, 0x72, 0x74, 0x65,
1163 0x64, 0x4c, 0x69, 0x73, 0x74, 0x03, 0x1d, 0x53, 0x79,
1164 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c,
1165 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53,
1166 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74,
1167 0x09, 0x02, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00,
1168 0x00, 0x1d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1169 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1170 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1171 0x4c, 0x69, 0x73, 0x74, 0x07, 0x00, 0x00, 0x00, 0x04,
1172 0x6b, 0x65, 0x79, 0x73, 0x06, 0x76, 0x61, 0x6c, 0x75,
1173 0x65, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x07,
1174 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x63,
1175 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x07, 0x6b,
1176 0x65, 0x79, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x76, 0x61,
1177 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x05, 0x05,
1178 0x00, 0x00, 0x03, 0x03, 0x03, 0x08, 0x08, 0x1b, 0x53,
1179 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
1180 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
1181 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x25,
1182 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
1183 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
1184 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69,
1185 0x73, 0x74, 0x2b, 0x4b, 0x65, 0x79, 0x4c, 0x69, 0x73,
1186 0x74, 0x27, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1187 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1188 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1189 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56, 0x61, 0x6c, 0x75,
1190 0x65, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x03, 0x00, 0x00,
1191 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
1192 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00,
1193 0x00, 0x0a, 0x09, 0x01, 0x00, 0x00, 0x00, 0x10, 0x03,
1194 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x08, 0x08,
1195 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x05, 0x00, 0x00,
1196 0x00, 0x08, 0x08, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x0d,
1197 0x10, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
1198 0x06, 0x07, 0x00, 0x00, 0x00, 0x01, 0x42, 0x06, 0x08,
1199 0x00, 0x00, 0x00, 0x01, 0x41, 0x06, 0x09, 0x00, 0x00,
1200 0x00, 0x01, 0x43, 0x0d, 0x0d, 0x04, 0x05, 0x00, 0x00,
1201 0x00, 0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1202 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1203 0x6e, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
1204 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x0b };