2 // MonoTests.System.Collections.Generic.DictionaryTest
5 // Sureshkumar T (tsureshkumar@novell.com)
6 // Ankit Jain (radical@corewars.org)
7 // David Waite (mass@akuma.org)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2005 David Waite (mass@akuma.org)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
35 using System.Collections;
36 using System.Collections.Generic;
38 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Threading;
42 using NUnit.Framework;
44 namespace MonoTests.System.Collections.Generic {
46 public class DictionaryTest {
50 public MyClass (int a, int b)
55 public override int GetHashCode ()
60 public override bool Equals (object obj)
62 if (!(obj is MyClass))
64 return ((MyClass)obj).Value == a;
74 Dictionary <string, object> _dictionary = null;
75 Dictionary <MyClass, MyClass> _dictionary2 = null;
76 Dictionary <int, int> _dictionary3 = null;
81 _dictionary = new Dictionary <string, object> ();
82 _dictionary2 = new Dictionary <MyClass, MyClass> ();
83 _dictionary3 = new Dictionary <int, int>();
87 public void AddTest ()
89 _dictionary.Add ("key1", "value");
90 Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
94 public void AddTest2 ()
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");
108 public void AddTest3 ()
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");
118 [Test, ExpectedException(typeof(ArgumentNullException))]
119 public void AddNullTest ()
121 _dictionary.Add (null, "");
124 [Test, ExpectedException(typeof(ArgumentException))]
125 public void AddDuplicateTest ()
127 _dictionary.Add("foo", "bar");
128 _dictionary.Add("foo", "bar");
131 //Tests Add when resize takes place
133 public void AddLargeTest ()
135 int i, numElems = 50;
137 for (i = 0; i < numElems; i++)
139 _dictionary3.Add (i, i);
143 foreach (KeyValuePair <int, int> entry in _dictionary3)
148 Assert.AreEqual (i, numElems, "Add with resize failed!");
152 public void IndexerGetExistingTest ()
154 _dictionary.Add ("key1", "value");
155 Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
158 [Test, ExpectedException(typeof(KeyNotFoundException))]
159 public void IndexerGetNonExistingTest ()
161 object foo = _dictionary ["foo"];
164 [Test, ExpectedException(typeof(ArgumentNullException))]
165 public void IndexerGetNullTest()
167 object s = _dictionary[null];
171 public void IndexerSetExistingTest ()
173 _dictionary.Add ("key1", "value1");
174 _dictionary ["key1"] = "value2";
175 Assert.AreEqual (1, _dictionary.Count);
176 Assert.AreEqual ("value2", _dictionary ["key1"]);
180 public void IndexerSetNonExistingTest ()
182 _dictionary ["key1"] = "value1";
183 Assert.AreEqual (1, _dictionary.Count);
184 Assert.AreEqual ("value1", _dictionary ["key1"]);
188 public void RemoveTest ()
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"));
201 public void RemoveTest2 ()
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");
215 [Category ("NotWorking")]
216 public void Remove_ZeroOut ()
218 object key = new object ();
219 object value = new object ();
221 WeakReference wrKey = new WeakReference (key);
222 WeakReference wrValue = new WeakReference (value);
224 Dictionary <object, object> dictionary = new Dictionary <object, object> ();
225 dictionary.Add (key, value);
226 dictionary.Remove (key);
233 Assert.IsNull (wrKey.Target, "#1");
234 Assert.IsNull (wrValue.Target, "#2");
237 [Test, ExpectedException(typeof(ArgumentNullException))]
238 public void IndexerSetNullTest()
240 _dictionary[null] = "bar";
244 public void ClearTest ()
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"));
256 public void Clear_Iterators ()
258 Dictionary<object, object> d = new Dictionary <object, object> ();
260 d [new object ()] = new object ();
263 foreach (object o in d) {
264 hash += o.GetHashCode ();
269 [Category ("NotWorking")]
270 public void Clear_ZeroOut ()
272 object key = new object ();
273 object value = new object ();
275 WeakReference wrKey = new WeakReference (key);
276 WeakReference wrValue = new WeakReference (value);
278 Dictionary <object, object> dictionary = new Dictionary <object, object> ();
279 dictionary.Add (key, value);
287 Assert.IsNull (wrKey.Target, "#1");
288 Assert.IsNull (wrValue.Target, "#2");
292 public void ContainsKeyTest ()
294 _dictionary.Add ("key1", "value1");
295 _dictionary.Add ("key2", "value2");
296 _dictionary.Add ("key3", "value3");
297 _dictionary.Add ("key4", "value4");
298 bool contains = _dictionary.ContainsKey ("key4");
299 Assert.IsTrue (contains, "ContainsKey does not return correct value!");
300 contains = _dictionary.ContainsKey ("key5");
301 Assert.IsFalse (contains, "ContainsKey for non existant does not return correct value!");
304 [Test, ExpectedException (typeof (ArgumentNullException))]
305 public void ContainsKeyTest2 ()
307 _dictionary.ContainsKey (null);
311 public void ContainsValueTest ()
313 _dictionary.Add ("key1", "value1");
314 _dictionary.Add ("key2", "value2");
315 _dictionary.Add ("key3", "value3");
316 _dictionary.Add ("key4", "value4");
317 bool contains = _dictionary.ContainsValue ("value2");
318 Assert.IsTrue(contains, "ContainsValue does not return correct value!");
319 contains = _dictionary.ContainsValue ("@@daisofja@@");
320 Assert.IsFalse (contains, "ContainsValue for non existant does not return correct value!");
324 public void TryGetValueTest()
326 _dictionary.Add ("key1", "value1");
327 _dictionary.Add ("key2", "value2");
328 _dictionary.Add ("key3", "value3");
329 _dictionary.Add ("key4", "value4");
331 bool retrieved = _dictionary.TryGetValue ("key4", out value);
332 Assert.IsTrue (retrieved);
333 Assert.AreEqual ("value4", (string)value, "TryGetValue does not return value!");
335 retrieved = _dictionary.TryGetValue ("key7", out value);
336 Assert.IsFalse (retrieved);
337 Assert.IsNull (value, "value for non existant value should be null!");
341 public void ValueTypeTest ()
343 Dictionary <int, float> dict = new Dictionary <int, float> ();
344 dict.Add (10, 10.3f);
345 dict.Add (11, 10.4f);
346 dict.Add (12, 10.5f);
347 Assert.AreEqual (10.4f, dict [11], "#5");
355 public MyTest (string name, int number)
361 public override int GetHashCode ()
363 return Name.GetHashCode () ^ RollNo;
366 public override bool Equals (object obj)
368 MyTest myt = obj as MyTest;
369 return myt.Name.Equals (this.Name) &&
370 myt.RollNo.Equals (this.RollNo);
375 public void ObjectAsKeyTest ()
377 Dictionary <object, object> dict = new Dictionary <object, object> ();
378 MyTest key1, key2, key3;
379 dict.Add ( (key1 = new MyTest ("key1", 234)), "value1");
380 dict.Add ( (key2 = new MyTest ("key2", 444)), "value2");
381 dict.Add ( (key3 = new MyTest ("key3", 5655)), "value3");
383 Assert.AreEqual ("value2", dict [key2], "value is not returned!");
384 Assert.AreEqual ("value3", dict [key3], "neg: exception should not be thrown!");
387 [Test, ExpectedException (typeof (ArgumentException))]
388 public void IDictionaryAddTest ()
390 IDictionary iDict = _dictionary as IDictionary;
391 iDict.Add ("key1", "value1");
392 iDict.Add ("key2", "value3");
393 Assert.AreEqual (2, iDict.Count, "IDictioanry interface add is not working!");
396 iDict.Add (12, "value");
397 iDict.Add ("key", 34);
401 public void IEnumeratorTest ()
403 _dictionary.Add ("key1", "value1");
404 _dictionary.Add ("key2", "value2");
405 _dictionary.Add ("key3", "value3");
406 _dictionary.Add ("key4", "value4");
407 IEnumerator itr = ((IEnumerable)_dictionary).GetEnumerator ();
408 while (itr.MoveNext ()) {
409 object o = itr.Current;
410 Assert.AreEqual (typeof (KeyValuePair<string,object>), o.GetType (), "Current should return a type of KeyValuePair");
411 KeyValuePair<string,object> entry = (KeyValuePair<string,object>) itr.Current;
413 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
418 public void IEnumeratorGenericTest ()
420 _dictionary.Add ("key1", "value1");
421 _dictionary.Add ("key2", "value2");
422 _dictionary.Add ("key3", "value3");
423 _dictionary.Add ("key4", "value4");
424 IEnumerator <KeyValuePair <string, object>> itr = ((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
425 while (itr.MoveNext ()) {
426 object o = itr.Current;
427 Assert.AreEqual (typeof (KeyValuePair <string, object>), o.GetType (), "Current should return a type of KeyValuePair<object,string>");
428 KeyValuePair <string, object> entry = (KeyValuePair <string, object>)itr.Current;
430 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
434 public void IDictionaryEnumeratorTest ()
436 _dictionary.Add ("key1", "value1");
437 _dictionary.Add ("key2", "value2");
438 _dictionary.Add ("key3", "value3");
439 _dictionary.Add ("key4", "value4");
440 IDictionaryEnumerator itr = ((IDictionary)_dictionary).GetEnumerator ();
441 while (itr.MoveNext ()) {
442 object o = itr.Current;
443 Assert.AreEqual (typeof (DictionaryEntry), o.GetType (), "Current should return a type of DictionaryEntry");
444 DictionaryEntry entry = (DictionaryEntry) itr.Current;
446 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
450 public void ForEachTest ()
452 _dictionary.Add ("key1", "value1");
453 _dictionary.Add ("key2", "value2");
454 _dictionary.Add ("key3", "value3");
455 _dictionary.Add ("key4", "value4");
458 foreach (KeyValuePair <string, object> entry in _dictionary)
460 Assert.AreEqual(4, i, "fail1: foreach entry failed!");
463 foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
465 Assert.AreEqual(4, i, "fail2: foreach entry failed!");
468 foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
470 Assert.AreEqual (4, i, "fail3: foreach entry failed!");
474 public void ResizeTest ()
476 Dictionary <string, object> dictionary = new Dictionary <string, object> (3);
477 dictionary.Add ("key1", "value1");
478 dictionary.Add ("key2", "value2");
479 dictionary.Add ("key3", "value3");
481 Assert.AreEqual (3, dictionary.Count);
483 dictionary.Add ("key4", "value4");
484 Assert.AreEqual (4, dictionary.Count);
485 Assert.AreEqual ("value1", dictionary ["key1"].ToString (), "");
486 Assert.AreEqual ("value2", dictionary ["key2"].ToString (), "");
487 Assert.AreEqual ("value4", dictionary ["key4"].ToString (), "");
488 Assert.AreEqual ("value3", dictionary ["key3"].ToString (), "");
492 public void KeyCollectionTest ()
494 _dictionary.Add ("key1", "value1");
495 _dictionary.Add ("key2", "value2");
496 _dictionary.Add ("key3", "value3");
497 _dictionary.Add ("key4", "value4");
499 ICollection <string> keys = ((IDictionary <string, object>)_dictionary).Keys;
500 Assert.AreEqual (4, keys.Count);
502 foreach (string key in keys)
506 Assert.AreEqual(4, i);
510 public void KeyValueEnumeratorTest ()
512 IDictionary<int, int> d = new Dictionary<int, int>();
514 // Values are chosen such that two keys map to the same bucket.
515 // Default dictionary table size == 10
520 Assert.AreEqual (d.Count, d.Keys.Count, "d and d.Keys don't appear to match");
521 Assert.AreEqual (d.Values.Count, d.Keys.Count, "d.Keys and d.Values don't appear to match");
524 foreach (int i in d.Values)
526 Assert.AreEqual (count, d.Values.Count, "d.Values doesn't have the correct number of elements");
529 foreach (int i in d.Keys)
531 Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't have the correct number of elements");
535 foreach (int i in d.Keys) {
537 if (count++ >= nkeys)
538 Assert.Fail ("Reading a value appears to trash enumerator state");
543 public void SliceCollectionsEnumeratorTest ()
545 Dictionary<string, int> values = new Dictionary<string, int> ();
547 IEnumerator <string> ke = values.Keys.GetEnumerator ();
548 IEnumerator <int> ve = values.Values.GetEnumerator ();
550 Assert.IsTrue (ke is Dictionary<string, int>.KeyCollection.Enumerator);
551 Assert.IsTrue (ve is Dictionary<string, int>.ValueCollection.Enumerator);
555 public void PlainEnumeratorReturnTest ()
557 // Test that we return a KeyValuePair even for non-generic dictionary iteration
558 _dictionary["foo"] = "bar";
559 IEnumerator<KeyValuePair<string, object>> enumerator = _dictionary.GetEnumerator();
560 Assert.IsTrue(enumerator.MoveNext(), "#1");
561 Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IEnumerator)enumerator).Current.GetType (), "#2");
562 Assert.AreEqual (typeof (DictionaryEntry), ((IDictionaryEnumerator)enumerator).Entry.GetType (), "#3");
563 Assert.AreEqual (typeof (KeyValuePair<string,object>), ((IDictionaryEnumerator)enumerator).Current.GetType (), "#4");
564 Assert.AreEqual (typeof (KeyValuePair<string,object>), ((object) enumerator.Current).GetType (), "#5");
567 [Test, ExpectedException (typeof (InvalidOperationException))]
568 public void FailFastTest1 ()
570 Dictionary<int, int> d = new Dictionary<int, int> ();
573 foreach (KeyValuePair<int, int> kv in d) {
574 d [kv.Key + 1] = kv.Value + 1;
576 Assert.Fail ("Should not be reached");
578 Assert.Fail ("Should not be reached");
581 [Test, ExpectedException (typeof (InvalidOperationException))]
582 public void FailFastTest2 ()
584 Dictionary<int, int> d = new Dictionary<int, int> ();
587 foreach (int i in d.Keys) {
590 Assert.Fail ("Should not be reached");
592 Assert.Fail ("Should not be reached");
595 [Test, ExpectedException (typeof (InvalidOperationException))]
596 public void FailFastTest3 ()
598 Dictionary<int, int> d = new Dictionary<int, int> ();
601 foreach (int i in d.Keys) {
604 Assert.Fail ("Should not be reached");
606 Assert.Fail ("Should not be reached");
610 [Category ("TargetJvmNotWorking")] // BUGBUG Very very slow on TARGET_JVM.
611 public void SerializationTest()
613 for (int i = 0; i < 50; i++)
615 _dictionary3.Add(i, i);
618 BinaryFormatter formatter = new BinaryFormatter();
619 MemoryStream stream = new MemoryStream();
620 formatter.Serialize(stream, _dictionary3);
623 object deserialized = formatter.Deserialize(stream);
625 Assert.IsNotNull(deserialized);
626 Assert.IsFalse(deserialized == _dictionary3);
628 Assert.IsTrue(deserialized is Dictionary<int, int>);
629 Dictionary<int, int> d3 = deserialized as Dictionary<int, int>;
631 Assert.AreEqual(50, d3.Count);
632 for (int i = 0; i < 50; i++)
634 Assert.AreEqual(i, d3[i]);
639 public void ZeroCapacity ()
641 Dictionary<int, int> x = new Dictionary <int, int> (0);
644 x = new Dictionary <int, int> (0);
647 x = new Dictionary <int, int> (0);
650 x = new Dictionary <int, int> (0);
653 } catch (KeyNotFoundException){
657 b = x.ContainsKey (10);
658 b = x.ContainsValue (10);
660 x = new Dictionary <int, int> (0);
663 x = new Dictionary <int, int> (0);
665 x.TryGetValue (1, out intv);
668 object ob = x.Values;
669 foreach (KeyValuePair<int,int> a in x){
674 public void Empty_KeysValues_CopyTo ()
676 Dictionary<int, int> d = new Dictionary<int, int> ();
677 int[] array = new int[1];
678 d.Keys.CopyTo (array, array.Length);
679 d.Values.CopyTo (array, array.Length);
683 public void Empty_CopyTo ()
685 Dictionary<int, int> d = new Dictionary<int, int> ();
686 ICollection c = (ICollection) d;
687 DictionaryEntry [] array = new DictionaryEntry [1];
688 c.CopyTo (array, array.Length);
690 ICollection<KeyValuePair<int,int>> c2 = d;
691 KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
692 c2.CopyTo (array2, array2.Length);
696 public void IDictionary_Contains ()
698 IDictionary d = new Dictionary<int, int> ();
700 Assert.IsTrue (d.Contains (1));
701 Assert.IsFalse (d.Contains (2));
702 Assert.IsFalse (d.Contains ("x"));
705 [Test, ExpectedException (typeof (ArgumentNullException))]
706 public void IDictionary_Contains2 ()
708 IDictionary d = new Dictionary<int, int> ();
712 [Test, ExpectedException (typeof (ArgumentNullException))]
713 public void IDictionary_Add1 ()
715 IDictionary d = new Dictionary<int, int> ();
719 [Test, ExpectedException (typeof (ArgumentException))]
720 public void IDictionary_Add2 ()
722 IDictionary d = new Dictionary<int, int> ();
726 [Test, ExpectedException (typeof (ArgumentException))]
727 public void IDictionary_Add3 ()
729 IDictionary d = new Dictionary<int, int> ();
734 public void IDictionary_Add_Null ()
736 IDictionary d = new Dictionary<int, string> ();
740 Assert.IsNull (d [1]);
741 Assert.IsNull (d [2]);
745 [ExpectedException (typeof (ArgumentException))]
746 public void IDictionary_Add_Null_2 ()
748 IDictionary d = new Dictionary<int, int> ();
753 public void IDictionary_Remove1 ()
755 IDictionary d = new Dictionary<int, int> ();
762 [Test, ExpectedException (typeof (ArgumentNullException))]
763 public void IDictionary_Remove2 ()
765 IDictionary d = new Dictionary<int, int> ();
770 public void IDictionary_IndexerGetNonExistingTest ()
772 IDictionary d = new Dictionary<int, int> ();
775 Assert.IsNull(d["foo"]);
778 [Test] // bug #332534
779 public void Dictionary_MoveNext ()
781 Dictionary<int,int> a = new Dictionary<int,int>();
785 IEnumerator en = a.GetEnumerator();
786 for (int i = 1; i < 10; i++)
791 public void CopyToArray ()
793 Dictionary<string, string> test = new Dictionary<string, string> ();
794 test.Add ("monkey", "singe");
795 test.Add ("singe", "mono");
796 test.Add ("mono", "monkey");
797 Assert.AreEqual (3, test.Keys.Count, "Dictionary.Count");
799 ArrayList list = new ArrayList (test.Keys);
800 Assert.AreEqual (3, list.Count, "ArrayList.Count");
801 Assert.IsTrue (list.Contains ("monkey"), "monkey");
802 Assert.IsTrue (list.Contains ("singe"), "singe");
803 Assert.IsTrue (list.Contains ("mono"), "mono");
807 public void KeyObjectMustNotGetChangedIfKeyAlreadyExists ()
809 Dictionary<String, int> d = new Dictionary<string, int> ();
811 string s2 = "Tes" + "T".ToLowerInvariant();
814 string comp = String.Empty;
815 foreach (String s in d.Keys)
817 Assert.IsTrue (Object.ReferenceEquals (s1, comp));
821 public void ResetKeysEnumerator ()
823 Dictionary<string, string> test = new Dictionary<string, string> ();
824 test.Add ("monkey", "singe");
825 test.Add ("singe", "mono");
826 test.Add ("mono", "monkey");
828 IEnumerator enumerator = test.Keys.GetEnumerator ();
830 Assert.IsTrue (enumerator.MoveNext ());
831 Assert.IsTrue (enumerator.MoveNext ());
835 Assert.IsTrue (enumerator.MoveNext ());
836 Assert.IsTrue (enumerator.MoveNext ());
837 Assert.IsTrue (enumerator.MoveNext ());
838 Assert.IsFalse (enumerator.MoveNext ());
842 public void ResetValuesEnumerator ()
844 Dictionary<string, string> test = new Dictionary<string, string> ();
845 test.Add ("monkey", "singe");
846 test.Add ("singe", "mono");
847 test.Add ("mono", "monkey");
849 IEnumerator enumerator = test.Values.GetEnumerator ();
851 Assert.IsTrue (enumerator.MoveNext ());
852 Assert.IsTrue (enumerator.MoveNext ());
856 Assert.IsTrue (enumerator.MoveNext ());
857 Assert.IsTrue (enumerator.MoveNext ());
858 Assert.IsTrue (enumerator.MoveNext ());
859 Assert.IsFalse (enumerator.MoveNext ());
863 public void ResetShimEnumerator ()
865 IDictionary test = new Dictionary<string, string> ();
866 test.Add ("monkey", "singe");
867 test.Add ("singe", "mono");
868 test.Add ("mono", "monkey");
870 IEnumerator enumerator = test.GetEnumerator ();
872 Assert.IsTrue (enumerator.MoveNext ());
873 Assert.IsTrue (enumerator.MoveNext ());
877 Assert.IsTrue (enumerator.MoveNext ());
878 Assert.IsTrue (enumerator.MoveNext ());
879 Assert.IsTrue (enumerator.MoveNext ());
880 Assert.IsFalse (enumerator.MoveNext ());
884 public void ICollectionOfKeyValuePairContains ()
886 var dictionary = new Dictionary<string, int> ();
887 dictionary.Add ("foo", 42);
888 dictionary.Add ("bar", 12);
890 var collection = dictionary as ICollection<KeyValuePair<string, int>>;
892 Assert.AreEqual (2, collection.Count);
894 Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("baz", 13)));
895 Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("foo", 13)));
896 Assert.IsTrue (collection.Contains (new KeyValuePair<string, int> ("foo", 42)));
900 public void ICollectionOfKeyValuePairRemove ()
902 var dictionary = new Dictionary<string, int> ();
903 dictionary.Add ("foo", 42);
904 dictionary.Add ("bar", 12);
906 var collection = dictionary as ICollection<KeyValuePair<string, int>>;
908 Assert.AreEqual (2, collection.Count);
910 Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("baz", 13)));
911 Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("foo", 13)));
912 Assert.IsTrue (collection.Remove (new KeyValuePair<string, int> ("foo", 42)));
914 Assert.AreEqual (12, dictionary ["bar"]);
915 Assert.IsFalse (dictionary.ContainsKey ("foo"));
919 public void ICollectionCopyToKeyValuePairArray ()
921 var dictionary = new Dictionary<string, int> ();
922 dictionary.Add ("foo", 42);
924 var collection = dictionary as ICollection;
926 Assert.AreEqual (1, collection.Count);
928 var pairs = new KeyValuePair<string, int> [1];
930 collection.CopyTo (pairs, 0);
932 Assert.AreEqual ("foo", pairs [0].Key);
933 Assert.AreEqual (42, pairs [0].Value);
937 public void ICollectionCopyToDictionaryEntryArray ()
939 var dictionary = new Dictionary<string, int> ();
940 dictionary.Add ("foo", 42);
942 var collection = dictionary as ICollection;
944 Assert.AreEqual (1, collection.Count);
946 var entries = new DictionaryEntry [1];
948 collection.CopyTo (entries, 0);
950 Assert.AreEqual ("foo", (string) entries [0].Key);
951 Assert.AreEqual (42, (int) entries [0].Value);
955 public void ICollectionCopyToObjectArray ()
957 var dictionary = new Dictionary<string, int> ();
958 dictionary.Add ("foo", 42);
960 var collection = dictionary as ICollection;
962 Assert.AreEqual (1, collection.Count);
964 var array = new object [1];
966 collection.CopyTo (array, 0);
968 var pair = (KeyValuePair<string, int>) array [0];
970 Assert.AreEqual ("foo", pair.Key);
971 Assert.AreEqual (42, pair.Value);
975 [ExpectedException (typeof (ArgumentException))]
976 public void ICollectionCopyToInvalidArray ()
978 var dictionary = new Dictionary<string, int> ();
979 dictionary.Add ("foo", 42);
981 var collection = dictionary as ICollection;
983 Assert.AreEqual (1, collection.Count);
985 var array = new int [1];
987 collection.CopyTo (array, 0);
991 public void ValuesCopyToObjectArray ()
993 var dictionary = new Dictionary<string, string> { { "foo", "bar" } };
995 var values = dictionary.Values as ICollection;
997 var array = new object [values.Count];
999 values.CopyTo (array, 0);
1001 Assert.AreEqual ("bar", array [0]);
1016 // based on #491858, #517415
1017 public void Enumerator_Current ()
1019 var e1 = new Dictionary<int,int>.Enumerator ();
1020 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1022 var d = new Dictionary<int,int> ();
1023 var e2 = d.GetEnumerator ();
1024 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1026 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1028 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1030 var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
1031 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1033 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1035 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1037 var e4 = ((IEnumerable) d).GetEnumerator ();
1038 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1040 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1041 ((IDisposable) e4).Dispose ();
1042 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1046 // based on #491858, #517415
1047 public void KeyEnumerator_Current ()
1049 var e1 = new Dictionary<int,int>.KeyCollection.Enumerator ();
1050 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1052 var d = new Dictionary<int,int> ().Keys;
1053 var e2 = d.GetEnumerator ();
1054 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1056 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1058 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1060 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1061 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1063 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1065 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1067 var e4 = ((IEnumerable) d).GetEnumerator ();
1068 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1070 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1071 ((IDisposable) e4).Dispose ();
1072 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1076 // based on #491858, #517415
1077 public void ValueEnumerator_Current ()
1079 var e1 = new Dictionary<int,int>.ValueCollection.Enumerator ();
1080 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1082 var d = new Dictionary<int,int> ().Values;
1083 var e2 = d.GetEnumerator ();
1084 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1086 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1088 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1090 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1091 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1093 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1095 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1097 var e4 = ((IEnumerable) d).GetEnumerator ();
1098 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1100 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1101 ((IDisposable) e4).Dispose ();
1102 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1106 public void ICollectionCopyTo ()
1108 var d = new Dictionary<int, string> ();
1111 c.CopyTo (new object [0], 0);
1112 c.CopyTo (new string [0], 0);
1113 c.CopyTo (new MyClass [0], 0);
1116 c.CopyTo (new object [0], 0);
1117 c.CopyTo (new ValueType [0], 0);
1120 c.CopyTo (new object [0], 0);
1121 c.CopyTo (new MyClass [0], 0);
1126 c.CopyTo (new object [1], 0);
1127 c.CopyTo (new ValueType [1], 0);
1130 c.CopyTo (new object [1], 0);
1131 c.CopyTo (new MyClass [1], 0);
1134 [Test, ExpectedException (typeof (ArgumentException))]
1135 public void ICollectionCopyTo_ex1 ()
1137 var d = new Dictionary<int, string> ();
1138 ICollection c = d.Keys;
1139 c.CopyTo (new string [1], 0);
1142 [Test, ExpectedException (typeof (ArgumentException))]
1143 public void ICollectionCopyTo_ex2 ()
1145 var d = new Dictionary<int, string> ();
1146 ICollection c = d.Values;
1147 c.CopyTo (new int [1], 0);
1150 [Test, ExpectedException (typeof (ArgumentException))]
1151 public void ICollectionCopyTo_ex3 ()
1153 var d = new Dictionary<int, string> ();
1156 ICollection c = d.Keys;
1157 c.CopyTo (new MyClass [1], 0);
1160 [Test, ExpectedException (typeof (ArgumentException))]
1161 public void ICollectionCopyTo_ex4 ()
1163 var d = new Dictionary<int, string> ();
1166 ICollection c = d.Values;
1167 c.CopyTo (new MyClass [1], 0);
1170 [Test] // bug 474009
1171 public void DeserializeEmptyDictionary ()
1173 // contains a Dictionary<string, int> with Count = 0
1174 // serialized with MS.NET 3.5
1176 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1177 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1178 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1179 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1180 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAwAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplAAMACJIB
1181 U3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5Q29tcGFyZXJgMVtbU3lz
1182 dGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQ
1183 dWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0IAAAAAAkCAAAAAAAAAAQCAAAAkgFTeXN0
1184 ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5HZW5lcmljRXF1YWxpdHlDb21wYXJlcmAxW1tTeXN0ZW0u
1185 U3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1Ymxp
1186 Y0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQAAAAAL";
1188 var stream = new MemoryStream (Convert.FromBase64String (data));
1189 var fmt = new BinaryFormatter ();
1190 var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1191 Assert.AreEqual (0, dict.Count);
1195 public void DeserializeNonEmptyDictionary ()
1197 // contains a Dictionary<string, int> with Count = 2
1198 // and dict [i.ToString()] == i for each i.
1199 // serialized with MS.NET 3.5
1201 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1202 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1203 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1204 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1205 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dBAAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplDUtleVZh
1206 bHVlUGFpcnMAAwADCJIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5
1207 Q29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3Vs
1208 dHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0I5QFTeXN0ZW0u
1209 Q29sbGVjdGlvbnMuR2VuZXJpYy5LZXlWYWx1ZVBhaXJgMltbU3lzdGVtLlN0cmluZywgbXNjb3Js
1210 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1211 NWM1NjE5MzRlMDg5XSxbU3lzdGVtLkludDMyLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBD
1212 dWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXVtdAgAAAAkC
1213 AAAAAwAAAAkDAAAABAIAAACSAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkdlbmVyaWNFcXVh
1214 bGl0eUNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAs
1215 IEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAAAAAAcD
1216 AAAAAAEAAAACAAAAA+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQYWlyYDJb
1217 W1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJh
1218 bCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwgbXNjb3Js
1219 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1220 NWM1NjE5MzRlMDg5XV0E/P///+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQ
1221 YWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9
1222 bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwg
1223 bXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tl
1224 bj1iNzdhNWM1NjE5MzRlMDg5XV0CAAAAA2tleQV2YWx1ZQEACAYFAAAAATAAAAAAAfr////8////
1226 var stream = new MemoryStream (Convert.FromBase64String (data));
1227 var fmt = new BinaryFormatter ();
1228 var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1229 Assert.AreEqual (2, dict.Count);
1230 for (int i = 0; i < dict.Count; i++)
1231 Assert.AreEqual (i, dict[i.ToString ()]);