Skip to content

Remove Common Extensions HashCodeCombiner #43454

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

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

39 changes: 0 additions & 39 deletions src/libraries/Common/tests/Extensions/HashCodeCombinerTest.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Extensions.Internal;
using System.Numerics.Hashing;

namespace Microsoft.Extensions.FileSystemGlobbing
{
Expand Down Expand Up @@ -66,13 +66,10 @@ public override bool Equals(object obj)
/// Gets a hash for the file pattern match.
/// </summary>
/// <returns>Some number</returns>
public override int GetHashCode()
{
var hashCodeCombiner = HashCodeCombiner.Start();
hashCodeCombiner.Add(Path, StringComparer.OrdinalIgnoreCase);
hashCodeCombiner.Add(Stem, StringComparer.OrdinalIgnoreCase);
public override int GetHashCode() =>
HashHelpers.Combine(GetHashCode(Path), GetHashCode(Stem));

return hashCodeCombiner;
}
private static int GetHashCode(string value) =>
value != null ? StringComparer.OrdinalIgnoreCase.GetHashCode(value) : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Extensions\HashCodeCombiner\HashCodeCombiner.cs"
Link="Common\src\Extensions\HashCodeCombiner\HashCodeCombiner.cs" />
<Compile Include="$(CoreLibSharedDir)\System\Numerics\Hashing\HashHelpers.cs"
Link="System\Numerics\Hashing\HashHelpers.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace Microsoft.Extensions.FileSystemGlobbing.Tests
{
public class FilePatternMatchTests
{
[Fact]
public void TestGetHashCode()
{
FilePatternMatch match1 = new FilePatternMatch("sub/sub2/bar/baz/three.txt", "sub2/bar/baz/three.txt");
FilePatternMatch match2 = new FilePatternMatch("sub/sub2/bar/baz/three.txt", "sub2/bar/baz/three.txt");
FilePatternMatch match3 = new FilePatternMatch("sub/sub2/bar/baz/one.txt", "sub2/bar/baz/three.txt");
FilePatternMatch match4 = new FilePatternMatch("sub/sub2/bar/baz/three.txt", "sub2/bar/baz/one.txt");

Assert.Equal(match1.GetHashCode(), match2.GetHashCode());
Assert.NotEqual(match1.GetHashCode(), match3.GetHashCode());
Assert.NotEqual(match1.GetHashCode(), match4.GetHashCode());

// FilePatternMatch is case insensitive
FilePatternMatch matchCase1 = new FilePatternMatch("Sub/Sub2/bar/baz/three.txt", "sub2/bar/baz/three.txt");
FilePatternMatch matchCase2 = new FilePatternMatch("sub/sub2/bar/baz/three.txt", "Sub2/bar/baz/thrEE.txt");
Assert.Equal(matchCase1.GetHashCode(), matchCase2.GetHashCode());
}

[Fact]
public void TestGetHashCodeWithNull()
{
FilePatternMatch match = new FilePatternMatch(null, null);
Assert.Equal(0, match.GetHashCode());

int hash1 = new FilePatternMatch("non null", null).GetHashCode();
int hash2 = new FilePatternMatch(null, "non null").GetHashCode();
Assert.NotEqual(0, hash1);
Assert.NotEqual(0, hash2);
Assert.NotEqual(hash1, hash2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Extensions\HashCodeCombiner\HashCodeCombiner.cs"
Link="Common\src\Extensions\HashCodeCombiner\HashCodeCombiner.cs" />
<Compile Include="$(CoreLibSharedDir)\System\Numerics\Hashing\HashHelpers.cs"
Link="System\Numerics\Hashing\HashHelpers.cs" />
<Compile Condition="'$(TargetFramework)' == 'netstandard2.0' or
$(TargetFramework.StartsWith('net4'))"
Include="$(CommonPath)System\Text\ValueStringBuilder.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics.Hashing;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Extensions.Internal;

namespace Microsoft.Extensions.Primitives
{
Expand Down Expand Up @@ -740,12 +740,12 @@ public override int GetHashCode()
{
return Unsafe.As<string>(this[0])?.GetHashCode() ?? Count.GetHashCode();
}
var hcc = default(HashCodeCombiner);
int hashCode = 0;
Copy link
Contributor

@maryamariyan maryamariyan Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we confirm the two logics yield the same results? Was going to recommend we move the two removed tests to where tests for HashHelpers live but could not find where src\libraries\System.Private.CoreLib\src\System\Numerics\Hashing\HashHelpers.cs tests are placed.

cc: @tannergooding

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we had any tests for it, it was an internal helper type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we confirm the two logics yield the same results?

GetHashCode doesn't need to return the exact same results across versions. It just needs to return the same results for 2 logically equivalent objects.

for (int i = 0; i < values.Length; i++)
{
hcc.Add(values[i]);
hashCode = HashHelpers.Combine(hashCode, values[i]?.GetHashCode() ?? 0);
}
return hcc.CombinedHash;
return hashCode;
}
else
{
Expand Down