Пример #1
0
        public void AddDiffs()
        {
            // Create object and ensure starting point is as expected
            var diff = new Diff <bool>();

            Assert.Empty(diff.Changes);

            // Add new change and verify its addition
            var change1 = new ValueChange <bool>((1, 2), false, true);

            diff.Add(change1);
            Assert.False(diff.IsCompressed);
            Assert.Single(diff.Changes);
            Assert.Equal(change1, diff.Changes[0]);
            Assert.False(diff.IsFinalized);

            // Add another change (at same location) and verify its addition
            var change2 = new ValueChange <bool>((1, 2), false, true);

            diff.Add(change2);
            Assert.False(diff.IsCompressed);
            Assert.Equal(2, diff.Changes.Count);
            Assert.Equal(change2, diff.Changes[1]);
            Assert.False(diff.IsFinalized);

            diff.FinalizeChanges();
            Assert.True(diff.IsFinalized);
        }
Пример #2
0
        public void CompressionBasic()
        {
            // Create diff and two uncompressable changes
            var diff           = new Diff <int>();
            var initialChange1 = new ValueChange <int>((1, 2), 0, 1);
            var initialChange2 = new ValueChange <int>((5, 6), 0, 1);

            diff.Add(initialChange1);
            diff.Add(initialChange2);
            Assert.Equal(2, diff.Changes.Count);
            Assert.False(diff.IsCompressed);

            // Compress (which should not remove either change since they're not to the same position);
            // but it will mark the diff as fully compressed
            diff.Compress();
            Assert.Equal(2, diff.Changes.Count);
            Assert.True(diff.IsCompressed);

            // Add a compressable change
            var change1TrueToFalse = new ValueChange <int>((1, 2), 1, 0);

            diff.Add(change1TrueToFalse);
            Assert.Equal(3, diff.Changes.Count);
            Assert.False(diff.IsCompressed);

            // Compress (which should remove one position entirely since the changes offset)
            diff.Compress();
            Assert.Single(diff.Changes);
            Assert.True(diff.IsCompressed);
            Assert.Equal(diff.Changes[0], initialChange2);

            // Add a series of changes, 2 of which offset
            var uniquePositionChange    = new ValueChange <int>((20, 20), 0, 1);
            var nonDuplicateForFirstPos = new ValueChange <int>((1, 2), 0, 2);

            diff.Add(initialChange1);
            diff.Add(uniquePositionChange);
            diff.Add(change1TrueToFalse);
            diff.Add(nonDuplicateForFirstPos);
            Assert.Equal(5, diff.Changes.Count);
            Assert.False(diff.IsCompressed);

            // Compress, which should remove the two offsetting changes, keep the most recent one, but not necessarily
            // preserve the relative ordering.
            diff.Compress();
            Assert.Equal(3, diff.Changes.Count);
            Assert.Equal(
                diff.Changes.ToHashSet(),
                new HashSet <ValueChange <int> > {
                initialChange2, uniquePositionChange, nonDuplicateForFirstPos
            });
            Assert.True(diff.IsCompressed);
        }
Пример #3
0
        public void ParseFormBinary(BinaryReader reader)
        {
            Code = (ErrorCode)reader.ReadInt32();
            int Diff_Len = reader.ReadInt32();

            while (Diff_Len-- > 0)
            {
                PlayerItem Diff_Temp = new PlayerItem();
                Diff_Temp = new PlayerItem(); Diff_Temp.ParseFormBinary(reader);
                Diff.Add(Diff_Temp);
            }
            Gold = reader.ReadInt32();
            Coin = reader.ReadInt32();
        }
Пример #4
0
        public void CompressionProducesMinimalSet()
        {
            var rand = new Random();

            // Create diff and add random changes that will have duplicates
            var diff = new Diff <int>();

            var positions     = new Point[] { (1, 2), (5, 6), (20, 21) };
            var currentValues = new Dictionary <Point, int>();

            for (int i = 0; i < 100; i++)
            {
                foreach (var pos in positions)
                {
                    int oldVal = currentValues.GetValueOrDefault(pos);
                    int newVal;
                    do
                    {
                        newVal = rand.Next(1, 6);
                    } while (oldVal == newVal);
                    diff.Add(new ValueChange <int>(pos, oldVal, newVal));
                    currentValues[pos] = newVal;
                }
            }
            Assert.Equal(100 * positions.Length, diff.Changes.Count);

            Assert.False(diff.IsCompressed);

            // We know what the current values are; since they all started at 0 and got changed precisely once,
            // there will always be 3 changes
            diff.Compress();
            Assert.True(diff.IsCompressed);
            Assert.Equal(3, diff.Changes.Count);
            foreach (var change in diff.Changes)
            {
                Assert.True(currentValues.ContainsKey(change.Position));
                Assert.Equal(0, change.OldValue);
                Assert.Equal(currentValues[change.Position], change.NewValue);
            }
        }
Пример #5
0
        public Diff Diff(Mapper input)
        {
            var outMapper = new Diff();

            foreach (var kvp in input)
            {
                var source = kvp.Key;

                log("Diff - Analyze {0} source '{1}'.",
                    source.Type, source.Url);

                var richSource = EnrichWithShas(source, true);

                foreach (var destination in kvp.Value)
                {
                    log("Diff - Analyze {0} target '{1}'.",
                        source.Type, destination.Url);

                    var richDestination = EnrichWithShas(destination, false);

                    if (richSource.Sha == richDestination.Sha)
                    {
                        log("Diff - No sync required. Matching sha ({0}) between target '{1}' and source '{2}.",
                            richSource.Sha.Substring(0, 7), destination.Url, source.Url);

                        continue;
                    }

                    log("Diff - {4} required. Non-matching sha ({0} vs {1}) between target '{2}' and source '{3}.",
                        richSource.Sha.Substring(0, 7), richDestination.Sha == null ? "NULL" : richDestination.Sha.Substring(0, 7), destination.Url, source.Url, richDestination.Sha == null ? "Creation" : "Updation");

                    outMapper.Add(richSource, richDestination);
                }
            }

            return outMapper;
        }