コード例 #1
0
        public void Enumerate()
        {
            var queue = new LLQueue <int>(Enumerable.Range(0, 100));
            int cur   = 0;

            foreach (int i in queue)
            {
                Assert.AreEqual(cur++, i);
            }
            Assert.AreEqual(100, queue.Count);
            cur = 0;
            foreach (int i in queue.DequeueAll())
            {
                Assert.AreEqual(cur++, i);
                Assert.AreEqual(100 - cur, queue.Count);
            }
            Assert.AreEqual(0, queue.Count);
            queue.EnqueueRange(Enumerable.Range(0, 100));
            Assert.AreEqual(100, queue.Count);
            cur = 0;
            foreach (int i in queue.AtomicDequeueAll())
            {
                Assert.AreEqual(cur++, i);
                Assert.AreEqual(0, queue.Count);
            }
        }
コード例 #2
0
ファイル: QueueTests.cs プロジェクト: adatapost/Ariadne
 public void HalfDequeue()
 {
     int cores = CoreCount();
     if(cores < 2)
         cores = 2;
     var threads = new Thread[cores];
     var queue = new LLQueue<int>();
     var secQueue = new LLQueue<int>();
     for(int i = 0; i != cores; ++i)
     {
         if(i % 2 == 0)
             threads[i] = new Thread(obj =>
                                     {
                                         int off = (int)obj;
                                         for(int x = 0; x != 100000; ++x)
                                             queue.Enqueue(x * cores + off);
                                     });
         else
             threads[i] = new Thread(obj =>
                                     {
                                         foreach(int taken in queue.DequeueAll())
                                             secQueue.Enqueue(taken);
                                     });
     }
     for(int i = 0; i < cores; i += 2)
         threads[i].Start(i);
     for(int i = 1; i < cores; i += 2)
         threads[i].Start(i);
     for(int i = 0; i != cores; ++i)
         threads[i].Join();
     Dictionary<int, int> dict = new Dictionary<int, int>();
     for(int i = 0; i != cores; ++i)
         dict[i] = -1;
     secQueue.EnqueueRange(queue);
     foreach(int test in secQueue)
     {
         int bucket = test % cores;
         int last = dict[bucket];
         Assert.IsTrue(test > last);
         dict[bucket] = last;
     }
 }
コード例 #3
0
ファイル: QueueTests.cs プロジェクト: kiszu/ForBlog
 public void Enumerate()
 {
     var queue = new LLQueue<int>(Enumerable.Range(0, 100));
     int cur = 0;
     foreach(int i in queue)
         Assert.AreEqual(cur++, i);
     Assert.AreEqual(100, queue.Count);
     cur = 0;
     foreach(int i in queue.DequeueAll())
     {
         Assert.AreEqual(cur++, i);
         Assert.AreEqual(100 - cur, queue.Count);
     }
     Assert.AreEqual(0, queue.Count);
     queue.EnqueueRange(Enumerable.Range(0, 100));
     Assert.AreEqual(100, queue.Count);
     cur = 0;
     foreach(int i in queue.AtomicDequeueAll())
     {
         Assert.AreEqual(cur++, i);
         Assert.AreEqual(0, queue.Count);
     }
 }
コード例 #4
0
        public void HalfDequeue()
        {
            int cores = CoreCount();

            if (cores < 2)
            {
                cores = 2;
            }
            var threads  = new Thread[cores];
            var queue    = new LLQueue <int>();
            var secQueue = new LLQueue <int>();

            for (int i = 0; i != cores; ++i)
            {
                if (i % 2 == 0)
                {
                    threads[i] = new Thread(obj =>
                    {
                        int off = (int)obj;
                        for (int x = 0; x != 100000; ++x)
                        {
                            queue.Enqueue(x * cores + off);
                        }
                    });
                }
                else
                {
                    threads[i] = new Thread(obj =>
                    {
                        foreach (int taken in queue.DequeueAll())
                        {
                            secQueue.Enqueue(taken);
                        }
                    });
                }
            }
            for (int i = 0; i < cores; i += 2)
            {
                threads[i].Start(i);
            }
            for (int i = 1; i < cores; i += 2)
            {
                threads[i].Start(i);
            }
            for (int i = 0; i != cores; ++i)
            {
                threads[i].Join();
            }
            Dictionary <int, int> dict = new Dictionary <int, int>();

            for (int i = 0; i != cores; ++i)
            {
                dict[i] = -1;
            }
            secQueue.EnqueueRange(queue);
            foreach (int test in secQueue)
            {
                int bucket = test % cores;
                int last   = dict[bucket];
                Assert.IsTrue(test > last);
                dict[bucket] = last;
            }
        }