Esempio n. 1
0
        public void TestFailedQueueAt()
        {
            int[] data =
            {
                235,
                21,
                222,
                163,
                522,
                884,
                68,
                17,
                544,
                867,
                444
            };

            var q = new JetPriorityQueue <int>();

            foreach (var i in data)
            {
                q.Add(i);
            }

            var res = new List <int>();

            while (!q.Any())
            {
                res.Add(q.Extract());
            }
            Console.WriteLine(string.Join(",", res.Select(x => x.ToString()).ToArray()) + " : " + res.Count);
        }
Esempio n. 2
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;
                }
            }
        }
Esempio n. 3
0
        public void TestStable()
        {
            var q  = new JetPriorityQueue <string>();
            var a1 = new string('a', 1);
            var a2 = new string('a', 1);
            var a3 = new string('a', 1);
            var a4 = new string('a', 1);

            q.Add(a1);
            q.Add(a2);
            q.Add(a3);
            q.Add(a4);

            Assert.True(ReferenceEquals(a1, q.Extract()));
            Assert.True(ReferenceEquals(a2, q.Extract()));
            Assert.True(ReferenceEquals(a3, q.Extract()));
            Assert.True(ReferenceEquals(a4, q.Extract()));
        }
Esempio n. 4
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.");
                }
            }
        }