Ejemplo n.º 1
0
        public void test_probability_of_collisions_in_tag_produced_by_taking_3_lowest_bytes_from_hashcode_of_guid()
        {
            var dic = new Dictionary <Int32, Guid>();
            var dup = new List <Int32>();

            const int iterations = 1000000;
            var       duplicates = 0;

            for (int i = 0; i < iterations; i++)
            {
                if (i % 100000 == 0)
                {
                    Console.WriteLine(".");
                }

                var guid = Guid.NewGuid();
                var tag  = ProtobufSerializer.GenerateHierarchyTag(guid);

                try
                {
                    dic.Add(tag, guid);
                }
                catch (Exception ex)
                {
                    var originalGuid = dic[tag];
                    duplicates++;
                    dup.Add(tag);
                }

                Assert.That(tag >= 0, Is.True, "Failed on comparing with 0 - {0}", tag);
                Assert.That(tag <= 16777215, Is.True, "Failed on {0}", tag);
            }
            var percent = (double)duplicates / iterations;

            Console.WriteLine("In {0} iterations, {1} duplicated tags. Percent {2}", iterations, duplicates, percent);

            // testing ability to adjust by one:

            var failes = 0;

            foreach (var tag in dup)
            {
                try
                {
                    dic.Add(tag + 1, Guid.Empty);
                }
                catch (Exception ex)
                {
                    failes++;
                }
            }

            //percent = (double)failes / iterations;
            Console.WriteLine("In {0} duplicated tags, {1} can be easely fixed by +1.", dup.Count, dup.Count - failes);
        }
Ejemplo n.º 2
0
        public void should_be_in_valid_range()
        {
            for (int i = 0; i < 100000000; i++)
            {
                var guid = Guid.NewGuid();
                var tag  = ProtobufSerializer.GenerateHierarchyTag(guid);

                Assert.That(tag >= 0, Is.True, "Should be greater or equal to 0, but was {0}", tag);
                Assert.That(tag <= 16777215, Is.True, "Should be lower or equal to 16777215, but was {0}", tag);
            }
        }
        public void hierarchy_tag_should_be_calculated_correctly()
        {
            Action <String, Int32> test = (guidText, tag) =>
            {
                var guid          = Guid.Parse(guidText);
                var calculatedTag = ProtobufSerializer.GenerateHierarchyTag(guid);
                Assert.That(calculatedTag, Is.EqualTo(tag));
            };

            test("{66047c12-32c5-4842-8023-70d1658a836e}", 12661822);
            test("{ea1dbd75-bf2b-4ed9-be6a-ce32c6b3e505}", 13175895);
            test("{decde923-ff46-4c20-a6f6-ac7645e5325b}", 7625384);
            test("{5c69c886-76ef-4458-8afb-dc7cc40501a2}", 7959428);
            test("{628f6c9b-99bb-4bda-90e5-7b406ca0271a}", 13359269);
        }