2 // OrderedDictionaryTest.cs -
3 // Unit tests for System.Collections.Specialized.OrderedDictionary
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.Serialization;
37 using NUnit.Framework;
39 namespace MonoTests.System.Collections.Specialized {
42 public class OrderedDictionaryTest {
44 private void Common (OrderedDictionary od)
46 Assert.IsNotNull (od.GetEnumerator (), "GetEnumerator");
47 Assert.AreEqual (0, od.Count, "Count-0");
48 Assert.IsFalse (od.IsReadOnly, "IsReadOnly");
50 Assert.AreEqual (1, od.Count, "Count-1");
52 Assert.AreEqual ("11", od["a"], "this[string]");
54 Assert.AreEqual ("111", od[0], "this[int]");
56 DictionaryEntry[] array = new DictionaryEntry[2];
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)");
65 od.Insert (0, "b", "2");
66 Assert.AreEqual (2, od.Count, "Count-2");
68 Assert.AreEqual (3, od.Count, "Count-3");
70 OrderedDictionary ro = od.AsReadOnly ();
73 Assert.AreEqual (2, od.Count, "Count-4");
74 Assert.IsFalse (od.Contains ("c"), "Contains(c)");
77 Assert.AreEqual (1, od.Count, "Count-5");
78 Assert.IsFalse (od.Contains ("b"), "Contains(b)");
81 Assert.AreEqual (0, od.Count, "Count-6");
83 Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
84 // it's a read-only reference
85 Assert.AreEqual (0, od.Count, "Count-7");
89 public void Constructor_Default ()
91 OrderedDictionary od = new OrderedDictionary ();
96 public void Constructor_Int ()
98 OrderedDictionary od = new OrderedDictionary ();
103 // [ExpectedException (typeof (ArgumentOutOfRangeException))]
104 public void Constructor_IntNegative ()
106 new OrderedDictionary (-1);
110 public void Constructor_IEqualityComparer ()
112 OrderedDictionary od = new OrderedDictionary (new EqualityComparer ());
117 public void Constructor_Int_IEqualityComparer ()
119 OrderedDictionary od = new OrderedDictionary (5, new EqualityComparer ());
124 public void Constructor_NoCase_IEqualityComparer ()
126 OrderedDictionary od = new OrderedDictionary (StringComparer.InvariantCultureIgnoreCase);
127 od ["Original_PhotoID"] = null;
128 od ["original_PhotoID"] = 12;
129 Assert.AreEqual (1, od.Count);
133 // [ExpectedException (typeof (ArgumentOutOfRangeException))]
134 public void Constructor_IntNegative_IEqualityComparer ()
136 new OrderedDictionary (-1, new EqualityComparer ());
140 [ExpectedException (typeof (NotSupportedException))]
141 public void ReadOnly_This_String ()
143 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
148 [ExpectedException (typeof (NotSupportedException))]
149 public void ReadOnly_This_int ()
151 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
156 [ExpectedException (typeof (NotSupportedException))]
157 public void ReadOnly_Add ()
159 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
164 [ExpectedException (typeof (NotSupportedException))]
165 public void ReadOnly_Clear ()
167 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
172 [ExpectedException (typeof (NotSupportedException))]
173 public void ReadOnly_Insert ()
175 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
176 od.Insert (0, "a", "1");
180 [ExpectedException (typeof (NotSupportedException))]
181 public void ReadOnly_Remove ()
183 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
188 [ExpectedException (typeof (NotSupportedException))]
189 public void ReadOnly_RemoveAt ()
191 OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
196 [ExpectedException (typeof (ArgumentNullException))]
197 public void GetObjectData_Null ()
199 OrderedDictionary coll = new OrderedDictionary ();
200 coll.GetObjectData (null, new StreamingContext ());
204 public void GetObjectData ()
206 OrderedDictionary coll = new OrderedDictionary (99);
209 SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
210 coll.GetObjectData (si, new StreamingContext ());
211 foreach (SerializationEntry se in si) {
214 Assert.IsNull (se.Value, se.Name);
217 Assert.IsFalse ((bool) se.Value, se.Name);
219 case "InitialCapacity":
220 Assert.AreEqual (99, se.Value, se.Name);
223 Assert.AreEqual ("1", ((DictionaryEntry)((object[]) se.Value)[0]).Value, se.Name);
226 string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
227 se.Name, se.ObjectType, se.Value);
235 public void GetObjectData_IEqualityComparer ()
237 EqualityComparer comparer = new EqualityComparer ();
238 OrderedDictionary coll = new OrderedDictionary (comparer);
241 coll = coll.AsReadOnly ();
243 SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ());
244 coll.GetObjectData (si, new StreamingContext ());
245 foreach (SerializationEntry se in si) {
248 Assert.AreSame (comparer, se.Value, se.Name);
251 Assert.IsTrue ((bool) se.Value, se.Name);
253 case "InitialCapacity":
254 Assert.AreEqual (0, se.Value, se.Name);
257 Assert.AreEqual (2, ((object[]) se.Value).Length, se.Name);
260 string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
261 se.Name, se.ObjectType, se.Value);