Beispiel #1
0
        public void TwoPopsWaitWhenItemsWillBeAvailable()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                AutoResetEvent resetEvent = new AutoResetEvent(false);

                var task1 = Task.Run(() =>
                {
                    resetEvent.Set();
                    return(richQueue.Pop());
                });

                // Wait when Pop task begin
                resetEvent.WaitOne();

                var task2 = Task.Run(() =>
                {
                    resetEvent.Set();
                    return(richQueue.Pop());
                });

                // Wait when Pop task begin
                resetEvent.WaitOne();

                richQueue.Push(5);
                richQueue.Push(7);

                Assert.AreEqual(5, task1.Result);
                Assert.AreEqual(7, task2.Result);
            }
        }
Beispiel #2
0
        public void SeveralDisposeCallsNotThrowsException()
        {
            RichQueue <int> richQueue = new RichQueue <int>();

            richQueue.Dispose();
            richQueue.Dispose();
        }
Beispiel #3
0
        public void PushThrowsOnDisposedObject()
        {
            RichQueue <int> richQueue = new RichQueue <int>();

            richQueue.Dispose();

            Assert.Catch <ObjectDisposedException>(() => richQueue.Push(5));
        }
Beispiel #4
0
        public void PopReturnsPushedItem()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                richQueue.Push(5);
                var actualValue = richQueue.Pop();

                Assert.AreEqual(5, actualValue);
            }
        }
Beispiel #5
0
        public void PushIncreasesCount()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                richQueue.Push(5);
                richQueue.Push(7);

                Assert.AreEqual(2, richQueue.Count);
            }
        }
Beispiel #6
0
        public void PopDecreasesCount()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                richQueue.Push(5);
                var countBeforePop = richQueue.Count;

                richQueue.Pop();

                Assert.AreEqual(countBeforePop - 1, richQueue.Count);
            }
        }
Beispiel #7
0
        public void CountThrowsOnDisposedObject()
        {
            RichQueue <int> richQueue = new RichQueue <int>();

            richQueue.Push(5);
            richQueue.Dispose();

            Assert.Catch <ObjectDisposedException>(() =>
            {
                var count = richQueue.Count;
            });
        }
Beispiel #8
0
        public void EmptyQueue_ZeroCountDefaultFirstValue()
        {
            // Arrange
            var queue = new RichQueue <int>();

            // Act
            var count = queue.Count;
            var first = queue.First;

            // Assert
            Assert.Equal(0, count);
            Assert.Equal(0, first);
        }
Beispiel #9
0
        public void DequeueEvenValues_ReturnsEmptyCollection()
        {
            // Arrange
            var queue            = new RichQueue <int>();
            var sourceCollection = new[] { 1, 3, 5, 7 };

            // Act
            queue.Enqueue(sourceCollection);
            var result = queue.Remove(x => x % 2 == 0).ToList();

            // Assert
            Assert.Empty(result);
        }
Beispiel #10
0
        public void EnqueueCollection_ValidCountAndFirst()
        {
            // Arrange
            var queue      = new RichQueue <int>();
            var collection = new [] { 1, 2, 3 };

            // Act
            queue.Enqueue(collection);

            // Assert
            Assert.Equal(collection.Length, queue.Count);
            Assert.Equal(collection[0], queue.First);
        }
Beispiel #11
0
        public void PopTwiseReturnsTwoPushedItem()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                richQueue.Push(5);
                richQueue.Push(7);

                var actualValue1 = richQueue.Pop();
                var actualValue2 = richQueue.Pop();

                Assert.AreEqual(5, actualValue1);
                Assert.AreEqual(7, actualValue2);
            }
        }
Beispiel #12
0
        public void DequeueEvenValues_ReturnsExpectedCollection()
        {
            // Arrange
            var queue              = new RichQueue <int>();
            var sourceCollection   = new[] { 1, 2, 3, 4, 5, 6, 7 };
            var expectedCollection = new[] { 2, 4, 6 };

            // Act
            queue.Enqueue(sourceCollection);
            var result = queue.Remove(x => x % 2 == 0).ToList();

            // Assert
            Assert.True(CollectionExtensions.IsCollectionsEqual(expectedCollection, result));
        }
Beispiel #13
0
        public void DequeueCollection_ValidCountAndFirst()
        {
            // Arrange
            var queue      = new RichQueue <int>();
            var collection = new[] { 1, 2, 3 };

            // Act
            queue.Enqueue(collection);
            var first  = queue.Dequeue();
            var second = queue.Dequeue();

            // Assert
            Assert.Equal(collection[0], first);
            Assert.Equal(collection[1], second);
            Assert.Equal(collection[2], queue.First);
        }
Beispiel #14
0
        public void PopWaitsWhenItemWillBeAvailable()
        {
            using (RichQueue <int> richQueue = new RichQueue <int>())
            {
                ManualResetEvent resetEvent = new ManualResetEvent(false);
                var task = Task.Run(() =>
                {
                    resetEvent.Set();

                    return(richQueue.Pop());
                });

                // Wait when Pop task begin
                resetEvent.WaitOne();
                richQueue.Push(5);

                Assert.AreEqual(5, task.Result);
            }
        }