public void Contains_works()
        {
            var testData = CreateTestData();
            var hashSet  = new ObservableHashSet <int>(testData);

            foreach (var item in testData)
            {
                Assert.True(hashSet.Contains(item));
            }

            foreach (var item in CreateTestData(1000, 10000).Except(testData))
            {
                Assert.False(hashSet.Contains(item));
            }
        }
Пример #2
0
        public void Can_remove()
        {
            var hashSet = new ObservableHashSet <string>
            {
                "Palmer",
                "Carmack"
            };
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 2;
            var countChange       = -1;
            var removing          = Array.Empty <string>();

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Remove, a.Action);
                Assert.Equal(removing, a.OldItems.OfType <string>());
                Assert.Null(a.NewItems);
                collectionChanged++;
            };

            removing = new[] { "Palmer" };
            Assert.True(hashSet.Remove("Palmer"));

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Carmack" }, hashSet);

            removing = new[] { "Carmack" };
            Assert.True(hashSet.Remove("Carmack"));

            Assert.Equal(2, countChanging);
            Assert.Equal(2, countChanged);
            Assert.Equal(2, collectionChanged);
            Assert.Empty(hashSet);

            Assert.False(hashSet.Remove("Palmer"));

            Assert.Equal(2, countChanging);
            Assert.Equal(2, countChanged);
            Assert.Equal(2, collectionChanged);
            Assert.Empty(hashSet);
        }
        public void Can_add()
        {
            var hashSet           = new ObservableHashSet <string>();
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 0;
            var countChange       = 1;
            var adding            = Array.Empty <string>();

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Add, a.Action);
                Assert.Null(a.OldItems);
                Assert.Equal(adding, a.NewItems.OfType <string>());
                collectionChanged++;
            };

            adding = new[] { "Palmer" };
            Assert.True(hashSet.Add("Palmer"));

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Palmer" }, hashSet);

            adding = new[] { "Carmack" };
            Assert.True(hashSet.Add("Carmack"));

            Assert.Equal(2, countChanging);
            Assert.Equal(2, countChanged);
            Assert.Equal(2, collectionChanged);
            Assert.Equal(new[] { "Carmack", "Palmer" }, hashSet.OrderBy(i => i));

            Assert.False(hashSet.Add("Palmer"));

            Assert.Equal(2, countChanging);
            Assert.Equal(2, countChanged);
            Assert.Equal(2, collectionChanged);
            Assert.Equal(new[] { "Carmack", "Palmer" }, hashSet.OrderBy(i => i));
        }
        public void Can_symmetrical_except_with()
        {
            var hashSet = new ObservableHashSet <string>
            {
                "Brendan",
                "Carmack",
                "Nate",
                "Palmer"
            };
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 4;
            var countChange       = -1;
            var removing          = new[] { "Carmack", "Palmer" };
            var adding            = new[] { "Abrash" };

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
                Assert.Equal(removing, a.OldItems.OfType <string>().OrderBy(i => i));
                Assert.Equal(adding, a.NewItems.OfType <string>().OrderBy(i => i));
                collectionChanged++;
            };

            hashSet.SymmetricExceptWith(new[] { "Carmack", "Palmer", "Abrash" });

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Abrash", "Brendan", "Nate" }, hashSet.OrderBy(i => i));

            hashSet.SymmetricExceptWith(Array.Empty <string>());

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Abrash", "Brendan", "Nate" }, hashSet.OrderBy(i => i));
        }
        public void Can_remove_with_predicate()
        {
            var hashSet = new ObservableHashSet <string>
            {
                "Brendan",
                "Carmack",
                "Nate",
                "Palmer"
            };
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 4;
            var countChange       = -2;
            var removing          = new[] { "Carmack", "Palmer" };

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
                Assert.Equal(removing, a.OldItems.OfType <string>().OrderBy(i => i));
                Assert.Empty(a.NewItems);
                collectionChanged++;
            };

            Assert.Equal(2, hashSet.RemoveWhere(i => i.Contains("m")));

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Nate" }, hashSet.OrderBy(i => i));

            Assert.Equal(0, hashSet.RemoveWhere(i => i.Contains("m")));

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Nate" }, hashSet.OrderBy(i => i));
        }
Пример #6
0
        public void Can_union_with()
        {
            var hashSet = new ObservableHashSet <string>
            {
                "Palmer",
                "Carmack"
            };
            var countChanging     = 0;
            var countChanged      = 0;
            var collectionChanged = 0;
            var currentCount      = 2;
            var countChange       = 2;
            var adding            = new[] { "Brendan", "Nate" };

            hashSet.PropertyChanging  += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
            hashSet.PropertyChanged   += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
            hashSet.CollectionChanged += (s, a) =>
            {
                Assert.Equal(NotifyCollectionChangedAction.Replace, a.Action);
                Assert.Empty(a.OldItems);
                Assert.Equal(adding, a.NewItems.OfType <string>().OrderBy(i => i));
                collectionChanged++;
            };

            hashSet.UnionWith(new[] { "Carmack", "Nate", "Brendan" });

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Carmack", "Nate", "Palmer" }, hashSet.OrderBy(i => i));

            hashSet.UnionWith(new[] { "Brendan" });

            Assert.Equal(1, countChanging);
            Assert.Equal(1, countChanged);
            Assert.Equal(1, collectionChanged);
            Assert.Equal(new[] { "Brendan", "Carmack", "Nate", "Palmer" }, hashSet.OrderBy(i => i));
        }