1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-25 20:48:47 +00:00

feat: add CountDigit for integer types

This commit is contained in:
Oliver Booth 2023-04-06 14:40:27 +01:00
parent 0621c246a0
commit 9791fd23bb
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
19 changed files with 586 additions and 0 deletions

View File

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added extension methods for `DateOnly`, for parity with `DateTime` and `DateTimeOffset`.
- X10D: Added math-related extension methods for `BigInteger`.
- X10D: Added `Span<T>.Replace(T, T)`.
- X10D: Added `CountDigits` for integer types.
### Changed
- X10D: `DateTime.Age(DateTime)` and `DateTimeOffset.Age(DateTimeOffset)` parameter renamed from `asOf` to `referenceDate`.

View File

@ -7,6 +7,61 @@ namespace X10D.Tests.Math;
[TestFixture]
public partial class BigIntegerTests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
BigInteger value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
BigInteger value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_GivenNegative1()
{
BigInteger value = -1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
BigInteger value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
BigInteger value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -6,6 +6,50 @@ namespace X10D.Tests.Math;
[TestFixture]
public partial class ByteTests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const byte value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const byte value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const byte value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const byte value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -6,6 +6,61 @@ namespace X10D.Tests.Math;
[TestFixture]
public partial class Int16Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const short value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const short value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_GivenNegative1()
{
const short value = -1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const short value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const short value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -6,6 +6,61 @@ namespace X10D.Tests.Math;
[TestFixture]
public partial class Int32Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const int value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const int value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_GivenNegative1()
{
const int value = -1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const int value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const int value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -6,6 +6,61 @@ namespace X10D.Tests.Math;
[TestFixture]
public partial class Int64Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const long value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const long value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_GivenNegative1()
{
const long value = -1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const long value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const long value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -7,6 +7,61 @@ namespace X10D.Tests.Math;
[CLSCompliant(false)]
public partial class SByteTests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const sbyte value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const sbyte value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_GivenNegative1()
{
const sbyte value = -1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const sbyte value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given127()
{
const sbyte value = 127;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -7,6 +7,50 @@ namespace X10D.Tests.Math;
[CLSCompliant(false)]
public partial class UInt16Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const ushort value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const ushort value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const ushort value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const ushort value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -7,6 +7,49 @@ namespace X10D.Tests.Math;
[CLSCompliant(false)]
public partial class UInt32Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const uint value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const uint value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const uint value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const uint value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -7,6 +7,50 @@ namespace X10D.Tests.Math;
[CLSCompliant(false)]
public partial class UInt64Tests
{
[Test]
public void CountDigits_ShouldReturn1_Given0()
{
const ulong value = 0;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn1_Given1()
{
const ulong value = 1;
const int expected = 1;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn2_Given10()
{
const ulong value = 10;
const int expected = 2;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void CountDigits_ShouldReturn3_Given199()
{
const ulong value = 199;
const int expected = 3;
int result = value.CountDigits();
Assert.That(result, Is.EqualTo(expected));
}
[Test]
public void DigitalRootShouldBeCorrect()
{

View File

@ -10,6 +10,21 @@ namespace X10D.Math;
/// </summary>
public static class BigIntegerExtensions
{
/// <summary>
/// Returns the number of digits in the current integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this BigInteger value)
{
if (value == 0)
{
return 1;
}
return (int)(1 + BigInteger.Log10(BigInteger.Abs(value)));
}
/// <summary>
/// Computes the digital root of this 8-bit integer.
/// </summary>

View File

@ -9,6 +9,21 @@ namespace X10D.Math;
/// </summary>
public static class ByteExtensions
{
/// <summary>
/// Returns the number of digits in the current 8-bit unsigned integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this byte value)
{
if (value == 0)
{
return 1;
}
return ((ulong)value).CountDigits();
}
/// <summary>
/// Computes the digital root of this 8-bit integer.
/// </summary>

View File

@ -9,6 +9,21 @@ namespace X10D.Math;
/// </summary>
public static class Int16Extensions
{
/// <summary>
/// Returns the number of digits in the current 16-bit signed integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this short value)
{
if (value == 0)
{
return 1;
}
return ((long)value).CountDigits();
}
/// <summary>
/// Computes the digital root of this 16-bit integer.
/// </summary>

View File

@ -9,6 +9,21 @@ namespace X10D.Math;
/// </summary>
public static class Int32Extensions
{
/// <summary>
/// Returns the number of digits in the current 32-bit signed integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this int value)
{
if (value == 0)
{
return 1;
}
return ((long)value).CountDigits();
}
/// <summary>
/// Computes the digital root of this 32-bit integer.
/// </summary>

View File

@ -9,6 +9,21 @@ namespace X10D.Math;
/// </summary>
public static class Int64Extensions
{
/// <summary>
/// Returns the number of digits in the current 64-bit signed integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this long value)
{
if (value == 0)
{
return 1;
}
return 1 + (int)System.Math.Floor(System.Math.Log10(System.Math.Abs(value)));
}
/// <summary>
/// Computes the digital root of this 64-bit integer.
/// </summary>

View File

@ -10,6 +10,21 @@ namespace X10D.Math;
[CLSCompliant(false)]
public static class SByteExtensions
{
/// <summary>
/// Returns the number of digits in the current 8-bit signed integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this sbyte value)
{
if (value == 0)
{
return 1;
}
return ((long)value).CountDigits();
}
/// <summary>
/// Computes the digital root of this 32-bit integer.
/// </summary>

View File

@ -10,6 +10,21 @@ namespace X10D.Math;
[CLSCompliant(false)]
public static class UInt16Extensions
{
/// <summary>
/// Returns the number of digits in the current 16-bit signed integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this ushort value)
{
if (value == 0)
{
return 1;
}
return ((ulong)value).CountDigits();
}
/// <summary>
/// Computes the digital root of the current 16-bit unsigned integer.
/// </summary>

View File

@ -10,6 +10,21 @@ namespace X10D.Math;
[CLSCompliant(false)]
public static class UInt32Extensions
{
/// <summary>
/// Returns the number of digits in the current 32-bit unsigned integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this uint value)
{
if (value == 0)
{
return 1;
}
return ((ulong)value).CountDigits();
}
/// <summary>
/// Computes the digital root of the current 32-bit unsigned integer.
/// </summary>

View File

@ -10,6 +10,21 @@ namespace X10D.Math;
[CLSCompliant(false)]
public static class UInt64Extensions
{
/// <summary>
/// Returns the number of digits in the current 64-bit unsigned integer.
/// </summary>
/// <param name="value">The value whose digit count to compute.</param>
/// <returns>The number of digits in <paramref name="value" />.</returns>
public static int CountDigits(this ulong value)
{
if (value == 0)
{
return 1;
}
return 1 + (int)System.Math.Floor(System.Math.Log10(System.Math.Abs((double)value)));
}
/// <summary>
/// Computes the digital root of the current 64-bit unsigned integer.
/// </summary>