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.
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Runtime.Serialization;
36 using NUnit.Framework;
38 namespace MonoTests.System.Collections.Generic
41 public class SortedDictionaryTest
44 public void CtorNullComparer ()
46 SortedDictionary<int,string> sd =
47 new SortedDictionary<int,string> ((IComparer<int>) null);
48 Assert.AreEqual (Comparer<int>.Default, sd.Comparer);
52 [ExpectedException (typeof (ArgumentNullException))]
53 public void CtorNullDictionary ()
55 new SortedDictionary<int,string> (default (IDictionary<int,string>));
59 [ExpectedException (typeof (ArgumentNullException))]
60 public void CtorComparerDictionaryNullComparer ()
62 new SortedDictionary<int,string> (default (IDictionary<int,string>), null);
66 [ExpectedException (typeof (ArgumentNullException))]
67 public void CtorComparerDictionaryNullDictionary ()
69 new SortedDictionary<int,string> (null, default (IComparer<int>));
73 public void CtorDefault ()
75 SortedDictionary<int,string> d =
76 new SortedDictionary<int,string> ();
77 Assert.IsNotNull (d.Comparer);
81 public void CtorDictionary ()
83 Dictionary<int,string> src = new Dictionary<int,string> ();
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");
99 src.Add (3, "Hoge"); // it does not affect.
100 Assert.AreEqual (3, d.Count, "#9");
104 [ExpectedException (typeof (ArgumentException))]
105 public void AddDuplicate ()
107 SortedDictionary<int,string> d =
108 new SortedDictionary<int,string> ();
114 public void AddNullValue ()
116 SortedDictionary<int,string> d =
117 new SortedDictionary<int,string> ();
121 Assert.IsNull (d [0], "#0");
122 Assert.AreEqual ("A", d [1], "#1");
123 Assert.IsNull (d [2], "#2");
127 [ExpectedException (typeof (ArgumentNullException))]
128 public void AddNullKey ()
130 SortedDictionary<string,string> d =
131 new SortedDictionary<string,string> ();
136 [ExpectedException (typeof (ArgumentNullException))]
137 public void AddNullKeyNullable ()
139 SortedDictionary<int?,string> d = new SortedDictionary<int?,string> ();
140 d.Add (null, "TEST");
144 [ExpectedException (typeof (KeyNotFoundException))]
145 public void GetItemNonexistent ()
147 SortedDictionary<int,int> d =
148 new SortedDictionary<int,int> ();
149 Assert.AreEqual (0, d [0]); // does not exist.
153 public void SetItemNonexistent ()
155 SortedDictionary<int,int> d =
156 new SortedDictionary<int,int> ();
158 Assert.AreEqual (1, d.Count);
162 public void SetItemExistent ()
164 SortedDictionary<int,int> d =
165 new SortedDictionary<int,int> ();
167 Assert.AreEqual (1, d.Count, "#1");
169 Assert.AreEqual (1, d.Count, "#2");
170 Assert.AreEqual (1, d [0], "#3");
174 public void GetEnumerator1 ()
176 SortedDictionary<int,string> d =
177 new SortedDictionary<int,string> ();
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");
192 [ExpectedException (typeof (InvalidOperationException))]
193 public void GetEnumerator2 ()
195 SortedDictionary<int,string> d =
196 new SortedDictionary<int,string> ();
200 IEnumerator e = d.GetEnumerator ();
206 public void CustomComparer ()
208 SortedDictionary<int,string> d =
209 new SortedDictionary<int,string> (
210 ReverseComparer<int>.Instance);
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");
226 public void Remove ()
228 SortedDictionary<int,string> d =
229 new SortedDictionary<int,string> ();
230 Assert.IsFalse (d.Remove (0), "#1");
232 Assert.IsTrue (d.Remove (0), "#2");
233 Assert.IsFalse (d.Remove (0), "#3");
237 public void TryGetValue ()
239 SortedDictionary<int,string> d =
240 new SortedDictionary<int,string> ();
242 Assert.IsFalse (d.TryGetValue (0, out s), "#1");
243 Assert.IsNull (s, "#2");
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");
252 public void CopyTo ()
254 SortedDictionary<int,string> d =
255 new SortedDictionary<int,string> ();
257 KeyValuePair <int, string> [] array =
258 new KeyValuePair <int, string> [d.Count];
260 Assert.AreEqual (1, array.Length);
261 Assert.AreEqual (1, array [0].Key);
262 Assert.AreEqual ("A", array [0].Value);
264 d = new SortedDictionary<int,string> ();
265 array = new KeyValuePair <int, string> [d.Count];
267 Assert.AreEqual (0, array.Length);
269 ICollection c = new SortedDictionary<int,string> ();
270 array = new KeyValuePair <int, string> [c.Count];
272 Assert.AreEqual (0, array.Length);
276 [ExpectedException (typeof (ArgumentNullException))]
277 public void IDictionaryAddKeyNull ()
279 IDictionary d = new SortedDictionary<string,string> ();
284 [ExpectedException (typeof (ArgumentNullException))]
285 public void IDictionaryAddKeyNullValueType ()
287 IDictionary d = new SortedDictionary<int,string> ();
292 public void IDictionaryAddValueNull ()
294 IDictionary d = new SortedDictionary<string,string> ();
295 // If we simply check "if (value is TValue)" it won't pass.
300 [ExpectedException (typeof (ArgumentException))]
301 public void IDictionaryAddValueNullValueType ()
303 IDictionary d = new SortedDictionary<string,int> ();
304 // If we simply allow null it won't result in ArgumentException.
309 [ExpectedException (typeof (NotSupportedException))]
310 public void KeysICollectionAdd ()
312 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
314 ICollection<int> col = d.Keys;
319 [ExpectedException (typeof (NotSupportedException))]
320 public void KeysICollectionClear ()
322 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
324 ICollection<int> col = d.Keys;
329 [ExpectedException (typeof (NotSupportedException))]
330 public void KeysICollectionRemove ()
332 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
334 ICollection<int> col = d.Keys;
339 public void KeysICollectionCopyTo ()
341 SortedDictionary<int,string> d = new SortedDictionary<int, string> ();
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]);
350 d = new SortedDictionary<int, string> ();
352 array = new int [col.Count];
353 col.CopyTo (array, 0);
357 [ExpectedException (typeof (NotSupportedException))]
358 public void ValuesICollectionAdd ()
360 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
362 ICollection<string> col = d.Values;
367 [ExpectedException (typeof (NotSupportedException))]
368 public void ValuesICollectionClear ()
370 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
372 ICollection<string> col = d.Values;
377 [ExpectedException (typeof (NotSupportedException))]
378 public void ValuesICollectionRemove ()
380 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
382 ICollection<string> col = d.Values;
387 public void ValuesICollectionCopyTo ()
389 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
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]);
397 d = new SortedDictionary<int,string> ();
399 array = new string [col.Count];
400 col.CopyTo (array, 0);
404 public void KeysGetEnumerator1 ()
406 SortedDictionary<int,string> d =
407 new SortedDictionary<int,string> ();
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");
422 [ExpectedException (typeof (InvalidOperationException))]
423 public void KeysGetEnumerator2 ()
425 SortedDictionary<int,string> d =
426 new SortedDictionary<int,string> ();
430 IEnumerator e = d.Keys.GetEnumerator ();
436 public void ValuesGetEnumerator1 ()
438 SortedDictionary<int,string> d =
439 new SortedDictionary<int,string> ();
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");
454 [ExpectedException (typeof (InvalidOperationException))]
455 public void ValuesGetEnumerator2 ()
457 SortedDictionary<int,string> d =
458 new SortedDictionary<int,string> ();
462 IEnumerator e = d.Values.GetEnumerator ();
480 // based on #491858, #517415
481 public void Enumerator_Current ()
483 var e1 = new SortedDictionary<int,int>.Enumerator ();
484 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
486 var d = new SortedDictionary<int,int> ();
487 var e2 = d.GetEnumerator ();
488 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
490 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
492 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
494 var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
495 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
497 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
499 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
501 var e4 = ((IEnumerable) d).GetEnumerator ();
502 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
504 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
505 ((IDisposable) e4).Dispose ();
506 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
510 // based on #491858, #517415
511 public void KeyEnumerator_Current ()
513 var e1 = new SortedDictionary<int,int>.KeyCollection.Enumerator ();
514 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
516 var d = new SortedDictionary<int,int> ().Keys;
517 var e2 = d.GetEnumerator ();
518 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
520 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
522 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
524 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
525 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
527 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
529 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
531 var e4 = ((IEnumerable) d).GetEnumerator ();
532 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
534 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
535 ((IDisposable) e4).Dispose ();
536 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
540 // based on #491858, #517415
541 public void ValueEnumerator_Current ()
543 var e1 = new SortedDictionary<int,int>.ValueCollection.Enumerator ();
544 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
546 var d = new SortedDictionary<int,int> ().Values;
547 var e2 = d.GetEnumerator ();
548 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
550 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
552 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
554 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
555 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
557 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
559 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
561 var e4 = ((IEnumerable) d).GetEnumerator ();
562 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
564 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
565 ((IDisposable) e4).Dispose ();
566 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
570 class ReverseComparer<T> : IComparer<T>
572 static ReverseComparer<T> instance = new ReverseComparer<T> ();
573 public static ReverseComparer<T> Instance {
574 get { return instance; }
581 public int Compare (T t1, T t2)
583 return Comparer<T>.Default.Compare (t2, t1);