Exemple #1
0
        public void MultipleTasksCanMakeChangesConcurrently()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            var tasks = new[] {
                Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 50; i++)
                    {
                        Assert.True(diffSet.Remove(i));
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    for (int i = 100; i < 150; i++)
                    {
                        Assert.True(diffSet.Add(i));
                    }
                })
            };

            Task.WaitAll(tasks);

            Assert.Equal(100, diffSet.GetSnapshot().Count);
            for (int i = 50; i < 150; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            diffPair = diffSet.GetDiff();

            Assert.Equal(50, diffPair.Added.Count);
            Assert.Equal(50, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 50; i++)
            {
                Assert.False(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.True(diffPair.Removed.Contains(i));
            }
            for (int i = 50; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
            for (int i = 100; i < 50; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
Exemple #2
0
        public void UsingNullItemsFail()
        {
            Assert.Throws <ArgumentNullException>(() => new DiffSet <string>(new[] { "this", "should", "fail", null }));

            var diffSet = new DiffSet <string>(new[] { "this", "should", "succeed" });

            Assert.Throws <ArgumentNullException>(() => diffSet.Add(null));
            Assert.Throws <ArgumentNullException>(() => diffSet.Remove(null));
            Assert.Throws <ArgumentNullException>(() => diffSet.Contains(null));
        }
Exemple #3
0
        public void AddingAndRemovingSameItemMultipleTimesShowsUpOnceInTheDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.False(diffSet.Add(0));     // no-op
            Assert.True(diffSet.Remove(99));
            Assert.False(diffSet.Remove(99)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(99, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 99; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.False(diffSet.Add(1));      // no-op
            Assert.True(diffSet.Add(99));
            Assert.False(diffSet.Add(99));     // no-op
            Assert.False(diffSet.Remove(101)); // no-op
            Assert.True(diffSet.Remove(0));
            Assert.False(diffSet.Remove(0));   // no-op


            Assert.Equal(99, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(1, diffPair.Added.Count);
            Assert.Equal(1, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            Assert.True(diffPair.Added.Contains(99));
            Assert.True(diffPair.Removed.Contains(0));
            for (int i = 1; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #4
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(0, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #5
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            // (0-100)
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            // (0-100)
            var changes = diffSet.DetectChanges();

            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #6
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(0, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #7
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var changes = diffSet.DetectChanges();
            var items   = diffSet.GetSnapshot();

            Assert.True(changes);
            Assert.Equal(100, items.Count);
            Assert.False(diffSet.Contains(0));

            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #8
0
        public void InitializeDiffSetWithChangingIEnumerable()
        {
            var diffSet = new DiffSet <int>(new NonResettingEnumerator().Ints(5));

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            var diffPair = diffSet.GetDiff();

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
Exemple #9
0
        public void AddingAndRemovingSameItemMultipleTimesShowsUpOnceInTheDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.False(diffSet.Add(0)); // no-op
            Assert.True(diffSet.Remove(99));
            Assert.False(diffSet.Remove(99)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(99, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 99; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }

            Assert.False(diffSet.Add(1)); // no-op
            Assert.True(diffSet.Add(99));
            Assert.False(diffSet.Add(99)); // no-op
            Assert.False(diffSet.Remove(101)); // no-op
            Assert.True(diffSet.Remove(0));
            Assert.False(diffSet.Remove(0)); // no-op

            Assert.Equal(99, diffSet.GetSnapshot().Count);

            diffPair = diffSet.GetDiff();
            Assert.Equal(1, diffPair.Added.Count);
            Assert.Equal(1, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            Assert.True(diffPair.Added.Contains(99));
            Assert.True(diffPair.Removed.Contains(0));
            for (int i = 1; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #10
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet <int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #11
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));
            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var changes = diffSet.DetectChanges();
            var items = diffSet.GetSnapshot();
            Assert.True(changes);
            Assert.Equal(100, items.Count);
            Assert.False(diffSet.Contains(0));

            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #12
0
        public void AddingAndRemovingSameItemDoesNotShowUpInDiff()
        {
            // (0-100)
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(98));
            Assert.True(diffSet.Remove(99));
            Assert.True(diffSet.Remove(100));
            Assert.True(diffSet.Add(99));
            Assert.True(diffSet.Add(98));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            // (0-100)
            var changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            Assert.True(diffSet.Add(150));
            Assert.True(diffSet.Add(200));
            Assert.True(diffSet.Remove(50));
            Assert.True(diffSet.Remove(200));
            Assert.True(diffSet.Remove(150));
            Assert.True(diffSet.Add(50));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            changes = diffSet.DetectChanges();
            Assert.False(changes);

            for (int i = 0; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #13
0
        public void UsingNullItemsFail()
        {
            Assert.Throws<ArgumentNullException>(() => new DiffSet<string>(new[] {"this", "should", "fail", null}));

            var diffSet = new DiffSet<string>(new[] {"this", "should", "succeed"});

            Assert.Throws<ArgumentNullException>(() => diffSet.Add(null));
            Assert.Throws<ArgumentNullException>(() => diffSet.Remove(null));
            Assert.Throws<ArgumentNullException>(() => diffSet.Contains(null));
        }
Exemple #14
0
        public void MultipleTasksCanMakeChangesConcurrently()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));

            var diffPair = diffSet.GetDiff();

            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);

            var tasks = new[] {
                Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 50; i++)
                    {
                        Assert.True(diffSet.Remove(i));
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    for (int i = 100; i < 150; i++)
                    {
                        Assert.True(diffSet.Add(i));
                    }
                })
            };

            Task.WaitAll(tasks);

            Assert.Equal(100, diffSet.GetSnapshot().Count);
            for (int i = 50; i < 150; i++)
            {
                Assert.True(diffSet.Contains(i));
            }

            diffPair = diffSet.GetDiff();

            Assert.Equal(50, diffPair.Added.Count);
            Assert.Equal(50, diffPair.Removed.Count);
            Assert.False(diffPair.Reset);
            for (int i = 0; i < 50; i++)
            {
                Assert.False(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.True(diffPair.Removed.Contains(i));
            }
            for (int i = 50; i < 100; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.False(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
            for (int i = 100; i < 50; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }
Exemple #15
0
        public void InitialValueCombineWithChangesInFirstDiff()
        {
            var diffSet = new DiffSet<int>(Enumerable.Range(0, 100));
            Assert.True(diffSet.Add(-1));
            Assert.True(diffSet.Add(100));
            Assert.True(diffSet.Remove(0));
            Assert.True(diffSet.Remove(-1));

            Assert.Equal(100, diffSet.GetSnapshot().Count);

            var diffPair = diffSet.GetDiff();
            Assert.Equal(100, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 1; i <= 100; i++)
            {
                Assert.True(diffPair.Added.Contains(i));
                Assert.True(diffSet.Contains(i));
            }
        }
Exemple #16
0
        public void InitializeDiffSetWithChangingIEnumerable()
        {
            var diffSet = new DiffSet<int>(new NonResettingEnumerator().Ints(5));

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            for (int i = 0; i < 5; i++ )
            {
                Assert.True(diffSet.Contains(i));
            }

            var diffPair = diffSet.GetDiff();

            Assert.Equal(5, diffSet.GetSnapshot().Count);
            Assert.Equal(5, diffPair.Added.Count);
            Assert.Equal(0, diffPair.Removed.Count);
            Assert.True(diffPair.Reset);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(diffSet.Contains(i));
                Assert.True(diffPair.Added.Contains(i));
                Assert.False(diffPair.Removed.Contains(i));
            }
        }