public void ShortRange_LengthDivisibleByStep_IterationCountMatchesCalculated()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            // note that the number of steps is limited to 1000 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 1000))));

            // In case range length is under 4, ensure the step is at least 1
            if (step < 1)
            {
                step = 1;
            }

            //ensure that step size is a factor of the length of the range
            start += (short)(length % step);

            ShortRange shortRange = new ShortRange(start, end, step);

            // Range endpoint is inclusive, so must take into account this extra iteration
            Assert.AreEqual(
                length / step + 1,
                shortRange.Count(),
                "Iteration count should be (end-start)/step + 1 where endpoint is included");
        }
        public void ShortRange_Iterating_DifferenceBetweenIterationsMatchesStepSize()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            // note that the number of steps is limited to 100 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 100))));

            // In case range length is under 4, ensure the step is at least 1
            if (step < 1)
            {
                step = 1;
            }

            ShortRange shortRange = new ShortRange(start, end, step);

            short?previous = null;

            foreach (short i in shortRange)
            {
                if (previous.HasValue)
                {
                    Assert.AreEqual(
                        i - previous,
                        step,
                        "Difference between iteration values should match the step value supplied");
                }
                previous = i;
            }
        }
        public void ShortRange_LengthNotDivisibleByStep_IterationCountMatchesCalculated()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            // note that the number of steps is limited to 1000 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 1000))));

            // In case range length is under 4, ensure the step is at least 2
            if (step < 2)
            {
                step = 2;
            }

            //ensure that step size is not a factor of the length of the range
            if (length % step == 0)
            {
                start += (short)Random.Next(1, step - 1);
                length = (short)(end - start);
            }

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreEqual(length / step + 1, shortRange.Count(), "Iteration count should be (start-end)/step +1");
        }
        public void ShortRange_EndBeforeStart_ThrowsArgumentOutOfRangeException()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue + length, short.MaxValue);
            short end    = (short)(start - length);

            ShortRange shortRange = new ShortRange(start, end);
        }
        public void ShortRange_ConstructorWithoutStepParam_StepDefaultsToOne()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);

            ShortRange shortRange = new ShortRange(start, end);

            Assert.AreEqual(start, shortRange.Start, "Starting point field must match the value supplied");
            Assert.AreEqual(end, shortRange.End, "End point field must match the value supplied");
            Assert.AreEqual(1, shortRange.Step, "Step amount must default to one");
        }
        public void ShortRange_StepIsLargerThanShortRange_ParametersMatchThoseGiven()
        {
            short length = (short)Random.Next(1, short.MaxValue / 2);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            short step = (short)(length + Random.Next(1, short.MaxValue / 3));

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreEqual(start, shortRange.Start, "Starting point field must match the value supplied");
            Assert.AreEqual(end, shortRange.End, "End point field must match the value supplied");
            Assert.AreEqual(step, shortRange.Step, "Step amount field must match the value supplied");
        }
        public void ShortRange_StepIsLargerThanShortRange_ParametersMatchThoseGiven()
        {
            short length = (short)Random.Next(1, short.MaxValue / 2);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            short step   = (short)(length + Random.Next(1, short.MaxValue / 3));

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreEqual(start, shortRange.Start, "Starting point field must match the value supplied");
            Assert.AreEqual(end, shortRange.End, "End point field must match the value supplied");
            Assert.AreEqual(step, shortRange.Step, "Step amount field must match the value supplied");
        }
        public void ShortRange_ConvertingToString_IsNotBlank()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            short step = (short)Random.Next(1, length / 2);

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreNotEqual(
                string.Empty,
                shortRange.ToString(),
                "String representation of range must not be an empty string");
        }
        public void ShortRange_ConvertingToString_IsNotBlank()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            short step   = (short)Random.Next(1, length / 2);

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreNotEqual(
                string.Empty,
                shortRange.ToString(),
                "String representation of range must not be an empty string");
        }
        public void ShortRange_UsingLargestPossibleParameters_IteratesSuccessfully()
        {
            // Step chosen to avoid an unfeasible number of iterations
            ShortRange shortRange = new ShortRange(short.MinValue, short.MaxValue, short.MaxValue / 16);

            bool iterated = false;

            foreach (short i in shortRange)
            {
                iterated = true;
            }

            Assert.AreEqual(true, iterated, "When iterating across full range, at least one value should be returned");
        }
        public void ShortRange_Iterating_ValuesStayWithinRange()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start  = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end    = (short)(start + length);
            // note that the number of steps is limited to 100 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 100))));

            // ensure the step is at least 1 (as length of less than four causes it to round down to zero)
            if (step < 1)
            {
                step = 1;
            }

            ShortRange shortRange = new ShortRange(start, end, step);

            foreach (short i in shortRange)
            {
                Assert.IsTrue(i >= start, "Value from iterator must by equal or above start parameter");
                Assert.IsTrue(i <= end, "Value from iterator must be equal or below end parameter");
            }
        }
        public void ShortRange_UsingLargestPossibleParameters_IteratesSuccessfully()
        {
            // Step chosen to avoid an unfeasible number of iterations
            ShortRange shortRange = new ShortRange(short.MinValue, short.MaxValue, short.MaxValue / 16);

            bool iterated = false;
            foreach (short i in shortRange)
                iterated = true;

            Assert.AreEqual(true, iterated, "When iterating across full range, at least one value should be returned");
        }
        public void ShortRange_Iterating_DifferenceBetweenIterationsMatchesStepSize()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            // note that the number of steps is limited to 100 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 100))));

            // In case range length is under 4, ensure the step is at least 1
            if (step < 1) step = 1;

            ShortRange shortRange = new ShortRange(start, end, step);

            short? previous = null;
            foreach (short i in shortRange)
            {
                if (previous.HasValue)
                    Assert.AreEqual(
                        i - previous,
                        step,
                        "Difference between iteration values should match the step value supplied");
                previous = i;
            }
        }
        public void ShortRange_LengthNotDivisibleByStep_IterationCountMatchesCalculated()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            // note that the number of steps is limited to 1000 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 1000))));

            // In case range length is under 4, ensure the step is at least 2
            if (step < 2) step = 2;

            //ensure that step size is not a factor of the length of the range
            if (length % step == 0)
            {
                start += (short)Random.Next(1, step - 1);
                length = (short)(end - start);
            }

            ShortRange shortRange = new ShortRange(start, end, step);

            Assert.AreEqual(length / step + 1, shortRange.Count(), "Iteration count should be (start-end)/step +1");
        }
        public void ShortRange_LengthDivisibleByStep_IterationCountMatchesCalculated()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            // note that the number of steps is limited to 1000 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 1000))));

            // In case range length is under 4, ensure the step is at least 1
            if (step < 1) step = 1;

            //ensure that step size is a factor of the length of the range
            start += (short)(length % step);

            ShortRange shortRange = new ShortRange(start, end, step);

            // Range endpoint is inclusive, so must take into account this extra iteration
            Assert.AreEqual(
                length / step + 1,
                shortRange.Count(),
                "Iteration count should be (end-start)/step + 1 where endpoint is included");
        }
        public void ShortRange_ConstructorWithoutStepParam_StepDefaultsToOne()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);

            ShortRange shortRange = new ShortRange(start, end);

            Assert.AreEqual(start, shortRange.Start, "Starting point field must match the value supplied");
            Assert.AreEqual(end, shortRange.End, "End point field must match the value supplied");
            Assert.AreEqual(1, shortRange.Step, "Step amount must default to one");
        }
        public void ShortRange_EndBeforeStart_ThrowsArgumentOutOfRangeException()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue + length, short.MaxValue);
            short end = (short)(start - length);

            ShortRange shortRange = new ShortRange(start, end);
        }
        public void ShortRange_Iterating_ValuesStayWithinRange()
        {
            short length = (short)Random.Next(1, short.MaxValue);
            short start = (short)Random.Next(short.MinValue, short.MaxValue - length);
            short end = (short)(start + length);
            // note that the number of steps is limited to 100 or fewer
            short step = (short)(length / Random.Next(4, Math.Max(4, Math.Min(length / 2, 100))));

            // ensure the step is at least 1 (as length of less than four causes it to round down to zero)
            if (step < 1) step = 1;

            ShortRange shortRange = new ShortRange(start, end, step);

            foreach (short i in shortRange)
            {
                Assert.IsTrue(i >= start, "Value from iterator must by equal or above start parameter");
                Assert.IsTrue(i <= end, "Value from iterator must be equal or below end parameter");
            }
        }