Beispiel #1
0
        public void Delete(int[] items, int capacity)
        {
            int  item;
            bool success;
            var  hashtable = new CrackingTheCodingInterview.DataStructures.Hashtable.MyHashtable <int>(capacity);
            int  n         = items.Length;

            for (int i = 0; i < n; i++)
            {
                hashtable.Add($"item {items[i]}", items[i]);
            }

            item = hashtable.Get("item 10");
            Assert.AreEqual(10, item);
            success = hashtable.Delete("item 10");
            Assert.AreEqual(--n, hashtable.Count);
            Assert.AreEqual(0, hashtable.Get("item 10"));
            Assert.IsTrue(success);

            item = hashtable.Get("item 7");
            Assert.AreEqual(7, item);
            success = hashtable.Delete("item 7");
            Assert.AreEqual(--n, hashtable.Count);
            Assert.AreEqual(0, hashtable.Get("item 7"));
            Assert.IsTrue(success);

            item = hashtable.Get("item 0");
            Assert.AreEqual(0, item);
            success = hashtable.Delete("item 0");
            Assert.AreEqual(--n, hashtable.Count);
            Assert.AreEqual(0, hashtable.Get("item 0"));
            Assert.IsTrue(success);
        }
Beispiel #2
0
        public void Add(int[] items, int capacity)
        {
            var hashtable = new CrackingTheCodingInterview.DataStructures.Hashtable.MyHashtable <int>(capacity);

            for (int i = 0; i < items.Length; i++)
            {
                hashtable.Add($"item {items[i]}", i);
            }

            Assert.AreEqual(items.Length, hashtable.Count);
        }
Beispiel #3
0
        public void Update_WithIndexer(int[] items, int capacity, string key, int expected)
        {
            var hashtable = new CrackingTheCodingInterview.DataStructures.Hashtable.MyHashtable <int>(capacity);

            for (int i = 0; i < items.Length; i++)
            {
                hashtable.Add($"item {items[i]}", items[i]);
            }

            hashtable[key] = expected;
            var actual = hashtable[key];

            Assert.AreEqual(expected, actual);
        }
Beispiel #4
0
        public void Update_NotFound(int[] items, int capacity)
        {
            int    expected = 10;
            string key      = Guid.NewGuid().ToString();

            var hashtable = new CrackingTheCodingInterview.DataStructures.Hashtable.MyHashtable <int>(capacity);

            for (int i = 0; i < items.Length; i++)
            {
                hashtable.Add($"item {items[i]}", items[i]);
            }

            hashtable[key] = expected;
            var actual = hashtable[key];

            Assert.AreEqual(0, actual);
        }
Beispiel #5
0
        public void Get_Indexer(int[] items, int capacity)
        {
            var hashtable = new CrackingTheCodingInterview.DataStructures.Hashtable.MyHashtable <int>(capacity);

            for (int i = 0; i < items.Length; i++)
            {
                hashtable.Add($"item {items[i]}", items[i]);
            }

            int actual = hashtable["item 10"];

            Assert.AreEqual(10, actual);

            actual = hashtable["item 7"];
            Assert.AreEqual(7, actual);

            actual = hashtable["item 2"];
            Assert.AreEqual(2, actual);

            actual = hashtable["item not found"];
            Assert.AreEqual(0, actual);
        }