Rename n.To(n) to n.RangeTo(n)

This commit is contained in:
Oliver Booth 2022-04-27 21:58:46 +01:00
parent 23f912f4bb
commit b743adb445
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
11 changed files with 340 additions and 240 deletions

View File

@ -33,4 +33,64 @@ public class ByteTests
// Π_(i=1)^n (2i) will overflow at i=4 for byte
}
[TestMethod]
public void RangeTo_Byte_ShouldYieldCorrectValues()
{
const byte start = 1;
const byte end = 10;
byte current = 1;
foreach (byte value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int16_ShouldYieldCorrectValues()
{
const byte start = 1;
const short end = 10;
short current = 1;
foreach (short value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int32_ShouldYieldCorrectValues()
{
const byte start = 1;
const int end = 10;
int current = 1;
foreach (int value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int64_ShouldYieldCorrectValues()
{
const byte start = 1;
const long end = 10;
long current = 1;
foreach (long value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
}

View File

@ -37,4 +37,49 @@ public class Int16Tests
// Π_(i=1)^n (2i) will overflow at i=6 for short
}
[TestMethod]
public void RangeTo_Int16_ShouldYieldCorrectValues()
{
const short start = 1;
const short end = 10;
short current = 1;
foreach (short value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int32_ShouldYieldCorrectValues()
{
const short start = 1;
const int end = 10;
int current = 1;
foreach (int value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int64_ShouldYieldCorrectValues()
{
const short start = 1;
const long end = 10;
long current = 1;
foreach (long value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
}

View File

@ -40,4 +40,34 @@ public class Int32Tests
// Π_(i=1)^n (2i) will overflow at i=10 for int
}
[TestMethod]
public void RangeTo_Int32_ShouldYieldCorrectValues()
{
const int start = 1;
const int end = 10;
int current = 1;
foreach (int value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
[TestMethod]
public void RangeTo_Int64_ShouldYieldCorrectValues()
{
const int start = 1;
const long end = 10;
long current = 1;
foreach (long value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
}

View File

@ -41,4 +41,19 @@ public class Int64Tests
Assert.AreEqual(185794560, Enumerable.Range(1, 9).Product(Double));
Assert.AreEqual(3715891200, Enumerable.Range(1, 10).Product(Double));
}
[TestMethod]
public void RangeTo_Int64_ShouldYieldCorrectValues()
{
const long start = 1;
const long end = 10;
long current = 1;
foreach (long value in start.RangeTo(end))
{
Assert.AreEqual(current++, value);
}
Assert.AreEqual(current, end);
}
}

View File

@ -7,86 +7,6 @@ namespace X10D;
/// </summary>
public static class Int16Extensions
{
/// <summary>
/// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<short> To(this short start, short end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 16-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 16-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<short> To(this short start, short end, short step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (short i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
short absStep = System.Math.Abs(step);
for (short i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>

View File

@ -70,86 +70,6 @@ public static class Int32Extensions
return ((float)value).DegreesToRadians();
}
/// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<int> To(this int start, int end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 32-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 32-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<int> To(this int start, int end, int step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (int i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
int absStep = System.Math.Abs(step);
for (int i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>

View File

@ -51,86 +51,6 @@ public static class Int64Extensions
return persistence;
}
/// <summary>
/// Returns an enumerable collection of 64-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <returns>
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<long> To(this long start, long end)
{
return start.To(end, 1);
}
/// <summary>
/// Returns an enumerable collection of 64-bit signed integers that range from the current value to a specified value.
/// </summary>
/// <param name="start">The value of the first integer in the sequence.</param>
/// <param name="end">The value of the last integer in the sequence.</param>
/// <param name="step">The increment by which to step.</param>
/// <returns>
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in ascending order if <paramref name="start" /> is less than than <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection of 64-bit signed integers that range from <paramref name="start" /> to
/// <paramref name="end" /> in descending order if <paramref name="start" /> is greater than than
/// <paramref name="end" />.
/// </para>
/// -or-
/// <para>
/// An enumerable collection that contains a single value if <paramref name="start" /> is equal to
/// <paramref name="end" />.
/// </para>
/// </returns>
public static IEnumerable<long> To(this long start, long end, long step)
{
if (step == 0)
{
throw new ArgumentOutOfRangeException(nameof(step), "Step cannot be zero.");
}
if (end == start)
{
yield return start;
yield break;
}
if (step > 0 || start < end)
{
for (long i = start; i <= end; i += step)
{
yield return i;
}
}
else
{
long absStep = System.Math.Abs(step);
for (long i = start; i >= end; i -= absStep)
{
yield return i;
}
}
}
/// <summary>
/// Converts an integer value from network byte order to host byte order.
/// </summary>

View File

@ -52,4 +52,80 @@ public static class ByteExtensions
{
return source.Select(selector).Product();
}
/// <summary>
/// Returns an enumerable sequence of 8-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 8-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<byte> RangeTo(this byte value, byte end)
{
byte start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (byte current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 16-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 16-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<short> RangeTo(this byte value, short end)
{
short start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (short current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 32-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 32-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<int> RangeTo(this byte value, int end)
{
int start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (int current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 64-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 64-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<long> RangeTo(this byte value, long end)
{
long start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (long current = start; current < end; current++)
{
yield return current;
}
}
}

View File

@ -52,4 +52,61 @@ public static class Int16Extensions
{
return source.Select(selector).Product();
}
/// <summary>
/// Returns an enumerable sequence of 16-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 16-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<short> RangeTo(this short value, short end)
{
short start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (short current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 32-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 32-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<int> RangeTo(this short value, int end)
{
int start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (int current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 64-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 64-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<long> RangeTo(this short value, long end)
{
long start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (long current = start; current < end; current++)
{
yield return current;
}
}
}

View File

@ -52,4 +52,42 @@ public static class Int32Extensions
{
return source.Select(selector).Product();
}
/// <summary>
/// Returns an enumerable sequence of 32-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 32-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<int> RangeTo(this int value, int end)
{
int start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (int current = start; current < end; current++)
{
yield return current;
}
}
/// <summary>
/// Returns an enumerable sequence of 64-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 64-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<long> RangeTo(this int value, long end)
{
long start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (long current = start; current < end; current++)
{
yield return current;
}
}
}

View File

@ -52,4 +52,23 @@ public static class Int64Extensions
{
return source.Select(selector).Product();
}
/// <summary>
/// Returns an enumerable sequence of 64-bit integers ranging from the current value to a specified value.
/// </summary>
/// <param name="value">The starting value of the sequence.</param>
/// <param name="end">The ending value of the sequence.</param>
/// <returns>
/// An enumerable collection of 64-bit integers, ranging from <paramref name="value" /> to <paramref name="end" />.
/// </returns>
public static IEnumerable<long> RangeTo(this long value, long end)
{
long start = System.Math.Min(value, end);
end = System.Math.Max(value, end);
for (long current = start; current < end; current++)
{
yield return current;
}
}
}