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.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Runtime.Serialization.Formatters.Binary;
35 using NUnit.Framework;
37 namespace MonoTests.System.Collections.Generic
40 public class SortedDictionaryTest
43 public void CtorNullComparer ()
45 SortedDictionary<int,string> sd =
46 new SortedDictionary<int,string> ((IComparer<int>) null);
47 Assert.AreEqual (Comparer<int>.Default, sd.Comparer);
51 [ExpectedException (typeof (ArgumentNullException))]
52 public void CtorNullDictionary ()
54 new SortedDictionary<int,string> (default (IDictionary<int,string>));
58 [ExpectedException (typeof (ArgumentNullException))]
59 public void CtorComparerDictionaryNullComparer ()
61 new SortedDictionary<int,string> (default (IDictionary<int,string>), null);
65 [ExpectedException (typeof (ArgumentNullException))]
66 public void CtorComparerDictionaryNullDictionary ()
68 new SortedDictionary<int,string> (null, default (IComparer<int>));
72 public void CtorDefault ()
74 SortedDictionary<int,string> d =
75 new SortedDictionary<int,string> ();
76 Assert.IsNotNull (d.Comparer);
80 public void CtorDictionary ()
82 Dictionary<int,string> src = new Dictionary<int,string> ();
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");
98 src.Add (3, "Hoge"); // it does not affect.
99 Assert.AreEqual (3, d.Count, "#9");
103 [ExpectedException (typeof (ArgumentException))]
104 public void AddDuplicate ()
106 SortedDictionary<int,string> d =
107 new SortedDictionary<int,string> ();
113 public void AddNullValue ()
115 SortedDictionary<int,string> d =
116 new SortedDictionary<int,string> ();
120 Assert.IsNull (d [0], "#0");
121 Assert.AreEqual ("A", d [1], "#1");
122 Assert.IsNull (d [2], "#2");
126 [ExpectedException (typeof (ArgumentNullException))]
127 public void AddNullKey ()
129 SortedDictionary<string,string> d =
130 new SortedDictionary<string,string> ();
135 [ExpectedException (typeof (ArgumentNullException))]
136 public void AddNullKeyNullable ()
138 SortedDictionary<int?,string> d = new SortedDictionary<int?,string> ();
139 d.Add (null, "TEST");
143 [ExpectedException (typeof (KeyNotFoundException))]
144 public void GetItemNonexistent ()
146 SortedDictionary<int,int> d =
147 new SortedDictionary<int,int> ();
148 Assert.AreEqual (0, d [0]); // does not exist.
152 public void SetItemNonexistent ()
154 SortedDictionary<int,int> d =
155 new SortedDictionary<int,int> ();
157 Assert.AreEqual (1, d.Count);
161 public void SetItemExistent ()
163 SortedDictionary<int,int> d =
164 new SortedDictionary<int,int> ();
166 Assert.AreEqual (1, d.Count, "#1");
168 Assert.AreEqual (1, d.Count, "#2");
169 Assert.AreEqual (1, d [0], "#3");
173 public void GetEnumerator1 ()
175 SortedDictionary<int,string> d =
176 new SortedDictionary<int,string> ();
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");
191 [ExpectedException (typeof (InvalidOperationException))]
192 public void GetEnumerator2 ()
194 SortedDictionary<int,string> d =
195 new SortedDictionary<int,string> ();
199 IEnumerator e = d.GetEnumerator ();
205 public void CustomComparer ()
207 SortedDictionary<int,string> d =
208 new SortedDictionary<int,string> (
209 ReverseComparer<int>.Instance);
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");
225 public void Remove ()
227 SortedDictionary<int,string> d =
228 new SortedDictionary<int,string> ();
229 Assert.IsFalse (d.Remove (0), "#1");
231 Assert.IsTrue (d.Remove (0), "#2");
232 Assert.IsFalse (d.Remove (0), "#3");
236 public void TryGetValue ()
238 SortedDictionary<int,string> d =
239 new SortedDictionary<int,string> ();
241 Assert.IsFalse (d.TryGetValue (0, out s), "#1");
242 Assert.IsNull (s, "#2");
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");
251 public void CopyTo ()
253 SortedDictionary<int,string> d =
254 new SortedDictionary<int,string> ();
256 KeyValuePair <int, string> [] array =
257 new KeyValuePair <int, string> [d.Count];
259 Assert.AreEqual (1, array.Length);
260 Assert.AreEqual (1, array [0].Key);
261 Assert.AreEqual ("A", array [0].Value);
263 d = new SortedDictionary<int,string> ();
264 array = new KeyValuePair <int, string> [d.Count];
266 Assert.AreEqual (0, array.Length);
268 ICollection c = new SortedDictionary<int,string> ();
269 array = new KeyValuePair <int, string> [c.Count];
271 Assert.AreEqual (0, array.Length);
275 [ExpectedException (typeof (ArgumentNullException))]
276 public void IDictionaryAddKeyNull ()
278 IDictionary d = new SortedDictionary<string,string> ();
283 [ExpectedException (typeof (ArgumentNullException))]
284 public void IDictionaryAddKeyNullValueType ()
286 IDictionary d = new SortedDictionary<int,string> ();
291 public void IDictionaryAddValueNull ()
293 IDictionary d = new SortedDictionary<string,string> ();
294 // If we simply check "if (value is TValue)" it won't pass.
299 [ExpectedException (typeof (ArgumentException))]
300 public void IDictionaryAddValueNullValueType ()
302 IDictionary d = new SortedDictionary<string,int> ();
303 // If we simply allow null it won't result in ArgumentException.
308 [ExpectedException (typeof (NotSupportedException))]
309 public void KeysICollectionAdd ()
311 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
313 ICollection<int> col = d.Keys;
318 [ExpectedException (typeof (NotSupportedException))]
319 public void KeysICollectionClear ()
321 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
323 ICollection<int> col = d.Keys;
328 [ExpectedException (typeof (NotSupportedException))]
329 public void KeysICollectionRemove ()
331 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
333 ICollection<int> col = d.Keys;
338 public void KeysICollectionCopyTo ()
340 SortedDictionary<int,string> d = new SortedDictionary<int, string> ();
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]);
349 d = new SortedDictionary<int, string> ();
351 array = new int [col.Count];
352 col.CopyTo (array, 0);
356 [ExpectedException (typeof (NotSupportedException))]
357 public void ValuesICollectionAdd ()
359 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
361 ICollection<string> col = d.Values;
366 [ExpectedException (typeof (NotSupportedException))]
367 public void ValuesICollectionClear ()
369 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
371 ICollection<string> col = d.Values;
376 [ExpectedException (typeof (NotSupportedException))]
377 public void ValuesICollectionRemove ()
379 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
381 ICollection<string> col = d.Values;
386 public void ValuesICollectionCopyTo ()
388 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
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]);
396 d = new SortedDictionary<int,string> ();
398 array = new string [col.Count];
399 col.CopyTo (array, 0);
403 public void KeysGetEnumerator1 ()
405 SortedDictionary<int,string> d =
406 new SortedDictionary<int,string> ();
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");
421 [ExpectedException (typeof (InvalidOperationException))]
422 public void KeysGetEnumerator2 ()
424 SortedDictionary<int,string> d =
425 new SortedDictionary<int,string> ();
429 IEnumerator e = d.Keys.GetEnumerator ();
435 public void ValuesGetEnumerator1 ()
437 SortedDictionary<int,string> d =
438 new SortedDictionary<int,string> ();
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");
453 [ExpectedException (typeof (InvalidOperationException))]
454 public void ValuesGetEnumerator2 ()
456 SortedDictionary<int,string> d =
457 new SortedDictionary<int,string> ();
461 IEnumerator e = d.Values.GetEnumerator ();
479 // based on #491858, #517415
480 public void Enumerator_Current ()
482 var e1 = new SortedDictionary<int,int>.Enumerator ();
483 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x);}));
485 var d = new SortedDictionary<int,int> ();
486 var e2 = d.GetEnumerator ();
487 Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);}));
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 var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
494 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);}));
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 var e4 = ((IEnumerable) d).GetEnumerator ();
501 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);}));
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);}));
509 // based on #491858, #517415
510 public void KeyEnumerator_Current ()
512 var e1 = new SortedDictionary<int,int>.KeyCollection.Enumerator ();
513 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
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); }));
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 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
524 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
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 var e4 = ((IEnumerable) d).GetEnumerator ();
531 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
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); }));
539 // based on #491858, #517415
540 public void ValueEnumerator_Current ()
542 var e1 = new SortedDictionary<int,int>.ValueCollection.Enumerator ();
543 Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); }));
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); }));
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 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
554 Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); }));
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 var e4 = ((IEnumerable) d).GetEnumerator ();
561 Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); }));
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); }));
569 public void ValueEnumeratorNotEmpty_Current ()
571 var dict = new SortedDictionary<int, string> ();
574 IEnumerator e = new SortedDictionary<int, string>.ValueCollection (dict).GetEnumerator ();
575 while (e.MoveNext()) {}
577 Assert.IsTrue (Throws (delegate { var x = e.Current; }));
580 // Serialize a dictionary out and deserialize it back in again
581 SortedDictionary<int, string> Roundtrip(SortedDictionary<int, string> dic)
583 BinaryFormatter bf = new BinaryFormatter ();
584 MemoryStream stream = new MemoryStream ();
585 bf.Serialize (stream, dic);
587 return (SortedDictionary<int, string>)bf.Deserialize (stream);
591 public void Serialize()
593 SortedDictionary<int, string> test = new SortedDictionary<int, string>();
598 SortedDictionary<int, string> result = Roundtrip(test);
599 Assert.AreEqual(3, result.Count);
601 Assert.AreEqual("a", result[1]);
602 Assert.AreEqual("b", result[2]);
603 Assert.AreEqual("c", result[3]);
607 public void SerializeReverseComparer()
609 SortedDictionary<int,string> test =
610 new SortedDictionary<int,string> (
611 ReverseComparer<int>.Instance);
617 SortedDictionary<int,string> result = Roundtrip (test);
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");
631 class ReverseComparer<T> : IComparer<T>
633 static ReverseComparer<T> instance = new ReverseComparer<T> ();
634 public static ReverseComparer<T> Instance {
635 get { return instance; }
642 public int Compare (T t1, T t2)
644 return Comparer<T>.Default.Compare (t2, t1);