In .:
[mono.git] / mcs / class / corlib / Test / System.Collections.ObjectModel / KeyedCollectionTest.cs
1 // KeyedCollectionTest.cs - NUnit Test Cases for System.Collections.ObjectModel.KeyedCollection
2 //
3 // Carlo Kok (ck@carlo-kok.com)
4 //
5 // (C) Carlo Kok
6 // 
7
8 #if NET_2_0
9 using NUnit.Framework;
10 using System;
11 using System.Globalization;
12 using System.Collections.Generic;
13 using System.Collections.ObjectModel;
14
15 namespace MonoTests.System.Collections.ObjectModel
16 {
17
18         [TestFixture]
19         public class KeyedCollectionTest 
20         {
21                 private class CaseInsensitiveComparer : IEqualityComparer<string>
22                 {
23                         public CaseInsensitiveComparer () { }
24
25                         #region IEqualityComparer<string> Members
26
27                         public bool Equals (string x, string y)
28                         {
29                                 return String.Compare (x, y, true,
30                                         CultureInfo.InvariantCulture) == 0;
31                         }
32
33                         public int GetHashCode (string obj)
34                         {
35                                 return obj.ToUpper (CultureInfo.InvariantCulture).GetHashCode ();
36                         }
37                         #endregion
38                 }
39
40                 private class StrKeyCollection : KeyedCollection<string, string>
41                 {
42                         public StrKeyCollection (
43                                 IEqualityComparer<string> comparer,
44                                 int dictionaryCreationThreshold):
45                                 base (comparer, dictionaryCreationThreshold)
46                         {
47                         }
48
49                         protected override string GetKeyForItem (string item)
50                         {
51                                 return "Key:" + item;
52                         }
53
54                         public IDictionary<string, string> GetDictionary()
55                         {
56                                 return Dictionary;
57                         }
58                 }
59
60                 public KeyedCollectionTest ()
61                 {
62                 }
63
64                 [Test]
65                 public void TestDelete ()
66                 {
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"));
75
76                         Assert.IsFalse (collection.Remove ("Unknown"));
77
78                         Assert.AreEqual (collection.GetDictionary ().Count, 2);
79
80
81                         Assert.AreEqual (collection.Count, 2, "Collection count not equal to 2");
82                         // check if all items are ordered correctly
83
84                         Assert.AreEqual (collection [0], "Two");
85                         Assert.AreEqual (collection [1], "Four");
86
87                         Assert.AreEqual (collection ["Key:Two"], "Two");
88                         Assert.AreEqual (collection ["Key:Four"], "Four");
89
90                         try {
91                                 collection ["Key:One"].ToString();
92                                 Assert.Fail ("Unknown key should fail");
93                         } catch (KeyNotFoundException e) {
94                                 e.ToString(); // avoid warning
95                                 // oke
96                         }
97
98                         try {
99                                 collection ["Key:One"].ToString();
100                                 Assert.Fail ("Unknown key should fail");
101                         } catch (KeyNotFoundException e) {
102                                 e.ToString (); // avoid warning
103                                 // oke
104                         }
105                 }
106
107                 [Test]
108                 public void TestInsert ()
109                 {
110                         StrKeyCollection collection =
111                                 new StrKeyCollection(EqualityComparer<string>.Default, 2);
112
113                         Assert.IsNull (collection.GetDictionary (), 
114                                 "Dictionary created too early"); // There can't be a dictionary yet
115
116                         collection.Add ("One"); // Key:First
117
118                         Assert.IsNull (collection.GetDictionary(),
119                                 "Dictionary created too early"); // There can't be a dictionary yet
120
121                         collection.Add ("Two"); // Key:Two
122
123                         Assert.IsNull (collection.GetDictionary (),
124                                 "Dictionary created too early"); // There can't be a dictionary yet
125
126                         collection.Add ("Four"); // Key:Four
127
128                         Assert.IsNotNull(collection.GetDictionary (),
129                                 "Dictionary created too late"); // There must be a dictionary 
130
131                         collection.Insert (2, "Three"); // Key:Three
132
133                         Assert.AreEqual (collection.Count, 4,
134                                 "Collection count not equal to 4");
135                         // check if all items are ordered correctly
136
137                         Assert.AreEqual (collection [0], "One");
138                         Assert.AreEqual (collection [1], "Two");
139                         Assert.AreEqual (collection [2], "Three");
140                         Assert.AreEqual (collection [3], "Four");
141
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");
146
147                         Assert.AreEqual (collection.GetDictionary ().Count, 4);
148
149                         try {
150                                 collection ["UnkownKey"].ToString();
151                                 Assert.Fail ("Unknown key should fail");
152                         } catch(KeyNotFoundException e) {
153                                 e.ToString(); // avoid warning
154                                 // oke
155                         }
156                 }
157         }
158 }
159 #endif