Merge branch 'feature-concurrent-sweep'
[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
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.IO;
37 using System.Runtime.Serialization.Formatters.Binary;
38 using System.Text;
39 using System.Threading;
40
41 using NUnit.Framework;
42
43 namespace MonoTests.System.Collections.Generic {
44         [TestFixture]
45         public class DictionaryTest {
46                 class MyClass {
47                         int a;
48                         int b;
49                         public MyClass (int a, int b)
50                         {
51                                 this.a = a;
52                                 this.b = b;
53                         }
54                         public override int GetHashCode ()
55                         {
56                                 return a + b;
57                         }
58         
59                         public override bool Equals (object obj)
60                         {
61                                 if (!(obj is MyClass))
62                                         return false;
63                                 return ((MyClass)obj).Value == a;
64                         }
65         
66         
67                         public int Value {
68                                 get { return a; }
69                         }
70         
71                 }
72         
73                 Dictionary <string, object> _dictionary = null;
74                 Dictionary <MyClass, MyClass> _dictionary2 = null;
75                 Dictionary <int, int> _dictionary3 = null;
76         
77                 [SetUp]
78                 public void SetUp ()
79                 {
80                         _dictionary = new Dictionary <string, object> ();
81                         _dictionary2 = new Dictionary <MyClass, MyClass> ();
82                         _dictionary3 = new Dictionary <int, int>();
83                 }
84         
85                 [Test]
86                 public void AddTest ()
87                 {
88                         _dictionary.Add ("key1", "value");
89                         Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
90                 }
91         
92                 [Test]
93                 public void AddTest2 ()
94                 {
95                         MyClass m1 = new MyClass (10,5);
96                         MyClass m2 = new MyClass (20,5);
97                         MyClass m3 = new MyClass (12,3);
98                         _dictionary2.Add (m1,m1);
99                         _dictionary2.Add (m2, m2);
100                         _dictionary2.Add (m3, m3);
101                         Assert.AreEqual (20, _dictionary2 [m2].Value, "#1");
102                         Assert.AreEqual (10, _dictionary2 [m1].Value, "#2");
103                         Assert.AreEqual (12, _dictionary2 [m3].Value, "#3");
104                 }
105
106                 [Test]
107                 public void AddTest3 ()
108                 {
109                         _dictionary3.Add (1, 2);
110                         _dictionary3.Add (2, 3);
111                         _dictionary3.Add (3, 4);
112                         Assert.AreEqual (2, _dictionary3[1], "#1");
113                         Assert.AreEqual (3, _dictionary3[2], "#2");
114                         Assert.AreEqual (4, _dictionary3[3], "#3");
115                 }
116
117                 [Test, ExpectedException(typeof(ArgumentNullException))]
118                 public void AddNullTest ()
119                 {
120                         _dictionary.Add (null, "");
121                 }
122         
123                 [Test, ExpectedException(typeof(ArgumentException))]
124                 public void AddDuplicateTest ()
125                 {
126                         _dictionary.Add("foo", "bar");
127                         _dictionary.Add("foo", "bar");
128                 }
129
130                 //Tests Add when resize takes place
131                 [Test]
132                 public void AddLargeTest ()
133                 {
134                         int i, numElems = 50;
135         
136                         for (i = 0; i < numElems; i++)
137                         {
138                                 _dictionary3.Add (i, i);
139                         }
140         
141                         i = 0;
142                         foreach (KeyValuePair <int, int> entry in _dictionary3)
143                         {
144                                 i++;
145                         }
146         
147                         Assert.AreEqual (i, numElems, "Add with resize failed!");
148                 }
149         
150                 [Test]
151                 public void IndexerGetExistingTest ()
152                 {
153                         _dictionary.Add ("key1", "value");
154                         Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
155                 }
156                 
157                 [Test, ExpectedException(typeof(KeyNotFoundException))]
158                 public void IndexerGetNonExistingTest ()
159                 {
160                         object foo = _dictionary ["foo"];
161                 }
162
163                 [Test, ExpectedException(typeof(ArgumentNullException))]
164                 public void IndexerGetNullTest()
165                 {
166                         object s = _dictionary[null];
167                 }
168
169                 [Test]
170                 public void IndexerSetExistingTest ()
171                 {
172                         _dictionary.Add ("key1", "value1");
173                         _dictionary ["key1"] =  "value2";
174                         Assert.AreEqual (1, _dictionary.Count);
175                         Assert.AreEqual ("value2", _dictionary ["key1"]);
176                 }
177
178                 [Test]
179                 public void IndexerSetNonExistingTest ()
180                 {
181                         _dictionary ["key1"] =  "value1";
182                         Assert.AreEqual (1, _dictionary.Count);
183                         Assert.AreEqual ("value1", _dictionary ["key1"]);
184                 }
185         
186                 [Test]
187                 public void RemoveTest ()
188                 {
189                         _dictionary.Add ("key1", "value1");
190                         _dictionary.Add ("key2", "value2");
191                         _dictionary.Add ("key3", "value3");
192                         _dictionary.Add ("key4", "value4");
193                         Assert.IsTrue (_dictionary.Remove ("key3"));
194                         Assert.IsFalse (_dictionary.Remove ("foo"));
195                         Assert.AreEqual (3, _dictionary.Count);
196                         Assert.IsFalse (_dictionary.ContainsKey ("key3"));
197                 }
198         
199                 [Test]
200                 public void RemoveTest2 ()
201                 {
202                         MyClass m1 = new MyClass (10, 5);
203                         MyClass m2 = new MyClass (20, 5);
204                         MyClass m3 = new MyClass (12, 3);
205                         _dictionary2.Add (m1, m1);
206                         _dictionary2.Add (m2, m2);
207                         _dictionary2.Add (m3, m3);
208                         _dictionary2.Remove (m1); // m2 is in rehash path
209                         Assert.AreEqual (20, _dictionary2 [m2].Value, "#4");
210                         
211                 }
212
213                 [Test]
214                 [Category ("NotWorking")]
215                 public void Remove_ZeroOut ()
216                 {
217                         object key = new object ();
218                         object value = new object ();
219
220                         WeakReference wrKey = new WeakReference (key);
221                         WeakReference wrValue = new WeakReference (value);
222
223                         Dictionary <object, object> dictionary = new Dictionary <object, object> ();
224                         dictionary.Add (key, value);
225                         dictionary.Remove (key);
226
227                         key = null;
228                         value = null;
229                         GC.Collect ();
230                         Thread.Sleep (200);
231
232                         Assert.IsNull (wrKey.Target, "#1");
233                         Assert.IsNull (wrValue.Target, "#2");
234                 }
235         
236                 [Test, ExpectedException(typeof(ArgumentNullException))]
237                 public void IndexerSetNullTest()
238                 {
239                         _dictionary[null] = "bar";
240                 }
241         
242                 [Test]
243                 public void ClearTest ()
244                 {
245                         _dictionary.Add ("key1", "value1");
246                         _dictionary.Add ("key2", "value2");
247                         _dictionary.Add ("key3", "value3");
248                         _dictionary.Add ("key4", "value4");
249                         _dictionary.Clear ();
250                         Assert.AreEqual (0, _dictionary.Count, "Clear method failed!");
251                         Assert.IsFalse (_dictionary.ContainsKey ("key2"));
252                 }
253         
254                 [Test] // bug 432441
255                 public void Clear_Iterators ()
256                 {
257                         Dictionary<object, object> d = new Dictionary <object, object> ();
258
259                         d [new object ()] = new object ();
260                         d.Clear ();
261                         int hash = 0;
262                         foreach (object o in d) {
263                                 hash += o.GetHashCode ();
264                         }
265                 }
266
267                 [Test]
268                 [Category ("NotWorking")]
269                 public void Clear_ZeroOut ()
270                 {
271                         object key = new object ();
272                         object value = new object ();
273
274                         WeakReference wrKey = new WeakReference (key);
275                         WeakReference wrValue = new WeakReference (value);
276
277                         Dictionary <object, object> dictionary = new Dictionary <object, object> ();
278                         dictionary.Add (key, value);
279                         dictionary.Clear ();
280
281                         key = null;
282                         value = null;
283                         GC.Collect ();
284                         Thread.Sleep (200);
285
286                         Assert.IsNull (wrKey.Target, "#1");
287                         Assert.IsNull (wrValue.Target, "#2");
288                 }
289
290                 [Test]
291                 public void ContainsKeyTest ()
292                 {
293                         _dictionary.Add ("key1", "value1");
294                         _dictionary.Add ("key2", "value2");
295                         _dictionary.Add ("key3", "value3");
296                         _dictionary.Add ("key4", "value4");
297                         bool contains = _dictionary.ContainsKey ("key4");
298                         Assert.IsTrue (contains, "ContainsKey does not return correct value!");
299                         contains = _dictionary.ContainsKey ("key5");
300                         Assert.IsFalse (contains, "ContainsKey for non existant does not return correct value!");
301                 }
302
303                 [Test, ExpectedException (typeof (ArgumentNullException))]
304                 public void ContainsKeyTest2 ()
305                 {
306                         _dictionary.ContainsKey (null);
307                 }
308         
309                 [Test]
310                 public void ContainsValueTest ()
311                 {
312                         _dictionary.Add ("key1", "value1");
313                         _dictionary.Add ("key2", "value2");
314                         _dictionary.Add ("key3", "value3");
315                         _dictionary.Add ("key4", "value4");
316                         bool contains = _dictionary.ContainsValue ("value2");
317                         Assert.IsTrue(contains, "ContainsValue does not return correct value!");
318                         contains = _dictionary.ContainsValue ("@@daisofja@@");
319                         Assert.IsFalse (contains, "ContainsValue for non existant does not return correct value!");
320                 }
321         
322                 [Test]
323                 public void TryGetValueTest()
324                 {
325                         _dictionary.Add ("key1", "value1");
326                         _dictionary.Add ("key2", "value2");
327                         _dictionary.Add ("key3", "value3");
328                         _dictionary.Add ("key4", "value4");
329                         object value = "";
330                         bool retrieved = _dictionary.TryGetValue ("key4", out value);
331                         Assert.IsTrue (retrieved);
332                         Assert.AreEqual ("value4", (string)value, "TryGetValue does not return value!");
333         
334                         retrieved = _dictionary.TryGetValue ("key7", out value);
335                         Assert.IsFalse (retrieved);
336                         Assert.IsNull (value, "value for non existant value should be null!");
337                 }
338         
339                 [Test]
340                 public void ValueTypeTest ()
341                 {
342                         Dictionary <int, float> dict = new Dictionary <int, float> ();
343                         dict.Add (10, 10.3f);
344                         dict.Add (11, 10.4f);
345                         dict.Add (12, 10.5f);
346                         Assert.AreEqual (10.4f, dict [11], "#5");
347                 }
348         
349                 private class MyTest
350                 {
351                         public string Name;
352                         public int RollNo;
353         
354                         public MyTest (string name, int number)
355                         {
356                                 Name = name;
357                                 RollNo = number;
358                         }
359
360                         public override int GetHashCode ()
361                         {
362                                 return Name.GetHashCode () ^ RollNo;
363                         }
364
365                         public override bool Equals (object obj)
366                         {
367                                 MyTest myt = obj as MyTest;
368                                 return myt.Name.Equals (this.Name) &&
369                                                 myt.RollNo.Equals (this.RollNo);
370                         }
371                 }
372         
373                 [Test]
374                 public void ObjectAsKeyTest ()
375                 {
376                         Dictionary <object, object> dict = new Dictionary <object, object> ();
377                         MyTest key1, key2, key3;
378                         dict.Add ( (key1 = new MyTest ("key1", 234)), "value1");
379                         dict.Add ( (key2 = new MyTest ("key2", 444)), "value2");
380                         dict.Add ( (key3 = new MyTest ("key3", 5655)), "value3");
381         
382                         Assert.AreEqual ("value2", dict [key2], "value is not returned!");
383                         Assert.AreEqual ("value3", dict [key3], "neg: exception should not be thrown!");
384                 }
385         
386                 [Test, ExpectedException (typeof (ArgumentException))]
387                 public void IDictionaryAddTest ()
388                 {
389                         IDictionary iDict = _dictionary as IDictionary;
390                         iDict.Add ("key1", "value1");
391                         iDict.Add ("key2", "value3");
392                         Assert.AreEqual (2, iDict.Count, "IDictioanry interface add is not working!");
393         
394                         //Negative test case
395                         iDict.Add (12, "value");
396                         iDict.Add ("key", 34);
397                 }
398         
399                 [Test]
400                 public void IEnumeratorTest ()
401                 {
402                         _dictionary.Add ("key1", "value1");
403                         _dictionary.Add ("key2", "value2");
404                         _dictionary.Add ("key3", "value3");
405                         _dictionary.Add ("key4", "value4");
406                         IEnumerator itr = ((IEnumerable)_dictionary).GetEnumerator ();
407                         while (itr.MoveNext ()) {
408                                 object o = itr.Current;
409                                 Assert.AreEqual (typeof (KeyValuePair<string,object>), o.GetType (), "Current should return a type of KeyValuePair");
410                                 KeyValuePair<string,object> entry = (KeyValuePair<string,object>) itr.Current;
411                         }
412                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
413                 }
414         
415         
416                 [Test]
417                 public void IEnumeratorGenericTest ()
418                 {
419                         _dictionary.Add ("key1", "value1");
420                         _dictionary.Add ("key2", "value2");
421                         _dictionary.Add ("key3", "value3");
422                         _dictionary.Add ("key4", "value4");
423                         IEnumerator <KeyValuePair <string, object>> itr = ((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
424                         while (itr.MoveNext ()) {
425                                 object o = itr.Current;
426                                 Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of KeyValuePair<object,string>");
427                                 KeyValuePair <string, object> entry = (KeyValuePair <string, object>)itr.Current;
428                         }
429                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
430                 }
431         
432                 [Test]
433                 public void IDictionaryEnumeratorTest ()
434                 {
435                         _dictionary.Add ("key1", "value1");
436                         _dictionary.Add ("key2", "value2");
437                         _dictionary.Add ("key3", "value3");
438                         _dictionary.Add ("key4", "value4");
439                         IDictionaryEnumerator itr = ((IDictionary)_dictionary).GetEnumerator ();
440                         while (itr.MoveNext ()) {
441                                 object o = itr.Current;
442                                 Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
443                                 DictionaryEntry entry = (DictionaryEntry) itr.Current;
444                         }
445                         Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
446                 }
447         
448                 [Test]
449                 public void ForEachTest ()
450                 {
451                         _dictionary.Add ("key1", "value1");
452                         _dictionary.Add ("key2", "value2");
453                         _dictionary.Add ("key3", "value3");
454                         _dictionary.Add ("key4", "value4");
455         
456                         int i = 0;
457                         foreach (KeyValuePair <string, object> entry in _dictionary)
458                                 i++;
459                         Assert.AreEqual(4, i, "fail1: foreach entry failed!");
460         
461                         i = 0;
462                         foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
463                                 i++;
464                         Assert.AreEqual(4, i, "fail2: foreach entry failed!");
465         
466                         i = 0;
467                         foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
468                                 i++;
469                         Assert.AreEqual (4, i, "fail3: foreach entry failed!");
470                 }
471         
472                 [Test]
473                 public void ResizeTest ()
474                 {
475                         Dictionary <string, object> dictionary = new Dictionary <string, object> (3);
476                         dictionary.Add ("key1", "value1");
477                         dictionary.Add ("key2", "value2");
478                         dictionary.Add ("key3", "value3");
479         
480                         Assert.AreEqual (3, dictionary.Count);
481         
482                         dictionary.Add ("key4", "value4");
483                         Assert.AreEqual (4, dictionary.Count);
484                         Assert.AreEqual ("value1", dictionary ["key1"].ToString (), "");
485                         Assert.AreEqual ("value2", dictionary ["key2"].ToString (), "");
486                         Assert.AreEqual ("value4", dictionary ["key4"].ToString (), "");
487                         Assert.AreEqual ("value3", dictionary ["key3"].ToString (), "");
488                 }
489         
490                 [Test]
491                 public void KeyCollectionTest ()
492                 {
493                         _dictionary.Add ("key1", "value1");
494                         _dictionary.Add ("key2", "value2");
495                         _dictionary.Add ("key3", "value3");
496                         _dictionary.Add ("key4", "value4");
497         
498                         ICollection <string> keys = ((IDictionary <string, object>)_dictionary).Keys;
499                         Assert.AreEqual (4, keys.Count);
500                         int i = 0;
501                         foreach (string key in keys)
502                         {
503                                 i++;
504                         }
505                         Assert.AreEqual(4, i);
506                 }
507
508                 [Test]
509                 public void KeyValueEnumeratorTest ()
510                 {
511                         IDictionary<int, int> d = new Dictionary<int, int>();
512
513                         // Values are chosen such that two keys map to the same bucket.
514                         // Default dictionary table size == 10
515                         d [9] = 1;
516                         d [10] = 2;
517                         d [19] = 3;
518
519                         Assert.AreEqual (d.Count, d.Keys.Count, "d and d.Keys don't appear to match");
520                         Assert.AreEqual (d.Values.Count, d.Keys.Count, "d.Keys and d.Values don't appear to match");
521
522                         int count = 0;
523                         foreach (int i in d.Values)
524                                 ++count;
525                         Assert.AreEqual (count, d.Values.Count, "d.Values doesn't have the correct number of elements");
526         
527                         count = 0;
528                         foreach (int i in d.Keys)
529                                 ++count;
530                         Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't have the correct number of elements");
531
532                         int nkeys = count;
533                         count = 0;
534                         foreach (int i in d.Keys) {
535                                 int foo = d [i];
536                                 if (count++ >= nkeys)
537                                         Assert.Fail ("Reading a value appears to trash enumerator state");
538                         }
539                 }
540
541                 [Test] // bug 75073
542                 public void SliceCollectionsEnumeratorTest ()
543                 {
544                         Dictionary<string, int> values = new Dictionary<string, int> ();
545
546                         IEnumerator <string> ke = values.Keys.GetEnumerator ();
547                         IEnumerator <int>    ve = values.Values.GetEnumerator ();
548
549                         Assert.IsTrue (ke is Dictionary<string, int>.KeyCollection.Enumerator);
550                         Assert.IsTrue (ve is Dictionary<string, int>.ValueCollection.Enumerator);
551                 }
552
553                 [Test]
554                 public void PlainEnumeratorReturnTest ()
555                 {
556                         // Test that we return a KeyValuePair even for non-generic dictionary iteration
557                         _dictionary["foo"] = "bar";
558                         IEnumerator<KeyValuePair<string, object>> enumerator = _dictionary.GetEnumerator();
559                         Assert.IsTrue(enumerator.MoveNext(), "#1");
560                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IEnumerator)enumerator).Current.GetType (), "#2");
561                         Assert.AreEqual (typeof (DictionaryEntry), ((IDictionaryEnumerator)enumerator).Entry.GetType (), "#3");
562                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IDictionaryEnumerator)enumerator).Current.GetType (), "#4");
563                         Assert.AreEqual (typeof (KeyValuePair<string,object>), ((object) enumerator.Current).GetType (), "#5");
564                 }
565
566                 [Test, ExpectedException (typeof (InvalidOperationException))]
567                 public void FailFastTest1 ()
568                 {
569                         Dictionary<int, int> d = new Dictionary<int, int> ();
570                         d [1] = 1;
571                         int count = 0;
572                         foreach (KeyValuePair<int, int> kv in d) {
573                                 d [kv.Key + 1] = kv.Value + 1;
574                                 if (count++ != 0)
575                                         Assert.Fail ("Should not be reached");
576                         }
577                         Assert.Fail ("Should not be reached");
578                 }
579
580                 [Test, ExpectedException (typeof (InvalidOperationException))]
581                 public void FailFastTest2 ()
582                 {
583                         Dictionary<int, int> d = new Dictionary<int, int> ();
584                         d [1] = 1;
585                         int count = 0;
586                         foreach (int i in d.Keys) {
587                                 d [i + 1] = i + 1;
588                                 if (count++ != 0)
589                                         Assert.Fail ("Should not be reached");
590                         }
591                         Assert.Fail ("Should not be reached");
592                 }
593
594                 [Test, ExpectedException (typeof (InvalidOperationException))]
595                 public void FailFastTest3 ()
596                 {
597                         Dictionary<int, int> d = new Dictionary<int, int> ();
598                         d [1] = 1;
599                         int count = 0;
600                         foreach (int i in d.Keys) {
601                                 d [i] = i;
602                                 if (count++ != 0)
603                                         Assert.Fail ("Should not be reached");
604                         }
605                         Assert.Fail ("Should not be reached");
606                 }
607
608                 [Test]
609                 public void SerializationTest()
610                 {
611                         for (int i = 0; i < 50; i++)
612                         {
613                                 _dictionary3.Add(i, i);
614                         }
615
616                         BinaryFormatter formatter = new BinaryFormatter();
617                         MemoryStream stream = new MemoryStream();
618                         formatter.Serialize(stream, _dictionary3);
619
620                         stream.Position = 0;
621                         object deserialized = formatter.Deserialize(stream);
622
623                         Assert.IsNotNull(deserialized);
624                         Assert.IsFalse(deserialized == _dictionary3);
625
626                         Assert.IsTrue(deserialized is Dictionary<int, int>);
627                         Dictionary<int, int> d3 = deserialized as Dictionary<int, int>;
628
629                         Assert.AreEqual(50, d3.Count);
630                         for (int i = 0; i < 50; i++)
631                         {
632                                 Assert.AreEqual(i, d3[i]);
633                         }
634                 }
635
636 #if !MONO
637                 [Test]
638                 public void SerializationCompatibilty ()
639                 {
640                         /* Serialization output from .net 
641
642                         var dict = new Dictionary<string, string> ();
643                         dict.Add ("key", "value");
644
645                         var dictSerializedStream = new MemoryStream ();
646                         fmt.Serialize (dictSerializedStream, dict);
647
648                         dictSerializedStream.Seek (0, SeekOrigin.Begin);
649                         var res = Convert.ToBase64String (dictSerializedStream.ToArray (), Base64FormattingOptions.InsertLineBreaks);
650
651                         */
652
653                         var x = "AAEAAAD/////AQAAAAAAAAAEAQAAAOIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv" +
654                                 "bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJl" +
655                                 "PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uU3RyaW5n" +
656                                 "LCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRv" +
657                                 "a2VuPWI3N2E1YzU2MTkzNGUwODldXQQAAAAHVmVyc2lvbghDb21wYXJlcghIYXNoU2l6ZQ1LZXlW" +
658                                 "YWx1ZVBhaXJzAAMAAwiSAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkdlbmVyaWNFcXVhbGl0" +
659                                 "eUNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1" +
660                                 "bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dCOYBU3lzdGVt" +
661                                 "LkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQYWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29y" +
662                                 "bGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3" +
663                                 "YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAs" +
664                                 "IEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dW10BAAAA" + 
665                                 "CQIAAAADAAAACQMAAAAEAgAAAJIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0Vx" +
666                                 "dWFsaXR5Q29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAu" + 
667                                 "MCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0AAAAA" + 
668                                 "BwMAAAAAAQAAAAEAAAAD5AFTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5LZXlWYWx1ZVBhaXJg" + 
669                                 "MltbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0" + 
670                                 "cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XSxbU3lzdGVtLlN0cmluZywgbXNj" + 
671                                 "b3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1i" + 
672                                 "NzdhNWM1NjE5MzRlMDg5XV0E/P///+QBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFs" + 
673                                 "dWVQYWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1" + 
674                                 "cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5TdHJp" + 
675                                 "bmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5" + 
676                                 "VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAgAAAANrZXkFdmFsdWUBAQYFAAAAA2tleQYGAAAABXZh" + 
677                                 "bHVlCw==";
678
679                         var dict = new Dictionary<string, string> ();
680                         dict.Add ("key", "value");
681                 
682                         BinaryFormatter fmt = new BinaryFormatter ();
683                         var mdict = (Dictionary<string, string>) fmt.Deserialize (new MemoryStream (Convert.FromBase64String (x)));
684
685                         Assert.AreEqual (1, dict.Count);
686                         Assert.AreEqual (dict.Comparer.GetType (), mdict.Comparer.GetType ());
687                 }
688 #endif
689
690                 [Test]
691                 public void ZeroCapacity ()
692                 {
693                         Dictionary<int, int> x = new Dictionary <int, int> (0);
694                         x.Add (1, 2);
695                         
696                         x = new Dictionary <int, int> (0);
697                         x.Clear ();
698
699                         x = new Dictionary <int, int> (0);
700                         int aa = x.Count;
701                         
702                         x = new Dictionary <int, int> (0);
703                         try {
704                                 int j = x [1];
705                         } catch (KeyNotFoundException){
706                         }
707
708                         bool b;
709                         b = x.ContainsKey (10);
710                         b = x.ContainsValue (10);
711
712                         x = new Dictionary <int, int> (0);
713                         x.Remove (10);
714                         
715                         x = new Dictionary <int, int> (0);
716                         int intv;
717                         x.TryGetValue (1, out intv);
718
719                         object oa = x.Keys;
720                         object ob = x.Values;
721                         foreach (KeyValuePair<int,int> a in x){
722                         }
723                 }
724
725                 [Test]
726                 public void Empty_KeysValues_CopyTo ()
727                 {
728                         Dictionary<int, int> d = new Dictionary<int, int> ();
729                         int[] array = new int[1];
730                         d.Keys.CopyTo (array, array.Length);
731                         d.Values.CopyTo (array, array.Length);
732                 }
733
734                 [Test]
735                 public void Empty_CopyTo ()
736                 {
737                         Dictionary<int, int> d = new Dictionary<int, int> ();
738                         ICollection c = (ICollection) d;
739                         DictionaryEntry [] array = new DictionaryEntry [1];
740                         c.CopyTo (array, array.Length);
741
742                         ICollection<KeyValuePair<int,int>> c2 = d;
743                         KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
744                         c2.CopyTo (array2, array2.Length);
745                 }
746
747                 [Test]
748                 public void IDictionary_Contains ()
749                 {
750                         IDictionary d = new Dictionary<int, int> ();
751                         d.Add (1, 2);
752                         Assert.IsTrue (d.Contains (1));
753                         Assert.IsFalse (d.Contains (2));
754                         Assert.IsFalse (d.Contains ("x"));
755                 }
756
757                 [Test, ExpectedException (typeof (ArgumentNullException))]
758                 public void IDictionary_Contains2 ()
759                 {
760                         IDictionary d = new Dictionary<int, int> ();
761                         d.Contains (null);
762                 }
763
764                 [Test, ExpectedException (typeof (ArgumentNullException))]
765                 public void IDictionary_Add1 ()
766                 {
767                         IDictionary d = new Dictionary<int, int> ();
768                         d.Add (null, 1);
769                 }
770
771                 [Test, ExpectedException (typeof (ArgumentException))]
772                 public void IDictionary_Add2 ()
773                 {
774                         IDictionary d = new Dictionary<int, int> ();
775                         d.Add ("bar", 1);
776                 }
777
778                 [Test, ExpectedException (typeof (ArgumentException))]
779                 public void IDictionary_Add3 ()
780                 {
781                         IDictionary d = new Dictionary<int, int> ();
782                         d.Add (1, "bar");
783                 }
784
785                 [Test]
786                 public void IDictionary_Add_Null ()
787                 {
788                         IDictionary d = new Dictionary<int, string> ();
789                         d.Add (1, null);
790                         d [2] = null;
791
792                         Assert.IsNull (d [1]);
793                         Assert.IsNull (d [2]);
794                 }
795
796                 [Test]
797                 [ExpectedException (typeof (ArgumentNullException))]
798                 public void IDictionary_Add_Null_2 ()
799                 {
800                         IDictionary d = new Dictionary<int, int> ();
801                         d.Add (1, null);
802                 }
803
804                 [Test]
805                 public void IDictionary_Remove1 ()
806                 {
807                         IDictionary d = new Dictionary<int, int> ();
808                         d.Add (1, 2);
809                         d.Remove (1);
810                         d.Remove (5);
811                         d.Remove ("foo");
812                 }
813
814                 [Test, ExpectedException (typeof (ArgumentNullException))]
815                 public void IDictionary_Remove2 ()
816                 {
817                         IDictionary d = new Dictionary<int, int> ();
818                         d.Remove (null);
819                 }
820                 
821                 [Test]
822                 public void IDictionary_IndexerGetNonExistingTest ()
823                 {
824                         IDictionary d = new Dictionary<int, int> ();
825                         d.Add(1, 2);
826                         Assert.IsNull(d[2]);
827                         Assert.IsNull(d["foo"]);
828                 }
829
830                 [Test] // bug #332534
831                 public void Dictionary_MoveNext ()
832                 {
833                         Dictionary<int,int> a = new Dictionary<int,int>();
834                         a.Add(3,1);
835                         a.Add(4,1);
836
837                         IEnumerator en = a.GetEnumerator();
838                         for (int i = 1; i < 10; i++)
839                                 en.MoveNext();
840                 }
841
842                 [Test]
843                 public void CopyToArray ()
844                 {
845                         Dictionary<string, string> test = new Dictionary<string, string> ();
846                         test.Add ("monkey", "singe");
847                         test.Add ("singe", "mono");
848                         test.Add ("mono", "monkey");
849                         Assert.AreEqual (3, test.Keys.Count, "Dictionary.Count");
850                         
851                         ArrayList list = new ArrayList (test.Keys);
852                         Assert.AreEqual (3, list.Count, "ArrayList.Count");
853                         Assert.IsTrue (list.Contains ("monkey"), "monkey");
854                         Assert.IsTrue (list.Contains ("singe"), "singe");
855                         Assert.IsTrue (list.Contains ("mono"), "mono");
856                 }
857
858                 [Test]
859                 public void KeyObjectMustNotGetChangedIfKeyAlreadyExists ()
860                 {
861                         Dictionary<String, int> d = new Dictionary<string, int> ();
862                         string s1 = "Test";
863                         string s2 = "Tes" + "T".ToLowerInvariant();
864                         d[s1] = 1;
865                         d[s2] = 2;
866                         string comp = String.Empty;
867                         foreach (String s in d.Keys)
868                                 comp = s;
869                         Assert.IsTrue (Object.ReferenceEquals (s1, comp));
870                 }
871
872                 [Test]
873                 public void ResetKeysEnumerator ()
874                 {
875                         Dictionary<string, string> test = new Dictionary<string, string> ();
876                         test.Add ("monkey", "singe");
877                         test.Add ("singe", "mono");
878                         test.Add ("mono", "monkey");
879
880                         IEnumerator enumerator = test.Keys.GetEnumerator ();
881
882                         Assert.IsTrue (enumerator.MoveNext ());
883                         Assert.IsTrue (enumerator.MoveNext ());
884
885                         enumerator.Reset ();
886
887                         Assert.IsTrue (enumerator.MoveNext ());
888                         Assert.IsTrue (enumerator.MoveNext ());
889                         Assert.IsTrue (enumerator.MoveNext ());
890                         Assert.IsFalse (enumerator.MoveNext ());
891                 }
892
893                 [Test]
894                 public void ResetValuesEnumerator ()
895                 {
896                         Dictionary<string, string> test = new Dictionary<string, string> ();
897                         test.Add ("monkey", "singe");
898                         test.Add ("singe", "mono");
899                         test.Add ("mono", "monkey");
900
901                         IEnumerator enumerator = test.Values.GetEnumerator ();
902
903                         Assert.IsTrue (enumerator.MoveNext ());
904                         Assert.IsTrue (enumerator.MoveNext ());
905
906                         enumerator.Reset ();
907
908                         Assert.IsTrue (enumerator.MoveNext ());
909                         Assert.IsTrue (enumerator.MoveNext ());
910                         Assert.IsTrue (enumerator.MoveNext ());
911                         Assert.IsFalse (enumerator.MoveNext ());
912                 }
913
914                 [Test]
915                 public void ResetShimEnumerator ()
916                 {
917                         IDictionary test = new Dictionary<string, string> ();
918                         test.Add ("monkey", "singe");
919                         test.Add ("singe", "mono");
920                         test.Add ("mono", "monkey");
921
922                         IEnumerator enumerator = test.GetEnumerator ();
923
924                         Assert.IsTrue (enumerator.MoveNext ());
925                         Assert.IsTrue (enumerator.MoveNext ());
926
927                         enumerator.Reset ();
928
929                         Assert.IsTrue (enumerator.MoveNext ());
930                         Assert.IsTrue (enumerator.MoveNext ());
931                         Assert.IsTrue (enumerator.MoveNext ());
932                         Assert.IsFalse (enumerator.MoveNext ());
933                 }
934
935                 [Test]
936                 public void ICollectionOfKeyValuePairContains ()
937                 {
938                         var dictionary = new Dictionary<string, int> ();
939                         dictionary.Add ("foo", 42);
940                         dictionary.Add ("bar", 12);
941
942                         var collection = dictionary as ICollection<KeyValuePair<string, int>>;
943
944                         Assert.AreEqual (2, collection.Count);
945
946                         Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("baz", 13)));
947                         Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("foo", 13)));
948                         Assert.IsTrue (collection.Contains (new KeyValuePair<string, int> ("foo", 42)));
949                 }
950
951                 [Test]
952                 public void ICollectionOfKeyValuePairRemove ()
953                 {
954                         var dictionary = new Dictionary<string, int> ();
955                         dictionary.Add ("foo", 42);
956                         dictionary.Add ("bar", 12);
957
958                         var collection = dictionary as ICollection<KeyValuePair<string, int>>;
959
960                         Assert.AreEqual (2, collection.Count);
961
962                         Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("baz", 13)));
963                         Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("foo", 13)));
964                         Assert.IsTrue (collection.Remove (new KeyValuePair<string, int> ("foo", 42)));
965
966                         Assert.AreEqual (12, dictionary ["bar"]);
967                         Assert.IsFalse (dictionary.ContainsKey ("foo"));
968                 }
969
970                 [Test]
971                 public void ICollectionCopyToKeyValuePairArray ()
972                 {
973                         var dictionary = new Dictionary<string, int> ();
974                         dictionary.Add ("foo", 42);
975
976                         var collection = dictionary as ICollection;
977
978                         Assert.AreEqual (1, collection.Count);
979
980                         var pairs = new KeyValuePair<string, int> [1];
981
982                         collection.CopyTo (pairs, 0);
983
984                         Assert.AreEqual ("foo", pairs [0].Key);
985                         Assert.AreEqual (42, pairs [0].Value);
986                 }
987
988                 [Test]
989                 public void ICollectionCopyToDictionaryEntryArray ()
990                 {
991                         var dictionary = new Dictionary<string, int> ();
992                         dictionary.Add ("foo", 42);
993
994                         var collection = dictionary as ICollection;
995
996                         Assert.AreEqual (1, collection.Count);
997
998                         var entries = new DictionaryEntry [1];
999
1000                         collection.CopyTo (entries, 0);
1001
1002                         Assert.AreEqual ("foo", (string) entries [0].Key);
1003                         Assert.AreEqual (42, (int) entries [0].Value);
1004                 }
1005
1006                 [Test]
1007                 public void ICollectionCopyToObjectArray ()
1008                 {
1009                         var dictionary = new Dictionary<string, int> ();
1010                         dictionary.Add ("foo", 42);
1011
1012                         var collection = dictionary as ICollection;
1013
1014                         Assert.AreEqual (1, collection.Count);
1015
1016                         var array = new object [1];
1017
1018                         collection.CopyTo (array, 0);
1019
1020                         var pair = (KeyValuePair<string, int>) array [0];
1021
1022                         Assert.AreEqual ("foo", pair.Key);
1023                         Assert.AreEqual (42, pair.Value);
1024                 }
1025
1026                 [Test]
1027                 [ExpectedException (typeof (ArgumentException))]
1028                 public void ICollectionCopyToInvalidArray ()
1029                 {
1030                         var dictionary = new Dictionary<string, int> ();
1031                         dictionary.Add ("foo", 42);
1032
1033                         var collection = dictionary as ICollection;
1034
1035                         Assert.AreEqual (1, collection.Count);
1036
1037                         var array = new int [1];
1038
1039                         collection.CopyTo (array, 0);
1040                 }
1041
1042                 [Test]
1043                 public void ValuesCopyToObjectArray ()
1044                 {
1045                         var dictionary = new Dictionary<string, string> { { "foo", "bar" } };
1046
1047                         var values = dictionary.Values as ICollection;
1048
1049                         var array = new object [values.Count];
1050
1051                         values.CopyTo (array, 0);
1052
1053                         Assert.AreEqual ("bar", array [0]);
1054                 }
1055
1056                 delegate void D ();
1057                 bool Throws (D d)
1058                 {
1059                         try {
1060                                 d ();
1061                                 return false;
1062                         } catch {
1063                                 return true;
1064                         }
1065                 }
1066
1067                 [Test]
1068                 // based on #491858, #517415
1069                 public void Enumerator_Current ()
1070                 {
1071                         var e1 = new Dictionary<int,int>.Enumerator ();
1072                         Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1073
1074                         var d = new Dictionary<int,int> ();
1075                         var e2 = d.GetEnumerator ();
1076                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1077                         e2.MoveNext ();
1078                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1079                         e2.Dispose ();
1080                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1081
1082                         var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
1083                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1084                         e3.MoveNext ();
1085                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1086                         e3.Dispose ();
1087                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1088
1089                         var e4 = ((IEnumerable) d).GetEnumerator ();
1090                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1091                         e4.MoveNext ();
1092                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1093                         ((IDisposable) e4).Dispose ();
1094                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1095                 }
1096
1097                 [Test]
1098                 // based on #491858, #517415
1099                 public void KeyEnumerator_Current ()
1100                 {
1101                         var e1 = new Dictionary<int,int>.KeyCollection.Enumerator ();
1102                         Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1103
1104                         var d = new Dictionary<int,int> ().Keys;
1105                         var e2 = d.GetEnumerator ();
1106                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1107                         e2.MoveNext ();
1108                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1109                         e2.Dispose ();
1110                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1111
1112                         var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1113                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1114                         e3.MoveNext ();
1115                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1116                         e3.Dispose ();
1117                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1118
1119                         var e4 = ((IEnumerable) d).GetEnumerator ();
1120                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1121                         e4.MoveNext ();
1122                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1123                         ((IDisposable) e4).Dispose ();
1124                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1125                 }
1126
1127                 [Test]
1128                 // based on #491858, #517415
1129                 public void ValueEnumerator_Current ()
1130                 {
1131                         var e1 = new Dictionary<int,int>.ValueCollection.Enumerator ();
1132                         Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1133
1134                         var d = new Dictionary<int,int> ().Values;
1135                         var e2 = d.GetEnumerator ();
1136                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1137                         e2.MoveNext ();
1138                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1139                         e2.Dispose ();
1140                         Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1141
1142                         var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1143                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1144                         e3.MoveNext ();
1145                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1146                         e3.Dispose ();
1147                         Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1148
1149                         var e4 = ((IEnumerable) d).GetEnumerator ();
1150                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1151                         e4.MoveNext ();
1152                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1153                         ((IDisposable) e4).Dispose ();
1154                         Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1155                 }
1156
1157                 [Test]
1158                 public void ICollectionCopyTo ()
1159                 {
1160                         var d = new Dictionary<int, string> ();
1161
1162                         ICollection c = d;
1163                         c.CopyTo (new object [0], 0);
1164                         c.CopyTo (new string [0], 0);
1165                         c.CopyTo (new MyClass [0], 0);
1166
1167                         c = d.Keys;
1168                         c.CopyTo (new object [0], 0);
1169                         c.CopyTo (new ValueType [0], 0);
1170
1171                         c = d.Values;
1172                         c.CopyTo (new object [0], 0);
1173                         c.CopyTo (new MyClass [0], 0);
1174
1175                         d [3] = null;
1176
1177                         c = d.Keys;
1178                         c.CopyTo (new object [1], 0);
1179                         c.CopyTo (new ValueType [1], 0);
1180
1181                         c = d.Values;
1182                         c.CopyTo (new object [1], 0);
1183                         c.CopyTo (new MyClass [1], 0);
1184                 }
1185
1186                 public void ICollectionCopyTo_ex1 ()
1187                 {
1188                         var d = new Dictionary<int, string> ();
1189                         ICollection c = d.Keys;
1190                         c.CopyTo (new string [1], 0);
1191                 }
1192
1193                 [Test, ExpectedException (typeof (ArgumentException))]
1194                 public void ICollectionCopyTo_ex2 ()
1195                 {
1196                         var d = new Dictionary<int, string> ();
1197                         ICollection c = d.Values;
1198                         c.CopyTo (new int [1], 0);
1199                 }
1200
1201                 [Test, ExpectedException (typeof (ArgumentException))]
1202                 public void ICollectionCopyTo_ex3 ()
1203                 {
1204                         var d = new Dictionary<int, string> ();
1205                         d [3] = "5";
1206
1207                         ICollection c = d.Keys;
1208                         c.CopyTo (new MyClass [1], 0);
1209                 }
1210
1211                 [Test, ExpectedException (typeof (ArgumentException))]
1212                 public void ICollectionCopyTo_ex4 ()
1213                 {
1214                         var d = new Dictionary<int, string> ();
1215                         d [3] = "5";
1216
1217                         ICollection c = d.Values;
1218                         c.CopyTo (new MyClass [1], 0);
1219                 }
1220
1221                 [Test] // bug 474009
1222                 public void DeserializeEmptyDictionary ()
1223                 {
1224                         // contains a Dictionary<string, int> with Count = 0
1225                         // serialized with MS.NET 3.5
1226                         string data =
1227 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1228 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1229 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1230 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1231 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAwAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplAAMACJIB
1232 U3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5Q29tcGFyZXJgMVtbU3lz
1233 dGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQ
1234 dWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0IAAAAAAkCAAAAAAAAAAQCAAAAkgFTeXN0
1235 ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5HZW5lcmljRXF1YWxpdHlDb21wYXJlcmAxW1tTeXN0ZW0u
1236 U3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1Ymxp
1237 Y0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQAAAAAL";
1238
1239                         var stream = new MemoryStream (Convert.FromBase64String (data));
1240                         var fmt = new BinaryFormatter ();
1241                         var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1242                         Assert.AreEqual (0, dict.Count);
1243                 }
1244
1245                 [Test]
1246                 public void DeserializeNonEmptyDictionary ()
1247                 {
1248                         // contains a Dictionary<string, int> with Count = 2
1249                         // and dict [i.ToString()] == i for each i.
1250                         // serialized with MS.NET 3.5
1251                         string data =
1252 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1253 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1254 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1255 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1256 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dBAAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplDUtleVZh
1257 bHVlUGFpcnMAAwADCJIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5
1258 Q29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3Vs
1259 dHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0I5QFTeXN0ZW0u
1260 Q29sbGVjdGlvbnMuR2VuZXJpYy5LZXlWYWx1ZVBhaXJgMltbU3lzdGVtLlN0cmluZywgbXNjb3Js
1261 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1262 NWM1NjE5MzRlMDg5XSxbU3lzdGVtLkludDMyLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBD
1263 dWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXVtdAgAAAAkC
1264 AAAAAwAAAAkDAAAABAIAAACSAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkdlbmVyaWNFcXVh
1265 bGl0eUNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAs
1266 IEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAAAAAAcD
1267 AAAAAAEAAAACAAAAA+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQYWlyYDJb
1268 W1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJh
1269 bCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwgbXNjb3Js
1270 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1271 NWM1NjE5MzRlMDg5XV0E/P///+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQ
1272 YWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9
1273 bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwg
1274 bXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tl
1275 bj1iNzdhNWM1NjE5MzRlMDg5XV0CAAAAA2tleQV2YWx1ZQEACAYFAAAAATAAAAAAAfr////8////
1276 BgcAAAABMQEAAAAL";
1277                         var stream = new MemoryStream (Convert.FromBase64String (data));
1278                         var fmt = new BinaryFormatter ();
1279                         var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1280                         Assert.AreEqual (2, dict.Count);
1281                         for (int i = 0; i < dict.Count; i++)
1282                                 Assert.AreEqual (i, dict[i.ToString ()]);
1283                 }
1284         }
1285 }
1286