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