public void OnRecevice()
        {
            List <PacketReader> packets = new List <PacketReader>();

            lock (ReceviceQueue)
            {
                while (ReceviceQueue.Count > 2)
                {
                    byte[] blen = new byte[2];
                    ReceviceQueue.Dequeue(blen);
                    int    len  = BitConverter.ToUInt16(blen, 0);
                    byte[] data = new byte[len];
                    if (ReceviceQueue.Count >= len)
                    {
                        ReceviceQueue.Dequeue(data);
                        PacketReader packet = new PacketReader(data);
                        packets.Add(packet);
                        //Logger.Debug("add packet");
                    }
                    else
                    {
                        break;
                    }
                }
            }
            ClientEvent.Handler(this, packets);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: viaches/leetcode
        static void Main(string[] args)
        {
            var arrayQueue = new ArrayQueue <string>();

            arrayQueue.Enqueue("1");
            arrayQueue.Enqueue("2");
            arrayQueue.Enqueue("3");
            arrayQueue.Enqueue("4");
            arrayQueue.Enqueue("5");

            arrayQueue.Dequeue();
            arrayQueue.Dequeue();
            arrayQueue.Dequeue();
            arrayQueue.Dequeue();

            var linkedListQueue = new LinkedListQueue <string>();

            linkedListQueue.Enqueue("1");
            linkedListQueue.Enqueue("2");
            linkedListQueue.Enqueue("3");
            linkedListQueue.Enqueue("4");
            linkedListQueue.Enqueue("5");

            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
            linkedListQueue.Dequeue();
        }
コード例 #3
0
        public void TestGrow()
        {
            ArrayQueue <string> queue = new ArrayQueue <string>(3, 5);

            Assert.AreEqual(3, queue.Capacity);

            queue.Enqueue("a");
            queue.Enqueue("b");
            Assert.AreEqual(3, queue.Capacity);
            queue.Enqueue("c");
            Assert.AreEqual(8, queue.Capacity);

            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Dequeue();
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Dequeue();
            }
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
            {
                queue.Enqueue("" + ('d' + i));
            }
            Assert.AreEqual(8, queue.Capacity);

            queue.Enqueue("z");
            Assert.AreEqual(13, queue.Capacity);

            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
            {
                queue.Enqueue("" + ('a' + i));
            }
            Assert.AreEqual(13, queue.Capacity);
            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
            {
                queue.Enqueue("" + ('a' + i));
            }
            Assert.AreEqual(13, queue.Capacity);
        }
コード例 #4
0
        public void QueueIsEnptyWhenAllElementsAreRemoved()
        {
            var sut = new ArrayQueue <int>();

            sut.Enqueue(1);
            sut.Enqueue(2);
            Assert.Equal(1, sut.Dequeue());
            Assert.Equal(2, sut.Dequeue());
            Assert.True(sut.IsEmpty);
        }
コード例 #5
0
 public void DequeueTest() {
     for (int i = 0; i < 6; i++) {
         queue.Enqueue(i);
     }
     
     for(int i = 0; i < 2; i++) {
         queue.Dequeue();
     }
     Assert.AreEqual(2, queue.Dequeue());
     Assert.AreEqual("{3, 4, 5}", queue.ToString());
 }
コード例 #6
0
        public void QueueWorksInFifoOrderWhenIntermittentEnqueuesAccure()
        {
            var sut = new ArrayQueue <int>();

            sut.Enqueue(1);
            sut.Enqueue(2);
            sut.Enqueue(3);
            Assert.Equal(1, sut.Dequeue());
            sut.Enqueue(4);
            sut.Enqueue(5);
            Assert.Equal(2, sut.Dequeue());
            Assert.Equal(3, sut.Dequeue());
            Assert.Equal(4, sut.Dequeue());
            Assert.Equal(5, sut.Dequeue());
        }
コード例 #7
0
        public void Run()
        {
            ArrayQueue queue = new ArrayQueue(5);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            queue.Dequeue();
            queue.Dequeue();
            queue.Enqueue(6);
            queue.Enqueue(7);
            Console.WriteLine(queue.ToString());
        }
コード例 #8
0
        public static void TestArrayQueue()
        {
            ArrayQueue <int> arrayQueue = new ArrayQueue <int>();

            arrayQueue.Enqueue(1);
            arrayQueue.Enqueue(2);
            arrayQueue.Enqueue(3);
            arrayQueue.Enqueue(5);
            arrayQueue.Enqueue(7);
            arrayQueue.PrintAllInQueue();
            arrayQueue.Dequeue();
            arrayQueue.Dequeue();
            arrayQueue.PrintAllInQueue();
            Console.ReadLine();
        }
コード例 #9
0
        public void Dequeue_ValidParamsPassed_Success()
        {
            IQueue <int> queue = new ArrayQueue <int>(5);

            queue.Enqueue(3);
            queue.Enqueue(1);
            queue.Enqueue(4);
            queue.Enqueue(8);

            var v1 = queue.Dequeue();
            var v2 = queue.Dequeue();

            Assert.AreEqual(v1, 3);
            Assert.AreEqual(v2, 1);
            Assert.That(queue, Is.EquivalentTo(new int[] { 4, 8 }));
        }
コード例 #10
0
        public void EnqueueAndDequeue()
        {
            var students = new Student[]
            {
                new Student {
                    No = 1, Name = "李1", Score = 22.1
                },
                new Student {
                    No = 2, Name = "李2", Score = 32.3
                },
                new Student {
                    No = 3, Name = "李3", Score = 42.5
                },
                new Student {
                    No = 4, Name = "李4", Score = 45
                }
            };

            var arrayQueue = new ArrayQueue <Student>(3);

            foreach (var student in students)
            {
                var result = arrayQueue.Enqueue(student);
            }


            var deqStudent = new Student();

            arrayQueue.Dequeue(out deqStudent);

            arrayQueue.Enqueue(students[3]);
        }
コード例 #11
0
        public void Enqueue_Get_Count_When_Enqueue_And_Dequeue()
        {
            var queue = new ArrayQueue <int>(5);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Dequeue();
            queue.Dequeue();
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            queue.Enqueue(6);
            queue.Enqueue(7);

            Assert.Equal(5, queue.Count);
        }
コード例 #12
0
        static void Main(string[] args)
        {
            int count = 1;
            ArrayQueue <string> arr = new ArrayQueue <string>(count);
            string s = "cfvfd";

            for (int i = 0; i < count; i++)
            {
                arr.Enqueue("элемент" + i);
            }
            IEnumerator <string> el1 = arr.GetEnumerator();

            arr.Enqueue(s);
            arr.Enqueue(s);
            arr.Enqueue(s);
            arr.Enqueue(s);
            foreach (string el in arr)
            {
                Console.WriteLine(el);
            }
            Console.WriteLine("//--//--//");
            Console.WriteLine();

            Console.WriteLine(arr.Dequeue());
            // Console.WriteLine(arr.Peek());
            // arr.Clear();
            Console.ReadLine();
        }
コード例 #13
0
        public void DequeueOnEmptyQueueThrowsQueueEmptyExceptionTest()
        {
            ArrayQueue target = new ArrayQueue(); // TODO: Initialize to an appropriate value

            target.Dequeue();
            Assert.Fail("Dequeued from empty queue didn't throw QueueEmptyException");
        }
        private void BreadthFirstSearch(LinkedList.LinkedList <T> visitedNodes)
        {
            IQueue <int> queue = new ArrayQueue <int>(_nodes.Count);

            bool[] visited = new bool[_nodes.Count];
            bool[] inqueue = new bool[_nodes.Count];

            queue.Enqueue(0);
            inqueue[0] = true;

            while (!queue.IsEmpty())
            {
                var n = queue.Dequeue();
                visited[n] = true;
                inqueue[n] = false;
                visitedNodes.Insert(_nodes[n]);

                foreach (var v in GetAdjacentVertices(n))
                {
                    if (!visited[v] && !inqueue[v])
                    {
                        queue.Enqueue(v);
                        inqueue[v] = true;
                    }
                }
            }
        }
コード例 #15
0
ファイル: ArrayQueueTests.cs プロジェクト: fcurdi/DSA
        public void DequeuingAllExceptOne()
        {
            var queue = new ArrayQueue <int>();

            int itemCount = 500000;

            for (int i = 0; i < itemCount; i++)
            {
                queue.Enqueue(i);
            }

            int lastItem = int.MinValue;

            for (int i = 0; i < itemCount - 1; i++)
            {
                if (lastItem > queue.Dequeue())
                {
                    Assert.Fail();
                }
            }

            int trueCount = 0;


            foreach (var item in queue)
            {
                trueCount++;
            }

            Assert.IsTrue(queue.Count == 1 &&
                          trueCount == 1);
        }
コード例 #16
0
        public void TestRemoveAt1()
        {
            ArrayQueue <object> queue = new ArrayQueue <object>(100, 3);

            for (int i = 1; i <= 16000; i++)
            {
                Assert.AreEqual(i, queue.Enqueue(i.ToString()));
            }

            Assert.AreEqual(16000, queue.Count);

            for (int i = 1; i <= 200; i++)
            {
                Assert.IsTrue(queue.Remove((500 + i).ToString()));
                Assert.AreEqual(16000 - i, queue.Count);
            }

            for (int i = 1; i <= 100; i++)
            {
                Assert.IsTrue(queue.Remove(i.ToString()));
                Assert.AreEqual((16000 - 200) - i, queue.Count);
            }

            Assert.IsFalse(queue.Remove(16001.ToString()));

            for (int i = queue.Count; i-- > 0;)
            {
                queue.Dequeue();
            }

            Assert.AreEqual(0, queue.Count);
        }
コード例 #17
0
        public void AddFiveItemsAndVerify()
        {
            int linkedListQueueLast = 0;
            int arrayQueueLast      = 0;

            for (int i = 1; i <= 5; i++)
            {
                linkedListQueue.Enqueue(i);
                arrayQueue.Enqueue(i);
            }

            Assert.AreEqual(linkedListQueue.Peek(), 1, "invalid top element");
            Assert.AreEqual(linkedListQueue.Count, 5, "invalid count");

            Assert.AreEqual(arrayQueue.Peek(), 1, "invalid top element");
            Assert.AreEqual(arrayQueue.Count, 5, "invalid count");

            while (linkedListQueue.Count > 0)
            {
                linkedListQueueLast = linkedListQueue.Dequeue();
                arrayQueueLast      = arrayQueue.Dequeue();
            }
            Assert.AreEqual(linkedListQueueLast, 5, "invalid last element");
            Assert.AreEqual(arrayQueueLast, 5, "invalid last element");
        }
コード例 #18
0
        public void TestDequeueEmptyQueueThrows()
        {
            var queue = new ArrayQueue <int>(5);
            var ex    = Assert.Throws <InvalidOperationException>(() => queue.Dequeue());

            Assert.AreEqual("Queue is empty", ex.Message);
        }
コード例 #19
0
        public void intNumbEnter()
        {
            //arrange
            int[] expected_ar = { 5, 6, 7 };

            //act
            ArrayQueue <int> arr = new ArrayQueue <int>(3);

            arr.Enqueue(5);
            arr.Enqueue(6);
            arr.Enqueue(7);
            int[] actual_ar = new int[3];
            int   i         = 0;

            while (!arr.IsEmpty())
            {
                actual_ar[i] = arr.Peek();
                arr.Dequeue();
                i++;
            }
            //foreach(var elem in arr)
            //{
            //    actual_ar[i] = elem;
            //    i++;
            //}

            //assert
            Assert.AreEqual(true, arr.isEmpty);
            CollectionAssert.AreEqual(expected_ar, actual_ar);
        }
コード例 #20
0
 public void ArrayQueueDequeueTest()
 {
     for (var i = 0; i < Elements - 1; i++)
     {
         _arrayQueue.Dequeue();
     }
     Assert.AreEqual(1000000000, _arrayQueue.Peek());
 }
コード例 #21
0
        public void PoppedItemsInOrder()
        {
            IQueue <int> underTest = new ArrayQueue <int>(3);

            Assert.AreEqual(underTest.Size(), 0);

            underTest.Enqueue(1);
            underTest.Enqueue(2);
            underTest.Enqueue(3);

            Assert.IsFalse(underTest.IsEmpty());
            Assert.AreEqual(underTest.Size(), 3);

            Assert.AreEqual(underTest.Dequeue(), 1);
            Assert.AreEqual(underTest.Dequeue(), 2);
            Assert.AreEqual(underTest.Dequeue(), 3);
        }
コード例 #22
0
        public void Enqueue_After_Dequeue_When_Full()
        {
            var queue = new ArrayQueue <int>(4);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Dequeue();
            queue.Dequeue();
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            queue.Enqueue(6);

            Exception ex = Assert.Throws <InvalidOperationException>(() => queue.Enqueue(100));

            Assert.IsType <InvalidOperationException>(ex);
        }
コード例 #23
0
        public void Dequeue_Throw_InvalidOperationException_When_Empty()
        {
            var queue = new ArrayQueue <int>(4);

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

            Assert.IsType <InvalidOperationException>(ex);
        }
コード例 #24
0
        public void Enque_EmptyQueue_ThrowsException()
        {
            var queue = new ArrayQueue <int>();

            Assert.Throws <InvalidOperationException>(() =>
            {
                queue.Dequeue();
            });
        }
コード例 #25
0
        public void TestDequeue()
        {
            var queue = new ArrayQueue <int>(5);

            queue.Enqueue(10);
            var item = queue.Dequeue();

            Assert.AreEqual(10, item);
        }
コード例 #26
0
        public void Dequeue_EmptyQueue_Failure()
        {
            IQueue <int> queue = new ArrayQueue <int>(2);

            Assert.Throws <InvalidOperationException>(() =>
            {
                queue.Dequeue();
            });
        }
コード例 #27
0
        public void EnumerateElements_BrokenChain_Success()
        {
            IQueue <int> queue = new ArrayQueue <int>(5);

            queue.Enqueue(3);
            queue.Enqueue(1);
            queue.Enqueue(4);
            queue.Enqueue(8);
            queue.Enqueue(9);

            queue.Dequeue();
            queue.Dequeue();

            queue.Enqueue(10);
            queue.Enqueue(11);

            Assert.That(queue, Is.EquivalentTo(new int[] { 4, 8, 9, 10, 11 }));
        }
コード例 #28
0
        public void Dequeue_SingleElement_BecomesEmpty()
        {
            var queue = new ArrayQueue <int>();

            queue.Enqueue(1);
            queue.Dequeue();

            Assert.IsTrue(queue.IsEmpty);
        }
コード例 #29
0
        public void EnqueueAndDequeue()
        {
            var queue = new ArrayQueue <int>();

            Assert.IsTrue(queue.IsEmpty);
            Assert.AreEqual(0, queue.Size);

            Console.WriteLine(queue);
            queue.Enqueue(3);
            Console.WriteLine(queue);
            queue.Enqueue(4);
            Console.WriteLine(queue);
            queue.Enqueue(1);
            Console.WriteLine(queue);

            Assert.IsFalse(queue.IsEmpty);
            Assert.AreEqual(3, queue.Size);
            Assert.AreEqual(3, queue.Front);

            Assert.AreEqual(3, queue.Dequeue());
            Assert.AreEqual(2, queue.Size);
            Assert.AreEqual(4, queue.Front);
            Console.WriteLine(queue);

            Assert.AreEqual(4, queue.Dequeue());
            Assert.AreEqual(1, queue.Size);
            Assert.AreEqual(1, queue.Front);
            Console.WriteLine(queue);

            Assert.AreEqual(1, queue.Dequeue());
            Assert.IsTrue(queue.IsEmpty);
            Assert.AreEqual(0, queue.Size);
            Console.WriteLine(queue);
            Assert.Catch <ArgumentOutOfRangeException>(() => queue.Dequeue());
            Assert.Catch <ArgumentOutOfRangeException>(() => { var f = queue.Front; });

            for (var i = 0; i < 20; i++)
            {
                queue.Enqueue(i);
            }
            Console.WriteLine(queue);

            Assert.GreaterOrEqual(queue.Capacity, 20);
        }
コード例 #30
0
        public void ProcessUntil(float timestamp)
        {
            ITeleportTimedMessage message;

            while (_messageQueue.Count > 0 && _messageQueue.Peek().Timestamp < timestamp)
            {
                message = _messageQueue.Dequeue();
                message.OnTimedPlayback();
            }
        }
コード例 #31
0
        public void TestGrow()
        {
            ArrayQueue<string> queue = new ArrayQueue<string>(3, 5);
            Assert.AreEqual(3, queue.Capacity);

            queue.Enqueue("a");
            queue.Enqueue("b");
            Assert.AreEqual(3, queue.Capacity);
            queue.Enqueue("c");
            Assert.AreEqual(8, queue.Capacity);

            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Dequeue();
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Dequeue();
            Assert.AreEqual(8, queue.Capacity);
            for (int i = 0; i < 4; i++)
                queue.Enqueue("" + ('d' + i));
            Assert.AreEqual(8, queue.Capacity);

            queue.Enqueue("z");
            Assert.AreEqual(13, queue.Capacity);

            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
                queue.Enqueue("" + ('a' + i));
            Assert.AreEqual(13, queue.Capacity);
            queue.Clear();
            Assert.AreEqual(13, queue.Capacity);
            for (int i = 0; i < 12; i++)
                queue.Enqueue("" + ('a' + i));
            Assert.AreEqual(13, queue.Capacity);
        }
コード例 #32
0
        public void TestRemoveAt1()
        {
            ArrayQueue<object> queue = new ArrayQueue<object>(100, 3);

            for (int i = 1; i <= 16000; i++)
            {
                Assert.AreEqual(i, queue.Enqueue(i.ToString()));
            }

            Assert.AreEqual(16000, queue.Count);

            for (int i = 1; i <= 200; i++)
            {
                Assert.IsTrue(queue.Remove((500+i).ToString()));
                Assert.AreEqual(16000 - i, queue.Count);
            }

            for (int i = 1; i <= 100; i++)
            {
                Assert.IsTrue(queue.Remove(i.ToString()));
                Assert.AreEqual((16000-200) - i, queue.Count);
            }

            Assert.IsFalse(queue.Remove(16001.ToString()));

            for (int i = queue.Count; i-- > 0; )
            {
                queue.Dequeue();
            }

            Assert.AreEqual(0, queue.Count);
        }
コード例 #33
0
        public void TestWrap()
        {
            ArrayQueue<string> queue = new ArrayQueue<string>(3);

            Assert.AreEqual(0,queue.Count);

            for (int i=0;i<3;i++)
            {
                queue.Offer("one");
                Assert.AreEqual(1,queue.Count);

                queue.Offer("two");
                Assert.AreEqual(2,queue.Count);

                queue.Offer("three");
                Assert.AreEqual(3,queue.Count);

                Assert.AreEqual("one",queue[0]);
                Assert.AreEqual("two",queue[1]);
                Assert.AreEqual("three",queue[2]);

                Assert.AreEqual("[one, two, three]",queue.ToString());

                Assert.AreEqual("one",queue.Dequeue());
                Assert.AreEqual(2,queue.Count);

                Assert.AreEqual("two", queue.Dequeue());
                Assert.AreEqual(1,queue.Count);

                Assert.AreEqual("three", queue.Dequeue());
                Assert.AreEqual(0,queue.Count);

                queue.Offer("xxx");
                Assert.AreEqual(1,queue.Count);
                Assert.AreEqual("xxx", queue.Dequeue());
                Assert.AreEqual(0,queue.Count);

            }
        }
コード例 #34
0
ファイル: Program.cs プロジェクト: Yuanxiangz/WorkSpace
        static void test8()
        {
            ArrayQueue<int> queue = new ArrayQueue<int>(3);
            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Dequeue();
            queue.Enqueue(3);
            queue.Dequeue();
            queue.Enqueue(4);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
        }