Exemplo n.º 1
0
        public void TestInsertAndSearch(string key, string value)
        {
            AVLTree <string, string> tree = new AVLTree <string, string>();

            tree[key] = value;

            var val = tree[key];

            Assert.Equal(value, val);//Check if value is valid

            Assert.False(tree.ContainsKey(key + "test"));
            Assert.True(tree.ContainsKey(key));

            Assert.Throws <KeyNotFoundException>(() => tree[key + "Test"]);//Test of throw exception if value not exist
        }
        /// <summary>
        /// generates a 128 bit AES hash that is not used in pagenames
        /// </summary>
        /// <returns></returns>
        public static string GenerateUnusedHash()
        {
GENERATE_NEW_HASH:
            string hash = Hash.GetHash();

            // Chris: if(hash already exists in any hash list) {goto GENERATE_NEW_HASH;}

            if (UserInfos.ContainsKey(hash))
            {
                goto GENERATE_NEW_HASH;
            }

            if (PerFileObjects.ContainsKey(hash))
            {
                goto GENERATE_NEW_HASH;
            }

            return(hash);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var a = new AVLTree <string, int>();
            //var a = new SortedDictionary<string, int>();
            //string input_text = "a b  l d e  g  h h k c m ";
            string str = "";

            string[] input_check = System.IO.File.ReadAllText(@"big.txt");
            // string[] input_check = System.IO.File.ReadAllLines(@"check.txt");
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            long elapsedMs;

            foreach (var i in input_text)
            {
                if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z' || i == '\'')
                {
                    str += i;
                }
                else if (str.Length > 0)
                {
                    if (a.ContainsKey(str))
                    {
                        ++a[str];
                    }
                    else
                    {
                        a.Add(str, 1);
                    }
                    str = "";
                }
            }
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            System.Console.WriteLine("time:  " + elapsedMs);
            //a.Print();
        }
Exemplo n.º 4
0
        public void ExecuteTestTree()
        {
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();
            const int     size   = 1000;

            Console.WriteLine("Small Tests...");
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value2");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Add(new KeyValuePair <string, string>("key", "avalue"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "avalue");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            Assert.IsFalse(tree.Remove("value"));
            Assert.IsTrue(tree.Remove("key"));
            tree.Validate();
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(tree.Count == 1);
            tree.Clear();
            Assert.IsTrue(tree.Count == 0);
            tree.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(tree.ContainsKey("key"));
            Assert.IsFalse(tree.ContainsKey("value"));
            Assert.IsTrue(tree["key"] == "value");
            Assert.IsTrue(tree.Count == 1);
            tree.Validate();
            tree.Clear();
            Assert.IsFalse(tree.Remove(""));
            Assert.IsFalse(tree.Remove(new KeyValuePair <string, string>("", "")));

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                tree[hashes[i]] = values[i];
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                Assert.IsTrue(tree.Keys.Contains(hashes[i]));
                Assert.IsTrue(tree.Values.Contains(values[i]));
                tree.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                values[i]       = Hash.GetHash();
                tree[hashes[i]] = values[i];
                Assert.IsTrue(tree.Count == size);
            }

            Console.WriteLine("Checking...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree[hashes[i]] == values[i]);
                Assert.IsTrue(tree.Keys.Contains(hashes[i]));
                Assert.IsTrue(tree.Values.Contains(values[i]));
            }

            Console.WriteLine("Validating...");

            tree.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree.Count == size - i);
                Assert.IsTrue(tree.ContainsKey(hashes[i]));
                Assert.IsTrue(tree[hashes[i]] != default(string));
                Assert.IsTrue(tree.Remove(hashes[i]));
                Assert.IsFalse(tree.Keys.Contains(hashes[i]));
                Assert.IsFalse(tree.Values.Contains(values[i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(tree[hashes[j]].Contains(hashes[j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(tree.Remove(hashes[j]));
                    }
                }

                Assert.IsTrue(tree[hashes[i]] == default(string));
                tree.Validate();
            }

            Serializer.WriteXmlData(tree, nameof(tree));
            tree = Serializer.ReadXmlData <AVLTree <string, string> >(nameof(tree));

            tree.Validate();
        }