New test.
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / OrderedDictionaryTest.cs
1 //
2 // OrderedDictionaryTest.cs -
3 //      Unit tests for System.Collections.Specialized.OrderedDictionary
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.Serialization;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Collections.Specialized {
40
41         [TestFixture]
42         public class OrderedDictionaryTest {
43
44                 private void Common (OrderedDictionary od)
45                 {
46                         Assert.IsNotNull (od.GetEnumerator (), "GetEnumerator");
47                         Assert.AreEqual (0, od.Count, "Count-0");
48                         Assert.IsFalse (od.IsReadOnly, "IsReadOnly");
49                         od.Add ("a", "1");
50                         Assert.AreEqual (1, od.Count, "Count-1");
51                         od["a"] = "11";
52                         Assert.AreEqual ("11", od["a"], "this[string]");
53                         od[0] = "111";
54                         Assert.AreEqual ("111", od[0], "this[int]");
55
56                         DictionaryEntry[] array = new DictionaryEntry[2];
57                         od.CopyTo (array, 1);
58
59                         Assert.AreEqual ("111", ((DictionaryEntry)array[1]).Value, "CopyTo");
60                         Assert.AreEqual (1, od.Keys.Count, "Keys");
61                         Assert.AreEqual (1, od.Values.Count, "Values");
62                         Assert.IsTrue (od.Contains ("a"), "Contains(a)");
63                         Assert.IsFalse (od.Contains ("111"), "Contains(111)");
64
65                         od.Insert (0, "b", "2");
66                         Assert.AreEqual (2, od.Count, "Count-2");
67                         od.Add ("c", "3");
68                         Assert.AreEqual (3, od.Count, "Count-3");
69
70                         OrderedDictionary ro = od.AsReadOnly ();
71
72                         od.RemoveAt (2);
73                         Assert.AreEqual (2, od.Count, "Count-4");
74                         Assert.IsFalse (od.Contains ("c"), "Contains(c)");
75
76                         od.Remove ("b");
77                         Assert.AreEqual (1, od.Count, "Count-5");
78                         Assert.IsFalse (od.Contains ("b"), "Contains(b)");
79
80                         od.Clear ();
81                         Assert.AreEqual (0, od.Count, "Count-6");
82
83                         Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
84                         // it's a read-only reference
85                         Assert.AreEqual (0, od.Count, "Count-7");
86                 }
87
88                 [Test]
89                 public void Constructor_Default ()
90                 {
91                         OrderedDictionary od = new OrderedDictionary ();
92                         Common (od);
93                 }
94
95                 [Test]
96                 public void Constructor_Int ()
97                 {
98                         OrderedDictionary od = new OrderedDictionary ();
99                         Common (od);
100                 }
101
102                 [Test]
103                 // [ExpectedException (typeof (ArgumentOutOfRangeException))]
104                 public void Constructor_IntNegative ()
105                 {
106                         new OrderedDictionary (-1);
107                 }
108
109                 [Test]
110                 public void Constructor_IEqualityComparer ()
111                 {
112                         OrderedDictionary od = new OrderedDictionary (new EqualityComparer ());
113                         Common (od);
114                 }
115
116                 [Test]
117                 public void Constructor_Int_IEqualityComparer ()
118                 {
119                         OrderedDictionary od = new OrderedDictionary (5, new EqualityComparer ());
120                         Common (od);
121                 }
122
123                 [Test]
124                 // [ExpectedException (typeof (ArgumentOutOfRangeException))]
125                 public void Constructor_IntNegative_IEqualityComparer ()
126                 {
127                         new OrderedDictionary (-1, new EqualityComparer ());
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (NotSupportedException))]
132                 public void ReadOnly_This_String ()
133                 {
134                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
135                         od["a"] = 1;
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (NotSupportedException))]
140                 public void ReadOnly_This_int ()
141                 {
142                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
143                         od[0] = 1;
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (NotSupportedException))]
148                 public void ReadOnly_Add ()
149                 {
150                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
151                         od.Add ("a", "1");
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (NotSupportedException))]
156                 public void ReadOnly_Clear ()
157                 {
158                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
159                         od.Clear ();
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (NotSupportedException))]
164                 public void ReadOnly_Insert ()
165                 {
166                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
167                         od.Insert (0, "a", "1");
168                 }
169
170                 [Test]
171                 [ExpectedException (typeof (NotSupportedException))]
172                 public void ReadOnly_Remove ()
173                 {
174                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
175                         od.Remove ("a");
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (NotSupportedException))]
180                 public void ReadOnly_RemoveAt ()
181                 {
182                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
183                         od.RemoveAt (0);
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof (ArgumentNullException))]
188                 public void GetObjectData_Null ()
189                 {
190                         OrderedDictionary coll = new OrderedDictionary ();
191                         coll.GetObjectData (null, new StreamingContext ());
192                 }
193
194                 [Test]
195                 public void GetObjectData ()
196                 {
197                         OrderedDictionary coll = new OrderedDictionary (99);
198                         coll.Add ("a", "1");
199
200                         SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
201                         coll.GetObjectData (si, new StreamingContext ());
202                         foreach (SerializationEntry se in si) {
203                                 switch (se.Name) {
204                                 case "KeyComparer":
205                                         Assert.IsNull (se.Value, se.Name);
206                                         break;
207                                 case "ReadOnly":
208                                         Assert.IsFalse ((bool) se.Value, se.Name);
209                                         break;
210                                 case "InitialCapacity":
211                                         Assert.AreEqual (99, se.Value, se.Name);
212                                         break;
213                                 case "ArrayList":
214                                         Assert.AreEqual ("1", ((DictionaryEntry)((object[]) se.Value)[0]).Value, se.Name);
215                                         break;
216                                 default:
217                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
218                                                 se.Name, se.ObjectType, se.Value);
219                                         Assert.Fail (msg);
220                                         break;
221                                 }
222                         }
223                 }
224
225                 [Test]
226                 public void GetObjectData_IEqualityComparer ()
227                 {
228                         EqualityComparer comparer = new EqualityComparer ();
229                         OrderedDictionary coll = new OrderedDictionary (comparer);
230                         coll.Add ("a", "1");
231                         coll.Add ("b", "2");
232                         coll = coll.AsReadOnly ();
233
234                         SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
235                         coll.GetObjectData (si, new StreamingContext ());
236                         foreach (SerializationEntry se in si) {
237                                 switch (se.Name) {
238                                 case "KeyComparer":
239                                         Assert.AreSame (comparer, se.Value, se.Name);
240                                         break;
241                                 case "ReadOnly":
242                                         Assert.IsTrue ((bool) se.Value, se.Name);
243                                         break;
244                                 case "InitialCapacity":
245                                         Assert.AreEqual (0, se.Value, se.Name);
246                                         break;
247                                 case "ArrayList":
248                                         Assert.AreEqual (2, ((object[]) se.Value).Length, se.Name);
249                                         break;
250                                 default:
251                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
252                                                 se.Name, se.ObjectType, se.Value);
253                                         Assert.Fail (msg);
254                                         break;
255                                 }
256                         }
257                 }
258         }
259 }
260
261 #endif