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.Specialized;
16 using NUnit.Framework;
18 namespace MonoTests.System.Collections.Specialized {
21 public class NameValueCollectionTest {
24 public void GetValues ()
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");
35 [ExpectedException (typeof (ArgumentOutOfRangeException))]
36 public void GetValues_OutOfRange ()
38 NameValueCollection c = new NameValueCollection ();
39 c.Add ("foo1", "bar1");
40 Assert.AreEqual (null, c.GetValues (1), "#5");
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");
56 [ExpectedException (typeof (ArgumentOutOfRangeException))]
57 public void Get_OutOfRange ()
59 NameValueCollection c = new NameValueCollection ();
60 c.Add ("foo1", "bar1");
61 Assert.AreEqual (null, c.Get (1), "#6");
67 NameValueCollection c = new NameValueCollection (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
68 c.Add ("foo1", "bar1");
69 Assert.AreEqual ("foo1", c.GetKey (0), "#1");
73 [ExpectedException (typeof (ArgumentOutOfRangeException))]
74 public void GetKey_OutOfRange ()
76 NameValueCollection c = new NameValueCollection ();
77 c.Add ("foo1", "bar1");
78 Assert.AreEqual (null, c.GetKey (1), "#2");
82 public void HasKeys ()
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");
93 NameValueCollection c = new NameValueCollection ();
94 Assert.AreEqual (0, c.Count, "#1");
95 c.Add ("foo1", "bar1");
96 Assert.AreEqual (1, c.Count, "#2");
98 Assert.AreEqual (0, c.Count, "#3");
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!");
115 public void Add_Multiples ()
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");
126 public void Add_Multiples_Null ()
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");
137 public void Add_NVC ()
139 NameValueCollection c1 = new NameValueCollection ();
140 NameValueCollection c2 = new NameValueCollection (c1);
143 Assert.AreEqual (0, c1.Count, "c1.Count");
144 Assert.AreEqual (0, c2.Count, "c2.Count");
146 c1.Add ("foo", "bar");
147 c2.Add ("bar", "foo");
149 Assert.AreEqual (1, c1.Count, "c1.Count");
150 Assert.AreEqual (1, c2.Count, "c2.Count");
153 Assert.AreEqual (1, c1.Count, "c1.Count");
154 Assert.AreEqual (2, c2.Count, "c2.Count");
159 [ExpectedException (typeof (ArgumentNullException))]
161 [ExpectedException (typeof (NullReferenceException))]
163 public void Add_NVC_Null ()
165 new NameValueCollection ().Add (null);
169 public void Add_NVC_Null2 ()
171 NameValueCollection a = new NameValueCollection ();
172 NameValueCollection b = new NameValueCollection ();
174 b.Add ("Test", null);
176 Assert.AreEqual (1, a.Count, "Count");
180 public void Set_New ()
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!");
193 public void Set_Replace ()
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!");
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");
215 public void CaseInsensitive ()
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");
227 public void CopyTo ()
229 string [] array = new string [4];
230 NameValueCollection c = new NameValueCollection ();
239 [ExpectedException (typeof (ArgumentNullException))]
240 public void CopyTo_Null ()
242 NameValueCollection c = new NameValueCollection ();
247 [ExpectedException (typeof (ArgumentOutOfRangeException))]
248 public void CopyTo_NegativeIndex ()
250 string [] array = new string [4];
251 NameValueCollection c = new NameValueCollection ();
256 c.CopyTo (array, -1);
260 [ExpectedException (typeof (ArgumentException))]
261 public void CopyTo_NotEnoughSpace ()
263 string [] array = new string [4];
264 NameValueCollection c = new NameValueCollection ();
273 // Note: not a RankException
274 [ExpectedException (typeof (ArgumentException))]
275 public void CopyTo_MultipleDimensionStringArray ()
277 string [,,] matrix = new string [2,3,4];
278 NameValueCollection c = new NameValueCollection ();
283 c.CopyTo (matrix, 0);
287 // Note: not a RankException
288 [ExpectedException (typeof (ArgumentException))]
289 public void CopyTo_MultipleDimensionArray ()
291 Array a = Array.CreateInstance (typeof (string), 1, 2, 3);
292 NameValueCollection c = new NameValueCollection ();
298 [ExpectedException (typeof (InvalidCastException))]
300 [ExpectedException (typeof (ArrayTypeMismatchException))]
302 public void CopyTo_WrongTypeArray ()
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());
312 public void Remove ()
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";
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");
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");
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");
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");
343 [ExpectedException (typeof (ArgumentNullException))]
345 [ExpectedException (typeof (NullReferenceException))]
347 public void Constructor_Null_NVC ()
349 NameValueCollection nvc = new NameValueCollection((NameValueCollection)null);
354 [ExpectedException (typeof (ArgumentNullException))]
356 [ExpectedException (typeof (NullReferenceException))]
358 public void Constructor_Capacity_Null_NVC ()
360 NameValueCollection nvc = new NameValueCollection(10, (NameValueCollection)null);
365 public void Constructor_IEqualityComparer ()
367 NameValueCollection coll = new NameValueCollection (new EqualityComparer ());
369 Assert.AreEqual (1, coll.Count, "#1");
373 public void Constructor_Int_IEqualityComparer ()
375 NameValueCollection coll = new NameValueCollection (5, new EqualityComparer ());
377 Assert.AreEqual (1, coll.Count, "#1");
381 [ExpectedException (typeof (ArgumentOutOfRangeException))]
382 public void Constructor_IntNegative_IEqualityComparer ()
384 new NameValueCollection (-1, new EqualityComparer ());
388 public void Constructor_IEqualityComparer_Null ()
\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
397 public void Constructor_NameValueCollection ()
\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