示例#1
0
        public void WeakKeyDictionary_SetUsingItemUsingObject_AddsItem()
        {
            IDictionary dictionary = new WeakKeyDictionary <object, object>();
            object      key        = new DisposableTestObject("howdy");
            object      value      = "doody";

            dictionary[key] = value;

            Assert.Equal(value, dictionary[key]);
        }
示例#2
0
 public void WeakKeyDictionary_AddEntry_ContainsKeyValuePairFails()
 {
     using (var key = new DisposableTestObject("Curly"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.False(dictionary.Contains(new KeyValuePair <DisposableTestObject, int>(key, -3)));
     }
 }
示例#3
0
 public void WeakKeyDictionary_AddEntry_ContainsKeyAsObjectSucceeds()
 {
     using (var key = new DisposableTestObject("Shemp"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.True(dictionary.Contains((object)key));
     }
 }
示例#4
0
 public void WeakKeyDictionary_RemoveKeyValuePair_EntryNotRemoved()
 {
     using (var key = new DisposableTestObject("Just kidding!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.False(dictionary.Remove(new KeyValuePair <DisposableTestObject, int>(key, 54)));
         Assert.True(dictionary.ContainsKey(key));
     }
 }
示例#5
0
 public void WeakKeyDictionary_RemoveKey_EntryRemoved()
 {
     using (var key = new DisposableTestObject("That's it, I'm outta here!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.True(dictionary.Remove(key));
         Assert.False(dictionary.ContainsKey(key));
     }
 }
示例#6
0
 public void WeakKeyDictionary_RemoveKeyAsObject_EntryRemoved()
 {
     using (var key = new DisposableTestObject("Remove Me, too!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         dictionary.Remove((object)key);
         Assert.False(dictionary.ContainsKey(key));
     }
 }
示例#7
0
 public void WeakKeyDictionary_AddDuplicateEntry_DuplicateNotAdded()
 {
     using (var key = new DisposableTestObject("dontDupMeBro"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.True(dictionary.ContainsKey(key));
         Assert.False(dictionary.AddEntry(key, -1));
     }
 }
示例#8
0
        public void WeakKeyDictionary_GetUsingItemUsingObject_GetsItem()
        {
            var    dictionary = new WeakKeyDictionary <object, object>();
            var    key        = new DisposableTestObject("howdy, pardner");
            object value      = "how's it goin'";

            Assert.True(dictionary.AddEntry(key, value));

            var valueOut = dictionary[key];

            Assert.Equal(value, valueOut);
        }
示例#9
0
        public void WeakKeyDictionary_GetUsingItem_GetsItem()
        {
            var          dictionary = new WeakKeyDictionary <DisposableTestObject, string>();
            var          key        = new DisposableTestObject("howdy, pardner");
            const string Value      = "how's it goin'";

            Assert.True(dictionary.AddEntry(key, Value));

            var value = dictionary[key];

            Assert.Equal(Value, value);
        }
示例#10
0
        public void WeakKeyDictionary_SetUsingItem_AddsItem()
        {
            var          dictionary = new WeakKeyDictionary <DisposableTestObject, string>();
            var          key        = new DisposableTestObject("howdy");
            const string Value      = "doody";

            dictionary[key] = Value;

            string value;

            Assert.True(dictionary.TryGetValue(key, out value));
        }
示例#11
0
        public void WeakKeyDictionary_AddKeyValue_TryGetValueSucceeds()
        {
            const int Value      = 1;
            var       key        = new DisposableTestObject("key");
            var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();

            dictionary.Add(key, Value);

            int value;

            Assert.True(dictionary.TryGetValue(key, out value));
            Assert.Equal(Value, value);
        }
示例#12
0
        public void WeakKeyDictionary_TryGetRemovedValue_ValueNotRetrieved()
        {
            using (var key = new DisposableTestObject("What? Where'd that one go?"))
            {
                const int FortyTwo   = 42;
                var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
                Assert.True(dictionary.AddEntry(key, FortyTwo));
                Assert.True(dictionary.RemoveEntry(key));

                int value;
                Assert.False(dictionary.TryGetValue(key, out value));
            }
        }
示例#13
0
        public void WeakKeyDictionary_AddEntry_TryGetValueSucceeds()
        {
            using (var key = new DisposableTestObject("Moe"))
            {
                const int FortyTwo   = 42;
                var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
                Assert.True(dictionary.AddEntry(key, FortyTwo));
                Assert.True(dictionary.ContainsKey(key));

                int value;
                Assert.True(dictionary.TryGetValue(key, out value));
                Assert.Equal(FortyTwo, value);
            }
        }
示例#14
0
        public void WeakKeyDictionary_AddKeyValuePair_TryGetValueSucceeds()
        {
            const int Value          = 2;
            var       key            = new DisposableTestObject("key");
            var       keeperArounder = new List <KeyValuePair <DisposableTestObject, int> >();
            var       dictionary     = new WeakKeyDictionary <DisposableTestObject, int>();

            var entry = new KeyValuePair <DisposableTestObject, int>(key, Value);

            keeperArounder.Add(entry);
            dictionary.Add(entry);

            int value;

            Assert.True(dictionary.TryGetValue(key, out value));
            Assert.Equal(Value, value);
        }
示例#15
0
        public void WeakKeyDictionary_AddKeysAndValues_EnsureValues()
        {
            var         weakKeyDictionary = new WeakKeyDictionary <DisposableTestObject, int>();
            var         keeperArounder    = new List <DisposableTestObject>();
            IDictionary dictionary        = weakKeyDictionary; // for code coverage
            var         values            = new[] { -1, 48, 62, 88, -32 };

            for (int i = 0; i < values.Length; ++i)
            {
                var entry = new DisposableTestObject(values[i].ToString());
                keeperArounder.Add(entry);
                weakKeyDictionary.Add(entry, values[i]);
            }

            var dictionaryValues = weakKeyDictionary.Values;

            Assert.Equal(values.Length, dictionaryValues.Count);
            Assert.Equal(values.Length, dictionary.Values.Count);
            var missingValues = values.Except(dictionaryValues);

            Assert.False(missingValues.Any());
        }
示例#16
0
        public void WeakKeyDictionaryWithData_EnumerateUsingIEnuberable_ValidateContents()
        {
            var weakKeyDictionary = new WeakKeyDictionary <DisposableTestObject, int>();
            var keeperArounder    = new List <DisposableTestObject>();
            var values            = new[] { 69, 2009, 68, 97, 99, 2002, 2004 };

            for (int i = 0; i < values.Length; ++i)
            {
                var key = new DisposableTestObject(values[i].ToString());
                keeperArounder.Add(key);
                weakKeyDictionary.Add(key, values[i]);
            }

            IEnumerable dictionary = weakKeyDictionary;
            var         index      = 0;

            foreach (KeyValuePair <DisposableTestObject, int> entry in dictionary)
            {
                Assert.Equal(values[index].ToString(), entry.Key.Name);
                Assert.Equal(values[index], entry.Value);
                ++index;
            }
        }