예제 #1
0
        public void ObservableConcurrentHashSetTest()
        {
            Set_CollectionChangedLock        = new object();
            Set_CollectionChangedInvokeCount = 0;
            var set = new ObservableConcurrentHashSet <int>();

            set.CollectionChanged += Set_CollectionChanged;
            try
            {
                // CollectionChanged fire 1
                set.TryAdd(1);
                Assert.Contains(1, set);
                Assert.Single(set);

                // CollectionChanged do not fire
                set.TryAdd(1);
                Assert.Contains(1, set);
                Assert.Single(set);

                // CollectionChanged do not fire
                set.TryRemove(2);
                Assert.Single(set);

                // CollectionChanged fire 2
                set.TryAdd(2);
                Assert.Contains(2, set);
                Assert.Equal(2, set.Count);

                // CollectionChanged fire 3
                set.TryRemove(2);
                Assert.Contains(1, set);
                Assert.DoesNotContain(2, set);
                Assert.Single(set);

                // CollectionChanged fire 4
                set.TryAdd(3);
                Assert.Contains(1, set);
                Assert.Contains(3, set);
                Assert.Equal(2, set.Count);

                // CollectionChanged fire 5
                set.Clear();
                Assert.NotNull(set);
                Assert.Empty(set);
            }
            finally
            {
                set.CollectionChanged -= Set_CollectionChanged;
            }
        }
예제 #2
0
        public void ObservableConcurrentHashSetTest()
        {
            var set = new ObservableConcurrentHashSet <int>();

            set.HashSetChanged += Set_HashSetChanged;
            try
            {
                // HashSetChanged fire 1
                set.TryAdd(1);
                Assert.Contains(1, set);
                Assert.Single(set);

                // HashSetChanged don't fire
                set.TryAdd(1);
                Assert.Contains(1, set);
                Assert.Single(set);

                // HashSetChanged don't fire
                set.TryRemove(2);
                Assert.Single(set);

                // HashSetChanged fire 2
                set.TryAdd(2);
                Assert.Contains(2, set);
                Assert.Equal(2, set.Count);

                // HashSetChanged fire 3
                set.TryRemove(2);
                Assert.Contains(1, set);
                Assert.DoesNotContain(2, set);
                Assert.Single(set);

                // HashSetChanged fire 4
                set.TryAdd(3);
                Assert.Contains(1, set);
                Assert.Contains(3, set);
                Assert.Equal(2, set.Count);

                // HashSetChanged fire 5
                set.Clear();
                Assert.NotNull(set);
                Assert.Empty(set);
            }
            finally
            {
                set.HashSetChanged -= Set_HashSetChanged;
            }
        }