[jenkins] Make the concurrent GC the default on mainline archtectures.
[mono.git] / mcs / class / corlib / Test / System.Collections / SortedListTest.cs
1 // SortedListTest.cs - NUnit Test Cases for the System.Collections.SortedList class
2 //
3 // Authors:
4 //      Jaak Simm
5 //      Duncan Mak (duncan@ximian.com)
6 //
7 // Thanks go to David Brandt (bucky@keystreams.com),
8 // because this file is based on his ArrayListTest.cs
9 //
10 // (C) Ximian, Inc.  http://www.ximian.com
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 // 
13 // main TODO: additional tests for functions affected by
14 //            fixedsize and read-only properties 
15
16
17 using System;
18 using System.Collections;
19 using System.IO;
20 using System.Runtime.Serialization.Formatters;
21 using System.Runtime.Serialization.Formatters.Binary;
22
23 using NUnit.Framework;
24
25 namespace MonoTests.System.Collections
26 {
27         public class SortedListComparer : IComparer
28         {
29                 public int Compare (object x, object y)
30                 {
31                         return x.GetHashCode () - y.GetHashCode ();
32                 }
33         }
34
35         [TestFixture]
36         public class SortedListTest
37         {
38                 protected SortedList sl1;
39                 protected SortedList sl2;
40                 protected SortedList emptysl;
41                 protected const int icap = 16;
42
43                 public void TestConstructor1 ()
44                 {
45                         SortedList temp1 = new SortedList ();
46                         Assert.IsNotNull (temp1, "#1");
47                 }
48
49                 [Test]
50                 public void TestConstructor2 ()
51                 {
52                         Comparer c = Comparer.Default;
53                         SortedList temp1 = new SortedList (c);
54                         Assert.IsNotNull (temp1, "#1");
55                 }
56
57                 [Test]
58                 public void TestConstructor3 ()
59                 {
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");
65
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");
70
71                         try {
72                                 new SortedList ((Hashtable) null);
73                                 Assert.Fail ("#B");
74                         } catch (ArgumentNullException) {
75                         }
76
77                         try {
78                                 d = new Hashtable ();
79                                 d.Add ("one", "Mircosoft");
80                                 d.Add ("two", "will");
81                                 d.Add ("three", "rule");
82                                 d.Add ("four", "the world");
83                                 d.Add (7987, "lkj");
84                                 new SortedList (d);
85                                 Assert.Fail ("#C");
86                         } catch (InvalidOperationException) {
87                         }
88                 }
89
90                 [Test]
91                 public void TestConstructor4 ()
92                 {
93                         SortedList temp1 = new SortedList (17);
94                         Assert.IsNotNull (temp1, "#A1");
95                         Assert.AreEqual (17, temp1.Capacity, "#A2");
96
97                         try {
98                                 new SortedList (-6);
99                                 Assert.Fail ("#B");
100                         } catch (ArgumentOutOfRangeException) {
101                         }
102
103                         temp1 = new SortedList (0);
104                         Assert.IsNotNull (temp1, "#C");
105                 }
106
107                 [Test]
108                 public void TestConstructor5 ()
109                 {
110                         Comparer c = Comparer.Default;
111                         SortedList temp1 = new SortedList (c, 27);
112                         Assert.IsNotNull (temp1, "#A1");
113                         Assert.AreEqual (27, temp1.Capacity, "#A2");
114
115                         try {
116                                 new SortedList (-12);
117                                 Assert.Fail ("#B");
118                         } catch (ArgumentOutOfRangeException) {
119                         }
120                 }
121
122
123                 [Test]
124                 public void TestIsSynchronized ()
125                 {
126                         SortedList sl1 = new SortedList ();
127                         Assert.IsFalse (sl1.IsSynchronized, "#1");
128                         SortedList sl2 = SortedList.Synchronized (sl1);
129                         Assert.IsTrue (sl2.IsSynchronized, "#2");
130                 }
131
132                 [Test]
133                 public void TestCapacity ()
134                 {
135                         for (int i = 0; i < 100; i++) {
136                                 SortedList sl1 = new SortedList (i);
137                                 Assert.AreEqual (i, sl1.Capacity, i.ToString ());
138                         }
139                 }
140
141                 [Test]
142                 public void TestCapacity2 ()
143                 {
144                         SortedList list = new SortedList ();
145
146                         list.Capacity = 5;
147                         Assert.AreEqual (5, list.Capacity, "#1");
148
149                         SortedList sync = SortedList.Synchronized (list);
150                         Assert.AreEqual (5, sync.Capacity, "#2");
151
152                         list.Capacity = 20;
153                         Assert.AreEqual (20, list.Capacity, "#3");
154                         Assert.AreEqual (20, sync.Capacity, "#4");
155                 }
156
157                 [Test]
158                 public void TestCapacity3 ()
159                 {
160                         int new_capacity = 5;
161                         SortedList list = new SortedList (1000);
162                         list.Capacity = new_capacity;
163
164                         Assert.AreEqual (new_capacity, list.Capacity);
165                 }
166
167                 [Test]
168                 public void Capacity_BackTo0 ()
169                 {
170                         SortedList list = new SortedList (42);
171                         Assert.AreEqual (42, list.Capacity, "#1");
172                         list.Capacity = 0;
173                 }
174
175                 // This doesn't fail on 64 bit systems
176                 /*
177                 [Test]
178                 [ExpectedException (typeof (OutOfMemoryException))]
179                 public void TestCapacity4 ()
180                 {
181                         SortedList list = new SortedList ();
182                         list.Capacity = Int32.MaxValue;
183                 }
184                 */
185
186                 [Test]
187                 public void TestCount ()
188                 {
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);
194                         }
195                 }
196
197                 [Test]
198                 public void TestIsFixed ()
199                 {
200                         SortedList sl1 = new SortedList ();
201                         Assert.IsFalse (sl1.IsFixedSize);
202                 }
203
204                 [Test]
205                 public void TestIsReadOnly ()
206                 {
207                         SortedList sl1 = new SortedList ();
208                         Assert.IsFalse (sl1.IsReadOnly);
209                 }
210
211                 [Test]
212                 public void TestItem ()
213                 {
214                         SortedList sl1 = new SortedList ();
215
216                         object o = sl1 [-1];
217                         Assert.IsNull (o, "#A");
218
219                         try {
220                                 o = sl1 [(string) null];
221                                 Assert.Fail ("#B");
222                         } catch (ArgumentNullException) {
223                         }
224
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);
229                 }
230
231                 [Test]
232                 public void TestSyncRoot ()
233                 {
234                         SortedList sl1 = new SortedList ();
235                         Assert.IsNotNull (sl1.SyncRoot);
236                         /*
237                         lock( sl1.SyncRoot ) {
238                                 foreach ( Object item in sl1 ) {
239                                         item="asdf";
240                                         Assert ("sl.SyncRoot: item not read-only",item.IsReadOnly);
241                                 }
242                         }
243                         */
244                 }
245
246                 [Test]
247                 public void TestValues ()
248                 {
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);
254                         }
255                 }
256
257
258                 // TODO: Add with IComparer
259                 [Test]
260                 public void TestAdd ()
261                 {
262                         // seems SortedList cannot be set fixedsize or readonly
263                         SortedList sl1 = new SortedList ();
264
265                         try {
266                                 sl1.Add ((string) null, "kala");
267                                 Assert.Fail ("#A");
268                         } catch (ArgumentNullException) {
269                         }
270
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);
275                         }
276
277                         try {
278                                 sl1.Add ("kala", 10);
279                                 sl1.Add ("kala", 11);
280                                 Assert.Fail ("#C");
281                         } catch (ArgumentException) {
282                         }
283                 }
284
285                 [Test]
286                 public void TestClear ()
287                 {
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");
293                         sl1.Clear ();
294                         Assert.AreEqual (0, sl1.Count, "#B1");
295                 }
296
297
298                 [Test]
299                 public void ClearDoesNotTouchCapacity ()
300                 {
301                         SortedList sl = new SortedList ();
302                         // according to MSDN docs Clear () does not change capacity
303                         for (int i = 0; i < 18; i++) {
304                                 sl.Add (i, i);
305                         }
306                         int capacityBeforeClear = sl.Capacity;
307                         sl.Clear ();
308                         int capacityAfterClear = sl.Capacity;
309                         Assert.AreEqual (capacityBeforeClear, capacityAfterClear);
310                 }
311
312                 [Test]
313                 public void TestClone ()
314                 {
315                         {
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);
322                         }
323                         {
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 ();
329                                 sl1.Add ("d1", d10);
330                                 sl1.Add ("d2", d11);
331                                 sl1.Add ("d3", d12);
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");
338                         }
339                 }
340
341                 [Test]
342                 public void TestContains ()
343                 {
344                         SortedList sl1 = new SortedList (55);
345                         for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
346
347                         try {
348                                 if (sl1.Contains (null)) {
349                                 }
350                                 Assert.Fail ("#A");
351                         } catch (ArgumentNullException) {
352                         }
353
354                         Assert.IsTrue (sl1.Contains ("kala 17"), "#B1");
355                         Assert.IsFalse (sl1.Contains ("ohoo"), "#B2");
356                 }
357
358                 [Test]
359                 public void TestContainsKey ()
360                 {
361                         SortedList sl1 = new SortedList (55);
362                         for (int i = 0; i <= 50; i++) { sl1.Add ("kala " + i, i); }
363
364                         try {
365                                 if (sl1.ContainsKey (null)) {
366                                 }
367                                 Assert.Fail ("#A");
368                         } catch (ArgumentNullException) {
369                         }
370
371                         Assert.IsTrue (sl1.ContainsKey ("kala 17"), "#B1");
372                         Assert.IsFalse (sl1.ContainsKey ("ohoo"), "#B2");
373                 }
374
375                 [Test]
376                 public void TestContainsValue ()
377                 {
378                         SortedList sl1 = new SortedList (55);
379                         sl1.Add (0, "zero");
380                         sl1.Add (1, "one");
381                         sl1.Add (2, "two");
382                         sl1.Add (3, "three");
383                         sl1.Add (4, "four");
384
385                         Assert.IsTrue (sl1.ContainsValue ("zero"), "#1");
386                         Assert.IsFalse (sl1.ContainsValue ("ohoo"), "#2");
387                         Assert.IsFalse (sl1.ContainsValue (null), "#3");
388                 }
389
390                 [Test]
391                 public void TestCopyTo ()
392                 {
393                         SortedList sl1 = new SortedList ();
394                         for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
395                         {
396                                 try {
397                                         sl1.CopyTo (null, 2);
398                                         Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
399                                 } catch (ArgumentNullException) {
400                                 }
401                         }
402                         {
403                                 try {
404                                         Char [,] c2 = new Char [2, 2];
405                                         sl1.CopyTo (c2, 2);
406                                         Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
407                                 } catch (ArgumentException) {
408                                 }
409                         }
410                         {
411                                 try {
412                                         Char [] c1 = new Char [2];
413                                         sl1.CopyTo (c1, -2);
414                                         Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
415                                 } catch (ArgumentOutOfRangeException) {
416                                 }
417                         }
418                         {
419                                 try {
420                                         Char [] c1 = new Char [2];
421                                         sl1.CopyTo (c1, 3);
422                                         Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
423                                 } catch (ArgumentException) {
424                                 }
425                         }
426                         {
427                                 try {
428                                         Char [] c1 = new Char [2];
429                                         sl1.CopyTo (c1, 1);
430                                         Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
431                                 } catch (ArgumentException) {
432                                 }
433                         }
434                         {
435                                 try {
436                                         Char [] c2 = new Char [15];
437                                         sl1.CopyTo (c2, 0);
438                                         Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
439                                 } catch (InvalidCastException) {
440                                 }
441                         }
442
443                         // CopyTo function does not work well with SortedList
444                         // even example at MSDN gave InvalidCastException
445                         // thus, it is NOT tested here
446                         /*
447                                         sl1.Clear();
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 );
460                                         sl1.CopyTo(copy,1);
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));
465                         */
466                 }
467
468                 public SortedList DefaultSL ()
469                 {
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");
480                         return sl1;
481                 }
482
483                 public IList DefaultValues ()
484                 {
485                         IList il = new ArrayList ();
486                         il.Add ("fox");
487                         il.Add ("The");
488                         il.Add ("quick");
489                         il.Add ("jumped");
490                         il.Add ("over");
491                         il.Add ("the");
492                         il.Add ("lazy");
493                         il.Add ("dog");
494                         il.Add ("brown");
495                         return il;
496                 }
497
498                 [Test]
499                 public void TestGetByIndex ()
500                 {
501                         SortedList sl1 = DefaultSL ();
502                         Assert.AreEqual ("over", sl1.GetByIndex (4), "#A1");
503                         Assert.AreEqual ("brown", sl1.GetByIndex (8), "#A2");
504
505                         try {
506                                 sl1.GetByIndex (-1);
507                                 Assert.Fail ("#B");
508                         } catch (ArgumentOutOfRangeException) {
509                         }
510
511                         try {
512                                 sl1.GetByIndex (100);
513                                 Assert.Fail ("#C");
514                         } catch (ArgumentOutOfRangeException) {
515                         }
516                 }
517
518                 [Test]
519                 public void GetEnumerator ()
520                 {
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");
529                 }
530
531                 [Test]
532                 public void TestGetKey ()
533                 {
534                         SortedList sl1 = DefaultSL ();
535                         Assert.AreEqual (1.5, sl1.GetKey (4), "#A1");
536                         Assert.AreEqual (34.0, sl1.GetKey (8), "#A2");
537
538                         try {
539                                 sl1.GetKey (-1);
540                                 Assert.Fail ("#B");
541                         } catch (ArgumentOutOfRangeException) {
542                         }
543
544                         try {
545                                 sl1.GetKey (100);
546                                 Assert.Fail ("#C");
547                         } catch (ArgumentOutOfRangeException) {
548                         }
549                 }
550
551                 [Test]
552                 public void TestGetKeyList ()
553                 {
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");
563                 }
564
565                 [Test]
566                 public void TestGetValueList ()
567                 {
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");
574
575                         for (int i = 0; i < sl1.Count; i++) {
576                                 Assert.AreEqual (vals [i], originalvals [i], "#A4:" + i);
577                         }
578
579                         sl1.Add (0.01, "ehhe");
580                         Assert.AreEqual (10, vals.Count, "#B1");
581                         Assert.AreEqual ("dog", vals [8], "#B2");
582                 }
583
584                 // TODO: IEnumerable.GetEnumerator [Explicit Interface Implementation]
585                 /*
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);
592                 }
593                 */
594
595                 [Test]
596                 public void TestIndexOfKey ()
597                 {
598                         SortedList sl1 = new SortedList (24);
599
600                         for (int i = 0; i <= 50; i++) {
601                                 string s = string.Format ("{0:D2}", i);
602                                 sl1.Add ("kala " + s, i);
603                         }
604                         Assert.AreEqual (-1, sl1.IndexOfKey ("kala "), "#A");
605
606                         try {
607                                 sl1.IndexOfKey ((string) null);
608                                 Assert.Fail ("#B");
609                         } catch (ArgumentNullException) {
610                         }
611
612                         try {
613                                 sl1.IndexOfKey (10);
614                                 Assert.Fail ("#C");
615                         } catch (InvalidOperationException) {
616                         }
617
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);
621                         }
622                 }
623
624                 [Test]
625                 public void TestIndexOfValue ()
626                 {
627                         SortedList sl1 = new SortedList (24);
628                         string s = null;
629                         for (int i = 0; i < 50; i++) {
630                                 s = string.Format ("{0:D2}", i);
631                                 sl1.Add ("kala " + s, 100 + i * i);
632                         }
633                         for (int i = 0; i < 50; i++) {
634                                 s = string.Format ("{0:D2}", i + 50);
635                                 sl1.Add ("kala " + s, 100 + i * i);
636                         }
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);
641                 }
642
643                 [Test]
644                 public void TestIndexOfValue2 ()
645                 {
646                         SortedList list = new SortedList ();
647                         list.Add ("key0", "la la");
648                         list.Add ("key1", "value");
649                         list.Add ("key2", "value");
650
651                         int i = list.IndexOfValue ("value");
652                         Assert.AreEqual (1, i);
653                 }
654
655                 [Test]
656                 public void TestIndexOfValue3 ()
657                 {
658                         SortedList list = new SortedList ();
659                         int i = list.IndexOfValue ((string) null);
660                         Assert.AreEqual (1, -i);
661                 }
662
663                 [Test]
664                 public void TestIndexer ()
665                 {
666                         SortedList list = new SortedList ();
667
668                         list.Add (1, new Queue ());
669                         list.Add (2, new Hashtable ());
670                         list.Add (3, new Stack ());
671
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");
675                 }
676
677                 [Test]
678                 public void TestEnumerator ()
679                 {
680                         SortedList list = new SortedList ();
681
682                         list.Add (1, new Queue ());
683                         list.Add (2, new Hashtable ());
684                         list.Add (3, new Stack ());
685
686                         foreach (DictionaryEntry d in list) {
687
688                                 int key = (int) d.Key;
689                                 Type value = d.Value.GetType ();
690
691                                 switch (key) {
692                                 case 1:
693                                         Assert.AreEqual (typeof (Queue), value, "#1");
694                                         break;
695
696                                 case 2:
697                                         Assert.AreEqual (typeof (Hashtable), value, "#2");
698                                         break;
699
700                                 case 3:
701                                         Assert.AreEqual (typeof (Stack), value, "#3");
702                                         break;
703
704                                 default:
705                                         Assert.Fail ("#4:" + value.FullName);
706                                         break;
707                                 }
708                         }
709                 }
710
711                 [Test]
712                 public void TestRemove ()
713                 {
714                         SortedList sl1 = new SortedList (24);
715                         string s = null;
716                         int k;
717                         for (int i = 0; i < 50; i++) sl1.Add ("kala " + i, i);
718
719                         try {
720                                 sl1.Remove (s);
721                                 Assert.Fail ("#A");
722                         } catch (ArgumentNullException) {
723                         }
724
725                         k = sl1.Count;
726                         sl1.Remove ("kala ");
727                         Assert.AreEqual (k, sl1.Count, "#B");
728
729                         try {
730                                 sl1.Remove (15);
731                                 Assert.Fail ("#C");
732                         } catch (InvalidOperationException) {
733                         }
734
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);
739
740                         Assert.AreEqual (40, sl1.Count, "#D1");
741                         for (int i = 45; i < 55; i++)
742                                 Assert.IsNull (sl1 ["kala " + i], "#D2:" + i);
743                 }
744
745                 [Test]
746                 public void TestRemoveAt ()
747                 {
748                         SortedList sl1 = new SortedList (24);
749                         int k;
750
751                         for (int i = 0; i < 50; i++) {
752                                 string s = string.Format ("{0:D2}", i);
753                                 sl1.Add ("kala " + s, i);
754                         }
755
756                         try {
757                                 sl1.RemoveAt (-1);
758                                 Assert.Fail ("#A");
759                         } catch (ArgumentOutOfRangeException) {
760                         }
761
762                         try {
763                                 sl1.RemoveAt (100);
764                                 Assert.Fail ("#B");
765                         } catch (ArgumentOutOfRangeException) {
766                         }
767
768                         k = sl1.Count;
769
770                         for (int i = 0; i < 20; i++)
771                                 sl1.RemoveAt (9);
772
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);
780                 }
781
782                 [Test]
783                 public void TestSetByIndex ()
784                 {
785                         SortedList sl1 = new SortedList (24);
786                         for (int i = 49; i >= 0; i--) sl1.Add (100 + i, i);
787
788                         try {
789                                 sl1.SetByIndex (-1, 77);
790                                 Assert.Fail ("#A");
791                         } catch (ArgumentOutOfRangeException) {
792                         }
793
794                         try {
795                                 sl1.SetByIndex (100, 88);
796                                 Assert.Fail ("#B");
797                         } catch (ArgumentOutOfRangeException) {
798                         }
799
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");
808                 }
809
810                 [Test]
811                 public void TestTrimToSize ()
812                 {
813                         SortedList sl1 = new SortedList (24);
814
815                         sl1.TrimToSize ();
816
817                         for (int i = 72; i >= 0; i--)
818                                 sl1.Add (100 + i, i);
819                         sl1.TrimToSize ();
820                 }
821
822                 [Test]
823                 public void SerializeTest ()
824                 {
825                         SortedList sl1 = new SortedList ();
826                         sl1.Add (5, "A");
827                         sl1.Add (0, "B");
828                         sl1.Add (7, "C");
829
830                         BinaryFormatter bf = new BinaryFormatter ();
831                         bf.AssemblyFormat = FormatterAssemblyStyle.Full;
832                         MemoryStream ms = new MemoryStream ();
833                         bf.Serialize (ms, sl1);
834                         ms.Position = 0;
835
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");
842                 }
843
844                 [Test]
845                 public void Keys_Serialize ()
846                 {
847                         SortedList sl = new SortedList ();
848                         sl.Add (5, "A");
849                         sl.Add (0, "B");
850                         sl.Add (7, "C");
851
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);
857                         ms.Position = 0;
858
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");
865                 }
866
867                 [Test]
868                 public void Values_Serialize ()
869                 {
870                         SortedList sl = new SortedList ();
871                         sl.Add (5, "A");
872                         sl.Add (0, "B");
873                         sl.Add (7, "C");
874
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);
880                         ms.Position = 0;
881
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");
888                 }
889
890                 [Test]
891                 [Category ("NotWorking")]
892                 public void Values_Deserialize ()
893                 {
894                         BinaryFormatter bf = new BinaryFormatter ();
895
896                         MemoryStream ms = new MemoryStream ();
897                         ms.Write (_serializedValues, 0, _serializedValues.Length);
898                         ms.Position = 0;
899
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");
905                 }
906
907                 [Test]
908                 [ExpectedException (typeof (InvalidOperationException))]
909                 public void SetIdenticalObjectException ()
910                 {
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 ();
914                         sl ["foo"] = "bar";
915                         foreach (string s in sl.Keys)
916                                 sl ["foo"] = "bar";
917                 }
918
919                 [Test]
920                 public void Ctor_IComparer ()
921                 {
922                         SortedList sl = new SortedList (new SortedListComparer ());
923                         sl.Add (new object (), new object ());
924                 }
925
926                 [Test]
927                 public void Ctor_IComparer_Null ()
928                 {
929                         SortedList sl = new SortedList ((IComparer) null);
930                         sl.Add (new object (), new object ());
931                 }
932
933                 [Test]
934                 public void Ctor_IDictionary_IComparer_Before ()
935                 {
936                         Hashtable ht = new Hashtable ();
937                         ht.Add (2, "a");
938                         ht.Add (1, "b");
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);
943                 }
944
945                 [Test]
946                 [ExpectedException (typeof (InvalidOperationException))]
947                 public void Ctor_IDictionary_DefaultInvariant_Before ()
948                 {
949                         Hashtable ht = new Hashtable ();
950                         ht.Add (2, "a");
951                         ht.Add (1, "b");
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);
956                 }
957
958                 [Test]
959                 public void Ctor_IDictionary_IComparer_Null_Before_1item ()
960                 {
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);
966                 }
967
968                 [Test]
969                 [ExpectedException (typeof (InvalidOperationException))]
970                 public void Ctor_IDictionary_IComparer_Null_Before_2items ()
971                 {
972                         Hashtable ht = new Hashtable ();
973                         ht.Add (2, "a");
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);
978                 }
979
980                 [Test]
981                 public void Ctor_IDictionary_IComparer_After ()
982                 {
983                         Hashtable ht = new Hashtable ();
984                         ht.Add (2, "a");
985                         ht.Add (1, "b");
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");
990                 }
991
992                 [Test]
993                 [ExpectedException (typeof (InvalidOperationException))]
994                 public void Ctor_IDictionary_DefaultInvariant_After ()
995                 {
996                         Hashtable ht = new Hashtable ();
997                         ht.Add (2, "a");
998                         ht.Add (1, "b");
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");
1003                 }
1004
1005                 [Test]
1006                 public void Ctor_IDictionary_IComparer_Null_After_1item ()
1007                 {
1008                         SortedList sl = new SortedList (new Hashtable (), null);
1009                         sl.Add (new object (), "b");
1010                 }
1011
1012                 [Test]
1013                 [ExpectedException (typeof (InvalidOperationException))]
1014                 public void Ctor_IDictionary_IComparer_Null_After_2items ()
1015                 {
1016                         SortedList sl = new SortedList (new Hashtable (), null);
1017                         sl.Add (2, "a");
1018                         sl.Add (new object (), "b");
1019                 }
1020
1021                 [Test]
1022                 public void IComparer_Clone ()
1023                 {
1024                         SortedList sl = new SortedList (new SortedListComparer ());
1025                         sl.Add (new object (), new object ());
1026                         SortedList clone = (SortedList) sl.Clone ();
1027                 }
1028
1029                 [Test]
1030                 public void IComparer_Null_Clone ()
1031                 {
1032                         SortedList sl = new SortedList ((IComparer) null);
1033                         sl.Add (new object (), new object ());
1034                         SortedList clone = (SortedList) sl.Clone ();
1035                 }
1036
1037                 sealed class StartsWithComparator : IComparer {
1038                         public static readonly StartsWithComparator Instance = new StartsWithComparator();
1039
1040                         public int Compare(object p, object w)
1041                         {
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);
1047
1048                                 // loop through all characters that part and whole have in common
1049                                 int pos = 0;
1050                                 bool match;
1051                                 do {
1052                                         match = (part[pos] == whole[pos]);
1053                                 } while (match && ++pos < part.Length);
1054
1055                                 // return result of last comparison
1056                                 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1057                         }
1058                 }
1059
1060                 sealed class StartsWithComparatorPartWholeCheck : IComparer
1061                 {
1062                         public static readonly StartsWithComparator Instance = new StartsWithComparator();
1063
1064                         public int Compare(object p, object w)
1065                         {
1066                                 string part = (string) p;
1067                                 string whole = (string) w;
1068                                 Assert.IsTrue(part == "Part", "#PWC0");
1069                                 Assert.IsTrue(whole == "Whole", "#PWC1");
1070
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);
1074
1075                                 // loop through all characters that part and whole have in common
1076                                 int pos = 0;
1077                                 bool match;
1078                                 do {
1079                                         match = (part[pos] == whole[pos]);
1080                                 } while (match && ++pos < part.Length);
1081
1082                                 // return result of last comparison
1083                                 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
1084                         }
1085                 }
1086
1087                 [Test]
1088                 public void ComparatorUsageTest()
1089                 {
1090                         SortedList sl = new SortedList(StartsWithComparator.Instance);
1091
1092                         sl.Add("Apples", "Value-Apples");
1093                         sl.Add("Bananas", "Value-Bananas");
1094                         sl.Add("Oranges", "Value-Oranges");
1095
1096                         // Ensure 3 objects exist in the collection
1097                         Assert.IsTrue(sl.Count == 3, "Count");
1098
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");
1103
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");
1108
1109                         // Reversed Match Test Set
1110                         Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
1111
1112                         // No match tests
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");
1116
1117                 }
1118
1119                 [Test]
1120                 public void ComparatorPartWholeCheck()
1121                 {
1122                         SortedList sl = new SortedList (StartsWithComparatorPartWholeCheck.Instance);
1123                         sl.Add("Part", "Value-Part");
1124                         Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
1125                 }
1126
1127                 [Test]
1128                 public void NonComparatorStringCheck()
1129                 {
1130                         SortedList sl = new SortedList ();
1131
1132                         sl.Add("Oranges", "Value-Oranges");
1133                         sl.Add("Apples", "Value-Apples");
1134                         sl.Add("Bananas", "Value-Bananas");
1135
1136                         int i = 0;
1137                         Assert.IsTrue(sl.Count == 3, "NCSC #A0");
1138
1139                         Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
1140                         Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
1141                         Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
1142
1143                         Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
1144                         Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
1145                         Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
1146
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");
1152                 }
1153
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 };
1205         }
1206 }