public void PrioritizedNodeTests_Constructor()
        {
            var prioritizable = new Prioritizable(1.21f);
            var node          = new PrioritizedNode <Prioritizable>(prioritizable);

            Assert.That(ReferenceEquals(node.Data, prioritizable), Is.True);
        }
        public void SamplesItemsWhenSizeLimitReached_AddIEnumerable()
        {
            const int   reservoirSize  = 100;
            const int   lowerPriority  = 100;
            const int   higherPriority = 300;
            const float priorityShift  = 0.001f;

            //Concurrent Priority Queue will only hold NumberOfItemsToAddInitially items.
            ConcurrentPriorityQueue.Resize(reservoirSize);

            var higherPriorityItemsToAdd = new PrioritizedNode <TestModel> [reservoirSize];
            var itemsToAddInitially      = new PrioritizedNode <TestModel> [reservoirSize];

            for (var i = 0; i < reservoirSize; ++i)
            {
                itemsToAddInitially[i]      = Create((i + lowerPriority) * priorityShift);
                higherPriorityItemsToAdd[i] = Create((i + higherPriority) * priorityShift);
            }

            ConcurrentPriorityQueue.Add(itemsToAddInitially);

            //make sure they are all accounted for
            Assert.That(ConcurrentPriorityQueue.Count, Is.EqualTo(reservoirSize));

            ConcurrentPriorityQueue.Add(higherPriorityItemsToAdd);

            //make sure the size is not over
            Assert.That(ConcurrentPriorityQueue.Count, Is.EqualTo(reservoirSize));

            foreach (var item in ConcurrentPriorityQueue)
            {
                Assert.That(item.Data.Priority, Is.GreaterThanOrEqualTo(higherPriority * priorityShift));
            }
        }
        public void PrioritizedNodeTests_SamePriorityHashCodesDiffer()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

            Assert.That(node1.GetHashCode(), Is.Not.EqualTo(node2.GetHashCode()));
        }
        public void PrioritizedNodeTests_DifferentPriorityABAStartLow()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2 = new PrioritizedNode <Prioritizable>(new Prioritizable(5.75f));
            var node3 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

            //node2 < node1 < node3
            Assert.Multiple(() =>
            {
                Assert.That(node1 > node2, Is.True);
                Assert.That(node1 >= node2, Is.True);
                Assert.That(node1 < node2, Is.False);
                Assert.That(node1 <= node2, Is.False);
                Assert.That(node1 == node2, Is.False);
                Assert.That(node1 != node2, Is.True);

                Assert.That(node1 > node3, Is.False);
                Assert.That(node1 >= node3, Is.False);
                Assert.That(node1 < node3, Is.True);
                Assert.That(node1 <= node3, Is.True);
                Assert.That(node1 == node3, Is.False);
                Assert.That(node1 != node3, Is.True);

                Assert.That(node2 > node3, Is.False);
                Assert.That(node2 >= node3, Is.False);
                Assert.That(node2 < node3, Is.True);
                Assert.That(node2 <= node3, Is.True);
                Assert.That(node2 == node3, Is.False);
                Assert.That(node2 != node3, Is.True);
            }
                            );
        }
        public void PrioritizedNodeTests_SamePriorityInequalityAsNull()
        {
            var    node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            object node2 = null;

            Assert.That(node1.CompareTo(node2), Is.EqualTo(1));
        }
        public void PrioritizedNodeTests_SamePriorityInequalityAsWrongObject()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2 = (object)new StringBuilder();

            Assert.That(() => node1.CompareTo(node2), Throws.ArgumentException);
        }
        public void PrioritizedNodeTests_SamePriorityInequalityNull()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2 = (PrioritizedNode <Prioritizable>)null;

            Assert.Multiple(() =>
            {
                Assert.That(() => node1 > null, Throws.ArgumentNullException);
                Assert.That(() => node1 >= null, Throws.ArgumentNullException);
                Assert.That(() => node1 <= null, Is.False);
                Assert.That(() => node1 < null, Is.False);
                Assert.That(() => null > node1, Is.False);
                Assert.That(() => null >= node1, Is.False);
                Assert.That(() => null <= node1, Throws.ArgumentNullException);
                Assert.That(() => null < node1, Throws.ArgumentNullException);

                Assert.That(() => node1 > node2, Throws.ArgumentNullException);
                Assert.That(() => node1 >= node2, Throws.ArgumentNullException);
                Assert.That(() => node1 <= node2, Is.False);
                Assert.That(() => node1 < node2, Is.False);
                Assert.That(() => node2 > node1, Is.False);
                Assert.That(() => node2 >= node1, Is.False);
                Assert.That(() => node2 <= node1, Throws.ArgumentNullException);
                Assert.That(() => node2 < node1, Throws.ArgumentNullException);
            });
        }
        public void PrioritizedNodeTests_identityOperatorEqual()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

#pragma warning disable CS1718 // Comparison made to same variable
            var result = node1 == node1;
#pragma warning restore CS1718 // Comparison made to same variable
            Assert.That(result, Is.True);
        }
        public void PrioritizedNodeTests_SamePriorityNotEqual()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

            Assert.That(node1, Is.Not.EqualTo(node2));
            var result = node1 != node2;

            Assert.That(result, Is.True);
        }
        public void PrioritizedNodeTests_SamePriorityInequalityAsObject()
        {
            var node1  = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2  = (object)new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var result = node1.CompareTo(node2);

            Assert.Multiple(() =>
            {
                Assert.That(result, Is.EqualTo(-1));
            });
        }
        public void PrioritizedNodeTests_SamePriorityInequality()
        {
            var node1                    = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var node2                    = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            var resultGreaterthan        = node1 > node2;
            var resultGreaterthanorequal = node1 >= node2;
            var resultLessthan           = node1 < node2;
            var resultLessthanorequal    = node1 <= node2;

            Assert.Multiple(() =>
            {
                Assert.That(resultGreaterthan, Is.False);
                Assert.That(resultGreaterthanorequal, Is.False);
                Assert.That(resultLessthan, Is.True);
                Assert.That(resultLessthanorequal, Is.True);
            });
        }
        private static void ExerciseFullApi(IResizableCappedCollection <PrioritizedNode <TestModel> > concurrentPriorityQueue, PrioritizedNode <TestModel>[] eventsToAdd, int countOfThreads)
        {
            // Add the new events
            foreach (var evt in eventsToAdd)
            {
                concurrentPriorityQueue.Add(evt);
            }

            //make sure each of the new events can be found (tests equality op)
            foreach (var ev in eventsToAdd)
            {
                Assert.That(concurrentPriorityQueue, Contains.Item(ev));
            }

            //copy the CPQ to an array
            var destinationArray = new PrioritizedNode <TestModel> [eventsToAdd.Count() * countOfThreads];

            concurrentPriorityQueue.CopyTo(destinationArray, 0);

            //check that the copied contents contains our events
            foreach (var ev in eventsToAdd)
            {
                Assert.That(destinationArray, Contains.Item(ev));
            }

            //count how many are actually in the destinationArray
            var nonnullCount = 0;

            while (nonnullCount < destinationArray.Length && null != destinationArray[nonnullCount])
            {
                ++nonnullCount;
            }

            //make sure the array is sorted properly
            for (var index = 1; index < nonnullCount; ++index)
            {
                Assert.That(destinationArray[index - 1].Data.Priority, Is.GreaterThanOrEqualTo(destinationArray[index].Data.Priority));
            }

            //make sure that remove is not supported
            Assert.That(() => concurrentPriorityQueue.Remove(eventsToAdd[0]), Throws.TypeOf <NotSupportedException>());
        }
        public void PrioritizedNodeTests_DifferentPrioritySecondLower()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(5.75f));
            var node2 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));
            //node1 < node2
            var resultGreaterthan        = node1 > node2;
            var resultGreaterthanorequal = node1 >= node2;
            var resultLessthan           = node1 < node2;
            var resultLessthanorequal    = node1 <= node2;
            var resultEqual    = node1 == node2;
            var resultNotEqual = node1 != node2;

            Assert.Multiple(() =>
            {
                Assert.That(resultGreaterthan, Is.False);
                Assert.That(resultGreaterthanorequal, Is.False);
                Assert.That(resultLessthan, Is.True);
                Assert.That(resultLessthanorequal, Is.True);
                Assert.That(resultEqual, Is.False);
                Assert.That(resultNotEqual, Is.True);
            });
        }
        public void SamplesItemsWhenSizeLimitReached()
        {
            const int numberOfItemsToAddInitially = 100;
            const int numberOfItemsToAddAfterReservoirLimitReached = 100;

            //Concurrent Priority Queue will only hold NumberOfItemsToAddInitially items.
            ConcurrentPriorityQueue.Resize(numberOfItemsToAddInitially);

            var itemsToAddInitially = new PrioritizedNode <TestModel> [numberOfItemsToAddInitially];

            for (var i = 0; i < numberOfItemsToAddInitially; ++i)
            {
                itemsToAddInitially[i] = Create(i * 0.001f);
            }

            //fill the CPQ with values 100-199
            foreach (var itemToAdd in itemsToAddInitially)
            {
                Assert.IsTrue(ConcurrentPriorityQueue.Add(itemToAdd), "failed to add initial value");
            }

            //make sure they are all accounted for
            Assert.AreEqual(ConcurrentPriorityQueue.Count, numberOfItemsToAddInitially);

            //now add more items that will cause the items from above to get removed. these will be valued 0-99 (precisely 100 less than those above)
            for (var i = 0; i < numberOfItemsToAddAfterReservoirLimitReached; ++i)
            {
                var itemToAdd = Create((i + 100) * 0.001f);
                var itemThatWillGetRemoved = itemsToAddInitially[i];

                //each one we add will cause the corresponding smaller item to get removed.
                Assert.IsTrue(ConcurrentPriorityQueue.Add(itemToAdd), "failed to add subsequent value");
                Assert.IsTrue(ConcurrentPriorityQueue.Contains(itemToAdd), "added value not found");
                Assert.IsFalse(ConcurrentPriorityQueue.Contains(itemThatWillGetRemoved), "initial value did not get removed on addition of subsequent value");
                Assert.AreEqual(ConcurrentPriorityQueue.Count, numberOfItemsToAddInitially);
            }
        }
        public void PrioritizedNodeTests_identityHashCodesSame()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

            Assert.That(node1.GetHashCode(), Is.EqualTo(node1.GetHashCode()));
        }
        public void PrioritizedNodeTests_identityEqualTo()
        {
            var node1 = new PrioritizedNode <Prioritizable>(new Prioritizable(1.21f));

            Assert.That(node1, Is.EqualTo(node1));
        }