Exemplo n.º 1
0
        public static void HashTableToArrayAssert(int personCount, IHashTable ht)
        {
            Person[] expected = GetExpectedPersons(personCount);
            Person[] actual   = ht.ToArray();

            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 2
0
 public IHuffmanTree CreateHuffmanTree( CNode root, IHashTable hTable )
 {
     return new CHuffmanTree {
         Root = root,
         HashTable = hTable
     };
 }
Exemplo n.º 3
0
        private static void PrintValues(IHashTable <User> hashTable)
        {
            var items = typeof(HashTable <User>).GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)
                        .GetValue(hashTable) as LinkedList <User>[];
            var min = items.Length;
            var max = 0;

            foreach (var linkedList in items)
            {
                if (linkedList == null)
                {
                    min = 0;
                    continue;
                }
                if (linkedList.Count > max)
                {
                    max = linkedList.Count;
                }
                if (linkedList.Count < min)
                {
                    min = linkedList.Count;
                }
            }
            Console.WriteLine($"Min: {min}");
            Console.WriteLine($"Max: {max}");
            Console.WriteLine($"Average: {hashTable.Count / (double) hashTable.Capacity}");
        }
Exemplo n.º 4
0
        public void IEnumerableImplementation_ShouldBeAbleToIterateOverTable()
        {
            // arrange
            var expected = new[]
            {
                new Entry <string, string>("key1", "value1"),
                new Entry <string, string>("key2", "value2"),
                new Entry <string, string>("key3", "value3"),
                new Entry <string, string>("key4", "value4"),
                new Entry <string, string>("key5", null)
            };

            // act
            _hashTable = new HashTable <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" },
                { "key3", "value3" },
                { "key4", "value4" },
                { "key5", null },
            };

            // assert
            _hashTable.AsEnumerable().ShouldBeEqualTo(expected);
        }
Exemplo n.º 5
0
        public void AddAndExistsTest(IHashTable hashTable)
        {
            hashTable.Add("Existing string");

            Assert.IsTrue(hashTable.Exists("Existing string"));
            Assert.IsFalse(hashTable.Exists("Not existing string"));
        }
Exemplo n.º 6
0
        public static void Init(int personCount, IHashTable ht)
        {
            switch (personCount)
            {
            case 1:
                ht.Add(new Person(1, "Uasya", "Pipirkin", 41));
                break;

            case 2:
                ht.Add(new Person(1, "Uasya", "Pipirkin", 41));
                ht.Add(new Person(2, "Vasya", "Pupkin", 42));
                break;

            case 7:
                ht.Add(new Person(1, "Uasya", "Pipirkin", 41));
                ht.Add(new Person(2, "Vasya", "Pupkin", 42));
                ht.Add(new Person(3, "Vasya", "Papkin", 43));
                ht.Add(new Person(4, "Kasya", "Babkin", 44));
                ht.Add(new Person(5, "Masya", "Lapkin", 45));
                ht.Add(new Person(6, "Figasya", "Lupkin", 46));
                ht.Add(new Person(7, "Pupasya", "Lurkin", 47));
                break;

            default:
                break;
            }
        }
Exemplo n.º 7
0
 public IHuffmanTree CreateHuffmanTree(CNode root, IHashTable hTable)
 {
     return(new CHuffmanTree {
         Root = root,
         HashTable = hTable
     });
 }
        public void HashTableRemovingTest()
        {
            foreach (Type hashTableType in this.hashTableTypes)
            {
                IHashTable hashTable = Activator.CreateInstance(hashTableType) as IHashTable;
                Assert.IsTrue(hashTable.Add(5));
                Assert.IsTrue(hashTable.Add(15));
                Assert.IsTrue(hashTable.Add(14));

                Assert.IsTrue(hashTable.Remove(15),
                              "hash table [{0}] should remove existed element",
                              hashTableType.Name);

                Assert.IsFalse(hashTable.Remove(17),
                               "hash table [{0}] should not be able to remove not existed element",
                               hashTableType.Name);

                Assert.IsTrue(hashTable.Contains(5),
                              "hash table [{0}] should contains not removed element",
                              hashTableType.Name);

                Assert.IsFalse(hashTable.Contains(15),
                               "hash table [{0}] should not contains removed element",
                               hashTableType.Name);
            }
        }
        public void Test_Constructor()
        {
            IHashTable <string> ht = this.CreateInstance(2);

            Assert.Equal(2, ht.Size);
            Assert.Equal(0, ht.Count);
        }
Exemplo n.º 10
0
        public List<bool> MakeHash(List<bool> path, IHashTable hash)
        {
            if ( Right == null && Left == null ) {
                hash[ Symbol ] = path ;
                return null;
            }

            List<bool> left = null;
            List<bool> right = null;

            if ( Left != null ) {
                var leftPath = new List<bool>();
                leftPath.AddRange( path );
                leftPath.Add( false );
                left = Left.MakeHash( leftPath, hash );
            }

            if ( Right != null ) {
                var rightPath = new List<bool>();
                rightPath.AddRange( path );
                rightPath.Add( true );
                right = Right.MakeHash( rightPath, hash );
            }

            return  left ?? right;
        }
Exemplo n.º 11
0
        public List <bool> MakeHash(List <bool> path, IHashTable hash)
        {
            if (Right == null && Left == null)
            {
                hash[Symbol] = path;
                return(null);
            }

            List <bool> left  = null;
            List <bool> right = null;

            if (Left != null)
            {
                var leftPath = new List <bool>();
                leftPath.AddRange(path);
                leftPath.Add(false);
                left = Left.MakeHash(leftPath, hash);
            }

            if (Right != null)
            {
                var rightPath = new List <bool>();
                rightPath.AddRange(path);
                rightPath.Add(true);
                right = Right.MakeHash(rightPath, hash);
            }

            return(left ?? right);
        }
Exemplo n.º 12
0
 static public void RemoveAll(ref IHashTable <K, V> ht, int number)
 {
     for (int i = 0; i < number; i++)
     {
         K key = (K)Convert.ChangeType(i.ToString(), typeof(K));
         ht.Remove(key);
     }
 }
Exemplo n.º 13
0
 public MeshBuilder(int width, int height, int depth, IHashTable <int> hashtable)
 {
     this.width   = width;
     this.height  = height;
     this.depth   = depth;
     mesh         = new Mesh();
     this.hashMap = hashtable;
 }
        public void Test_Contains()
        {
            IHashTable <string> ht = this.CreateFullHashTable();

            foreach (string value in VALUES)
            {
                Assert.True(ht.Contains(value));
            }
        }
Exemplo n.º 15
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 Test_Add()
        {
            IHashTable <string> ht = this.CreateInstance(2);

            ht.Add("Metal Church");

            Assert.True(ht.Contains("Metal Church"));
            Assert.Equal(1, ht.Count);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
 private static void AddAll(IHashTable <K, V> ht, long number)
 {
     for (int i = 0; i < number; i++)
     {
         K k = (K)Convert.ChangeType(i.ToString(), typeof(K));
         V v = (V)Convert.ChangeType((i + 100).ToString(), typeof(V));
         HashNode <K, V> node = new HashNode <K, V>(k, v);
         ht.Add(node);
     }
 }
        protected IHashTable <string> CreateFullHashTable()
        {
            IHashTable <string> hashTable = this.CreateInstance(VALUES.Length, AsciiCharSumHashFunction.Instance);

            foreach (string value in VALUES)
            {
                hashTable.Add(value);
            }

            return(hashTable);
        }
Exemplo n.º 20
0
 private void AssertSearchResult(IHashTable <int, string> instance, int key, string expectedValue)
 {
     if (expectedValue == null)
     {
         Assert.IsNull(instance.Search(key));
     }
     else
     {
         Assert.AreEqual(expectedValue, instance.Search(key));
     }
 }
Exemplo n.º 21
0
        private static void CommandExecution(IHashTable hashTable)
        {
            int number = int.Parse(Console.ReadLine());

            while (number != 0)
            {
                switch (number)
                {
                case 1:
                {
                    int value = ValueEntryRequest();
                    hashTable.Add(value);
                }
                break;

                case 2:
                {
                    int value = ValueEntryRequest();
                    hashTable.Remove(value);
                }
                break;

                case 3:
                {
                    int value = ValueEntryRequest();
                    if (hashTable.Exists(value))
                    {
                        Console.WriteLine("Value is in the hash table");
                    }
                    else
                    {
                        Console.WriteLine("Value is not in the hash table");
                    }
                }
                break;

                case 4:
                    hashTable.Print();
                    break;

                case 5:
                    hashTable.Clear();
                    break;

                default:
                    Console.WriteLine("Enter correct number");
                    break;
                }

                Console.Write("Input number: ");
                number = int.Parse(Console.ReadLine());
            }
        }
Exemplo n.º 22
0
        public void HashTableCreationTest()
        {
            foreach (Type hashTableType in this.hashTableTypes)
            {
                IHashTable hashTable = Activator.CreateInstance(hashTableType) as IHashTable;

                Assert.IsNotNull(hashTable);
                Assert.IsFalse(hashTable.Contains(5),
                               "hash table [{0}] should not contains any value after creation",
                               hashTableType.Name);
            }
        }
Exemplo n.º 23
0
        public void HashTableAddingTest()
        {
            foreach (Type hashTableType in this.hashTableTypes)
            {
                IHashTable hashTable = Activator.CreateInstance(hashTableType) as IHashTable;
                Assert.IsTrue(hashTable.Add(5));

                Assert.IsTrue(hashTable.Contains(5),
                              "hash table [{0}] should contains added value",
                              hashTableType.Name);
            }
        }
Exemplo n.º 24
0
        public static void HashTableEnumeratorAssert(int personCount, IHashTable ht)
        {
            Person[]      expected = GetExpectedPersons(personCount);
            List <Person> actual   = new List <Person>();

            foreach (Person item in ht)
            {
                actual.Add(item);
            }

            CollectionAssert.AreEqual(expected, actual.ToArray());
        }
Exemplo n.º 25
0
        public void HashTableClearTest()
        {
            foreach (Type hashTableType in this.hashTableTypes)
            {
                IHashTable hashTable = Activator.CreateInstance(hashTableType) as IHashTable;
                Assert.IsTrue(hashTable.Add(5));
                hashTable.Clear();

                Assert.IsTrue(hashTable.Count() == 0,
                              "hash table [{0}] should contains 0 elements after clearing",
                              hashTableType.Name);
            }
        }
        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);
        }
Exemplo n.º 27
0
        private static void IHashTableTest(IHashTable <string> table)
        {
            while (true)
            {
                var input = IOSystem.SafeSimpleChoice("Выберите действие с таблицей:", new string[]
                {
                    "Добавить узел",
                    "Удалить узел",
                    "Получить узел",
                    "Вывести таблицу",
                    "Закончить тест"
                });

                bool endTest = false;

                switch (input)
                {
                case 0:
                    int key = IOSystem.GetInt("Введите ключ: ");
                    Console.Write("Введите значение: ");
                    string value = Console.ReadLine();
                    table.Add(key, value);
                    break;

                case 1:
                    table.Remove(IOSystem.GetInt("Введите ключ: "));
                    break;

                case 2:
                    Console.WriteLine("Найденное значение: " + table.FindByKey(IOSystem.GetInt("Введите ключ: ")));
                    break;

                case 3:
                    Console.WriteLine("Начало вывода");
                    table.View();
                    Console.WriteLine("Конец вывода");
                    break;

                case 4:
                    endTest = true;
                    break;
                }

                Console.WriteLine();

                if (endTest)
                {
                    break;
                }
            }
        }
Exemplo n.º 28
0
        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"));
        }
Exemplo n.º 29
0
 //test for basic functionality
 static public void TestAllPurposes(IHashTable <K, V> ht)
 {
     AddAll(ht, 50);
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Remove(ht, (K)Convert.ChangeType("4", typeof(K)));
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Add(ht, new HashNode <K, V>((K)Convert.ChangeType("4", typeof(K)), (V)Convert.ChangeType("104", typeof(V))));
     ht.Print();
     Console.WriteLine("----------------------------------------");
     Console.WriteLine(Search(ht, (K)Convert.ChangeType("4", typeof(K))));
     Console.WriteLine(Search(ht, (K)Convert.ChangeType("5", typeof(K))));
     Console.WriteLine("----------------------------------------");
     RemoveAll(ref ht, 50);
     ht.Print();
 }
Exemplo n.º 30
0
        public void RunProbe(IHashTable ht, Random rnd, int size)
        {
            var keysInsert  = KeyUnique(rnd, size);
            var keysInclude = new string[keysInsert.Length];

            int index = 0;

            for (int i = 0; i < keysInsert.Length; i++)
            {
                var result = ht.InsertProbe(keysInsert[i], keysInsert[i]);
                if (result.Result == 0)
                {
                    keysInclude[index] = keysInsert[i];
                    index++;
                }
                else
                {
                    Console.WriteLine($"Insert error {keysInsert[i]} = {result.Result}");
                }
            }

            Array.Resize(ref keysInclude, index);
            for (int i = 0; i < keysInclude.Length; i++)
            {
                var result = ht.SearchProbe(keysInclude[i]);
                if (result.Result != 0)
                {
                    Console.WriteLine($"Search include {keysInsert[i]} = {result.Result}");
                }
            }

            var keysExclude = KeyUnique(rnd, keysInclude);

            for (int i = 0; i < keysExclude.Length; i++)
            {
                var result = ht.SearchProbe(keysExclude[i]);
                if (result.Result == 0)
                {
                    Console.WriteLine($"Search exclude {keysInsert[i]} = {result.Result}");
                }
            }
        }
Exemplo n.º 31
0
        public static void HashTableAddAssert(int personCount, IHashTable ht)
        {
            bool       result  = false;
            IHashTable localHT = null;

            if (ht.GetType() == typeof(LinearHashTable))
            {
                localHT = new LinearHashTable();
            }
            else if (ht.GetType() == typeof(ChainedHashTable))
            {
                localHT = new ChainedHashTable();
            }

            Init(personCount, localHT);
            localHT.Add(new Person(5, "Sasya", "Lapkin", 45));
            result = localHT.Equals(ht);

            Assert.IsTrue(result);
        }
        public void Test_Resize_Up()
        {
            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());
            }

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

            Assert.Equal(oldSize * 2, ht.Size);
            Assert.Equal(oldSize, ht.Count);
        }