Esempio n. 1
0
        public void Traverse(ushort offset, int n)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();
            var list = new List <ushort>(n);
            var dict = new Dictionary <ushort, Node>();

            for (int i = 0; i < n; ++i)
            {
                var seq = (ushort)(offset + i);
                list.Add(seq);
                var node = new Node()
                {
                    SequenceNumber = new Protocol.Ordinal(seq)
                };
                dict.Add(seq, node);
                Assert.True(tree.TryAdd(node));
                Assert.False(tree.IsEmpty);
            }

            int index = 0;

            Assert.True(tree.Traverse((Node node, ref int s) =>
            {
                Assert.Equal(list[index], (ushort)node.SequenceNumber);
                Assert.Same((object)dict[(ushort)node.SequenceNumber], (object)node);
                index++;
                return(true);
            }, ref index));

            Assert.Equal(n, index);
        }
Esempio n. 2
0
        public void TryRemoveFromEmptyTree(ushort seq)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            Assert.False(tree.TryRemove(new Protocol.Ordinal(seq), out Node node));
            Assert.Null(node);
        }
Esempio n. 3
0
        public void RemoveAndDisposeBefore(ushort offset, int n, int index)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            for (int i = 0; i < n; ++i)
            {
                var seq = new Protocol.Ordinal((ushort)(offset + i));
                Assert.True(tree.TryAdd(new Node()
                {
                    SequenceNumber = seq
                }));
                Assert.False(tree.IsEmpty);
            }

            var keep = new Protocol.Ordinal((ushort)(offset + index));

            tree.RemoveAndDisposeBefore(keep);
            for (int i = 0; i < n; ++i)
            {
                var seq = new Protocol.Ordinal((ushort)(offset + i));
                if (seq < keep)
                {
                    Assert.False(tree.TryGet(seq, out Node node));
                }
                else
                {
                    Assert.True(tree.TryGet(seq, out Node node));
                }

                if (index >= n)
                {
                    Assert.True(tree.IsEmpty);
                }
            }
        }
Esempio n. 4
0
        public void TryAddOrGetMany(ushort offset, int n, int repetitions)
        {
            var random = new Random();

            var dict = new Dictionary <ushort, Node>();
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            for (int r = 0; r < repetitions; ++r)
            {
                for (int i = 0; i < n; ++i)
                {
                    ushort seq;
                    do
                    {
                        seq = (ushort)(offset + random.Next(0, Protocol.Ordinal.Window.Size - 1));
                    }while (dict.ContainsKey(seq));

                    Assert.True(tree.TryAddOrGet(new Protocol.Ordinal(seq), Allocator, out Node node));
                    Assert.NotNull(node);
                    Assert.False(tree.IsEmpty);
                    dict.Add(seq, node);
                }

                foreach (var kv in dict)
                {
                    Assert.False(tree.TryAddOrGet(new Protocol.Ordinal(kv.Key), Allocator, out Node node));
                    Assert.NotNull(node);
                    Assert.False(tree.IsEmpty);
                }
            }
        }
Esempio n. 5
0
        public void TryAddMany(ushort offset, int n)
        {
            var random = new Random();
            var set    = new HashSet <ushort>();
            var tree   = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            for (int i = 0; i < n; ++i)
            {
                ushort seq;
                do
                {
                    seq = (ushort)(offset + random.Next(0, Protocol.Ordinal.Window.Size - 1));
                }while (set.Contains(seq));

                Assert.True(tree.TryAdd(new Node()
                {
                    SequenceNumber = new Protocol.Ordinal(seq)
                }));
                Assert.False(tree.IsEmpty);
                set.Add(seq);
            }

            foreach (var seq in set)
            {
                Assert.False(tree.TryAdd(new Node()
                {
                    SequenceNumber = new Protocol.Ordinal(seq)
                }));
                Assert.False(tree.IsEmpty);
            }
        }
Esempio n. 6
0
        public void TryAddOne(ushort seq)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            Assert.True(tree.TryAdd(new Node()
            {
                SequenceNumber = new Protocol.Ordinal(seq)
            }));
            Assert.False(tree.IsEmpty);
        }
Esempio n. 7
0
        public void TryAddOrGetOne(ushort seq)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            Assert.True(tree.TryAddOrGet(new Protocol.Ordinal(seq), Allocator, out Node node1));
            Assert.NotNull(node1);
            Assert.False(tree.IsEmpty);

            Assert.False(tree.TryAddOrGet(new Protocol.Ordinal(seq), Allocator, out Node node2));
            Assert.False(tree.IsEmpty);

            Assert.Same((object)node1, (object)node2);
        }
Esempio n. 8
0
        public void RemoveAndDisposeAll(ushort offset, int n)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            for (int i = 0; i < n; ++i)
            {
                var seq = new Protocol.Ordinal((ushort)(offset + i));
                Assert.True(tree.TryAdd(new Node()
                {
                    SequenceNumber = seq
                }));
                Assert.False(tree.IsEmpty);
            }

            tree.RemoveAndDisposeAll();
            Assert.True(tree.IsEmpty);
            Assert.Null(tree.First);
        }
Esempio n. 9
0
        public void TryAddManyThenRemove(ushort offset, int n, int repetitions)
        {
            var random = new Random();

            var set  = new HashSet <ushort>();
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            for (int r = 0; r < repetitions; ++r)
            {
                for (int i = 0; i < n; ++i)
                {
                    ushort seq;
                    do
                    {
                        seq = (ushort)(offset + random.Next(0, Protocol.Ordinal.Window.Size - 1));
                    }while (set.Contains(seq));

                    Assert.True(tree.TryAdd(new Node()
                    {
                        SequenceNumber = new Protocol.Ordinal(seq)
                    }));
                    Assert.False(tree.IsEmpty);
                    set.Add(seq);
                }

                foreach (var seq in set)
                {
                    Assert.True(tree.TryRemove(new Protocol.Ordinal(seq), out Node node));
                    Assert.NotNull(node);
                    n--;
                    if (n > 0)
                    {
                        Assert.False(tree.IsEmpty);
                    }
                }

                Assert.True(tree.IsEmpty);
                set.Clear();
            }
        }
Esempio n. 10
0
        public void RemoveAndDisposeAllFromEmptyTree()
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            tree.RemoveAndDisposeAll();
        }
Esempio n. 11
0
        public void RemoveAndDisposeBeforeSequenceNumberFromEmptyTree(ushort seq)
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            tree.RemoveAndDisposeBefore(new Protocol.Ordinal(seq));
        }
Esempio n. 12
0
        public void FirstFromEmptyTree()
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            Assert.Null(tree.First);
        }
Esempio n. 13
0
        public void IsEmptyFromEmptyTree()
        {
            var tree = new Carambolas.Net.Channel.Inbound.Node.Tree <Node>();

            Assert.True(tree.IsEmpty);
        }