Пример #1
0
        public static void Synchronized()
        {
            Queue queue     = Helpers.CreateIntQueue(100);
            Queue syncQueue = Queue.Synchronized(queue);

            Assert.True(syncQueue.IsSynchronized);
            Assert.Equal(queue.Count, syncQueue.Count);
            for (int i = 0; i < queue.Count; i++)
            {
                Assert.True(syncQueue.Contains(i));
            }
        }
Пример #2
0
        public static void Dequeue_UntilEmpty(int count)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                for (int i = 0; i < count; i++)
                {
                    queue2.Dequeue();
                }
                Assert.Throws <InvalidOperationException>(() => queue2.Dequeue());
            });
        }
Пример #3
0
        public static void Dequeue(int count)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                for (int i = 1; i <= count; i++)
                {
                    int obj = (int)queue2.Dequeue();
                    Assert.Equal(i - 1, obj);
                    Assert.Equal(count - i, queue2.Count);
                }
            });
        }
Пример #4
0
        public static void Contains_NonExistentObject()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.False(queue2.Contains(101));
                Assert.False(queue2.Contains("hello world"));
                Assert.False(queue2.Contains(null));

                queue2.Enqueue(null);
                Assert.False(queue2.Contains(-1)); // We have a null item in the list, so the algorithm may use a different branch
            });
        }
Пример #5
0
        public static void ToArray(int count)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                object[] arr = queue2.ToArray();
                Assert.Equal(count, arr.Length);
                for (int i = 0; i < count; i++)
                {
                    Assert.Equal(queue2.Dequeue(), arr[i]);
                }
            });
        }
Пример #6
0
        public static void CopyTo_Invalid()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.Throws <ArgumentNullException>("array", () => queue2.CopyTo(null, 0));                   // Array is null
                Assert.Throws <ArgumentException>(() => queue2.CopyTo(new object[150, 150], 0));                // Array is multidimensional

                Assert.Throws <ArgumentOutOfRangeException>("index", () => queue2.CopyTo(new object[150], -1)); // Index < 0

                Assert.Throws <ArgumentException>(null, () => queue2.CopyTo(new object[150], 51));              // Index + queue.Count > array.Length
            });
        }
Пример #7
0
        public static void CopyTo(int count, int index)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                var array = new object[count + index];
                queue2.CopyTo(array, index);
                Assert.Equal(count + index, array.Length);
                for (int i = index; i < index + count; i++)
                {
                    Assert.Equal(queue2.Dequeue(), array[i]);
                }
            });
        }
Пример #8
0
        public static void Contains()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                for (int i = 0; i < queue2.Count; i++)
                {
                    Assert.True(queue2.Contains(i));
                }

                queue2.Enqueue(null);
                Assert.True(queue2.Contains(null));
            });
        }
Пример #9
0
        public static void Clone_Clear()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.Clear();

                Queue clone = (Queue)queue2.Clone();
                Assert.Equal(0, clone.Count);

                // Can change clone queue
                clone.Enqueue(500);
                Assert.Equal(500, clone.Dequeue());
            });
        }
Пример #10
0
        public static void Clone()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Queue clone = (Queue)queue2.Clone();
                Assert.Equal(queue2.IsSynchronized, clone.IsSynchronized);

                Assert.Equal(queue2.Count, clone.Count);
                for (int i = 0; i < queue2.Count; i++)
                {
                    Assert.True(clone.Contains(i));
                }
            });
        }
Пример #11
0
        public static void TrimToSize_DequeueAll(int count)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            for (int i = 0; i < count; i++)
            {
                queue1.Dequeue();
            }

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.TrimToSize();
                Assert.Equal(0, queue2.Count);

                // Can change the queue after trimming
                queue2.Enqueue(1);
                Assert.Equal(1, queue2.Dequeue());
            });
        }
Пример #12
0
        public static void Clone_DequeueUntilEmpty()
        {
            Queue queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                for (int i = 0; i < 100; i++)
                {
                    queue2.Dequeue();
                }

                Queue clone = (Queue)queue2.Clone();
                Assert.Equal(0, queue2.Count);

                // Can change clone the queue
                clone.Enqueue(500);
                Assert.Equal(500, clone.Dequeue());
            });
        }
Пример #13
0
        public static void GetEnumerator(int count)
        {
            var queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.NotSame(queue2.GetEnumerator(), queue2.GetEnumerator());
                IEnumerator enumerator = queue2.GetEnumerator();
                for (int i = 0; i < 2; i++)
                {
                    int counter = 0;
                    while (enumerator.MoveNext())
                    {
                        Assert.Equal(counter, enumerator.Current);
                        counter++;
                    }
                    Assert.Equal(count, counter);
                    enumerator.Reset();
                }
            });
        }
Пример #14
0
        public void SynchronizedEnqueue()
        {
            // Enqueue
            _queue = Queue.Synchronized(new Queue());
            PerformTest(StartEnqueueThread, 40);

            // Dequeue
            Queue queue = Helpers.CreateIntQueue(_threadAge);

            _queue = Queue.Synchronized(queue);
            PerformTest(StartDequeueThread, 0);

            // Enqueue, dequeue
            _queue = Queue.Synchronized(new Queue());
            PerformTest(StartEnqueueDequeueThread, 0);

            // Dequeue, enqueue
            queue  = Helpers.CreateIntQueue(_threadAge);
            _queue = Queue.Synchronized(queue);
            PerformTest(StartDequeueEnqueueThread, _threadAge);
        }
Пример #15
0
        public static void GetEnumerator_InMiddleOfEnumeration_Clone()
        {
            Queue queue = Helpers.CreateIntQueue(10);

            IEnumerator enumerator = queue.GetEnumerator();

            enumerator.MoveNext();
            ICloneable cloneableEnumerator = (ICloneable)enumerator;

            // Cloned and original enumerators should start at the same spot, even
            // if the original is in the middle of enumeration.
            IEnumerator clonedEnumerator = (IEnumerator)cloneableEnumerator.Clone();

            Assert.Equal(enumerator.Current, clonedEnumerator.Current);

            for (int i = 0; i < queue.Count - 1; i++)
            {
                Assert.True(clonedEnumerator.MoveNext());
            }

            Assert.False(clonedEnumerator.MoveNext());
        }
Пример #16
0
        public static void TrimToSize(int count)
        {
            Queue queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.TrimToSize();
                Assert.Equal(count, queue2.Count);

                // Can change the queue after trimming
                queue2.Enqueue(100);
                Assert.Equal(count + 1, queue2.Count);
                if (count == 0)
                {
                    Assert.Equal(100, queue2.Dequeue());
                }
                else
                {
                    Assert.Equal(0, queue2.Dequeue());
                }
            });
        }
Пример #17
0
        public static void GetEnumerator_StartOfEnumeration_Clone()
        {
            Queue queue = Helpers.CreateIntQueue(10);

            IEnumerator enumerator          = queue.GetEnumerator();
            ICloneable  cloneableEnumerator = (ICloneable)enumerator;

            IEnumerator clonedEnumerator = (IEnumerator)cloneableEnumerator.Clone();

            Assert.NotSame(enumerator, clonedEnumerator);

            // Cloned and original enumerators should enumerate separately.
            Assert.True(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => clonedEnumerator.Current);

            Assert.True(clonedEnumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);
            Assert.Equal(0, clonedEnumerator.Current);

            // Cloned and original enumerators should enumerate in the same sequence.
            for (int i = 1; i < queue.Count; i++)
            {
                Assert.True(enumerator.MoveNext());
                Assert.NotEqual(enumerator.Current, clonedEnumerator.Current);

                Assert.True(clonedEnumerator.MoveNext());
                Assert.Equal(enumerator.Current, clonedEnumerator.Current);
            }

            Assert.False(enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Equal(queue.Count - 1, clonedEnumerator.Current);

            Assert.False(clonedEnumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            Assert.Throws <InvalidOperationException>(() => clonedEnumerator.Current);
        }
Пример #18
0
        public static void GetEnumerator_Invalid()
        {
            var queue1 = Helpers.CreateIntQueue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                IEnumerator enumerator = queue2.GetEnumerator();
                Assert.Throws <InvalidOperationException>(() => enumerator.Current);

                // If the underlying collection is modified, MoveNext and Reset throw, but Current doesn't
                enumerator.MoveNext();
                object dequeued = queue2.Dequeue();
                Assert.Throws <InvalidOperationException>(() => enumerator.MoveNext());
                Assert.Throws <InvalidOperationException>(() => enumerator.Reset());
                Assert.Equal(dequeued, enumerator.Current);


                // Current throws if the current index is < 0 or >= count
                enumerator = queue2.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ;
                }

                Assert.False(enumerator.MoveNext());
                Assert.False(enumerator.MoveNext());

                Assert.Throws <InvalidOperationException>(() => enumerator.Current);

                // Current throws after resetting
                enumerator = queue2.GetEnumerator();
                Assert.True(enumerator.MoveNext());
                Assert.True(enumerator.MoveNext());

                enumerator.Reset();
                Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            });
        }
Пример #19
0
        public static void TestGetEnumerator(int count)
        {
            var queue1 = Helpers.CreateIntQueue(count);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                IEnumerator enumerator1 = queue2.GetEnumerator();
                IEnumerator enumerator2 = queue2.GetEnumerator();

                IEnumerator[] enumerators = { enumerator1, enumerator2 };
                foreach (IEnumerator enumerator in enumerators)
                {
                    Assert.NotNull(enumerator);
                    int i = 0;
                    while (enumerator.MoveNext())
                    {
                        Assert.Equal(i, enumerator.Current);
                        i++;
                    }
                    Assert.Equal(count, i);
                    enumerator.Reset();
                }
            });
        }