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");
47 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
48 Assert.AreEqual (icap, temp1.Capacity, "#2");
53 public void TestConstructor2 ()
55 Comparer c = Comparer.Default;
56 SortedList temp1 = new SortedList (c);
57 Assert.IsNotNull (temp1, "#1");
58 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
59 Assert.AreEqual (icap, temp1.Capacity, "#2");
64 public void TestConstructor3 ()
66 Hashtable d = new Hashtable ();
67 d.Add ("one", "Mircosoft");
68 d.Add ("two", "will");
69 d.Add ("three", "rule");
70 d.Add ("four", "the world");
72 SortedList temp1 = new SortedList (d);
73 Assert.IsNotNull (temp1, "#A1");
74 Assert.AreEqual (4, temp1.Capacity, "#A2");
75 Assert.AreEqual (4, temp1.Count, "#A3");
78 new SortedList ((Hashtable) null);
80 } catch (ArgumentNullException) {
85 d.Add ("one", "Mircosoft");
86 d.Add ("two", "will");
87 d.Add ("three", "rule");
88 d.Add ("four", "the world");
92 } catch (InvalidOperationException) {
97 public void TestConstructor4 ()
99 SortedList temp1 = new SortedList (17);
100 Assert.IsNotNull (temp1, "#A1");
101 Assert.AreEqual (17, temp1.Capacity, "#A2");
106 } catch (ArgumentOutOfRangeException) {
109 temp1 = new SortedList (0);
110 Assert.IsNotNull (temp1, "#C");
114 public void TestConstructor5 ()
116 Comparer c = Comparer.Default;
117 SortedList temp1 = new SortedList (c, 27);
118 Assert.IsNotNull (temp1, "#A1");
119 Assert.AreEqual (27, temp1.Capacity, "#A2");
122 new SortedList (-12);
124 } catch (ArgumentOutOfRangeException) {
128 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
130 public void Constructor_Capacity ()
132 SortedList sl = new SortedList (0);
133 Assert.AreEqual (0, sl.Capacity, "#A1");
135 // doesn't reset to class default (16)
136 Assert.AreEqual (0, sl.Capacity, "#A2");
138 for (int i = 1; i <= 16; i++) {
139 sl = new SortedList (i);
140 Assert.AreEqual (i, sl.Capacity, "#B1:" + i);
142 // reset to class default (16)
143 Assert.AreEqual (16, sl.Capacity, "#B2:" + i);
149 public void TestIsSynchronized ()
151 SortedList sl1 = new SortedList ();
152 Assert.IsFalse (sl1.IsSynchronized, "#1");
153 SortedList sl2 = SortedList.Synchronized (sl1);
154 Assert.IsTrue (sl2.IsSynchronized, "#2");
158 public void TestCapacity ()
160 for (int i = 0; i < 100; i++) {
161 SortedList sl1 = new SortedList (i);
162 Assert.AreEqual (i, sl1.Capacity, i.ToString ());
167 public void TestCapacity2 ()
169 SortedList list = new SortedList ();
172 Assert.AreEqual (5, list.Capacity, "#1");
174 SortedList sync = SortedList.Synchronized (list);
175 Assert.AreEqual (5, sync.Capacity, "#2");
178 Assert.AreEqual (20, list.Capacity, "#3");
179 Assert.AreEqual (20, sync.Capacity, "#4");
183 public void TestCapacity3 ()
185 int new_capacity = 5;
186 SortedList list = new SortedList (1000);
187 list.Capacity = new_capacity;
189 Assert.AreEqual (new_capacity, list.Capacity);
193 public void Capacity_BackTo0 ()
195 SortedList list = new SortedList (42);
196 Assert.AreEqual (42, list.Capacity, "#1");
200 // This doesn't fail on 64 bit systems
203 [ExpectedException (typeof (OutOfMemoryException))]
204 public void TestCapacity4 ()
206 SortedList list = new SortedList ();
207 list.Capacity = Int32.MaxValue;
212 public void TestCount ()
214 SortedList sl1 = new SortedList ();
215 Assert.AreEqual (0, sl1.Count, "#1");
216 for (int i = 1; i <= 100; i++) {
217 sl1.Add ("" + i, "" + i);
218 Assert.AreEqual (i, sl1.Count, "#2:" + i);
223 public void TestIsFixed ()
225 SortedList sl1 = new SortedList ();
226 Assert.IsFalse (sl1.IsFixedSize);
230 public void TestIsReadOnly ()
232 SortedList sl1 = new SortedList ();
233 Assert.IsFalse (sl1.IsReadOnly);
237 public void TestItem ()
239 SortedList sl1 = new SortedList ();
242 Assert.IsNull (o, "#A");
245 o = sl1 [(string) null];
247 } catch (ArgumentNullException) {
250 for (int i = 0; i <= 100; i++)
251 sl1.Add ("kala " + i, i);
252 for (int i = 0; i <= 100; i++)
253 Assert.AreEqual (i, sl1 ["kala " + i], "#C:" + i);
257 public void TestSyncRoot ()
259 SortedList sl1 = new SortedList ();
260 Assert.IsNotNull (sl1.SyncRoot);
262 lock( sl1.SyncRoot ) {
263 foreach ( Object item in sl1 ) {
265 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
272 public void TestValues ()
274 SortedList sl1 = new SortedList ();
275 ICollection ic1 = sl1.Values;
276 for (int i = 0; i <= 100; i++) {
277 sl1.Add ("kala " + i, i);
278 Assert.AreEqual (ic1.Count, sl1.Count);
283 // TODO: Add with IComparer
285 public void TestAdd ()
287 // seems SortedList cannot be set fixedsize or readonly
288 SortedList sl1 = new SortedList ();
291 sl1.Add ((string) null, "kala");
293 } catch (ArgumentNullException) {
296 for (int i = 1; i <= 100; i++) {
297 sl1.Add ("kala " + i, i);
298 Assert.AreEqual (i, sl1.Count, "#B1:" + i);
299 Assert.AreEqual (i, sl1 ["kala " + i], "#B2:" + i);
303 sl1.Add ("kala", 10);
304 sl1.Add ("kala", 11);
306 } catch (ArgumentException) {
311 public void TestClear ()
313 SortedList sl1 = new SortedList (10);
314 sl1.Add ("kala", 'c');
315 sl1.Add ("kala2", 'd');
316 Assert.AreEqual (10, sl1.Capacity, "#A1");
317 Assert.AreEqual (2, sl1.Count, "#A2");
319 Assert.AreEqual (0, sl1.Count, "#B1");
320 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
321 Assert.AreEqual (16, sl1.Capacity, "#B2");
325 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
327 public void Clear_Capacity ()
329 // strangely Clear change the default capacity (while Capacity doesn't)
330 for (int i = 0; i <= 16; i++) {
331 SortedList sl = new SortedList (i);
332 Assert.AreEqual (i, sl.Capacity, "#1:"+ i);
334 // reset to class default (16)
335 Assert.AreEqual (16, sl.Capacity, "#2:" + i);
340 public void Clear_Capacity_Reset ()
342 SortedList sl = new SortedList (0);
343 Assert.AreEqual (0, sl.Capacity, "#1");
345 // reset to class default (16)
346 Assert.AreEqual (16, sl.Capacity, "#2");
348 Assert.AreEqual (16, sl.Capacity, "#3");
349 // note: we didn't return to 0 - so Clear cahnge the default capacity
354 public void ClearDoesNotTouchCapacity ()
356 SortedList sl = new SortedList ();
357 // according to MSDN docs Clear () does not change capacity
358 for (int i = 0; i < 18; i++) {
361 int capacityBeforeClear = sl.Capacity;
363 int capacityAfterClear = sl.Capacity;
364 Assert.AreEqual (capacityBeforeClear, capacityAfterClear);
368 public void TestClone ()
371 SortedList sl1 = new SortedList (10);
372 for (int i = 0; i <= 50; i++)
373 sl1.Add ("kala " + i, i);
374 SortedList sl2 = (SortedList) sl1.Clone ();
375 for (int i = 0; i <= 50; i++)
376 Assert.AreEqual (sl1 ["kala " + i], sl2 ["kala " + i], "#A:" + i);
379 char [] d10 = { 'a', 'b' };
380 char [] d11 = { 'a', 'c' };
381 char [] d12 = { 'b', 'c' };
382 //char[][] d1 = {d10, d11, d12};
383 SortedList sl1 = new SortedList ();
387 SortedList sl2 = (SortedList) sl1.Clone ();
388 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B1");
389 Assert.AreEqual (sl1 ["d2"], sl2 ["d2"], "#B2");
390 Assert.AreEqual (sl1 ["d3"], sl2 ["d3"], "#B3");
391 ((char []) sl1 ["d1"]) [0] = 'z';
392 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B4");
397 public void TestContains ()
399 SortedList sl1 = new SortedList (55);
400 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
403 if (sl1.Contains (null)) {
406 } catch (ArgumentNullException) {
409 Assert.IsTrue (sl1.Contains ("kala 17"), "#B1");
410 Assert.IsFalse (sl1.Contains ("ohoo"), "#B2");
414 public void TestContainsKey ()
416 SortedList sl1 = new SortedList (55);
417 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
420 if (sl1.ContainsKey (null)) {
423 } catch (ArgumentNullException) {
426 Assert.IsTrue (sl1.ContainsKey ("kala 17"), "#B1");
427 Assert.IsFalse (sl1.ContainsKey ("ohoo"), "#B2");
431 public void TestContainsValue ()
433 SortedList sl1 = new SortedList (55);
437 sl1.Add (3, "three");
440 Assert.IsTrue (sl1.ContainsValue ("zero"), "#1");
441 Assert.IsFalse (sl1.ContainsValue ("ohoo"), "#2");
442 Assert.IsFalse (sl1.ContainsValue (null), "#3");
446 public void TestCopyTo ()
448 SortedList sl1 = new SortedList ();
449 for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
452 sl1.CopyTo (null, 2);
453 Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
454 } catch (ArgumentNullException) {
459 Char [,] c2 = new Char [2, 2];
461 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
462 } catch (ArgumentException) {
467 Char [] c1 = new Char [2];
469 Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
470 } catch (ArgumentOutOfRangeException) {
475 Char [] c1 = new Char [2];
477 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
478 } catch (ArgumentException) {
483 Char [] c1 = new Char [2];
485 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
486 } catch (ArgumentException) {
491 Char [] c2 = new Char [15];
493 Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
494 } catch (InvalidCastException) {
498 // CopyTo function does not work well with SortedList
499 // even example at MSDN gave InvalidCastException
500 // thus, it is NOT tested here
503 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
504 Char[] copy = new Char[15];
505 Array.Clear(copy,0,copy.Length);
506 copy.SetValue( "The", 0 );
507 copy.SetValue( "quick", 1 );
508 copy.SetValue( "brown", 2 );
509 copy.SetValue( "fox", 3 );
510 copy.SetValue( "jumped", 4 );
511 copy.SetValue( "over", 5 );
512 copy.SetValue( "the", 6 );
513 copy.SetValue( "lazy", 7 );
514 copy.SetValue( "dog", 8 );
516 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
517 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
518 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
519 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
523 public SortedList DefaultSL ()
525 SortedList sl1 = new SortedList ();
526 sl1.Add (1.0, "The");
527 sl1.Add (1.1, "quick");
528 sl1.Add (34.0, "brown");
529 sl1.Add (-100.75, "fox");
530 sl1.Add (1.4, "jumped");
531 sl1.Add (1.5, "over");
532 sl1.Add (1.6, "the");
533 sl1.Add (1.7, "lazy");
534 sl1.Add (1.8, "dog");
538 public IList DefaultValues ()
540 IList il = new ArrayList ();
554 public void TestGetByIndex ()
556 SortedList sl1 = DefaultSL ();
557 Assert.AreEqual ("over", sl1.GetByIndex (4), "#A1");
558 Assert.AreEqual ("brown", sl1.GetByIndex (8), "#A2");
563 } catch (ArgumentOutOfRangeException) {
567 sl1.GetByIndex (100);
569 } catch (ArgumentOutOfRangeException) {
574 public void GetEnumerator ()
576 SortedList sl1 = DefaultSL ();
577 IDictionaryEnumerator e = sl1.GetEnumerator ();
578 Assert.IsNotNull (e, "#1");
579 Assert.IsTrue (e.MoveNext (), "#2");
580 Assert.IsNotNull (e.Current, "#3");
581 Assert.IsTrue ((e is ICloneable), "#4");
582 Assert.IsTrue ((e is IDictionaryEnumerator), "#5");
583 Assert.IsTrue ((e is IEnumerator), "#6");
587 public void TestGetKey ()
589 SortedList sl1 = DefaultSL ();
590 Assert.AreEqual (1.5, sl1.GetKey (4), "#A1");
591 Assert.AreEqual (34.0, sl1.GetKey (8), "#A2");
596 } catch (ArgumentOutOfRangeException) {
602 } catch (ArgumentOutOfRangeException) {
607 public void TestGetKeyList ()
609 SortedList sl1 = DefaultSL ();
610 IList keys = sl1.GetKeyList ();
611 Assert.IsNotNull (keys, "#A1");
612 Assert.IsTrue (keys.IsReadOnly, "#A2");
613 Assert.AreEqual (9, keys.Count, "#A3");
614 Assert.AreEqual (1.4, keys [3], "#A4");
615 sl1.Add (33.9, "ehhe");
616 Assert.AreEqual (10, keys.Count, "#B1");
617 Assert.AreEqual (33.9, keys [8], "#B2");
621 public void TestGetValueList ()
623 SortedList sl1 = DefaultSL ();
624 IList originalvals = DefaultValues ();
625 IList vals = sl1.GetValueList ();
626 Assert.IsNotNull (vals, "#A1");
627 Assert.IsTrue (vals.IsReadOnly, "#A2");
628 Assert.AreEqual (vals.Count, sl1.Count, "#A3");
630 for (int i = 0; i < sl1.Count; i++) {
631 Assert.AreEqual (vals [i], originalvals [i], "#A4:" + i);
634 sl1.Add (0.01, "ehhe");
635 Assert.AreEqual (10, vals.Count, "#B1");
636 Assert.AreEqual ("dog", vals [8], "#B2");
639 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
641 public void TestIEnumerable_GetEnumerator() {
642 SortedList sl1 = DefaultSL();
643 IEnumerator e = sl1.IEnumerable.GetEnumerator();
644 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
645 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
646 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
651 public void TestIndexOfKey ()
653 SortedList sl1 = new SortedList (24);
655 for (int i = 0; i <= 50; i++) {
656 string s = string.Format ("{0:D2}", i);
657 sl1.Add ("kala " + s, i);
659 Assert.AreEqual (-1, sl1.IndexOfKey ("kala "), "#A");
662 sl1.IndexOfKey ((string) null);
664 } catch (ArgumentNullException) {
670 } catch (InvalidOperationException) {
673 for (int i = 0; i <= 50; i++) {
674 string s = string.Format ("{0:D2}", i);
675 Assert.AreEqual (i, sl1.IndexOfKey ("kala " + s), "#D:" + i);
680 public void TestIndexOfValue ()
682 SortedList sl1 = new SortedList (24);
684 for (int i = 0; i < 50; i++) {
685 s = string.Format ("{0:D2}", i);
686 sl1.Add ("kala " + s, 100 + i * i);
688 for (int i = 0; i < 50; i++) {
689 s = string.Format ("{0:D2}", i + 50);
690 sl1.Add ("kala " + s, 100 + i * i);
692 Assert.AreEqual (-1, sl1.IndexOfValue (102), "#1");
693 Assert.AreEqual (-1, sl1.IndexOfValue (null), "#2");
694 for (int i = 0; i < 50; i++)
695 Assert.AreEqual (i, sl1.IndexOfValue (100 + i * i), "#3:" + i);
699 public void TestIndexOfValue2 ()
701 SortedList list = new SortedList ();
702 list.Add ("key0", "la la");
703 list.Add ("key1", "value");
704 list.Add ("key2", "value");
706 int i = list.IndexOfValue ("value");
707 Assert.AreEqual (1, i);
711 public void TestIndexOfValue3 ()
713 SortedList list = new SortedList ();
714 int i = list.IndexOfValue ((string) null);
715 Assert.AreEqual (1, -i);
719 public void TestIndexer ()
721 SortedList list = new SortedList ();
723 list.Add (1, new Queue ());
724 list.Add (2, new Hashtable ());
725 list.Add (3, new Stack ());
727 Assert.AreEqual (typeof (Queue), list [1].GetType (), "#1");
728 Assert.AreEqual (typeof (Hashtable), list [2].GetType (), "#2");
729 Assert.AreEqual (typeof (Stack), list [3].GetType (), "#3");
733 public void TestEnumerator ()
735 SortedList list = new SortedList ();
737 list.Add (1, new Queue ());
738 list.Add (2, new Hashtable ());
739 list.Add (3, new Stack ());
741 foreach (DictionaryEntry d in list) {
743 int key = (int) d.Key;
744 Type value = d.Value.GetType ();
748 Assert.AreEqual (typeof (Queue), value, "#1");
752 Assert.AreEqual (typeof (Hashtable), value, "#2");
756 Assert.AreEqual (typeof (Stack), value, "#3");
760 Assert.Fail ("#4:" + value.FullName);
767 public void TestRemove ()
769 SortedList sl1 = new SortedList (24);
772 for (int i = 0; i < 50; i++) sl1.Add ("kala " + i, i);
777 } catch (ArgumentNullException) {
781 sl1.Remove ("kala ");
782 Assert.AreEqual (k, sl1.Count, "#B");
787 } catch (InvalidOperationException) {
790 for (int i = 15; i < 20; i++)
791 sl1.Remove ("kala " + i);
792 for (int i = 45; i < 55; i++)
793 sl1.Remove ("kala " + i);
795 Assert.AreEqual (40, sl1.Count, "#D1");
796 for (int i = 45; i < 55; i++)
797 Assert.IsNull (sl1 ["kala " + i], "#D2:" + i);
801 public void TestRemoveAt ()
803 SortedList sl1 = new SortedList (24);
806 for (int i = 0; i < 50; i++) {
807 string s = string.Format ("{0:D2}", i);
808 sl1.Add ("kala " + s, i);
814 } catch (ArgumentOutOfRangeException) {
820 } catch (ArgumentOutOfRangeException) {
825 for (int i = 0; i < 20; i++)
828 Assert.AreEqual (30, sl1.Count, 30, "#C1");
829 for (int i = 0; i < 9; i++)
830 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C2:" + i);
831 for (int i = 9; i < 29; i++)
832 Assert.IsNull (sl1 ["kala " + string.Format ("{0:D2}", i)], "#C3:" + i);
833 for (int i = 29; i < 50; i++)
834 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C4:" + i);
838 public void TestSetByIndex ()
840 SortedList sl1 = new SortedList (24);
841 for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i);
844 sl1.SetByIndex (-1, 77);
846 } catch (ArgumentOutOfRangeException) {
850 sl1.SetByIndex (100, 88);
852 } catch (ArgumentOutOfRangeException) {
855 for (int i = 5; i < 25; i++)
856 sl1.SetByIndex (i, -1);
857 for (int i = 0; i < 5; i++)
858 Assert.AreEqual (i, sl1 [100 + i], "#C1");
859 for (int i = 5; i < 25; i++)
860 Assert.AreEqual (-1, sl1 [100 + i], "#C2");
861 for (int i = 25; i < 50; i++)
862 Assert.AreEqual (i, sl1 [100 + i], "#C3");
866 public void TestTrimToSize ()
868 SortedList sl1 = new SortedList (24);
871 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
872 Assert.AreEqual (icap, sl1.Capacity, "#1");
875 for (int i = 72; i >= 0; i--)
876 sl1.Add (100 + i, i);
878 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
879 Assert.AreEqual (73, sl1.Capacity, "#2");
884 public void SerializeTest ()
886 SortedList sl1 = new SortedList ();
891 BinaryFormatter bf = new BinaryFormatter ();
892 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
893 MemoryStream ms = new MemoryStream ();
894 bf.Serialize (ms, sl1);
897 SortedList sl2 = (SortedList) bf.Deserialize (ms);
898 Assert.IsNotNull (sl2, "#1");
899 Assert.AreEqual (3, sl2.Count, "#2");
900 Assert.AreEqual (sl1 [0], sl2 [0], "#3");
901 Assert.AreEqual (sl1 [1], sl2 [1], "#4");
902 Assert.AreEqual (sl1 [2], sl2 [2], "#5");
906 public void Keys_Serialize ()
908 SortedList sl = new SortedList ();
913 IList keys1 = (IList) sl.Keys;
914 BinaryFormatter bf = new BinaryFormatter ();
915 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
916 MemoryStream ms = new MemoryStream ();
917 bf.Serialize (ms, keys1);
920 IList keys2 = (IList) bf.Deserialize (ms);
921 Assert.IsNotNull (keys2, "#1");
922 Assert.AreEqual (3, keys2.Count, "#2");
923 Assert.AreEqual (keys1 [0], keys2 [0], "#3");
924 Assert.AreEqual (keys1 [1], keys2 [1], "#4");
925 Assert.AreEqual (keys1 [2], keys2 [2], "#5");
929 public void Values_Serialize ()
931 SortedList sl = new SortedList ();
936 IList values1 = (IList) sl.Values;
937 BinaryFormatter bf = new BinaryFormatter ();
938 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
939 MemoryStream ms = new MemoryStream ();
940 bf.Serialize (ms, values1);
943 IList values2 = (IList) bf.Deserialize (ms);
944 Assert.IsNotNull (values2, "#1");
945 Assert.AreEqual (3, values2.Count, "#2");
946 Assert.AreEqual (values1 [0], values2 [0], "#3");
947 Assert.AreEqual (values1 [1], values2 [1], "#4");
948 Assert.AreEqual (values1 [2], values2 [2], "#5");
952 [Category ("NotWorking")]
953 public void Values_Deserialize ()
955 BinaryFormatter bf = new BinaryFormatter ();
957 MemoryStream ms = new MemoryStream ();
958 ms.Write (_serializedValues, 0, _serializedValues.Length);
961 IList values = (IList) bf.Deserialize (ms);
962 Assert.AreEqual (3, values.Count, "#1");
963 Assert.AreEqual ("B", values [0], "#2");
964 Assert.AreEqual ("A", values [1], "#3");
965 Assert.AreEqual ("C", values [2], "#4");
969 [ExpectedException (typeof (InvalidOperationException))]
970 public void SetIdenticalObjectException ()
972 // Even though the key/value pair being set are identical to
973 // the existing one, it causes snapshot out of sync.
974 SortedList sl = new SortedList ();
976 foreach (string s in sl.Keys)
981 public void Ctor_IComparer ()
983 SortedList sl = new SortedList (new SortedListComparer ());
984 sl.Add (new object (), new object ());
988 public void Ctor_IComparer_Null ()
990 SortedList sl = new SortedList ((IComparer) null);
991 sl.Add (new object (), new object ());
995 public void Ctor_IDictionary_IComparer_Before ()
997 Hashtable ht = new Hashtable ();
1000 // adding a non-IComparable in Hashtable
1001 ht.Add (new object (), "c");
1002 SortedList sl = new SortedList (ht, new SortedListComparer ());
1003 Assert.AreEqual (3, sl.Count);
1007 [ExpectedException (typeof (InvalidOperationException))]
1008 public void Ctor_IDictionary_DefaultInvariant_Before ()
1010 Hashtable ht = new Hashtable ();
1013 // adding a non-IComparable in Hashtable
1014 ht.Add (new object (), "c");
1015 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1016 Assert.AreEqual (3, sl.Count);
1020 public void Ctor_IDictionary_IComparer_Null_Before_1item ()
1022 Hashtable ht = new Hashtable ();
1023 // adding a non-IComparable in Hashtable
1024 ht.Add (new object (), "c");
1025 SortedList sl = new SortedList (ht, null);
1026 Assert.AreEqual (1, sl.Count);
1030 [ExpectedException (typeof (InvalidOperationException))]
1031 public void Ctor_IDictionary_IComparer_Null_Before_2items ()
1033 Hashtable ht = new Hashtable ();
1035 // adding a non-IComparable in Hashtable
1036 ht.Add (new object (), "c");
1037 SortedList sl = new SortedList (ht, null);
1038 Assert.AreEqual (2, sl.Count);
1042 public void Ctor_IDictionary_IComparer_After ()
1044 Hashtable ht = new Hashtable ();
1047 SortedList sl = new SortedList (ht, new SortedListComparer ());
1048 Assert.AreEqual (2, sl.Count);
1049 // adding a non-IComparable in SortedList
1050 sl.Add (new object (), "c");
1054 [ExpectedException (typeof (InvalidOperationException))]
1055 public void Ctor_IDictionary_DefaultInvariant_After ()
1057 Hashtable ht = new Hashtable ();
1060 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1061 Assert.AreEqual (2, sl.Count);
1062 // adding a non-IComparable in SortedList
1063 sl.Add (new object (), "c");
1067 public void Ctor_IDictionary_IComparer_Null_After_1item ()
1069 SortedList sl = new SortedList (new Hashtable (), null);
1070 sl.Add (new object (), "b");
1074 [ExpectedException (typeof (InvalidOperationException))]
1075 public void Ctor_IDictionary_IComparer_Null_After_2items ()
1077 SortedList sl = new SortedList (new Hashtable (), null);
1079 sl.Add (new object (), "b");
1083 public void IComparer_Clone ()
1085 SortedList sl = new SortedList (new SortedListComparer ());
1086 sl.Add (new object (), new object ());
1087 SortedList clone = (SortedList) sl.Clone ();
1091 public void IComparer_Null_Clone ()
1093 SortedList sl = new SortedList ((IComparer) null);
1094 sl.Add (new object (), new object ());
1095 SortedList clone = (SortedList) sl.Clone ();
1098 sealed class StartsWithComparator : IComparer {
1099 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1101 public int Compare(object p, object w)
1103 string part = (string) p;
1104 string whole = (string) w;
1105 // let the default string comparer deal with null or when part is not smaller then whole
1106 if (part == null || whole == null || part.Length >= whole.Length)
1107 return String.Compare (part, whole);
1109 // loop through all characters that part and whole have in common
1113 match = (part[pos] == whole[pos]);
1114 } while (match && ++pos < part.Length);
1116 // return result of last comparison
1117 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1121 sealed class StartsWithComparatorPartWholeCheck : IComparer
1123 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1125 public int Compare(object p, object w)
1127 string part = (string) p;
1128 string whole = (string) w;
1129 Assert.IsTrue(part == "Part", "#PWC0");
1130 Assert.IsTrue(whole == "Whole", "#PWC1");
1132 // let the default string comparer deal with null or when part is not smaller then whole
1133 if (part == null || whole == null || part.Length >= whole.Length)
1134 return String.Compare(part, whole);
1136 // loop through all characters that part and whole have in common
1140 match = (part[pos] == whole[pos]);
1141 } while (match && ++pos < part.Length);
1143 // return result of last comparison
1144 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1149 public void ComparatorUsageTest()
1151 SortedList sl = new SortedList(StartsWithComparator.Instance);
1153 sl.Add("Apples", "Value-Apples");
1154 sl.Add("Bananas", "Value-Bananas");
1155 sl.Add("Oranges", "Value-Oranges");
1157 // Ensure 3 objects exist in the collection
1158 Assert.IsTrue(sl.Count == 3, "Count");
1160 // Complete Match Test Set
1161 Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
1162 Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
1163 Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
1165 // Partial Match Test Set
1166 Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
1167 Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
1168 Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
1170 // Reversed Match Test Set
1171 Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
1174 Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
1175 Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
1176 Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
1181 public void ComparatorPartWholeCheck()
1183 SortedList sl = new SortedList (StartsWithComparatorPartWholeCheck.Instance);
1184 sl.Add("Part", "Value-Part");
1185 Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
1189 public void NonComparatorStringCheck()
1191 SortedList sl = new SortedList ();
1193 sl.Add("Oranges", "Value-Oranges");
1194 sl.Add("Apples", "Value-Apples");
1195 sl.Add("Bananas", "Value-Bananas");
1198 Assert.IsTrue(sl.Count == 3, "NCSC #A0");
1200 Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
1201 Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
1202 Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
1204 Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
1205 Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
1206 Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
1208 string [] keys = new string [sl.Keys.Count];
1209 sl.Keys.CopyTo (keys, 0);
1210 Assert.IsTrue(keys [0] == "Apples", "NCSC #D1");
1211 Assert.IsTrue(keys [1] == "Bananas", "NCSC #D2");
1212 Assert.IsTrue(keys [2] == "Oranges", "NCSC #D3");
1215 private static byte [] _serializedValues = new byte [] {
1216 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
1217 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
1218 0x01, 0x00, 0x00, 0x00, 0x27, 0x53, 0x79, 0x73, 0x74,
1219 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63,
1220 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72,
1221 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56,
1222 0x61, 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x01,
1223 0x00, 0x00, 0x00, 0x0a, 0x73, 0x6f, 0x72, 0x74, 0x65,
1224 0x64, 0x4c, 0x69, 0x73, 0x74, 0x03, 0x1d, 0x53, 0x79,
1225 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c,
1226 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53,
1227 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74,
1228 0x09, 0x02, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00,
1229 0x00, 0x1d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1230 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1231 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1232 0x4c, 0x69, 0x73, 0x74, 0x07, 0x00, 0x00, 0x00, 0x04,
1233 0x6b, 0x65, 0x79, 0x73, 0x06, 0x76, 0x61, 0x6c, 0x75,
1234 0x65, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x07,
1235 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x63,
1236 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x07, 0x6b,
1237 0x65, 0x79, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x76, 0x61,
1238 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x05, 0x05,
1239 0x00, 0x00, 0x03, 0x03, 0x03, 0x08, 0x08, 0x1b, 0x53,
1240 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
1241 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
1242 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x25,
1243 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
1244 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
1245 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69,
1246 0x73, 0x74, 0x2b, 0x4b, 0x65, 0x79, 0x4c, 0x69, 0x73,
1247 0x74, 0x27, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1248 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1249 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1250 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56, 0x61, 0x6c, 0x75,
1251 0x65, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x03, 0x00, 0x00,
1252 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
1253 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00,
1254 0x00, 0x0a, 0x09, 0x01, 0x00, 0x00, 0x00, 0x10, 0x03,
1255 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x08, 0x08,
1256 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x05, 0x00, 0x00,
1257 0x00, 0x08, 0x08, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x0d,
1258 0x10, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
1259 0x06, 0x07, 0x00, 0x00, 0x00, 0x01, 0x42, 0x06, 0x08,
1260 0x00, 0x00, 0x00, 0x01, 0x41, 0x06, 0x09, 0x00, 0x00,
1261 0x00, 0x01, 0x43, 0x0d, 0x0d, 0x04, 0x05, 0x00, 0x00,
1262 0x00, 0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1263 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1264 0x6e, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
1265 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x0b };