1 // KeyedCollectionTest.cs - NUnit Test Cases for System.Collections.ObjectModel.KeyedCollection
3 // Carlo Kok (ck@carlo-kok.com)
11 using System.Globalization;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
15 namespace MonoTests.System.Collections.ObjectModel
19 public class KeyedCollectionTest
21 private class CaseInsensitiveComparer : IEqualityComparer<string>
23 public CaseInsensitiveComparer () { }
25 #region IEqualityComparer<string> Members
27 public bool Equals (string x, string y)
29 return String.Compare (x, y, true,
30 CultureInfo.InvariantCulture) == 0;
33 public int GetHashCode (string obj)
35 return obj.ToUpper (CultureInfo.InvariantCulture).GetHashCode ();
40 private class StrKeyCollection : KeyedCollection<string, string>
42 public StrKeyCollection (
43 IEqualityComparer<string> comparer,
44 int dictionaryCreationThreshold):
45 base (comparer, dictionaryCreationThreshold)
49 protected override string GetKeyForItem (string item)
54 public IDictionary<string, string> GetDictionary()
60 public KeyedCollectionTest ()
65 public void TestDelete ()
67 StrKeyCollection collection =
68 new StrKeyCollection (EqualityComparer<string>.Default, 2);
69 collection.Add ("One"); // Key:First
70 collection.Add ("Two"); // Key:Two
71 Assert.IsTrue (collection.Remove ("Key:One"));
72 collection.Add ("Four"); // Key:Four
73 collection.Insert (2, "Three"); // Key:Three
74 Assert.IsTrue (collection.Remove ("Key:Three"));
76 Assert.IsFalse (collection.Remove ("Unknown"));
78 Assert.AreEqual (collection.GetDictionary ().Count, 2);
81 Assert.AreEqual (collection.Count, 2, "Collection count not equal to 2");
82 // check if all items are ordered correctly
84 Assert.AreEqual (collection [0], "Two");
85 Assert.AreEqual (collection [1], "Four");
87 Assert.AreEqual (collection ["Key:Two"], "Two");
88 Assert.AreEqual (collection ["Key:Four"], "Four");
91 collection ["Key:One"].ToString();
92 Assert.Fail ("Unknown key should fail");
93 } catch (KeyNotFoundException e) {
94 e.ToString(); // avoid warning
99 collection ["Key:One"].ToString();
100 Assert.Fail ("Unknown key should fail");
101 } catch (KeyNotFoundException e) {
102 e.ToString (); // avoid warning
108 public void TestInsert ()
110 StrKeyCollection collection =
111 new StrKeyCollection(EqualityComparer<string>.Default, 2);
113 Assert.IsNull (collection.GetDictionary (),
114 "Dictionary created too early"); // There can't be a dictionary yet
116 collection.Add ("One"); // Key:First
118 Assert.IsNull (collection.GetDictionary(),
119 "Dictionary created too early"); // There can't be a dictionary yet
121 collection.Add ("Two"); // Key:Two
123 Assert.IsNull (collection.GetDictionary (),
124 "Dictionary created too early"); // There can't be a dictionary yet
126 collection.Add ("Four"); // Key:Four
128 Assert.IsNotNull(collection.GetDictionary (),
129 "Dictionary created too late"); // There must be a dictionary
131 collection.Insert (2, "Three"); // Key:Three
133 Assert.AreEqual (collection.Count, 4,
134 "Collection count not equal to 4");
135 // check if all items are ordered correctly
137 Assert.AreEqual (collection [0], "One");
138 Assert.AreEqual (collection [1], "Two");
139 Assert.AreEqual (collection [2], "Three");
140 Assert.AreEqual (collection [3], "Four");
142 Assert.AreEqual (collection ["Key:One"], "One");
143 Assert.AreEqual (collection ["Key:Two"], "Two");
144 Assert.AreEqual (collection ["Key:Three"], "Three");
145 Assert.AreEqual (collection ["Key:Four"], "Four");
147 Assert.AreEqual (collection.GetDictionary ().Count, 4);
150 collection ["UnkownKey"].ToString();
151 Assert.Fail ("Unknown key should fail");
152 } catch(KeyNotFoundException e) {
153 e.ToString(); // avoid warning