示例#1
0
        public void full()
        {
            CircularQueue <int?> queue = new CircularQueue <int?>(1);

            Assert.IsFalse(queue.IsFull());
            Assert.IsTrue(queue.Put(0));
            Assert.IsTrue(queue.IsFull());
            Assert.AreEqual(0, queue.Get());
            Assert.IsFalse(queue.IsFull());

            Assert.IsTrue(queue.Put(0));
            Assert.IsFalse(queue.Put(0, -1));
            Assert.IsFalse(queue.Put(0, 1));
            Assert.IsFalse(queue.Put(0, 10));
        }
示例#2
0
        public void stress5()
        {
            CircularQueue <int?> queue = new CircularQueue <int?>(3);

            // we will setup two threads waiting to put to the queue,
            // then in a single synchronized step, read two items from
            // the queue. the first thread will be woken to put, and
            // once done the second thread should be woken by the first.

            queue.Put(0);
            queue.Put(1);
            queue.Put(2);
            Assert.IsTrue(queue.IsFull());

            Thread t1 = new Thread(
                delegate()
            {
                try
                {
                    Assert.IsTrue(queue.Put(3));
                }
                catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e);
                }
            }
                );

            Thread t2 = new Thread(
                delegate()
            {
                try
                {
                    Assert.IsTrue(queue.Put(4));
                }
                catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e);
                }
            }
                );

            t1.Start();
            t2.Start();

            // wait until both threads are waiting on queue...

            Thread.Sleep(100);

            lock (queue)
            {
                Assert.IsNotNull(queue.Get());
                Assert.IsNotNull(queue.Get());
            }

            harvest(t1);
            harvest(t2);
        }
示例#3
0
        public void putget1()
        {
            CircularQueue <int?> queue = new CircularQueue <int?>();

            Assert.AreEqual(0, queue.Count());
            Assert.IsTrue(queue.IsEmpty());
            Assert.IsFalse(queue.IsFull());

            for (int i = 0; i < 10000; i++)
            {
                queue.Put(i);
                Assert.AreEqual(1, queue.Count());
                Assert.IsFalse(queue.IsEmpty());

                Assert.AreEqual(i, queue.Get());
                Assert.AreEqual(0, queue.Count());
                Assert.IsTrue(queue.IsEmpty());
            }
        }