2 // SortedDictionaryTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.Runtime.Serialization.Formatters.Binary;
37 using NUnit.Framework;
39 namespace MonoTests.System.Collections.Generic
42 public class SortedDictionaryTest
45 public void CtorNullComparer ()
47 SortedDictionary<int,string> sd =
48 new SortedDictionary<int,string> ((IComparer<int>) null);
49 Assert.AreEqual (Comparer<int>.Default, sd.Comparer);
53 [ExpectedException (typeof (ArgumentNullException))]
54 public void CtorNullDictionary ()
56 new SortedDictionary<int,string> (default (IDictionary<int,string>));
60 [ExpectedException (typeof (ArgumentNullException))]
61 public void CtorComparerDictionaryNullComparer ()
63 new SortedDictionary<int,string> (default (IDictionary<int,string>), null);
67 [ExpectedException (typeof (ArgumentNullException))]
68 public void CtorComparerDictionaryNullDictionary ()
70 new SortedDictionary<int,string> (null, default (IComparer<int>));
74 public void CtorDefault ()
76 SortedDictionary<int,string> d =
77 new SortedDictionary<int,string> ();
78 Assert.IsNotNull (d.Comparer);
82 public void CtorDictionary ()
84 Dictionary<int,string> src = new Dictionary<int,string> ();
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");
100 src.Add (3, "Hoge"); // it does not affect.
101 Assert.AreEqual (3, d.Count, "#9");
105 [ExpectedException (typeof (ArgumentException))]
106 public void AddDuplicate ()
108 SortedDictionary<int,string> d =
109 new SortedDictionary<int,string> ();
115 public void AddNullValue ()
117 SortedDictionary<int,string> d =
118 new SortedDictionary<int,string> ();
122 Assert.IsNull (d [0], "#0");
123 Assert.AreEqual ("A", d [1], "#1");
124 Assert.IsNull (d [2], "#2");
128 [ExpectedException (typeof (ArgumentNullException))]
129 public void AddNullKey ()
131 SortedDictionary<string,string> d =
132 new SortedDictionary<string,string> ();
137 [ExpectedException (typeof (ArgumentNullException))]
138 public void AddNullKeyNullable ()
140 SortedDictionary<int?,string> d = new SortedDictionary<int?,string> ();
141 d.Add (null, "TEST");
145 [ExpectedException (typeof (KeyNotFoundException))]
146 public void GetItemNonexistent ()
148 SortedDictionary<int,int> d =
149 new SortedDictionary<int,int> ();
150 Assert.AreEqual (0, d [0]); // does not exist.
154 public void SetItemNonexistent ()
156 SortedDictionary<int,int> d =
157 new SortedDictionary<int,int> ();
159 Assert.AreEqual (1, d.Count);
163 public void SetItemExistent ()
165 SortedDictionary<int,int> d =
166 new SortedDictionary<int,int> ();
168 Assert.AreEqual (1, d.Count, "#1");
170 Assert.AreEqual (1, d.Count, "#2");
171 Assert.AreEqual (1, d [0], "#3");
175 public void GetEnumerator1 ()
177 SortedDictionary<int,string> d =
178 new SortedDictionary<int,string> ();
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");
193 [ExpectedException (typeof (InvalidOperationException))]
194 public void GetEnumerator2 ()
196 SortedDictionary<int,string> d =
197 new SortedDictionary<int,string> ();
201 IEnumerator e = d.GetEnumerator ();
207 public void CustomComparer ()
209 SortedDictionary<int,string> d =
210 new SortedDictionary<int,string> (
211 ReverseComparer<int>.Instance);
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");
227 public void Remove ()
229 SortedDictionary<int,string> d =
230 new SortedDictionary<int,string> ();
231 Assert.IsFalse (d.Remove (0), "#1");
233 Assert.IsTrue (d.Remove (0), "#2");
234 Assert.IsFalse (d.Remove (0), "#3");
238 public void TryGetValue ()
240 SortedDictionary<int,string> d =
241 new SortedDictionary<int,string> ();
243 Assert.IsFalse (d.TryGetValue (0, out s), "#1");
244 Assert.IsNull (s, "#2");
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");
253 public void CopyTo ()
255 SortedDictionary<int,string> d =
256 new SortedDictionary<int,string> ();
258 KeyValuePair <int, string> [] array =
259 new KeyValuePair <int, string> [d.Count];
261 Assert.AreEqual (1, array.Length);
262 Assert.AreEqual (1, array [0].Key);
263 Assert.AreEqual ("A", array [0].Value);
265 d = new SortedDictionary<int,string> ();
266 array = new KeyValuePair <int, string> [d.Count];
268 Assert.AreEqual (0, array.Length);
270 ICollection c = new SortedDictionary<int,string> ();
271 array = new KeyValuePair <int, string> [c.Count];
273 Assert.AreEqual (0, array.Length);
277 [ExpectedException (typeof (ArgumentNullException))]
278 public void IDictionaryAddKeyNull ()
280 IDictionary d = new SortedDictionary<string,string> ();
285 [ExpectedException (typeof (ArgumentNullException))]
286 public void IDictionaryAddKeyNullValueType ()
288 IDictionary d = new SortedDictionary<int,string> ();
293 public void IDictionaryAddValueNull ()
295 IDictionary d = new SortedDictionary<string,string> ();
296 // If we simply check "if (value is TValue)" it won't pass.
301 [ExpectedException (typeof (ArgumentException))]
302 public void IDictionaryAddValueNullValueType ()
304 IDictionary d = new SortedDictionary<string,int> ();
305 // If we simply allow null it won't result in ArgumentException.
310 [ExpectedException (typeof (NotSupportedException))]
311 public void KeysICollectionAdd ()
313 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
315 ICollection<int> col = d.Keys;
320 [ExpectedException (typeof (NotSupportedException))]
321 public void KeysICollectionClear ()
323 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
325 ICollection<int> col = d.Keys;
330 [ExpectedException (typeof (NotSupportedException))]
331 public void KeysICollectionRemove ()
333 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
335 ICollection<int> col = d.Keys;
340 public void KeysICollectionCopyTo ()
342 SortedDictionary<int,string> d = new SortedDictionary<int, string> ();
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]);
351 d = new SortedDictionary<int, string> ();
353 array = new int [col.Count];
354 col.CopyTo (array, 0);
358 [ExpectedException (typeof (NotSupportedException))]
359 public void ValuesICollectionAdd ()
361 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
363 ICollection<string> col = d.Values;
368 [ExpectedException (typeof (NotSupportedException))]
369 public void ValuesICollectionClear ()
371 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
373 ICollection<string> col = d.Values;
378 [ExpectedException (typeof (NotSupportedException))]
379 public void ValuesICollectionRemove ()
381 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
383 ICollection<string> col = d.Values;
388 public void ValuesICollectionCopyTo ()
390 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
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]);
398 d = new SortedDictionary<int,string> ();
400 array = new string [col.Count];
401 col.CopyTo (array, 0);
405 public void KeysGetEnumerator1 ()
407 SortedDictionary<int,string> d =
408 new SortedDictionary<int,string> ();
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");
423 [ExpectedException (typeof (InvalidOperationException))]
424 public void KeysGetEnumerator2 ()
426 SortedDictionary<int,string> d =
427 new SortedDictionary<int,string> ();
431 IEnumerator e = d.Keys.GetEnumerator ();
437 public void ValuesGetEnumerator1 ()
439 SortedDictionary<int,string> d =
440 new SortedDictionary<int,string> ();
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");
455 [ExpectedException (typeof (InvalidOperationException))]
456 public void ValuesGetEnumerator2 ()
458 SortedDictionary<int,string> d =
459 new SortedDictionary<int,string> ();
463 IEnumerator e = d.Values.GetEnumerator ();
481 // based on #491858, #517415
482 public void Enumerator_Current ()
484 var e1 = new SortedDictionary<int,int>.Enumerator ();
485 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x);}));
487 var d = new SortedDictionary<int,int> ();
488 var e2 = d.GetEnumerator ();
489 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
491 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
493 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
495 var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
496 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
498 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
500 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
502 var e4 = ((IEnumerable) d).GetEnumerator ();
503 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
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);}));
511 // based on #491858, #517415
512 public void KeyEnumerator_Current ()
514 var e1 = new SortedDictionary<int,int>.KeyCollection.Enumerator ();
515 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
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); }));
521 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
523 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
525 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
526 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
528 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
530 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
532 var e4 = ((IEnumerable) d).GetEnumerator ();
533 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
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); }));
541 // based on #491858, #517415
542 public void ValueEnumerator_Current ()
544 var e1 = new SortedDictionary<int,int>.ValueCollection.Enumerator ();
545 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
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); }));
551 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
553 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); }));
555 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
556 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
558 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
560 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
562 var e4 = ((IEnumerable) d).GetEnumerator ();
563 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
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); }));
570 // Serialize a dictionary out and deserialize it back in again
571 SortedDictionary<int, string> Roundtrip(SortedDictionary<int, string> dic)
573 BinaryFormatter bf = new BinaryFormatter ();
574 MemoryStream stream = new MemoryStream ();
575 bf.Serialize (stream, dic);
577 return (SortedDictionary<int, string>)bf.Deserialize (stream);
581 public void Serialize()
583 SortedDictionary<int, string> test = new SortedDictionary<int, string>();
588 SortedDictionary<int, string> result = Roundtrip(test);
589 Assert.AreEqual(3, result.Count);
591 Assert.AreEqual("a", result[1]);
592 Assert.AreEqual("b", result[2]);
593 Assert.AreEqual("c", result[3]);
597 public void SerializeReverseComparer()
599 SortedDictionary<int,string> test =
600 new SortedDictionary<int,string> (
601 ReverseComparer<int>.Instance);
607 SortedDictionary<int,string> result = Roundtrip (test);
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");
621 class ReverseComparer<T> : IComparer<T>
623 static ReverseComparer<T> instance = new ReverseComparer<T> ();
624 public static ReverseComparer<T> Instance {
625 get { return instance; }
632 public int Compare (T t1, T t2)
634 return Comparer<T>.Default.Compare (t2, t1);