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");
201 [ExpectedException (typeof (OutOfMemoryException))]
202 public void TestCapacity4 ()
204 SortedList list = new SortedList ();
205 list.Capacity = Int32.MaxValue;
209 public void TestCount ()
211 SortedList sl1 = new SortedList ();
212 Assert.AreEqual (0, sl1.Count, "#1");
213 for (int i = 1; i <= 100; i++) {
214 sl1.Add ("" + i, "" + i);
215 Assert.AreEqual (i, sl1.Count, "#2:" + i);
220 public void TestIsFixed ()
222 SortedList sl1 = new SortedList ();
223 Assert.IsFalse (sl1.IsFixedSize);
227 public void TestIsReadOnly ()
229 SortedList sl1 = new SortedList ();
230 Assert.IsFalse (sl1.IsReadOnly);
234 public void TestItem ()
236 SortedList sl1 = new SortedList ();
239 Assert.IsNull (o, "#A");
242 o = sl1 [(string) null];
244 } catch (ArgumentNullException) {
247 for (int i = 0; i <= 100; i++)
248 sl1.Add ("kala " + i, i);
249 for (int i = 0; i <= 100; i++)
250 Assert.AreEqual (i, sl1 ["kala " + i], "#C:" + i);
254 public void TestSyncRoot ()
256 SortedList sl1 = new SortedList ();
257 Assert.IsNotNull (sl1.SyncRoot);
259 lock( sl1.SyncRoot ) {
260 foreach ( Object item in sl1 ) {
262 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
269 public void TestValues ()
271 SortedList sl1 = new SortedList ();
272 ICollection ic1 = sl1.Values;
273 for (int i = 0; i <= 100; i++) {
274 sl1.Add ("kala " + i, i);
275 Assert.AreEqual (ic1.Count, sl1.Count);
280 // TODO: Add with IComparer
282 public void TestAdd ()
284 // seems SortedList cannot be set fixedsize or readonly
285 SortedList sl1 = new SortedList ();
288 sl1.Add ((string) null, "kala");
290 } catch (ArgumentNullException) {
293 for (int i = 1; i <= 100; i++) {
294 sl1.Add ("kala " + i, i);
295 Assert.AreEqual (i, sl1.Count, "#B1:" + i);
296 Assert.AreEqual (i, sl1 ["kala " + i], "#B2:" + i);
300 sl1.Add ("kala", 10);
301 sl1.Add ("kala", 11);
303 } catch (ArgumentException) {
308 public void TestClear ()
310 SortedList sl1 = new SortedList (10);
311 sl1.Add ("kala", 'c');
312 sl1.Add ("kala2", 'd');
313 Assert.AreEqual (10, sl1.Capacity, "#A1");
314 Assert.AreEqual (2, sl1.Count, "#A2");
316 Assert.AreEqual (0, sl1.Count, "#B1");
317 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
318 Assert.AreEqual (16, sl1.Capacity, "#B2");
322 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
324 public void Clear_Capacity ()
326 // strangely Clear change the default capacity (while Capacity doesn't)
327 for (int i = 0; i <= 16; i++) {
328 SortedList sl = new SortedList (i);
329 Assert.AreEqual (i, sl.Capacity, "#1:"+ i);
331 // reset to class default (16)
332 Assert.AreEqual (16, sl.Capacity, "#2:" + i);
337 public void Clear_Capacity_Reset ()
339 SortedList sl = new SortedList (0);
340 Assert.AreEqual (0, sl.Capacity, "#1");
342 // reset to class default (16)
343 Assert.AreEqual (16, sl.Capacity, "#2");
345 Assert.AreEqual (16, sl.Capacity, "#3");
346 // note: we didn't return to 0 - so Clear cahnge the default capacity
351 public void TestClone ()
354 SortedList sl1 = new SortedList (10);
355 for (int i = 0; i <= 50; i++)
356 sl1.Add ("kala " + i, i);
357 SortedList sl2 = (SortedList) sl1.Clone ();
358 for (int i = 0; i <= 50; i++)
359 Assert.AreEqual (sl1 ["kala " + i], sl2 ["kala " + i], "#A:" + i);
362 char [] d10 = { 'a', 'b' };
363 char [] d11 = { 'a', 'c' };
364 char [] d12 = { 'b', 'c' };
365 //char[][] d1 = {d10, d11, d12};
366 SortedList sl1 = new SortedList ();
370 SortedList sl2 = (SortedList) sl1.Clone ();
371 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B1");
372 Assert.AreEqual (sl1 ["d2"], sl2 ["d2"], "#B2");
373 Assert.AreEqual (sl1 ["d3"], sl2 ["d3"], "#B3");
374 ((char []) sl1 ["d1"]) [0] = 'z';
375 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B4");
380 public void TestContains ()
382 SortedList sl1 = new SortedList (55);
383 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
386 if (sl1.Contains (null)) {
389 } catch (ArgumentNullException) {
392 Assert.IsTrue (sl1.Contains ("kala 17"), "#B1");
393 Assert.IsFalse (sl1.Contains ("ohoo"), "#B2");
397 public void TestContainsKey ()
399 SortedList sl1 = new SortedList (55);
400 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
403 if (sl1.ContainsKey (null)) {
406 } catch (ArgumentNullException) {
409 Assert.IsTrue (sl1.ContainsKey ("kala 17"), "#B1");
410 Assert.IsFalse (sl1.ContainsKey ("ohoo"), "#B2");
414 public void TestContainsValue ()
416 SortedList sl1 = new SortedList (55);
420 sl1.Add (3, "three");
423 Assert.IsTrue (sl1.ContainsValue ("zero"), "#1");
424 Assert.IsFalse (sl1.ContainsValue ("ohoo"), "#2");
425 Assert.IsFalse (sl1.ContainsValue (null), "#3");
429 public void TestCopyTo ()
431 SortedList sl1 = new SortedList ();
432 for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
435 sl1.CopyTo (null, 2);
436 Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
437 } catch (ArgumentNullException) {
442 Char [,] c2 = new Char [2, 2];
444 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
445 } catch (ArgumentException) {
450 Char [] c1 = new Char [2];
452 Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
453 } catch (ArgumentOutOfRangeException) {
458 Char [] c1 = new Char [2];
460 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
461 } catch (ArgumentException) {
466 Char [] c1 = new Char [2];
468 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
469 } catch (ArgumentException) {
474 Char [] c2 = new Char [15];
476 Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
477 } catch (InvalidCastException) {
481 // CopyTo function does not work well with SortedList
482 // even example at MSDN gave InvalidCastException
483 // thus, it is NOT tested here
486 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
487 Char[] copy = new Char[15];
488 Array.Clear(copy,0,copy.Length);
489 copy.SetValue( "The", 0 );
490 copy.SetValue( "quick", 1 );
491 copy.SetValue( "brown", 2 );
492 copy.SetValue( "fox", 3 );
493 copy.SetValue( "jumped", 4 );
494 copy.SetValue( "over", 5 );
495 copy.SetValue( "the", 6 );
496 copy.SetValue( "lazy", 7 );
497 copy.SetValue( "dog", 8 );
499 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
500 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
501 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
502 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
506 public SortedList DefaultSL ()
508 SortedList sl1 = new SortedList ();
509 sl1.Add (1.0, "The");
510 sl1.Add (1.1, "quick");
511 sl1.Add (34.0, "brown");
512 sl1.Add (-100.75, "fox");
513 sl1.Add (1.4, "jumped");
514 sl1.Add (1.5, "over");
515 sl1.Add (1.6, "the");
516 sl1.Add (1.7, "lazy");
517 sl1.Add (1.8, "dog");
521 public IList DefaultValues ()
523 IList il = new ArrayList ();
537 public void TestGetByIndex ()
539 SortedList sl1 = DefaultSL ();
540 Assert.AreEqual ("over", sl1.GetByIndex (4), "#A1");
541 Assert.AreEqual ("brown", sl1.GetByIndex (8), "#A2");
546 } catch (ArgumentOutOfRangeException) {
550 sl1.GetByIndex (100);
552 } catch (ArgumentOutOfRangeException) {
557 public void GetEnumerator ()
559 SortedList sl1 = DefaultSL ();
560 IDictionaryEnumerator e = sl1.GetEnumerator ();
561 Assert.IsNotNull (e, "#1");
562 Assert.IsTrue (e.MoveNext (), "#2");
563 Assert.IsNotNull (e.Current, "#3");
564 Assert.IsTrue ((e is ICloneable), "#4");
565 Assert.IsTrue ((e is IDictionaryEnumerator), "#5");
566 Assert.IsTrue ((e is IEnumerator), "#6");
570 public void TestGetKey ()
572 SortedList sl1 = DefaultSL ();
573 Assert.AreEqual (1.5, sl1.GetKey (4), "#A1");
574 Assert.AreEqual (34.0, sl1.GetKey (8), "#A2");
579 } catch (ArgumentOutOfRangeException) {
585 } catch (ArgumentOutOfRangeException) {
590 public void TestGetKeyList ()
592 SortedList sl1 = DefaultSL ();
593 IList keys = sl1.GetKeyList ();
594 Assert.IsNotNull (keys, "#A1");
595 Assert.IsTrue (keys.IsReadOnly, "#A2");
596 Assert.AreEqual (9, keys.Count, "#A3");
597 Assert.AreEqual (1.4, keys [3], "#A4");
598 sl1.Add (33.9, "ehhe");
599 Assert.AreEqual (10, keys.Count, "#B1");
600 Assert.AreEqual (33.9, keys [8], "#B2");
604 public void TestGetValueList ()
606 SortedList sl1 = DefaultSL ();
607 IList originalvals = DefaultValues ();
608 IList vals = sl1.GetValueList ();
609 Assert.IsNotNull (vals, "#A1");
610 Assert.IsTrue (vals.IsReadOnly, "#A2");
611 Assert.AreEqual (vals.Count, sl1.Count, "#A3");
613 for (int i = 0; i < sl1.Count; i++) {
614 Assert.AreEqual (vals [i], originalvals [i], "#A4:" + i);
617 sl1.Add (0.01, "ehhe");
618 Assert.AreEqual (10, vals.Count, "#B1");
619 Assert.AreEqual ("dog", vals [8], "#B2");
622 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
624 public void TestIEnumerable_GetEnumerator() {
625 SortedList sl1 = DefaultSL();
626 IEnumerator e = sl1.IEnumerable.GetEnumerator();
627 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
628 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
629 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
634 public void TestIndexOfKey ()
636 SortedList sl1 = new SortedList (24);
638 for (int i = 0; i <= 50; i++) {
639 string s = string.Format ("{0:D2}", i);
640 sl1.Add ("kala " + s, i);
642 Assert.AreEqual (-1, sl1.IndexOfKey ("kala "), "#A");
645 sl1.IndexOfKey ((string) null);
647 } catch (ArgumentNullException) {
653 } catch (InvalidOperationException) {
656 for (int i = 0; i <= 50; i++) {
657 string s = string.Format ("{0:D2}", i);
658 Assert.AreEqual (i, sl1.IndexOfKey ("kala " + s), "#D:" + i);
663 public void TestIndexOfValue ()
665 SortedList sl1 = new SortedList (24);
667 for (int i = 0; i < 50; i++) {
668 s = string.Format ("{0:D2}", i);
669 sl1.Add ("kala " + s, 100 + i * i);
671 for (int i = 0; i < 50; i++) {
672 s = string.Format ("{0:D2}", i + 50);
673 sl1.Add ("kala " + s, 100 + i * i);
675 Assert.AreEqual (-1, sl1.IndexOfValue (102), "#1");
676 Assert.AreEqual (-1, sl1.IndexOfValue (null), "#2");
677 for (int i = 0; i < 50; i++)
678 Assert.AreEqual (i, sl1.IndexOfValue (100 + i * i), "#3:" + i);
682 public void TestIndexOfValue2 ()
684 SortedList list = new SortedList ();
685 list.Add ("key0", "la la");
686 list.Add ("key1", "value");
687 list.Add ("key2", "value");
689 int i = list.IndexOfValue ("value");
690 Assert.AreEqual (1, i);
694 public void TestIndexOfValue3 ()
696 SortedList list = new SortedList ();
697 int i = list.IndexOfValue ((string) null);
698 Assert.AreEqual (1, -i);
702 public void TestIndexer ()
704 SortedList list = new SortedList ();
706 list.Add (1, new Queue ());
707 list.Add (2, new Hashtable ());
708 list.Add (3, new Stack ());
710 Assert.AreEqual (typeof (Queue), list [1].GetType (), "#1");
711 Assert.AreEqual (typeof (Hashtable), list [2].GetType (), "#2");
712 Assert.AreEqual (typeof (Stack), list [3].GetType (), "#3");
716 public void TestEnumerator ()
718 SortedList list = new SortedList ();
720 list.Add (1, new Queue ());
721 list.Add (2, new Hashtable ());
722 list.Add (3, new Stack ());
724 foreach (DictionaryEntry d in list) {
726 int key = (int) d.Key;
727 Type value = d.Value.GetType ();
731 Assert.AreEqual (typeof (Queue), value, "#1");
735 Assert.AreEqual (typeof (Hashtable), value, "#2");
739 Assert.AreEqual (typeof (Stack), value, "#3");
743 Assert.Fail ("#4:" + value.FullName);
750 public void TestRemove ()
752 SortedList sl1 = new SortedList (24);
755 for (int i = 0; i < 50; i++) sl1.Add ("kala " + i, i);
760 } catch (ArgumentNullException) {
764 sl1.Remove ("kala ");
765 Assert.AreEqual (k, sl1.Count, "#B");
770 } catch (InvalidOperationException) {
773 for (int i = 15; i < 20; i++)
774 sl1.Remove ("kala " + i);
775 for (int i = 45; i < 55; i++)
776 sl1.Remove ("kala " + i);
778 Assert.AreEqual (40, sl1.Count, "#D1");
779 for (int i = 45; i < 55; i++)
780 Assert.IsNull (sl1 ["kala " + i], "#D2:" + i);
784 public void TestRemoveAt ()
786 SortedList sl1 = new SortedList (24);
789 for (int i = 0; i < 50; i++) {
790 string s = string.Format ("{0:D2}", i);
791 sl1.Add ("kala " + s, i);
797 } catch (ArgumentOutOfRangeException) {
803 } catch (ArgumentOutOfRangeException) {
808 for (int i = 0; i < 20; i++)
811 Assert.AreEqual (30, sl1.Count, 30, "#C1");
812 for (int i = 0; i < 9; i++)
813 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C2:" + i);
814 for (int i = 9; i < 29; i++)
815 Assert.IsNull (sl1 ["kala " + string.Format ("{0:D2}", i)], "#C3:" + i);
816 for (int i = 29; i < 50; i++)
817 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C4:" + i);
821 public void TestSetByIndex ()
823 SortedList sl1 = new SortedList (24);
824 for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i);
827 sl1.SetByIndex (-1, 77);
829 } catch (ArgumentOutOfRangeException) {
833 sl1.SetByIndex (100, 88);
835 } catch (ArgumentOutOfRangeException) {
838 for (int i = 5; i < 25; i++)
839 sl1.SetByIndex (i, -1);
840 for (int i = 0; i < 5; i++)
841 Assert.AreEqual (i, sl1 [100 + i], "#C1");
842 for (int i = 5; i < 25; i++)
843 Assert.AreEqual (-1, sl1 [100 + i], "#C2");
844 for (int i = 25; i < 50; i++)
845 Assert.AreEqual (i, sl1 [100 + i], "#C3");
849 public void TestTrimToSize ()
851 SortedList sl1 = new SortedList (24);
854 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
855 Assert.AreEqual (icap, sl1.Capacity, "#1");
858 for (int i = 72; i >= 0; i--)
859 sl1.Add (100 + i, i);
861 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
862 Assert.AreEqual (73, sl1.Capacity, "#2");
867 [Category ("TargetJvmNotWorking")]
868 public void SerializeTest ()
870 SortedList sl1 = new SortedList ();
876 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
878 BinaryFormatter bf = new BinaryFormatter ();
880 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
881 MemoryStream ms = new MemoryStream ();
882 bf.Serialize (ms, sl1);
885 SortedList sl2 = (SortedList) bf.Deserialize (ms);
886 Assert.IsNotNull (sl2, "#1");
887 Assert.AreEqual (3, sl2.Count, "#2");
888 Assert.AreEqual (sl1 [0], sl2 [0], "#3");
889 Assert.AreEqual (sl1 [1], sl2 [1], "#4");
890 Assert.AreEqual (sl1 [2], sl2 [2], "#5");
894 [Category ("TargetJvmNotWorking")]
895 public void Keys_Serialize ()
897 SortedList sl = new SortedList ();
902 IList keys1 = (IList) sl.Keys;
904 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
906 BinaryFormatter bf = new BinaryFormatter ();
908 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
909 MemoryStream ms = new MemoryStream ();
910 bf.Serialize (ms, keys1);
913 IList keys2 = (IList) bf.Deserialize (ms);
914 Assert.IsNotNull (keys2, "#1");
915 Assert.AreEqual (3, keys2.Count, "#2");
916 Assert.AreEqual (keys1 [0], keys2 [0], "#3");
917 Assert.AreEqual (keys1 [1], keys2 [1], "#4");
918 Assert.AreEqual (keys1 [2], keys2 [2], "#5");
922 [Category ("TargetJvmNotWorking")]
923 public void Values_Serialize ()
925 SortedList sl = new SortedList ();
930 IList values1 = (IList) sl.Values;
932 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
934 BinaryFormatter bf = new BinaryFormatter ();
936 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
937 MemoryStream ms = new MemoryStream ();
938 bf.Serialize (ms, values1);
941 IList values2 = (IList) bf.Deserialize (ms);
942 Assert.IsNotNull (values2, "#1");
943 Assert.AreEqual (3, values2.Count, "#2");
944 Assert.AreEqual (values1 [0], values2 [0], "#3");
945 Assert.AreEqual (values1 [1], values2 [1], "#4");
946 Assert.AreEqual (values1 [2], values2 [2], "#5");
950 [Category ("NotWorking")]
951 public void Values_Deserialize ()
954 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
956 BinaryFormatter bf = new BinaryFormatter ();
959 MemoryStream ms = new MemoryStream ();
960 ms.Write (_serializedValues, 0, _serializedValues.Length);
963 IList values = (IList) bf.Deserialize (ms);
964 Assert.AreEqual (3, values.Count, "#1");
965 Assert.AreEqual ("B", values [0], "#2");
966 Assert.AreEqual ("A", values [1], "#3");
967 Assert.AreEqual ("C", values [2], "#4");
971 [ExpectedException (typeof (InvalidOperationException))]
972 public void SetIdenticalObjectException ()
974 // Even though the key/value pair being set are identical to
975 // the existing one, it causes snapshot out of sync.
976 SortedList sl = new SortedList ();
978 foreach (string s in sl.Keys)
983 public void Ctor_IComparer ()
985 SortedList sl = new SortedList (new SortedListComparer ());
986 sl.Add (new object (), new object ());
990 public void Ctor_IComparer_Null ()
992 SortedList sl = new SortedList ((IComparer) null);
993 sl.Add (new object (), new object ());
997 public void Ctor_IDictionary_IComparer_Before ()
999 Hashtable ht = new Hashtable ();
1002 // adding a non-IComparable in Hashtable
1003 ht.Add (new object (), "c");
1004 SortedList sl = new SortedList (ht, new SortedListComparer ());
1005 Assert.AreEqual (3, sl.Count);
1009 [ExpectedException (typeof (InvalidOperationException))]
1010 public void Ctor_IDictionary_DefaultInvariant_Before ()
1012 Hashtable ht = new Hashtable ();
1015 // adding a non-IComparable in Hashtable
1016 ht.Add (new object (), "c");
1017 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1018 Assert.AreEqual (3, sl.Count);
1022 public void Ctor_IDictionary_IComparer_Null_Before_1item ()
1024 Hashtable ht = new Hashtable ();
1025 // adding a non-IComparable in Hashtable
1026 ht.Add (new object (), "c");
1027 SortedList sl = new SortedList (ht, null);
1028 Assert.AreEqual (1, sl.Count);
1032 [ExpectedException (typeof (InvalidOperationException))]
1033 public void Ctor_IDictionary_IComparer_Null_Before_2items ()
1035 Hashtable ht = new Hashtable ();
1037 // adding a non-IComparable in Hashtable
1038 ht.Add (new object (), "c");
1039 SortedList sl = new SortedList (ht, null);
1040 Assert.AreEqual (2, sl.Count);
1044 public void Ctor_IDictionary_IComparer_After ()
1046 Hashtable ht = new Hashtable ();
1049 SortedList sl = new SortedList (ht, new SortedListComparer ());
1050 Assert.AreEqual (2, sl.Count);
1051 // adding a non-IComparable in SortedList
1052 sl.Add (new object (), "c");
1056 [ExpectedException (typeof (InvalidOperationException))]
1057 public void Ctor_IDictionary_DefaultInvariant_After ()
1059 Hashtable ht = new Hashtable ();
1062 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1063 Assert.AreEqual (2, sl.Count);
1064 // adding a non-IComparable in SortedList
1065 sl.Add (new object (), "c");
1069 public void Ctor_IDictionary_IComparer_Null_After_1item ()
1071 SortedList sl = new SortedList (new Hashtable (), null);
1072 sl.Add (new object (), "b");
1076 [ExpectedException (typeof (InvalidOperationException))]
1077 public void Ctor_IDictionary_IComparer_Null_After_2items ()
1079 SortedList sl = new SortedList (new Hashtable (), null);
1081 sl.Add (new object (), "b");
1085 public void IComparer_Clone ()
1087 SortedList sl = new SortedList (new SortedListComparer ());
1088 sl.Add (new object (), new object ());
1089 SortedList clone = (SortedList) sl.Clone ();
1093 public void IComparer_Null_Clone ()
1095 SortedList sl = new SortedList ((IComparer) null);
1096 sl.Add (new object (), new object ());
1097 SortedList clone = (SortedList) sl.Clone ();
1100 sealed class StartsWithComparator : IComparer {
1101 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1103 public int Compare(object p, object w)
1105 string part = (string) p;
1106 string whole = (string) w;
1107 // let the default string comparer deal with null or when part is not smaller then whole
1108 if (part == null || whole == null || part.Length >= whole.Length)
1109 return String.Compare (part, whole);
1111 // loop through all characters that part and whole have in common
1115 match = (part[pos] == whole[pos]);
1116 } while (match && ++pos < part.Length);
1118 // return result of last comparison
1119 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1123 sealed class StartsWithComparatorPartWholeCheck : IComparer
1125 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1127 public int Compare(object p, object w)
1129 string part = (string) p;
1130 string whole = (string) w;
1131 Assert.IsTrue(part == "Part", "#PWC0");
1132 Assert.IsTrue(whole == "Whole", "#PWC1");
1134 // let the default string comparer deal with null or when part is not smaller then whole
1135 if (part == null || whole == null || part.Length >= whole.Length)
1136 return String.Compare(part, whole);
1138 // loop through all characters that part and whole have in common
1142 match = (part[pos] == whole[pos]);
1143 } while (match && ++pos < part.Length);
1145 // return result of last comparison
1146 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1151 public void ComparatorUsageTest()
1153 SortedList sl = new SortedList(StartsWithComparator.Instance);
1155 sl.Add("Apples", "Value-Apples");
1156 sl.Add("Bananas", "Value-Bananas");
1157 sl.Add("Oranges", "Value-Oranges");
1159 // Ensure 3 objects exist in the collection
1160 Assert.IsTrue(sl.Count == 3, "Count");
1162 // Complete Match Test Set
1163 Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
1164 Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
1165 Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
1167 // Partial Match Test Set
1168 Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
1169 Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
1170 Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
1172 // Reversed Match Test Set
1173 Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
1176 Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
1177 Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
1178 Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
1183 public void ComparatorPartWholeCheck()
1185 SortedList sl = new SortedList (StartsWithComparatorPartWholeCheck.Instance);
1186 sl.Add("Part", "Value-Part");
1187 Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
1191 public void NonComparatorStringCheck()
1193 SortedList sl = new SortedList ();
1195 sl.Add("Oranges", "Value-Oranges");
1196 sl.Add("Apples", "Value-Apples");
1197 sl.Add("Bananas", "Value-Bananas");
1200 Assert.IsTrue(sl.Count == 3, "NCSC #A0");
1202 Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
1203 Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
1204 Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
1206 Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
1207 Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
1208 Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
1210 string [] keys = new string [sl.Keys.Count];
1211 sl.Keys.CopyTo (keys, 0);
1212 Assert.IsTrue(keys [0] == "Apples", "NCSC #D1");
1213 Assert.IsTrue(keys [1] == "Bananas", "NCSC #D2");
1214 Assert.IsTrue(keys [2] == "Oranges", "NCSC #D3");
1217 private static byte [] _serializedValues = new byte [] {
1218 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
1219 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
1220 0x01, 0x00, 0x00, 0x00, 0x27, 0x53, 0x79, 0x73, 0x74,
1221 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63,
1222 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72,
1223 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56,
1224 0x61, 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x01,
1225 0x00, 0x00, 0x00, 0x0a, 0x73, 0x6f, 0x72, 0x74, 0x65,
1226 0x64, 0x4c, 0x69, 0x73, 0x74, 0x03, 0x1d, 0x53, 0x79,
1227 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c,
1228 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53,
1229 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74,
1230 0x09, 0x02, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00,
1231 0x00, 0x1d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1232 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1233 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1234 0x4c, 0x69, 0x73, 0x74, 0x07, 0x00, 0x00, 0x00, 0x04,
1235 0x6b, 0x65, 0x79, 0x73, 0x06, 0x76, 0x61, 0x6c, 0x75,
1236 0x65, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x07,
1237 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x63,
1238 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x07, 0x6b,
1239 0x65, 0x79, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x76, 0x61,
1240 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x05, 0x05,
1241 0x00, 0x00, 0x03, 0x03, 0x03, 0x08, 0x08, 0x1b, 0x53,
1242 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
1243 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
1244 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x25,
1245 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
1246 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
1247 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69,
1248 0x73, 0x74, 0x2b, 0x4b, 0x65, 0x79, 0x4c, 0x69, 0x73,
1249 0x74, 0x27, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1250 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1251 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1252 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56, 0x61, 0x6c, 0x75,
1253 0x65, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x03, 0x00, 0x00,
1254 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
1255 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00,
1256 0x00, 0x0a, 0x09, 0x01, 0x00, 0x00, 0x00, 0x10, 0x03,
1257 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x08, 0x08,
1258 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x05, 0x00, 0x00,
1259 0x00, 0x08, 0x08, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x0d,
1260 0x10, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
1261 0x06, 0x07, 0x00, 0x00, 0x00, 0x01, 0x42, 0x06, 0x08,
1262 0x00, 0x00, 0x00, 0x01, 0x41, 0x06, 0x09, 0x00, 0x00,
1263 0x00, 0x01, 0x43, 0x0d, 0x0d, 0x04, 0x05, 0x00, 0x00,
1264 0x00, 0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1265 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1266 0x6e, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
1267 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x0b };