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;
190 Assert.AreEqual (new_capacity, list.Capacity);
192 Assert.AreEqual (16, list.Capacity);
197 public void Capacity_BackTo0 ()
199 SortedList list = new SortedList (42);
200 Assert.AreEqual (42, list.Capacity, "#1");
202 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
203 Assert.AreEqual (16, list.Capacity, "#2");
208 [ExpectedException (typeof (OutOfMemoryException))]
209 public void TestCapacity4 ()
211 SortedList list = new SortedList ();
212 list.Capacity = Int32.MaxValue;
216 public void TestCount ()
218 SortedList sl1 = new SortedList ();
219 Assert.AreEqual (0, sl1.Count, "#1");
220 for (int i = 1; i <= 100; i++) {
221 sl1.Add ("" + i, "" + i);
222 Assert.AreEqual (i, sl1.Count, "#2:" + i);
227 public void TestIsFixed ()
229 SortedList sl1 = new SortedList ();
230 Assert.IsFalse (sl1.IsFixedSize);
234 public void TestIsReadOnly ()
236 SortedList sl1 = new SortedList ();
237 Assert.IsFalse (sl1.IsReadOnly);
241 public void TestItem ()
243 SortedList sl1 = new SortedList ();
246 Assert.IsNull (o, "#A");
249 o = sl1 [(string) null];
251 } catch (ArgumentNullException) {
254 for (int i = 0; i <= 100; i++)
255 sl1.Add ("kala " + i, i);
256 for (int i = 0; i <= 100; i++)
257 Assert.AreEqual (i, sl1 ["kala " + i], "#C:" + i);
261 public void TestSyncRoot ()
263 SortedList sl1 = new SortedList ();
264 Assert.IsNotNull (sl1.SyncRoot);
266 lock( sl1.SyncRoot ) {
267 foreach ( Object item in sl1 ) {
269 Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
276 public void TestValues ()
278 SortedList sl1 = new SortedList ();
279 ICollection ic1 = sl1.Values;
280 for (int i = 0; i <= 100; i++) {
281 sl1.Add ("kala " + i, i);
282 Assert.AreEqual (ic1.Count, sl1.Count);
287 // TODO: Add with IComparer
289 public void TestAdd ()
291 // seems SortedList cannot be set fixedsize or readonly
292 SortedList sl1 = new SortedList ();
295 sl1.Add ((string) null, "kala");
297 } catch (ArgumentNullException) {
300 for (int i = 1; i <= 100; i++) {
301 sl1.Add ("kala " + i, i);
302 Assert.AreEqual (i, sl1.Count, "#B1:" + i);
303 Assert.AreEqual (i, sl1 ["kala " + i], "#B2:" + i);
307 sl1.Add ("kala", 10);
308 sl1.Add ("kala", 11);
310 } catch (ArgumentException) {
315 public void TestClear ()
317 SortedList sl1 = new SortedList (10);
318 sl1.Add ("kala", 'c');
319 sl1.Add ("kala2", 'd');
320 Assert.AreEqual (10, sl1.Capacity, "#A1");
321 Assert.AreEqual (2, sl1.Count, "#A2");
323 Assert.AreEqual (0, sl1.Count, "#B1");
324 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
325 Assert.AreEqual (16, sl1.Capacity, "#B2");
329 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
331 public void Clear_Capacity ()
333 // strangely Clear change the default capacity (while Capacity doesn't)
334 for (int i = 0; i <= 16; i++) {
335 SortedList sl = new SortedList (i);
336 Assert.AreEqual (i, sl.Capacity, "#1:"+ i);
338 // reset to class default (16)
339 Assert.AreEqual (16, sl.Capacity, "#2:" + i);
344 public void Clear_Capacity_Reset ()
346 SortedList sl = new SortedList (0);
347 Assert.AreEqual (0, sl.Capacity, "#1");
349 // reset to class default (16)
350 Assert.AreEqual (16, sl.Capacity, "#2");
352 Assert.AreEqual (16, sl.Capacity, "#3");
353 // note: we didn't return to 0 - so Clear cahnge the default capacity
358 public void TestClone ()
361 SortedList sl1 = new SortedList (10);
362 for (int i = 0; i <= 50; i++)
363 sl1.Add ("kala " + i, i);
364 SortedList sl2 = (SortedList) sl1.Clone ();
365 for (int i = 0; i <= 50; i++)
366 Assert.AreEqual (sl1 ["kala " + i], sl2 ["kala " + i], "#A:" + i);
369 char [] d10 = { 'a', 'b' };
370 char [] d11 = { 'a', 'c' };
371 char [] d12 = { 'b', 'c' };
372 //char[][] d1 = {d10, d11, d12};
373 SortedList sl1 = new SortedList ();
377 SortedList sl2 = (SortedList) sl1.Clone ();
378 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B1");
379 Assert.AreEqual (sl1 ["d2"], sl2 ["d2"], "#B2");
380 Assert.AreEqual (sl1 ["d3"], sl2 ["d3"], "#B3");
381 ((char []) sl1 ["d1"]) [0] = 'z';
382 Assert.AreEqual (sl1 ["d1"], sl2 ["d1"], "#B4");
387 public void TestContains ()
389 SortedList sl1 = new SortedList (55);
390 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
393 if (sl1.Contains (null)) {
396 } catch (ArgumentNullException) {
399 Assert.IsTrue (sl1.Contains ("kala 17"), "#B1");
400 Assert.IsFalse (sl1.Contains ("ohoo"), "#B2");
404 public void TestContainsKey ()
406 SortedList sl1 = new SortedList (55);
407 for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
410 if (sl1.ContainsKey (null)) {
413 } catch (ArgumentNullException) {
416 Assert.IsTrue (sl1.ContainsKey ("kala 17"), "#B1");
417 Assert.IsFalse (sl1.ContainsKey ("ohoo"), "#B2");
421 public void TestContainsValue ()
423 SortedList sl1 = new SortedList (55);
427 sl1.Add (3, "three");
430 Assert.IsTrue (sl1.ContainsValue ("zero"), "#1");
431 Assert.IsFalse (sl1.ContainsValue ("ohoo"), "#2");
432 Assert.IsFalse (sl1.ContainsValue (null), "#3");
436 public void TestCopyTo ()
438 SortedList sl1 = new SortedList ();
439 for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
442 sl1.CopyTo (null, 2);
443 Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
444 } catch (ArgumentNullException) {
449 Char [,] c2 = new Char [2, 2];
451 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
452 } catch (ArgumentException) {
457 Char [] c1 = new Char [2];
459 Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
460 } catch (ArgumentOutOfRangeException) {
465 Char [] c1 = new Char [2];
467 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
468 } catch (ArgumentException) {
473 Char [] c1 = new Char [2];
475 Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
476 } catch (ArgumentException) {
481 Char [] c2 = new Char [15];
483 Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
484 } catch (InvalidCastException) {
488 // CopyTo function does not work well with SortedList
489 // even example at MSDN gave InvalidCastException
490 // thus, it is NOT tested here
493 for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
494 Char[] copy = new Char[15];
495 Array.Clear(copy,0,copy.Length);
496 copy.SetValue( "The", 0 );
497 copy.SetValue( "quick", 1 );
498 copy.SetValue( "brown", 2 );
499 copy.SetValue( "fox", 3 );
500 copy.SetValue( "jumped", 4 );
501 copy.SetValue( "over", 5 );
502 copy.SetValue( "the", 6 );
503 copy.SetValue( "lazy", 7 );
504 copy.SetValue( "dog", 8 );
506 AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
507 AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
508 for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
509 AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
513 public SortedList DefaultSL ()
515 SortedList sl1 = new SortedList ();
516 sl1.Add (1.0, "The");
517 sl1.Add (1.1, "quick");
518 sl1.Add (34.0, "brown");
519 sl1.Add (-100.75, "fox");
520 sl1.Add (1.4, "jumped");
521 sl1.Add (1.5, "over");
522 sl1.Add (1.6, "the");
523 sl1.Add (1.7, "lazy");
524 sl1.Add (1.8, "dog");
528 public IList DefaultValues ()
530 IList il = new ArrayList ();
544 public void TestGetByIndex ()
546 SortedList sl1 = DefaultSL ();
547 Assert.AreEqual ("over", sl1.GetByIndex (4), "#A1");
548 Assert.AreEqual ("brown", sl1.GetByIndex (8), "#A2");
553 } catch (ArgumentOutOfRangeException) {
557 sl1.GetByIndex (100);
559 } catch (ArgumentOutOfRangeException) {
564 public void GetEnumerator ()
566 SortedList sl1 = DefaultSL ();
567 IDictionaryEnumerator e = sl1.GetEnumerator ();
568 Assert.IsNotNull (e, "#1");
569 Assert.IsTrue (e.MoveNext (), "#2");
570 Assert.IsNotNull (e.Current, "#3");
571 Assert.IsTrue ((e is ICloneable), "#4");
572 Assert.IsTrue ((e is IDictionaryEnumerator), "#5");
573 Assert.IsTrue ((e is IEnumerator), "#6");
577 public void TestGetKey ()
579 SortedList sl1 = DefaultSL ();
580 Assert.AreEqual (1.5, sl1.GetKey (4), "#A1");
581 Assert.AreEqual (34.0, sl1.GetKey (8), "#A2");
586 } catch (ArgumentOutOfRangeException) {
592 } catch (ArgumentOutOfRangeException) {
597 public void TestGetKeyList ()
599 SortedList sl1 = DefaultSL ();
600 IList keys = sl1.GetKeyList ();
601 Assert.IsNotNull (keys, "#A1");
602 Assert.IsTrue (keys.IsReadOnly, "#A2");
603 Assert.AreEqual (9, keys.Count, "#A3");
604 Assert.AreEqual (1.4, keys [3], "#A4");
605 sl1.Add (33.9, "ehhe");
606 Assert.AreEqual (10, keys.Count, "#B1");
607 Assert.AreEqual (33.9, keys [8], "#B2");
611 public void TestGetValueList ()
613 SortedList sl1 = DefaultSL ();
614 IList originalvals = DefaultValues ();
615 IList vals = sl1.GetValueList ();
616 Assert.IsNotNull (vals, "#A1");
617 Assert.IsTrue (vals.IsReadOnly, "#A2");
618 Assert.AreEqual (vals.Count, sl1.Count, "#A3");
620 for (int i = 0; i < sl1.Count; i++) {
621 Assert.AreEqual (vals [i], originalvals [i], "#A4:" + i);
624 sl1.Add (0.01, "ehhe");
625 Assert.AreEqual (10, vals.Count, "#B1");
626 Assert.AreEqual ("dog", vals [8], "#B2");
629 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
631 public void TestIEnumerable_GetEnumerator() {
632 SortedList sl1 = DefaultSL();
633 IEnumerator e = sl1.IEnumerable.GetEnumerator();
634 AssertNotNull("sl.GetEnumerator: does not return enumerator", e);
635 AssertEquals("sl.GetEnumerator: enumerator not working(1)",e.MoveNext(),true);
636 AssertNotNull("sl.GetEnumerator: enumerator not working(2)",e.Current);
641 public void TestIndexOfKey ()
643 SortedList sl1 = new SortedList (24);
645 for (int i = 0; i <= 50; i++) {
646 string s = string.Format ("{0:D2}", i);
647 sl1.Add ("kala " + s, i);
649 Assert.AreEqual (-1, sl1.IndexOfKey ("kala "), "#A");
652 sl1.IndexOfKey ((string) null);
654 } catch (ArgumentNullException) {
660 } catch (InvalidOperationException) {
663 for (int i = 0; i <= 50; i++) {
664 string s = string.Format ("{0:D2}", i);
665 Assert.AreEqual (i, sl1.IndexOfKey ("kala " + s), "#D:" + i);
670 public void TestIndexOfValue ()
672 SortedList sl1 = new SortedList (24);
674 for (int i = 0; i < 50; i++) {
675 s = string.Format ("{0:D2}", i);
676 sl1.Add ("kala " + s, 100 + i * i);
678 for (int i = 0; i < 50; i++) {
679 s = string.Format ("{0:D2}", i + 50);
680 sl1.Add ("kala " + s, 100 + i * i);
682 Assert.AreEqual (-1, sl1.IndexOfValue (102), "#1");
683 Assert.AreEqual (-1, sl1.IndexOfValue (null), "#2");
684 for (int i = 0; i < 50; i++)
685 Assert.AreEqual (i, sl1.IndexOfValue (100 + i * i), "#3:" + i);
689 public void TestIndexOfValue2 ()
691 SortedList list = new SortedList ();
692 list.Add ("key0", "la la");
693 list.Add ("key1", "value");
694 list.Add ("key2", "value");
696 int i = list.IndexOfValue ("value");
697 Assert.AreEqual (1, i);
701 public void TestIndexOfValue3 ()
703 SortedList list = new SortedList ();
704 int i = list.IndexOfValue ((string) null);
705 Assert.AreEqual (1, -i);
709 public void TestIndexer ()
711 SortedList list = new SortedList ();
713 list.Add (1, new Queue ());
714 list.Add (2, new Hashtable ());
715 list.Add (3, new Stack ());
717 Assert.AreEqual (typeof (Queue), list [1].GetType (), "#1");
718 Assert.AreEqual (typeof (Hashtable), list [2].GetType (), "#2");
719 Assert.AreEqual (typeof (Stack), list [3].GetType (), "#3");
723 public void TestEnumerator ()
725 SortedList list = new SortedList ();
727 list.Add (1, new Queue ());
728 list.Add (2, new Hashtable ());
729 list.Add (3, new Stack ());
731 foreach (DictionaryEntry d in list) {
733 int key = (int) d.Key;
734 Type value = d.Value.GetType ();
738 Assert.AreEqual (typeof (Queue), value, "#1");
742 Assert.AreEqual (typeof (Hashtable), value, "#2");
746 Assert.AreEqual (typeof (Stack), value, "#3");
750 Assert.Fail ("#4:" + value.FullName);
757 public void TestRemove ()
759 SortedList sl1 = new SortedList (24);
762 for (int i = 0; i < 50; i++) sl1.Add ("kala " + i, i);
767 } catch (ArgumentNullException) {
771 sl1.Remove ("kala ");
772 Assert.AreEqual (k, sl1.Count, "#B");
777 } catch (InvalidOperationException) {
780 for (int i = 15; i < 20; i++)
781 sl1.Remove ("kala " + i);
782 for (int i = 45; i < 55; i++)
783 sl1.Remove ("kala " + i);
785 Assert.AreEqual (40, sl1.Count, "#D1");
786 for (int i = 45; i < 55; i++)
787 Assert.IsNull (sl1 ["kala " + i], "#D2:" + i);
791 public void TestRemoveAt ()
793 SortedList sl1 = new SortedList (24);
796 for (int i = 0; i < 50; i++) {
797 string s = string.Format ("{0:D2}", i);
798 sl1.Add ("kala " + s, i);
804 } catch (ArgumentOutOfRangeException) {
810 } catch (ArgumentOutOfRangeException) {
815 for (int i = 0; i < 20; i++)
818 Assert.AreEqual (30, sl1.Count, 30, "#C1");
819 for (int i = 0; i < 9; i++)
820 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C2:" + i);
821 for (int i = 9; i < 29; i++)
822 Assert.IsNull (sl1 ["kala " + string.Format ("{0:D2}", i)], "#C3:" + i);
823 for (int i = 29; i < 50; i++)
824 Assert.AreEqual (i, sl1 ["kala " + string.Format ("{0:D2}", i)], "#C4:" + i);
828 public void TestSetByIndex ()
830 SortedList sl1 = new SortedList (24);
831 for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i);
834 sl1.SetByIndex (-1, 77);
836 } catch (ArgumentOutOfRangeException) {
840 sl1.SetByIndex (100, 88);
842 } catch (ArgumentOutOfRangeException) {
845 for (int i = 5; i < 25; i++)
846 sl1.SetByIndex (i, -1);
847 for (int i = 0; i < 5; i++)
848 Assert.AreEqual (i, sl1 [100 + i], "#C1");
849 for (int i = 5; i < 25; i++)
850 Assert.AreEqual (-1, sl1 [100 + i], "#C2");
851 for (int i = 25; i < 50; i++)
852 Assert.AreEqual (i, sl1 [100 + i], "#C3");
856 public void TestTrimToSize ()
858 SortedList sl1 = new SortedList (24);
861 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
862 Assert.AreEqual (icap, sl1.Capacity, "#1");
865 for (int i = 72; i >= 0; i--)
866 sl1.Add (100 + i, i);
868 #if !NET_2_0 // no such expectation as it is broken in .NET 2.0
869 Assert.AreEqual (73, sl1.Capacity, "#2");
874 [Category ("TargetJvmNotWorking")]
875 public void SerializeTest ()
877 SortedList sl1 = new SortedList ();
883 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
885 BinaryFormatter bf = new BinaryFormatter ();
887 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
888 MemoryStream ms = new MemoryStream ();
889 bf.Serialize (ms, sl1);
892 SortedList sl2 = (SortedList) bf.Deserialize (ms);
893 Assert.IsNotNull (sl2, "#1");
894 Assert.AreEqual (3, sl2.Count, "#2");
895 Assert.AreEqual (sl1 [0], sl2 [0], "#3");
896 Assert.AreEqual (sl1 [1], sl2 [1], "#4");
897 Assert.AreEqual (sl1 [2], sl2 [2], "#5");
901 [Category ("TargetJvmNotWorking")]
902 public void Keys_Serialize ()
904 SortedList sl = new SortedList ();
909 IList keys1 = (IList) sl.Keys;
911 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
913 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 [Category ("TargetJvmNotWorking")]
930 public void Values_Serialize ()
932 SortedList sl = new SortedList ();
937 IList values1 = (IList) sl.Values;
939 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
941 BinaryFormatter bf = new BinaryFormatter ();
943 bf.AssemblyFormat = FormatterAssemblyStyle.Full;
944 MemoryStream ms = new MemoryStream ();
945 bf.Serialize (ms, values1);
948 IList values2 = (IList) bf.Deserialize (ms);
949 Assert.IsNotNull (values2, "#1");
950 Assert.AreEqual (3, values2.Count, "#2");
951 Assert.AreEqual (values1 [0], values2 [0], "#3");
952 Assert.AreEqual (values1 [1], values2 [1], "#4");
953 Assert.AreEqual (values1 [2], values2 [2], "#5");
957 [Category ("NotWorking")]
958 public void Values_Deserialize ()
961 BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
963 BinaryFormatter bf = new BinaryFormatter ();
966 MemoryStream ms = new MemoryStream ();
967 ms.Write (_serializedValues, 0, _serializedValues.Length);
970 IList values = (IList) bf.Deserialize (ms);
971 Assert.AreEqual (3, values.Count, "#1");
972 Assert.AreEqual ("B", values [0], "#2");
973 Assert.AreEqual ("A", values [1], "#3");
974 Assert.AreEqual ("C", values [2], "#4");
978 [ExpectedException (typeof (InvalidOperationException))]
979 public void SetIdenticalObjectException ()
981 // Even though the key/value pair being set are identical to
982 // the existing one, it causes snapshot out of sync.
983 SortedList sl = new SortedList ();
985 foreach (string s in sl.Keys)
990 public void Ctor_IComparer ()
992 SortedList sl = new SortedList (new SortedListComparer ());
993 sl.Add (new object (), new object ());
997 public void Ctor_IComparer_Null ()
999 SortedList sl = new SortedList ((IComparer) null);
1000 sl.Add (new object (), new object ());
1004 public void Ctor_IDictionary_IComparer_Before ()
1006 Hashtable ht = new Hashtable ();
1009 // adding a non-IComparable in Hashtable
1010 ht.Add (new object (), "c");
1011 SortedList sl = new SortedList (ht, new SortedListComparer ());
1012 Assert.AreEqual (3, sl.Count);
1016 [ExpectedException (typeof (InvalidOperationException))]
1017 public void Ctor_IDictionary_DefaultInvariant_Before ()
1019 Hashtable ht = new Hashtable ();
1022 // adding a non-IComparable in Hashtable
1023 ht.Add (new object (), "c");
1024 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1025 Assert.AreEqual (3, sl.Count);
1029 public void Ctor_IDictionary_IComparer_Null_Before_1item ()
1031 Hashtable ht = new Hashtable ();
1032 // adding a non-IComparable in Hashtable
1033 ht.Add (new object (), "c");
1034 SortedList sl = new SortedList (ht, null);
1035 Assert.AreEqual (1, sl.Count);
1039 [ExpectedException (typeof (InvalidOperationException))]
1040 public void Ctor_IDictionary_IComparer_Null_Before_2items ()
1042 Hashtable ht = new Hashtable ();
1044 // adding a non-IComparable in Hashtable
1045 ht.Add (new object (), "c");
1046 SortedList sl = new SortedList (ht, null);
1047 Assert.AreEqual (2, sl.Count);
1051 public void Ctor_IDictionary_IComparer_After ()
1053 Hashtable ht = new Hashtable ();
1056 SortedList sl = new SortedList (ht, new SortedListComparer ());
1057 Assert.AreEqual (2, sl.Count);
1058 // adding a non-IComparable in SortedList
1059 sl.Add (new object (), "c");
1063 [ExpectedException (typeof (InvalidOperationException))]
1064 public void Ctor_IDictionary_DefaultInvariant_After ()
1066 Hashtable ht = new Hashtable ();
1069 SortedList sl = new SortedList (ht, Comparer.DefaultInvariant);
1070 Assert.AreEqual (2, sl.Count);
1071 // adding a non-IComparable in SortedList
1072 sl.Add (new object (), "c");
1076 public void Ctor_IDictionary_IComparer_Null_After_1item ()
1078 SortedList sl = new SortedList (new Hashtable (), null);
1079 sl.Add (new object (), "b");
1083 [ExpectedException (typeof (InvalidOperationException))]
1084 public void Ctor_IDictionary_IComparer_Null_After_2items ()
1086 SortedList sl = new SortedList (new Hashtable (), null);
1088 sl.Add (new object (), "b");
1092 public void IComparer_Clone ()
1094 SortedList sl = new SortedList (new SortedListComparer ());
1095 sl.Add (new object (), new object ());
1096 SortedList clone = (SortedList) sl.Clone ();
1100 public void IComparer_Null_Clone ()
1102 SortedList sl = new SortedList ((IComparer) null);
1103 sl.Add (new object (), new object ());
1104 SortedList clone = (SortedList) sl.Clone ();
1107 sealed class StartsWithComparator : IComparer {
1108 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1110 public int Compare(object p, object w)
1112 string part = (string) p;
1113 string whole = (string) w;
1114 // let the default string comparer deal with null or when part is not smaller then whole
1115 if (part == null || whole == null || part.Length >= whole.Length)
1116 return String.Compare (part, whole);
1118 // loop through all characters that part and whole have in common
1122 match = (part[pos] == whole[pos]);
1123 } while (match && ++pos < part.Length);
1125 // return result of last comparison
1126 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1130 sealed class StartsWithComparatorPartWholeCheck : IComparer
1132 public static readonly StartsWithComparator Instance = new StartsWithComparator();
1134 public int Compare(object p, object w)
1136 string part = (string) p;
1137 string whole = (string) w;
1138 Assert.IsTrue(part == "Part", "#PWC0");
1139 Assert.IsTrue(whole == "Whole", "#PWC1");
1141 // let the default string comparer deal with null or when part is not smaller then whole
1142 if (part == null || whole == null || part.Length >= whole.Length)
1143 return String.Compare(part, whole);
1145 // loop through all characters that part and whole have in common
1149 match = (part[pos] == whole[pos]);
1150 } while (match && ++pos < part.Length);
1152 // return result of last comparison
1153 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1158 public void ComparatorUsageTest()
1160 SortedList sl = new SortedList(StartsWithComparator.Instance);
1162 sl.Add("Apples", "Value-Apples");
1163 sl.Add("Bananas", "Value-Bananas");
1164 sl.Add("Oranges", "Value-Oranges");
1166 // Ensure 3 objects exist in the collection
1167 Assert.IsTrue(sl.Count == 3, "Count");
1169 // Complete Match Test Set
1170 Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
1171 Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
1172 Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
1174 // Partial Match Test Set
1175 Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
1176 Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
1177 Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
1179 // Reversed Match Test Set
1180 Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
1183 Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
1184 Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
1185 Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
1190 public void ComparatorPartWholeCheck()
1192 SortedList sl = new SortedList (StartsWithComparatorPartWholeCheck.Instance);
1193 sl.Add("Part", "Value-Part");
1194 Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
1198 public void NonComparatorStringCheck()
1200 SortedList sl = new SortedList ();
1202 sl.Add("Oranges", "Value-Oranges");
1203 sl.Add("Apples", "Value-Apples");
1204 sl.Add("Bananas", "Value-Bananas");
1207 Assert.IsTrue(sl.Count == 3, "NCSC #A0");
1209 Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
1210 Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
1211 Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
1213 Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
1214 Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
1215 Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
1217 string [] keys = new string [sl.Keys.Count];
1218 sl.Keys.CopyTo (keys, 0);
1219 Assert.IsTrue(keys [0] == "Apples", "NCSC #D1");
1220 Assert.IsTrue(keys [1] == "Bananas", "NCSC #D2");
1221 Assert.IsTrue(keys [2] == "Oranges", "NCSC #D3");
1224 private static byte [] _serializedValues = new byte [] {
1225 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
1226 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
1227 0x01, 0x00, 0x00, 0x00, 0x27, 0x53, 0x79, 0x73, 0x74,
1228 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63,
1229 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72,
1230 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56,
1231 0x61, 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x01,
1232 0x00, 0x00, 0x00, 0x0a, 0x73, 0x6f, 0x72, 0x74, 0x65,
1233 0x64, 0x4c, 0x69, 0x73, 0x74, 0x03, 0x1d, 0x53, 0x79,
1234 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c, 0x6c,
1235 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x53,
1236 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69, 0x73, 0x74,
1237 0x09, 0x02, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00,
1238 0x00, 0x1d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1239 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1240 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1241 0x4c, 0x69, 0x73, 0x74, 0x07, 0x00, 0x00, 0x00, 0x04,
1242 0x6b, 0x65, 0x79, 0x73, 0x06, 0x76, 0x61, 0x6c, 0x75,
1243 0x65, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x07,
1244 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x08, 0x63,
1245 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x07, 0x6b,
1246 0x65, 0x79, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x76, 0x61,
1247 0x6c, 0x75, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x05, 0x05,
1248 0x00, 0x00, 0x03, 0x03, 0x03, 0x08, 0x08, 0x1b, 0x53,
1249 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
1250 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e,
1251 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x25,
1252 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f,
1253 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73,
1254 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x4c, 0x69,
1255 0x73, 0x74, 0x2b, 0x4b, 0x65, 0x79, 0x4c, 0x69, 0x73,
1256 0x74, 0x27, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1257 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1258 0x6e, 0x73, 0x2e, 0x53, 0x6f, 0x72, 0x74, 0x65, 0x64,
1259 0x4c, 0x69, 0x73, 0x74, 0x2b, 0x56, 0x61, 0x6c, 0x75,
1260 0x65, 0x4c, 0x69, 0x73, 0x74, 0x09, 0x03, 0x00, 0x00,
1261 0x00, 0x09, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
1262 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00,
1263 0x00, 0x0a, 0x09, 0x01, 0x00, 0x00, 0x00, 0x10, 0x03,
1264 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x08, 0x08,
1265 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x05, 0x00, 0x00,
1266 0x00, 0x08, 0x08, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x0d,
1267 0x10, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
1268 0x06, 0x07, 0x00, 0x00, 0x00, 0x01, 0x42, 0x06, 0x08,
1269 0x00, 0x00, 0x00, 0x01, 0x41, 0x06, 0x09, 0x00, 0x00,
1270 0x00, 0x01, 0x43, 0x0d, 0x0d, 0x04, 0x05, 0x00, 0x00,
1271 0x00, 0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e,
1272 0x43, 0x6f, 0x6c, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
1273 0x6e, 0x73, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
1274 0x65, 0x72, 0x00, 0x00, 0x00, 0x00, 0x0b };