Ejemplo n.º 1
0
        void Test_values()
        {
            string[] expectedValues = { "first", "second", "third" };
            MyHashTableSeperateChaining <string, string> table = new MyHashTableSeperateChaining <string, string>();

            table.Insert("a", "first");
            table.Insert("b", "second");
            table.Insert("c", "third");

            var values = table.Values();

            Assert.Equal(3, values.Count);
            values.ForEach(v => {
                int index = Array.IndexOf(expectedValues, v);
                Assert.NotEqual(-1, index);
                expectedValues[index] = null;
            });

            foreach (var k in expectedValues)
            {
                Assert.Null(k);
            }
        }
Ejemplo n.º 2
0
        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"));
        }