예제 #1
0
        public void ClearLast()
        {
            var queue = new LLQueue <FinalisationNoter>();

            queue.Enqueue(new FinalisationNoter());
            queue.Enqueue(new FinalisationNoter());
            queue.DequeueToList();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.AreEqual(1, FinalisationNoter.FinalisationCount);
            queue.ClearLastItem();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.AreEqual(2, FinalisationNoter.FinalisationCount);
        }
예제 #2
0
        public void SimpleAddAndRemove()
        {
            var queue = new LLQueue <int>();

            for (int i = 0; i != 10; ++i)
            {
                queue.Enqueue(i);
            }
            int cur = 0;
            int res;
            int peek;

            queue.TryPeek(out peek);
            Assert.AreEqual(0, peek);
            while (queue.TryDequeue(out res))
            {
                Assert.AreEqual(cur++, res);
                if (queue.TryPeek(out peek))
                {
                    Assert.AreEqual(cur, peek);
                    Assert.IsFalse(queue.IsEmpty);
                }
                else
                {
                    Assert.IsTrue(queue.IsEmpty);
                }
            }
        }
예제 #3
0
        public void RacingEnumeration()
        {
            int cores = CoreCount();

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

            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 =>
                    {
                        int prev = -1;
                        foreach (int taken in queue)
                        {
                            if (prev >= taken)
                            {
                                failed = false;
                            }
                            prev = 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();
            }
            Assert.IsFalse(failed);
        }
예제 #4
0
        public void RacingClears()
        {
            int cores = CoreCount();

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

            threads[0] = new Thread(() =>
            {
                for (int x = 0; x != 1000000; ++x)
                {
                    queue.Enqueue(x);
                }
                Interlocked.Increment(ref done);
            });
            for (int i = 1; i != cores; ++i)
            {
                threads[i] = new Thread(() =>
                {
                    while (done == 0)
                    {
                        for (int x = 0; x != 1000; ++x)
                        {
                            queue.Clear();
                        }
                    }
                });
            }
            for (int i = 0; i < cores; ++i)
            {
                threads[i].Start();
            }
            for (int i = 0; i < cores; ++i)
            {
                threads[i].Join();
            }
            Assert.IsTrue(queue.IsEmpty);
        }
예제 #5
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;
     }
 }
예제 #6
0
        public void MultiAdd()
        {
            int cores   = CoreCount();
            var threads = new Thread[cores];
            var queue   = new LLQueue <int>();

            for (int i = 0; i != cores; ++i)
            {
                threads[i] = new Thread(obj =>
                {
                    int off = (int)obj;
                    for (int x = 0; x != 100000; ++x)
                    {
                        queue.Enqueue(x * cores + off);
                    }
                });
            }
            for (int i = 0; i != cores; ++i)
            {
                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;
            }
            foreach (int test in queue)
            {
                int bucket = test % cores;
                int last   = dict[bucket];
                Assert.IsTrue(test > last);
                dict[bucket] = last;
            }
        }
예제 #7
0
파일: QueueTests.cs 프로젝트: kiszu/ForBlog
 public void SimpleAddAndRemove()
 {
     var queue = new LLQueue<int>();
     for(int i = 0; i != 10; ++i)
         queue.Enqueue(i);
     int cur = 0;
     int res;
     int peek;
     queue.TryPeek(out peek);
     Assert.AreEqual(0, peek);
     while(queue.TryDequeue(out res))
     {
         Assert.AreEqual(cur++, res);
         if(queue.TryPeek(out peek))
         {
             Assert.AreEqual(cur, peek);
             Assert.IsFalse(queue.IsEmpty);
         }
         else
             Assert.IsTrue(queue.IsEmpty);
     }
 }
예제 #8
0
    static void Main()
    {
        LLQueue q = new LLQueue();

        q.Enqueue(123);
        q.Enqueue("lgslaya");
        q.Enqueue('f');
        q.Enqueue("Nodes R US");
        q.Enqueue(34.46);
        Console.WriteLine("Peek: " + q.Peek());
        Console.WriteLine(q.ToString());
        q.Dequeue();

        q.Dequeue();
        q.Dequeue();

        q.Dequeue();
        Console.WriteLine(q.ToString());
        q.Enqueue(1337);
        q.Enqueue("\'7th item");
        q.Enqueue("Should be wrapping around");
        q.Dequeue();
        Console.WriteLine(q.ToString());
    }
        static void Main(string[] args)
        {
            //test queue

            //create list
            LinkedList myList = new LinkedList();
            //create queue
            LLQueue myQueue = new LLQueue(myList);

            //enqueue 6 numbers
            Console.WriteLine("enqueue 6");
            myQueue.Enqueue(6);
            Console.WriteLine("enqueue 5");
            myQueue.Enqueue(5);
            Console.WriteLine("enqueue 4");
            myQueue.Enqueue(4);
            Console.WriteLine("enqueue 3");
            myQueue.Enqueue(3);
            Console.WriteLine("enqueue 2");
            myQueue.Enqueue(2);
            Console.WriteLine("enqueue 1");
            myQueue.Enqueue(1);
            //peek front to ensure that "6" is the first node in queue
            Console.WriteLine("Peek front: " + myQueue.Peek());

            //dequeue the first 3 nodes
            Console.WriteLine("Dequeue");
            Console.WriteLine("removed " + myQueue.Dequeue());
            Console.WriteLine("Dequeue");
            Console.WriteLine("removed " + myQueue.Dequeue());
            Console.WriteLine("Dequeue");
            Console.WriteLine("removed " + myQueue.Dequeue());

            //peek front again to ensure that "3" is now the front node
            Console.WriteLine("Peek Front: " + myQueue.Peek());
            Console.ReadKey();
        }
예제 #10
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;
            }
        }
예제 #11
0
파일: QueueTests.cs 프로젝트: kiszu/ForBlog
 public void ClearLast()
 {
     var queue = new LLQueue<FinalisationNoter>();
     queue.Enqueue(new FinalisationNoter());
     queue.Enqueue(new FinalisationNoter());
     queue.DequeueToList();
     GC.Collect();
     GC.WaitForPendingFinalizers();
     Assert.AreEqual(1, FinalisationNoter.FinalisationCount);
     queue.ClearLastItem();
     GC.Collect();
     GC.WaitForPendingFinalizers();
     Assert.AreEqual(2, FinalisationNoter.FinalisationCount);
 }
예제 #12
0
파일: QueueTests.cs 프로젝트: kiszu/ForBlog
 public void RacingClears()
 {
     int cores = CoreCount();
     if(cores < 2)
         cores = 2;
     var threads = new Thread[cores];
     var queue = new LLQueue<int>();
     int done = 0;
     threads[0] = new Thread(() =>
                                 {
                                     for(int x = 0; x != 1000000; ++x)
                                         queue.Enqueue(x);
                                     Interlocked.Increment(ref done);
                                 });
     for(int i = 1; i != cores; ++i)
     {
         threads[i] = new Thread(() =>
                                 {
                                     while(done == 0)
                                         for(int x = 0; x != 1000; ++x)
                                             queue.Clear();
                                 });
     }
     for(int i = 0; i < cores; ++i)
         threads[i].Start();
     for(int i = 0; i < cores; ++i)
         threads[i].Join();
     Assert.IsTrue(queue.IsEmpty);
 }
예제 #13
0
파일: QueueTests.cs 프로젝트: kiszu/ForBlog
 public void RacingEnumeration()
 {
     int cores = CoreCount();
     if(cores < 2)
         cores = 2;
     var threads = new Thread[cores];
     var queue = new LLQueue<int>();
     bool failed = false;
     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 =>
                                     {
                                         int prev = -1;
                                         foreach(int taken in queue)
                                         {
                                             if(prev >= taken)
                                                 failed = false;
                                             prev = 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();
     Assert.IsFalse(failed);
 }
예제 #14
0
파일: QueueTests.cs 프로젝트: kiszu/ForBlog
 public void MultiAdd()
 {
     int cores = CoreCount();
     var threads = new Thread[cores];
     var queue = new LLQueue<int>();
     for(int i = 0; i != cores; ++i)
     {
         threads[i] = new Thread(obj =>
                                 {
                                     int off = (int)obj;
                                     for(int x = 0; x != 100000; ++x)
                                         queue.Enqueue(x * cores + off);
                                 });
     }
     for(int i = 0; i != cores; ++i)
         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;
     foreach(int test in queue)
     {
         int bucket = test % cores;
         int last = dict[bucket];
         Assert.IsTrue(test > last);
         dict[bucket] = last;
     }
 }