예제 #1
0
        public void TestIEnumeratorT()
        {
            var queue = new TreeQueue <int>();
            IEnumerator <int> enumerator = queue.GetEnumerator();

            Assert.Equal(0, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);

            // Adding an item to the queue invalidates it, but Current is still unchecked
            queue.Enqueue(1);
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
            Assert.Equal(0, enumerator.Current);

            enumerator = queue.GetEnumerator();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);

            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
        }
예제 #2
0
        public void TestEnumerator()
        {
            var queue = new TreeQueue <int>();

            TreeQueue <int> .Enumerator enumerator = queue.GetEnumerator();
            Assert.Equal(0, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);

            // Adding an item to the queue invalidates it, but Current is still unchecked
            queue.Enqueue(1);
            Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);

            enumerator = queue.GetEnumerator();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);

            // Reset has no effect due to boxing the value type
            ((IEnumerator <int>)enumerator).Reset();
            Assert.Equal(1, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
        }
예제 #3
0
        public void GenerateTreeMakesNewTree()
        {
            TreeQueue <int> q = new TreeQueue <int>();

            q.EnQueue(1);
            q.EnQueue(2);
            q.EnQueue(3);
            q.EnQueue(4);
            q.EnQueue(5);
            q.EnQueue(6);
            q.EnQueue(7);
            q.EnQueue(8);
            q.EnQueue(9);

            BinaryTree <int> result = q.GenerateTree();


            Assert.Equal(1, result.Root.Value);
            Assert.Equal(2, result.Root.Left.Value);
            Assert.Equal(3, result.Root.Right.Value);
            Assert.Equal(4, result.Root.Left.Left.Value);
            Assert.Equal(5, result.Root.Left.Right.Value);
            Assert.Equal(6, result.Root.Right.Left.Value);
            Assert.Equal(7, result.Root.Right.Right.Value);
        }
예제 #4
0
        public void TestQueueLikeBehavior()
        {
            Random          random    = new Random();
            TreeQueue <int> queue     = new TreeQueue <int>(branchingFactor: 4);
            Queue <int>     reference = new Queue <int>();

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int item = random.Next();
                queue.Enqueue(item);
                reference.Enqueue(item);
            }

            while (queue.Count > 0)
            {
                var expected = reference.Peek();
                Assert.Equal(expected, queue.Peek());
                Assert.Equal(expected, reference.Dequeue());
                Assert.Equal(expected, queue.Dequeue());
                queue.Validate(ValidationRules.None);

                Assert.Equal(reference, queue);
            }

            Assert.Empty(queue);
            Assert.Empty(reference);
        }
 internal PersistentCommandDispatcher(TreeQueue queue, TreeQueueThreadPool threadPool, ICommandDistributor distributor, ICommandPublishingStore store, ISerializer formatter, ISchedulingProvider schedulingProvider)
 {
     this.queue              = queue;
     this.threadPool         = threadPool;
     this.distributor        = distributor;
     this.store              = store;
     this.formatter          = formatter;
     this.schedulingProvider = schedulingProvider;
     Initialize();
 }
예제 #6
0
        public void TestTreeQueueBranchingFactorConstructor()
        {
            TreeQueue <int> queue = new TreeQueue <int>(8);

            Assert.Empty(queue);

            Assert.Throws <ArgumentOutOfRangeException>(() => new TreeQueue <int>(-1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new TreeQueue <int>(0));
            Assert.Throws <ArgumentOutOfRangeException>(() => new TreeQueue <int>(1));
        }
예제 #7
0
        private static TreeQueue <T> CreateTreeQueue <T>(IEnumerable <T> source)
        {
            var result = new TreeQueue <T>();

            foreach (T item in source)
            {
                result.Enqueue(item);
            }

            return(result);
        }
예제 #8
0
        public void TestEnqueueStaysPacked()
        {
            TreeQueue <int> queue = new TreeQueue <int>(branchingFactor: 4);

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                queue.Enqueue(i);
            }

            queue.Validate(ValidationRules.RequirePacked);
        }
        private void EnsureInternals()
        {
            if (queue == null)
            {
                queue = new TreeQueue();
            }

            if (threadPool == null)
            {
                threadPool = new TreeQueueThreadPool(queue);
            }
        }
예제 #10
0
        public static BinaryTree <string> FizzTree(BinaryTree <int> input)
        {
            TreeQueue <string> q = new TreeQueue <string>();
            var list             = input.Breadth();

            foreach (int val in list)
            {
                q.EnQueue(Fizzify(val));
            }
            var result = q.GenerateTree();

            return(result);
        }
예제 #11
0
        public void TestEmptyQueue()
        {
            var queue = new TreeQueue <int>();

            Assert.Throws <InvalidOperationException>(() => queue.Peek());
            Assert.Throws <InvalidOperationException>(() => queue.Dequeue());

            Assert.False(queue.TryPeek(out var result));
            Assert.Equal(0, result);

            Assert.False(queue.TryDequeue(out result));
            Assert.Equal(0, result);
        }
예제 #12
0
        public void TestEnqueue()
        {
            const int Value = 600;

            TreeQueue <int> queue = new TreeQueue <int>();

            Assert.Empty(queue);
            queue.Enqueue(Value);
            Assert.Single(queue);
            Assert.Equal(Value, queue.Peek());
            int[] expected = { Value };
            int[] actual   = queue.ToArray();
            Assert.Equal(expected, actual);
        }
예제 #13
0
 public bool MoveNext()
 {
     if (TreeQueue.Any())
     {
         CurrentRoseTree = TreeQueue.Pop().Value;
         foreach (var tree in CurrentRoseTree.Children.Reverse())
         {
             TreeQueue.Push(tree);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
예제 #14
0
 /// <summary>
 /// Creates new instance.
 /// </summary>
 /// <param name="distributor">The command-to-the-queue distributor.</param>
 /// <param name="store">The publishing store for command persistent delivery.</param>
 /// <param name="formatter">The formatter for serializing commands.</param>
 /// <param name="schedulingProvider">The provider of a delay computation for delayed commands.</param>
 public PersistentCommandDispatcher(ICommandDistributor distributor, ICommandPublishingStore store, ISerializer formatter, ISchedulingProvider schedulingProvider, ILogFactory logFactory)
 {
     Ensure.NotNull(distributor, "distributor");
     Ensure.NotNull(store, "store");
     Ensure.NotNull(formatter, "formatter");
     Ensure.NotNull(schedulingProvider, "schedulingProvider");
     Ensure.NotNull(logFactory, "logFactory");
     this.queue              = new TreeQueue();
     this.threadPool         = new TreeQueueThreadPool(queue);
     this.distributor        = distributor;
     this.store              = store;
     this.formatter          = formatter;
     this.schedulingProvider = schedulingProvider;
     this.log = logFactory.Scope("PersistentCommandDispatcher");
     Initialize();
 }
예제 #15
0
        public void TestClear()
        {
            var queue = new TreeQueue <int>(branchingFactor: 3);

            queue.Clear();
            Assert.Empty(queue);

            foreach (int item in Enumerable.Range(0, 10))
            {
                queue.Enqueue(item);
            }

            Assert.NotEmpty(queue);
            queue.Clear();
            Assert.Empty(queue);
        }
예제 #16
0
        private void EnsureInternals()
        {
            if (queue == null)
            {
                queue = new TreeQueue();
            }

            if (threadPool == null)
            {
                threadPool = new TreeQueueThreadPool(queue);
            }

            if (logFactory == null)
            {
                logFactory = new DefaultLogFactory();
            }
        }
예제 #17
0
        public void TestCopyToValidation()
        {
            TreeQueue <int> queue = CreateTreeQueue(Enumerable.Range(0, 10));

            Assert.Throws <ArgumentNullException>("dest", () => queue.CopyTo(null !, 0));
            Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => queue.CopyTo(new int[queue.Count], -1));
            Assert.Throws <ArgumentException>(string.Empty, () => queue.CopyTo(new int[queue.Count], 1));

            ICollection collection = queue;

            Assert.Throws <ArgumentNullException>("dest", () => collection.CopyTo(null !, 0));
            Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => collection.CopyTo(new int[collection.Count], -1));
            Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => collection.CopyTo(Array.CreateInstance(typeof(int), new[] { queue.Count }, new[] { 1 }), 0));
            Assert.Throws <ArgumentException>(string.Empty, () => collection.CopyTo(new int[collection.Count], collection.Count + 1));
            Assert.Throws <ArgumentException>(null, () => collection.CopyTo(new int[queue.Count, 1], 0));
            collection.CopyTo(Array.CreateInstance(typeof(int), new[] { queue.Count }, new[] { 1 }), 1);
        }
예제 #18
0
        public void TestContains()
        {
            Random random = new Random();
            var    queue  = new TreeQueue <int>(branchingFactor: 4);

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int value = random.Next(queue.Count + 1);
                queue.Enqueue(i);

                // Use queue.Contains(i) since this is a targeted collection API test
#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
                Assert.True(queue.Contains(i));
#pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection
            }

            queue.Validate(ValidationRules.RequirePacked);
        }
예제 #19
0
        public void TestTrimExcess()
        {
            Random          random    = new Random();
            TreeQueue <int> queue     = new TreeQueue <int>(branchingFactor: 4);
            Queue <int>     reference = new Queue <int>();

            for (int i = 0; i < (2 * 4 * 4) + 2; i++)
            {
                queue.Enqueue(i);
                reference.Enqueue(i);
            }

            for (int i = 0; i < 2; i++)
            {
                queue.Dequeue();
                reference.Dequeue();
            }

            queue.Validate(ValidationRules.None);

            // In the first call to TrimExcess, items will move
            queue.TrimExcess();
            queue.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, queue);

            // In the second call, the list is already packed so nothing will move
            queue.TrimExcess();
            queue.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, queue);

            TreeQueue <int> empty = new TreeQueue <int>();

            empty.Validate(ValidationRules.RequirePacked);
            empty.TrimExcess();
            empty.Validate(ValidationRules.RequirePacked);

            TreeQueue <int> single = CreateTreeQueue(Enumerable.Range(0, 1));

            single.Validate(ValidationRules.RequirePacked);
            single.TrimExcess();
            single.Validate(ValidationRules.RequirePacked);
        }
예제 #20
0
 public void Reset()
 {
     TreeQueue.Clear();
     CurrentRoseTree = null;
     TreeQueue.Push(new Lazy <RoseTree <T> >(() => OriginalTree));
 }
예제 #21
0
        public void TestTreeQueueConstructor()
        {
            TreeQueue <int> queue = new TreeQueue <int>();

            Assert.Empty(queue);
        }