Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement INumber onFraction #373

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions specs/Qowaiv.Specs/Hashing/Hash_specs.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Corniel marked this conversation as resolved.
Show resolved Hide resolved
{
[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_finate([RandomFraction(Count)] Fraction fraction)
Corniel marked this conversation as resolved.
Show resolved Hide resolved
=> 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 maxmaginiute_equal_to_decimal([RandomFraction(3)] Fraction x, [RandomFraction(3)] Fraction y)
Corniel marked this conversation as resolved.
Show resolved Hide resolved
{
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