예제 #1
0
        public void FillFactorMustBeZeroInEmptyTableAndPositiveInNonEmpty()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            Assert.AreEqual(0, table.FillFactor, 1e-5);

            table.Add("testproba555spbu", new object());
        }
예제 #2
0
        public void ElementCheckMustWorkCorrectlyOnExistingElement()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            table["testproba"] = new object();

            Assert.IsTrue(table.IsInTable("testproba"));
        }
예제 #3
0
        public void AddingElementUsingIndexerMustWork()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            for (int i = 0; i < 10; ++i)
            {
                table[(i + 100500).ToString()] = new object();
            }
        }
예제 #4
0
        public void AddingExistentElementUsingMethodMustReplaceTheValue()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            table.Add("testproba", 100);
            table.Add("testproba", 200);

            Assert.AreEqual(200, table["testproba"]);
        }
예제 #5
0
        public void AddingElementUsingMethodMustWork()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            for (int i = 0; i < 10; ++i)
            {
                table.Add((i + 100500).ToString(), new object());
            }
        }
예제 #6
0
        public void ElementEraseMustWorkOnSingleElement()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            table["testproba"] = new object();

            table.Erase("testproba");

            const double epsilon = 1e-5;

            Assert.IsTrue(table.FillFactor < epsilon);
        }
예제 #7
0
        public void TwoElementsWithTheSameHashShouldExistSeparately()
        {
            var table = new HashTableStuff.HashTable <string, int>(HashTableStuff.StringHashFunctions.SimpleHash);

            table.Add("+-", 100);
            table.Add("X", 200);

            // In order to demonstate that tests are correct
            Assert.AreEqual(
                HashTableStuff.StringHashFunctions.SimpleHash("+-"),
                HashTableStuff.StringHashFunctions.SimpleHash("X"));

            Assert.IsTrue(table.IsInTable("+-"), "`+-` does not exists");
            Assert.IsTrue(table.IsInTable("X"), "`X` does not exists");

            Assert.AreEqual(100, table["+-"], "`+-` has invalid value");
            Assert.AreEqual(200, table["X"], "`X` has invalid value");
        }
예제 #8
0
        public void AddingAndErasingMultipleElementsMustWork()
        {
            var table = new HashTableStuff.HashTable <string, int>();

            for (int i = -10000; i <= 10000; ++i)
            {
                table.Add(i.ToString(), i - 100);
            }

            for (int i = -10000; i <= 10000; ++i)
            {
                Assert.IsTrue(table.IsInTable(i.ToString()));

                table.Erase(i.ToString());
            }

            Assert.AreEqual(0, table.FillFactor, 1e-5);
        }
예제 #9
0
        public void ErasingTwoElementsWithTheSameHashMustWork()
        {
            var table = new HashTableStuff.HashTable <string, int>(HashTableStuff.StringHashFunctions.SimpleHash);

            table.Add("+-", 100);
            table.Add("X", 200);

            // In order to demonstate that tests are correct
            Assert.AreEqual(
                HashTableStuff.StringHashFunctions.SimpleHash("+-"),
                HashTableStuff.StringHashFunctions.SimpleHash("X"));

            const double epsilon = 1e-5;

            table.Erase("+-");
            Assert.IsTrue(table.FillFactor > epsilon);

            table.Erase("X");
            Assert.IsTrue(table.FillFactor < epsilon);
        }
예제 #10
0
        public static void Main(string[] args)
        {
            PrintProgramName();
            var hashFunction = ChooseHashFunction();

            PrintHelp();

            var hashTable = new HashTableStuff.HashTable <string, int>(hashFunction);

            while (true)
            {
                Console.Write("Enter command: ");
                if (!int.TryParse(Console.ReadLine(), out int currentCommand))
                {
                    Console.WriteLine("Invalid command!");
                    PrintHelp();
                }

                if (!HashTableHelp.HelpCommands.IsDefined(typeof(HashTableHelp.HelpCommands), currentCommand))
                {
                    Console.WriteLine("Invalid command!");
                    PrintHelp();
                }
                else
                {
                    switch ((HashTableHelp.HelpCommands)currentCommand)
                    {
                    case HashTableHelp.HelpCommands.ExitProgram:
                        Console.WriteLine("Good bye!");
                        return;

                    case HashTableHelp.HelpCommands.CheckInTable:
                        Console.Write("Enter key to check: ");
                        var checkKey = Console.ReadLine();
                        Console.WriteLine(hashTable.IsInTable(checkKey) ? "In table" : "Not in table");
                        break;

                    case HashTableHelp.HelpCommands.InsertElement:
                        Console.Write("Enter key to insert (or modify): ");
                        var insertKey = Console.ReadLine();
                        Console.Write($"Enter new value of table[\"{insertKey}\"] (int): ");
                        var insertValue = int.Parse(Console.ReadLine());
                        hashTable[insertKey] = insertValue;
                        break;

                    case HashTableHelp.HelpCommands.EraseElement:
                        Console.Write("Enter key to erase: ");
                        var eraseKey = Console.ReadLine();
                        hashTable.Erase(eraseKey);
                        break;

                    case HashTableHelp.HelpCommands.Factor:
                        Console.WriteLine($"Fill factor == {hashTable.FillFactor}");
                        break;

                    default:
                        PrintHelp();
                        break;
                    }
                }
            }
        }
예제 #11
0
        public void ErasingNonexistentElementShouldCauseException()
        {
            var table = new HashTableStuff.HashTable <string, object>();

            table.Erase("blahblah");
        }
예제 #12
0
 public void GettingNonexistentElementShouldCauseException()
 {
     var table = new HashTableStuff.HashTable <string, object>();
     var t     = table["nonexistent"];
 }