remove TARGET_JVM
[mono.git] / mcs / class / corlib / Test / System.Collections / HashtableTest.cs
1 // HashtableTest.cs - NUnit Test Cases for the System.Collections.Hashtable class\r
2 //\r
3 //\r
4 // (C) Ximian, Inc.  http://www.ximian.com\r
5 // \r
6 \r
7 \r
8 using System;\r
9 using System.Collections;\r
10 \r
11 using System.IO;\r
12 using System.Runtime.Serialization;\r
13 using System.Runtime.Serialization.Formatters;\r
14 using System.Runtime.Serialization.Formatters.Binary;\r
15 \r
16 using NUnit.Framework;\r
17 \r
18 \r
19 \r
20 namespace MonoTests.System.Collections {\r
21 \r
22 \r
23 /// <summary>Hashtable test.</summary>\r
24 [TestFixture]\r
25 public class HashtableTest : Assertion {\r
26 \r
27         [Test]\r
28         public void TestCtor1() {\r
29                 Hashtable h = new Hashtable();\r
30                 AssertNotNull("No hash table", h);\r
31         }\r
32 \r
33         [Test]\r
34         public void TestCtor2() {\r
35                 {\r
36                         bool errorThrown = false;\r
37                         try {\r
38                                 Hashtable h = new Hashtable((IDictionary) null);\r
39                         } catch (ArgumentNullException) {\r
40                                 errorThrown = true;\r
41                         }\r
42                         Assert("null hashtable error not thrown", \r
43                                errorThrown);\r
44                 }\r
45                 {\r
46                         string[] keys = {"this", "is", "a", "test"};\r
47                         char[] values = {'a', 'b', 'c', 'd'};\r
48                         Hashtable h1 = new Hashtable();\r
49                         for (int i = 0; i < keys.Length; i++) {\r
50                                 h1[keys[i]] = values[i];\r
51                         }\r
52                         Hashtable h2 = new Hashtable(h1);\r
53                         for (int i = 0; i < keys.Length; i++) {\r
54                                 AssertEquals("No match for key " + keys[i],\r
55                                              values[i], h2[keys[i]]);\r
56                         }\r
57                 }\r
58         }\r
59 \r
60         [Test]\r
61         [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
62         public void TestCtor3 ()\r
63         {\r
64                 Hashtable h = new Hashtable ();\r
65                 Hashtable hh = new Hashtable (h, Single.NaN);\r
66         }\r
67 \r
68         [Test]\r
69         [ExpectedException (typeof (ArgumentException))]\r
70         public void TestCtor4 ()\r
71         {\r
72                 Hashtable ht = new Hashtable (Int32.MaxValue, 0.1f, null, null);\r
73         }\r
74 \r
75         [Test]\r
76         public void TestCtor5 ()\r
77         {\r
78                 // tests if negative capacity throws exception\r
79                 try {\r
80                         Hashtable ht = new Hashtable (-10, 0.1f, null, null);\r
81                         Assert("must throw ArgumentOutOfRange exception, param: capacity", false);\r
82                 } catch (ArgumentOutOfRangeException e) {\r
83                         Assert("ParamName is not capacity", e.ParamName == "capacity");\r
84                 }\r
85 \r
86                 // tests if loadFactor out of range throws exception (low)\r
87                 try {\r
88                         Hashtable ht = new Hashtable (100, 0.01f, null, null);\r
89                         Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too low value", false);\r
90                 }       catch (ArgumentOutOfRangeException e) \r
91                 {\r
92                         Assert("ParamName is not loadFactor",e.ParamName == "loadFactor");\r
93                 }\r
94 \r
95                 // tests if loadFactor out of range throws exception (high)\r
96                 try \r
97                 {\r
98                         Hashtable ht = new Hashtable (100, 2f, null, null);\r
99                         Assert("must throw ArgumentOutOfRange exception, param: loadFactor, too high value", false);\r
100                 }       \r
101                 catch (ArgumentOutOfRangeException e) \r
102                 {\r
103                         Assert("ParamName is not loadFactor", e.ParamName == "loadFactor");\r
104                 }\r
105 \r
106         }\r
107 \r
108         // TODO - Ctors for capacity and load (how to test? any access?)\r
109         // TODO - Ctors with IComparer, IHashCodeProvider, Serialization\r
110 \r
111         [Test]  \r
112         public void TestCount() {\r
113                 Hashtable h = new Hashtable();\r
114                 AssertEquals("new table - count zero", 0, h.Count);\r
115                 int max = 100;\r
116                 for (int i = 1; i <= max; i++) {\r
117                         h[i] = i;\r
118                         AssertEquals("Count wrong for " + i,\r
119                                      i, h.Count);\r
120                 }\r
121                 for (int i = 1; i <= max; i++) {\r
122                         h[i] = i * 2;\r
123                         AssertEquals("Count shouldn't change at " + i,\r
124                                      max, h.Count);\r
125                 }\r
126         }\r
127 \r
128         [Test]        \r
129         public void TestIsFixedSize() {\r
130                 Hashtable h = new Hashtable();\r
131                 AssertEquals("hashtable not fixed by default",\r
132                              false, h.IsFixedSize);\r
133                 // TODO - any way to get a fixed-size hashtable?\r
134         }\r
135 \r
136         public void TestIsReadOnly() {\r
137                 Hashtable h = new Hashtable();\r
138                 AssertEquals("hashtable not read-only by default",\r
139                              false, h.IsReadOnly);\r
140                 // TODO - any way to get a read-only hashtable?\r
141         }\r
142 \r
143         [Test]        \r
144         public void TestIsSynchronized ()\r
145         {\r
146                 Hashtable h = new Hashtable ();\r
147                 Assert ("hashtable not synched by default", !h.IsSynchronized);\r
148 \r
149                 Hashtable h2 = Hashtable.Synchronized (h);\r
150                 Assert ("hashtable should by synched", h2.IsSynchronized);\r
151 \r
152                 Hashtable h3 = (Hashtable) h2.Clone ();\r
153                 Assert ("Cloned Hashtable should by synched", h3.IsSynchronized);\r
154         }\r
155 \r
156         [Test]\r
157         public void TestItem() {\r
158                 {\r
159                         bool errorThrown = false;\r
160                         try {\r
161                                 Hashtable h = new Hashtable();\r
162                                 Object o = h[null];\r
163                         } catch (ArgumentNullException e) {\r
164                                 errorThrown = true;\r
165                                 AssertEquals("ParamName is not \"key\"", "key", e.ParamName);\r
166                         }\r
167                         Assert("null hashtable error not thrown", \r
168                                errorThrown);\r
169                 }\r
170                 // TODO - if read-only and/or fixed-size is possible,\r
171                 //        test 'NotSupportedException' here\r
172 \r
173                 {\r
174                         Hashtable h = new Hashtable();\r
175                         int max = 100;\r
176                         for (int i = 1; i <= max; i++) {\r
177                                 h[i] = i;\r
178                                 AssertEquals("value wrong for " + i,\r
179                                              i, h[i]);\r
180                         }\r
181                 }\r
182         }\r
183 \r
184         [Test]\r
185         public void TestKeys() {\r
186                 string[] keys = {"this", "is", "a", "test"};\r
187                 string[] keys2 = {"new", "keys"};\r
188                 char[] values1 = {'a', 'b', 'c', 'd'};\r
189                 char[] values2 = {'e', 'f', 'g', 'h'};\r
190                 ICollection keysReference, keysReference2;\r
191                 Hashtable h1 = new Hashtable();\r
192                 for (int i = 0; i < keys.Length; i++) {\r
193                         h1[keys[i]] = values1[i];\r
194                 }\r
195                 AssertEquals("keys wrong size",\r
196                              keys.Length, h1.Keys.Count);\r
197                 for (int i = 0; i < keys.Length; i++) {\r
198                         h1[keys[i]] = values2[i];\r
199                 }\r
200                 AssertEquals("keys wrong size 2",\r
201                              keys.Length, h1.Keys.Count);\r
202 \r
203                 // MS .NET Always returns the same reference when calling Keys property\r
204                 keysReference = h1.Keys;\r
205             keysReference2 = h1.Keys;\r
206                 AssertEquals("keys references differ", keysReference, keysReference2);\r
207 \r
208                 for (int i = 0; i < keys2.Length; i++) \r
209                 {\r
210                         h1[keys2[i]] = values2[i];\r
211                 }\r
212                 AssertEquals("keys wrong size 3",\r
213                         keys.Length+keys2.Length, h1.Keys.Count);\r
214                 AssertEquals("keys wrong size 4",\r
215                         keys.Length+keys2.Length, keysReference.Count);\r
216         }\r
217 \r
218         // TODO - SyncRoot\r
219         [Test]        \r
220         public void TestValues() {\r
221                 string[] keys = {"this", "is", "a", "test"};\r
222                 char[] values1 = {'a', 'b', 'c', 'd'};\r
223                 char[] values2 = {'e', 'f', 'g', 'h'};\r
224                 Hashtable h1 = new Hashtable();\r
225                 for (int i = 0; i < keys.Length; i++) {\r
226                         h1[keys[i]] = values1[i];\r
227                 }\r
228                 AssertEquals("values wrong size",\r
229                              keys.Length, h1.Values.Count);\r
230                 for (int i = 0; i < keys.Length; i++) {\r
231                         h1[keys[i]] = values2[i];\r
232                 }\r
233                 AssertEquals("values wrong size 2",\r
234                              keys.Length, h1.Values.Count);\r
235 \r
236                 // MS .NET Always returns the same reference when calling Values property\r
237                 ICollection valuesReference1 = h1.Values;\r
238                 ICollection valuesReference2 = h1.Values;\r
239                 AssertEquals("values references differ", valuesReference1, valuesReference2);\r
240         }\r
241 \r
242         [Test]\r
243         public void TestAdd() {\r
244                 {\r
245                         bool errorThrown = false;\r
246                         try {\r
247                                 Hashtable h = new Hashtable();\r
248                                 h.Add(null, "huh?");\r
249                         } catch (ArgumentNullException e) {\r
250                                 errorThrown = true;\r
251                                 AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
252                         }\r
253                         Assert("null add error not thrown", \r
254                                errorThrown);\r
255                 }\r
256                 {\r
257                         bool errorThrown = false;\r
258                         try {\r
259                                 Hashtable h = new Hashtable();\r
260                                 h.Add('a', 1);\r
261                                 h.Add('a', 2);\r
262                         } catch (ArgumentException) {\r
263                                 errorThrown = true;\r
264                         }\r
265                         Assert("re-add error not thrown", \r
266                                errorThrown);\r
267                 }\r
268                 // TODO - hit NotSupportedException\r
269                 {\r
270                         Hashtable h = new Hashtable();\r
271                         int max = 100;\r
272                         for (int i = 1; i <= max; i++) {\r
273                                 h.Add(i, i);\r
274                                 AssertEquals("value wrong for " + i,\r
275                                              i, h[i]);\r
276                         }\r
277                 }\r
278         }\r
279 \r
280         [Test]\r
281         public void TestClear() {\r
282                 // TODO - hit NotSupportedException\r
283                 Hashtable h = new Hashtable();\r
284                 AssertEquals("new table - count zero", 0, h.Count);\r
285                 int max = 100;\r
286                 for (int i = 1; i <= max; i++) {\r
287                         h[i] = i;\r
288                 }\r
289                 Assert("table don't gots stuff", h.Count > 0);\r
290                 h.Clear();\r
291                 AssertEquals("Table should be cleared",\r
292                              0, h.Count);\r
293         }\r
294 \r
295         [Test]\r
296         public void TestClone() {\r
297                 {\r
298                         char[] c1 = {'a', 'b', 'c'};\r
299                         char[] c2 = {'d', 'e', 'f'};\r
300                         Hashtable h1 = new Hashtable();\r
301                         for (int i = 0; i < c1.Length; i++) {\r
302                                 h1[c1[i]] = c2[i];\r
303                         }\r
304                         Hashtable h2 = (Hashtable)h1.Clone();\r
305                         AssertNotNull("got no clone!", h2);\r
306                         AssertNotNull("clone's got nothing!", h2[c1[0]]);\r
307                         for (int i = 0; i < c1.Length; i++) {\r
308                                 AssertEquals("Hashtable match", \r
309                                              h1[c1[i]], h2[c1[i]]);\r
310                         }\r
311                 }\r
312                 {\r
313                         char[] c1 = {'a', 'b', 'c'};\r
314                         char[] c20 = {'1', '2'};\r
315                         char[] c21 = {'3', '4'};\r
316                         char[] c22 = {'5', '6'};\r
317                         char[][] c2 = {c20, c21, c22};\r
318                         Hashtable h1 = new Hashtable();\r
319                         for (int i = 0; i < c1.Length; i++) {\r
320                                 h1[c1[i]] = c2[i];\r
321                         }\r
322                         Hashtable h2 = (Hashtable)h1.Clone();\r
323                         AssertNotNull("got no clone!", h2);\r
324                         AssertNotNull("clone's got nothing!", h2[c1[0]]);\r
325                         for (int i = 0; i < c1.Length; i++) {\r
326                                 AssertEquals("Hashtable match", \r
327                                              h1[c1[i]], h2[c1[i]]);\r
328                         }\r
329 \r
330                         ((char[])h1[c1[0]])[0] = 'z';\r
331                         AssertEquals("shallow copy", h1[c1[0]], h2[c1[0]]);\r
332                 }\r
333         }\r
334 \r
335         [Test]\r
336         public void TestContains() {\r
337                 {\r
338                         bool errorThrown = false;\r
339                         try {\r
340                                 Hashtable h = new Hashtable();\r
341                                 bool result = h.Contains(null);\r
342                         } catch (ArgumentNullException e) {\r
343                                 errorThrown = true;\r
344                                 AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
345                         }\r
346                         Assert("null add error not thrown", \r
347                                errorThrown);\r
348                 }\r
349                 {\r
350                         Hashtable h = new Hashtable();\r
351                         for (int i = 0; i < 10000; i += 2) \r
352                         {\r
353                                 h[i] = i;\r
354                         }\r
355                         for (int i = 0; i < 10000; i += 2) \r
356                         {\r
357                                 Assert("hashtable must contain"+i.ToString(), h.Contains(i));\r
358                                 Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));\r
359                         }\r
360                 }\r
361         }\r
362 \r
363         [Test]\r
364         public void TestContainsKey() {\r
365         {\r
366                         bool errorThrown = false;\r
367                         try \r
368                         {\r
369                                 Hashtable h = new Hashtable();\r
370                                 bool result = h.Contains(null);\r
371                         } \r
372                         catch (ArgumentNullException e) \r
373                         {\r
374                                 errorThrown = true;\r
375                                 AssertEquals("ParamName is not 'key'", "key", e.ParamName);\r
376                         }\r
377                         Assert("null add error not thrown", \r
378                                 errorThrown);\r
379                 }\r
380                 {\r
381                         Hashtable h = new Hashtable();\r
382                         for (int i = 0; i < 1000; i += 2) \r
383                         {\r
384                                 h[i] = i;\r
385                         }\r
386                         for (int i = 0; i < 1000; i += 2) \r
387                         {\r
388                                 Assert("hashtable must contain"+i.ToString(), h.Contains(i));\r
389                                 Assert("hashtable does not contain "+((int)(i+1)).ToString(), !h.Contains(i+1));\r
390                         }\r
391                 }\r
392 \r
393         }\r
394 \r
395         [Test]        \r
396         public void TestContainsValue() {\r
397                 {\r
398                         Hashtable h = new Hashtable();\r
399                         h['a'] = "blue";\r
400                         Assert("blue? it's in there!", \r
401                                h.ContainsValue("blue"));\r
402                         Assert("green? no way!", \r
403                                !h.ContainsValue("green"));\r
404                         Assert("null? no way!", \r
405                                 !h.ContainsValue(null));\r
406                         h['b'] = null;\r
407                         Assert("null? it's in there!", \r
408                                 h.ContainsValue(null));\r
409 \r
410                 }\r
411         }\r
412 \r
413         [Test]\r
414 #if TARGET_JVM\r
415         [Category ("NotWorking")]\r
416 #endif\r
417         public void TestCopyTo() {\r
418                 {\r
419                         bool errorThrown = false;\r
420                         try {\r
421                                 Hashtable h = new Hashtable();\r
422                                 h.CopyTo(null, 0);\r
423                         } catch (ArgumentNullException e) {\r
424                                 errorThrown = true;\r
425                                 AssertEquals("ParamName is not \"array\"", "array", e.ParamName); \r
426                         }\r
427                         Assert("null hashtable error not thrown", \r
428                                errorThrown);\r
429                 }\r
430                 {\r
431                         bool errorThrown = false;\r
432                         try {\r
433                                 Hashtable h = new Hashtable();\r
434                                 Object[] o = new Object[1];\r
435                                 h.CopyTo(o, -1);\r
436                         } catch (ArgumentOutOfRangeException e) {\r
437                                 errorThrown = true;\r
438                                 AssertEquals("ParamName is not \"arrayIndex\"", "arrayIndex", e.ParamName);\r
439                         }\r
440                         Assert("out of range error not thrown", \r
441                                errorThrown);\r
442                 }\r
443                 {\r
444                         bool errorThrown = false;\r
445                         try {\r
446                                 Hashtable h = new Hashtable();\r
447                                 Object[,] o = new Object[1,1];\r
448                                 h.CopyTo(o, 1);\r
449                         } catch (ArgumentException) {\r
450                                 errorThrown = true;\r
451                         }\r
452                         Assert("multi-dim array error not thrown", \r
453                                errorThrown);\r
454                 }\r
455                 {\r
456                         bool errorThrown = false;\r
457                         try {\r
458                                 Hashtable h = new Hashtable();\r
459                                 h['a'] = 1; // no error if table is empty\r
460                                 Object[] o = new Object[5];\r
461                                 h.CopyTo(o, 5);\r
462                         } catch (ArgumentException) {\r
463                                 errorThrown = true;\r
464                         }\r
465                         Assert("no room in array error not thrown", \r
466                                errorThrown);\r
467                 }\r
468                 {\r
469                         bool errorThrown = false;\r
470                         try {\r
471                                 Hashtable h = new Hashtable();\r
472                                 h['a'] = 1;\r
473                                 h['b'] = 2;\r
474                                 h['c'] = 2;\r
475                                 Object[] o = new Object[2];\r
476                                 h.CopyTo(o, 0);\r
477                         } catch (ArgumentException) {\r
478                                 errorThrown = true;\r
479                         }\r
480                         Assert("table too big error not thrown", \r
481                                errorThrown);\r
482                 }\r
483                 {\r
484                         bool errorThrown = false;\r
485                         try {\r
486                                 Hashtable h = new Hashtable();\r
487                                 h["blue"] = 1;\r
488                                 h["green"] = 2;\r
489                                 h["red"] = 3;\r
490                                 Char[] o = new Char[3];\r
491                                 h.CopyTo(o, 0);\r
492                         } catch (InvalidCastException) {\r
493                                 errorThrown = true;\r
494                         }\r
495                         Assert("invalid cast error not thrown", \r
496                                errorThrown);\r
497                 }\r
498 \r
499                 {\r
500                         Hashtable h = new Hashtable();\r
501                         h['a'] = 1;\r
502                         h['b'] = 2;\r
503                         DictionaryEntry[] o = new DictionaryEntry[2];\r
504                         h.CopyTo(o,0);\r
505                         AssertEquals("first copy fine.", 'a', o[0].Key);\r
506                         AssertEquals("first copy fine.", 1, o[0].Value);\r
507                         AssertEquals("second copy fine.", 'b', o[1].Key);\r
508                         AssertEquals("second copy fine.", 2, o[1].Value);\r
509                 }\r
510         }\r
511 \r
512         [Test]        \r
513         public void TestGetEnumerator() {\r
514                 String[] s1 = {"this", "is", "a", "test"};\r
515                 Char[] c1 = {'a', 'b', 'c', 'd'};\r
516                 Hashtable h1 = new Hashtable();\r
517                 for (int i = 0; i < s1.Length; i++) {\r
518                         h1[s1[i]] = c1[i];\r
519                 }\r
520                 IDictionaryEnumerator en = h1.GetEnumerator();\r
521                 AssertNotNull("No enumerator", en);\r
522                 \r
523                 for (int i = 0; i < s1.Length; i++) {\r
524                         en.MoveNext();\r
525                         Assert("Not enumerating for " + en.Key, \r
526                                Array.IndexOf(s1, en.Key) >= 0);\r
527                         Assert("Not enumerating for " + en.Value, \r
528                                Array.IndexOf(c1, en.Value) >= 0);\r
529                 }\r
530         }\r
531 \r
532         [Test]\r
533         public void TestSerialization () {\r
534                 Hashtable table1 = new Hashtable();\r
535                 Hashtable table2;\r
536                 Stream str = new MemoryStream ();
537                 BinaryFormatter formatter = new BinaryFormatter();\r
538 \r
539                 for (int i = 0; i < 100; i++)\r
540                         table1[i] = "TestString Key: " + i.ToString();
541                 
542                 formatter.Serialize (str, table1);
543                 str.Position = 0;
544                 table2 = (Hashtable) formatter.Deserialize (str);
545                 \r
546                 bool result;\r
547                 foreach (DictionaryEntry de in table1)
548                         AssertEquals (de.Value, table2 [de.Key]);\r
549         }\r
550         \r
551         [Test]\r
552 #if TARGET_JVM\r
553         [Category ("NotWorking")]\r
554 #endif\r
555         public void TestSerialization2 () {\r
556                 // Test from bug #70570\r
557                 MemoryStream stream = new MemoryStream();\r
558                 BinaryFormatter formatter = new BinaryFormatter();\r
559         \r
560                 Hashtable table = new Hashtable();\r
561                 table.Add (new Bug(), "Hello");\r
562 \r
563                 formatter.Serialize(stream, table);\r
564                 stream.Position = 0;\r
565                 table = (Hashtable) formatter.Deserialize(stream);\r
566                 AssertEquals ("#1", 1, table.Count);\r
567         }\r
568 \r
569         [Test]        \r
570         public void TestRemove() {\r
571                 {\r
572                         bool errorThrown = false;\r
573                         try {\r
574                                 Hashtable h = new Hashtable();\r
575                                 h.Remove(null);\r
576                         } catch (ArgumentNullException e) {\r
577                                 errorThrown = true;\r
578                                 AssertEquals("ParamName is not \"key\"", "key", e.ParamName);\r
579                         }\r
580                         Assert("null hashtable error not thrown", \r
581                                errorThrown);\r
582                 }\r
583                 {\r
584                         string[] keys = {"this", "is", "a", "test"};\r
585                         char[] values = {'a', 'b', 'c', 'd'};\r
586                         Hashtable h = new Hashtable();\r
587                         for (int i = 0; i < keys.Length; i++) {\r
588                                 h[keys[i]] = values[i];\r
589                         }\r
590                         AssertEquals("not enough in table",\r
591                                      4, h.Count);\r
592                         h.Remove("huh?");\r
593                         AssertEquals("not enough in table",\r
594                                      4, h.Count);\r
595                         h.Remove("this");\r
596                         AssertEquals("Wrong count in table",\r
597                                      3, h.Count);\r
598                         h.Remove("this");\r
599                         AssertEquals("Wrong count in table",\r
600                                      3, h.Count);\r
601                 }\r
602         }\r
603 \r
604         [Test]        \r
605         public void TestSynchronized() {\r
606                 {\r
607                         bool errorThrown = false;\r
608                         try {\r
609                                 Hashtable h = Hashtable.Synchronized(null);\r
610                         } catch (ArgumentNullException e) {\r
611                                 errorThrown = true;\r
612                                 AssertEquals("ParamName is not \"table\"", "table", e.ParamName);\r
613                         }\r
614                         Assert("null hashtable error not thrown", \r
615                                errorThrown);\r
616                 }\r
617                 {\r
618                         Hashtable h = new Hashtable();\r
619                         Assert("hashtable not synced by default", \r
620                                !h.IsSynchronized);\r
621                         Hashtable h2 = Hashtable.Synchronized(h);\r
622                         Assert("hashtable should by synced", \r
623                                h2.IsSynchronized);\r
624                 }\r
625         }\r
626         \r
627         \r
628         protected Hashtable ht;\r
629         private static Random rnd;\r
630         \r
631         [SetUp]\r
632         public void SetUp() {\r
633                 ht=new Hashtable();\r
634                 rnd=new Random();\r
635         }\r
636         \r
637         private void SetDefaultData() {\r
638                 ht.Clear();\r
639                 ht.Add("k1","another");\r
640                 ht.Add("k2","yet");\r
641                 ht.Add("k3","hashtable");\r
642         }\r
643         \r
644         [Test]\r
645         public void TestAddRemoveClear() {\r
646                 ht.Clear();\r
647                 Assert(ht.Count==0);\r
648                 \r
649                 SetDefaultData();\r
650                 Assert(ht.Count==3);\r
651                 \r
652                 bool thrown=false;\r
653                 try {\r
654                         ht.Add("k2","cool");\r
655                 } catch (ArgumentException) {thrown=true;}\r
656                 Assert("Must throw ArgumentException!",thrown);\r
657                 \r
658                 ht["k2"]="cool";\r
659                 Assert(ht.Count==3);\r
660                 Assert(ht["k2"].Equals("cool"));\r
661                 \r
662         }\r
663 \r
664         [Test]  \r
665         public void TestCopyTo2() {\r
666                 SetDefaultData();\r
667                 Object[] entries=new Object[ht.Count];\r
668                 ht.CopyTo(entries,0);\r
669                 Assert("Not an entry.",entries[0] is DictionaryEntry);\r
670         }\r
671 \r
672         [Test]\r
673         public void CopyTo_Empty ()\r
674         {\r
675                 Hashtable ht = new Hashtable ();\r
676                 AssertEquals ("Count", 0, ht.Count);\r
677                 object[] array = new object [ht.Count];\r
678                 ht.CopyTo (array, 0);\r
679         }\r
680         \r
681         [Test]  \r
682         public void TestUnderHeavyLoad() {\r
683                 ht.Clear();\r
684                 int max=100000;\r
685                 String[] cache=new String[max*2];\r
686                 int n=0;\r
687                 \r
688                 for (int i=0;i<max;i++) {\r
689                         int id=rnd.Next()&0xFFFF;\r
690                         String key=""+id+"-key-"+id;\r
691                         String val="value-"+id;\r
692                         if (ht[key]==null) {\r
693                                 ht[key]=val;\r
694                                 cache[n]=key;\r
695                                 cache[n+max]=val;\r
696                                 n++;\r
697                         }\r
698                 }\r
699                 \r
700                 Assert(ht.Count==n);\r
701                 \r
702                 for (int i=0;i<n;i++) {\r
703                         String key=cache[i];\r
704                         String val=ht[key] as String;\r
705                         String err="ht[\""+key+"\"]=\""+val+\r
706                                 "\", expected \""+cache[i+max]+"\"";\r
707                         Assert(err,val!=null && val.Equals(cache[i+max]));\r
708                 }\r
709                 \r
710                 int r1=(n/3);\r
711                 int r2=r1+(n/5);\r
712                 \r
713                 for (int i=r1;i<r2;i++) {\r
714                         ht.Remove(cache[i]);\r
715                 }\r
716                 \r
717                 \r
718                 for (int i=0;i<n;i++) {\r
719                         if (i>=r1 && i<r2) {\r
720                                 Assert(ht[cache[i]]==null);\r
721                         } else {\r
722                                 String key=cache[i];\r
723                                 String val=ht[key] as String;\r
724                                 String err="ht[\""+key+"\"]=\""+val+\r
725                                         "\", expected \""+cache[i+max]+"\"";\r
726                                 Assert(err,val!=null && val.Equals(cache[i+max]));\r
727                         }\r
728                 }\r
729                 \r
730                 ICollection keys=ht.Keys;\r
731                 int nKeys=0;\r
732                 foreach (Object key in keys) {\r
733                         Assert((key as String) != null);\r
734                         nKeys++;\r
735                 }\r
736                 Assert(nKeys==ht.Count);\r
737 \r
738                 \r
739                 ICollection vals=ht.Values;\r
740                 int nVals=0;\r
741                 foreach (Object val in vals) {\r
742                         Assert((val as String) != null);\r
743                         nVals++;\r
744                 }\r
745                 Assert(nVals==ht.Count);\r
746                 \r
747         }\r
748 \r
749         \r
750         /// <summary>\r
751         ///  Test hashtable with CaseInsensitiveHashCodeProvider\r
752         ///  and CaseInsensitive comparer.\r
753         /// </summary>\r
754         [Test]        \r
755         public void TestCaseInsensitive ()\r
756         {\r
757                 // Not very meaningfull test, just to make\r
758                 // sure that hcp is set properly set.\r
759                 Hashtable ciHashtable = new Hashtable(11,1.0f,CaseInsensitiveHashCodeProvider.Default,CaseInsensitiveComparer.Default);\r
760                 ciHashtable ["key1"] = "value";\r
761                 ciHashtable ["key2"] = "VALUE";\r
762                 Assert(ciHashtable ["key1"].Equals ("value"));\r
763                 Assert(ciHashtable ["key2"].Equals ("VALUE"));\r
764 \r
765                 ciHashtable ["KEY1"] = "new_value";\r
766                 Assert(ciHashtable ["key1"].Equals ("new_value"));\r
767 \r
768         }\r
769 \r
770         [Test]\r
771         public void TestCopyConstructor ()\r
772         {\r
773                 SetDefaultData ();\r
774 \r
775                 Hashtable htCopy = new Hashtable (ht);\r
776 \r
777                 Assert(ht.Count == htCopy.Count);\r
778         }\r
779 \r
780         [Test]\r
781         public void TestEnumerator ()\r
782         {\r
783                 SetDefaultData ();\r
784 \r
785                 IEnumerator e = ht.GetEnumerator ();\r
786 \r
787                 while (e.MoveNext ()) {}\r
788 \r
789                 Assert (!e.MoveNext ());\r
790 \r
791         }\r
792 \r
793         [Test]\r
794         [ExpectedException (typeof (ArgumentNullException))]\r
795         public void GetObjectData_NullSerializationInfo () \r
796         {\r
797                 SetDefaultData ();\r
798                 ht.GetObjectData (null, new StreamingContext ());\r
799         }\r
800 \r
801         // bug #75790\r
802         [Test]\r
803         [Category ("NotDotNet")] // .NET raises InvalidOperationException.\r
804         public void SyncHashtable_ICollectionsGetEnumerator ()\r
805         {\r
806                 Hashtable hashtable = Hashtable.Synchronized (new Hashtable ());\r
807                 hashtable["a"] = 1;\r
808                 //IEnumerator e = (hashtable.Clone() as\r
809                 IEnumerator e = (hashtable as ICollection).GetEnumerator ();\r
810                 //e.Reset();\r
811                 e.MoveNext ();\r
812                 DictionaryEntry de = (DictionaryEntry) e.Current;\r
813         }\r
814 \r
815         [Test]\r
816         public void SerializableSubClasses ()\r
817         {\r
818                 Hashtable ht = new Hashtable ();\r
819                 // see bug #76300\r
820                 Assert ("Keys.IsSerializable", ht.Keys.GetType ().IsSerializable);\r
821                 Assert ("Values.IsSerializable", ht.Values.GetType ().IsSerializable);\r
822                 Assert ("GetEnumerator.IsSerializable", ht.GetEnumerator ().GetType ().IsSerializable);\r
823                 Assert ("Synchronized.IsSerializable", Hashtable.Synchronized (ht).GetType ().IsSerializable);\r
824         }\r
825 }\r
826 \r
827 [Serializable]\r
828 public class Bug :ISerializable {\r
829 \r
830         [Serializable]\r
831         private sealed class InnerClassSerializationHelper : IObjectReference {\r
832                 public object GetRealObject( StreamingContext context )\r
833                 {\r
834                         return new Bug();\r
835                 }\r
836         };\r
837 \r
838         void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context )\r
839         {\r
840                 info.SetType( typeof(InnerClassSerializationHelper) );\r
841         }\r
842 };\r
843 \r
844 \r
845 }\r