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 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); }