public static PerformActionOnAllHashtableWrappers ( Hashtable hashtable, Action |
||
hashtable | Hashtable | |
action | Action |
|
return | void |
public void ContainsValue() { Hashtable hash1 = Helpers.CreateStringHashtable(100); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { for (int i = 0; i < hash2.Count; i++) { string value = "Value_" + i; Assert.True(hash2.ContainsValue(value)); } Assert.False(hash2.ContainsValue("Non Existent Value")); Assert.False(hash2.ContainsValue(101)); Assert.False(hash2.ContainsValue(null)); hash2.Add("Key_101", null); Assert.True(hash2.ContainsValue(null)); string removedKey = "Key_1"; string removedValue = "Value_1"; hash2.Remove(removedKey); Assert.False(hash2.ContainsValue(removedValue)); }); }
public void ContainsKey() { Hashtable hash1 = Helpers.CreateStringHashtable(100); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { for (int i = 0; i < hash2.Count; i++) { string key = "Key_" + i; Assert.True(hash2.ContainsKey(key)); Assert.True(hash2.Contains(key)); } Assert.False(hash2.ContainsKey("Non Existent Key")); Assert.False(hash2.Contains("Non Existent Key")); Assert.False(hash2.ContainsKey(101)); Assert.False(hash2.Contains("Non Existent Key")); string removedKey = "Key_1"; hash2.Remove(removedKey); Assert.False(hash2.ContainsKey(removedKey)); Assert.False(hash2.Contains(removedKey)); }); }
public void ContainsKey_NullKey_ThrowsArgumentNullException() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { AssertExtensions.Throws <ArgumentNullException>("key", () => hash2.ContainsKey(null)); // Key is null AssertExtensions.Throws <ArgumentNullException>("key", () => hash2.Contains(null)); // Key is null }); }
public void Add_ReferenceType() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { // Value is a reference var foo = new Foo(); hash2.Add("Key", foo); Assert.Equal("Hello World", ((Foo)hash2["Key"]).StringValue); // Changing original object should change the object stored in the Hashtable foo.StringValue = "Goodbye"; Assert.Equal("Goodbye", ((Foo)hash2["Key"]).StringValue); }); }
public void ContainsValue_EqualObjects() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { var foo1 = new Foo() { StringValue = "Goodbye" }; var foo2 = new Foo() { StringValue = "Goodbye" }; hash2.Add(101, foo1); Assert.True(hash2.ContainsValue(foo2)); }); }
public void ContainsKey_EqualObjects() { var hash1 = new Hashtable(); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { var foo1 = new Foo() { StringValue = "Goodbye" }; var foo2 = new Foo() { StringValue = "Goodbye" }; hash2.Add(foo1, 101); Assert.True(hash2.ContainsKey(foo2)); Assert.True(hash2.Contains(foo2)); int i1 = 0x10; int i2 = 0x100; long l1 = (((long)i1) << 32) + i2; // Create two longs with same hashcode long l2 = (((long)i2) << 32) + i1; hash2.Add(l1, 101); hash2.Add(l2, 101); // This will cause collision bit of the first entry to be set Assert.True(hash2.ContainsKey(l1)); Assert.True(hash2.Contains(l1)); hash2.Remove(l1); // Remove the first item Assert.False(hash2.ContainsKey(l1)); Assert.False(hash2.Contains(l1)); Assert.True(hash2.ContainsKey(l2)); Assert.True(hash2.Contains(l2)); }); }
public void Clone(int count) { Hashtable hash1 = Helpers.CreateStringHashtable(count); Helpers.PerformActionOnAllHashtableWrappers(hash1, hash2 => { Hashtable clone = (Hashtable)hash2.Clone(); Assert.Equal(hash2.Count, clone.Count); Assert.Equal(hash2.IsSynchronized, clone.IsSynchronized); Assert.Equal(hash2.IsFixedSize, clone.IsFixedSize); Assert.Equal(hash2.IsReadOnly, clone.IsReadOnly); for (int i = 0; i < clone.Count; i++) { string key = "Key_" + i; string value = "Value_" + i; Assert.True(clone.ContainsKey(key)); Assert.True(clone.ContainsValue(value)); Assert.Equal(value, clone[key]); } }); }