Exemple #1
0
        public void MergeDistinctElements()
        {
            var mpA = new MerklePatricia
            {
                ["aa"] = "aa".CompactEncodeString(),
                ["ab"] = "ab".CompactEncodeString(),
                ["ba"] = "ba".CompactEncodeString(),
                ["bb"] = "bb".CompactEncodeString(),
                ["ca"] = "ca".CompactEncodeString()
            };

            Assert.True(mpA.Validade());

            var mpB = new MerklePatricia
            {
                ["za"] = "za".CompactEncodeString(),
                ["ya"] = "ya".CompactEncodeString(),
                ["xa"] = "xa".CompactEncodeString(),
                ["zf"] = "zf".CompactEncodeString(),
                ["ze"] = "ze".CompactEncodeString(),
                ["zb"] = "zb".CompactEncodeString()
            };

            Assert.True(mpB.Validade());
            var mpMerge = mpA.Merge(mpB);

            Assert.True(mpMerge.Validade());
            Assert.False(mpMerge.IsEmpty());
            Assert.NotNull(mpMerge["aa"]);
            Assert.NotNull(mpMerge["zb"]);
            Assert.Null(mpMerge["bala"]);
            Assert.Equal(11, mpMerge.Count());
            Assert.True(mpMerge.Validade());
        }
Exemple #2
0
 private void RunThread(MerklePatricia mp, IEnumerable <string> lista)
 {
     foreach (var it in lista)
     {
         mp[it] = it;
     }
 }
        public void ColideKeys()
        {
            var merklePatricia = new MerklePatricia
            {
                ["oi"] = "batata",
                ["oi"] = "batatatinha"
            };

            Assert.True(merklePatricia.ContainsKey("oi"));
            Assert.Equal("batatatinha", merklePatricia["oi"]);
            Assert.True(merklePatricia.Validade());

            merklePatricia["orelha"] = "batatatinha";
            Assert.Equal("batatatinha", merklePatricia["orelha"]);
            Assert.True(merklePatricia.Validade());

            merklePatricia["orfão"] = "criança";
            Assert.Equal("criança", merklePatricia["orfão"]);
            Assert.True(merklePatricia.Validade());

            merklePatricia["orfanato"] = "crianças";
            Assert.Equal("crianças", merklePatricia["orfanato"]);
            Assert.True(merklePatricia.Validade());

            merklePatricia.Remove("orfanato");
            Assert.Equal("criança", merklePatricia["orfão"]);
            Assert.False(merklePatricia.ContainsKey("orfanato"));
            Assert.True(merklePatricia.Validade());

            merklePatricia["orfã"] = "menina";
            Assert.Equal("menina", merklePatricia["orfã"]);
            Assert.True(merklePatricia.Validade());
        }
        public void PatriciaCount()
        {
            var mp = new MerklePatricia();

            Assert.Equal(0, mp.Count());
            Assert.True(mp.Validade());

            mp["oi"] = "oi";
            Assert.Equal(1, mp.Count());
            Assert.True(mp.Validade());

            mp["oi"] = "oi";
            Assert.Equal(1, mp.Count());
            Assert.True(mp.Validade());

            mp["oi"] = "oi1";
            Assert.Equal(1, mp.Count());

            mp["oi1"] = "oi1";
            mp["oi2"] = "oi2";
            Assert.Equal(3, mp.Count());

            mp["bala"] = "bala2";
            Assert.Equal(4, mp.Count());
            Assert.True(mp.Validade());
        }
        public void PatriciaToString()
        {
            var mp = new MerklePatricia();

            Assert.True(mp.Validade());
            System.Console.WriteLine($"a:\n {mp}");
            mp["oi"] = "bala";
            Assert.True(mp.Validade());
            System.Console.WriteLine($"a:\n {mp}");
            mp["oi1"] = "bala1";
            System.Console.WriteLine($"a:\n {mp}");
            mp["oi2"] = "bala2";
            System.Console.WriteLine($"a:\n {mp}");
            mp["oi12"] = "bala12";
            Assert.True(mp.Validade());
            System.Console.WriteLine($"a:\n {mp}");
            mp["bola"] = "123bala12";
            System.Console.WriteLine($"a:\n {mp}");
            mp["birosca123"] = "13bala12";
            System.Console.WriteLine($"a:\n {mp}");
            mp["ca123"] = "3bala12";
            mp["oi123"] = "asfbala12";
            Assert.True(mp.Validade());
            System.Console.WriteLine($"a:\n {mp}");
        }
        public void Dictionary()
        {
            var exemplo = new Dictionary <string, string>
            {
                ["oi"]         = "bala",
                ["oi1"]        = "bala1",
                ["oi2"]        = "bala2",
                ["oi12"]       = "bala12",
                ["bola"]       = "oi",
                ["birosca123"] = "bruca123",
                ["ca123"]      = "que123",
                ["oi123"]      = "bala123"
            };

            var merklePatricia = new MerklePatricia();

            foreach (var keyValue in exemplo)
            {
                merklePatricia[keyValue.Key] = keyValue.Value;
            }
            Assert.True(merklePatricia.Validade());

            foreach (var keyValue in exemplo)
            {
                Assert.Equal(keyValue.Value, merklePatricia[keyValue.Key]);
            }
            Assert.True(merklePatricia.Validade());
        }
        public void One()
        {
            var merklePatricia = new MerklePatricia();

            Assert.True(merklePatricia.Validade());
            Assert.Equal(0, merklePatricia.Height());

            void InserirTestar(string x, string y)
            {
                merklePatricia[x] = y;
                Assert.True(merklePatricia.Validade());
                Assert.True(merklePatricia.ContainsKey(x));
                Assert.False(merklePatricia.ContainsKey(x + "123"));
                Assert.Equal(y, merklePatricia[x]);
                Assert.Null(merklePatricia[x + "123k"]);
            }

            InserirTestar("01a2", "valor1");
            Assert.Equal(1, merklePatricia.Height());
            Assert.True(merklePatricia.Validade());

            InserirTestar("11a2", "valor2");
            Assert.True(merklePatricia.Validade());

            InserirTestar("0212", "valor3");
//            Assert.Equal(3, merklePatricia.Height());
            Assert.True(merklePatricia.Validade());

            merklePatricia["0"] = "valor4";
            Assert.True(merklePatricia.Validade());
        }
        public void Lista()
        {
            var lista = new[] { "oi", "oi1", "oi2", "oi12", "bola", "birosca123", "ca123", "oi123" };
            var mp    = new MerklePatricia();

            foreach (var it in lista)
            {
                mp[it] = it.CompactEncodeString();
                System.Console.WriteLine($"{mp}");
                Assert.True(mp.Validade());
            }
        }
        public void EqualsThree()
        {
            var mpA = new MerklePatricia
            {
                ["oi"]  = "bola",
                ["oi1"] = "1bola",
                ["oi2"] = "b2ola",
                ["oi1"] = "bola1"
            };

            Assert.True(mpA.Validade());

            var mpB = new MerklePatricia
            {
                ["oi"]  = "bola",
                ["oi1"] = "1bola",
                ["oi2"] = "b2ola",
                ["oi1"] = "bola1"
            };

            Assert.True(mpB.Validade());
            Assert.Equal(mpA, mpB);

            mpA["oi"] = "escola";
            Assert.NotEqual(mpA, mpB);
            Assert.True(mpA.Validade());

            mpB["oi"] = "escola";
            Assert.Equal(mpA, mpB);
            Assert.True(mpB.Validade());

            mpA["oi123"] = "escola";
            mpA["oi12"]  = "escola1";
            mpA["bola"]  = "escola2";
            mpA["dog"]   = "escola2";
            Assert.True(mpA.Validade());

            mpB["bola"]  = "escola2";
            mpB["dog"]   = "escola2";
            mpB["oi12"]  = "escola1";
            mpB["oi123"] = "escola";
            Assert.Equal(mpA, mpB);
            Assert.True(mpB.Validade());

            mpA.Remove("oi");
            mpA.Remove("oi");
            Assert.NotEqual(mpA, mpB);
            Assert.True(mpA.Validade());

            mpB.Remove("oi");
            Assert.Equal(mpA, mpB);
            Assert.True(mpB.Validade());
        }
Exemple #10
0
        public void MergeSameElements()
        {
            var mpA = new MerklePatricia
            {
                ["oi"]    = "ola",
                ["oi1"]   = "ola1",
                ["oi2"]   = "ola2",
                ["oi3"]   = "ola3",
                ["bola"]  = "bola0",
                ["bola1"] = "bola1",
                ["bola2"] = "bola2",
                ["zza"]   = "zza123",
                ["o"]     = "o0",
                ["of"]    = "of0"
            };

            Assert.True(mpA.Validade());
            var mpB = new MerklePatricia
            {
                ["oi1"]   = "ola1",
                ["oi"]    = "ola",
                ["oi3"]   = "ola3",
                ["o"]     = "o0",
                ["zza"]   = "zza123",
                ["bola1"] = "bola1",
                ["of"]    = "of0",
                ["bola"]  = "bola0",
                ["bola2"] = "bola2",
                ["oi2"]   = "ola2"
            };

            Assert.True(mpB.Validade());

            var mpMerge = mpA.Merge(mpB);

            Assert.True(mpMerge.Validade());
            Assert.False(mpMerge.IsEmpty());
            Assert.NotNull(mpMerge["oi"]);
            Assert.Null(mpMerge["bala"]);
            Assert.Equal(10, mpMerge.Count());
            Assert.True(mpMerge.Validade());

            Assert.Equal("ola", mpMerge["oi"]);
            Assert.Equal("ola1", mpMerge["oi1"]);
            Assert.Equal("ola2", mpMerge["oi2"]);
            Assert.Equal("ola3", mpMerge["oi3"]);
            Assert.Equal("bola0", mpMerge["bola"]);
            Assert.Equal("bola1", mpMerge["bola1"]);
            Assert.Equal("bola2", mpMerge["bola2"]);
            Assert.Equal("o0", mpMerge["o"]);
            Assert.Equal("zza123", mpMerge["zza"]);
        }
Exemple #11
0
        public void MergeEmpty()
        {
            var mpA     = new MerklePatricia();
            var mpB     = new MerklePatricia();
            var mpMerge = mpA.Merge(mpB);

            Assert.True(mpMerge.IsEmpty());
            Assert.True(mpMerge.Validade());
            Assert.Null(mpMerge["oi"]);
            Assert.Null(mpMerge["bala"]);
            Assert.Null(mpMerge[""]);
            Assert.True(mpMerge.Validade());
        }
Exemple #12
0
        public void Remove()
        {
            var mp = new MerklePatricia();

            Assert.True(mp.Validade());

            void RemoverTestar(string x, string y)
            {
                mp[x] = y;
                Assert.True(mp.ContainsKey(x));
                Assert.False(mp.ContainsKey(x + "123"));
                Assert.Equal(y, mp[x]);
                Assert.Null(mp[x + "123k"]);

                Assert.True(mp.Remove(x));
                Assert.False(mp.Remove(x));
            }

            RemoverTestar("oi", "bala");
            Assert.True(mp.Validade());
            mp.Remove("oi");
            Assert.False(mp.ContainsKey("oi"));
            Assert.True(mp.Validade());

            mp["123"]  = "abc";
            mp["a123"] = "1abc";
            Assert.Equal(2, mp.Count());
            Assert.True(mp.Validade());

            Assert.False(mp.Remove("b123"));
            Assert.Equal(2, mp.Count());
            Assert.True(mp.Remove("a123"));
            Assert.True(mp.Validade());
            Assert.Equal(1, mp.Count());
            Assert.False(mp.ContainsKey("a123"));
            Assert.True(mp.ContainsKey("123"));
            Assert.True(mp.ContainsKey("123"));
            Assert.True(mp.Validade());

            var mp2 = new MerklePatricia {
                ["123"] = "abc"
            };

            Assert.Equal(mp2, mp);
            Assert.True(mp.Validade());
            Assert.True(mp2.Validade());
        }
Exemple #13
0
        public void ContainsValue()
        {
            var mp = new MerklePatricia
            {
                ["aoi"]  = "oi",
                ["boi2"] = "oi2",
                ["coi1"] = "oi3"
            };

            Assert.True(mp.Validade());
            Assert.True(mp.ContainsValue("oi"));
            Assert.True(mp.ContainsValue("oi2"));
            Assert.True(mp.ContainsValue("oi3"));

            Assert.False(mp.ContainsValue("aoi"));
            Assert.False(mp.ContainsValue("boi2"));
            Assert.False(mp.ContainsValue("coi3"));
            Assert.True(mp.Validade());
        }
Exemple #14
0
        public void Merge()
        {
            var mpA = new MerklePatricia
            {
                ["oi"]    = "oi".CompactEncodeString(),
                ["oi1"]   = "oi1".CompactEncodeString(),
                ["oi2"]   = "oi2".CompactEncodeString(),
                ["oi3"]   = "oi3".CompactEncodeString(),
                ["bola"]  = "bola".CompactEncodeString(),
                ["bola1"] = "bola1".CompactEncodeString(),
                ["bola2"] = "bola2".CompactEncodeString(),
                ["zza"]   = "zza".CompactEncodeString(),
                ["o"]     = "o".CompactEncodeString(),
                ["of"]    = "of".CompactEncodeString()
            };

            Assert.True(mpA.Validade());
            var mpB = new MerklePatricia
            {
                ["o"]     = "o".CompactEncodeString(),
                ["bola1"] = "bola1".CompactEncodeString(),
                ["of"]    = "of".CompactEncodeString(),
                ["bola"]  = "bola".CompactEncodeString(),
                ["cao2"]  = "cao2".CompactEncodeString(),
                ["cao"]   = "cao".CompactEncodeString(),
                ["cao1"]  = "cao1".CompactEncodeString(),
                ["bola2"] = "bola2".CompactEncodeString(),
                ["oi2"]   = "oi2".CompactEncodeString()
            };

            Assert.True(mpB.Validade());

            var mpMerge = mpA.Merge(mpB);

            Assert.False(mpMerge.IsEmpty());
            Assert.NotNull(mpMerge["oi"]);
            Assert.Null(mpMerge["bala"]);
//            Assert.Equal(13, mpMerge.Count());
            Assert.True(mpMerge.Validade());

            Assert.Equal("oi".CompactEncodeString(), mpMerge["oi"]);
            Assert.Equal("oi1".CompactEncodeString(), mpMerge["oi1"]);
            Assert.Equal("oi2".CompactEncodeString(), mpMerge["oi2"]);
            Assert.Equal("oi3".CompactEncodeString(), mpMerge["oi3"]);
            Assert.Equal("bola".CompactEncodeString(), mpMerge["bola"]);
            Assert.Equal("bola1".CompactEncodeString(), mpMerge["bola1"]);
            Assert.Equal("bola2".CompactEncodeString(), mpMerge["bola2"]);
            Assert.Equal("o".CompactEncodeString(), mpMerge["o"]);
            Assert.Equal("of".CompactEncodeString(), mpMerge["of"]);
            Assert.Equal("zza".CompactEncodeString(), mpMerge["zza"]);

            Assert.Equal("cao".CompactEncodeString(), mpMerge["cao"]);
            Assert.Equal("cao1".CompactEncodeString(), mpMerge["cao1"]);
            Assert.Equal("cao2".CompactEncodeString(), mpMerge["cao2"]);

            var mpC = new MerklePatricia
            {
                ["oi"]    = "oi".CompactEncodeString(),
                ["oi1"]   = "oi1".CompactEncodeString(),
                ["oi2"]   = "oi2".CompactEncodeString(),
                ["oi3"]   = "oi3".CompactEncodeString(),
                ["bola"]  = "bola".CompactEncodeString(),
                ["bola1"] = "bola1".CompactEncodeString(),
                ["bola2"] = "bola2".CompactEncodeString(),
                ["zza"]   = "zza".CompactEncodeString(),
                ["o"]     = "o".CompactEncodeString(),
                ["of"]    = "of".CompactEncodeString(),
                ["o"]     = "o".CompactEncodeString(),
                ["bola1"] = "bola1".CompactEncodeString(),
                ["of"]    = "of".CompactEncodeString(),
                ["bola"]  = "bola".CompactEncodeString(),
                ["cao2"]  = "cao2".CompactEncodeString(),
                ["cao"]   = "cao".CompactEncodeString(),
                ["cao1"]  = "cao1".CompactEncodeString(),
                ["bola2"] = "bola2".CompactEncodeString(),
                ["oi2"]   = "oi2".CompactEncodeString()
            };

            Assert.True(mpC.Validade());

            var mpD = new MerklePatricia
            {
                ["bola"]  = "bola".CompactEncodeString(),
                ["bola1"] = "bola1".CompactEncodeString(),
                ["bola2"] = "bola2".CompactEncodeString(),
                ["cao"]   = "cao".CompactEncodeString(),
                ["cao1"]  = "cao1".CompactEncodeString(),
                ["cao2"]  = "cao2".CompactEncodeString(),
                ["o"]     = "o".CompactEncodeString(),
                ["of"]    = "of".CompactEncodeString(),
                ["oi"]    = "oi".CompactEncodeString(),
                ["oi1"]   = "oi1".CompactEncodeString(),
                ["oi2"]   = "oi2".CompactEncodeString(),
                ["oi3"]   = "oi3".CompactEncodeString(),
                ["zza"]   = "zza".CompactEncodeString()
            };

            Assert.True(mpD.Validade());
//            System.Console.WriteLine($"C:{mpC}");
//            System.Console.WriteLine($"merge:{mpMerge}");
            Assert.Equal(mpC, mpD);

            Assert.Equal(mpC, mpMerge);
        }
Exemple #15
0
        public void ParallelThreds()
        {
            var amount     = 10000;
            var prefixSize = 20;
            var size       = 40;
            var prefixA    = StringTools.RandomString(prefixSize);
            var prefixB    = StringTools.RandomString(prefixSize);
            var prefixC    = StringTools.RandomString(prefixSize);
            var prefixD    = StringTools.RandomString(prefixSize);

            for (var i = 0; i < amount; i++)
            {
                listaA.Add(prefixA + StringTools.RandomString(size));
                listaB.Add(prefixB + StringTools.RandomString(size));
                listaC.Add(prefixC + StringTools.RandomString(size));
                listaD.Add(prefixD + StringTools.RandomString(size));
            }

            var threadA            = new Thread(RunThreadA);
            var threadB            = new Thread(RunThreadB);
            var threadC            = new Thread(RunThreadC);
            var threadD            = new Thread(RunThreadD);
            var timeThreds         = Stopwatch.StartNew();
            var timeThredsAndMerge = Stopwatch.StartNew();

            threadA.Start();
            threadB.Start();
            threadC.Start();
            threadD.Start();

            System.Console.WriteLine("Começou");

            threadA.Join();
            threadB.Join();
            threadC.Join();
            threadD.Join();

            timeThreds.Stop();
            var mpMerged = mpA.Merge(mpB);

            mpMerged = mpMerged.Merge(mpC);
            mpMerged = mpMerged.Merge(mpD);
            timeThredsAndMerge.Stop();

            var timeNoThred = Stopwatch.StartNew();
            var mpNoThread  = new MerklePatricia();

            RunThread(mpNoThread, listaA);
            RunThread(mpNoThread, listaB);
            RunThread(mpNoThread, listaC);
            RunThread(mpNoThread, listaD);
            timeNoThred.Stop();

            System.Console.WriteLine($"time using threds: {timeThreds.ElapsedMilliseconds}ms");
            System.Console.WriteLine(
                $"time using threds: {timeThredsAndMerge.ElapsedMilliseconds}ms (with merge time)");
            System.Console.WriteLine($"time using no thred: {timeNoThred.ElapsedMilliseconds}ms");
            System.Console.WriteLine(
                $"{timeThreds.ElapsedMilliseconds},{timeThredsAndMerge.ElapsedMilliseconds},{timeNoThred.ElapsedMilliseconds}");
            System.Console.WriteLine(
                $"{timeNoThred.ElapsedMilliseconds}/{timeThredsAndMerge.ElapsedMilliseconds}={(double) timeNoThred.ElapsedMilliseconds / timeThredsAndMerge.ElapsedMilliseconds}");

            Assert.Equal(mpMerged, mpNoThread);
        }
Exemple #16
0
        public void OperatorEqual()
        {
            var merklePatricia = new MerklePatricia();

            Assert.False(merklePatricia == null);
        }