public void DeleteTest(IHashTable hashTable)
        {
            hashTable.Add("TestString1");
            hashTable.Add("TestString2");
            hashTable.Add("TestString3");

            Assert.IsTrue(hashTable.Delete("TestString1"));
            Assert.IsFalse(hashTable.Delete("TestString1"));
            Assert.IsTrue(hashTable.Delete("TestString3"));

            Assert.IsFalse(hashTable.Exists("TestString1"));
            Assert.IsFalse(hashTable.Exists("TestString3"));
            Assert.IsTrue(hashTable.Exists("TestString2"));
        }
Exemple #2
0
        /// <summary>
        /// Performs an action depending on the entered command.
        /// </summary>
        /// <param name="hashTable">Hash table to work with.</param>
        /// <param name="command">Command entered by user.</param>
        public void CommandExecution(IHashTable hashTable, int command)
        {
            switch (command)
            {
            case 0:
                break;

            case 1:
            {
                Console.WriteLine("Enter the data of element you want to add");
                var data = int.Parse(Console.ReadLine());

                if (!hashTable.Add(data))
                {
                    Console.WriteLine("The element with the given data already exists");
                }
                break;
            }

            case 2:
            {
                Console.WriteLine("Enter the data of element you want to delete");
                var data = int.Parse(Console.ReadLine());

                if (!hashTable.Delete(data))
                {
                    Console.WriteLine("The element with the given data does not exists");
                }
                break;
            }

            case 3:
            {
                Console.WriteLine("Enter the data of hash table element");
                var data = int.Parse(Console.ReadLine());

                if (hashTable.Exists(data))
                {
                    Console.WriteLine("The element with the given data exists");
                }
                else
                {
                    Console.WriteLine("The element with the given data does not exists");
                }
                break;
            }

            case 4:
                Console.WriteLine("The hash table:");
                hashTable.Print();
                break;

            default:
                Console.WriteLine("The command you entered is incorrect");
                break;
            }
        }
        public void SizeOfHashTableTest(IHashTable hashTable)
        {
            hashTable.Add("TestString 1");
            hashTable.Add("TestString 2");
            hashTable.Add("TestString 3");
            hashTable.Delete("TestString 2");

            Assert.AreEqual(2, hashTable.NumberOfElements);
        }
        public void Test_Delete()
        {
            IHashTable <string> ht = this.CreateFullHashTable();

            foreach (string value in VALUES)
            {
                ht.Delete(value);

                Assert.False(ht.Contains(value));
            }

            Assert.Equal(0, ht.Count);
        }
        public void Test_Resize_Down()
        {
            IHashTable <string> ht = this.CreateInstance(10);

            int oldSize = ht.Size;

            // It's needed more than 75% to trigger
            // a resize up.
            for (int i = 0; i < oldSize; i++)
            {
                ht.Add(i.ToString());
            }

            oldSize = ht.Size;

            // Resize up = 20
            // 20 - 20 * 0.75 = 5
            // Trigger count = 5
            int triggerCount = (int)(oldSize - oldSize * 0.75);

            // It's needed less than 25% to trigger
            // a resize down.
            // Removing the upper values eases testing.
            for (int i = oldSize; i >= triggerCount; i--)
            {
                ht.Delete(i.ToString());
            }

            for (int i = 0; i < (oldSize - oldSize * 0.75); i++)
            {
                Assert.True(ht.Contains(i.ToString()));
            }

            Assert.Equal(oldSize / 2, ht.Size);
            Assert.Equal(triggerCount, ht.Count);
        }
 public void DeleteFromEmptyHashTableTest(IHashTable hashTable) => Assert.IsFalse(hashTable.Delete("TestString"));
 public void Test_Delete_Null_NullRef_Exc()
 {
     Assert.Throws <NullReferenceException>(() => ht.Delete(null));
 }