void Test_get_enumerator() { string[] sortedKeys = { "b", "c", "a" }; MyHashTableSeperateChaining <string, string> table = new MyHashTableSeperateChaining <string, string>(); table.Insert("a", "1"); table.Insert("b", "2"); table.Insert("c", "3"); List <string> keys = new List <string>(); foreach (var k in table) { keys.Add(k); int index = Array.FindIndex(sortedKeys, s => k == s); sortedKeys[index] = null; } foreach (var s in sortedKeys) { Assert.Null(s); } Assert.Equal(3, keys.Count); table.Clear(); keys.Clear(); table.Insert("h", "1"); table.Insert("km", "2"); table.Insert("zy", "3"); sortedKeys = new string[] { "h", "km", "zy" }; foreach (var k in table) { keys.Add(k); int index = Array.FindIndex(sortedKeys, s => k == s); sortedKeys[index] = null; } foreach (var s in sortedKeys) { Assert.Null(s); } Assert.Equal(3, keys.Count); }
void Test_exhaustively() { MyHashTableSeperateChaining <string, string> table = new MyHashTableSeperateChaining <string, string>(); table.Insert("a", "1"); table.Insert("b", "2"); table.Insert("c", "3"); Assert.Equal(3, table.Size); Assert.False(table.IsEmpty()); table.Remove("a"); Assert.False(table.ContainsKey("a")); Assert.True(table.ContainsKey("b")); Assert.True(table.ContainsKey("c")); Assert.Equal(2, table.Size); var keys = table.Keys(); var values = table.Values(); Assert.Equal(2, keys.Count); Assert.Equal(2, values.Count); table.Clear(); Assert.True(table.IsEmpty()); Assert.False(table.ContainsKey("a")); Assert.False(table.ContainsKey("b")); Assert.False(table.ContainsKey("c")); var keys2 = table.Keys(); var values2 = table.Values(); Assert.Empty(keys2); Assert.Empty(values2); table.Insert("h", "100"); table.Insert("z", "255"); table.Insert("z", "300"); Assert.Equal(2, table.Size); Assert.False(table.IsEmpty()); Assert.Equal("300", table.GetValue("z")); Assert.False(table.ContainsKey("a")); Assert.False(table.ContainsKey("b")); Assert.False(table.ContainsKey("c")); Assert.True(table.ContainsKey("h")); Assert.True(table.ContainsKey("z")); var keys3 = table.Keys(); var values3 = table.Values(); Assert.Equal(2, keys3.Count); Assert.Equal(2, values3.Count); table.Remove("z"); Assert.Equal(1, table.Size); Assert.False(table.IsEmpty()); Assert.True(table.ContainsKey("h")); Assert.False(table.ContainsKey("z")); table.Remove("h"); Assert.True(table.IsEmpty()); Assert.False(table.ContainsKey("h")); Assert.False(table.ContainsKey("z")); var keys4 = table.Keys(); var values4 = table.Values(); Assert.Empty(keys4); Assert.Empty(values4); Assert.Throws <ArgumentException>(() => table.Remove("z")); }