2008-09-24 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / Test / System.Collections.Generic / DictionaryTest.cs
1 //
2 // MonoTests.System.Collections.Generic.DictionaryTest
3 //
4 // Authors:
5 //      Sureshkumar T (tsureshkumar@novell.com)
6 //      Ankit Jain (radical@corewars.org)
7 //      David Waite (mass@akuma.org)
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2005 David Waite (mass@akuma.org)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33
34 using System;
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.IO;
38 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Text;
40 using System.Threading;
41
42 using NUnit.Framework;
43
44 namespace MonoTests.System.Collections.Generic {
45         [TestFixture]
46         public class DictionaryTest {
47                 class MyClass {
48                         int a;
49                         int b;
50                         public MyClass (int a, int b)
51                         {
52                                 this.a = a;
53                                 this.b = b;
54                         }
55                         public override int GetHashCode ()
56                         {
57                                 return a + b;
58                         }
59         
60                         public override bool Equals (object obj)
61                         {
62                                 if (!(obj is MyClass))
63                                         return false;
64                                 return ((MyClass)obj).Value == a;
65                         }
66         
67         
68                         public int Value {
69                                 get { return a; }
70                         }
71         
72                 }
73         
74                 Dictionary <string, object> _dictionary = null;
75                 Dictionary <MyClass, MyClass> _dictionary2 = null;
76                 Dictionary <int, int> _dictionary3 = null;
77         
78                 [SetUp]
79                 public void SetUp ()
80                 {
81                         _dictionary = new Dictionary <string, object> ();
82                         _dictionary2 = new Dictionary <MyClass, MyClass> ();
83                         _dictionary3 = new Dictionary <int, int>();
84                 }
85         
86                 [Test]
87                 public void AddTest ()
88                 {
89                         _dictionary.Add ("key1", "value");
90                         Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
91                 }
92         
93                 [Test]
94                 public void AddTest2 ()
95                 {
96                         MyClass m1 = new MyClass (10,5);
97                         MyClass m2 = new MyClass (20,5);
98                         MyClass m3 = new MyClass (12,3);
99                         _dictionary2.Add (m1,m1);
100                         _dictionary2.Add (m2, m2);
101                         _dictionary2.Add (m3, m3);
102                         Assert.AreEqual (20, _dictionary2 [m2].Value, "#1");
103                         Assert.AreEqual (10, _dictionary2 [m1].Value, "#2");
104                         Assert.AreEqual (12, _dictionary2 [m3].Value, "#3");
105                 }
106
107                 [Test]
108                 public void AddTest3 ()
109                 {
110                         _dictionary3.Add (1, 2);
111                         _dictionary3.Add (2, 3);
112                         _dictionary3.Add (3, 4);
113                         Assert.AreEqual (2, _dictionary3[1], "#1");
114                         Assert.AreEqual (3, _dictionary3[2], "#2");
115                         Assert.AreEqual (4, _dictionary3[3], "#3");
116                 }
117
118                 [Test, ExpectedException(typeof(ArgumentNullException))]
119                 public void AddNullTest ()
120                 {
121                         _dictionary.Add (null, "");
122                 }
123         
124                 [Test, ExpectedException(typeof(ArgumentException))]
125                 public void AddDuplicateTest ()
126                 {
127                         _dictionary.Add("foo", "bar");
128                         _dictionary.Add("foo", "bar");
129                 }
130
131                 //Tests Add when resize takes place
132                 [Test]
133                 public void AddLargeTest ()
134                 {
135                         int i, numElems = 50;
136         
137                         for (i = 0; i < numElems; i++)
138                         {
139                                 _dictionary3.Add (i, i);
140                         }
141         
142                         i = 0;
143                         foreach (KeyValuePair <int, int> entry in _dictionary3)
144                         {
145                                 i++;
146                         }
147         
148                         Assert.AreEqual (i, numElems, "Add with resize failed!");
149                 }
150         
151                 [Test]
152                 public void IndexerGetExistingTest ()
153                 {
154                         _dictionary.Add ("key1", "value");
155                         Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
156                 }
157                 
158                 [Test, ExpectedException(typeof(KeyNotFoundException))]
159                 public void IndexerGetNonExistingTest ()
160                 {
161                         object foo = _dictionary ["foo"];
162                 }
163
164                 [Test, ExpectedException(typeof(ArgumentNullException))]
165                 public void IndexerGetNullTest()
166                 {
167                         object s = _dictionary[null];
168                 }
169
170                 [Test]
171                 public void IndexerSetExistingTest ()
172                 {
173                         _dictionary.Add ("key1", "value1");
174                         _dictionary ["key1"] =  "value2";
175                         Assert.AreEqual (1, _dictionary.Count);
176                         Assert.AreEqual ("value2", _dictionary ["key1"]);
177                 }
178
179                 [Test]
180                 public void IndexerSetNonExistingTest ()
181                 {
182                         _dictionary ["key1"] =  "value1";
183                         Assert.AreEqual (1, _dictionary.Count);
184                         Assert.AreEqual ("value1", _dictionary ["key1"]);
185                 }
186         
187                 [Test]
188                 public void RemoveTest ()
189                 {
190                         _dictionary.Add ("key1", "value1");
191                         _dictionary.Add ("key2", "value2");
192                         _dictionary.Add ("key3", "value3");
193                         _dictionary.Add ("key4", "value4");
194                         Assert.IsTrue (_dictionary.Remove ("key3"));
195                         Assert.IsFalse (_dictionary.Remove ("foo"));
196                         Assert.AreEqual (3, _dictionary.Count);
197                         Assert.IsFalse (_dictionary.ContainsKey ("key3"));
198                 }
199         
200                 [Test]
201                 public void RemoveTest2 ()
202                 {
203                         MyClass m1 = new MyClass (10, 5);
204                         MyClass m2 = new MyClass (20, 5);
205                         MyClass m3 = new MyClass (12, 3);
206                         _dictionary2.Add (m1, m1);
207                         _dictionary2.Add (m2, m2);
208                         _dictionary2.Add (m3, m3);
209                         _dictionary2.Remove (m1); // m2 is in rehash path
210                         Assert.AreEqual (20, _dictionary2 [m2].Value, "#4");
211                         
212                 }
213
214                 [Test]
215                 [Category ("NotWorking")]
216                 public void Remove_ZeroOut ()
217                 {
218                         object key = new object ();
219                         object value = new object ();
220
221                         WeakReference wrKey = new WeakReference (key);
222                         WeakReference wrValue = new WeakReference (value);
223
224                         Dictionary <object, object> dictionary = new Dictionary <object, object> ();
225                         dictionary.Add (key, value);
226                         dictionary.Remove (key);
227
228                         key = null;
229                         value = null;
230                         GC.Collect ();
231                         Thread.Sleep (200);
232
233                         Assert.IsNull (wrKey.Target, "#1");
234                         Assert.IsNull (wrValue.Target, "#2");
235                 }
236         
237                 [Test, ExpectedException(typeof(ArgumentNullException))]
238                 public void IndexerSetNullTest()
239                 {
240                         _dictionary[null] = "bar";
241                 }
242         
243                 [Test]
244                 public void ClearTest ()
245                 {
246                         _dictionary.Add ("key1", "value1");
247                         _dictionary.Add ("key2", "value2");
248                         _dictionary.Add ("key3", "value3");
249                         _dictionary.Add ("key4", "value4");
250                         _dictionary.Clear ();
251                         Assert.AreEqual (0, _dictionary.Count, "Clear method failed!");
252                         Assert.IsFalse (_dictionary.ContainsKey ("key2"));
253                 }
254
255                 [Test]
256                 [Category ("NotWorking")]
257                 public void Clear_ZeroOut ()
258                 {
259                         object key = new object ();
260                         object value = new object ();
261
262                         WeakReference wrKey = new WeakReference (key);
263                         WeakReference wrValue = new WeakReference (value);
264
265                         Dictionary <object, object> dictionary = new Dictionary <object, object> ();
266                         dictionary.Add (key, value);
267                         dictionary.Clear ();
268
269                         key = null;
270                         value = null;
271                         GC.Collect ();
272                         Thread.Sleep (200);
273
274                         Assert.IsNull (wrKey.Target, "#1");
275                         Assert.IsNull (wrValue.Target, "#2");
276                 }
277
278                 [Test]
279                 public void ContainsKeyTest ()
280                 {
281                         _dictionary.Add ("key1", "value1");
282                         _dictionary.Add ("key2", "value2");
283                         _dictionary.Add ("key3", "value3");
284                         _dictionary.Add ("key4", "value4");
285                         bool contains = _dictionary.ContainsKey ("key4");
286                         Assert.IsTrue (contains, "ContainsKey does not return correct value!");
287                         contains = _dictionary.ContainsKey ("key5");
288                         Assert.IsFalse (contains, "ContainsKey for non existant does not return correct value!");
289                 }
290         
291                 [Test]
292                 public void ContainsValueTest ()
293                 {
294                         _dictionary.Add ("key1", "value1");
295                         _dictionary.Add ("key2", "value2");
296                         _dictionary.Add ("key3", "value3");
297                         _dictionary.Add ("key4", "value4");
298                         bool contains = _dictionary.ContainsValue ("value2");
299                         Assert.IsTrue(contains, "ContainsValue does not return correct value!");
300                         contains = _dictionary.ContainsValue ("@@daisofja@@");
301                         Assert.IsFalse (contains, "ContainsValue for non existant does not return correct value!");
302                 }
303         
304                 [Test]
305                 public void TryGetValueTest()
306                 {
307                         _dictionary.Add ("key1", "value1");
308                         _dictionary.Add ("key2", "value2");
309                         _dictionary.Add ("key3", "value3");
310                         _dictionary.Add ("key4", "value4");
311                         object value = "";
312                         bool retrieved = _dictionary.TryGetValue ("key4", out value);
313                         Assert.IsTrue (retrieved);
314                         Assert.AreEqual ("value4", (string)value, "TryGetValue does not return value!");
315         
316                         retrieved = _dictionary.TryGetValue ("key7", out value);
317                         Assert.IsFalse (retrieved);
318                         Assert.IsNull (value, "value for non existant value should be null!");
319                 }
320         
321                 [Test]
322                 public void ValueTypeTest ()
323                 {
324                         Dictionary <int, float> dict = new Dictionary <int, float> ();
325                         dict.Add (10, 10.3f);
326                         dict.Add (11, 10.4f);
327                         dict.Add (12, 10.5f);
328                         Assert.AreEqual (10.4f, dict [11], "#5");
329                 }
330         
331                 private class MyTest
332                 {
333                         public string Name;
334                         public int RollNo;
335         
336                         public MyTest (string name, int number)
337                         {
338                                 Name = name;
339                                 RollNo = number;
340                         }
341
342                         public override int GetHashCode ()
343                         {
344                                 return Name.GetHashCode () ^ RollNo;
345                         }
346
347                         public override bool Equals (object obj)
348                         {
349                                 MyTest myt = obj as MyTest;
350                                 return myt.Name.Equals (this.Name) &&
351                                                 myt.RollNo.Equals (this.RollNo);
352                         }
353                 }
354         
355                 [Test]
356                 public void ObjectAsKeyTest ()
357                 {
358                         Dictionary <object, object> dict = new Dictionary <object, object> ();
359                         MyTest key1, key2, key3;
360                         dict.Add ( (key1 = new MyTest ("key1", 234)), "value1");
361                         dict.Add ( (key2 = new MyTest ("key2", 444)), "value2");
362                         dict.Add ( (key3 = new MyTest ("key3", 5655)), "value3");
363         
364                         Assert.AreEqual ("value2", dict [key2], "value is not returned!");
365                         Assert.AreEqual ("value3", dict [key3], "neg: exception should not be thrown!");
366                 }
367         
368                 [Test, ExpectedException (typeof (ArgumentException))]
369                 public void IDictionaryAddTest ()
370                 {
371                         IDictionary iDict = _dictionary as IDictionary;
372                         iDict.Add ("key1", "value1");
373                         iDict.Add ("key2", "value3");
374                         Assert.AreEqual (2, iDict.Count, "IDictioanry interface add is not working!");
375         
376                         //Negative test case
377                         iDict.Add (12, "value");
378                         iDict.Add ("key", 34);
379                 }
380         
381                 [Test]
382                 public void IEnumeratorTest ()
383                 {
384                         _dictionary.Add ("key1", "value1");
385                         _dictionary.Add ("key2", "value2");
386                         _dictionary.Add ("key3", "value3");
387                         _dictionary.Add ("key4", "value4");
388                         IEnumerator itr = ((IEnumerable)_dictionary).GetEnumerator ();
389                         while (itr.MoveNext ()) {
390                                 object o = itr.Current;
391                                 Assert.AreEqual (typeof (KeyValuePair<string,object>), o.GetType (), "Current should return a type of KeyValuePair");
392                                 KeyValuePair<string,object> entry = (KeyValuePair<string,object>) itr.Current;
393                         }
394                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
395                 }
396         
397         
398                 [Test]
399                 public void IEnumeratorGenericTest ()
400                 {
401                         _dictionary.Add ("key1", "value1");
402                         _dictionary.Add ("key2", "value2");
403                         _dictionary.Add ("key3", "value3");
404                         _dictionary.Add ("key4", "value4");
405                         IEnumerator <KeyValuePair <string, object>> itr = ((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
406                         while (itr.MoveNext ()) {
407                                 object o = itr.Current;
408                                 Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of KeyValuePair<object,string>");
409                                 KeyValuePair <string, object> entry = (KeyValuePair <string, object>)itr.Current;
410                         }
411                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
412                 }
413         
414                 [Test]
415                 public void IDictionaryEnumeratorTest ()
416                 {
417                         _dictionary.Add ("key1", "value1");
418                         _dictionary.Add ("key2", "value2");
419                         _dictionary.Add ("key3", "value3");
420                         _dictionary.Add ("key4", "value4");
421                         IDictionaryEnumerator itr = ((IDictionary)_dictionary).GetEnumerator ();
422                         while (itr.MoveNext ()) {
423                                 object o = itr.Current;
424                                 Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
425                                 DictionaryEntry entry = (DictionaryEntry) itr.Current;
426                         }
427                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
428                 }
429         
430                 [Test]
431                 public void ForEachTest ()
432                 {
433                         _dictionary.Add ("key1", "value1");
434                         _dictionary.Add ("key2", "value2");
435                         _dictionary.Add ("key3", "value3");
436                         _dictionary.Add ("key4", "value4");
437         
438                         int i = 0;
439                         foreach (KeyValuePair <string, object> entry in _dictionary)
440                                 i++;
441                         Assert.AreEqual(4, i, "fail1: foreach entry failed!");
442         
443                         i = 0;
444                         foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
445                                 i++;
446                         Assert.AreEqual(4, i, "fail2: foreach entry failed!");
447         
448                         i = 0;
449                         foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
450                                 i++;
451                         Assert.AreEqual (4, i, "fail3: foreach entry failed!");
452                 }
453         
454                 [Test]
455                 public void ResizeTest ()
456                 {
457                         Dictionary <string, object> dictionary = new Dictionary <string, object> (3);
458                         dictionary.Add ("key1", "value1");
459                         dictionary.Add ("key2", "value2");
460                         dictionary.Add ("key3", "value3");
461         
462                         Assert.AreEqual (3, dictionary.Count);
463         
464                         dictionary.Add ("key4", "value4");
465                         Assert.AreEqual (4, dictionary.Count);
466                         Assert.AreEqual ("value1", dictionary ["key1"].ToString (), "");
467                         Assert.AreEqual ("value2", dictionary ["key2"].ToString (), "");
468                         Assert.AreEqual ("value4", dictionary ["key4"].ToString (), "");
469                         Assert.AreEqual ("value3", dictionary ["key3"].ToString (), "");
470                 }
471         
472                 [Test]
473                 public void KeyCollectionTest ()
474                 {
475                         _dictionary.Add ("key1", "value1");
476                         _dictionary.Add ("key2", "value2");
477                         _dictionary.Add ("key3", "value3");
478                         _dictionary.Add ("key4", "value4");
479         
480                         ICollection <string> keys = ((IDictionary <string, object>)_dictionary).Keys;
481                         Assert.AreEqual (4, keys.Count);
482                         int i = 0;
483                         foreach (string key in keys)
484                         {
485                                 i++;
486                         }
487                         Assert.AreEqual(4, i);
488                 }
489
490                 [Test]
491                 public void KeyValueEnumeratorTest ()
492                 {
493                         IDictionary<int, int> d = new Dictionary<int, int>();
494
495                         // Values are chosen such that two keys map to the same bucket.
496                         // Default dictionary table size == 10
497                         d [9] = 1;
498                         d [10] = 2;
499                         d [19] = 3;
500
501                         Assert.AreEqual (d.Count, d.Keys.Count, "d and d.Keys don't appear to match");
502                         Assert.AreEqual (d.Values.Count, d.Keys.Count, "d.Keys and d.Values don't appear to match");
503
504                         int count = 0;
505                         foreach (int i in d.Values)
506                                 ++count;
507                         Assert.AreEqual (count, d.Values.Count, "d.Values doesn't have the correct number of elements");
508         
509                         count = 0;
510                         foreach (int i in d.Keys)
511                                 ++count;
512                         Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't have the correct number of elements");
513
514                         int nkeys = count;
515                         count = 0;
516                         foreach (int i in d.Keys) {
517                                 int foo = d [i];
518                                 if (count++ >= nkeys)
519                                         Assert.Fail ("Reading a value appears to trash enumerator state");
520                         }
521                 }
522
523                 [Test] // bug 75073
524                 public void SliceCollectionsEnumeratorTest ()
525                 {
526                         Dictionary<string, int> values = new Dictionary<string, int> ();
527
528                         IEnumerator <string> ke = values.Keys.GetEnumerator ();
529                         IEnumerator <int>    ve = values.Values.GetEnumerator ();
530
531                         Assert.IsTrue (ke is Dictionary<string, int>.KeyCollection.Enumerator);
532                         Assert.IsTrue (ve is Dictionary<string, int>.ValueCollection.Enumerator);
533                 }
534
535                 [Test]
536                 public void PlainEnumeratorReturnTest ()
537                 {
538                         // Test that we return a KeyValuePair even for non-generic dictionary iteration
539                         _dictionary["foo"] = "bar";
540                         IEnumerator<KeyValuePair<string, object>> enumerator = _dictionary.GetEnumerator();
541                         Assert.IsTrue(enumerator.MoveNext(), "#1");
542                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IEnumerator)enumerator).Current.GetType (), "#2");
543                         Assert.AreEqual (typeof (DictionaryEntry), ((IDictionaryEnumerator)enumerator).Entry.GetType (), "#3");
544                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IDictionaryEnumerator)enumerator).Current.GetType (), "#4");
545                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((object) enumerator.Current).GetType (), "#5");
546                 }
547
548                 [Test, ExpectedException (typeof (InvalidOperationException))]
549                 public void FailFastTest1 ()
550                 {
551                         Dictionary<int, int> d = new Dictionary<int, int> ();
552                         d [1] = 1;
553                         int count = 0;
554                         foreach (KeyValuePair<int, int> kv in d) {
555                                 d [kv.Key + 1] = kv.Value + 1;
556                                 if (count++ != 0)
557                                         Assert.Fail ("Should not be reached");
558                         }
559                         Assert.Fail ("Should not be reached");
560                 }
561
562                 [Test, ExpectedException (typeof (InvalidOperationException))]
563                 public void FailFastTest2 ()
564                 {
565                         Dictionary<int, int> d = new Dictionary<int, int> ();
566                         d [1] = 1;
567                         int count = 0;
568                         foreach (int i in d.Keys) {
569                                 d [i + 1] = i + 1;
570                                 if (count++ != 0)
571                                         Assert.Fail ("Should not be reached");
572                         }
573                         Assert.Fail ("Should not be reached");
574                 }
575
576                 [Test, ExpectedException (typeof (InvalidOperationException))]
577                 public void FailFastTest3 ()
578                 {
579                         Dictionary<int, int> d = new Dictionary<int, int> ();
580                         d [1] = 1;
581                         int count = 0;
582                         foreach (int i in d.Keys) {
583                                 d [i] = i;
584                                 if (count++ != 0)
585                                         Assert.Fail ("Should not be reached");
586                         }
587                         Assert.Fail ("Should not be reached");
588                 }
589
590                 [Test]
591                 [Category ("TargetJvmNotWorking")] // BUGBUG Very very slow on TARGET_JVM.
592                 public void SerializationTest()
593                 {
594                         for (int i = 0; i < 50; i++)
595                         {
596                                 _dictionary3.Add(i, i);
597                         }
598
599                         BinaryFormatter formatter = new BinaryFormatter();
600                         MemoryStream stream = new MemoryStream();
601                         formatter.Serialize(stream, _dictionary3);
602
603                         stream.Position = 0;
604                         object deserialized = formatter.Deserialize(stream);
605
606                         Assert.IsNotNull(deserialized);
607                         Assert.IsFalse(deserialized == _dictionary3);
608
609                         Assert.IsTrue(deserialized is Dictionary<int, int>);
610                         Dictionary<int, int> d3 = deserialized as Dictionary<int, int>;
611
612                         Assert.AreEqual(50, d3.Count);
613                         for (int i = 0; i < 50; i++)
614                         {
615                                 Assert.AreEqual(i, d3[i]);
616                         }
617                 }
618
619                 [Test]
620                 public void ZeroCapacity ()
621                 {
622                         Dictionary<int, int> x = new Dictionary <int, int> (0);
623                         x.Add (1, 2);
624                         
625                         x = new Dictionary <int, int> (0);
626                         x.Clear ();
627
628                         x = new Dictionary <int, int> (0);
629                         int aa = x.Count;
630                         
631                         x = new Dictionary <int, int> (0);
632                         try {
633                                 int j = x [1];
634                         } catch (KeyNotFoundException){
635                         }
636
637                         bool b;
638                         b = x.ContainsKey (10);
639                         b = x.ContainsValue (10);
640
641                         x = new Dictionary <int, int> (0);
642                         x.Remove (10);
643                         
644                         x = new Dictionary <int, int> (0);
645                         int intv;
646                         x.TryGetValue (1, out intv);
647
648                         object oa = x.Keys;
649                         object ob = x.Values;
650                         foreach (KeyValuePair<int,int> a in x){
651                         }
652                 }
653
654                 [Test]
655                 public void Empty_KeysValues_CopyTo ()
656                 {
657                         Dictionary<int, int> d = new Dictionary<int, int> ();
658                         int[] array = new int[1];
659                         d.Keys.CopyTo (array, array.Length);
660                         d.Values.CopyTo (array, array.Length);
661                 }
662
663                 [Test]
664                 public void Empty_CopyTo ()
665                 {
666                         Dictionary<int, int> d = new Dictionary<int, int> ();
667                         ICollection c = (ICollection) d;
668                         DictionaryEntry [] array = new DictionaryEntry [1];
669                         c.CopyTo (array, array.Length);
670
671                         ICollection<KeyValuePair<int,int>> c2 = d;
672                         KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
673                         c2.CopyTo (array2, array2.Length);
674                 }
675
676                 [Test]
677                 public void IDictionary_Contains ()
678                 {
679                         IDictionary d = new Dictionary<int, int> ();
680                         d.Add (1, 2);
681                         Assert.IsTrue (d.Contains (1));
682                         Assert.IsFalse (d.Contains (2));
683                         Assert.IsFalse (d.Contains ("x"));
684                 }
685
686                 [Test, ExpectedException (typeof (ArgumentNullException))]
687                 public void IDictionary_Contains2 ()
688                 {
689                         IDictionary d = new Dictionary<int, int> ();
690                         d.Contains (null);
691                 }
692
693                 [Test, ExpectedException (typeof (ArgumentNullException))]
694                 public void IDictionary_Add1 ()
695                 {
696                         IDictionary d = new Dictionary<int, int> ();
697                         d.Add (null, 1);
698                 }
699
700                 [Test, ExpectedException (typeof (ArgumentException))]
701                 public void IDictionary_Add2 ()
702                 {
703                         IDictionary d = new Dictionary<int, int> ();
704                         d.Add ("bar", 1);
705                 }
706
707                 [Test, ExpectedException (typeof (ArgumentException))]
708                 public void IDictionary_Add3 ()
709                 {
710                         IDictionary d = new Dictionary<int, int> ();
711                         d.Add (1, "bar");
712                 }
713
714                 [Test]
715                 public void IDictionary_Add_Null ()
716                 {
717                         IDictionary d = new Dictionary<int, string> ();
718                         d.Add (1, null);
719                         d [2] = null;
720
721                         Assert.IsNull (d [1]);
722                         Assert.IsNull (d [2]);
723                 }
724
725                 [Test]
726                 [ExpectedException (typeof (ArgumentException))]
727                 public void IDictionary_Add_Null_2 ()
728                 {
729                         IDictionary d = new Dictionary<int, int> ();
730                         d.Add (1, null);
731                 }
732
733                 [Test]
734                 public void IDictionary_Remove1 ()
735                 {
736                         IDictionary d = new Dictionary<int, int> ();
737                         d.Add (1, 2);
738                         d.Remove (1);
739                         d.Remove (5);
740                         d.Remove ("foo");
741                 }
742
743                 [Test, ExpectedException (typeof (ArgumentNullException))]
744                 public void IDictionary_Remove2 ()
745                 {
746                         IDictionary d = new Dictionary<int, int> ();
747                         d.Remove (null);
748                 }
749                 
750                 [Test]
751                 public void IDictionary_IndexerGetNonExistingTest ()
752                 {
753                         IDictionary d = new Dictionary<int, int> ();
754                         d.Add(1, 2);
755                         Assert.IsNull(d[2]);
756                         Assert.IsNull(d["foo"]);
757                 }
758
759                 [Test] // bug #332534
760                 public void Dictionary_MoveNext ()
761                 {
762                         Dictionary<int,int> a = new Dictionary<int,int>();
763                         a.Add(3,1);
764                         a.Add(4,1);
765
766                         IEnumerator en = a.GetEnumerator();
767                         for (int i = 1; i < 10; i++)
768                                 en.MoveNext();
769                 }
770
771                 [Test]
772                 public void CopyToArray ()
773                 {
774                         Dictionary<string, string> test = new Dictionary<string, string> ();
775                         test.Add ("monkey", "singe");
776                         test.Add ("singe", "mono");
777                         test.Add ("mono", "monkey");
778                         Assert.AreEqual (3, test.Keys.Count, "Dictionary.Count");
779                         
780                         ArrayList list = new ArrayList (test.Keys);
781                         Assert.AreEqual (3, list.Count, "ArrayList.Count");
782                         Assert.IsTrue (list.Contains ("monkey"), "monkey");
783                         Assert.IsTrue (list.Contains ("singe"), "singe");
784                         Assert.IsTrue (list.Contains ("mono"), "mono");
785                 }
786
787                 [Test]
788                 public void KeyObjectMustNotGetChangedIfKeyAlreadyExists ()
789                 {
790                         Dictionary<String, int> d = new Dictionary<string, int> ();
791                         string s1 = "Test";
792                         string s2 = "Tes" + "T".ToLowerInvariant();
793                         d[s1] = 1;
794                         d[s2] = 2;
795                         string comp = String.Empty;
796                         foreach (String s in d.Keys)
797                                 comp = s;
798                         Assert.IsTrue (Object.ReferenceEquals (s1, comp));
799                 }
800
801                 [Test]
802                 public void ResetKeysEnumerator ()
803                 {
804                         Dictionary<string, string> test = new Dictionary<string, string> ();
805                         test.Add ("monkey", "singe");
806                         test.Add ("singe", "mono");
807                         test.Add ("mono", "monkey");
808
809                         IEnumerator enumerator = test.Keys.GetEnumerator ();
810
811                         Assert.IsTrue (enumerator.MoveNext ());
812                         Assert.IsTrue (enumerator.MoveNext ());
813
814                         enumerator.Reset ();
815
816                         Assert.IsTrue (enumerator.MoveNext ());
817                         Assert.IsTrue (enumerator.MoveNext ());
818                         Assert.IsTrue (enumerator.MoveNext ());
819                         Assert.IsFalse (enumerator.MoveNext ());
820                 }
821
822                 [Test]
823                 public void ResetValuesEnumerator ()
824                 {
825                         Dictionary<string, string> test = new Dictionary<string, string> ();
826                         test.Add ("monkey", "singe");
827                         test.Add ("singe", "mono");
828                         test.Add ("mono", "monkey");
829
830                         IEnumerator enumerator = test.Values.GetEnumerator ();
831
832                         Assert.IsTrue (enumerator.MoveNext ());
833                         Assert.IsTrue (enumerator.MoveNext ());
834
835                         enumerator.Reset ();
836
837                         Assert.IsTrue (enumerator.MoveNext ());
838                         Assert.IsTrue (enumerator.MoveNext ());
839                         Assert.IsTrue (enumerator.MoveNext ());
840                         Assert.IsFalse (enumerator.MoveNext ());
841                 }
842
843                 [Test]
844                 public void ResetShimEnumerator ()
845                 {
846                         IDictionary test = new Dictionary<string, string> ();
847                         test.Add ("monkey", "singe");
848                         test.Add ("singe", "mono");
849                         test.Add ("mono", "monkey");
850
851                         IEnumerator enumerator = test.GetEnumerator ();
852
853                         Assert.IsTrue (enumerator.MoveNext ());
854                         Assert.IsTrue (enumerator.MoveNext ());
855
856                         enumerator.Reset ();
857
858                         Assert.IsTrue (enumerator.MoveNext ());
859                         Assert.IsTrue (enumerator.MoveNext ());
860                         Assert.IsTrue (enumerator.MoveNext ());
861                         Assert.IsFalse (enumerator.MoveNext ());
862                 }
863         }
864 }
865
866 #endif // NET_2_0