2 // System.Collections.SortedListTest.cs
5 // Zoltan Varga (vargaz@gmail.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Generic;
38 using System.Runtime.Serialization;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using NUnit.Framework;
42 namespace MonoTests.System.Collections.Generic
45 public class SortedListTest
47 SortedList<int, string> list;
48 SortedList<string, int> list2;
51 public void SetUp () {
52 list = new SortedList <int, string> ();
58 list2 = new SortedList<string, int> ();
63 Assert.AreEqual ("A", list [0]);
64 Assert.AreEqual ("B", list [2]);
65 Assert.AreEqual ("C", list [5]);
69 Assert.AreEqual ("D", list [2]);
73 [ExpectedException (typeof (ArgumentNullException))]
74 public void ItemNullKey () {
79 [ExpectedException (typeof (KeyNotFoundException))]
80 public void ItemMissingKey () {
86 IList<int> keys = list.Keys;
88 Assert.AreEqual (3, keys.Count);
89 Assert.AreEqual (0, keys [0]);
90 Assert.AreEqual (2, keys [1]);
91 Assert.AreEqual (5, keys [2]);
93 int[] arr = new int [4];
95 Assert.AreEqual (0, arr [1]);
96 Assert.AreEqual (2, arr [2]);
97 Assert.AreEqual (5, arr [3]);
99 Assert.AreEqual (true, keys.Contains (2));
100 Assert.AreEqual (false, keys.Contains (100));
102 Assert.AreEqual (2, keys.IndexOf (5));
103 Assert.AreEqual (-1, keys.IndexOf (100));
109 foreach (int i in keys)
111 Assert.AreEqual (0, arr [0]);
112 Assert.AreEqual (2, arr [1]);
113 Assert.AreEqual (5, arr [2]);
117 public void KeysNonGeneric () {
118 ICollection keys = ((IDictionary)list).Keys;
120 Assert.AreEqual (3, keys.Count);
122 int[] arr = new int [4];
123 keys.CopyTo (arr, 1);
124 Assert.AreEqual (0, arr [1]);
125 Assert.AreEqual (2, arr [2]);
126 Assert.AreEqual (5, arr [3]);
132 foreach (int i in keys)
134 Assert.AreEqual (0, arr [0]);
135 Assert.AreEqual (2, arr [1]);
136 Assert.AreEqual (5, arr [2]);
140 public void Values () {
141 IList<string> values = list.Values;
143 Assert.AreEqual (3, values.Count);
144 Assert.AreEqual ("A", values [0]);
145 Assert.AreEqual ("B", values [1]);
146 Assert.AreEqual ("C", values [2]);
148 string[] arr = new string [4];
149 values.CopyTo (arr, 1);
150 Assert.AreEqual ("A", arr [1]);
151 Assert.AreEqual ("B", arr [2]);
152 Assert.AreEqual ("C", arr [3]);
154 Assert.AreEqual (true, values.Contains ("B"));
155 Assert.AreEqual (false, values.Contains ("X"));
157 Assert.AreEqual (2, values.IndexOf ("C"));
158 Assert.AreEqual (-1, values.IndexOf ("X"));
164 foreach (string s in values)
166 Assert.AreEqual ("A", arr [0]);
167 Assert.AreEqual ("B", arr [1]);
168 Assert.AreEqual ("C", arr [2]);
172 public void ValuesNonGeneric () {
173 ICollection values = ((IDictionary)list).Values;
175 Assert.AreEqual (3, values.Count);
177 string[] arr = new string [4];
178 values.CopyTo (arr, 1);
179 Assert.AreEqual ("A", arr [1]);
180 Assert.AreEqual ("B", arr [2]);
181 Assert.AreEqual ("C", arr [3]);
187 foreach (string s in values)
189 Assert.AreEqual ("A", arr [0]);
190 Assert.AreEqual ("B", arr [1]);
191 Assert.AreEqual ("C", arr [2]);
195 public void KeysIDictionaryGeneric () {
196 ICollection<int> keys = ((IDictionary<int,string>)list).Keys;
198 Assert.AreEqual (3, keys.Count);
202 public void EmptyKeysCopyToZeroSizedArray ()
204 string [] ary = new string [0];
205 list2.Keys.CopyTo (ary, 0);
209 public void EmptyValuesCopyToZeroSizedArray ()
211 int [] ary = new int [0];
212 list2.Values.CopyTo (ary, 0);
216 public void ValuesIDictionaryGeneric () {
217 ICollection<string> values = ((IDictionary<int,string>)list).Values;
219 Assert.AreEqual (3, values.Count);
225 Assert.AreEqual ("D", list [10]);
229 [ExpectedException (typeof (ArgumentNullException))]
230 public void AddNullKey () {
231 list2.Add (null, 10);
235 [ExpectedException (typeof (ArgumentException))]
236 public void AddKeyAlreadyExists () {
242 public void ContainsKey () {
243 Assert.AreEqual (true, list.ContainsKey (5));
244 Assert.AreEqual (false, list.ContainsKey (10));
248 public void Remove () {
249 Assert.AreEqual (true, list.Remove (5));
250 Assert.AreEqual (false, list.Remove (5));
251 Assert.AreEqual (false, list.Remove (10));
255 [ExpectedException (typeof (ArgumentNullException))]
256 public void RemoveNullKey () {
261 public void GetEnumerator () {
262 int[] keys = new int [3];
263 string[] values = new string [3];
265 foreach (KeyValuePair <int, string> kvp in list) {
267 values [i] = kvp.Value;
271 Assert.AreEqual (0, keys [0]);
272 Assert.AreEqual (2, keys [1]);
273 Assert.AreEqual (5, keys [2]);
274 Assert.AreEqual ("A", values [0]);
275 Assert.AreEqual ("B", values [1]);
276 Assert.AreEqual ("C", values [2]);
280 public void CopyTo ()
282 ICollection<KeyValuePair<int, string>> col1 =
283 list as ICollection<KeyValuePair<int, string>>;
284 KeyValuePair <int, string> [] array1 =
285 new KeyValuePair <int, string> [col1.Count];
286 col1.CopyTo (array1, 0);
287 Assert.AreEqual (3, array1.Length);
289 ICollection col = list as ICollection;
290 array1 = new KeyValuePair <int, string> [col.Count];
291 col.CopyTo (array1, 0);
292 Assert.AreEqual (3, array1.Length);
294 ICollection<KeyValuePair<string, int>> col2 =
295 list2 as ICollection<KeyValuePair<string, int>>;
296 KeyValuePair <string, int> [] array2 =
297 new KeyValuePair <string, int> [col2.Count];
298 col2.CopyTo (array2, 0);
299 Assert.AreEqual (0, array2.Length);
301 col = list2 as ICollection;
302 array2 = new KeyValuePair <string, int> [col.Count];
303 col.CopyTo (array2, 0);
304 Assert.AreEqual (0, array2.Length);
308 [ExpectedException (typeof (InvalidOperationException))]
309 public void KeyEnumeratorVersionChecking () {
310 var en = list.Keys.GetEnumerator();
314 list.Remove (en.Current);
319 [ExpectedException (typeof (InvalidOperationException))]
320 public void ValueEnumeratorVersionChecking () {
321 var en = list.Values.GetEnumerator();
329 sealed class StartsWithComparator : IComparer<string> {
330 private readonly static Comparer<string> _stringComparer = Comparer<string>.Default;
331 public static readonly StartsWithComparator Instance = new StartsWithComparator();
333 public int Compare(string part, string whole)
335 // let the default string comparer deal with null or when part is not smaller then whole
336 if (part == null || whole == null || part.Length >= whole.Length)
337 return _stringComparer.Compare(part, whole);
339 // loop through all characters that part and whole have in common
343 match = (part[pos] == whole[pos]);
344 } while (match && ++pos < part.Length);
346 // return result of last comparison
347 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
351 sealed class StartsWithComparatorPartWholeCheck : IComparer<string>
353 private readonly static Comparer<string> _stringComparer = Comparer<string>.Default;
355 public static readonly StartsWithComparator Instance = new StartsWithComparator();
357 public int Compare(string part, string whole)
359 Assert.IsTrue(part == "Part", "#PWC0");
360 Assert.IsTrue(whole == "Whole", "#PWC1");
362 // let the default string comparer deal with null or when part is not smaller then whole
363 if (part == null || whole == null || part.Length >= whole.Length)
364 return _stringComparer.Compare(part, whole);
366 // loop through all characters that part and whole have in common
370 match = (part[pos] == whole[pos]);
371 } while (match && ++pos < part.Length);
373 // return result of last comparison
374 return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
379 public void ComparatorUsageTest()
381 SortedList<string, string> sl = new SortedList<string, string>(StartsWithComparator.Instance);
383 sl.Add("Apples", "Value-Apples");
384 sl.Add("Bananas", "Value-Bananas");
385 sl.Add("Oranges", "Value-Oranges");
387 // Ensure 3 objects exist in the collection
388 Assert.IsTrue(sl.Count == 3, "Count");
390 // Complete Match Test Set
391 Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
392 Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
393 Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
395 // Partial Match Test Set
396 Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
397 Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
398 Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
400 // Reversed Match Test Set
401 Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
404 Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
405 Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
406 Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
411 public void ComparatorPartWholeCheck()
413 SortedList<string, string> sl = new SortedList<string, string>(StartsWithComparatorPartWholeCheck.Instance);
414 sl.Add("Part", "Value-Part");
415 Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
419 public void NonComparatorStringCheck()
421 SortedList<string, string> sl = new SortedList<string, string>();
423 sl.Add("Oranges", "Value-Oranges");
424 sl.Add("Apples", "Value-Apples");
425 sl.Add("Bananas", "Value-Bananas");
428 Assert.IsTrue(sl.Count == 3, "NCSC #A0");
430 Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
431 Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
432 Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
434 Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
435 Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
436 Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
438 Assert.IsTrue(sl.Keys[0] == "Apples", "NCSC #D1");
439 Assert.IsTrue(sl.Keys[1] == "Bananas", "NCSC #D2");
440 Assert.IsTrue(sl.Keys[2] == "Oranges", "NCSC #D3");
444 public void NonComparatorIntCheck()
446 SortedList<int, string> sl = new SortedList<int, string>();
448 sl.Add(3, "Value-Oranges");
449 sl.Add(2, "Value-Bananas");
450 sl.Add(1, "Value-Apples");
452 Assert.IsTrue(sl.Count == 3, "NCIC #A0");
454 Assert.IsTrue(sl.ContainsKey(1), "NCIC #B1");
455 Assert.IsTrue(sl.ContainsKey(2), "NCIC #B2");
456 Assert.IsTrue(sl.ContainsKey(3), "NCIC #B3");
458 Assert.IsFalse(sl.ContainsKey(11), "NCIC #C1");
459 Assert.IsFalse(sl.ContainsKey(22), "NCIC #C2");
460 Assert.IsFalse(sl.ContainsKey(33), "NCIC #C3");
462 Assert.IsTrue(sl.Keys[0] == 1, "NCIC #D1");
463 Assert.IsTrue(sl.Keys[1] == 2, "NCIC #D2");
464 Assert.IsTrue(sl.Keys[2] == 3, "NCIC #D3");
468 public void ClearDoesNotTouchCapacity ()
470 SortedList<int, int> sl = new SortedList<int, int> ();
471 for (int i = 0; i < 18; i++) {
474 int capacityBeforeClear = sl.Capacity;
476 int capacityAfterClear = sl.Capacity;
477 Assert.AreEqual (capacityBeforeClear, capacityAfterClear);