Skip to content

2D test failing due to change in GetHashCode in Core 3 #848

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

Closed
grubioe opened this issue Jun 4, 2019 · 6 comments
Closed

2D test failing due to change in GetHashCode in Core 3 #848

grubioe opened this issue Jun 4, 2019 · 6 comments
Assignees
Labels
rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release
Milestone

Comments

@grubioe
Copy link
Contributor

grubioe commented Jun 4, 2019

Copied from VSO ID: 803447

Test:

Name="Color API Test" Priority="1" Area="2D" SubArea="API Tests"

Fails due to a change in GetHashCode from corefx. What the test code is doing is generating 2 different colors and verifying that the resulting hash codes are not equal, the hashcode is calculated from System.Windows.Media.Color by overriding GetHashCode:

///<summary>
/// GetHashCode
///</summary>
public override int GetHashCode()
{
return this.scRgbColor.GetHashCode(); //^this.context.GetHashCode();
}

which essentially only returns the hashcode from its internal struct which is defined as:

private struct MILColorF // this structure is the "milrendertypes.h" structure and should be identical for performance
{
public float a, r, g, b;
};

in Framework different values for a,r,g,b would result in a different HashCode in Core it seems only the first value is considered for HashCode calculation, our test has the same first value so the hash code is the same.

As per corefx recommendation (https://github.com/dotnet/corefx/issues/35613) we should override and implement a correct HashCode mechanism for our 4 floats in MILColorF, ideally mirroring what Framework used to do

@weltkante
Copy link

Was it verified in your original ticket source that this isn't actually a bug in .NET Core? Otherweise I think it'd be worth opening an issue with coreclr to verify that this behavior is intended.

I don't mind proper GetHashCode implementations in WPF but if thats an unintended bug the poor hashcode quality will likely impact users who port their applications as well.

@weltkante
Copy link

weltkante commented Jun 6, 2019

@grubioe @ojhad actually I cannot reproduce this, I took above definition of MILColor and created instances which differ only in a single component. Regardless which component differs, the hash code always differs too for me.

Debug.WriteLine($"Hash = {new MILColor { a = 0xFF, r = 0xAA, g = 0xBB, b = 0xCC }.GetHashCode()}");
Debug.WriteLine($"Hash = {new MILColor { a = 0x00, r = 0xAA, g = 0xBB, b = 0xCC }.GetHashCode()}");
Debug.WriteLine($"Hash = {new MILColor { a = 0xFF, r = 0x00, g = 0xBB, b = 0xCC }.GetHashCode()}");
Debug.WriteLine($"Hash = {new MILColor { a = 0xFF, r = 0xAA, g = 0x00, b = 0xCC }.GetHashCode()}");
Debug.WriteLine($"Hash = {new MILColor { a = 0xFF, r = 0xAA, g = 0xBB, b = 0x00 }.GetHashCode()}");

// output
//Hash = -669667421
//Hash = -669667492
//Hash = -669710941
//Hash = -659640413
//Hash = 336965539

@grubioe
Copy link
Contributor Author

grubioe commented Jun 6, 2019

Thanks for checking @weltkante

@miguep - is this still valid? It was logged back in February and seems that it no longer repros.

@grubioe grubioe added this to the 3.0 milestone Jun 6, 2019
@miguep
Copy link
Contributor

miguep commented Jun 11, 2019

the issue can still be reproduced like this:

            System.Windows.Media.Color ColorA = System.Windows.Media.Color.FromScRgb(1.0f, 0.3f, 0.4f, 1.0f);
            System.Windows.Media.Color ColorB = System.Windows.Media.Color.FromScRgb(0.99999f, 0.3f, 0.4f, 1.0f);
            System.Windows.Media.Color ColorC = System.Windows.Media.Color.FromScRgb(1f, 0.2999f, 0.4f, 1.0f);


            int hashA = ColorA.GetHashCode();
            int hashB = ColorB.GetHashCode();
            int hashC = ColorC.GetHashCode();

In .NET Framework all three hashcodes are different, but in .NET Core, A and C are the same.

an issue against corefx was opened https://github.com/dotnet/corefx/issues/35613, and closed as a by-design behavior, with the recommendation that we implement GetHashCode to our requirements, we'll use this issue to track that work, since we would like Color.GetHashCode to behave as it did in .NET Framework

@weltkante
Copy link

I see, GetHashCode actually doesn't use MILColor from the original issue description, but MILColorF, so thats the reason why I couldn't reproduce it, as it only happens with float/double members. Thanks for verifying that its intentional.

@miguep
Copy link
Contributor

miguep commented Jun 11, 2019

@weltkante good observation, the description was actually misleading, I've updated it for clarity, thank you

@vatsan-madhavan vatsan-madhavan added the regression status: This issue is a regression from a previous build or release label Jul 2, 2019
@grubioe grubioe added the rank20 Rank: Priority/rank on a scale of (1..100) label Jul 16, 2019
@ojhad ojhad closed this as completed Jul 23, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Apr 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release
Projects
None yet
Development

No branches or pull requests

5 participants