diff --git a/PolyShim.Tests/Net80/EqualityComparerTests.cs b/PolyShim.Tests/Net80/EqualityComparerTests.cs new file mode 100644 index 0000000..c36396b --- /dev/null +++ b/PolyShim.Tests/Net80/EqualityComparerTests.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.Net80; + +public class EqualityComparerTests +{ + [Fact] + public void Create_Test() + { + // Arrange + var comparer = EqualityComparer.Create( + (x, y) => Math.Abs(x) == Math.Abs(y), + x => Math.Abs(x) + ); + + // Act & Assert + comparer.Equals(5, 5).Should().BeTrue(); + comparer.Equals(5, -5).Should().BeTrue(); + comparer.Equals(5, 4).Should().BeFalse(); + comparer.GetHashCode(5).Should().Be(comparer.GetHashCode(-5)); + } + + [Fact] + public void Create_NoGetHashCode_Test() + { + // Arrange + var comparer = EqualityComparer.Create((x, y) => Math.Abs(x) == Math.Abs(y)); + + // Act & Assert + comparer.Equals(5, 5).Should().BeTrue(); + comparer.Equals(5, -5).Should().BeTrue(); + comparer.Equals(5, 4).Should().BeFalse(); + Assert.Throws(() => comparer.GetHashCode(5)); + } +} diff --git a/PolyShim/Net80/EqualityComparer.cs b/PolyShim/Net80/EqualityComparer.cs new file mode 100644 index 0000000..7c986cf --- /dev/null +++ b/PolyShim/Net80/EqualityComparer.cs @@ -0,0 +1,31 @@ +#if (NETCOREAPP && !NET8_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; +using System.Collections.Generic; + +internal static partial class PolyfillExtensions +{ + private class DelegateEqualityComparer(Func equals, Func? getHashCode) + : EqualityComparer + { + public override bool Equals(T? x, T? y) => equals(x, y); + + public override int GetHashCode(T obj) => + getHashCode is not null ? getHashCode(obj) : throw new NotSupportedException(); + } + + extension(EqualityComparer) + { + // https://learn.microsoft.com/dotnet/api/system.collections.generic.equalitycomparer-1.create + public static EqualityComparer Create( + Func equals, + Func? getHashCode = null + ) => new DelegateEqualityComparer(equals, getHashCode); + } +} +#endif diff --git a/PolyShim/Signatures.md b/PolyShim/Signatures.md index 8fa5928..e968ab5 100644 --- a/PolyShim/Signatures.md +++ b/PolyShim/Signatures.md @@ -1,8 +1,8 @@ # Signatures -- **Total:** 228 +- **Total:** 229 - **Types:** 62 -- **Members:** 166 +- **Members:** 167 ___ @@ -43,6 +43,8 @@ ___ - `Environment` - [`int ProcessId`](https://learn.microsoft.com/dotnet/api/system.environment.processid) .NET 5.0 - [`string? ProcessPath`](https://learn.microsoft.com/dotnet/api/system.environment.processpath) .NET 6.0 +- `EqualityComparer` + - [`EqualityComparer Create(Func, Func?)`](https://learn.microsoft.com/dotnet/api/system.collections.generic.equalitycomparer-1.create) .NET 8.0 - `ExcludeFromCodeCoverageAttribute` - [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute) .NET Core 2.0 - `ExtensionAttribute`