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.
34 using System.Collections;
35 using System.Collections.Generic;
37 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Threading;
41 using NUnit.Framework;
43 namespace MonoTests.System.Collections.Generic {
45 public class DictionaryTest {
49 public MyClass (int a, int b)
54 public override int GetHashCode ()
59 public override bool Equals (object obj)
61 if (!(obj is MyClass))
63 return ((MyClass)obj).Value == a;
73 Dictionary <string, object> _dictionary = null;
74 Dictionary <MyClass, MyClass> _dictionary2 = null;
75 Dictionary <int, int> _dictionary3 = null;
80 _dictionary = new Dictionary <string, object> ();
81 _dictionary2 = new Dictionary <MyClass, MyClass> ();
82 _dictionary3 = new Dictionary <int, int>();
86 public void AddTest ()
88 _dictionary.Add ("key1", "value");
89 Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
93 public void AddTest2 ()
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");
107 public void AddTest3 ()
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");
117 [Test, ExpectedException(typeof(ArgumentNullException))]
118 public void AddNullTest ()
120 _dictionary.Add (null, "");
123 [Test, ExpectedException(typeof(ArgumentException))]
124 public void AddDuplicateTest ()
126 _dictionary.Add("foo", "bar");
127 _dictionary.Add("foo", "bar");
130 //Tests Add when resize takes place
132 public void AddLargeTest ()
134 int i, numElems = 50;
136 for (i = 0; i < numElems; i++)
138 _dictionary3.Add (i, i);
142 foreach (KeyValuePair <int, int> entry in _dictionary3)
147 Assert.AreEqual (i, numElems, "Add with resize failed!");
151 public void IndexerGetExistingTest ()
153 _dictionary.Add ("key1", "value");
154 Assert.AreEqual ("value", _dictionary ["key1"].ToString (), "Add failed!");
157 [Test, ExpectedException(typeof(KeyNotFoundException))]
158 public void IndexerGetNonExistingTest ()
160 object foo = _dictionary ["foo"];
163 [Test, ExpectedException(typeof(ArgumentNullException))]
164 public void IndexerGetNullTest()
166 object s = _dictionary[null];
170 public void IndexerSetExistingTest ()
172 _dictionary.Add ("key1", "value1");
173 _dictionary ["key1"] = "value2";
174 Assert.AreEqual (1, _dictionary.Count);
175 Assert.AreEqual ("value2", _dictionary ["key1"]);
179 public void IndexerSetNonExistingTest ()
181 _dictionary ["key1"] = "value1";
182 Assert.AreEqual (1, _dictionary.Count);
183 Assert.AreEqual ("value1", _dictionary ["key1"]);
187 public void RemoveTest ()
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"));
200 public void RemoveTest2 ()
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");
214 [Category ("NotWorking")]
215 public void Remove_ZeroOut ()
217 object key = new object ();
218 object value = new object ();
220 WeakReference wrKey = new WeakReference (key);
221 WeakReference wrValue = new WeakReference (value);
223 Dictionary <object, object> dictionary = new Dictionary <object, object> ();
224 dictionary.Add (key, value);
225 dictionary.Remove (key);
232 Assert.IsNull (wrKey.Target, "#1");
233 Assert.IsNull (wrValue.Target, "#2");
236 [Test, ExpectedException(typeof(ArgumentNullException))]
237 public void IndexerSetNullTest()
239 _dictionary[null] = "bar";
243 public void ClearTest ()
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"));
255 public void Clear_Iterators ()
257 Dictionary<object, object> d = new Dictionary <object, object> ();
259 d [new object ()] = new object ();
262 foreach (object o in d) {
263 hash += o.GetHashCode ();
268 [Category ("NotWorking")]
269 public void Clear_ZeroOut ()
271 object key = new object ();
272 object value = new object ();
274 WeakReference wrKey = new WeakReference (key);
275 WeakReference wrValue = new WeakReference (value);
277 Dictionary <object, object> dictionary = new Dictionary <object, object> ();
278 dictionary.Add (key, value);
286 Assert.IsNull (wrKey.Target, "#1");
287 Assert.IsNull (wrValue.Target, "#2");
291 public void ContainsKeyTest ()
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!");
303 [Test, ExpectedException (typeof (ArgumentNullException))]
304 public void ContainsKeyTest2 ()
306 _dictionary.ContainsKey (null);
310 public void ContainsValueTest ()
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!");
323 public void TryGetValueTest()
325 _dictionary.Add ("key1", "value1");
326 _dictionary.Add ("key2", "value2");
327 _dictionary.Add ("key3", "value3");
328 _dictionary.Add ("key4", "value4");
330 bool retrieved = _dictionary.TryGetValue ("key4", out value);
331 Assert.IsTrue (retrieved);
332 Assert.AreEqual ("value4", (string)value, "TryGetValue does not return value!");
334 retrieved = _dictionary.TryGetValue ("key7", out value);
335 Assert.IsFalse (retrieved);
336 Assert.IsNull (value, "value for non existant value should be null!");
340 public void ValueTypeTest ()
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");
354 public MyTest (string name, int number)
360 public override int GetHashCode ()
362 return Name.GetHashCode () ^ RollNo;
365 public override bool Equals (object obj)
367 MyTest myt = obj as MyTest;
368 return myt.Name.Equals (this.Name) &&
369 myt.RollNo.Equals (this.RollNo);
374 public void ObjectAsKeyTest ()
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");
382 Assert.AreEqual ("value2", dict [key2], "value is not returned!");
383 Assert.AreEqual ("value3", dict [key3], "neg: exception should not be thrown!");
386 [Test, ExpectedException (typeof (ArgumentException))]
387 public void IDictionaryAddTest ()
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!");
395 iDict.Add (12, "value");
396 iDict.Add ("key", 34);
400 public void IEnumeratorTest ()
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;
412 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
417 public void IEnumeratorGenericTest ()
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;
429 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
433 public void IDictionaryEnumeratorTest ()
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;
445 Assert.AreEqual ("value4", _dictionary ["key4"].ToString (), "");
449 public void ForEachTest ()
451 _dictionary.Add ("key1", "value1");
452 _dictionary.Add ("key2", "value2");
453 _dictionary.Add ("key3", "value3");
454 _dictionary.Add ("key4", "value4");
457 foreach (KeyValuePair <string, object> entry in _dictionary)
459 Assert.AreEqual(4, i, "fail1: foreach entry failed!");
462 foreach (KeyValuePair <string, object> entry in ((IEnumerable)_dictionary))
464 Assert.AreEqual(4, i, "fail2: foreach entry failed!");
467 foreach (DictionaryEntry entry in ((IDictionary)_dictionary))
469 Assert.AreEqual (4, i, "fail3: foreach entry failed!");
473 public void ResizeTest ()
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");
480 Assert.AreEqual (3, dictionary.Count);
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 (), "");
491 public void KeyCollectionTest ()
493 _dictionary.Add ("key1", "value1");
494 _dictionary.Add ("key2", "value2");
495 _dictionary.Add ("key3", "value3");
496 _dictionary.Add ("key4", "value4");
498 ICollection <string> keys = ((IDictionary <string, object>)_dictionary).Keys;
499 Assert.AreEqual (4, keys.Count);
501 foreach (string key in keys)
505 Assert.AreEqual(4, i);
509 public void KeyValueEnumeratorTest ()
511 IDictionary<int, int> d = new Dictionary<int, int>();
513 // Values are chosen such that two keys map to the same bucket.
514 // Default dictionary table size == 10
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");
523 foreach (int i in d.Values)
525 Assert.AreEqual (count, d.Values.Count, "d.Values doesn't have the correct number of elements");
528 foreach (int i in d.Keys)
530 Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't have the correct number of elements");
534 foreach (int i in d.Keys) {
536 if (count++ >= nkeys)
537 Assert.Fail ("Reading a value appears to trash enumerator state");
542 public void SliceCollectionsEnumeratorTest ()
544 Dictionary<string, int> values = new Dictionary<string, int> ();
546 IEnumerator <string> ke = values.Keys.GetEnumerator ();
547 IEnumerator <int> ve = values.Values.GetEnumerator ();
549 Assert.IsTrue (ke is Dictionary<string, int>.KeyCollection.Enumerator);
550 Assert.IsTrue (ve is Dictionary<string, int>.ValueCollection.Enumerator);
554 public void PlainEnumeratorReturnTest ()
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");
566 [Test, ExpectedException (typeof (InvalidOperationException))]
567 public void FailFastTest1 ()
569 Dictionary<int, int> d = new Dictionary<int, int> ();
572 foreach (KeyValuePair<int, int> kv in d) {
573 d [kv.Key + 1] = kv.Value + 1;
575 Assert.Fail ("Should not be reached");
577 Assert.Fail ("Should not be reached");
580 [Test, ExpectedException (typeof (InvalidOperationException))]
581 public void FailFastTest2 ()
583 Dictionary<int, int> d = new Dictionary<int, int> ();
586 foreach (int i in d.Keys) {
589 Assert.Fail ("Should not be reached");
591 Assert.Fail ("Should not be reached");
594 [Test, ExpectedException (typeof (InvalidOperationException))]
595 public void FailFastTest3 ()
597 Dictionary<int, int> d = new Dictionary<int, int> ();
600 foreach (int i in d.Keys) {
603 Assert.Fail ("Should not be reached");
605 Assert.Fail ("Should not be reached");
609 public void SerializationTest()
611 for (int i = 0; i < 50; i++)
613 _dictionary3.Add(i, i);
616 BinaryFormatter formatter = new BinaryFormatter();
617 MemoryStream stream = new MemoryStream();
618 formatter.Serialize(stream, _dictionary3);
621 object deserialized = formatter.Deserialize(stream);
623 Assert.IsNotNull(deserialized);
624 Assert.IsFalse(deserialized == _dictionary3);
626 Assert.IsTrue(deserialized is Dictionary<int, int>);
627 Dictionary<int, int> d3 = deserialized as Dictionary<int, int>;
629 Assert.AreEqual(50, d3.Count);
630 for (int i = 0; i < 50; i++)
632 Assert.AreEqual(i, d3[i]);
637 public void ZeroCapacity ()
639 Dictionary<int, int> x = new Dictionary <int, int> (0);
642 x = new Dictionary <int, int> (0);
645 x = new Dictionary <int, int> (0);
648 x = new Dictionary <int, int> (0);
651 } catch (KeyNotFoundException){
655 b = x.ContainsKey (10);
656 b = x.ContainsValue (10);
658 x = new Dictionary <int, int> (0);
661 x = new Dictionary <int, int> (0);
663 x.TryGetValue (1, out intv);
666 object ob = x.Values;
667 foreach (KeyValuePair<int,int> a in x){
672 public void Empty_KeysValues_CopyTo ()
674 Dictionary<int, int> d = new Dictionary<int, int> ();
675 int[] array = new int[1];
676 d.Keys.CopyTo (array, array.Length);
677 d.Values.CopyTo (array, array.Length);
681 public void Empty_CopyTo ()
683 Dictionary<int, int> d = new Dictionary<int, int> ();
684 ICollection c = (ICollection) d;
685 DictionaryEntry [] array = new DictionaryEntry [1];
686 c.CopyTo (array, array.Length);
688 ICollection<KeyValuePair<int,int>> c2 = d;
689 KeyValuePair<int,int> [] array2 = new KeyValuePair<int,int> [1];
690 c2.CopyTo (array2, array2.Length);
694 public void IDictionary_Contains ()
696 IDictionary d = new Dictionary<int, int> ();
698 Assert.IsTrue (d.Contains (1));
699 Assert.IsFalse (d.Contains (2));
700 Assert.IsFalse (d.Contains ("x"));
703 [Test, ExpectedException (typeof (ArgumentNullException))]
704 public void IDictionary_Contains2 ()
706 IDictionary d = new Dictionary<int, int> ();
710 [Test, ExpectedException (typeof (ArgumentNullException))]
711 public void IDictionary_Add1 ()
713 IDictionary d = new Dictionary<int, int> ();
717 [Test, ExpectedException (typeof (ArgumentException))]
718 public void IDictionary_Add2 ()
720 IDictionary d = new Dictionary<int, int> ();
724 [Test, ExpectedException (typeof (ArgumentException))]
725 public void IDictionary_Add3 ()
727 IDictionary d = new Dictionary<int, int> ();
732 public void IDictionary_Add_Null ()
734 IDictionary d = new Dictionary<int, string> ();
738 Assert.IsNull (d [1]);
739 Assert.IsNull (d [2]);
743 [ExpectedException (typeof (ArgumentNullException))]
744 public void IDictionary_Add_Null_2 ()
746 IDictionary d = new Dictionary<int, int> ();
751 public void IDictionary_Remove1 ()
753 IDictionary d = new Dictionary<int, int> ();
760 [Test, ExpectedException (typeof (ArgumentNullException))]
761 public void IDictionary_Remove2 ()
763 IDictionary d = new Dictionary<int, int> ();
768 public void IDictionary_IndexerGetNonExistingTest ()
770 IDictionary d = new Dictionary<int, int> ();
773 Assert.IsNull(d["foo"]);
776 [Test] // bug #332534
777 public void Dictionary_MoveNext ()
779 Dictionary<int,int> a = new Dictionary<int,int>();
783 IEnumerator en = a.GetEnumerator();
784 for (int i = 1; i < 10; i++)
789 public void CopyToArray ()
791 Dictionary<string, string> test = new Dictionary<string, string> ();
792 test.Add ("monkey", "singe");
793 test.Add ("singe", "mono");
794 test.Add ("mono", "monkey");
795 Assert.AreEqual (3, test.Keys.Count, "Dictionary.Count");
797 ArrayList list = new ArrayList (test.Keys);
798 Assert.AreEqual (3, list.Count, "ArrayList.Count");
799 Assert.IsTrue (list.Contains ("monkey"), "monkey");
800 Assert.IsTrue (list.Contains ("singe"), "singe");
801 Assert.IsTrue (list.Contains ("mono"), "mono");
805 public void KeyObjectMustNotGetChangedIfKeyAlreadyExists ()
807 Dictionary<String, int> d = new Dictionary<string, int> ();
809 string s2 = "Tes" + "T".ToLowerInvariant();
812 string comp = String.Empty;
813 foreach (String s in d.Keys)
815 Assert.IsTrue (Object.ReferenceEquals (s1, comp));
819 public void ResetKeysEnumerator ()
821 Dictionary<string, string> test = new Dictionary<string, string> ();
822 test.Add ("monkey", "singe");
823 test.Add ("singe", "mono");
824 test.Add ("mono", "monkey");
826 IEnumerator enumerator = test.Keys.GetEnumerator ();
828 Assert.IsTrue (enumerator.MoveNext ());
829 Assert.IsTrue (enumerator.MoveNext ());
833 Assert.IsTrue (enumerator.MoveNext ());
834 Assert.IsTrue (enumerator.MoveNext ());
835 Assert.IsTrue (enumerator.MoveNext ());
836 Assert.IsFalse (enumerator.MoveNext ());
840 public void ResetValuesEnumerator ()
842 Dictionary<string, string> test = new Dictionary<string, string> ();
843 test.Add ("monkey", "singe");
844 test.Add ("singe", "mono");
845 test.Add ("mono", "monkey");
847 IEnumerator enumerator = test.Values.GetEnumerator ();
849 Assert.IsTrue (enumerator.MoveNext ());
850 Assert.IsTrue (enumerator.MoveNext ());
854 Assert.IsTrue (enumerator.MoveNext ());
855 Assert.IsTrue (enumerator.MoveNext ());
856 Assert.IsTrue (enumerator.MoveNext ());
857 Assert.IsFalse (enumerator.MoveNext ());
861 public void ResetShimEnumerator ()
863 IDictionary test = new Dictionary<string, string> ();
864 test.Add ("monkey", "singe");
865 test.Add ("singe", "mono");
866 test.Add ("mono", "monkey");
868 IEnumerator enumerator = test.GetEnumerator ();
870 Assert.IsTrue (enumerator.MoveNext ());
871 Assert.IsTrue (enumerator.MoveNext ());
875 Assert.IsTrue (enumerator.MoveNext ());
876 Assert.IsTrue (enumerator.MoveNext ());
877 Assert.IsTrue (enumerator.MoveNext ());
878 Assert.IsFalse (enumerator.MoveNext ());
882 public void ICollectionOfKeyValuePairContains ()
884 var dictionary = new Dictionary<string, int> ();
885 dictionary.Add ("foo", 42);
886 dictionary.Add ("bar", 12);
888 var collection = dictionary as ICollection<KeyValuePair<string, int>>;
890 Assert.AreEqual (2, collection.Count);
892 Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("baz", 13)));
893 Assert.IsFalse (collection.Contains (new KeyValuePair<string, int> ("foo", 13)));
894 Assert.IsTrue (collection.Contains (new KeyValuePair<string, int> ("foo", 42)));
898 public void ICollectionOfKeyValuePairRemove ()
900 var dictionary = new Dictionary<string, int> ();
901 dictionary.Add ("foo", 42);
902 dictionary.Add ("bar", 12);
904 var collection = dictionary as ICollection<KeyValuePair<string, int>>;
906 Assert.AreEqual (2, collection.Count);
908 Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("baz", 13)));
909 Assert.IsFalse (collection.Remove (new KeyValuePair<string, int> ("foo", 13)));
910 Assert.IsTrue (collection.Remove (new KeyValuePair<string, int> ("foo", 42)));
912 Assert.AreEqual (12, dictionary ["bar"]);
913 Assert.IsFalse (dictionary.ContainsKey ("foo"));
917 public void ICollectionCopyToKeyValuePairArray ()
919 var dictionary = new Dictionary<string, int> ();
920 dictionary.Add ("foo", 42);
922 var collection = dictionary as ICollection;
924 Assert.AreEqual (1, collection.Count);
926 var pairs = new KeyValuePair<string, int> [1];
928 collection.CopyTo (pairs, 0);
930 Assert.AreEqual ("foo", pairs [0].Key);
931 Assert.AreEqual (42, pairs [0].Value);
935 public void ICollectionCopyToDictionaryEntryArray ()
937 var dictionary = new Dictionary<string, int> ();
938 dictionary.Add ("foo", 42);
940 var collection = dictionary as ICollection;
942 Assert.AreEqual (1, collection.Count);
944 var entries = new DictionaryEntry [1];
946 collection.CopyTo (entries, 0);
948 Assert.AreEqual ("foo", (string) entries [0].Key);
949 Assert.AreEqual (42, (int) entries [0].Value);
953 public void ICollectionCopyToObjectArray ()
955 var dictionary = new Dictionary<string, int> ();
956 dictionary.Add ("foo", 42);
958 var collection = dictionary as ICollection;
960 Assert.AreEqual (1, collection.Count);
962 var array = new object [1];
964 collection.CopyTo (array, 0);
966 var pair = (KeyValuePair<string, int>) array [0];
968 Assert.AreEqual ("foo", pair.Key);
969 Assert.AreEqual (42, pair.Value);
973 [ExpectedException (typeof (ArgumentException))]
974 public void ICollectionCopyToInvalidArray ()
976 var dictionary = new Dictionary<string, int> ();
977 dictionary.Add ("foo", 42);
979 var collection = dictionary as ICollection;
981 Assert.AreEqual (1, collection.Count);
983 var array = new int [1];
985 collection.CopyTo (array, 0);
989 public void ValuesCopyToObjectArray ()
991 var dictionary = new Dictionary<string, string> { { "foo", "bar" } };
993 var values = dictionary.Values as ICollection;
995 var array = new object [values.Count];
997 values.CopyTo (array, 0);
999 Assert.AreEqual ("bar", array [0]);
1014 // based on #491858, #517415
1015 public void Enumerator_Current ()
1017 var e1 = new Dictionary<int,int>.Enumerator ();
1018 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1020 var d = new Dictionary<int,int> ();
1021 var e2 = d.GetEnumerator ();
1022 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1024 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1026 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1028 var e3 = ((IEnumerable<KeyValuePair<int,int>>) d).GetEnumerator ();
1029 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1031 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1033 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1035 var e4 = ((IEnumerable) d).GetEnumerator ();
1036 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1038 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1039 ((IDisposable) e4).Dispose ();
1040 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1044 // based on #491858, #517415
1045 public void KeyEnumerator_Current ()
1047 var e1 = new Dictionary<int,int>.KeyCollection.Enumerator ();
1048 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1050 var d = new Dictionary<int,int> ().Keys;
1051 var e2 = d.GetEnumerator ();
1052 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1054 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1056 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1058 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1059 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1061 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1063 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1065 var e4 = ((IEnumerable) d).GetEnumerator ();
1066 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1068 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1069 ((IDisposable) e4).Dispose ();
1070 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1074 // based on #491858, #517415
1075 public void ValueEnumerator_Current ()
1077 var e1 = new Dictionary<int,int>.ValueCollection.Enumerator ();
1078 Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
1080 var d = new Dictionary<int,int> ().Values;
1081 var e2 = d.GetEnumerator ();
1082 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1084 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1086 Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
1088 var e3 = ((IEnumerable<int>) d).GetEnumerator ();
1089 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1091 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1093 Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
1095 var e4 = ((IEnumerable) d).GetEnumerator ();
1096 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1098 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1099 ((IDisposable) e4).Dispose ();
1100 Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
1104 public void ICollectionCopyTo ()
1106 var d = new Dictionary<int, string> ();
1109 c.CopyTo (new object [0], 0);
1110 c.CopyTo (new string [0], 0);
1111 c.CopyTo (new MyClass [0], 0);
1114 c.CopyTo (new object [0], 0);
1115 c.CopyTo (new ValueType [0], 0);
1118 c.CopyTo (new object [0], 0);
1119 c.CopyTo (new MyClass [0], 0);
1124 c.CopyTo (new object [1], 0);
1125 c.CopyTo (new ValueType [1], 0);
1128 c.CopyTo (new object [1], 0);
1129 c.CopyTo (new MyClass [1], 0);
1132 public void ICollectionCopyTo_ex1 ()
1134 var d = new Dictionary<int, string> ();
1135 ICollection c = d.Keys;
1136 c.CopyTo (new string [1], 0);
1139 [Test, ExpectedException (typeof (ArgumentException))]
1140 public void ICollectionCopyTo_ex2 ()
1142 var d = new Dictionary<int, string> ();
1143 ICollection c = d.Values;
1144 c.CopyTo (new int [1], 0);
1147 [Test, ExpectedException (typeof (ArgumentException))]
1148 public void ICollectionCopyTo_ex3 ()
1150 var d = new Dictionary<int, string> ();
1153 ICollection c = d.Keys;
1154 c.CopyTo (new MyClass [1], 0);
1157 [Test, ExpectedException (typeof (ArgumentException))]
1158 public void ICollectionCopyTo_ex4 ()
1160 var d = new Dictionary<int, string> ();
1163 ICollection c = d.Values;
1164 c.CopyTo (new MyClass [1], 0);
1167 [Test] // bug 474009
1168 public void DeserializeEmptyDictionary ()
1170 // contains a Dictionary<string, int> with Count = 0
1171 // serialized with MS.NET 3.5
1173 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1174 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1175 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1176 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1177 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAwAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplAAMACJIB
1178 U3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5Q29tcGFyZXJgMVtbU3lz
1179 dGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQ
1180 dWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0IAAAAAAkCAAAAAAAAAAQCAAAAkgFTeXN0
1181 ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYy5HZW5lcmljRXF1YWxpdHlDb21wYXJlcmAxW1tTeXN0ZW0u
1182 U3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1Ymxp
1183 Y0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQAAAAAL";
1185 var stream = new MemoryStream (Convert.FromBase64String (data));
1186 var fmt = new BinaryFormatter ();
1187 var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1188 Assert.AreEqual (0, dict.Count);
1192 public void DeserializeNonEmptyDictionary ()
1194 // contains a Dictionary<string, int> with Count = 2
1195 // and dict [i.ToString()] == i for each i.
1196 // serialized with MS.NET 3.5
1198 @"AAEAAAD/////AQAAAAAAAAAEAQAAAOEBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlv
1199 bmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBDdWx0dXJl
1200 PW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uSW50MzIs
1201 IG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9r
1202 ZW49Yjc3YTVjNTYxOTM0ZTA4OV1dBAAAAAdWZXJzaW9uCENvbXBhcmVyCEhhc2hTaXplDUtleVZh
1203 bHVlUGFpcnMAAwADCJIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5
1204 Q29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3Vs
1205 dHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0I5QFTeXN0ZW0u
1206 Q29sbGVjdGlvbnMuR2VuZXJpYy5LZXlWYWx1ZVBhaXJgMltbU3lzdGVtLlN0cmluZywgbXNjb3Js
1207 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1208 NWM1NjE5MzRlMDg5XSxbU3lzdGVtLkludDMyLCBtc2NvcmxpYiwgVmVyc2lvbj0yLjAuMC4wLCBD
1209 dWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXVtdAgAAAAkC
1210 AAAAAwAAAAkDAAAABAIAAACSAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkdlbmVyaWNFcXVh
1211 bGl0eUNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAs
1212 IEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAAAAAAcD
1213 AAAAAAEAAAACAAAAA+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQYWlyYDJb
1214 W1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9bmV1dHJh
1215 bCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwgbXNjb3Js
1216 aWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdh
1217 NWM1NjE5MzRlMDg5XV0E/P///+MBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQ
1218 YWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTIuMC4wLjAsIEN1bHR1cmU9
1219 bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5JbnQzMiwg
1220 bXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tl
1221 bj1iNzdhNWM1NjE5MzRlMDg5XV0CAAAAA2tleQV2YWx1ZQEACAYFAAAAATAAAAAAAfr////8////
1223 var stream = new MemoryStream (Convert.FromBase64String (data));
1224 var fmt = new BinaryFormatter ();
1225 var dict = (Dictionary <string, int>) fmt.Deserialize (stream);
1226 Assert.AreEqual (2, dict.Count);
1227 for (int i = 0; i < dict.Count; i++)
1228 Assert.AreEqual (i, dict[i.ToString ()]);