PerformActionOnAllQueueWrappers() 공개 정적인 메소드

public static PerformActionOnAllQueueWrappers ( Queue queue, Action action ) : void
queue Queue
action Action
리턴 void
예제 #1
0
        public static void Clone_DequeueThenEnqueue()
        {
            var queue1 = new Queue(100);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                // Insert 50 items in the Queue
                for (int i = 0; i < 50; i++)
                {
                    queue2.Enqueue(i);
                }

                // Insert and Remove 75 items in the Queue. This should wrap the queue
                // where there is 25 at the end of the array and 25 at the beginning
                for (int i = 0; i < 75; i++)
                {
                    queue2.Enqueue(i + 50);
                    queue2.Dequeue();
                }

                Queue queClone = (Queue)queue2.Clone();

                Assert.Equal(50, queClone.Count);
                Assert.Equal(75, queClone.Dequeue());

                // Add an item to the Queue
                queClone.Enqueue(100);
                Assert.Equal(50, queClone.Count);
                Assert.Equal(76, queClone.Dequeue());
            });
        }
예제 #2
0
        public static void Peek()
        {
            string s1  = "hello";
            string s2  = "world";
            char   c   = '\0';
            bool   b   = false;
            byte   i8  = 0;
            short  i16 = 0;
            int    i32 = 0;
            long   i64 = 0L;
            float  f   = (float)0.0;
            double d   = 0.0;

            var queue1 = new Queue();

            queue1.Enqueue(s1);
            queue1.Enqueue(s2);
            queue1.Enqueue(c);
            queue1.Enqueue(b);
            queue1.Enqueue(i8);
            queue1.Enqueue(i16);
            queue1.Enqueue(i32);
            queue1.Enqueue(i64);
            queue1.Enqueue(f);
            queue1.Enqueue(d);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.Same(s1, queue2.Peek());
                queue2.Dequeue();

                Assert.Same(s2, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(c, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(b, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(i8, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(i16, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(i32, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(i64, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(f, queue2.Peek());
                queue2.Dequeue();

                Assert.Equal(d, queue2.Peek());
                queue2.Dequeue();
            });
        }
예제 #3
0
        public static void Peek_EmptyQueue_ThrowsInvalidOperationException()
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.Throws <InvalidOperationException>(() => queue2.Peek()); // Queue is empty
            });
        }
예제 #4
0
        public static void Enqueue_Null()
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.Enqueue(null);
                Assert.Equal(1, queue2.Count);
            });
        }
예제 #5
0
        public static void ToArray_EmptyQueue()
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                object[] arr = queue2.ToArray();
                Assert.Equal(0, arr.Length);
            });
        }
예제 #6
0
        public static void Contains_EmptyQueue()
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                Assert.False(queue2.Contains(101));
                Assert.False(queue2.Contains("hello world"));
                Assert.False(queue2.Contains(null));
            });
        }
예제 #7
0
        public static void Clear(int capacity)
        {
            var queue1 = new Queue(capacity);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.Enqueue(1);
                queue2.Clear();
                Assert.Equal(0, queue2.Count);
            });
        }
예제 #8
0
        public static void ToArray_Wrapped()
        {
            var queue1 = new Queue(1);

            queue1.Enqueue(1);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                object[] arr = queue2.ToArray();
                Assert.Equal(1, arr.Length);
                Assert.Equal(1, arr[0]);
            });
        }
예제 #9
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());
            });
        }
예제 #10
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);
                }
            });
        }
예제 #11
0
        public static void Clone_Empty()
        {
            var queue1 = new Queue();

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

                // Can change the clone queue
                clone.Enqueue(500);
                Assert.Equal(500, clone.Dequeue());
            });
        }
예제 #12
0
        public static void Enqueue(int count)
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                for (int i = 1; i <= count; i++)
                {
                    queue2.Enqueue(i);
                    Assert.Equal(i, queue2.Count);
                }
                Assert.Equal(count, queue2.Count);
            });
        }
예제 #13
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]);
                }
            });
        }
예제 #14
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
            });
        }
예제 #15
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
            });
        }
예제 #16
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]);
                }
            });
        }
예제 #17
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));
            });
        }
예제 #18
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));
                }
            });
        }
예제 #19
0
        public static void Clone_IsShallowCopy()
        {
            var queue1 = new Queue();

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                queue2.Enqueue(new Foo(10));
                Queue clone = (Queue)queue2.Clone();

                var foo      = (Foo)queue2.Dequeue();
                foo.IntValue = 50;

                var fooClone = (Foo)clone.Dequeue();
                Assert.Equal(50, fooClone.IntValue);
            });
        }
예제 #20
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());
            });
        }
예제 #21
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());
            });
        }
예제 #22
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());
            });
        }
예제 #23
0
        public static void ToArrayDiffentObjectTypes()
        {
            string s1  = "hello";
            string s2  = "world";
            char   c   = '\0';
            bool   b   = false;
            byte   i8  = 0;
            short  i16 = 0;
            int    i32 = 0;
            long   i64 = 0L;
            float  f   = (float)0.0;
            double d   = 0.0;

            var queue1 = new Queue();

            queue1.Enqueue(s1);
            queue1.Enqueue(s2);
            queue1.Enqueue(c);
            queue1.Enqueue(b);
            queue1.Enqueue(i8);
            queue1.Enqueue(i16);
            queue1.Enqueue(i32);
            queue1.Enqueue(i64);
            queue1.Enqueue(f);
            queue1.Enqueue(d);

            Helpers.PerformActionOnAllQueueWrappers(queue1, queue2 =>
            {
                object[] arr = queue2.ToArray();
                Assert.Same(s1, arr[0]);
                Assert.Same(s2, arr[1]);
                Assert.Equal(c, arr[2]);
                Assert.Equal(b, arr[3]);
                Assert.Equal(i8, arr[4]);
                Assert.Equal(i16, arr[5]);
                Assert.Equal(i32, arr[6]);
                Assert.Equal(i64, arr[7]);
                Assert.Equal(f, arr[8]);
                Assert.Equal(d, arr[9]);
            });
        }
예제 #24
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();
                }
            });
        }
예제 #25
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());
                }
            });
        }
예제 #26
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);
            });
        }
예제 #27
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();
                }
            });
        }