Merge branch 'master' of github.com:tgiphil/mono
[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.Specialized;
14 using System.Text;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Collections.Specialized {
19
20         [TestFixture]
21         public class NameValueCollectionTest {
22
23                 [Test]
24                 public void GetValues ()
25                 {
26                         NameValueCollection col = new NameValueCollection ();
27                         col.Add ("foo1", "bar1");
28                         Assert.AreEqual (null, col.GetValues (null), "#1");
29                         Assert.AreEqual (null, col.GetValues (""), "#2");
30                         Assert.AreEqual (null, col.GetValues ("NotExistent"), "#3");
31                         Assert.AreEqual (1, col.GetValues (0).Length, "#4");
32                 }
33
34                 [Test]
35                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
36                 public void GetValues_OutOfRange ()
37                 {
38                         NameValueCollection c = new NameValueCollection ();
39                         c.Add ("foo1", "bar1");
40                         Assert.AreEqual (null, c.GetValues (1), "#5");
41                 }
42
43                 [Test]
44                 public void Get ()
45                 {
46                         NameValueCollection col = new NameValueCollection (5);
47                         col.Add ("foo1", "bar1");
48                         Assert.AreEqual (null, col.Get (null), "#1");
49                         Assert.AreEqual (null, col.Get (""), "#2");
50                         Assert.AreEqual (null, col.Get ("NotExistent"), "#3");
51                         Assert.AreEqual ("bar1", col.Get ("foo1"), "#4");
52                         Assert.AreEqual ("bar1", col.Get (0), "#5");
53                 }
54
55                 [Test]
56                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
57                 public void Get_OutOfRange ()
58                 {
59                         NameValueCollection c = new NameValueCollection ();
60                         c.Add ("foo1", "bar1");
61                         Assert.AreEqual (null, c.Get (1), "#6");
62                 }
63
64                 [Test]
65                 public void GetKey ()
66                 {
67                         NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
68                         c.Add ("foo1", "bar1");
69                         Assert.AreEqual ("foo1", c.GetKey (0), "#1");
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
74                 public void GetKey_OutOfRange ()
75                 {
76                         NameValueCollection c = new NameValueCollection ();
77                         c.Add ("foo1", "bar1");
78                         Assert.AreEqual (null, c.GetKey (1), "#2");
79                 }
80
81                 [Test]
82                 public void HasKeys ()
83                 {
84                         NameValueCollection c = new NameValueCollection (5, CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
85                         Assert.IsTrue (!c.HasKeys (), "#1");
86                         c.Add ("foo1", "bar1");
87                         Assert.IsTrue (c.HasKeys (), "#2");
88                 }
89
90                 [Test]
91                 public void Clear ()
92                 {
93                         NameValueCollection c = new NameValueCollection ();
94                         Assert.AreEqual (0, c.Count, "#1");
95                         c.Add ("foo1", "bar1");
96                         Assert.AreEqual (1, c.Count, "#2");
97                         c.Clear ();
98                         Assert.AreEqual (0, c.Count, "#3");
99                 }
100
101                 [Test]
102                 public void Add ()
103                 {
104                         NameValueCollection c = new NameValueCollection ();
105                         c.Add ("mono", "mono");
106                         c.Add ("!mono", null);
107                         c.Add (null, "mono!");
108                         Assert.AreEqual (3, c.Count, "Count");
109                         Assert.AreEqual ("mono", c ["mono"], "mono");
110                         Assert.IsNull (c ["!mono"], "!mono");
111                         Assert.AreEqual ("mono!", c [null], "mono!");
112                 }
113
114                 [Test]
115                 public void Add_Multiples ()
116                 {
117                         NameValueCollection c = new NameValueCollection ();
118                         c.Add ("mono", "mono");
119                         c.Add ("mono", "mono");
120                         c.Add ("mono", "mono");
121                         Assert.AreEqual (1, c.Count, "Count");
122                         Assert.AreEqual ("mono,mono,mono", c ["mono"], "mono");
123                 }
124
125                 [Test]
126                 public void Add_Multiples_Null ()
127                 {
128                         NameValueCollection c = new NameValueCollection ();
129                         c.Add ("mono", "mono");
130                         c.Add ("mono", null);
131                         c.Add ("mono", "mono");
132                         Assert.AreEqual (1, c.Count, "Count");
133                         Assert.AreEqual ("mono,mono", c ["mono"], "mono");
134                 }
135
136                 [Test]
137                 public void Add_NVC ()
138                 {
139                         NameValueCollection c1 = new NameValueCollection ();
140                         NameValueCollection c2 = new NameValueCollection (c1);
141
142                         c2.Add (c1);
143                         Assert.AreEqual (0, c1.Count, "c1.Count");
144                         Assert.AreEqual (0, c2.Count, "c2.Count");
145
146                         c1.Add ("foo", "bar");
147                         c2.Add ("bar", "foo");
148
149                         Assert.AreEqual (1, c1.Count, "c1.Count");
150                         Assert.AreEqual (1, c2.Count, "c2.Count");
151
152                         c2.Add (c1);
153                         Assert.AreEqual (1, c1.Count, "c1.Count");
154                         Assert.AreEqual (2, c2.Count, "c2.Count");
155                 }
156
157                 [Test]
158 #if NET_2_0
159                 [ExpectedException (typeof (ArgumentNullException))]
160 #else
161                 [ExpectedException (typeof (NullReferenceException))]
162 #endif
163                 public void Add_NVC_Null ()
164                 {
165                         new NameValueCollection ().Add (null);
166                 }
167
168                 [Test]
169                 public void Add_NVC_Null2 ()
170                 {
171                         NameValueCollection a = new NameValueCollection ();
172                         NameValueCollection b = new NameValueCollection ();
173
174                         b.Add ("Test", null);
175                         a.Add (b);
176                         Assert.AreEqual (1, a.Count, "Count");
177                 }
178
179                 [Test]
180                 public void Set_New ()
181                 {
182                         NameValueCollection c = new NameValueCollection ();
183                         c.Set ("mono", "mono");
184                         c.Set ("!mono", null);
185                         c.Set (null, "mono!");
186                         Assert.AreEqual (3, c.Count, "Count");
187                         Assert.AreEqual ("mono", c ["mono"], "mono");
188                         Assert.IsNull (c ["!mono"], "!mono");
189                         Assert.AreEqual ("mono!", c [null], "mono!");
190                 }
191
192                 [Test]
193                 public void Set_Replace ()
194                 {
195                         NameValueCollection c = new NameValueCollection ();
196                         c.Add ("mono", "mono");
197                         c.Add ("!mono", "!mono");
198                         c.Add ("mono!", "mono!");
199                         Assert.AreEqual (3, c.Count, "Count");
200                         Assert.AreEqual ("mono", c ["mono"], "mono");
201                         Assert.AreEqual ("!mono", c ["!mono"], "!mono");
202                         Assert.AreEqual ("mono!", c ["mono!"], "mono!");
203
204                         c.Set ("mono", "nomo");
205                         c.Set ("!mono", null);
206                         c.Set (null, "mono!");
207                         Assert.AreEqual (4, c.Count, "Count"); // mono! isn't removed
208                         Assert.AreEqual ("nomo", c ["mono"], "mono");
209                         Assert.IsNull (c ["!mono"], "!mono");
210                         Assert.AreEqual ("mono!", c ["mono!"], "mono!1");
211                         Assert.AreEqual ("mono!", c [null], "mono!2");
212                 }
213
214                 [Test]
215                 public void CaseInsensitive () 
216                 {
217                         // default constructor is case insensitive
218                         NameValueCollection c = new NameValueCollection ();
219                         c.Add ("mono", "mono");
220                         c.Add ("MoNo", "MoNo");
221                         c.Add ("mOnO", "mOnO");
222                         c.Add ("MONO", "MONO");
223                         Assert.AreEqual (1, c.Count, "Count");
224                 }
225
226                 [Test]
227                 public void CopyTo () 
228                 {
229                         string [] array = new string [4];
230                         NameValueCollection c = new NameValueCollection ();
231                         c.Add ("1", "mono");
232                         c.Add ("2", "MoNo");
233                         c.Add ("3", "mOnO");
234                         c.Add ("4", "MONO");
235                         c.CopyTo (array, 0);
236                 }
237
238                 [Test]
239                 [ExpectedException (typeof (ArgumentNullException))]
240                 public void CopyTo_Null () 
241                 {
242                         NameValueCollection c = new NameValueCollection ();
243                         c.CopyTo (null, 0);
244                 }
245
246                 [Test]
247                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248                 public void CopyTo_NegativeIndex () 
249                 {
250                         string [] array = new string [4];
251                         NameValueCollection c = new NameValueCollection ();
252                         c.Add ("1", "mono");
253                         c.Add ("2", "MoNo");
254                         c.Add ("3", "mOnO");
255                         c.Add ("4", "MONO");
256                         c.CopyTo (array, -1);
257                 }
258
259                 [Test]
260                 [ExpectedException (typeof (ArgumentException))]
261                 public void CopyTo_NotEnoughSpace () 
262                 {
263                         string [] array = new string [4];
264                         NameValueCollection c = new NameValueCollection ();
265                         c.Add ("1", "mono");
266                         c.Add ("2", "MoNo");
267                         c.Add ("3", "mOnO");
268                         c.Add ("4", "MONO");
269                         c.CopyTo (array, 2);
270                 }
271
272                 [Test]
273                 // Note: not a RankException
274                 [ExpectedException (typeof (ArgumentException))]
275                 public void CopyTo_MultipleDimensionStringArray () 
276                 {
277                         string [,,] matrix = new string [2,3,4];
278                         NameValueCollection c = new NameValueCollection ();
279                         c.Add ("1", "mono");
280                         c.Add ("2", "MoNo");
281                         c.Add ("3", "mOnO");
282                         c.Add ("4", "MONO");
283                         c.CopyTo (matrix, 0);
284                 }
285
286                 [Test]
287                 // Note: not a RankException
288                 [ExpectedException (typeof (ArgumentException))]
289                 public void CopyTo_MultipleDimensionArray () 
290                 {
291                         Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
292                         NameValueCollection c = new NameValueCollection ();
293                         c.CopyTo (a, 0);
294                 }
295                 
296                 [Test]
297 #if NET_2_0
298                 [ExpectedException (typeof (InvalidCastException))]
299 #else           
300                 [ExpectedException (typeof (ArrayTypeMismatchException))]
301 #endif
302                 public void CopyTo_WrongTypeArray ()
303                 {
304                         Array a = Array.CreateInstance (typeof (DateTime), 3);
305                         NameValueCollection c = new NameValueCollection ();
306                         for (int i = 0; i < 3; i++)
307                                 c.Add(i.ToString(), i.ToString());
308                         c.CopyTo(a, 0);
309                 }
310
311                 [Test]
312                 public void Remove () 
313                 {
314                         string[] items = { "mono", "MoNo", "mOnO", "MONO" };
315                         // default constructor is case insensitive
316                         NameValueCollection c = new NameValueCollection ();
317                         for (int i=0; i < items.Length; i++) {
318                                 string add = "Add-" + i.ToString () + "-Count";
319
320                                 c.Add (items [i], add);
321                                 Assert.AreEqual (1, c.Count, add);
322                                 c.Remove (items [0]);
323                                 Assert.AreEqual (0, c.Count, "Remove-0-Count");
324
325                                 c.Add (items [i], add);
326                                 Assert.AreEqual (1, c.Count, add);
327                                 c.Remove (items [1]);
328                                 Assert.AreEqual (0, c.Count, "Remove-1-Count");
329
330                                 c.Add (items [i], add);
331                                 Assert.AreEqual (1, c.Count, add);
332                                 c.Remove (items [2]);
333                                 Assert.AreEqual (0, c.Count, "Remove-2-Count");
334
335                                 c.Add (items [i], add);
336                                 Assert.AreEqual (1, c.Count, add);
337                                 c.Remove (items [3]);
338                                 Assert.AreEqual (0, c.Count, "Remove-3-Count");
339                         }
340                 }
341                 [Test]
342 #if NET_2_0
343                 [ExpectedException (typeof (ArgumentNullException))]
344 #else
345                 [ExpectedException (typeof (NullReferenceException))]
346 #endif          
347                 public void Constructor_Null_NVC ()
348                 {
349                         NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
350                 }
351                 
352                 [Test]
353 #if NET_2_0
354                 [ExpectedException (typeof (ArgumentNullException))]
355 #else
356                 [ExpectedException (typeof (NullReferenceException))]
357 #endif          
358                 public void Constructor_Capacity_Null_NVC ()
359                 {
360                         NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
361                 }
362
363 #if NET_2_0
364                 [Test]
365                 public void Constructor_IEqualityComparer ()
366                 {
367                         NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
368                         coll.Add ("a", "1");
369                         Assert.AreEqual (1, coll.Count, "#1");
370                 }
371
372                 [Test]
373                 public void Constructor_Int_IEqualityComparer ()
374                 {
375                         NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
376                         coll.Add ("a", "1");
377                         Assert.AreEqual (1, coll.Count, "#1");
378                 }
379
380                 [Test]
381                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
382                 public void Constructor_IntNegative_IEqualityComparer ()
383                 {
384                         new NameValueCollection (-1, new EqualityComparer ());
385                 }
386
387                 [Test]\r
388                 public void Constructor_IEqualityComparer_Null ()\r
389                 {\r
390                         NameValueCollection c1 = new NameValueCollection ((IEqualityComparer)null);\r
391                         c1.Add ("key", "value");\r
392                         Assert.AreEqual (c1.Get ("KEY"), "value", "Constructor_IEqualityComparer_Null");\r
393                         c1.Remove ("key");\r
394                 }\r
395 \r
396                 [Test]\r
397                 public void Constructor_NameValueCollection ()\r
398                 {\r
399                         NameValueCollection c1 = new NameValueCollection (StringComparer.InvariantCultureIgnoreCase);\r
400                         c1.Add ("key", "value");\r
401                         NameValueCollection c2 = new NameValueCollection (c1);\r
402                         Assert.AreEqual (c2.Get ("KEY"), "value", "Constructor_NameValueCollection");\r
403                         c2.Remove ("key");\r
404                 }
405 #endif\r
406         }
407 }