Merge pull request #2434 from knocte/bitmapattributes
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / NameValueCollectionTest.cs
1 // created on 7/21/2001 at 2:36 PM
2 //
3 // Authors:
4 //      Martin Willemoes Hansen (mwh@sysrq.dk)
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Martin Willemoes Hansen
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Collections.Specialized;
15 using System.Text;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Collections.Specialized {
20
21         [TestFixture]
22         public class NameValueCollectionTest {
23
24                 [Test]
25                 public void GetValues ()
26                 {
27                         NameValueCollection col = new NameValueCollection ();
28                         col.Add ("foo1", "bar1");
29                         Assert.AreEqual (null, col.GetValues (null), "#1");
30                         Assert.AreEqual (null, col.GetValues (""), "#2");
31                         Assert.AreEqual (null, col.GetValues ("NotExistent"), "#3");
32                         Assert.AreEqual (1, col.GetValues (0).Length, "#4");
33                 }
34
35                 [Test]
36                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
37                 public void GetValues_OutOfRange ()
38                 {
39                         NameValueCollection c = new NameValueCollection ();
40                         c.Add ("foo1", "bar1");
41                         Assert.AreEqual (null, c.GetValues (1), "#5");
42                 }
43
44                 [Test]
45                 public void Get ()
46                 {
47                         NameValueCollection col = new NameValueCollection (5);
48                         col.Add ("foo1", "bar1");
49                         Assert.AreEqual (null, col.Get (null), "#1");
50                         Assert.AreEqual (null, col.Get (""), "#2");
51                         Assert.AreEqual (null, col.Get ("NotExistent"), "#3");
52                         Assert.AreEqual ("bar1", col.Get ("foo1"), "#4");
53                         Assert.AreEqual ("bar1", col.Get (0), "#5");
54                 }
55
56                 [Test]
57                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
58                 public void Get_OutOfRange ()
59                 {
60                         NameValueCollection c = new NameValueCollection ();
61                         c.Add ("foo1", "bar1");
62                         Assert.AreEqual (null, c.Get (1), "#6");
63                 }
64
65                 [Test]
66                 public void GetKey ()
67                 {
68                         NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
69                         c.Add ("foo1", "bar1");
70                         Assert.AreEqual ("foo1", c.GetKey (0), "#1");
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
75                 public void GetKey_OutOfRange ()
76                 {
77                         NameValueCollection c = new NameValueCollection ();
78                         c.Add ("foo1", "bar1");
79                         Assert.AreEqual (null, c.GetKey (1), "#2");
80                 }
81
82                 [Test]
83                 public void HasKeys ()
84                 {
85                         NameValueCollection c = new NameValueCollection (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
86                         Assert.IsTrue (!c.HasKeys (), "#1");
87                         c.Add ("foo1", "bar1");
88                         Assert.IsTrue (c.HasKeys (), "#2");
89                 }
90
91                 [Test]
92                 public void Clear ()
93                 {
94                         NameValueCollection c = new NameValueCollection ();
95                         Assert.AreEqual (0, c.Count, "#1");
96                         c.Add ("foo1", "bar1");
97                         Assert.AreEqual (1, c.Count, "#2");
98                         c.Clear ();
99                         Assert.AreEqual (0, c.Count, "#3");
100                 }
101
102                 [Test]
103                 public void Add ()
104                 {
105                         NameValueCollection c = new NameValueCollection ();
106                         c.Add ("mono", "mono");
107                         c.Add ("!mono", null);
108                         c.Add (null, "mono!");
109                         Assert.AreEqual (3, c.Count, "Count");
110                         Assert.AreEqual ("mono", c ["mono"], "mono");
111                         Assert.IsNull (c ["!mono"], "!mono");
112                         Assert.AreEqual ("mono!", c [null], "mono!");
113                 }
114
115                 [Test]
116                 public void Add_Calls ()
117                 {
118                         var nvc1 = new MyNVC ();
119                         var nvc2 = new MyNVC ();
120                         nvc1.Add ("one", "1");
121                         nvc1.Add ("one", "one");
122                         nvc1.Add ("two", null);
123                         nvc2.Add (nvc1);
124
125                         string[] values;
126                         Assert.AreEqual (8, nvc1.Log.Count, "#A1-1");
127                         Assert.AreEqual ("Add (string, string)", nvc1.Log [0], "#A1-2");
128                         Assert.AreEqual ("Add (string, string)", nvc1.Log [1], "#A1-3");
129                         Assert.AreEqual ("Add (string, string)", nvc1.Log [2], "#A1-4");
130                         Assert.AreEqual ("get_Count", nvc1.Log [3], "#A1-5");
131                         Assert.AreEqual ("GetKey (int)", nvc1.Log [4], "#A1-6");
132                         Assert.AreEqual ("GetValues (int)", nvc1.Log [5], "#A1-7");
133                         Assert.AreEqual ("GetKey (int)", nvc1.Log [6], "#A1-8");
134                         Assert.AreEqual ("GetValues (int)", nvc1.Log [7], "#A1-9");
135
136                         Assert.AreEqual (2, nvc1.Count, "#A2-1");
137                         values = nvc1.GetValues (0);
138                         Assert.AreEqual ("one", nvc1.GetKey (0), "#A2-2");
139                         Assert.AreEqual ("1", values [0], "#A2-3");
140                         Assert.AreEqual ("one", values [1], "#A2-4");
141                         values = nvc1.GetValues (1);
142                         Assert.AreEqual ("two", nvc1.GetKey (1), "#A2-5");
143                         Assert.IsTrue (values == null, "#A2-6");
144
145                         Assert.AreEqual (3, nvc2.Log.Count, "#B1-1");
146                         Assert.AreEqual ("Add (string, string)", nvc2.Log [0], "#B1-2");
147                         Assert.AreEqual ("Add (string, string)", nvc2.Log [1], "#B1-3");
148                         Assert.AreEqual ("Add (string, string)", nvc2.Log [2], "#B1-4");
149
150                         Assert.AreEqual (2, nvc2.Count, "#B2-1");
151                         values = nvc2.GetValues (0);
152                         Assert.AreEqual ("one", nvc2.GetKey (0), "#B2-2");
153                         Assert.AreEqual ("1", values [0], "#B2-3");
154                         Assert.AreEqual ("one", values [1], "#B2-4");
155                         values = nvc2.GetValues (1);
156                         Assert.AreEqual ("two", nvc2.GetKey (1), "#B2-5");
157                         Assert.IsTrue (values == null, "#B2-6");
158                 }
159                 
160                 [Test]
161                 public void Add_Multiples ()
162                 {
163                         NameValueCollection c = new NameValueCollection ();
164                         c.Add ("mono", "mono");
165                         c.Add ("mono", "mono");
166                         c.Add ("mono", "mono");
167                         Assert.AreEqual (1, c.Count, "Count");
168                         Assert.AreEqual ("mono,mono,mono", c ["mono"], "mono");
169                 }
170
171                 [Test]
172                 public void Add_Multiples_Null ()
173                 {
174                         NameValueCollection c = new NameValueCollection ();
175                         c.Add ("mono", "mono");
176                         c.Add ("mono", null);
177                         c.Add ("mono", "mono");
178                         Assert.AreEqual (1, c.Count, "Count");
179                         Assert.AreEqual ("mono,mono", c ["mono"], "mono");
180                 }
181
182                 [Test]
183                 public void Add_NVC ()
184                 {
185                         NameValueCollection c1 = new NameValueCollection ();
186                         NameValueCollection c2 = new NameValueCollection (c1);
187
188                         c2.Add (c1);
189                         Assert.AreEqual (0, c1.Count, "c1.Count");
190                         Assert.AreEqual (0, c2.Count, "c2.Count");
191
192                         c1.Add ("foo", "bar");
193                         c2.Add ("bar", "foo");
194
195                         Assert.AreEqual (1, c1.Count, "c1.Count");
196                         Assert.AreEqual (1, c2.Count, "c2.Count");
197
198                         c2.Add (c1);
199                         Assert.AreEqual (1, c1.Count, "c1.Count");
200                         Assert.AreEqual (2, c2.Count, "c2.Count");
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (ArgumentNullException))]
205                 public void Add_NVC_Null ()
206                 {
207                         new NameValueCollection ().Add (null);
208                 }
209
210                 [Test]
211                 public void Add_NVC_Null2 ()
212                 {
213                         NameValueCollection a = new NameValueCollection ();
214                         NameValueCollection b = new NameValueCollection ();
215
216                         b.Add ("Test", null);
217                         a.Add (b);
218                         Assert.AreEqual (1, a.Count, "Count");
219                 }
220
221                 [Test]
222                 public void Set_New ()
223                 {
224                         NameValueCollection c = new NameValueCollection ();
225                         c.Set ("mono", "mono");
226                         c.Set ("!mono", null);
227                         c.Set (null, "mono!");
228                         Assert.AreEqual (3, c.Count, "Count");
229                         Assert.AreEqual ("mono", c ["mono"], "mono");
230                         Assert.IsNull (c ["!mono"], "!mono");
231                         Assert.AreEqual ("mono!", c [null], "mono!");
232                 }
233
234                 [Test]
235                 public void Set_Replace ()
236                 {
237                         NameValueCollection c = new NameValueCollection ();
238                         c.Add ("mono", "mono");
239                         c.Add ("!mono", "!mono");
240                         c.Add ("mono!", "mono!");
241                         Assert.AreEqual (3, c.Count, "Count");
242                         Assert.AreEqual ("mono", c ["mono"], "mono");
243                         Assert.AreEqual ("!mono", c ["!mono"], "!mono");
244                         Assert.AreEqual ("mono!", c ["mono!"], "mono!");
245
246                         c.Set ("mono", "nomo");
247                         c.Set ("!mono", null);
248                         c.Set (null, "mono!");
249                         Assert.AreEqual (4, c.Count, "Count"); // mono! isn't removed
250                         Assert.AreEqual ("nomo", c ["mono"], "mono");
251                         Assert.IsNull (c ["!mono"], "!mono");
252                         Assert.AreEqual ("mono!", c ["mono!"], "mono!1");
253                         Assert.AreEqual ("mono!", c [null], "mono!2");
254                 }
255
256                 [Test]
257                 public void CaseInsensitive () 
258                 {
259                         // default constructor is case insensitive
260                         NameValueCollection c = new NameValueCollection ();
261                         c.Add ("mono", "mono");
262                         c.Add ("MoNo", "MoNo");
263                         c.Add ("mOnO", "mOnO");
264                         c.Add ("MONO", "MONO");
265                         Assert.AreEqual (1, c.Count, "Count");
266                 }
267
268                 [Test]
269                 public void CopyTo () 
270                 {
271                         string [] array = new string [4];
272                         NameValueCollection c = new NameValueCollection ();
273                         c.Add ("1", "mono");
274                         c.Add ("2", "MoNo");
275                         c.Add ("3", "mOnO");
276                         c.Add ("4", "MONO");
277                         c.CopyTo (array, 0);
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (ArgumentNullException))]
282                 public void CopyTo_Null () 
283                 {
284                         NameValueCollection c = new NameValueCollection ();
285                         c.CopyTo (null, 0);
286                 }
287
288                 [Test]
289                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
290                 public void CopyTo_NegativeIndex () 
291                 {
292                         string [] array = new string [4];
293                         NameValueCollection c = new NameValueCollection ();
294                         c.Add ("1", "mono");
295                         c.Add ("2", "MoNo");
296                         c.Add ("3", "mOnO");
297                         c.Add ("4", "MONO");
298                         c.CopyTo (array, -1);
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (ArgumentException))]
303                 public void CopyTo_NotEnoughSpace () 
304                 {
305                         string [] array = new string [4];
306                         NameValueCollection c = new NameValueCollection ();
307                         c.Add ("1", "mono");
308                         c.Add ("2", "MoNo");
309                         c.Add ("3", "mOnO");
310                         c.Add ("4", "MONO");
311                         c.CopyTo (array, 2);
312                 }
313
314                 [Test]
315                 // Note: not a RankException
316                 [ExpectedException (typeof (ArgumentException))]
317                 public void CopyTo_MultipleDimensionStringArray () 
318                 {
319                         string [,,] matrix = new string [2,3,4];
320                         NameValueCollection c = new NameValueCollection ();
321                         c.Add ("1", "mono");
322                         c.Add ("2", "MoNo");
323                         c.Add ("3", "mOnO");
324                         c.Add ("4", "MONO");
325                         c.CopyTo (matrix, 0);
326                 }
327
328                 [Test]
329                 // Note: not a RankException
330                 [ExpectedException (typeof (ArgumentException))]
331                 public void CopyTo_MultipleDimensionArray () 
332                 {
333                         Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
334                         NameValueCollection c = new NameValueCollection ();
335                         c.CopyTo (a, 0);
336                 }
337                 
338                 [Test]
339                 [ExpectedException (typeof (InvalidCastException))]
340                 public void CopyTo_WrongTypeArray ()
341                 {
342                         Array a = Array.CreateInstance (typeof (DateTime), 3);
343                         NameValueCollection c = new NameValueCollection ();
344                         for (int i = 0; i < 3; i++)
345                                 c.Add(i.ToString(), i.ToString());
346                         c.CopyTo(a, 0);
347                 }
348
349                 [Test]
350                 public void Remove () 
351                 {
352                         string[] items = { "mono", "MoNo", "mOnO", "MONO" };
353                         // default constructor is case insensitive
354                         NameValueCollection c = new NameValueCollection ();
355                         for (int i=0; i < items.Length; i++) {
356                                 string add = "Add-" + i.ToString () + "-Count";
357
358                                 c.Add (items [i], add);
359                                 Assert.AreEqual (1, c.Count, add);
360                                 c.Remove (items [0]);
361                                 Assert.AreEqual (0, c.Count, "Remove-0-Count");
362
363                                 c.Add (items [i], add);
364                                 Assert.AreEqual (1, c.Count, add);
365                                 c.Remove (items [1]);
366                                 Assert.AreEqual (0, c.Count, "Remove-1-Count");
367
368                                 c.Add (items [i], add);
369                                 Assert.AreEqual (1, c.Count, add);
370                                 c.Remove (items [2]);
371                                 Assert.AreEqual (0, c.Count, "Remove-2-Count");
372
373                                 c.Add (items [i], add);
374                                 Assert.AreEqual (1, c.Count, add);
375                                 c.Remove (items [3]);
376                                 Assert.AreEqual (0, c.Count, "Remove-3-Count");
377                         }
378                 }
379                 [Test]
380                 [ExpectedException (typeof (ArgumentNullException))]
381                 public void Constructor_Null_NVC ()
382                 {
383                         NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
384                 }
385                 
386                 [Test]
387                 [ExpectedException (typeof (ArgumentNullException))]
388                 public void Constructor_Capacity_Null_NVC ()
389                 {
390                         NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
391                 }
392
393                 [Test]
394                 public void Constructor_IEqualityComparer ()
395                 {
396                         NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
397                         coll.Add ("a", "1");
398                         Assert.AreEqual (1, coll.Count, "#1");
399                 }
400
401                 [Test]
402                 public void Constructor_Int_IEqualityComparer ()
403                 {
404                         NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
405                         coll.Add ("a", "1");
406                         Assert.AreEqual (1, coll.Count, "#1");
407                 }
408
409                 [Test]
410                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
411                 public void Constructor_IntNegative_IEqualityComparer ()
412                 {
413                         new NameValueCollection (-1, new EqualityComparer ());
414                 }
415
416                 [Test]
417                 public void Constructor_IEqualityComparer_Null ()
418                 {
419                         NameValueCollection c1 = new NameValueCollection ((IEqualityComparer)null);
420                         c1.Add ("key", "value");
421                         Assert.AreEqual (c1.Get ("KEY"), "value", "Constructor_IEqualityComparer_Null");
422                         c1.Remove ("key");
423                 }
424
425                 [Test]
426                 public void Constructor_NameValueCollection ()
427                 {
428                         NameValueCollection c1 = new NameValueCollection (StringComparer.InvariantCultureIgnoreCase);
429                         c1.Add ("key", "value");
430                         NameValueCollection c2 = new NameValueCollection (c1);
431                         Assert.AreEqual (c2.Get ("KEY"), "value", "Constructor_NameValueCollection");
432                         c2.Remove ("key");
433                 }
434                 class MyNVC : NameValueCollection
435                 {
436                         List<string> log;
437
438                         public List<string> Log {
439                                 get {
440                                         if (log == null)
441                                                 log = new List<string> ();
442                                         return log;
443                                 }
444                         }
445
446                         public override KeysCollection Keys {
447                                 get {
448                                         Log.Add ("get_Keys");
449                                         return base.Keys;
450                                 }
451                         }
452
453                         public override int Count {
454                                 get {
455                                         Log.Add ("get_Count");
456                                         return base.Count;
457                                 }
458                         }
459
460                         public override string[] AllKeys {
461                                 get {
462                                         Log.Add ("get_AllKeys");
463                                         return base.AllKeys;
464                                 }
465                         }
466                         
467                         public override string Get (int index)
468                         {
469                                 Log.Add ("Get (int)");
470                                 return base.Get (index);
471                         }
472
473                         public override string Get (string name)
474                         {
475                                 Log.Add ("Get (string)");
476                                 return base.Get (name);
477                         }
478
479                         public override string GetKey (int index)
480                         {
481                                 Log.Add ("GetKey (int)");
482                                 return base.GetKey (index);
483                         }
484
485                         public override string[] GetValues (int index)
486                         {
487                                 Log.Add ("GetValues (int)");
488                                 return base.GetValues (index);
489                         }
490
491                         public override string[] GetValues (string name)
492                         {
493                                 Log.Add ("GetValues (string)");
494                                 return base.GetValues (name);
495                         }
496
497                         public override void Add (string name, string value)
498                         {
499                                 Log.Add ("Add (string, string)");
500                                 base.Add (name, value);
501                         }
502
503                         public override void Set (string name, string value)
504                         {
505                                 Log.Add ("Set (string, string)");
506                                 base.Set (name, value);
507                         }
508                 }
509         }
510 }