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