1 // created on 7/21/2001 at 2:36 PM
4 // Martin Willemoes Hansen (mwh@sysrq.dk)
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2003 Martin Willemoes Hansen
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Collections.Specialized;
17 using NUnit.Framework;
19 namespace MonoTests.System.Collections.Specialized {
22 public class NameValueCollectionTest {
25 public void GetValues ()
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");
36 [ExpectedException (typeof (ArgumentOutOfRangeException))]
37 public void GetValues_OutOfRange ()
39 NameValueCollection c = new NameValueCollection ();
40 c.Add ("foo1", "bar1");
41 Assert.AreEqual (null, c.GetValues (1), "#5");
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");
57 [ExpectedException (typeof (ArgumentOutOfRangeException))]
58 public void Get_OutOfRange ()
60 NameValueCollection c = new NameValueCollection ();
61 c.Add ("foo1", "bar1");
62 Assert.AreEqual (null, c.Get (1), "#6");
68 NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
69 c.Add ("foo1", "bar1");
70 Assert.AreEqual ("foo1", c.GetKey (0), "#1");
74 [ExpectedException (typeof (ArgumentOutOfRangeException))]
75 public void GetKey_OutOfRange ()
77 NameValueCollection c = new NameValueCollection ();
78 c.Add ("foo1", "bar1");
79 Assert.AreEqual (null, c.GetKey (1), "#2");
83 public void HasKeys ()
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");
94 NameValueCollection c = new NameValueCollection ();
95 Assert.AreEqual (0, c.Count, "#1");
96 c.Add ("foo1", "bar1");
97 Assert.AreEqual (1, c.Count, "#2");
99 Assert.AreEqual (0, c.Count, "#3");
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!");
116 public void Add_Calls ()
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);
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");
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");
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");
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");
161 public void Add_Multiples ()
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");
172 public void Add_Multiples_Null ()
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");
183 public void Add_NVC ()
185 NameValueCollection c1 = new NameValueCollection ();
186 NameValueCollection c2 = new NameValueCollection (c1);
189 Assert.AreEqual (0, c1.Count, "c1.Count");
190 Assert.AreEqual (0, c2.Count, "c2.Count");
192 c1.Add ("foo", "bar");
193 c2.Add ("bar", "foo");
195 Assert.AreEqual (1, c1.Count, "c1.Count");
196 Assert.AreEqual (1, c2.Count, "c2.Count");
199 Assert.AreEqual (1, c1.Count, "c1.Count");
200 Assert.AreEqual (2, c2.Count, "c2.Count");
205 [ExpectedException (typeof (ArgumentNullException))]
207 [ExpectedException (typeof (NullReferenceException))]
209 public void Add_NVC_Null ()
211 new NameValueCollection ().Add (null);
215 public void Add_NVC_Null2 ()
217 NameValueCollection a = new NameValueCollection ();
218 NameValueCollection b = new NameValueCollection ();
220 b.Add ("Test", null);
222 Assert.AreEqual (1, a.Count, "Count");
226 public void Set_New ()
228 NameValueCollection c = new NameValueCollection ();
229 c.Set ("mono", "mono");
230 c.Set ("!mono", null);
231 c.Set (null, "mono!");
232 Assert.AreEqual (3, c.Count, "Count");
233 Assert.AreEqual ("mono", c ["mono"], "mono");
234 Assert.IsNull (c ["!mono"], "!mono");
235 Assert.AreEqual ("mono!", c [null], "mono!");
239 public void Set_Replace ()
241 NameValueCollection c = new NameValueCollection ();
242 c.Add ("mono", "mono");
243 c.Add ("!mono", "!mono");
244 c.Add ("mono!", "mono!");
245 Assert.AreEqual (3, c.Count, "Count");
246 Assert.AreEqual ("mono", c ["mono"], "mono");
247 Assert.AreEqual ("!mono", c ["!mono"], "!mono");
248 Assert.AreEqual ("mono!", c ["mono!"], "mono!");
250 c.Set ("mono", "nomo");
251 c.Set ("!mono", null);
252 c.Set (null, "mono!");
253 Assert.AreEqual (4, c.Count, "Count"); // mono! isn't removed
254 Assert.AreEqual ("nomo", c ["mono"], "mono");
255 Assert.IsNull (c ["!mono"], "!mono");
256 Assert.AreEqual ("mono!", c ["mono!"], "mono!1");
257 Assert.AreEqual ("mono!", c [null], "mono!2");
261 public void CaseInsensitive ()
263 // default constructor is case insensitive
264 NameValueCollection c = new NameValueCollection ();
265 c.Add ("mono", "mono");
266 c.Add ("MoNo", "MoNo");
267 c.Add ("mOnO", "mOnO");
268 c.Add ("MONO", "MONO");
269 Assert.AreEqual (1, c.Count, "Count");
273 public void CopyTo ()
275 string [] array = new string [4];
276 NameValueCollection c = new NameValueCollection ();
285 [ExpectedException (typeof (ArgumentNullException))]
286 public void CopyTo_Null ()
288 NameValueCollection c = new NameValueCollection ();
293 [ExpectedException (typeof (ArgumentOutOfRangeException))]
294 public void CopyTo_NegativeIndex ()
296 string [] array = new string [4];
297 NameValueCollection c = new NameValueCollection ();
302 c.CopyTo (array, -1);
306 [ExpectedException (typeof (ArgumentException))]
307 public void CopyTo_NotEnoughSpace ()
309 string [] array = new string [4];
310 NameValueCollection c = new NameValueCollection ();
319 // Note: not a RankException
320 [ExpectedException (typeof (ArgumentException))]
321 public void CopyTo_MultipleDimensionStringArray ()
323 string [,,] matrix = new string [2,3,4];
324 NameValueCollection c = new NameValueCollection ();
329 c.CopyTo (matrix, 0);
333 // Note: not a RankException
334 [ExpectedException (typeof (ArgumentException))]
335 public void CopyTo_MultipleDimensionArray ()
337 Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
338 NameValueCollection c = new NameValueCollection ();
344 [ExpectedException (typeof (InvalidCastException))]
346 [ExpectedException (typeof (ArrayTypeMismatchException))]
348 public void CopyTo_WrongTypeArray ()
350 Array a = Array.CreateInstance (typeof (DateTime), 3);
351 NameValueCollection c = new NameValueCollection ();
352 for (int i = 0; i < 3; i++)
353 c.Add(i.ToString(), i.ToString());
358 public void Remove ()
360 string[] items = { "mono", "MoNo", "mOnO", "MONO" };
361 // default constructor is case insensitive
362 NameValueCollection c = new NameValueCollection ();
363 for (int i=0; i < items.Length; i++) {
364 string add = "Add-" + i.ToString () + "-Count";
366 c.Add (items [i], add);
367 Assert.AreEqual (1, c.Count, add);
368 c.Remove (items [0]);
369 Assert.AreEqual (0, c.Count, "Remove-0-Count");
371 c.Add (items [i], add);
372 Assert.AreEqual (1, c.Count, add);
373 c.Remove (items [1]);
374 Assert.AreEqual (0, c.Count, "Remove-1-Count");
376 c.Add (items [i], add);
377 Assert.AreEqual (1, c.Count, add);
378 c.Remove (items [2]);
379 Assert.AreEqual (0, c.Count, "Remove-2-Count");
381 c.Add (items [i], add);
382 Assert.AreEqual (1, c.Count, add);
383 c.Remove (items [3]);
384 Assert.AreEqual (0, c.Count, "Remove-3-Count");
389 [ExpectedException (typeof (ArgumentNullException))]
391 [ExpectedException (typeof (NullReferenceException))]
393 public void Constructor_Null_NVC ()
395 NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
400 [ExpectedException (typeof (ArgumentNullException))]
402 [ExpectedException (typeof (NullReferenceException))]
404 public void Constructor_Capacity_Null_NVC ()
406 NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
411 public void Constructor_IEqualityComparer ()
413 NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
415 Assert.AreEqual (1, coll.Count, "#1");
419 public void Constructor_Int_IEqualityComparer ()
421 NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
423 Assert.AreEqual (1, coll.Count, "#1");
427 [ExpectedException (typeof (ArgumentOutOfRangeException))]
428 public void Constructor_IntNegative_IEqualityComparer ()
430 new NameValueCollection (-1, new EqualityComparer ());
434 public void Constructor_IEqualityComparer_Null ()
436 NameValueCollection c1 = new NameValueCollection ((IEqualityComparer)null);
437 c1.Add ("key", "value");
438 Assert.AreEqual (c1.Get ("KEY"), "value", "Constructor_IEqualityComparer_Null");
443 public void Constructor_NameValueCollection ()
445 NameValueCollection c1 = new NameValueCollection (StringComparer.InvariantCultureIgnoreCase);
446 c1.Add ("key", "value");
447 NameValueCollection c2 = new NameValueCollection (c1);
448 Assert.AreEqual (c2.Get ("KEY"), "value", "Constructor_NameValueCollection");
452 class MyNVC : NameValueCollection
456 public List<string> Log {
459 log = new List<string> ();
464 public override KeysCollection Keys {
466 Log.Add ("get_Keys");
471 public override int Count {
473 Log.Add ("get_Count");
478 public override string[] AllKeys {
480 Log.Add ("get_AllKeys");
485 public override string Get (int index)
487 Log.Add ("Get (int)");
488 return base.Get (index);
491 public override string Get (string name)
493 Log.Add ("Get (string)");
494 return base.Get (name);
497 public override string GetKey (int index)
499 Log.Add ("GetKey (int)");
500 return base.GetKey (index);
503 public override string[] GetValues (int index)
505 Log.Add ("GetValues (int)");
506 return base.GetValues (index);
509 public override string[] GetValues (string name)
511 Log.Add ("GetValues (string)");
512 return base.GetValues (name);
515 public override void Add (string name, string value)
517 Log.Add ("Add (string, string)");
518 base.Add (name, value);
521 public override void Set (string name, string value)
523 Log.Add ("Set (string, string)");
524 base.Set (name, value);