Exemplo n.º 1
0
 /// <summary>
 /// Tests linked list and adjacent nodes for correct linking and ownership.
 /// </summary>
 /// <typeparam name="T">The type of a value for the test container item.</typeparam>
 /// <param name="list">Linked list owning the nodes being tested.</param>
 /// <param name="head">The head node of a container.</param>
 /// <param name="prev">The preceding node adjacent to the source node.</param>
 /// <param name="next">The following node adjacent to the source node.</param>
 /// <param name="tail">The tail node of a container.</param>
 public static void AssertNode <T>(OrderedContainer <T> list, OrderedContainer <T> .OrderedNode head, OrderedContainer <T> .OrderedNode prev, OrderedContainer <T> .OrderedNode next, OrderedContainer <T> .OrderedNode tail)
     where T : IComparable <T>
 {
     Assert.AreEqual(head, list.Head, $"Container Property: {nameof(list.Head)}");
     Assert.AreEqual(tail, list.Tail, $"Container Property: {nameof(list.Tail)}");
     if (prev != null)
     {
         Assert.AreEqual(list, prev.Parent, $"Prev Property: {nameof(prev.Parent)}");
     }
     if (next != null)
     {
         Assert.AreEqual(list, next.Parent, $"Next Property: {nameof(next.Parent)}");
     }
 }
Exemplo n.º 2
0
        public void OrderedListLinks_Decrementor_MiddleUpper()
        {
            var container = new OrderedContainer <double>();
            var node1     = new OrderedContainer <double> .OrderedNode(0).InsertNext(container);

            var node2 = new OrderedContainer <double> .OrderedNode(1).InsertNext(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertNext(container);

            var node4 = new OrderedContainer <double> .OrderedNode(3).InsertNext(container);

            var node5 = new OrderedContainer <double> .OrderedNode(4).InsertNext(container);

            AssertSequence(container.GetDecrement(node5, node2).ToList(), 4, 3, 2, 1);
        }
Exemplo n.º 3
0
        public void OrderedListLinks_Decrementor_OpenLower()
        {
            var container = new OrderedContainer <double>();
            var node1     = new OrderedContainer <double> .OrderedNode(0).InsertNext(container);

            var node2 = new OrderedContainer <double> .OrderedNode(1).InsertNext(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertNext(container);

            var node4 = new OrderedContainer <double> .OrderedNode(3).InsertNext(container);

            var node5 = new OrderedContainer <double> .OrderedNode(4).InsertNext(container);

            AssertSequence(container.GetDecrement(upper: node3).ToList(), 2, 1, 0);
        }
Exemplo n.º 4
0
        public void OrderedListLinks_Incrementor_MiddleLower()
        {
            var container = new OrderedContainer <double>();
            var node1     = new OrderedContainer <double> .OrderedNode(0).InsertNext(container);

            var node2 = new OrderedContainer <double> .OrderedNode(1).InsertNext(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertNext(container);

            var node4 = new OrderedContainer <double> .OrderedNode(3).InsertNext(container);

            var node5 = new OrderedContainer <double> .OrderedNode(4).InsertNext(container);

            AssertSequence(container.GetIncrement(node2, node5).ToList(), 1, 2, 3, 4);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Tests linked list and adjacent nodes for correct linking and ownership.
 /// </summary>
 /// <typeparam name="T">The type of a value for the test container item.</typeparam>
 /// <param name="link">The source node to test.</param>
 /// <param name="prev">The preceding node adjacent to the source node.</param>
 /// <param name="next">The following node adjacent to the source node.</param>
 public static void AssertNode <T>(OrderedContainer <T> .OrderedNode link, OrderedContainer <T> .OrderedNode prev, OrderedContainer <T> .OrderedNode next)
     where T : IComparable <T>
 {
     Assert.IsNotNull(link);
     Assert.AreEqual(prev, link.InternalPrev, $"Node Property: {nameof(link.Prev)}");
     Assert.AreEqual(next, link.InternalNext, $"Node Property: {nameof(link.Next)}");
     if (prev != null)
     {
         Assert.AreEqual(link, prev.InternalNext, $"Prev Property: {nameof(prev.Next)}");
     }
     if (next != null)
     {
         Assert.AreEqual(link, next.InternalPrev, $"Next Property: {nameof(next.Prev)}");
     }
 }
Exemplo n.º 6
0
        public void List_WhenUnsubscribed_ShouldStopReceivingNotifications()
        {
            AsyncContext.Run(async delegate
            {
                var container = new OrderedContainer();
                _realm.Write(() => _realm.Add(container));

                var eventArgs = new List <NotifyCollectionChangedEventArgs>();
                var handler   = new NotifyCollectionChangedEventHandler((sender, e) =>
                {
                    eventArgs.Add(e);
                });

                var propertyEventArgs = new List <string>();
                var propertyHandler   = new PropertyChangedEventHandler((sender, e) => propertyEventArgs.Add(e.PropertyName));

                var collection = container.Items.AsRealmCollection();
                collection.CollectionChanged += handler;
                collection.PropertyChanged   += propertyHandler;

                _realm.Write(() =>
                {
                    container.Items.Add(new OrderedObject());
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));

                collection.CollectionChanged -= handler;
                collection.PropertyChanged   -= propertyHandler;

                _realm.Write(() =>
                {
                    container.Items.Add(new OrderedObject());
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
            });
        }
Exemplo n.º 7
0
        public void OrderedListLinks_InsertPrev_Items2UpperSame2()
        {
            var container = new OrderedContainer <double>();
            var node1     = new OrderedContainer <double> .OrderedNode(0).InsertPrev(container);

            var node4 = new OrderedContainer <double> .OrderedNode(2).InsertPrev(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertPrev(container);

            var node2 = new OrderedContainer <double> .OrderedNode(2).InsertPrev(container);

            AssertNode(node1, container, node1, null, node2, node4);
            AssertNode(node2, container, node1, node1, node3, node4);
            AssertNode(node3, container, node1, node2, node4, node4);
            AssertNode(node4, container, node1, node3, null, node4);
        }
Exemplo n.º 8
0
        public void ListShouldSendNotifications()
        {
            var container = new OrderedContainer();

            _realm.Write(() => _realm.Add(container));
            ChangeSet changes = null;
            NotificationCallbackDelegate <OrderedObject> cb = (s, c, e) => changes = c;

            using (container.Items.SubscribeForNotifications(cb))
            {
                _realm.Write(() => container.Items.Add(new OrderedObject()));

                _realm.Refresh();
                Assert.That(changes, Is.Not.Null);
                Assert.That(changes.InsertedIndices, Is.EquivalentTo(new int[] { 0 }));
            }
        }
Exemplo n.º 9
0
        public void ListShouldSendNotifications()
        {
            var container = new OrderedContainer();

            _realm.Write(() => _realm.Add(container));
            ChangeSet changes = null;

            void OnNotification(IRealmCollection <OrderedObject> s, ChangeSet c, Exception e) => changes = c;

            using (container.Items.SubscribeForNotifications(OnNotification))
            {
                _realm.Write(() => container.Items.Add(new OrderedObject()));

                _realm.Refresh();
                Assert.That(changes, Is.Not.Null);
                Assert.That(changes.InsertedIndices, Is.EquivalentTo(new int[] { 0 }));
            }
        }
Exemplo n.º 10
0
        public void ListShouldSendNotifications()
        {
            AsyncContext.Run(async delegate
            {
                var container = new OrderedContainer();
                _realm.Write(() => _realm.Add(container));
                ChangeSet changes = null;
                NotificationCallbackDelegate <OrderedObject> cb = (s, c, e) => changes = c;

                using (container.Items.SubscribeForNotifications(cb))
                {
                    _realm.Write(() => container.Items.Add(new OrderedObject()));

                    await Task.Delay(MillisecondsToWaitForCollectionNotification);
                    Assert.That(changes, Is.Not.Null);
                    Assert.That(changes.InsertedIndices, Is.EquivalentTo(new int[] { 0 }));
                }
            });
        }
Exemplo n.º 11
0
        public void OrderedListLinks_InsertNext_Items5SequenceReverse()
        {
            var container = new OrderedContainer <double>();
            var node5     = new OrderedContainer <double> .OrderedNode(4).InsertNext(container);

            var node4 = new OrderedContainer <double> .OrderedNode(3).InsertNext(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertNext(container);

            var node2 = new OrderedContainer <double> .OrderedNode(1).InsertNext(container);

            var node1 = new OrderedContainer <double> .OrderedNode(0).InsertNext(container);

            AssertNode(node1, container, node1, null, node2, node5);
            AssertNode(node2, container, node1, node1, node3, node5);
            AssertNode(node3, container, node1, node2, node4, node5);
            AssertNode(node4, container, node1, node3, node5, node5);
            AssertNode(node5, container, node1, node4, null, node5);
        }
Exemplo n.º 12
0
        public void OrderedListLinks_InsertPrev_Items5DenseLeave()
        {
            var container = new OrderedContainer <double>();
            var node1     = new OrderedContainer <double> .OrderedNode(0).InsertPrev(container);

            var node5 = new OrderedContainer <double> .OrderedNode(4).InsertPrev(container);

            var node3 = new OrderedContainer <double> .OrderedNode(2).InsertPrev(container);

            var node2 = new OrderedContainer <double> .OrderedNode(1).InsertPrev(container);

            var node4 = new OrderedContainer <double> .OrderedNode(3).InsertPrev(container);

            AssertNode(node1, container, node1, null, node2, node5);
            AssertNode(node2, container, node1, node1, node3, node5);
            AssertNode(node3, container, node1, node2, node4, node5);
            AssertNode(node4, container, node1, node3, node5, node5);
            AssertNode(node5, container, node1, node4, null, node5);
        }
Exemplo n.º 13
0
        public void TestRealmListNotifications(int[] initial, NotifyCollectionChangedAction action, int[] change, int startIndex)
        {
            AsyncContext.Run(async delegate
            {
                var container = new OrderedContainer();
                foreach (var i in initial)
                {
                    container.Items.Add(new OrderedObject { Order = i });
                }

                _realm.Write(() => _realm.Add(container));

                Exception error = null;
                _realm.Error += (sender, e) =>
                {
                    error = e.Exception;
                };

                var collection = container.Items.AsRealmCollection();

                var eventArgs = new List<NotifyCollectionChangedEventArgs>();
                var propertyEventArgs = new List<string>();

                collection.CollectionChanged += (o, e) => eventArgs.Add(e);
                collection.PropertyChanged += (o, e) => propertyEventArgs.Add(e.PropertyName);

                Assert.That(error, Is.Null);
                _realm.Write(() =>
                {
                    if (action == NotifyCollectionChangedAction.Add)
                    {
                        foreach (var value in change)
                        {
                            container.Items.Add(new OrderedObject
                            {
                                Order = value
                            });
                        }
                    }
                    else if (action == NotifyCollectionChangedAction.Remove)
                    {
                        foreach (var value in change)
                        {
                            container.Items.Remove(_realm.All<OrderedObject>().Single(o => o.Order == value));
                        }
                    }
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);
                Assert.That(error, Is.Null);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                var arg = eventArgs[0];
                if (action == NotifyCollectionChangedAction.Add)
                {
                    Assert.That(arg.Action == action);
                    Assert.That(arg.NewStartingIndex, Is.EqualTo(initial.Length));
                    Assert.That(arg.NewItems.Cast<OrderedObject>().Select(o => o.Order), Is.EquivalentTo(change));
                }
                else if (action == NotifyCollectionChangedAction.Remove)
                {
                    if (startIndex < 0)
                    {
                        Assert.That(arg.Action == NotifyCollectionChangedAction.Reset);
                    }
                    else
                    {
                        Assert.That(arg.Action == action);
                        Assert.That(arg.OldStartingIndex, Is.EqualTo(startIndex));
                        Assert.That(arg.OldItems.Count, Is.EqualTo(change.Length));
                    }
                }

                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
            });
        }
Exemplo n.º 14
0
        // Adds 5 OrderedObject to a List, executes moveAction and returns the single change notification argument.
        private async Task<NotifyCollectionChangedEventArgs> TestMoves(Action<IList<OrderedObject>> moveAction, NotifyCollectionChangedAction expectedAction)
        {
            var container = new OrderedContainer();
            for (var i = 0; i < 5; i++)
            {
                container.Items.Add(new OrderedObject
                {
                    Order = i
                });
            }

            _realm.Write(() => _realm.Add(container));

            var eventArgs = new List<NotifyCollectionChangedEventArgs>();
            var propertyEventArgs = new List<string>();

            var collection = container.Items.AsRealmCollection();
            collection.CollectionChanged += (sender, e) => eventArgs.Add(e);
            collection.PropertyChanged += (sender, e) => propertyEventArgs.Add(e.PropertyName);

            _realm.Write(() => moveAction(container.Items));

            await Task.Delay(MillisecondsToWaitForCollectionNotification);

            Assert.That(eventArgs.Count, Is.EqualTo(1));
            Assert.That(eventArgs[0].Action, Is.EqualTo(expectedAction));
            Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
            Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));

            return eventArgs[0];
        }
Exemplo n.º 15
0
        public void List_WhenTransactionHasBothAddAndRemove_ShouldReset()
        {
            AsyncContext.Run(async delegate
            {
                // The INotifyCollectionChanged API doesn't have a mechanism to report both added and removed items,
                // as that would mess up the indices a lot. That's why when we have both removed and added items,
                // we should raise a Reset.
                var container = new OrderedContainer();
                container.Items.Add(new OrderedObject());
                _realm.Write(() => _realm.Add(container));

                var eventArgs = new List<NotifyCollectionChangedEventArgs>();
                var propertyEventArgs = new List<string>();

                var collection = container.Items.AsRealmCollection();
                collection.CollectionChanged += (sender, e) => eventArgs.Add(e);
                collection.PropertyChanged += (sender, e) => propertyEventArgs.Add(e.PropertyName);

                _realm.Write(() =>
                {
                    container.Items.Clear();
                    container.Items.Add(new OrderedObject());
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Reset));
                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
            });
        }
Exemplo n.º 16
0
        public void List_WhenUnsubscribed_ShouldStopReceivingNotifications()
        {
            AsyncContext.Run(async delegate
            {
                var container = new OrderedContainer();
                _realm.Write(() => _realm.Add(container));

                var eventArgs = new List<NotifyCollectionChangedEventArgs>();
                var handler = new NotifyCollectionChangedEventHandler((sender, e) =>
                {
                    eventArgs.Add(e);
                });

                var propertyEventArgs = new List<string>();
                var propertyHandler = new PropertyChangedEventHandler((sender, e) => propertyEventArgs.Add(e.PropertyName));

                var collection = container.Items.AsRealmCollection();
                collection.CollectionChanged += handler;
                collection.PropertyChanged += propertyHandler;

                _realm.Write(() =>
                {
                    container.Items.Add(new OrderedObject());
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));

                collection.CollectionChanged -= handler;
                collection.PropertyChanged -= propertyHandler;

                _realm.Write(() =>
                {
                    container.Items.Add(new OrderedObject());
                });

                await Task.Delay(MillisecondsToWaitForCollectionNotification);

                Assert.That(eventArgs.Count, Is.EqualTo(1));
                Assert.That(eventArgs[0].Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
                Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
                Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
            });
        }
Exemplo n.º 17
0
        public void TestRealmListNotifications(int[] initial, NotifyCollectionChangedAction action, int[] change, int startIndex)
        {
            var container = new OrderedContainer();

            foreach (var i in initial)
            {
                container.Items.Add(new OrderedObject {
                    Order = i
                });
            }

            _realm.Write(() => _realm.Add(container));

            Exception error = null;

            _realm.Error += (sender, e) =>
            {
                error = e.Exception;
            };

            var collection = container.Items.AsRealmCollection();

            var eventArgs         = new List <NotifyCollectionChangedEventArgs>();
            var propertyEventArgs = new List <string>();

            collection.CollectionChanged += (o, e) => eventArgs.Add(e);
            collection.PropertyChanged   += (o, e) => propertyEventArgs.Add(e.PropertyName);

            Assert.That(error, Is.Null);
            _realm.Write(() =>
            {
                if (action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var value in change)
                    {
                        container.Items.Add(new OrderedObject
                        {
                            Order = value
                        });
                    }
                }
                else if (action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (var value in change)
                    {
                        container.Items.Remove(_realm.All <OrderedObject>().Single(o => o.Order == value));
                    }
                }
            });

            _realm.Refresh();
            Assert.That(error, Is.Null);

            Assert.That(eventArgs.Count, Is.EqualTo(1));
            var arg = eventArgs[0];

            if (action == NotifyCollectionChangedAction.Add)
            {
                Assert.That(arg.Action == action);
                Assert.That(arg.NewStartingIndex, Is.EqualTo(initial.Length));
                Assert.That(arg.NewItems.Cast <OrderedObject>().Select(o => o.Order), Is.EquivalentTo(change));
            }
            else if (action == NotifyCollectionChangedAction.Remove)
            {
                if (startIndex < 0)
                {
                    Assert.That(arg.Action == NotifyCollectionChangedAction.Reset);
                }
                else
                {
                    Assert.That(arg.Action == action);
                    Assert.That(arg.OldStartingIndex, Is.EqualTo(startIndex));
                    Assert.That(arg.OldItems.Count, Is.EqualTo(change.Length));
                }
            }

            Assert.That(propertyEventArgs.Count, Is.EqualTo(2));
            Assert.That(propertyEventArgs, Is.EquivalentTo(new[] { "Count", "Item[]" }));
        }
Exemplo n.º 18
0
        public void ListShouldSendNotifications()
        {
            AsyncContext.Run(async delegate
            {
                var container = new OrderedContainer();
                _realm.Write(() => _realm.Add(container));
                ChangeSet changes = null;
                NotificationCallbackDelegate<OrderedObject> cb = (s, c, e) => changes = c;

                using (container.Items.SubscribeForNotifications(cb))
                {
                    _realm.Write(() => container.Items.Add(new OrderedObject()));

                    await Task.Delay(MillisecondsToWaitForCollectionNotification);
                    Assert.That(changes, Is.Not.Null);
                    Assert.That(changes.InsertedIndices, Is.EquivalentTo(new int[] { 0 }));
                }
            });
        }