Skip to content

Commit

Permalink
Implement INumber onFraction (#373)
Browse files Browse the repository at this point in the history
Implement INumber for Fraction.
  • Loading branch information
Corniel committed Feb 14, 2024
1 parent 654f681 commit b3d404c
Show file tree
Hide file tree
Showing 13 changed files with 1,190 additions and 838 deletions.
16 changes: 8 additions & 8 deletions specs/Qowaiv.Specs/Financial/Amount_INumber_specs.cs
Expand Up @@ -20,21 +20,21 @@ public void radixis_10()
=> Number.Radix<Amount>().Should().Be(10);

[Test]
public void Additive_identityIs_1()
public void additive_identityIs_1()
=> Number.AdditiveIdentity<Amount>().Should().Be(1.Amount());

[Test]
public void Multiplicative_identityis_1()
public void multiplicative_identityis_1()
=> Number.MultiplicativeIdentity<Amount>().Should().Be(1.Amount());

[Test]
public void Is_canonical_equal_to_decimal([Random(Min, Max, Count)] decimal d)
public void is_canonical_equal_to_decimal([Random(Min, Max, Count)] decimal d)
{
Number.IsCanonical(d.Amount()).Should().Be(Number.IsCanonical(d));
}

[Test]
public void Abs_equal_to_Amount_Abs([Random(Min, Max, Count)] decimal d)
public void abs_equal_to_Amount_Abs([Random(Min, Max, Count)] decimal d)
{
Number.Abs(d.Amount()).Should().Be(d.Amount().Abs());
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public void is_never_imaginary([Random(Min, Max, Count)] decimal d)
=> Number.IsImaginaryNumber(d.Amount()).Should().BeFalse();

[Test]
public void is_always_finate([Random(Min, Max, Count)] decimal d)
public void is_always_finite([Random(Min, Max, Count)] decimal d)
=> Number.IsFinite(d.Amount()).Should().BeTrue();

[Test]
Expand Down Expand Up @@ -128,21 +128,21 @@ public void is_positive_equal_to_decimal([Random(Min, Max, Count)] decimal d)
}

[Test]
public void Is_not_Normal_when_zero()
public void is_not_Normal_when_zero()
=> Number.IsNormal(Amount.Zero).Should().BeFalse();

[Test]
public void Is_never_subnormal([Random(Min, Max, Count)] decimal d)
=> Number.IsSubnormal(d.Amount()).Should().BeFalse();

[Test]
public void Is_normal_when_not_zero([Random(Min, Max, Count)] decimal d)
public void is_normal_when_not_zero([Random(Min, Max, Count)] decimal d)
{
Number.IsNormal(d.Amount()).Should().BeTrue();
}

[Test]
public void maxmaginiute_equal_to_decimal([Random(Min, Max, 3)] decimal x, [Random(Min, Max, 3)] decimal y)
public void max_magnitude_equal_to_decimal([Random(Min, Max, 3)] decimal x, [Random(Min, Max, 3)] decimal y)
{
Number.MaxMagnitude(x.Amount(), y.Amount()).Should().Be(Number.MaxMagnitude(x, y).Amount());
Number.MaxMagnitudeNumber(x.Amount(), y.Amount()).Should().Be(Number.MaxMagnitudeNumber(x, y).Amount());
Expand Down
14 changes: 14 additions & 0 deletions specs/Qowaiv.Specs/Hashing/Hash_specs.cs
Expand Up @@ -69,6 +69,7 @@ public void GetHashCode_is_not_supported()
call.Should().Throw<HashingNotSupported>();
}
}

public class Get_hash_code_with_fixed_randomizer
{
[Test]
Expand Down Expand Up @@ -188,3 +189,16 @@ public void not_equal_to_different_value()
public void equal_to_same_value()
=> Svo.Hash.Equals(Hash.Code("QOWAIV")).Should().BeTrue();
}

public class To_string
{
[Test]
public void displays_the_hash()
{
using (Hash.WithoutRandomizer())
{
var hash = Hash.Code(12);
hash.ToString().Should().Be("12");
}
}
}
144 changes: 144 additions & 0 deletions specs/Qowaiv.Specs/Mathematics/Fraction_INumber_specs.cs
@@ -0,0 +1,144 @@
#if NET8_0_OR_GREATER

using Qowaiv.Specs.TestTools;
using Qowaiv.TestTools.Numerics;

namespace Mathematics.Fraction_specs;

public class Fraction_as_INumber
{
[Test]
public void radixis_10()
=> Number.Radix<Fraction>().Should().Be(10);

[Test]
public void Additive_identityIs_1()
=> Number.AdditiveIdentity<Fraction>().Should().Be(Fraction.One);

[Test]
public void Multiplicative_identityis_1()
=> Number.MultiplicativeIdentity<Fraction>().Should().Be(Fraction.One);

[Test]
public void Is_always_canonical([RandomFraction(Count)] Fraction fraction)
=> Number.IsCanonical(fraction).Should().BeTrue();

[Test]
public void Abs_equal_to_Fraction_Abs([RandomFraction(Count)] Fraction fraction)
=> Number.Abs(fraction).Should().Be(Number.Abs((decimal)fraction).Fraction());

[Test]
public void is_never_a_complexnumber([RandomFraction(Count)] Fraction fraction)
=> Number.IsComplexNumber(fraction).Should().BeFalse();

[TestCase(4, true)]
[TestCase(9, true)]
[TestCase(1, true)]
[TestCase(-1, true)]
[TestCase(-2, true)]
[TestCase("3/5", false)]
[TestCase("-3/4", false)]
public void is_integer(Fraction fraction, bool isEvenInteger)
=> Number.IsInteger(fraction).Should().Be(isEvenInteger);

[TestCase(4, true)]
[TestCase(8, true)]
[TestCase(0, true)]
[TestCase(-2, true)]
[TestCase(-1, false)]
[TestCase("3/5", false)]
[TestCase("-3/4", false)]
public void is_even_integer(Fraction fraction, bool isEvenInteger)
=> Number.IsEvenInteger(fraction).Should().Be(isEvenInteger);

[TestCase(5, true)]
[TestCase(9, true)]
[TestCase(1, true)]
[TestCase(-1, true)]
[TestCase(-2, false)]
[TestCase("3/5", false)]
[TestCase("-3/4", false)]
public void is_odd_integer(Fraction p, bool isEvenInteger)
=> Number.IsOddInteger(p).Should().Be(isEvenInteger);

[Test]
public void is_always_real([RandomFraction(Count)] Fraction fraction)
=> Number.IsRealNumber(fraction).Should().BeTrue();

[Test]
public void is_never_complex([RandomFraction(Count)] Fraction fraction)
=> Number.IsComplexNumber(fraction).Should().BeFalse();

[Test]
public void is_never_imaginary([RandomFraction(Count)] Fraction fraction)
=> Number.IsImaginaryNumber(fraction).Should().BeFalse();

[Test]
public void is_always_finite([RandomFraction(Count)] Fraction fraction)
=> Number.IsFinite(fraction).Should().BeTrue();

[Test]
public void is_never_infinite([RandomFraction(Count)] Fraction fraction)
{
Number.IsInfinity(fraction).Should().BeFalse();
Number.IsNegativeInfinity(fraction).Should().BeFalse();
Number.IsPositiveInfinity(fraction).Should().BeFalse();
}

[Test]
public void is_never_NaN([RandomFraction(Count)] Fraction fraction)
=> Number.IsNaN(fraction).Should().BeFalse();

[Test]
public void is_negative_equal_to_decimal([RandomFraction(Count)] Fraction fraction)
=> Number.IsNegative(fraction).Should().Be(Number.IsNegative((decimal)fraction));

[Test]
public void zero_is_positive_and_not_negative()
{
Number.IsPositive(Fraction.Zero).Should().BeTrue();
Number.IsNegative(Fraction.Zero).Should().BeFalse();

Number.IsPositive(0m).Should().BeTrue();
Number.IsNegative(0m).Should().BeFalse();
}

[Test]
public void is_zero_is_false_for_all_but_zero([RandomFraction(Count)] Fraction fraction)
=> Number.IsZero(fraction).Should().BeFalse();

[Test]
public void is_positive_equal_to_decimal([RandomFraction(Count)] Fraction fraction)
=> Number.IsPositive(fraction).Should().Be(Number.IsPositive((decimal)fraction));

[Test]
public void Is_always_normal([RandomFraction(Count)] Fraction fraction)
=> Number.IsNormal(fraction).Should().BeTrue();

[Test]
public void Is_never_subnormal([RandomFraction(Count)] Fraction fraction)
=> Number.IsSubnormal(fraction).Should().BeFalse();

[Test]
public void max_magnitude_equal_to_decimal([RandomFraction(3)] Fraction x, [RandomFraction(3)] Fraction y)
{
var x_ = (decimal)x.Numerator / x.Denominator;
var y_ = (decimal)y.Numerator / y.Denominator;

Number.MaxMagnitude(x, y).Should().Be(Number.MaxMagnitude(x_, y_).Fraction());
Number.MaxMagnitudeNumber(x, y).Should().Be(Number.MaxMagnitudeNumber(x_, y_).Fraction());
}

[Test]
public void min_maginiute_equal_to_decimal([RandomFraction(3)] Fraction x, [RandomFraction(3)] Fraction y)
{
var x_ = (decimal)x.Numerator / x.Denominator;
var y_ = (decimal)y.Numerator / y.Denominator;

Number.MinMagnitude(x, y).Should().Be(Number.MinMagnitude(x_, y_).Fraction());
Number.MinMagnitudeNumber(x, y).Should().Be(Number.MinMagnitudeNumber(x_, y_).Fraction());
}

private const int Count = 8;
}
#endif

0 comments on commit b3d404c

Please sign in to comment.