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 [Category ("NotWorking")] // see bug #78019
138 public void AddNullKeyNullable ()
140 SortedDictionary<Nullable<int>,string> d =
141 new SortedDictionary<Nullable<int>,string> ();
142 d.Add (null, "TEST");
146 [ExpectedException (typeof (KeyNotFoundException))]
147 public void GetItemNonexistent ()
149 SortedDictionary<int,int> d =
150 new SortedDictionary<int,int> ();
151 Assert.AreEqual (0, d [0]); // does not exist.
155 public void SetItemNonexistent ()
157 SortedDictionary<int,int> d =
158 new SortedDictionary<int,int> ();
160 Assert.AreEqual (1, d.Count);
164 public void SetItemExistent ()
166 SortedDictionary<int,int> d =
167 new SortedDictionary<int,int> ();
169 Assert.AreEqual (1, d.Count, "#1");
171 Assert.AreEqual (1, d.Count, "#2");
172 Assert.AreEqual (1, d [0], "#3");
176 public void GetEnumerator1 ()
178 SortedDictionary<int,string> d =
179 new SortedDictionary<int,string> ();
183 SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
184 Assert.IsTrue (e.MoveNext (), "#1");
185 Assert.AreEqual ("A", e.Current.Value, "#2");
186 Assert.IsTrue (e.MoveNext (), "#3");
187 Assert.AreEqual ("C", e.Current.Value, "#4");
188 Assert.IsTrue (e.MoveNext (), "#5");
189 Assert.AreEqual ("B", e.Current.Value, "#6");
190 Assert.IsFalse (e.MoveNext (), "#7");
194 [ExpectedException (typeof (InvalidOperationException))]
195 public void GetEnumerator2 ()
197 SortedDictionary<int,string> d =
198 new SortedDictionary<int,string> ();
202 IEnumerator e = d.GetEnumerator ();
208 public void CustomComparer ()
210 SortedDictionary<int,string> d =
211 new SortedDictionary<int,string> (
212 ReverseComparer<int>.Instance);
217 SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
218 Assert.IsTrue (e.MoveNext (), "#1");
219 Assert.AreEqual ("B", e.Current.Value, "#2");
220 Assert.IsTrue (e.MoveNext (), "#3");
221 Assert.AreEqual ("C", e.Current.Value, "#4");
222 Assert.IsTrue (e.MoveNext (), "#5");
223 Assert.AreEqual ("A", e.Current.Value, "#6");
224 Assert.IsFalse (e.MoveNext (), "#7");
228 public void Remove ()
230 SortedDictionary<int,string> d =
231 new SortedDictionary<int,string> ();
232 Assert.IsFalse (d.Remove (0), "#1");
234 Assert.IsTrue (d.Remove (0), "#2");
235 Assert.IsFalse (d.Remove (0), "#3");
239 public void TryGetValue ()
241 SortedDictionary<int,string> d =
242 new SortedDictionary<int,string> ();
244 Assert.IsFalse (d.TryGetValue (0, out s), "#1");
245 Assert.IsNull (s, "#2");
247 Assert.IsTrue (d.TryGetValue (0, out s), "#3");
248 Assert.AreEqual ("Test", s, "#4");
249 Assert.IsFalse (d.TryGetValue (1, out s), "#5");
250 Assert.IsNull (s, "#6");
254 [ExpectedException (typeof (ArgumentNullException))]
255 public void IDictionaryAddKeyNull ()
257 IDictionary d = new SortedDictionary<string,string> ();
262 [ExpectedException (typeof (ArgumentNullException))]
263 public void IDictionaryAddKeyNullValueType ()
265 IDictionary d = new SortedDictionary<int,string> ();
270 public void IDictionaryAddValueNull ()
272 IDictionary d = new SortedDictionary<string,string> ();
273 // If we simply check "if (value is TValue)" it won't pass.
278 [ExpectedException (typeof (ArgumentException))]
279 public void IDictionaryAddValueNullValueType ()
281 IDictionary d = new SortedDictionary<string,int> ();
282 // If we simply allow null it won't result in ArgumentException.
287 [ExpectedException (typeof (NotSupportedException))]
288 public void KeysICollectionAdd ()
290 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
292 ICollection<int> col = d.Keys;
297 [ExpectedException (typeof (NotSupportedException))]
298 public void KeysICollectionClear ()
300 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
302 ICollection<int> col = d.Keys;
307 [ExpectedException (typeof (NotSupportedException))]
308 public void KeysICollectionRemove ()
310 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
312 ICollection<int> col = d.Keys;
317 [ExpectedException (typeof (NotSupportedException))]
318 public void ValuesICollectionAdd ()
320 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
322 ICollection<string> col = d.Values;
327 [ExpectedException (typeof (NotSupportedException))]
328 public void ValuesICollectionClear ()
330 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
332 ICollection<string> col = d.Values;
337 [ExpectedException (typeof (NotSupportedException))]
338 public void ValuesICollectionRemove ()
340 SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
342 ICollection<string> col = d.Values;
347 public void KeysGetEnumerator1 ()
349 SortedDictionary<int,string> d =
350 new SortedDictionary<int,string> ();
354 IEnumerator e = d.Keys.GetEnumerator ();
355 Assert.IsTrue (e.MoveNext (), "#1");
356 Assert.AreEqual (1, e.Current, "#2");
357 Assert.IsTrue (e.MoveNext (), "#3");
358 Assert.AreEqual (2, e.Current, "#4");
359 Assert.IsTrue (e.MoveNext (), "#5");
360 Assert.AreEqual (3, e.Current, "#6");
361 Assert.IsFalse (e.MoveNext (), "#7");
365 [ExpectedException (typeof (InvalidOperationException))]
366 public void KeysGetEnumerator2 ()
368 SortedDictionary<int,string> d =
369 new SortedDictionary<int,string> ();
373 IEnumerator e = d.Keys.GetEnumerator ();
379 public void ValuesGetEnumerator1 ()
381 SortedDictionary<int,string> d =
382 new SortedDictionary<int,string> ();
386 IEnumerator e = d.Values.GetEnumerator ();
387 Assert.IsTrue (e.MoveNext (), "#1");
388 Assert.AreEqual ("A", e.Current, "#2");
389 Assert.IsTrue (e.MoveNext (), "#3");
390 Assert.AreEqual ("C", e.Current, "#4");
391 Assert.IsTrue (e.MoveNext (), "#5");
392 Assert.AreEqual ("B", e.Current, "#6");
393 Assert.IsFalse (e.MoveNext (), "#7");
397 [ExpectedException (typeof (InvalidOperationException))]
398 public void ValuesGetEnumerator2 ()
400 SortedDictionary<int,string> d =
401 new SortedDictionary<int,string> ();
405 IEnumerator e = d.Values.GetEnumerator ();
411 class ReverseComparer<T> : IComparer<T>
413 static ReverseComparer<T> instance = new ReverseComparer<T> ();
414 public static ReverseComparer<T> Instance {
415 get { return instance; }
422 public int Compare (T t1, T t2)
424 return Comparer<T>.Default.Compare (t2, t1);