Exemple #1
0
        public void CanContains()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);
            q.Enqueue(4);

            Assert.False(q.Contains(1));
            Assert.True(q.Contains(2));
            Assert.True(q.Contains(3));
            Assert.True(q.Contains(4));
            Assert.False(q.Contains(5));

            var qq = new CappedQueue <string>(3);

            qq.Enqueue("one");
            qq.Enqueue("two");
            qq.Enqueue(null);

            Assert.True(qq.Contains("one"));
            Assert.True(qq.Contains("two"));
            Assert.True(qq.Contains(null));
            Assert.False(qq.Contains("three"));
        }
Exemple #2
0
        public void SetCountLimit_Throw()
        {
            var sut = new CappedQueue <MyItem>(i => i == null ? 0 : i.Name == null ? 0 : i.Name.Length);

            sut.CountLimit = 3;
            sut.Handling   = QueueLimitHandling.Throw;

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Cat"
            }));
            Aver.AreEqual(1, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Dog"
            }));
            Aver.AreEqual(2, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(null));
            Aver.AreEqual(3, sut.Count);

            try
            {
                //Rabbit will not fit - will throw
                Aver.IsFalse(sut.TryEnqueue(new MyItem {
                    Name = "Rabbit"
                }));
            }
            catch (AzosException error)
            {
                Aver.IsTrue(error.Message.Contains("limit is reached"));
                Conout.WriteLine("Expected and got: " + error.ToMessageWithType());
                return;
            }
            Aver.Fail("No expected exception");
        }
	    public void Foo2()
		{
			var queue = new CappedQueue<int>(5);
			
			queue.Enqueue(10);
			queue.Enqueue(20);
			queue.Enqueue(30);
			
			var average = queue.Average(x=>x);
		}
Exemple #4
0
        public void CanCreate()
        {
            var q = new CappedQueue <int>(1);

            Assert.Equal(1, q.Capacity);
            Assert.Equal(0, q.Count);

            Assert.Throws <ArgumentOutOfRangeException>(() => new CappedQueue <int>(0));
            Assert.Throws <ArgumentOutOfRangeException>(() => new CappedQueue <int>(-1));
        }
Exemple #5
0
	    public void Foo2()
		{
			var queue = new CappedQueue<int>(5);
			
			queue.Enqueue(10);
			queue.Enqueue(20);
			queue.Enqueue(30);
			
			var average = queue.Sum() / queue.Count();
		}
	    public void Foo()
		{
			var queue = new CappedQueue<DateTime>(5);
			
			queue.Enqueue(DateTime.Now); // each time the user press the key
			queue.Enqueue(DateTime.Now); // each time the user press the key
			queue.Enqueue(DateTime.Now); // each time the user press the key
			
			
			TimeSpan diff = (queue.Last() / queue.Fisrt())/queue.Count();
		}
Exemple #7
0
        public void SetCountLimit_DiscardNew()
        {
            var sut = new CappedQueue <MyItem>(i => i == null ? 0 : i.Name == null ? 0 : i.Name.Length);

            sut.CountLimit = 3;
            sut.Handling   = QueueLimitHandling.DiscardNew;

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Cat"
            }));
            Aver.AreEqual(1, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Dog"
            }));
            Aver.AreEqual(2, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(null));
            Aver.AreEqual(3, sut.Count);

            //Rabbit will not fit - return FALSE
            Aver.IsFalse(sut.TryEnqueue(new MyItem {
                Name = "Rabbit"
            }));
            Aver.AreEqual(3, sut.Count);


            //1st is Dog because Cat was removed
            Aver.AreEqual(3, sut.EnqueuedCount);
            Aver.AreEqual(3 + 3, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out var got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Cat", got.Name);
            Aver.AreEqual(2, sut.EnqueuedCount);
            Aver.AreEqual(3, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Dog", got.Name);
            Aver.AreEqual(1, sut.EnqueuedCount);
            Aver.AreEqual(0, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNull(got);
            Aver.AreEqual(0, sut.EnqueuedCount);
            Aver.AreEqual(0, sut.EnqueuedSize);

            //Rabbit was lost
            Aver.IsFalse(sut.TryPeek(out got));
            Aver.IsFalse(sut.TryDequeue(out got));
        }
Exemple #8
0
        public void SetCountLimit_DefaultHandling()
        {
            var sut = new CappedQueue <MyItem>(i => i == null ? 0 : i.Name == null ? 0 : i.Name.Length);

            sut.CountLimit = 3;

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Cat"
            }));
            Aver.AreEqual(1, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Dog"
            }));
            Aver.AreEqual(2, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(null));
            Aver.AreEqual(3, sut.Count);

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Rabbit"
            }));
            Aver.AreEqual(3, sut.Count);


            //1st is Dog because Cat was removed
            Aver.AreEqual(3, sut.EnqueuedCount);
            Aver.AreEqual(3 + 6, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out var got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Dog", got.Name);
            Aver.AreEqual(2, sut.EnqueuedCount);
            Aver.AreEqual(6, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNull(got);
            Aver.AreEqual(1, sut.EnqueuedCount);
            Aver.AreEqual(6, sut.EnqueuedSize);

            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Rabbit", got.Name);
            Aver.AreEqual(0, sut.EnqueuedCount);
            Aver.AreEqual(0, sut.EnqueuedSize);

            Aver.IsFalse(sut.TryPeek(out got));
            Aver.IsFalse(sut.TryDequeue(out got));
        }
Exemple #9
0
        public void CanCopyTo()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            var array = new int[5];

            q.CopyTo(array, 2);
            const int zero = default(int);

            Assert.Equal(Seq(zero, zero, 1, 2, 3), array);
        }
Exemple #10
0
        public void CanClear()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);
            q.Enqueue(4);

            q.Clear();
            q.Clear();             // idempotent

            Assert.Equal(0, q.Count);
            Assert.Equal(3, q.Capacity);
        }
Exemple #11
0
        public void CanEnumerate()
        {
            var q = new CappedQueue <int>(3);

            Assert.Empty(q);

            q.Enqueue(1);

            Assert.Single(q, 1);

            q.Enqueue(2);

            Assert.Equal(Seq(1, 2), q);

            q.Enqueue(3);

            Assert.Equal(Seq(1, 2, 3), q);

            q.Enqueue(4);

            Assert.Equal(Seq(2, 3, 4), q);
        }
Exemple #12
0
        public void CanEnqueue()
        {
            var q1 = new CappedQueue <int>(1);

            q1.Enqueue(1);
            Assert.Equal(1, q1.Count);
            Assert.Equal(1, q1.GetElement(0));

            q1.Enqueue(2);
            Assert.Equal(1, q1.Count);
            Assert.Equal(2, q1.GetElement(0));

            var q2 = new CappedQueue <int>(2);

            q2.Enqueue(1);
            q2.Enqueue(2);
            q2.Enqueue(3);

            Assert.Equal(2, q2.Count);
            Assert.Equal(2, q2.GetElement(0));
            Assert.Equal(3, q2.GetElement(1));
        }
Exemple #13
0
        public void CanSetCapacity()
        {
            var q = new CappedQueue <int>(1);

            q.Capacity = 2;             // enlarge empty queue

            Assert.Equal(0, q.Count);
            Assert.Equal(2, q.Capacity);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));

            q.Capacity = 3;             // enlarge

            Assert.Equal(3, q.Capacity);
            Assert.Equal(2, q.Count);

            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));

            q.Enqueue(4);

            Assert.Equal(3, q.Count);
            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));
            Assert.Equal(4, q.GetElement(2));

            q.Capacity = 2;             // shrink

            Assert.Equal(2, q.Capacity);
            Assert.Equal(2, q.Count);

            Assert.Equal(3, q.GetElement(0));
            Assert.Equal(4, q.GetElement(1));
        }
Exemple #14
0
        public void BasicQueueFunctions()
        {
            var sut = new CappedQueue <MyItem>(i => i == null ? 0 : i.Name == null ? 0 : i.Name.Length);

            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Cat"
            }));
            Aver.AreEqual(1, sut.Count);
            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Dog"
            }));
            Aver.AreEqual(2, sut.Count);
            Aver.IsTrue(sut.TryEnqueue(null));
            Aver.AreEqual(3, sut.Count);
            Aver.IsTrue(sut.TryEnqueue(new MyItem {
                Name = "Bird"
            }));
            Aver.AreEqual(4, sut.Count);

            Aver.AreEqual(4, ((IEnumerable <MyItem>)sut).Count());

            Aver.AreEqual(4, sut.EnqueuedCount);
            Aver.AreEqual(3 + 3 + 4, sut.EnqueuedSize);

            //1st time
            Aver.IsTrue(sut.TryPeek(out var got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Cat", got.Name);

            //2nd time peek
            Aver.IsTrue(sut.TryPeek(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Cat", got.Name);

            //3rd time dequeue
            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Cat", got.Name);

            Aver.AreEqual(3, sut.EnqueuedCount);
            Aver.AreEqual(3 + 4, sut.EnqueuedSize);

            //4th time peek
            Aver.IsTrue(sut.TryPeek(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Dog", got.Name);

            //5th time dequeue
            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Dog", got.Name);

            Aver.AreEqual(2, sut.EnqueuedCount);
            Aver.AreEqual(4, sut.EnqueuedSize);

            //6th time dequeue
            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNull(got);

            Aver.AreEqual(1, sut.EnqueuedCount);
            Aver.AreEqual(4, sut.EnqueuedSize);

            //7th time dequeue the last
            Aver.IsTrue(sut.TryDequeue(out got));
            Aver.IsNotNull(got);
            Aver.AreEqual("Bird", got.Name);

            Aver.AreEqual(0, sut.EnqueuedCount);
            Aver.AreEqual(0, sut.EnqueuedSize);

            Aver.IsFalse(sut.TryPeek(out got));
            Aver.IsFalse(sut.TryDequeue(out got));
        }