Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions PolyShim.Tests/Net80/EqualityComparerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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<int>.Create(
(x, y) => Math.Abs(x) == Math.Abs(y),
x => Math.Abs(x).GetHashCode()
);

// 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<int>.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<NotSupportedException>(() =>
comparer.GetHashCode(5).Should().Be(comparer.GetHashCode(-5))
);
}
}
31 changes: 31 additions & 0 deletions PolyShim/Net80/EqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -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<T>(Func<T?, T?, bool> equals, Func<T, int>? getHashCode)
: EqualityComparer<T>
{
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<T>(EqualityComparer<T>)
{
// https://learn.microsoft.com/dotnet/api/system.collections.generic.equalitycomparer-1.create
public static EqualityComparer<T> Create(
Func<T?, T?, bool> equals,
Func<T, int>? getHashCode = null
) => new DelegateEqualityComparer<T>(equals, getHashCode);
}
}
#endif
6 changes: 4 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 228
- **Total:** 229
- **Types:** 62
- **Members:** 166
- **Members:** 167

___

Expand Down Expand Up @@ -43,6 +43,8 @@ ___
- `Environment`
- [`int ProcessId`](https://learn.microsoft.com/dotnet/api/system.environment.processid) <sup><sub>.NET 5.0</sub></sup>
- [`string? ProcessPath`](https://learn.microsoft.com/dotnet/api/system.environment.processpath) <sup><sub>.NET 6.0</sub></sup>
- `EqualityComparer<T>`
- [`EqualityComparer<T> Create(Func<T?, T?, bool>, Func<T, int>?)`](https://learn.microsoft.com/dotnet/api/system.collections.generic.equalitycomparer-1.create) <sup><sub>.NET 8.0</sub></sup>
- `ExcludeFromCodeCoverageAttribute`
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute) <sup><sub>.NET Core 2.0</sub></sup>
- `ExtensionAttribute`
Expand Down
Loading