public void TestSymmetricExceptWith()
        {
            _set.CollectionChanged += (sender, args) =>
            {
                Assert.AreEqual(1, args.AddedItems.Count);
                Assert.AreEqual(1, args.RemovedItems.Count);
                Assert.AreEqual("item 4", args.AddedItems.First());
                Assert.AreEqual("item 2", args.RemovedItems.First());
            };
            _set.SymmetricExceptWith(new[] { "item 2", "item 4" });
            ExpectChangeCalls(1, 0);

            _set.SymmetricExceptWith(new string[0]);
            ExpectChangeCalls(0);
        }
Beispiel #2
0
        public void TestSymmetricExceptWith()
        {
            _set.CollectionChanged += (sender, args) =>
            {
                Assert.AreEqual(NotifyCollectionChangedAction.Replace, args.Action);
                Assert.AreEqual(1, args.NewItems.Count);
                Assert.AreEqual(1, args.OldItems.Count);
                Assert.AreEqual("item 4", args.NewItems[0]);
                Assert.AreEqual("item 2", args.OldItems[0]);
            };
            _set.SymmetricExceptWith(new[] { "item 2", "item 4" });
            ExpectChangeCalls(1);

            _set.SymmetricExceptWith(new string[0]);
            ExpectChangeCalls(0);
        }
Beispiel #3
0
        public void SetOperationsTest()
        {
            const int initialCount = 15;
            const int removeItems  = 4;
            const int insertItems  = 2;
            var       items        = new[] { 40, 63, 98, 20, 24, 76, 96, 53, 5, 11, 29, 12, 46, 59, 7, 45, 86, 91, 57, 95 };
            var       indexes      = new[] { 6, 2, 5, 11, 0, 3, 13, 1 };

            var originalSet = new HashSet <Person>(items.Take(initialCount).Select(x => new Person(x)), new PersonEqualityComparer());
            var set         = new ObservableSet <Person>(originalSet);
            var sorted      = new SortedReadOnlyObservableList <Person>(set, (x, y) => x.Age.CompareTo(y.Age),
                                                                        new PersonEqualityComparer(), nameof(Person.Age));

            Assert.AreEqual(initialCount, set.Count);
            Assert.AreEqual(initialCount, sorted.Count);
            Assert.IsTrue(items.Take(initialCount).Select(x => new Person(x)).OrderBy(p => p.Age).SequenceEqual(sorted, new PersonEqualityComparer()));

            foreach (var person in indexes.Take(removeItems).Select(i => new Person(items[i])))
            {
                set.Remove(person);
            }

            Assert.AreEqual(initialCount - removeItems, set.Count);
            Assert.AreEqual(initialCount - removeItems, sorted.Count);
            Assert.IsTrue(originalSet.OrderBy(p => p.Age).SequenceEqual(sorted, new PersonEqualityComparer()));

            foreach (var person in items.Skip(initialCount).Take(insertItems).Select(x => new Person(x)))
            {
                set.Add(person);
            }

            Assert.AreEqual(initialCount - removeItems + insertItems, set.Count);
            Assert.AreEqual(initialCount - removeItems + insertItems, sorted.Count);
            Assert.IsTrue(originalSet.OrderBy(p => p.Age).SequenceEqual(sorted, new PersonEqualityComparer()));

            foreach (var result in indexes.Skip(removeItems)
                     .Zip(items.Skip(initialCount + insertItems),
                          (i, x) => new { OldItem = new Person(items[i]), NewItem = new Person(x) }))
            {
                set.SymmetricExceptWith(new[] { result.OldItem, result.NewItem });
            }

            Assert.AreEqual(initialCount - removeItems + insertItems, set.Count);
            Assert.AreEqual(initialCount - removeItems + insertItems, sorted.Count);
            Assert.IsTrue(originalSet.OrderBy(p => p.Age).SequenceEqual(sorted, new PersonEqualityComparer()));

            set.Clear();

            Assert.AreEqual(0, set.Count);
            Assert.AreEqual(0, sorted.Count);
        }