Merge pull request #274 from iainlane/master
[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                 public void Constructor_NoCase_IEqualityComparer ()
125                 {
126                         OrderedDictionary od = new OrderedDictionary (StringComparer.InvariantCultureIgnoreCase);
127                         od ["Original_PhotoID"] = null;
128                         od ["original_PhotoID"] = 12;
129                         Assert.AreEqual (1, od.Count);
130                 }
131
132                 [Test]
133                 // [ExpectedException (typeof (ArgumentOutOfRangeException))]
134                 public void Constructor_IntNegative_IEqualityComparer ()
135                 {
136                         new OrderedDictionary (-1, new EqualityComparer ());
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (NotSupportedException))]
141                 public void ReadOnly_This_String ()
142                 {
143                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
144                         od["a"] = 1;
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (NotSupportedException))]
149                 public void ReadOnly_This_int ()
150                 {
151                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
152                         od[0] = 1;
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (NotSupportedException))]
157                 public void ReadOnly_Add ()
158                 {
159                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
160                         od.Add ("a", "1");
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (NotSupportedException))]
165                 public void ReadOnly_Clear ()
166                 {
167                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
168                         od.Clear ();
169                 }
170
171                 [Test]
172                 [ExpectedException (typeof (NotSupportedException))]
173                 public void ReadOnly_Insert ()
174                 {
175                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
176                         od.Insert (0, "a", "1");
177                 }
178
179                 [Test]
180                 [ExpectedException (typeof (NotSupportedException))]
181                 public void ReadOnly_Remove ()
182                 {
183                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
184                         od.Remove ("a");
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (NotSupportedException))]
189                 public void ReadOnly_RemoveAt ()
190                 {
191                         OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
192                         od.RemoveAt (0);
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (ArgumentNullException))]
197                 public void GetObjectData_Null ()
198                 {
199                         OrderedDictionary coll = new OrderedDictionary ();
200                         coll.GetObjectData (null, new StreamingContext ());
201                 }
202
203                 [Test]
204                 public void GetObjectData ()
205                 {
206                         OrderedDictionary coll = new OrderedDictionary (99);
207                         coll.Add ("a", "1");
208
209                         SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
210                         coll.GetObjectData (si, new StreamingContext ());
211                         foreach (SerializationEntry se in si) {
212                                 switch (se.Name) {
213                                 case "KeyComparer":
214                                         Assert.IsNull (se.Value, se.Name);
215                                         break;
216                                 case "ReadOnly":
217                                         Assert.IsFalse ((bool) se.Value, se.Name);
218                                         break;
219                                 case "InitialCapacity":
220                                         Assert.AreEqual (99, se.Value, se.Name);
221                                         break;
222                                 case "ArrayList":
223                                         Assert.AreEqual ("1", ((DictionaryEntry)((object[]) se.Value)[0]).Value, se.Name);
224                                         break;
225                                 default:
226                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
227                                                 se.Name, se.ObjectType, se.Value);
228                                         Assert.Fail (msg);
229                                         break;
230                                 }
231                         }
232                 }
233
234                 [Test]
235                 public void GetObjectData_IEqualityComparer ()
236                 {
237                         EqualityComparer comparer = new EqualityComparer ();
238                         OrderedDictionary coll = new OrderedDictionary (comparer);
239                         coll.Add ("a", "1");
240                         coll.Add ("b", "2");
241                         coll = coll.AsReadOnly ();
242
243                         SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
244                         coll.GetObjectData (si, new StreamingContext ());
245                         foreach (SerializationEntry se in si) {
246                                 switch (se.Name) {
247                                 case "KeyComparer":
248                                         Assert.AreSame (comparer, se.Value, se.Name);
249                                         break;
250                                 case "ReadOnly":
251                                         Assert.IsTrue ((bool) se.Value, se.Name);
252                                         break;
253                                 case "InitialCapacity":
254                                         Assert.AreEqual (0, se.Value, se.Name);
255                                         break;
256                                 case "ArrayList":
257                                         Assert.AreEqual (2, ((object[]) se.Value).Length, se.Name);
258                                         break;
259                                 default:
260                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
261                                                 se.Name, se.ObjectType, se.Value);
262                                         Assert.Fail (msg);
263                                         break;
264                                 }
265                         }
266                 }
267         }
268 }
269
270 #endif