Example #1
0
        public void TestAdd2()
        {
            var dictionary = new WeakKeyDictionary <string, int>();

            dictionary.Add("Foobar", 42);
            dictionary.Add("Klondyke Bar", 9001);
            dictionary.Count.Should().Be(2);
            dictionary.ContainsKey("Foobar").Should().BeTrue();
            dictionary.ContainsKey("Klondyke Bar").Should().BeTrue();
            dictionary.Version.Should().Be(2);

            EnsureIntegrity(dictionary);
        }
Example #2
0
        public void TestAdd5()
        {
            var dictionary = new WeakKeyDictionary <Key, int>();
            var key1       = new Key(1, 42);
            var key2       = new Key(2, 42);

            key1.GetHashCode().Should().Be(key2.GetHashCode());
            key1.Equals(key2).Should().BeFalse();

            dictionary.Add(key1, 9);
            dictionary.Add(key2, 10);
            dictionary.Count.Should().Be(2);
            dictionary.ContainsKey(key1).Should().BeTrue();
            dictionary.ContainsKey(key2).Should().BeTrue();

            EnsureIntegrity(dictionary);
        }
Example #3
0
        public void Add(IXmlNode node, object keyValue, object newValue, bool isInGraph)
        {
            if (keyValue == null)
            {
                throw Error.ArgumentNull("keyValue");
            }
            if (newValue == null)
            {
                throw Error.ArgumentNull("newValue");
            }

            var type = newValue.GetComponentType();

            if (ShouldExclude(type))
            {
                return;
            }
            if (entriesByValue.ContainsKey(newValue))
            {
                return;
            }

            Entry entry;

            if (entriesByValue.TryGetValue(keyValue, out entry))
            {
                if (newValue == keyValue)
                {
                    return;
                }
            }
            else if (node != null)
            {
                bool reference;
                if (!TryGetEntry(node, out entry, out reference))
                {
                    entry = new Entry(node);
                }
            }
            else
            {
                return;
            }

            AddValueCore(entry, type, newValue, isInGraph);
        }
        public void TryGetValue_ReferenceNotFound()
        {
            var dictionary = new WeakKeyDictionary <string, string>();

            bool result = dictionary.TryGetValue("x", out string v);

            Assert.False(result);
            Assert.Null(v);
            Assert.False(dictionary.ContainsKey("x"));
        }
Example #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));
     }
 }
Example #6
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));
     }
 }
Example #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));
     }
 }
Example #8
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));
     }
 }
Example #9
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);
            }
        }
        public void Indexer_ReferenceFound()
        {
            string k1 = "key";
            string v1 = "value";

            var dictionary = new WeakKeyDictionary <string, string>();

            dictionary[k1] = v1;

            // Now look for the same key we inserted
            string v2 = dictionary[k1];

            Assert.True(object.ReferenceEquals(v1, v2));
            Assert.True(dictionary.ContainsKey(k1));
        }
Example #11
0
        public void TestAdd3()
        {
            var dictionary = new WeakKeyDictionary <string, int>();

            dictionary.Add("Foobar", 42);
            new Action(() => dictionary.Add("Foobar", 42))
            .Should().Throw <ArgumentException>()
            .WithMessage("An item with the same key has already been added.");

            dictionary.Count.Should().Be(1);
            dictionary.ContainsKey("Foobar").Should().BeTrue();
            dictionary.Version.Should().Be(1);

            EnsureIntegrity(dictionary);
        }
Example #12
0
        public void TestAdd7()
        {
            const int numValues  = 1000;
            var       values     = new List <object>(numValues);
            var       dictionary = new WeakKeyDictionary <object, int>();

            for (int i = 0; i < numValues; ++i)
            {
                var key = new Key(i, i);
                values.Add(key);
                dictionary.Add(key, i);

                EnsureIntegrity(dictionary);
            }

            dictionary.Count.Should().Be(numValues);
            for (int i = 0; i < numValues; ++i)
            {
                var key = values[i];
                dictionary.ContainsKey(key).Should().BeTrue();
                dictionary[key].Should().Be(i);
            }
        }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary <TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;

            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);

            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;

            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;

            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }
Example #14
0
 public bool ContainsKey(WeakKeyDictionary <object, object, object, int, object> d, Tuple <object, object, object, int> k)
 {
     return(d.ContainsKey(k.Item1, k.Item2, k.Item3, k.Item4));
 }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary<TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;
            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);
            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;
            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;
            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }
Example #16
0
        public void TestRemove()
        {
            WeakKeyDictionary<object, string> dictionary =
                new WeakKeyDictionary<object, string>();
            dictionary[new object()] = "test";
            dictionary[new object()] = "foo";
            dictionary[new object()] = "bar";
            Assert.AreEqual(3, dictionary.Count);

            object testObject = new object();
            dictionary[testObject] = "blah";
            Assert.AreEqual(4, dictionary.Count);
            Assert.AreEqual(4, dictionary.Count);
            Assert.IsTrue(dictionary.ContainsKey(testObject));
            Assert.IsTrue(dictionary.Remove(testObject));
            Assert.AreEqual(3, dictionary.Count);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            foreach (object key in dictionary.Keys)
            {
                Console.WriteLine("gen[{0}] = {1}", key, GC.GetGeneration(key));
            }
            Assert.AreEqual(0, dictionary.Count);
            Assert.AreEqual(0, dictionary.Values.Count);
        }
Example #17
0
        public void TestHold()
        {
            WeakKeyDictionary<object, string> dictionary =
                new WeakKeyDictionary<object, string>();
            dictionary[new object()] = "test";
            dictionary[new object()] = "foo";
            dictionary[new object()] = "bar";
            object testObject = new object();
            dictionary[testObject] = "blah";
            Assert.AreEqual(4, dictionary.Count);
            Assert.IsTrue(dictionary.ContainsKey(testObject));

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.AreEqual(1, dictionary.Count);
            Assert.AreEqual(1, dictionary.Values.Count);
            Assert.IsTrue(dictionary.ContainsKey(testObject));
        }