예제 #1
0
        public void TestTimedQueue()
        {
            var queue = new JetPriorityQueue <DateTime>(10, new DateTimeComparer());

            for (int seed = 0; seed < 1000; seed++)
            {
                var random = new Random(seed);

                var now = DateTime.Now;
                for (int i = 0; i < 11; i++)
                {
                    queue.Add(now + TimeSpan.FromMilliseconds(random.Next(0, 1000)));
                }

                var      lastDateTime = DateTime.MinValue;
                DateTime res;
                while (queue.TryPeek(out res))
                {
                    res = queue.Extract();
                    Assert.LessOrEqual(lastDateTime, res, string.Format("Count = {0}, Seed = {1}", queue.Count, seed));

                    lastDateTime = res;
                }
            }
        }
예제 #2
0
        public void TestQueueDifferentSizes()
        {
            for (var size = 0; size <= 20; size++)
            {
                var queue = new JetPriorityQueue <int>(size);

                for (var seed = 0; seed < 10; seed++)
                {
                    var random = new Random(seed);

                    var source = new List <int>(size);
                    for (var i = 0; i < size; i++)
                    {
                        source.Add(random.Next(0, 1000));
                    }

                    for (var i = 0; i < size; i++)
                    {
                        queue.Add(source[i]);
                        Assert.AreEqual(queue.Count, i + 1, "Add failed.");
                    }

                    int prevVal = -1;
                    int peekVal;
                    for (var i = 0; i < size; i++)
                    {
                        Assert.True(queue.TryPeek(out peekVal), "TryPeek failed.");

                        var curVal = queue.Extract();
                        Assert.AreEqual(peekVal, curVal, "Peek/Extract mismatch.");

                        Assert.LessOrEqual(prevVal, curVal, string.Format("Values are not ordered (Size = {0}, Seed = {1}, Count = {2}).", size, seed, queue.Count));
                        prevVal = curVal;
                    }
                    Assert.False(queue.TryPeek(out peekVal), "TryPeek failed.");
                }
            }
        }
예제 #3
0
        public void TestNoComparer()
        {
            var q = new JetPriorityQueue <int>();

            Assert.AreEqual(0, q.Count);
            Assert.True(!q.Any());
            q.Add(5);
            q.Add(3);
            q.Add(4);
            q.Add(1);
            q.Add(2);
            Assert.AreEqual(5, q.Count);
            Assert.False(!q.Any());


            AssertExtract(1, q);
            AssertExtract(2, q);
            AssertExtract(3, q);
            Assert.AreEqual(2, q.Count);

            q.Add(5);
            q.Add(3);
            Assert.AreEqual(4, q.Count);

            AssertExtract(3, q);
            q.Add(1);
            AssertExtract(1, q);


            AssertExtract(4, q);
            AssertExtract(5, q);
            AssertExtract(5, q);

            int x;

            Assert.False(q.TryPeek(out x));
            Assert.False(q.TryExtract(out x));
            Assert.AreEqual(0, q.ExtractOrDefault());
        }