New test.
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / NameObjectCollectionBaseTest.cs
1 //
2 // ListDictionaryCas.cs - CAS unit tests for 
3 //      System.Collections.Specialized.ListDictionary
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 using NUnit.Framework;
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Formatters.Binary;
37
38 namespace MonoTests.System.Collections.Specialized {
39
40         public class UnitTestNameObjectCollectionBase: NameObjectCollectionBase {
41
42                 public UnitTestNameObjectCollectionBase ()
43                 {
44                 }
45
46                 public UnitTestNameObjectCollectionBase (int capacity)
47                         : base (capacity)
48                 {
49                 }
50
51                 public UnitTestNameObjectCollectionBase (IHashCodeProvider provider, IComparer comparer)
52                         : base (provider, comparer)
53                 {
54                 }
55
56                 public UnitTestNameObjectCollectionBase (int capacity, IHashCodeProvider provider, IComparer comparer)
57                         : base (capacity, provider, comparer)
58                 {
59                 }
60
61 #if NET_2_0
62                 public UnitTestNameObjectCollectionBase (IEqualityComparer comparer)
63                         : base (comparer)
64                 {
65                 }
66
67                 public UnitTestNameObjectCollectionBase (int capacity, IEqualityComparer comparer)
68                         : base (capacity, comparer)
69                 {
70                 }
71 #endif
72                 public bool _IsReadOnly {
73                         get { return base.IsReadOnly; }
74                         set { base.IsReadOnly = value; }
75                 }
76
77                 public void Add (string name, object value)
78                 {
79                         base.BaseAdd (name, value);
80                 }
81
82                 public void Clear ()
83                 {
84                         base.BaseClear ();
85                 }
86
87                 public object Get (int index)
88                 {
89                         return base.BaseGet (index);
90                 }
91
92                 public string[] GetAllKeys ()
93                 {
94                         return base.BaseGetAllKeys ();
95                 }
96
97                 public object[] GetAllValues ()
98                 {
99                         return base.BaseGetAllValues ();
100                 }
101
102                 public object[] GetAllValues (Type type)
103                 {
104                         return base.BaseGetAllValues (type);
105                 }
106
107                 public string GetKey (int index)
108                 {
109                         return base.BaseGetKey (index);
110                 }
111
112                 public bool HasKeys ()
113                 {
114                         return base.BaseHasKeys ();
115                 }
116
117                 public void Remove (string name)
118                 {
119                         base.BaseRemove (name);
120                 }
121
122                 public void RemoveAt (int index)
123                 {
124                         base.BaseRemoveAt (index);
125                 }
126
127                 public void Set (int index, object value)
128                 {
129                         base.BaseSet (index, value);
130                 }
131
132                 public void Set (string name, object value)
133                 {
134                         base.BaseSet (name, value);
135                 }
136         }
137
138 #if NET_2_0
139         public class EqualityComparer: IEqualityComparer {
140
141                 public bool Equals (object x, object y)
142                 {
143                         return (CaseInsensitiveComparer.DefaultInvariant.Compare (x, y) == 0);
144                 }
145
146                 public int GetHashCode (object obj)
147                 {
148                         return obj.GetHashCode ();
149                 }
150         }
151 #endif
152
153         [TestFixture]
154         public class NameObjectCollectionBaseTest {
155
156                 private void CheckICollection (UnitTestNameObjectCollectionBase coll, int count)
157                 {
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 ());
166                         }
167                 }
168
169                 [Test]
170                 public void Constructor_Default ()
171                 {
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");
176
177                         coll.Add ("a", "1");
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");
183
184                         coll.Add ("b", "2");
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");
188
189                         coll.Remove ("a");
190                         Assert.AreEqual (1, coll.Count, "Count-3");
191
192                         coll.Set (0, "3");
193                         Assert.AreEqual ("3", coll.Get (0), "Get(0)b");
194                         coll.Set ("b", "4");
195                         Assert.AreEqual ("4", coll.Get (0), "Get(0)c");
196
197                         coll.RemoveAt (0);
198                         Assert.AreEqual (0, coll.Count, "Count-4");
199                         Assert.IsFalse (coll.HasKeys (), "HasKeys-2");
200                 }
201
202                 [Test]
203                 public void Constructor_Int ()
204                 {
205                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (0);
206                         for (int i = 0; i < 10; i++)
207                                 coll.Add (i.ToString (), i);
208
209                         CheckICollection (coll, 10);
210                         Assert.AreEqual (10, coll.Keys.Count, "Keys");
211
212                         coll.Clear ();
213                         Assert.AreEqual (0, coll.Count, "Count");
214                         Assert.IsFalse (coll.HasKeys (), "HasKeys");
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
219                 public void Constructor_Int_MinValue ()
220                 {
221                         new UnitTestNameObjectCollectionBase (Int32.MinValue);
222                 }
223 #if NET_2_0
224                 [Test]
225                 public void Constructor_IEqualityComparer ()
226                 {
227                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (new EqualityComparer ());
228                         coll.Add ("a", "1");
229                         CheckICollection (coll, 1);
230                 }
231
232                 [Test]
233                 public void Constructor_Int_IEqualityComparer ()
234                 {
235                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, new EqualityComparer ());
236                         coll.Add ("a", "1");
237                         CheckICollection (coll, 1);
238                 }
239
240                 [Test]
241                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
242                 public void Constructor_IntNegative_IEqualityComparer ()
243                 {
244                         new UnitTestNameObjectCollectionBase (-1, new EqualityComparer ());
245                 }
246
247                 [Test]
248                 public void GetObjectData_IEqualityComparer ()
249                 {
250                         EqualityComparer comparer = new EqualityComparer ();
251                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, comparer);
252                         coll.Add ("a", "1");
253                         coll.Add ("b", "2");
254                         coll._IsReadOnly = true;
255
256                         SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
257                         coll.GetObjectData (si, new StreamingContext ());
258                         foreach (SerializationEntry se in si) {
259                                 switch (se.Name) {
260                                 case "KeyComparer":
261                                         Assert.AreSame (comparer, se.Value, se.Name);
262                                         break;
263                                 case "ReadOnly":
264                                         Assert.IsTrue ((bool) se.Value, se.Name);
265                                         break;
266                                 case "Count":
267                                         Assert.AreEqual (2, se.Value, se.Name);
268                                         break;
269                                 case "Values":
270                                         Assert.AreEqual (2, (se.Value as object[]).Length, se.Name);
271                                         break;
272                                 case "Keys":
273                                         Assert.AreEqual (2, (se.Value as string[]).Length, se.Name);
274                                         break;
275                                 case "Version":
276                                         Assert.AreEqual (4, se.Value, se.Name);
277                                         break;
278                                 default:
279                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
280                                                 se.Name, se.ObjectType, se.Value);
281                                         Assert.Fail (msg);
282                                         break;
283                                 }
284                         }
285                 }
286 #endif
287                 [Test]
288                 public void Constructor_Provider_Comparer ()
289                 {
290                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
291                         coll.Add (null, null);
292                         Assert.AreEqual (1, coll.Count, "Count-1");
293                         coll.Remove (null);
294                         Assert.AreEqual (0, coll.Count, "Count-0");
295                 }
296
297                 [Test]
298                 public void Constructor_Int_Provider_Comparer ()
299                 {
300                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
301                         coll.Add ("a", "1");
302                         int i = 0;
303                         IEnumerator e = coll.GetEnumerator ();
304                         while (e.MoveNext ()) {
305                                 i++;
306                         }
307                         Assert.AreEqual (1, i, "GetEnumerator");
308                 }
309
310                 [Test]
311                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
312                 public void Constructor_Int_Negative ()
313                 {
314                         new UnitTestNameObjectCollectionBase (-1, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
315                 }
316
317                 [Test]
318                 [ExpectedException (typeof (ArgumentNullException))]
319                 public void GetObjectData_Null ()
320                 {
321                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
322                         coll.GetObjectData (null, new StreamingContext ());
323                 }
324
325                 [Test]
326                 public void GetObjectData ()
327                 {
328                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
329                         coll.Add ("a", "1");
330
331                         SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
332                         coll.GetObjectData (si, new StreamingContext ());
333                         foreach (SerializationEntry se in si) {
334                                 switch (se.Name) {
335                                 case "HashProvider":
336                                         Assert.AreSame (CaseInsensitiveHashCodeProvider.DefaultInvariant, se.Value, se.Name);
337                                         break;
338                                 case "Comparer":
339                                         Assert.AreSame (CaseInsensitiveComparer.DefaultInvariant, se.Value, se.Name);
340                                         break;
341                                 case "ReadOnly":
342                                         Assert.IsFalse ((bool)se.Value, se.Name);
343                                         break;
344                                 case "Count":
345                                         Assert.AreEqual (1, se.Value, se.Name);
346                                         break;
347                                 case "Values":
348                                         Assert.AreEqual (1, (se.Value as object[]).Length, se.Name);
349                                         break;
350                                 case "Keys":
351                                         Assert.AreEqual (1, (se.Value as string[]).Length, se.Name);
352                                         break;
353                                 case "Version":
354                                         Assert.AreEqual (2, se.Value, se.Name);
355                                         break;
356                                 default:
357                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
358                                                 se.Name, se.ObjectType, se.Value);
359                                         Assert.Fail (msg);
360                                         break;
361                                 }
362                         }
363                 }
364
365                 [Test]
366                 [ExpectedException (typeof (NotSupportedException))]
367                 public void Add_ReadOnly ()
368                 {
369                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
370                         coll._IsReadOnly = true;
371                         coll.Add ("a", "1");
372                 }
373
374                 [Test]
375                 [ExpectedException (typeof (NotSupportedException))]
376                 public void Clear_ReadOnly ()
377                 {
378                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
379                         coll._IsReadOnly = true;
380                         // even if we're empty
381                         coll.Clear ();
382                 }
383
384                 [Test]
385                 [ExpectedException (typeof (NotSupportedException))]
386                 public void Remove_ReadOnly ()
387                 {
388                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
389                         coll.Add ("a", "!");
390                         coll._IsReadOnly = true;
391                         coll.Remove ("a");
392                 }
393
394                 [Test]
395                 [ExpectedException (typeof (NotSupportedException))]
396                 public void RemoveAt_ReadOnly ()
397                 {
398                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
399                         coll.Add ("a", "!");
400                         coll._IsReadOnly = true;
401                         coll.RemoveAt (0);
402                 }
403
404                 [Test]
405 #if NET_2_0
406                 [ExpectedException (typeof (NotSupportedException))]
407 #else
408                 [Ignore ("Read-only is ignored for Set() under MS 1.x.")]
409 #endif
410                 public void Set_ReadOnly ()
411                 {
412                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
413                         coll.Add ("a", "!");
414                         coll._IsReadOnly = true;
415                         coll.Set (0, "1");
416                         // no exception before 2.0
417                         Assert.AreEqual ("1", coll.Get (0), "Get(0)"); // :-(
418                 }
419
420                 [Test]
421                 [ExpectedException (typeof (ArrayTypeMismatchException))]
422                 public void GetAllValues_Type_Mismatch ()
423                 {
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]");
430                 }
431
432                 [Test]
433                 public void GetAllValues_Type ()
434                 {
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]");
442                 }
443
444                 [Test]
445                 public void GetAllValues ()
446                 {
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]");
454                 }
455         }
456 }