Ejemplo n.º 1
0
        public void TestFairness()
        {
            var fq       = new FairQueue <string>();
            int enqueued = 0;

            fq.Enqueue(1, "11"); ++enqueued;
            fq.Enqueue(1, "12"); ++enqueued;
            fq.Enqueue(1, "13"); ++enqueued;
            fq.Enqueue(1, "14"); ++enqueued;

            fq.Enqueue(2, "21"); ++enqueued;
            fq.Enqueue(2, "22"); ++enqueued;
            fq.Enqueue(2, "23"); ++enqueued;
            fq.Enqueue(2, "24"); ++enqueued;

            fq.Enqueue(3, "31"); ++enqueued;
            fq.Enqueue(3, "32"); ++enqueued;
            fq.Enqueue(3, "33"); ++enqueued;
            fq.Enqueue(3, "34"); ++enqueued;

            int tag, ptag;

            fq.Dequeue(out tag); --enqueued;
            for (int i = 0; i < enqueued; ++i)
            {
                ptag = tag;
                fq.Dequeue(out tag);
                Assert.AreEqual((ptag + 1) % 3, tag % 3, "Incorrect cycling between tags");
            }

            Assert.IsTrue(fq.Empty, "The queue should be empty");
        }
Ejemplo n.º 2
0
        public void TestFairness()
        {
            var fq = new FairQueue<string>();
            int enqueued = 0;
            fq.Enqueue(1, "11"); ++enqueued;
            fq.Enqueue(1, "12"); ++enqueued;
            fq.Enqueue(1, "13"); ++enqueued;
            fq.Enqueue(1, "14"); ++enqueued;

            fq.Enqueue(2, "21"); ++enqueued;
            fq.Enqueue(2, "22"); ++enqueued;
            fq.Enqueue(2, "23"); ++enqueued;
            fq.Enqueue(2, "24"); ++enqueued;

            fq.Enqueue(3, "31"); ++enqueued;
            fq.Enqueue(3, "32"); ++enqueued;
            fq.Enqueue(3, "33"); ++enqueued;
            fq.Enqueue(3, "34"); ++enqueued;

            int tag, ptag;
            fq.Dequeue(out tag); --enqueued;
            for (int i = 0; i < enqueued; ++i)
            {
                ptag = tag;
                fq.Dequeue(out tag);
                Assert.AreEqual((ptag + 1) % 3, tag % 3, "Incorrect cycling between tags");
            }

            Assert.IsTrue(fq.Empty, "The queue should be empty");
        }
Ejemplo n.º 3
0
        public void TestDequeue()
        {
            var fq = new FairQueue <string>();

            fq.Enqueue("1");
            var r = fq.Dequeue();

            Assert.IsTrue(fq.Empty, "The queue should be empty");
            Assert.AreEqual("1", r, "Incorrect dequeued value");

            fq.Enqueue("1");
            fq.Enqueue(2, "2");
            fq.Enqueue(2, "3");
            fq.Enqueue(5, "4");
            fq.Enqueue("5");
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            Assert.IsTrue(fq.Empty, "The queue should be empty");

            Assert.Throws(typeof(InvalidOperationException), () => fq.Dequeue(), "Dequeueing from an empty queue should throw an InvalidOperationException");

            int tag;

            fq.Enqueue("1");
            fq.Dequeue(out tag);
            Assert.AreEqual(0, tag, "The queue is supposed to contain only one tag and we got an incorrect value for it");
            fq.Enqueue(42, "1");
            fq.Dequeue(out tag);
            Assert.AreEqual(42, tag, "The queue is supposed to contain only one tag and we got an incorrect value for it");
        }
Ejemplo n.º 4
0
        public void TestDequeue()
        {
            var fq = new FairQueue<string>();
            fq.Enqueue("1");
            var r = fq.Dequeue();
            Assert.IsTrue(fq.Empty, "The queue should be empty");
            Assert.AreEqual("1", r, "Incorrect dequeued value");

            fq.Enqueue("1");
            fq.Enqueue(2, "2");
            fq.Enqueue(2, "3");
            fq.Enqueue(5, "4");
            fq.Enqueue("5");
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            fq.Dequeue();
            Assert.IsTrue(fq.Empty, "The queue should be empty");

            Assert.Throws(typeof(InvalidOperationException), () => fq.Dequeue(), "Dequeueing from an empty queue should throw an InvalidOperationException");

            int tag;
            fq.Enqueue("1");
            fq.Dequeue(out tag);
            Assert.AreEqual(0, tag, "The queue is supposed to contain only one tag and we got an incorrect value for it");
            fq.Enqueue(42, "1");
            fq.Dequeue(out tag);
            Assert.AreEqual(42, tag, "The queue is supposed to contain only one tag and we got an incorrect value for it");
        }
Ejemplo n.º 5
0
        public void TestManyUsers()
        {
            FairQueue queue = new FairQueue();

            DateTime dateTime = DateTime.Now;

            foreach (int userNumber in Enumerable.Range(1, 3))
            {
                foreach (int songForUser in Enumerable.Range(1, 3))
                {
                    queue.Add(new RequestedSong()
                    {
                        RequestedDate = dateTime.AddDays(userNumber).AddMinutes(songForUser),
                        Song          = new Song()
                        {
                            FileName = songForUser + "_Song.mp3"
                        },
                        RequesterName = userNumber + "_User"
                    });
                }
            }

            RequestedSong[] requestedSongs = queue.Current.ToArray();

            Assert.AreEqual("1_User", requestedSongs[0].RequesterName);
            Assert.AreEqual("2_User", requestedSongs[1].RequesterName);
            Assert.AreEqual("3_User", requestedSongs[2].RequesterName);
            Assert.AreEqual("1_User", requestedSongs[3].RequesterName);
            Assert.AreEqual("2_User", requestedSongs[4].RequesterName);
            Assert.AreEqual("3_User", requestedSongs[5].RequesterName);
            Assert.AreEqual("1_User", requestedSongs[6].RequesterName);
            Assert.AreEqual("2_User", requestedSongs[7].RequesterName);
            Assert.AreEqual("3_User", requestedSongs[8].RequesterName);
        }
Ejemplo n.º 6
0
        public void TestEnqueue()
        {
            var fq = new FairQueue<string>();
            fq.Enqueue("1");
            Assert.AreEqual(1, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");

            fq.Enqueue("2");
            Assert.AreEqual(2, fq.Count);
            Assert.IsFalse(fq.Empty);

            fq.Enqueue(1, "3");
            Assert.AreEqual(3, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");

            fq.Enqueue(2, "4");
            fq.Enqueue(2, "5");
            Assert.AreEqual(5, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");
        }
Ejemplo n.º 7
0
        public void TestEnqueue()
        {
            var fq = new FairQueue <string>();

            fq.Enqueue("1");
            Assert.AreEqual(1, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");

            fq.Enqueue("2");
            Assert.AreEqual(2, fq.Count);
            Assert.IsFalse(fq.Empty);

            fq.Enqueue(1, "3");
            Assert.AreEqual(3, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");

            fq.Enqueue(2, "4");
            fq.Enqueue(2, "5");
            Assert.AreEqual(5, fq.Count, "Incorrect number of elements in the queue");
            Assert.IsFalse(fq.Empty, "The queue should not be empty");
        }
Ejemplo n.º 8
0
        public void TestFairQueue()
        {
            FairQueue queue = new FairQueue();

            var first = new RequestedSong()
            {
                RequestedDate = DateTime.Now, Song = new Song()
                {
                    FileName = "A.mp3"
                }, RequesterName = "A"
            };

            queue.Add(first);

            Assert.AreEqual(queue.Count, 1);
            Assert.IsNotNull(queue.Current.First());

            queue.Add(new RequestedSong()
            {
                RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song()
                {
                    FileName = "B.mp3"
                }, RequesterName = "A"
            });

            Assert.AreEqual(queue.Count, 2);
            Assert.IsNotNull(queue.Current.First());
            Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName);

            queue.Add(new RequestedSong()
            {
                RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song()
                {
                    FileName = "C.mp3"
                }, RequesterName = "B"
            });

            Assert.AreEqual(queue.Count, 3);
            Assert.IsNotNull(queue.Current.First());
            Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName);
            Assert.AreEqual("C.mp3", queue.Current.ElementAt(1).Song.FileName);
            Assert.AreEqual("B.mp3", queue.Current.ElementAt(2).Song.FileName);

            queue.Add(new RequestedSong()
            {
                RequestedDate = DateTime.Now.AddSeconds(1), Song = new Song()
                {
                    FileName = "D.mp3"
                }, RequesterName = "C"
            });

            Assert.AreEqual(queue.Count, 4);
            Assert.IsNotNull(queue.Current.First());
            Assert.AreEqual("A.mp3", queue.Current.First().Song.FileName);
            Assert.AreEqual("C.mp3", queue.Current.ElementAt(1).Song.FileName);
            Assert.AreEqual("D.mp3", queue.Current.ElementAt(2).Song.FileName);
            Assert.AreEqual("B.mp3", queue.Current.ElementAt(3).Song.FileName);

            queue.Remove(first);

            Assert.AreEqual(queue.Count, 3);
            Assert.IsNotNull(queue.Current.First());
            Assert.AreEqual("C.mp3", queue.Current.ElementAt(0).Song.FileName);
            Assert.AreEqual("D.mp3", queue.Current.ElementAt(1).Song.FileName);
            Assert.AreEqual("B.mp3", queue.Current.ElementAt(2).Song.FileName);
        }