public void EnsureCapacity_Generic_ExistingCapacityRequested_SameValueReturned(int capacity)
        {
            var set = new SegmentedHashSet <T>(capacity);

            Assert.Equal(capacity, set.EnsureCapacity(capacity));

            set = (SegmentedHashSet <T>)GenericISetFactory(capacity);
            Assert.Equal(capacity, set.EnsureCapacity(capacity));
        }
        public void EnsureCapacity_Generic_HashsetNotEmpty_SetsToAtLeastTheRequested(int setLength)
        {
            SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength);

            // get current capacity
            int currentCapacity = set.EnsureCapacity(0);

            // assert we can update to a larger capacity
            int newCapacity = set.EnsureCapacity(currentCapacity * 2);

            Assert.InRange(newCapacity, currentCapacity * 2, int.MaxValue);
        }
        public void EnsureCapacity_Generic_GrowCapacityWithFreeList(int setLength)
        {
            SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength);

            // Remove the first element to ensure we have a free list.
            Assert.True(set.Remove(set.ElementAt(0)));

            int currentCapacity = set.EnsureCapacity(0);

            Assert.True(currentCapacity > 0);

            int newCapacity = set.EnsureCapacity(currentCapacity + 1);

            Assert.True(newCapacity > currentCapacity);
        }
        public void EnsureCapacity_Generic_RequestingLargerCapacity_DoesNotInvalidateEnumeration(int setLength)
        {
            SegmentedHashSet <T> set   = (SegmentedHashSet <T>)(GenericISetFactory(setLength));
            var         capacity       = set.EnsureCapacity(0);
            IEnumerator valuesEnum     = set.GetEnumerator();
            IEnumerator valuesListEnum = new List <T>(set).GetEnumerator();

            set.EnsureCapacity(capacity + 1); // Verify EnsureCapacity does not invalidate enumeration

            while (valuesEnum.MoveNext())
            {
                valuesListEnum.MoveNext();
                Assert.Equal(valuesListEnum.Current, valuesEnum.Current);
            }
        }
        public void EnsureCapacity_Generic_RequestedCapacitySmallerThanCurrent_CapacityUnchanged(int currentCapacity)
        {
            SegmentedHashSet <T> set;

            // assert capacity remains the same when ensuring a capacity smaller or equal than existing
            for (int i = 0; i <= currentCapacity; i++)
            {
                set = new SegmentedHashSet <T>(currentCapacity);
                Assert.Equal(currentCapacity, set.EnsureCapacity(i));
            }
        }
        public void HashSet_Generic_Constructor_Capacity_ToNextPrimeNumber()
        {
            // Highest pre-computed number + 1.
            const int Capacity = 7199370;
            var       set      = new SegmentedHashSet <T>(Capacity);

            // Assert that the HashTable's capacity is set to the descendant prime number of the given one.
            const int NextPrime = 7199371;

            Assert.Equal(NextPrime, set.EnsureCapacity(0));
        }
        public void EnsureCapacity_Generic_CapacityIsSetToPrimeNumberLargerOrEqualToRequested()
        {
            var set = new SegmentedHashSet <T>();

            Assert.Equal(17, set.EnsureCapacity(17));

            set = new SegmentedHashSet <T>();
            Assert.Equal(17, set.EnsureCapacity(15));

            set = new SegmentedHashSet <T>();
            Assert.Equal(17, set.EnsureCapacity(13));
        }
        public void EnsureCapacity_Generic_EnsureCapacityCalledTwice_ReturnsSameValue(int setLength)
        {
            SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength);
            int capacity             = set.EnsureCapacity(0);

            Assert.Equal(capacity, set.EnsureCapacity(0));

            set      = (SegmentedHashSet <T>)GenericISetFactory(setLength);
            capacity = set.EnsureCapacity(setLength);
            Assert.Equal(capacity, set.EnsureCapacity(setLength));

            set      = (SegmentedHashSet <T>)GenericISetFactory(setLength);
            capacity = set.EnsureCapacity(setLength + 1);
            Assert.Equal(capacity, set.EnsureCapacity(setLength + 1));
        }
        public void EnsureCapacity_Generic_HashsetNotEmpty_RequestedSmallerThanCount_ReturnsAtLeastSizeOfCount(int setLength)
        {
            SegmentedHashSet <T> set = (SegmentedHashSet <T>)GenericISetFactory(setLength);

            Assert.InRange(set.EnsureCapacity(setLength - 1), setLength, int.MaxValue);
        }
        public void EnsureCapacity_Generic_HashsetNotInitialized_RequestedNonZero_CapacityIsSetToAtLeastTheRequested(int requestedCapacity)
        {
            var set = new SegmentedHashSet <T>();

            Assert.InRange(set.EnsureCapacity(requestedCapacity), requestedCapacity, int.MaxValue);
        }
        public void EnsureCapacity_Generic_HashsetNotInitialized_RequestedZero_ReturnsZero()
        {
            var set = new SegmentedHashSet <T>();

            Assert.Equal(0, set.EnsureCapacity(0));
        }
        public void EnsureCapacity_Generic_NegativeCapacityRequested_Throws()
        {
            var set = new SegmentedHashSet <T>();

            Assert.Throws <ArgumentOutOfRangeException>("capacity", () => set.EnsureCapacity(-1));
        }