Exemplo n.º 1
0
        public void RemoveRange_NotSequentialRemove3_FiresRemoveEvent()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f", "g" });

            var received = new List <NotifyCollectionChangedEventArgs>();

            col.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Remove)
                {
                    received.Add(args);
                }
            };
            col.RemoveRange(new[] { "b", "c", "a", "g" });

            received.Count.Should().Be(3);
            received[0].OldItems.Should().BeEquivalentTo(new[] { "b", "c" });
            received[0].OldStartingIndex.Should().Be(1);
            received[1].OldItems.Should().BeEquivalentTo(new[] { "a" });
            received[1].OldStartingIndex.Should().Be(0);
            received[2].OldItems.Should().BeEquivalentTo(new[] { "g" });
            received[2].OldStartingIndex.Should().Be(3);

            col.Should().BeEquivalentTo("d", "e", "f");
        }
Exemplo n.º 2
0
        public void Write_ReplaceElement_ElementReplaced()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col[2] = "z";

            col.Should().BeEquivalentTo("a", "b", "z");
        }
Exemplo n.º 3
0
        public void Write_RemoveNotExisting_DoesntFail()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "b", "c", "d" });

            col.RemoveRange(new[] { "b", "X" });

            col.Should().BeEquivalentTo("c", "d");
        }
Exemplo n.º 4
0
        public void Write_RemoveRange_ElementsRemoved()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "b", "c", "d" });

            col.RemoveRange(new[] { "b", "c" });

            col.Should().BeEquivalentTo("d");
        }
Exemplo n.º 5
0
        public void Write_InsertElement_ElementInserted()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.Insert(1, "x");

            col.Should().BeEquivalentTo("a", "x", "b", "c");
        }
Exemplo n.º 6
0
        public void Write_RemoveElementAtIndex_ElementRemoved()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.RemoveAt(1);

            col.Should().BeEquivalentTo("a", "c");
        }
Exemplo n.º 7
0
        public void RemoveRange_AcquireRangeToRemoveUsingLinq_RangeRemovedWithoutExceptions()
        {
            var col    = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });
            var select = col.Where(c => c.Equals("c"));

            col.RemoveRange(select);

            col.Should().BeEquivalentTo("a", "b");
        }
Exemplo n.º 8
0
        public void Write_AddRange_ElementsAdded()
        {
            var col      = new ConcurrentObservableCollection <string>();
            var expected = new[] { "a", "b" };

            col.AddRange(expected);

            col.Should().BeEquivalentTo(expected);
        }
Exemplo n.º 9
0
        public void Write_ComplexUpdateOperationFrom2ThreadsAndEnumerationInTheMiddle_CollectionUpdatedProperly()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 100; i < 200; i++)
                {
                    _testOutputHelper.WriteLine("Add {0}", i);
                    col.Add(i);
                }
            });
            var task2 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    _testOutputHelper.WriteLine("Remove {0}", i);
                    col.Remove(i);
                }
            });

            var list  = new List <int>();
            var task3 = new Task(() => col.ForEach(c =>
            {
                _testOutputHelper.WriteLine("Enumerating {0}", c);
                list.Add(c);
            }));

            task1.Start();
            task2.Start();
            task3.Start();

            Task.WaitAll(task1, task2, task3);

            var expected = new int[100];

            for (int i = 100; i < 200; i++)
            {
                expected[i - 100] = i;
            }

            col.Should().BeEquivalentTo(expected, "the collection should be updated properly");
            list.Should().NotBeEmpty("the enumeration should find at least one element");
        }
Exemplo n.º 10
0
        public void Write_ComplexOperation_CollectionUpdatedProperly()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c" });

            col.Add("d");
            col.Remove("b");
            col.Insert(0, "x");
            col.AddRange(new[] { "z", "f", "y" });
            col.RemoveAt(4);
            col.RemoveRange(new[] { "y", "c" });
            col[2] = "p";

            col.Should().BeEquivalentTo("x", "a", "p", "f");
        }
Exemplo n.º 11
0
        public void RemoveRange_AllRemove_FiresResetEvent()
        {
            var col = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f" });

            int received = 0;

            col.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Reset)
                {
                    ++received;
                }
            };

            col.RemoveRange(new[] { "a", "b", "c", "d", "e", "f" });

            received.Should().Be(1);
            col.Should().BeEmpty();
        }
Exemplo n.º 12
0
        public void RemoveRange_RemoveOneElement_FiresRemoveEvent()
        {
            var col      = new ConcurrentObservableCollection <string>(new[] { "a", "b", "c", "d", "e", "f", "g" });
            var received = new List <string>();
            int oldIndex = -1;

            col.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Remove)
                {
                    received.AddRange(args.OldItems.OfType <string>());
                    oldIndex = args.OldStartingIndex;
                }
            };
            col.RemoveRange(new[] { "d" });

            oldIndex.Should().Be(3);
            received.Should().BeEquivalentTo("d");
            col.Should().BeEquivalentTo("a", "b", "c", "e", "f", "g");
        }
Exemplo n.º 13
0
        public void AddRange_5SequentialAdds_CollectionChangeEventsAreReported()
        {
            var col      = new ConcurrentObservableCollection <string>(new[] { "a" });
            var argsList = new List <NotifyCollectionChangedEventArgs>();

            col.CollectionChanged += (sender, args) => { argsList.Add(args); };
            col.AddRange(new[] { "z1", "f1", "y1" });
            col.AddRange(new[] { "z2", "f2", "y2" });
            col.AddRange(new[] { "z3", "f3", "y3" });
            col.AddRange(new[] { "z4", "f4", "y4" });
            col.AddRange(new[] { "z5", "f5", "y5" });

            argsList.Count(x => x.Action == NotifyCollectionChangedAction.Add).Should().Be(5);
            foreach (var args in argsList)
            {
                col.Skip(args.NewStartingIndex).Take(args.NewItems.Count).Should().BeEquivalentTo(args.NewItems.OfType <string>());
            }

            col.Should().BeEquivalentTo("a", "z1", "f1", "y1", "z2", "f2", "y2", "z3", "f3", "y3", "z4", "f4", "y4",
                                        "z5", "f5", "y5");
        }
Exemplo n.º 14
0
        public void Write_ComplexUpdateOperationFrom2Threads_CollectionUpdatedProperly()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 100; i < 200; i++)
                {
                    col.Add(i);
                }
            });
            var task2 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    col.Remove(i);
                }
            });

            task1.Start();
            task2.Start();
            Task.WaitAll(task1, task2);

            var expected = new int[100];

            for (int i = 100; i < 200; i++)
            {
                expected[i - 100] = i;
            }

            col.Should().BeEquivalentTo(expected);
        }