public void WeakKeyDictionaryWithData_EnumerateUsingIDictionaryEnumerator_ValidateContents() { IDictionary dictionary = new WeakKeyDictionary <string, int>(); var values = new[] { -1, 48, 62, 88, -32 }; for (int i = 0; i < values.Length; ++i) { dictionary.Add(values[i].ToString(), values[i]); } var dictionaryEnumerator = dictionary.GetEnumerator(); while (dictionaryEnumerator.MoveNext()) { var current = dictionaryEnumerator.Current; var entry = dictionaryEnumerator.Entry; Assert.Equal(current, entry); var key = dictionaryEnumerator.Key; var value = dictionaryEnumerator.Value; var entryKey = entry.Key as string; var entryValue = (int)entry.Value; Assert.Equal(key, entryKey); Assert.Equal(value, entryValue); } var disposable = dictionaryEnumerator as IDisposable; if (disposable != null) { disposable.Dispose(); dictionaryEnumerator = null; disposable = null; } }
public ServantStorage(IRemotingEndPoint remotingEndPoint, IEndPointChannel endPointChannel, GrainIdGenerator idGenerator, ICodeGenerator codeGenerator) { if (remotingEndPoint == null) { throw new ArgumentNullException(nameof(remotingEndPoint)); } if (endPointChannel == null) { throw new ArgumentNullException(nameof(endPointChannel)); } if (idGenerator == null) { throw new ArgumentNullException(nameof(idGenerator)); } if (codeGenerator == null) { throw new ArgumentNullException(nameof(codeGenerator)); } _remotingEndPoint = remotingEndPoint; _endPointChannel = endPointChannel; _idGenerator = idGenerator; _codeGenerator = codeGenerator; _syncRoot = new object(); _servantsById = new Dictionary <ulong, IServant>(); _servantsBySubject = new WeakKeyDictionary <object, IServant>(); }
public void TestCollect8() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new Key(1, 9001); var key4 = new Key(4, 9001); new Action(() => { dictionary.Add(key1, 1); dictionary.Add(new Key(2, 9001), 2); dictionary.Add(new Key(3, 9001), 3); dictionary.Add(key4, 4); })(); GC.Collect(); GC.WaitForPendingFinalizers(); dictionary.Collect(); dictionary.Count.Should().Be(2); var key2 = new Key(2, 9001); var key3 = new Key(3, 9001); dictionary.Add(key2, 2); dictionary.Add(key3, 3); dictionary.Count.Should().Be(4); dictionary[key2].Should().Be(2); dictionary[key3].Should().Be(3); EnsureIntegrity(dictionary); }
public void TestCollect1() { var dictionary = new WeakKeyDictionary <object, int>(); WeakReference <object> weakKey = null; new Action(() => { var key = new ByReferenceClass(); weakKey = new WeakReference <object>(key); dictionary.Add(key, 42); dictionary.Count.Should().Be(1); })(); GC.Collect(); GC.WaitForPendingFinalizers(); weakKey.Should().NotBeNull(); dictionary.Collect(); dictionary.Count.Should().Be(0); object unused; weakKey.TryGetTarget(out unused).Should().BeFalse("Because the dictionary shouldn't keep the key alive"); EnsureIntegrity(dictionary); }
public void TestCollect4() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new Key(1, 9001); var key4 = new Key(4, 9001); new Action(() => { dictionary.Add(key1, 1); dictionary.Add(new Key(2, 9001), 2); dictionary.Add(new Key(3, 9001), 3); dictionary.Add(key4, 4); })(); GC.Collect(); GC.WaitForPendingFinalizers(); dictionary.Count.Should().Be(4); dictionary.Collect(); dictionary.Count.Should().Be(2); dictionary[key1].Should().Be(1); dictionary[key4].Should().Be(4); dictionary._freeCount.Should().Be(2, "Because 2 previously used entries were reclaimed"); EnsureIntegrity(dictionary); }
public void TestAdd8() { var dictionary = new WeakKeyDictionary <object, int>(); var key1 = new Key(1, 42); WeakReference <Key> weakKey2 = null; var key3 = new Key(3, 42); new Action(() => { var key2 = new Key(2, 42); weakKey2 = new WeakReference <Key>(key2); dictionary.Add(key1, 1); dictionary.Add(key2, 2); dictionary.Add(key3, 3); })(); dictionary.Count.Should().Be(3); dictionary._freeCount.Should().Be(0); GC.Collect(); GC.WaitForPendingFinalizers(); // This is a special case that tests that iterating over an entry within a bucket // that has been collected by the GC works as expected: There has been a bug that caused // the iteration within the bucket to completely stop. var key4 = new Key(1, 42); new Action(() => dictionary.Add(key4, 4)) .Should().Throw <ArgumentException>() .WithMessage("An item with the same key has already been added."); dictionary.Count.Should().Be(2); dictionary._freeCount.Should().Be(1); }
public void TestAddPerformance() { const int num = 1000000; var keys = Enumerable.Range(0, num).Select(i => new object()).ToList(); var dictionary = new Dictionary <object, int>(); var weakDictionary = new WeakKeyDictionary <object, int>(); var sw1 = new Stopwatch(); sw1.Start(); for (int i = 0; i < num; ++i) { dictionary.Add(keys[i], i); } sw1.Stop(); var sw2 = new Stopwatch(); sw2.Start(); for (int i = 0; i < num; ++i) { weakDictionary.Add(keys[i], i); } sw2.Stop(); Console.WriteLine("Dictionary.Add: {0}ms", sw1.ElapsedMilliseconds); Console.WriteLine("WeakKeyDictionary.Add: {0}ms", sw2.ElapsedMilliseconds); sw2.ElapsedMilliseconds.Should().BeLessOrEqualTo(sw1.ElapsedMilliseconds * 10, "Because the weak key dictionary should not be slower than by an order of magnitude"); }
public void KeysCollectable() { string k1 = new string('k', BigMemoryFootprintTest); string v1 = new string('v', BigMemoryFootprintTest); // Each character is 2 bytes, so about 4MB of this should be the strings long memory1 = GC.GetTotalMemory(true); var dictionary = new WeakKeyDictionary <string, string>(); dictionary[k1] = v1; k1 = null; long memory2 = GC.GetTotalMemory(true); // Key collected, should be about 2MB less long difference = memory1 - memory2; this.logger.WriteLine("Start {0}, end {1}, diff {2}", memory1, memory2, difference); Assert.True(difference > 1500000); // 2MB minus big noise allowance // This line is VERY important, as it keeps the GC from being too smart and collecting // the dictionary and its large strings because we never use them again. GC.KeepAlive(dictionary); }
public void ScavengeOnGrow() { var dictionary = new WeakKeyDictionary <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 this.logger.WriteLine("Count {0}", dictionary.Count); Assert.True(dictionary.Count < 100); // Finish with explicit scavenge int count1 = dictionary.Count; int removed = dictionary.Scavenge(); int count2 = dictionary.Count; this.logger.WriteLine("Removed {0}", removed); Assert.Equal(removed, count1 - count2); }
public void WeakKeyDictionaryWithData_CopyToEndOfSufficientlyLargeKeyValuePairArray_Succeeds() { const int NumValuesToAdd = 20; var keeperArounder = new List <KeyValuePair <string, int> >(); ICollection <KeyValuePair <string, int> > dictionary = new WeakKeyDictionary <string, int>(); for (int i = 0; i < NumValuesToAdd; ++i) { var entry = new KeyValuePair <string, int>(i.ToString(), i); keeperArounder.Add(entry); dictionary.Add(entry); } Assert.Equal(NumValuesToAdd, dictionary.Count); var destination = new KeyValuePair <string, int> [50]; const int StartIndex = 30; dictionary.CopyTo(destination, StartIndex); for (int i = 0; i < NumValuesToAdd; ++i) { Assert.Equal(i.ToString(), destination[i + StartIndex].Key); Assert.Equal(i, destination[i + StartIndex].Value); } }
public void TestAdd9() { var dictionary = new WeakKeyDictionary <object, string>(); new Action(() => dictionary.Add(null, "foo")) .Should().Throw <ArgumentNullException>() .WithMessage("Value cannot be null.\r\nParameter name: key"); }
public void TestCtor() { var dictionary = new WeakKeyDictionary <string, int>(); dictionary.Count.Should().Be(0); dictionary.Version.Should().Be(0); EnsureIntegrity(dictionary); }
public void TestIndexer1() { var dictionary = new WeakKeyDictionary <string, int>(); dictionary.Add("a", 1); dictionary["a"].Should().Be(1); EnsureIntegrity(dictionary); }
public XmlReferenceManager(IXmlNode root, IXmlReferenceFormat format) { entriesById = new Dictionary <int, Entry>(); entriesByValue = new WeakKeyDictionary <object, Entry>(ReferenceEqualityComparer <object> .Instance); this.format = format; this.nextId = 1; Populate(root); }
[MethodImpl(MethodImplOptions.NoInlining)] // must not be inlined so that locals are guaranteed to be freed. private static void ExplicitScavenge_Helper(WeakKeyDictionary <object, object> dictionary) { object k1 = new object(); object v1 = new object(); dictionary[k1] = v1; Assert.Equal(1, dictionary.Count); }
public XmlReferenceManager(IXmlNode root, IXmlReferenceFormat format) { entriesById = new Dictionary<int, Entry>(); entriesByValue = new WeakKeyDictionary<object, Entry>(ReferenceEqualityComparer<object>.Instance); this.format = format; this.nextId = 1; Populate(root); }
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))); } }
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)); } }
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")); }
public void TestCollect5() { var dictionary = new WeakKeyDictionary <object, int>(); dictionary.Version.Should().Be(0); dictionary.Collect(); dictionary.Version.Should().Be(0, "Because no entry should've been modified"); EnsureIntegrity(dictionary); }
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]); }
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)); } }
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)); } }
public void ExplicitScavenge() { var dictionary = new WeakKeyDictionary <object, object>(); ExplicitScavenge_Helper(dictionary); GC.Collect(); dictionary.Scavenge(); Assert.Equal(0, dictionary.Count); }
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)); } }
public void TestIndexer2() { var dictionary = new WeakKeyDictionary <string, int>(); dictionary.Add("a", 1); new Action(() => { int value = dictionary["b"]; }) .Should().Throw <ArgumentException>() .WithMessage("The given key was not present in the dictionary."); EnsureIntegrity(dictionary); }
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)); } }
public void TestAdd1() { var dictionary = new WeakKeyDictionary <string, int>(); dictionary.Add("Foobar", 42); dictionary.Count.Should().Be(1); dictionary.ContainsKey("Foobar").Should().BeTrue(); dictionary.Version.Should().Be(1); EnsureIntegrity(dictionary); }
public object AddOrUpdate(WeakKeyDictionary <object, int, object> d, Tuple <object, int> k, object addValue, Func <Tuple <object, int>, object, object> updateValueFactory) { return (d.AddOrUpdate( k.Item1, k.Item2, addValue, (kp1, kp2, v) => updateValueFactory(Tuple.Create(kp1, kp2), v) ) ); }
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); }
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); }
public void Test_WeakKeyDictionary() { var normalKeyDiction = new Dictionary<TestKey, int>(); var key = new TestKey("Yohan"); var keyReference = new WeakReference(key); normalKeyDiction.Add(key, 1); Assert.AreEqual(1, normalKeyDiction.Count); Assert.AreEqual(1, normalKeyDiction[key]); Assert.IsTrue(keyReference.IsAlive); key = null; GC.Collect(); //Normal Dictionary hold the strong reference to the key. Assert.IsTrue(keyReference.IsAlive); var weakKeyDictionary = new WeakKeyDictionary<TestKey, int>(); key = new TestKey("Yohan"); keyReference = new WeakReference(key); weakKeyDictionary.Add(key, 1); Assert.AreEqual(1, weakKeyDictionary.Count); Assert.AreEqual(1, weakKeyDictionary[key]); IList keys = weakKeyDictionary.Keys; Assert.AreEqual(1, keys.Count); Assert.IsTrue(keyReference.IsAlive); key = null; GC.Collect(); //WeakKeyDictionary hold the WeakReference of the key. Assert.IsFalse(keyReference.IsAlive); GC.KeepAlive(normalKeyDiction); }
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); }
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); }
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)); }
private WeakKeyDictionary<object, string> PopulateDictionary() { WeakKeyDictionary<object, string> dictionary = new WeakKeyDictionary<object, string>(); dictionary[new object()] = "test"; dictionary[new object()] = "foo"; dictionary[new object()] = "bar"; return dictionary; }