Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[mono.git] / mcs / class / System / Test / System.Collections.Generic / SortedDictionaryTest.cs
1 //
2 // SortedDictionaryTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Runtime.Serialization.Formatters.Binary;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Collections.Generic
40 {
41         [TestFixture]
42         public class SortedDictionaryTest
43         {
44                 [Test]
45                 public void CtorNullComparer ()
46                 {
47                         SortedDictionary<int,string> sd =
48                                 new SortedDictionary<int,string> ((IComparer<int>) null);
49                         Assert.AreEqual (Comparer<int>.Default, sd.Comparer);
50                 }
51
52                 [Test]
53                 [ExpectedException (typeof (ArgumentNullException))]
54                 public void CtorNullDictionary ()
55                 {
56                         new SortedDictionary<int,string> (default (IDictionary<int,string>));
57                 }
58
59                 [Test]
60                 [ExpectedException (typeof (ArgumentNullException))]
61                 public void CtorComparerDictionaryNullComparer ()
62                 {
63                         new SortedDictionary<int,string> (default (IDictionary<int,string>), null);
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (ArgumentNullException))]
68                 public void CtorComparerDictionaryNullDictionary ()
69                 {
70                         new SortedDictionary<int,string> (null, default (IComparer<int>));
71                 }
72
73                 [Test]
74                 public void CtorDefault ()
75                 {
76                         SortedDictionary<int,string> d =
77                                 new SortedDictionary<int,string> ();
78                         Assert.IsNotNull (d.Comparer);
79                 }
80
81                 [Test]
82                 public void CtorDictionary ()
83                 {
84                         Dictionary<int,string> src = new Dictionary<int,string> ();
85                         src.Add (0, "Foo");
86                         src.Add (4, "Bar");
87                         src.Add (2, "Baz");
88                         SortedDictionary<int,string> d =
89                                 new SortedDictionary<int,string> (src);
90                         Assert.AreEqual (3, d.Count, "#1");
91                         Assert.AreEqual ("Bar", d [4], "#2");
92                         IDictionaryEnumerator e = d.GetEnumerator ();
93                         Assert.IsTrue (e.MoveNext (), "#3");
94                         Assert.AreEqual ("Foo", e.Value, "#4");
95                         Assert.IsTrue (e.MoveNext (), "#5");
96                         Assert.AreEqual ("Baz", e.Value, "#6");
97                         Assert.IsTrue (e.MoveNext (), "#7");
98                         Assert.AreEqual ("Bar", e.Value, "#8");
99
100                         src.Add (3, "Hoge"); // it does not affect.
101                         Assert.AreEqual (3, d.Count, "#9");
102                 }
103
104                 [Test]
105                 [ExpectedException (typeof (ArgumentException))]
106                 public void AddDuplicate ()
107                 {
108                         SortedDictionary<int,string> d =
109                                 new SortedDictionary<int,string> ();
110                         d.Add (0, "A");
111                         d.Add (0, "A");
112                 }
113
114                 [Test]
115                 public void AddNullValue ()
116                 {
117                         SortedDictionary<int,string> d =
118                                 new SortedDictionary<int,string> ();
119                         d.Add (0, null);
120                         d.Add (1, "A");
121                         d.Add (2, null);
122                         Assert.IsNull (d [0], "#0");
123                         Assert.AreEqual ("A", d [1], "#1");
124                         Assert.IsNull (d [2], "#2");
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (ArgumentNullException))]
129                 public void AddNullKey ()
130                 {
131                         SortedDictionary<string,string> d =
132                                 new SortedDictionary<string,string> ();
133                         d.Add (null, null);
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentNullException))]
138                 public void AddNullKeyNullable ()
139                 {
140                         SortedDictionary<int?,string> d = new SortedDictionary<int?,string> ();
141                         d.Add (null, "TEST");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (KeyNotFoundException))]
146                 public void GetItemNonexistent ()
147                 {
148                         SortedDictionary<int,int> d =
149                                 new SortedDictionary<int,int> ();
150                         Assert.AreEqual (0, d [0]); // does not exist.
151                 }
152
153                 [Test]
154                 public void SetItemNonexistent ()
155                 {
156                         SortedDictionary<int,int> d =
157                                 new SortedDictionary<int,int> ();
158                         d [0] = 1;
159                         Assert.AreEqual (1, d.Count);
160                 }
161
162                 [Test]
163                 public void SetItemExistent ()
164                 {
165                         SortedDictionary<int,int> d =
166                                 new SortedDictionary<int,int> ();
167                         d.Add (0, 0);
168                         Assert.AreEqual (1, d.Count, "#1");
169                         d [0] = 1;
170                         Assert.AreEqual (1, d.Count, "#2");
171                         Assert.AreEqual (1, d [0], "#3");
172                 }
173
174                 [Test]
175                 public void GetEnumerator1 ()
176                 {
177                         SortedDictionary<int,string> d =
178                                 new SortedDictionary<int,string> ();
179                         d.Add (1, "A");
180                         d.Add (3, "B");
181                         d.Add (2, "C");
182                         SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
183                         Assert.IsTrue (e.MoveNext (), "#1");
184                         Assert.AreEqual ("A", e.Current.Value, "#2");
185                         Assert.IsTrue (e.MoveNext (), "#3");
186                         Assert.AreEqual ("C", e.Current.Value, "#4");
187                         Assert.IsTrue (e.MoveNext (), "#5");
188                         Assert.AreEqual ("B", e.Current.Value, "#6");
189                         Assert.IsFalse (e.MoveNext (), "#7");
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (InvalidOperationException))]
194                 public void GetEnumerator2 ()
195                 {
196                         SortedDictionary<int,string> d =
197                                 new SortedDictionary<int,string> ();
198                         d.Add (1, "A");
199                         d.Add (3, "B");
200                         d.Add (2, "C");
201                         IEnumerator e = d.GetEnumerator ();
202                         d.Add (4, "D");
203                         e.MoveNext ();
204                 }
205
206                 [Test]
207                 public void CustomComparer ()
208                 {
209                         SortedDictionary<int,string> d =
210                                 new SortedDictionary<int,string> (
211                                         ReverseComparer<int>.Instance);
212
213                         d.Add (1, "A");
214                         d.Add (3, "B");
215                         d.Add (2, "C");
216                         SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
217                         Assert.IsTrue (e.MoveNext (), "#1");
218                         Assert.AreEqual ("B", e.Current.Value, "#2");
219                         Assert.IsTrue (e.MoveNext (), "#3");
220                         Assert.AreEqual ("C", e.Current.Value, "#4");
221                         Assert.IsTrue (e.MoveNext (), "#5");
222                         Assert.AreEqual ("A", e.Current.Value, "#6");
223                         Assert.IsFalse (e.MoveNext (), "#7");
224                 }
225
226                 [Test]
227                 public void Remove ()
228                 {
229                         SortedDictionary<int,string> d =
230                                 new SortedDictionary<int,string> ();
231                         Assert.IsFalse (d.Remove (0), "#1");
232                         d.Add (0, "Foo");
233                         Assert.IsTrue (d.Remove (0), "#2");
234                         Assert.IsFalse (d.Remove (0), "#3");
235                 }
236
237                 [Test]
238                 public void TryGetValue ()
239                 {
240                         SortedDictionary<int,string> d =
241                                 new SortedDictionary<int,string> ();
242                         string s;
243                         Assert.IsFalse (d.TryGetValue (0, out s), "#1");
244                         Assert.IsNull (s, "#2");
245                         d.Add (0, "Test");
246                         Assert.IsTrue (d.TryGetValue (0, out s), "#3");
247                         Assert.AreEqual ("Test", s, "#4");
248                         Assert.IsFalse (d.TryGetValue (1, out s), "#5");
249                         Assert.IsNull (s, "#6");
250                 }
251
252                 [Test]
253                 public void CopyTo ()
254                 {
255                         SortedDictionary<int,string> d =
256                                 new SortedDictionary<int,string> ();                    
257                         d.Add (1, "A");                 
258                         KeyValuePair <int, string> [] array =
259                                 new KeyValuePair <int, string> [d.Count];
260                         d.CopyTo (array, 0);
261                         Assert.AreEqual (1, array.Length);
262                         Assert.AreEqual (1, array [0].Key);
263                         Assert.AreEqual ("A", array [0].Value);
264                         
265                         d = new SortedDictionary<int,string> ();                        
266                         array = new KeyValuePair <int, string> [d.Count];
267                         d.CopyTo (array, 0);
268                         Assert.AreEqual (0, array.Length);
269                         
270                         ICollection c = new SortedDictionary<int,string> ();
271                         array = new KeyValuePair <int, string> [c.Count];
272                         c.CopyTo (array, 0);
273                         Assert.AreEqual (0, array.Length);
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (ArgumentNullException))]
278                 public void IDictionaryAddKeyNull ()
279                 {
280                         IDictionary d = new SortedDictionary<string,string> ();
281                         d.Add (null, "A");
282                 }
283
284                 [Test]
285                 [ExpectedException (typeof (ArgumentNullException))]
286                 public void IDictionaryAddKeyNullValueType ()
287                 {
288                         IDictionary d = new SortedDictionary<int,string> ();
289                         d.Add (null, "A");
290                 }
291
292                 [Test]
293                 public void IDictionaryAddValueNull ()
294                 {
295                         IDictionary d = new SortedDictionary<string,string> ();
296                         // If we simply check "if (value is TValue)" it won't pass.
297                         d.Add ("A", null);
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (ArgumentException))]
302                 public void IDictionaryAddValueNullValueType ()
303                 {
304                         IDictionary d = new SortedDictionary<string,int> ();
305                         // If we simply allow null it won't result in ArgumentException.
306                         d.Add ("A", null);
307                 }
308
309                 [Test]
310                 [ExpectedException (typeof (NotSupportedException))]
311                 public void KeysICollectionAdd ()
312                 {
313                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
314                         d.Add (1, "A");
315                         ICollection<int> col = d.Keys;
316                         col.Add (2);
317                 }
318
319                 [Test]
320                 [ExpectedException (typeof (NotSupportedException))]
321                 public void KeysICollectionClear ()
322                 {
323                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
324                         d.Add (1, "A");
325                         ICollection<int> col = d.Keys;
326                         col.Clear ();
327                 }
328
329                 [Test]
330                 [ExpectedException (typeof (NotSupportedException))]
331                 public void KeysICollectionRemove ()
332                 {
333                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
334                         d.Add (1, "A");
335                         ICollection<int> col = d.Keys;
336                         col.Remove (1);
337                 }
338
339                 [Test]          
340                 public void KeysICollectionCopyTo ()
341                 {
342                         SortedDictionary<int,string> d = new SortedDictionary<int, string> ();
343                         d.Add (1, "A");
344                         ICollection<int> col = d.Keys;
345                         int[] array = new int [col.Count];
346                         col.CopyTo (array, 0);
347                         Assert.AreEqual (1, array.Length);
348                         Assert.AreEqual (1, array [0]);
349                         
350                         // Bug #497720
351                         d = new SortedDictionary<int, string> ();                       
352                         col = d.Keys;
353                         array = new int [col.Count];
354                         col.CopyTo (array, 0);
355                 }
356                 
357                 [Test]
358                 [ExpectedException (typeof (NotSupportedException))]
359                 public void ValuesICollectionAdd ()
360                 {
361                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
362                         d.Add (1, "A");
363                         ICollection<string> col = d.Values;
364                         col.Add ("B");
365                 }
366
367                 [Test]
368                 [ExpectedException (typeof (NotSupportedException))]
369                 public void ValuesICollectionClear ()
370                 {
371                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
372                         d.Add (1, "A");
373                         ICollection<string> col = d.Values;
374                         col.Clear ();
375                 }
376
377                 [Test]
378                 [ExpectedException (typeof (NotSupportedException))]
379                 public void ValuesICollectionRemove ()
380                 {
381                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
382                         d.Add (1, "A");
383                         ICollection<string> col = d.Values;
384                         col.Remove ("A");
385                 }
386
387                 [Test]          
388                 public void ValuesICollectionCopyTo ()
389                 {
390                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
391                         d.Add (1, "A");
392                         ICollection<string> col = d.Values;
393                         string[] array = new string [col.Count];
394                         col.CopyTo (array, 0);
395                         Assert.AreEqual (1, array.Length);
396                         Assert.AreEqual ("A", array [0]);
397                         
398                         d = new SortedDictionary<int,string> ();                        
399                         col = d.Values;
400                         array = new string [col.Count];
401                         col.CopyTo (array, 0);
402                 }
403
404                 [Test]
405                 public void KeysGetEnumerator1 ()
406                 {
407                         SortedDictionary<int,string> d =
408                                 new SortedDictionary<int,string> ();
409                         d.Add (1, "A");
410                         d.Add (3, "B");
411                         d.Add (2, "C");
412                         IEnumerator e = d.Keys.GetEnumerator ();
413                         Assert.IsTrue (e.MoveNext (), "#1");
414                         Assert.AreEqual (1, e.Current, "#2");
415                         Assert.IsTrue (e.MoveNext (), "#3");
416                         Assert.AreEqual (2, e.Current, "#4");
417                         Assert.IsTrue (e.MoveNext (), "#5");
418                         Assert.AreEqual (3, e.Current, "#6");
419                         Assert.IsFalse (e.MoveNext (), "#7");
420                 }
421
422                 [Test]
423                 [ExpectedException (typeof (InvalidOperationException))]
424                 public void KeysGetEnumerator2 ()
425                 {
426                         SortedDictionary<int,string> d =
427                                 new SortedDictionary<int,string> ();
428                         d.Add (1, "A");
429                         d.Add (3, "B");
430                         d.Add (2, "C");
431                         IEnumerator e = d.Keys.GetEnumerator ();
432                         d.Add (4, "D");
433                         e.MoveNext ();
434                 }
435
436                 [Test]
437                 public void ValuesGetEnumerator1 ()
438                 {
439                         SortedDictionary<int,string> d =
440                                 new SortedDictionary<int,string> ();
441                         d.Add (1, "A");
442                         d.Add (3, "B");
443                         d.Add (2, "C");
444                         IEnumerator e = d.Values.GetEnumerator ();
445                         Assert.IsTrue (e.MoveNext (), "#1");
446                         Assert.AreEqual ("A", e.Current, "#2");
447                         Assert.IsTrue (e.MoveNext (), "#3");
448                         Assert.AreEqual ("C", e.Current, "#4");
449                         Assert.IsTrue (e.MoveNext (), "#5");
450                         Assert.AreEqual ("B", e.Current, "#6");
451                         Assert.IsFalse (e.MoveNext (), "#7");
452                 }
453
454                 [Test]
455                 [ExpectedException (typeof (InvalidOperationException))]
456                 public void ValuesGetEnumerator2 ()
457                 {
458                         SortedDictionary<int,string> d =
459                                 new SortedDictionary<int,string> ();
460                         d.Add (1, "A");
461                         d.Add (3, "B");
462                         d.Add (2, "C");
463                         IEnumerator e = d.Values.GetEnumerator ();
464                         d.Add (4, "D");
465                         e.MoveNext ();
466                 }
467
468
469                 delegate void D ();
470                 bool Throws (D d)
471                 {
472                         try {
473                                 d ();
474                                 return false;
475                         } catch {
476                                 return true;
477                         }
478                 }
479
480                 [Test]
481                 // based on #491858, #517415
482                 public void Enumerator_Current ()
483                 {
484                         var e1 = new SortedDictionary<int,int>.Enumerator ();
485                         Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x);}));
486
487                         var d = new SortedDictionary<int,int> ();
488                         var e2 = d.GetEnumerator ();
489                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
490                         e2.MoveNext ();
491                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
492                         e2.Dispose ();
493                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
494
495                         var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
496                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
497                         e3.MoveNext ();
498                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
499                         e3.Dispose ();
500                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
501
502                         var e4 = ((IEnumerable) d).GetEnumerator ();
503                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
504                         e4.MoveNext ();
505                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
506                         ((IDisposable) e4).Dispose ();
507                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
508                 }
509
510                 [Test]
511                 // based on #491858, #517415
512                 public void KeyEnumerator_Current ()
513                 {
514                         var e1 = new SortedDictionary<int,int>.KeyCollection.Enumerator ();
515                         Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
516
517                         var d = new SortedDictionary<int,int> ().Keys;
518                         var e2 = d.GetEnumerator ();
519                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
520                         e2.MoveNext ();
521                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
522                         e2.Dispose ();
523                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
524
525                         var e3 = ((IEnumerable<int>) d).GetEnumerator ();
526                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
527                         e3.MoveNext ();
528                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
529                         e3.Dispose ();
530                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
531
532                         var e4 = ((IEnumerable) d).GetEnumerator ();
533                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
534                         e4.MoveNext ();
535                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
536                         ((IDisposable) e4).Dispose ();
537                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
538                 }
539
540                 [Test]
541                 // based on #491858, #517415
542                 public void ValueEnumerator_Current ()
543                 {
544                         var e1 = new SortedDictionary<int,int>.ValueCollection.Enumerator ();
545                         Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
546
547                         var d = new SortedDictionary<int,int> ().Values;
548                         var e2 = d.GetEnumerator ();
549                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
550                         e2.MoveNext ();
551                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
552                         e2.Dispose ();
553                         Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
554
555                         var e3 = ((IEnumerable<int>) d).GetEnumerator ();
556                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
557                         e3.MoveNext ();
558                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
559                         e3.Dispose ();
560                         Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
561
562                         var e4 = ((IEnumerable) d).GetEnumerator ();
563                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
564                         e4.MoveNext ();
565                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
566                         ((IDisposable) e4).Dispose ();
567                         Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
568                 }
569
570                 // Serialize a dictionary out and deserialize it back in again
571                 SortedDictionary<int, string> Roundtrip(SortedDictionary<int, string> dic)
572                 {
573                         BinaryFormatter bf = new BinaryFormatter ();
574                         MemoryStream stream = new MemoryStream ();
575                         bf.Serialize (stream, dic);
576                         stream.Position = 0;
577                         return (SortedDictionary<int, string>)bf.Deserialize (stream);
578                 }
579             
580                 [Test]
581                 public void Serialize()
582                 {
583                         SortedDictionary<int, string> test = new SortedDictionary<int, string>();
584                         test.Add(1, "a");
585                         test.Add(3, "c");
586                         test.Add(2, "b");
587
588                         SortedDictionary<int, string> result = Roundtrip(test);
589                         Assert.AreEqual(3, result.Count);
590
591                         Assert.AreEqual("a", result[1]);
592                         Assert.AreEqual("b", result[2]);
593                         Assert.AreEqual("c", result[3]);
594                 }
595
596                 [Test]
597                 public void SerializeReverseComparer()
598                 {
599                         SortedDictionary<int,string> test =
600                                 new SortedDictionary<int,string> (
601                                         ReverseComparer<int>.Instance);
602
603                         test.Add (1, "A");
604                         test.Add (3, "B");
605                         test.Add (2, "C");
606
607                         SortedDictionary<int,string> result = Roundtrip (test);
608                     
609                         SortedDictionary<int,string>.Enumerator e = result.GetEnumerator ();
610                         Assert.IsTrue (e.MoveNext (), "#1");
611                         Assert.AreEqual ("B", e.Current.Value, "#2");
612                         Assert.IsTrue (e.MoveNext (), "#3");
613                         Assert.AreEqual ("C", e.Current.Value, "#4");
614                         Assert.IsTrue (e.MoveNext (), "#5");
615                         Assert.AreEqual ("A", e.Current.Value, "#6");
616                         Assert.IsFalse (e.MoveNext (), "#7");
617                 }
618         }
619
620         [Serializable]
621         class ReverseComparer<T> : IComparer<T>
622         {
623                 static ReverseComparer<T> instance = new ReverseComparer<T> ();
624                 public static ReverseComparer<T> Instance {
625                         get { return instance; }
626                 }
627
628                 ReverseComparer ()
629                 {
630                 }
631
632                 public int Compare (T t1, T t2)
633                 {
634                         return Comparer<T>.Default.Compare (t2, t1);
635                 }
636         }
637 }
638
639 #endif