Skip to content

Commit

Permalink
Rewrote some GetHashCode() tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jan 20, 2024
1 parent ad1db13 commit 63a0925
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 52 deletions.
13 changes: 13 additions & 0 deletions specs/Qowaiv.Specs/Financial/Currency_specs.cs
Expand Up @@ -117,3 +117,16 @@ public void throws_for_invalid_json(object json, Type exceptionType)
.Should().Throw<Exception>()
.And.Should().BeOfType(exceptionType);
}

public class Is_equal_by_value
{
[TestCase("", 0)]
[TestCase("EUR", -943833935)]
public void hash_code_is_value_based(Currency svo, int hash)
{
using (Hash.WithoutRandomizer())
{
svo.GetHashCode().Should().Be(hash);
}
}
}
21 changes: 21 additions & 0 deletions specs/Qowaiv.Specs/Formatting/Formatting_arguments_specs.cs
@@ -1,5 +1,26 @@
namespace Formatting_arguments_specs;

public class Is_equal_by_value
{
[Test]
public void hash_code_is_value_based_for_none()
{
using (Hash.WithoutRandomizer())
{
FormattingArguments.None.GetHashCode().Should().Be(0);
}
}

[Test]
public void hash_code_is_value_based_for_value()
{
using (Hash.WithoutRandomizer())
{
new FormattingArguments("f", null).GetHashCode().Should().Be(-362413457);
}
}
}

public class ToString
{
[Test]
Expand Down
14 changes: 14 additions & 0 deletions specs/Qowaiv.Specs/Globalization/Country_specs.cs
Expand Up @@ -210,6 +210,19 @@ public void storing_value_in_SerializationInfo()
}
#endif

public class Is_equal_by_value
{
[TestCase("", 0)]
[TestCase("NL", -1174190069)]
public void hash_code_is_value_based(Country svo, int hash)
{
using (Hash.WithoutRandomizer())
{
svo.GetHashCode().Should().Be(hash);
}
}
}

public class Can_parse
{
[Test]
Expand All @@ -220,3 +233,4 @@ public void prefering_existing_over_former_countries()
public void former_countries_via_ISO_3166_3()
=> Country.Parse("BQAQ").Should().Be(Country.BQAQ);
}

14 changes: 0 additions & 14 deletions specs/Qowaiv.Specs/_Legacy/Financial/CurrencyTest.cs
Expand Up @@ -531,20 +531,6 @@ public void FormatDouble_EUR_E12Dot34()

#region IEquatable tests

/// <summary>GetHash should not fail for Currency.Empty.</summary>
[Test]
public void GetHash_Empty_0()
{
Currency.Empty.GetHashCode().Should().Be(0);
}

/// <summary>GetHash should not fail for the test struct.</summary>
[Test]
public void GetHash_TestStruct_NotZero()
{
Assert.NotZero(TestStruct.GetHashCode());
}

[Test]
public void Equals_EmptyEmpty_IsTrue()
{
Expand Down
14 changes: 0 additions & 14 deletions specs/Qowaiv.Specs/_Legacy/Formatting/FormattingArgumentsTest.cs
Expand Up @@ -80,20 +80,6 @@ public void XmlSerializeDeserialize_Empty_AreEqual()

#region IEquatable tests

/// <summary>GetHash should not fail for FormattableArguments.Empty.</summary>
[Test]
public void GetHash_Empty_0()
{
FormattingArguments.None.GetHashCode().Should().Be(0);
}

/// <summary>GetHash should not fail for the test struct.</summary>
[Test]
public void GetHash_TestStruct_NotZero()
{
Assert.NotZero(TestStruct.GetHashCode());
}

[Test]
public void Equals_EmptyEmpty_IsTrue()
{
Expand Down
14 changes: 0 additions & 14 deletions specs/Qowaiv.Specs/_Legacy/Globalization/CountryTest.cs
Expand Up @@ -449,20 +449,6 @@ public void ToStringF_MZ_AreEqual()

#region IEquatable tests

/// <summary>GetHash should not fail for Country.Empty.</summary>
[Test]
public void GetHash_Empty_Hash()
{
Country.Empty.GetHashCode().Should().Be(0);
}

/// <summary>GetHash should not fail for the test struct.</summary>
[Test]
public void GetHash_TestStruct_NotZero()
{
Assert.NotZero(TestStruct.GetHashCode());
}

[Test]
public void Equals_EmptyEmpty_IsTrue()
{
Expand Down
11 changes: 1 addition & 10 deletions src/Qowaiv/Formatting/FormattingArguments.cs
Expand Up @@ -114,16 +114,7 @@ public bool Equals(FormattingArguments other)

/// <inheritdoc />
[Pure]
public override int GetHashCode()
{
int hash = (Format == null) ? 0 : Format.GetHashCode();

if (FormatProvider != null)
{
hash ^= FormatProvider.GetHashCode();
}
return hash;
}
public override int GetHashCode() => Hash.Code(Format).And(FormatProvider);

/// <summary>Returns true if the left and right operand are not equal, otherwise false.</summary>
/// <param name="left">The left operand.</param>
Expand Down

0 comments on commit 63a0925

Please sign in to comment.