[bcl] Remove more NET_2_0 checks from class libs
[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                 public UnitTestNameObjectCollectionBase (IEqualityComparer comparer)
62                         : base (comparer)
63                 {
64                 }
65
66                 public UnitTestNameObjectCollectionBase (int capacity, IEqualityComparer comparer)
67                         : base (capacity, comparer)
68                 {
69                 }
70                 public bool _IsReadOnly {
71                         get { return base.IsReadOnly; }
72                         set { base.IsReadOnly = value; }
73                 }
74
75                 public void Add (string name, object value)
76                 {
77                         base.BaseAdd (name, value);
78                 }
79
80                 public void Clear ()
81                 {
82                         base.BaseClear ();
83                 }
84
85                 public object Get (int index)
86                 {
87                         return base.BaseGet (index);
88                 }
89
90                 public string[] GetAllKeys ()
91                 {
92                         return base.BaseGetAllKeys ();
93                 }
94
95                 public object[] GetAllValues ()
96                 {
97                         return base.BaseGetAllValues ();
98                 }
99
100                 public object[] GetAllValues (Type type)
101                 {
102                         return base.BaseGetAllValues (type);
103                 }
104
105                 public string GetKey (int index)
106                 {
107                         return base.BaseGetKey (index);
108                 }
109
110                 public bool HasKeys ()
111                 {
112                         return base.BaseHasKeys ();
113                 }
114
115                 public void Remove (string name)
116                 {
117                         base.BaseRemove (name);
118                 }
119
120                 public void RemoveAt (int index)
121                 {
122                         base.BaseRemoveAt (index);
123                 }
124
125                 public void Set (int index, object value)
126                 {
127                         base.BaseSet (index, value);
128                 }
129
130                 public void Set (string name, object value)
131                 {
132                         base.BaseSet (name, value);
133                 }
134         }
135
136         public class EqualityComparer: IEqualityComparer {
137
138                 bool IEqualityComparer.Equals (object x, object y)
139                 {
140                         return (CaseInsensitiveComparer.DefaultInvariant.Compare (x, y) == 0);
141                 }
142
143                 public int GetHashCode (object obj)
144                 {
145                         return obj.GetHashCode ();
146                 }
147         }
148
149         [TestFixture]
150         public class NameObjectCollectionBaseTest
151         {
152                 private void CheckICollection (UnitTestNameObjectCollectionBase coll, int count)
153                 {
154                         ICollection collection = (coll as ICollection);
155                         Assert.AreEqual (count, collection.Count, "Count");
156                         Assert.IsFalse (collection.IsSynchronized, "IsSynchronized");
157                         Assert.IsNotNull (collection.SyncRoot, "SyncRoot");
158                         string[] array = new string[count];
159                         collection.CopyTo (array, 0);
160                         for (int i = 0; i < count; i++) {
161                                 Assert.AreEqual (coll.GetKey (i), array[i], "#" + i.ToString ());
162                         }
163                 }
164
165                 [Test]
166                 public void Constructor_Default ()
167                 {
168                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
169                         Assert.AreEqual (0, coll.Count, "Count-0");
170                         Assert.IsFalse (coll._IsReadOnly, "IsReadOnly");
171                         Assert.IsFalse (coll.HasKeys (), "HasKeys-0");
172
173                         coll.Add ("a", "1");
174                         CheckICollection (coll, 1);
175                         Assert.AreEqual (1, coll.Count, "Count-1");
176                         Assert.AreEqual ("1", coll.Get (0), "Get(0)");
177                         Assert.AreEqual (1, coll.GetAllKeys ().Length, "GetAllKeys");
178                         Assert.IsTrue (coll.HasKeys (), "HasKeys-1");
179
180                         coll.Add ("b", "2");
181                         Assert.AreEqual (2, coll.Count, "Count-2");
182                         Assert.AreEqual ("b", coll.GetKey (1), "GetKey(1)");
183                         Assert.AreEqual (2, coll.GetAllValues ().Length, "GetAllValues");
184
185                         coll.Remove ("a");
186                         Assert.AreEqual (1, coll.Count, "Count-3");
187
188                         coll.Set (0, "3");
189                         Assert.AreEqual ("3", coll.Get (0), "Get(0)b");
190                         coll.Set ("b", "4");
191                         Assert.AreEqual ("4", coll.Get (0), "Get(0)c");
192
193                         coll.RemoveAt (0);
194                         Assert.AreEqual (0, coll.Count, "Count-4");
195                         Assert.IsFalse (coll.HasKeys (), "HasKeys-2");
196                 }
197
198                 [Test]
199                 public void Constructor_Int ()
200                 {
201                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (0);
202                         for (int i = 0; i < 10; i++)
203                                 coll.Add (i.ToString (), i);
204
205                         CheckICollection (coll, 10);
206                         Assert.AreEqual (10, coll.Keys.Count, "Keys");
207
208                         coll.Clear ();
209                         Assert.AreEqual (0, coll.Count, "Count");
210                         Assert.IsFalse (coll.HasKeys (), "HasKeys");
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
215                 public void Constructor_Int_MinValue ()
216                 {
217                         new UnitTestNameObjectCollectionBase (Int32.MinValue);
218                 }
219                 [Test]
220                 public void Constructor_IEqualityComparer ()
221                 {
222                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (new EqualityComparer ());
223                         coll.Add ("a", "1");
224                         CheckICollection (coll, 1);
225                 }
226
227                 [Test]
228                 public void Constructor_Int_IEqualityComparer ()
229                 {
230                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, new EqualityComparer ());
231                         coll.Add ("a", "1");
232                         CheckICollection (coll, 1);
233                 }
234
235                 [Test]
236                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
237                 public void Constructor_IntNegative_IEqualityComparer ()
238                 {
239                         new UnitTestNameObjectCollectionBase (-1, new EqualityComparer ());
240                 }
241
242                 [Test]
243                 public void GetObjectData_IEqualityComparer ()
244                 {
245                         EqualityComparer comparer = new EqualityComparer ();
246                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, comparer);
247                         coll.Add ("a", "1");
248                         coll.Add ("b", "2");
249                         coll._IsReadOnly = true;
250
251                         SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
252                         coll.GetObjectData (si, new StreamingContext ());
253                         foreach (SerializationEntry se in si) {
254                                 switch (se.Name) {
255                                 case "KeyComparer":
256                                         Assert.AreSame (comparer, se.Value, se.Name);
257                                         break;
258                                 case "ReadOnly":
259                                         Assert.IsTrue ((bool) se.Value, se.Name);
260                                         break;
261                                 case "Count":
262                                         Assert.AreEqual (2, se.Value, se.Name);
263                                         break;
264                                 case "Values":
265                                         Assert.AreEqual (2, (se.Value as object[]).Length, se.Name);
266                                         break;
267                                 case "Keys":
268                                         Assert.AreEqual (2, (se.Value as string[]).Length, se.Name);
269                                         break;
270                                 case "Version":
271                                         Assert.AreEqual (4, se.Value, se.Name);
272                                         break;
273                                 default:
274                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
275                                                 se.Name, se.ObjectType, se.Value);
276                                         Assert.Fail (msg);
277                                         break;
278                                 }
279                         }
280                 }
281                 [Test]
282                 public void Constructor_Provider_Comparer ()
283                 {
284                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
285                         coll.Add (null, null);
286                         Assert.AreEqual (1, coll.Count, "Count-1");
287                         coll.Remove (null);
288                         Assert.AreEqual (0, coll.Count, "Count-0");
289                 }
290
291                 [Test]
292                 public void Constructor_Int_Provider_Comparer ()
293                 {
294                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
295                         coll.Add ("a", "1");
296                         int i = 0;
297                         IEnumerator e = coll.GetEnumerator ();
298                         while (e.MoveNext ()) {
299                                 i++;
300                         }
301                         Assert.AreEqual (1, i, "GetEnumerator");
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
306                 public void Constructor_Int_Negative ()
307                 {
308                         new UnitTestNameObjectCollectionBase (-1, 
309                                 CaseInsensitiveHashCodeProvider.DefaultInvariant,
310                                 CaseInsensitiveComparer.DefaultInvariant);
311                 }
312
313                 [Test]
314                 public void GetObjectData_Info_Null ()
315                 {
316                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
317                         try {
318                                 coll.GetObjectData (null, new StreamingContext ());
319                                 Assert.Fail ("#1");
320                         } catch (ArgumentNullException ex) {
321                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
322                                 Assert.IsNull (ex.InnerException, "#3");
323                                 Assert.IsNotNull (ex.Message, "#4");
324                                 Assert.IsNotNull (ex.ParamName, "#5");
325                                 Assert.AreEqual ("info", ex.ParamName, "#6");
326                         }
327                 }
328
329                 [Test]
330                 public void GetObjectData ()
331                 {
332                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
333                         coll.Add ("a", "1");
334
335                         SerializationInfo si = new SerializationInfo (typeof (UnitTestNameObjectCollectionBase), new FormatterConverter ());
336                         coll.GetObjectData (si, new StreamingContext ());
337                         foreach (SerializationEntry se in si) {
338                                 switch (se.Name) {
339                                 case "HashProvider":
340                                         Assert.AreSame (CaseInsensitiveHashCodeProvider.DefaultInvariant, se.Value, se.Name);
341                                         break;
342                                 case "Comparer":
343                                         Assert.AreSame (CaseInsensitiveComparer.DefaultInvariant, se.Value, se.Name);
344                                         break;
345                                 case "ReadOnly":
346                                         Assert.IsFalse ((bool)se.Value, se.Name);
347                                         break;
348                                 case "Count":
349                                         Assert.AreEqual (1, se.Value, se.Name);
350                                         break;
351                                 case "Values":
352                                         Assert.AreEqual (1, (se.Value as object[]).Length, se.Name);
353                                         break;
354                                 case "Keys":
355                                         Assert.AreEqual (1, (se.Value as string[]).Length, se.Name);
356                                         break;
357                                 case "Version":
358                                         Assert.AreEqual (2, se.Value, se.Name);
359                                         break;
360                                 default:
361                                         string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.",
362                                                 se.Name, se.ObjectType, se.Value);
363                                         Assert.Fail (msg);
364                                         break;
365                                 }
366                         }
367                 }
368
369                 [Test]
370                 public void Add_ReadOnly ()
371                 {
372                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
373                         coll._IsReadOnly = true;
374                         try {
375                                 coll.Add ("a", "1");
376                                 Assert.Fail ("#1");
377                         } catch (NotSupportedException ex) {
378                                 // Collection is read-only
379                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
380                                 Assert.IsNull (ex.InnerException, "#3");
381                                 Assert.IsNotNull (ex.Message, "#4");
382                         }
383                 }
384
385                 [Test]
386                 public void Clear_ReadOnly ()
387                 {
388                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
389                         coll._IsReadOnly = true;
390                         try {
391                                 // even if we're empty
392                                 coll.Clear ();
393                                 Assert.Fail ("#1");
394                         } catch (NotSupportedException ex) {
395                                 // Collection is read-only
396                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
397                                 Assert.IsNull (ex.InnerException, "#3");
398                                 Assert.IsNotNull (ex.Message, "#4");
399                         }
400                 }
401
402                 [Test]
403                 public void Remove_ReadOnly ()
404                 {
405                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
406                         coll.Add ("a", "!");
407                         coll._IsReadOnly = true;
408                         try {
409                                 coll.Remove ("a");
410                                 Assert.Fail ("#1");
411                         } catch (NotSupportedException ex) {
412                                 // Collection is read-only
413                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
414                                 Assert.IsNull (ex.InnerException, "#3");
415                                 Assert.IsNotNull (ex.Message, "#4");
416                         }
417                 }
418
419                 [Test]
420                 public void RemoveAt_ReadOnly ()
421                 {
422                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
423                         coll.Add ("a", "!");
424                         coll._IsReadOnly = true;
425                         try {
426                                 coll.RemoveAt (0);
427                                 Assert.Fail ("#1");
428                         } catch (NotSupportedException ex) {
429                                 // Collection is read-only
430                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
431                                 Assert.IsNull (ex.InnerException, "#3");
432                                 Assert.IsNotNull (ex.Message, "#4");
433                         }
434                 }
435
436                 [Test]
437                 public void Set_ReadOnly ()
438                 {
439                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
440                         coll.Add ("a", "!");
441                         coll._IsReadOnly = true;
442                         try {
443                                 coll.Set (0, "1");
444                                 Assert.Fail ("#1");
445                         } catch (NotSupportedException ex) {
446                                 // Collection is read-only
447                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
448                                 Assert.IsNull (ex.InnerException, "#3");
449                                 Assert.IsNotNull (ex.Message, "#4");
450                         }
451                 }
452
453                 [Test]
454                 [ExpectedException (typeof (ArrayTypeMismatchException))]
455                 public void GetAllValues_Type_Mismatch ()
456                 {
457                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
458                         coll.Add ("a", "string");
459                         coll.Add ("b", Int32.MinValue);
460                         string[] array = (string[]) coll.GetAllValues (typeof (string));
461                         Assert.AreEqual (1, array.Length, "Length");
462                         Assert.AreEqual ("string", array[0], "[0]");
463                 }
464
465                 [Test]
466                 public void GetAllValues_Type ()
467                 {
468                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
469                         coll.Add ("a", "string1");
470                         coll.Add ("b", "string2");
471                         string[] array = (string[]) coll.GetAllValues (typeof (string));
472                         Assert.AreEqual (2, array.Length, "Length");
473                         Assert.AreEqual ("string1", array[0], "[0]");
474                         Assert.AreEqual ("string2", array[1], "[1]");
475                 }
476
477                 [Test]
478                 public void GetAllValues ()
479                 {
480                         UnitTestNameObjectCollectionBase coll = new UnitTestNameObjectCollectionBase ();
481                         coll.Add ("a", "string1");
482                         coll.Add ("b", "string2");
483                         object[] array = (object[]) coll.GetAllValues ();
484                         Assert.AreEqual (2, array.Length, "Length");
485                         Assert.AreEqual ("string1", array[0], "[0]");
486                         Assert.AreEqual ("string2", array[1], "[1]");
487                 }
488
489                 [Test]
490                 public void CopyTo_Array_Null () 
491                 {
492                         UnitTestNameObjectCollectionBase c = new UnitTestNameObjectCollectionBase ();
493                         try {
494                                 ((ICollection)c).CopyTo (null, 0);
495                                 Assert.Fail ("#A1");
496                         } catch (ArgumentNullException ex) {
497                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
498                                 Assert.IsNull (ex.InnerException, "#A3");
499                                 Assert.IsNotNull (ex.Message, "#A4");
500                                 Assert.IsNotNull (ex.ParamName, "#A5");
501                                 Assert.AreEqual ("array", ex.ParamName, "#A6");
502                         }
503
504                         c.Add ("1", "mono");
505
506                         try {
507                                 ((ICollection) c).CopyTo (null, 0);
508                                 Assert.Fail ("#B1");
509                         } catch (ArgumentNullException ex) {
510                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
511                                 Assert.IsNull (ex.InnerException, "#B3");
512                                 Assert.IsNotNull (ex.Message, "#B4");
513                                 Assert.IsNotNull (ex.ParamName, "#B5");
514                                 Assert.AreEqual ("array", ex.ParamName, "#B6");
515                         }
516                 }
517
518                 [Test]
519                 public void CopyTo_Index_Negative () 
520                 {
521                         string [] array = new string [1];
522                         UnitTestNameObjectCollectionBase c = new UnitTestNameObjectCollectionBase ();
523                         c.Add ("1", "mono");
524                         try {
525                                 ((ICollection) c).CopyTo (array, -1);
526                                 Assert.Fail ("#1");
527                         } catch (ArgumentOutOfRangeException ex) {
528                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
529                                 Assert.IsNull (ex.InnerException, "#3");
530                                 Assert.IsNotNull (ex.Message, "#4");
531                                 Assert.IsNotNull (ex.ParamName, "#5");
532                         }
533                 }
534
535                 [Test]
536                 public void CopyTo_NotEnoughSpace () 
537                 {
538                         string [] array = new string [4];
539                         UnitTestNameObjectCollectionBase c = new UnitTestNameObjectCollectionBase ();
540                         c.Add ("1", "mono");
541                         c.Add ("2", "MoNo");
542                         c.Add ("3", "mOnO");
543                         c.Add ("4", "MONO");
544                         try {
545                                 ((ICollection) c).CopyTo (array, 2);
546                                 Assert.Fail ("#1");
547                         } catch (ArgumentException ex) {
548                                 // Insufficient space in the target location to
549                                 // copy the information
550                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
551                                 Assert.IsNull (ex.InnerException, "#3");
552                                 Assert.IsNotNull (ex.Message, "#4");
553                                 Assert.IsNull (ex.ParamName, "#5");
554                         }
555                 }
556
557                 [Test]
558                 public void CopyTo_MultipleDimensionStringArray () 
559                 {
560                         string [,] matrix = new string [1,1];
561                         UnitTestNameObjectCollectionBase c = new UnitTestNameObjectCollectionBase ();
562                         c.Add ("1", "mono");
563                         try {
564                                 ((ICollection)c).CopyTo (matrix, 0);
565                                 Assert.Fail ("#1");
566                         } catch (ArgumentException ex) {
567                                 // Multi dimension array is not supported on
568                                 // this operation
569                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
570                                 Assert.IsNull (ex.InnerException, "#3");
571                                 Assert.IsNotNull (ex.Message, "#4");
572                                 Assert.IsNull (ex.ParamName, "#5");
573                         }
574                 }
575         }
576 }