2 // ListDictionaryCas.cs - CAS unit tests for
3 // System.Collections.Specialized.ListDictionary
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.
30 using NUnit.Framework;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Formatters.Binary;
38 namespace MonoTests.System.Collections.Specialized {
40 public class UnitTestNameObjectCollectionBase: NameObjectCollectionBase {
42 public UnitTestNameObjectCollectionBase ()
46 public UnitTestNameObjectCollectionBase (int capacity)
51 public UnitTestNameObjectCollectionBase (IHashCodeProvider provider, IComparer comparer)
52 : base (provider, comparer)
56 public UnitTestNameObjectCollectionBase (int capacity, IHashCodeProvider provider, IComparer comparer)
57 : base (capacity, provider, comparer)
62 public UnitTestNameObjectCollectionBase (IEqualityComparer comparer)
67 public UnitTestNameObjectCollectionBase (int capacity, IEqualityComparer comparer)
68 : base (capacity, comparer)
72 public bool _IsReadOnly {
73 get { return base.IsReadOnly; }
74 set { base.IsReadOnly = value; }
77 public void Add (string name, object value)
79 base.BaseAdd (name, value);
87 public object Get (int index)
89 return base.BaseGet (index);
92 public string[] GetAllKeys ()
94 return base.BaseGetAllKeys ();
97 public object[] GetAllValues ()
99 return base.BaseGetAllValues ();
102 public object[] GetAllValues (Type type)
104 return base.BaseGetAllValues (type);
107 public string GetKey (int index)
109 return base.BaseGetKey (index);
112 public bool HasKeys ()
114 return base.BaseHasKeys ();
117 public void Remove (string name)
119 base.BaseRemove (name);
122 public void RemoveAt (int index)
124 base.BaseRemoveAt (index);
127 public void Set (int index, object value)
129 base.BaseSet (index, value);
132 public void Set (string name, object value)
134 base.BaseSet (name, value);
139 public class EqualityComparer: IEqualityComparer {
141 public bool Equals (object x, object y)
143 return (CaseInsensitiveComparer.DefaultInvariant.Compare (x, y) == 0);
146 public int GetHashCode (object obj)
148 return obj.GetHashCode ();
154 public class NameObjectCollectionBaseTest {
156 private void CheckICollection (UnitTestNameObjectCollectionBase coll, int count)
158 ICollection collection = (coll as ICollection);
159 Assert.AreEqual (count, collection.Count, "Count");
160 Assert.IsFalse (collection.IsSynchronized, "IsSynchronized");
161 Assert.IsNotNull (collection.SyncRoot, "SyncRoot");
162 string[] array = new string[count];
163 collection.CopyTo (array, 0);
164 for (int i = 0; i < count; i++) {
165 Assert.AreEqual (coll.GetKey (i), array[i], "#" + i.ToString ());
170 public void Constructor_Default ()
172 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
173 Assert.AreEqual (0, coll.Count, "Count-0");
174 Assert.IsFalse (coll._IsReadOnly, "IsReadOnly");
175 Assert.IsFalse (coll.HasKeys (), "HasKeys-0");
178 CheckICollection (coll, 1);
179 Assert.AreEqual (1, coll.Count, "Count-1");
180 Assert.AreEqual ("1", coll.Get (0), "Get(0)");
181 Assert.AreEqual (1, coll.GetAllKeys ().Length, "GetAllKeys");
182 Assert.IsTrue (coll.HasKeys (), "HasKeys-1");
185 Assert.AreEqual (2, coll.Count, "Count-2");
186 Assert.AreEqual ("b", coll.GetKey (1), "GetKey(1)");
187 Assert.AreEqual (2, coll.GetAllValues ().Length, "GetAllValues");
190 Assert.AreEqual (1, coll.Count, "Count-3");
193 Assert.AreEqual ("3", coll.Get (0), "Get(0)b");
195 Assert.AreEqual ("4", coll.Get (0), "Get(0)c");
198 Assert.AreEqual (0, coll.Count, "Count-4");
199 Assert.IsFalse (coll.HasKeys (), "HasKeys-2");
203 public void Constructor_Int ()
205 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (0);
206 for (int i = 0; i < 10; i++)
207 coll.Add (i.ToString (), i);
209 CheckICollection (coll, 10);
210 Assert.AreEqual (10, coll.Keys.Count, "Keys");
213 Assert.AreEqual (0, coll.Count, "Count");
214 Assert.IsFalse (coll.HasKeys (), "HasKeys");
218 [ExpectedException (typeof (ArgumentOutOfRangeException))]
219 public void Constructor_Int_MinValue ()
221 new UnitTestNameObjectCollectionBase (Int32.MinValue);
225 public void Constructor_IEqualityComparer ()
227 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (new EqualityComparer ());
229 CheckICollection (coll, 1);
233 public void Constructor_Int_IEqualityComparer ()
235 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, new EqualityComparer ());
237 CheckICollection (coll, 1);
241 [ExpectedException (typeof (ArgumentOutOfRangeException))]
242 public void Constructor_IntNegative_IEqualityComparer ()
244 new UnitTestNameObjectCollectionBase (-1, new EqualityComparer ());
248 public void GetObjectData_IEqualityComparer ()
250 EqualityComparer comparer = new EqualityComparer ();
251 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, comparer);
254 coll._IsReadOnly = true;
256 SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
257 coll.GetObjectData (si, new StreamingContext ());
258 foreach (SerializationEntry se in si) {
261 Assert.AreSame (comparer, se.Value, se.Name);
264 Assert.IsTrue ((bool) se.Value, se.Name);
267 Assert.AreEqual (2, se.Value, se.Name);
270 Assert.AreEqual (2, (se.Value as object[]).Length, se.Name);
273 Assert.AreEqual (2, (se.Value as string[]).Length, se.Name);
276 Assert.AreEqual (4, se.Value, se.Name);
279 string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
280 se.Name, se.ObjectType, se.Value);
288 public void Constructor_Provider_Comparer ()
290 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
291 coll.Add (null, null);
292 Assert.AreEqual (1, coll.Count, "Count-1");
294 Assert.AreEqual (0, coll.Count, "Count-0");
298 public void Constructor_Int_Provider_Comparer ()
300 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
303 IEnumerator e = coll.GetEnumerator ();
304 while (e.MoveNext ()) {
307 Assert.AreEqual (1, i, "GetEnumerator");
311 [ExpectedException (typeof (ArgumentOutOfRangeException))]
312 public void Constructor_Int_Negative ()
314 new UnitTestNameObjectCollectionBase (-1, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
318 [ExpectedException (typeof (ArgumentNullException))]
319 public void GetObjectData_Null ()
321 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
322 coll.GetObjectData (null, new StreamingContext ());
326 public void GetObjectData ()
328 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
331 SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
332 coll.GetObjectData (si, new StreamingContext ());
333 foreach (SerializationEntry se in si) {
336 Assert.AreSame (CaseInsensitiveHashCodeProvider.DefaultInvariant, se.Value, se.Name);
339 Assert.AreSame (CaseInsensitiveComparer.DefaultInvariant, se.Value, se.Name);
342 Assert.IsFalse ((bool)se.Value, se.Name);
345 Assert.AreEqual (1, se.Value, se.Name);
348 Assert.AreEqual (1, (se.Value as object[]).Length, se.Name);
351 Assert.AreEqual (1, (se.Value as string[]).Length, se.Name);
354 Assert.AreEqual (2, se.Value, se.Name);
357 string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
358 se.Name, se.ObjectType, se.Value);
366 [ExpectedException (typeof (NotSupportedException))]
367 public void Add_ReadOnly ()
369 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
370 coll._IsReadOnly = true;
375 [ExpectedException (typeof (NotSupportedException))]
376 public void Clear_ReadOnly ()
378 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
379 coll._IsReadOnly = true;
380 // even if we're empty
385 [ExpectedException (typeof (NotSupportedException))]
386 public void Remove_ReadOnly ()
388 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
390 coll._IsReadOnly = true;
395 [ExpectedException (typeof (NotSupportedException))]
396 public void RemoveAt_ReadOnly ()
398 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
400 coll._IsReadOnly = true;
406 [ExpectedException (typeof (NotSupportedException))]
408 [Ignore ("Read-only is ignored for Set() under MS 1.x.")]
410 public void Set_ReadOnly ()
412 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
414 coll._IsReadOnly = true;
416 // no exception before 2.0
417 Assert.AreEqual ("1", coll.Get (0), "Get(0)"); // :-(
421 [ExpectedException (typeof (ArrayTypeMismatchException))]
422 public void GetAllValues_Type_Mismatch ()
424 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
425 coll.Add ("a", "string");
426 coll.Add ("b", Int32.MinValue);
427 string[] array = (string[]) coll.GetAllValues (typeof (string));
428 Assert.AreEqual (1, array.Length, "Length");
429 Assert.AreEqual ("string", array[0], "[0]");
433 public void GetAllValues_Type ()
435 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
436 coll.Add ("a", "string1");
437 coll.Add ("b", "string2");
438 string[] array = (string[]) coll.GetAllValues (typeof (string));
439 Assert.AreEqual (2, array.Length, "Length");
440 Assert.AreEqual ("string1", array[0], "[0]");
441 Assert.AreEqual ("string2", array[1], "[1]");
445 public void GetAllValues ()
447 UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
448 coll.Add ("a", "string1");
449 coll.Add ("b", "string2");
450 object[] array = (object[]) coll.GetAllValues ();
451 Assert.AreEqual (2, array.Length, "Length");
452 Assert.AreEqual ("string1", array[0], "[0]");
453 Assert.AreEqual ("string2", array[1], "[1]");