Ejemplo n.º 1
0
        public void IteratorConcurrencyTest()
        {
            var q     = UnsafeMPMCQueue.Allocate <int>(16000);
            int count = 10000;

            Thread writer = new Thread(() =>
            {
                for (int i = 0; i < count;)
                {
                    if (UnsafeMPMCQueue.TryEnqueue(q, i))
                    {
                        i++;
                    }
                }
            });

            writer.Start();

            Thread.Sleep(3); // Wait some arbitrary time so there's data to enumerate

            var enumerator = UnsafeMPMCQueue.GetEnumerator <int>(q);
            int num        = 0;

            foreach (int i in enumerator)
            {
                Assert.AreEqual(num++, i);
            }

            writer.Join();

            Assert.AreEqual(count, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 2
0
        public void ConstructorTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(10);

            Assert.AreEqual(0, UnsafeMPMCQueue.GetCount(q));
            Assert.AreEqual(16, UnsafeMPMCQueue.GetCapacity(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 3
0
        public void ClearTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(16);

            //Inserts 10 items.
            SplitQueue(q);

            Assert.AreEqual(10, UnsafeMPMCQueue.GetCount(q));
            UnsafeMPMCQueue.Clear(q);
            Assert.AreEqual(0, UnsafeMPMCQueue.GetCount(q));

            Assert.IsTrue(UnsafeMPMCQueue.IsEmpty <int>(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 4
0
        public void EnqueueTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(100);

            for (int i = 0; i < 100; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, i * i);
            }

            Assert.AreEqual(100, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Clear(q);

            Assert.AreEqual(0, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 5
0
        public void PeekTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>(10);

            for (int i = 0; i < 10; i++)
            {
                UnsafeMPMCQueue.TryEnqueue(q, (int)Math.Pow(i + 2, 2));
            }

            for (int i = 0; i < 10; i++)
            {
                UnsafeMPMCQueue.TryPeek(q, out int result);
                Assert.AreEqual(4, result);
            }

            //Verify no items are dequeued
            Assert.AreEqual(10, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 6
0
        public void ExpandTest()
        {
            var q = UnsafeMPMCQueue.Allocate <int>();

            SplitQueue(q);

            // Fill buffer beyond capacity
            for (int i = 0; i < 100;)
            {
                if (UnsafeMPMCQueue.TryEnqueue(q, 999))
                {
                    i++;
                }
            }


            Assert.AreEqual(110, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 7
0
        public void ClearFixedTest()
        {
            int size = 128; // Power of two.
            var q    = UnsafeMPMCQueue.Allocate <int>(size, true);

            //Inserts 10 items.
            SplitQueue(q);

            Assert.AreEqual(10, UnsafeMPMCQueue.GetCount(q));
            Assert.AreEqual(size, UnsafeMPMCQueue.GetCapacity(q));

            UnsafeMPMCQueue.Clear(q);

            Assert.AreEqual(0, UnsafeMPMCQueue.GetCount(q));
            // Queue capacity needs to remain unchanged after clear.
            Assert.AreEqual(size, UnsafeMPMCQueue.GetCapacity(q));

            Assert.IsTrue(UnsafeMPMCQueue.IsEmpty <int>(q));

            UnsafeMPMCQueue.Free(q);
        }
Ejemplo n.º 8
0
        // Demonstration that this queue is MPSC
        public void MPSCConcurrencyTest()
        {
            var q     = UnsafeMPMCQueue.Allocate <int>(16000);
            int count = 10000;

            Thread writer = new Thread(() =>
            {
                for (int i = 0; i < count / 2;)
                {
                    if (UnsafeMPMCQueue.TryEnqueue(q, i))
                    {
                        i++;
                    }
                }
            });

            Thread writer2 = new Thread(() =>
            {
                for (int i = 0; i < count / 2;)
                {
                    if (UnsafeMPMCQueue.TryEnqueue(q, i))
                    {
                        i++;
                    }
                }
            });

            writer.Start();
            writer2.Start();


            writer.Join();
            writer2.Join();

            Assert.AreEqual(count, UnsafeMPMCQueue.GetCount(q));

            UnsafeMPMCQueue.Free(q);
        }