public void ScavengeOnGrow()
        {
            var dictionary = new WeakValueDictionary <object, object>();

            for (int i = 0; i < 100; i++)
            {
                dictionary[new Object()] = new Object();

                // Randomly collect some
                if (i == 15)
                {
                    GC.Collect();
                }
            }

            // We should have scavenged at least once
            Console.WriteLine("Count {0}", dictionary.Count);
            Assertion.AssertEquals(true, dictionary.Count < 100);

            // Finish with explicit scavenge
            int count1  = dictionary.Count;
            int removed = dictionary.Scavenge();
            int count2  = dictionary.Count;

            Console.WriteLine("Removed {0}", removed);
            Assertion.AssertEquals(removed, count1 - count2);
        }
        public void ExplicitScavenge()
        {
            object k1 = new object();
            object v1 = new object();

            var dictionary = new WeakValueDictionary <object, object>();

            dictionary[k1] = v1;

            Assertion.AssertEquals(1, dictionary.Count);

            v1 = null;
            GC.Collect();

            dictionary.Scavenge();

            Assertion.AssertEquals(0, dictionary.Count);
        }
        public void Indexer_NullValue_ReferenceFound()
        {
            string k1 = "key";
            string v1 = null;

            var dictionary = new WeakValueDictionary<string, string>();
            dictionary[k1] = v1;

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

            Assertion.AssertEquals(true, Object.ReferenceEquals(v1, v2));
            Assertion.AssertEquals(true, dictionary.Contains(k1));

            // Should not scavenge values that are null, rather than collected
            dictionary.Scavenge();
            Assertion.AssertEquals(1, dictionary.Count);
        }
        public void Indexer_NullValue_ReferenceFound()
        {
            string k1 = "key";
            string v1 = null;

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

            dictionary[k1] = v1;

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

            Assertion.AssertEquals(true, Object.ReferenceEquals(v1, v2));
            Assertion.AssertEquals(true, dictionary.Contains(k1));

            // Should not scavenge values that are null, rather than collected
            dictionary.Scavenge();
            Assertion.AssertEquals(1, dictionary.Count);
        }
        public void TryGetNullValue_ReferenceFound()
        {
            string k1 = "key";
            string v1 = null;

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

            dictionary[k1] = v1;

            // Now look for the same key we inserted
            string v2;
            bool   result = dictionary.TryGetValue(k1, out v2);

            Assertion.AssertEquals(true, result);
            Assertion.AssertEquals(true, Object.ReferenceEquals(v1, v2));

            // Should not scavenge values that are null, rather than collected
            dictionary.Scavenge();
            Assertion.AssertEquals(1, dictionary.Count);
        }
        public void ScavengeOnGrow()
        {
            var dictionary = new WeakValueDictionary<object, object>();

            for (int i = 0; i < 100; i++)
            {
                dictionary[new Object()] = new Object();

                // Randomly collect some
                if (i == 15)
                {
                    GC.Collect();
                }
            }

            // We should have scavenged at least once
            Console.WriteLine("Count {0}", dictionary.Count);
            Assertion.AssertEquals(true, dictionary.Count < 100);

            // Finish with explicit scavenge
            int count1 = dictionary.Count;
            int removed = dictionary.Scavenge();
            int count2 = dictionary.Count;

            Console.WriteLine("Removed {0}", removed);
            Assertion.AssertEquals(removed, count1 - count2);
        }
        public void ExplicitScavenge()
        {
            object k1 = new object();
            object v1 = new object();

            var dictionary = new WeakValueDictionary<object, object>();
            dictionary[k1] = v1;

            Assertion.AssertEquals(1, dictionary.Count);

            v1 = null;
            GC.Collect();

            dictionary.Scavenge();

            Assertion.AssertEquals(0, dictionary.Count);
        }
        public void TryGetNullValue_ReferenceFound()
        {
            string k1 = "key";
            string v1 = null;

            var dictionary = new WeakValueDictionary<string, string>();
            dictionary[k1] = v1;

            // Now look for the same key we inserted
            string v2;
            bool result = dictionary.TryGetValue(k1, out v2);

            Assertion.AssertEquals(true, result);
            Assertion.AssertEquals(true, Object.ReferenceEquals(v1, v2));

            // Should not scavenge values that are null, rather than collected
            dictionary.Scavenge();
            Assertion.AssertEquals(1, dictionary.Count);
        }