예제 #1
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);
        }
예제 #2
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);
            }
        }