-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding unit tests for
ValueTupleComparer
and `ValueTupleEqualityCom…
…parer` (#645)
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Test; | ||
|
||
public class ValueTupleComparerTest | ||
{ | ||
private class TestComparer : IComparer<string> | ||
{ | ||
public int Compare([AllowNull] string x, [AllowNull] string y) | ||
{ | ||
var xLen = x?.Length ?? 0; | ||
var yLen = y?.Length ?? 0; | ||
return xLen.CompareTo(yLen); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleComparerShouldCreateWithDefaultComparers() | ||
{ | ||
var comparer = ValueTupleComparer.Create<int, int>(null, null); | ||
ValueTuple<int, int> left = new(1, 2); | ||
ValueTuple<int, int> right = new(3, 4); | ||
var result = comparer.Compare(left, right); | ||
Assert.Equal(-1, result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleComparerShouldCheckSecondItemIfFirstIsZero() | ||
{ | ||
var comparer = ValueTupleComparer.Create<int, int>(null, null); | ||
ValueTuple<int, int> left = new(1, 3); | ||
ValueTuple<int, int> right = new(1, 2); | ||
var result = comparer.Compare(left, right); | ||
Assert.Equal(1, result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleComparerShouldAcceptCustomComparers() | ||
{ | ||
TestComparer innerLeftComparer = new(); | ||
TestComparer innerRightComparer = new(); | ||
var comparer = ValueTupleComparer.Create(innerLeftComparer, innerRightComparer); | ||
ValueTuple<string, string> left = new("123", "1"); | ||
ValueTuple<string, string> right = new("123", "123"); | ||
var result = comparer.Compare(left, right); | ||
Assert.Equal(-1, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Test; | ||
|
||
public class ValueTupleEqualityComparerTest | ||
{ | ||
private record TestObject(string Value); | ||
private class TestComparer<T>(Func<T?, T?, bool> comparer) : IEqualityComparer<T> | ||
{ | ||
public bool Equals(T? x, T? y) => comparer(x, y); | ||
public int GetHashCode([DisallowNull] T obj) => obj.GetHashCode(); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithOneTypeArgShouldCreateWhenNoComparerProvided() | ||
{ | ||
var comparer = ValueTupleEqualityComparer.Create<int>(null); | ||
ValueTuple<int> left = new(1); | ||
ValueTuple<int> right = new(1); | ||
var result = comparer.Equals(left, right); | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithOneTypeArgShouldGetHashCode() | ||
{ | ||
var comparer = ValueTupleEqualityComparer.Create<int?>(null); | ||
ValueTuple<int?> first = new(null); | ||
var firstHashCode = comparer.GetHashCode(first); | ||
Assert.Equal(0, firstHashCode); | ||
|
||
ValueTuple<int?> second = new(2); | ||
var secondHashCode = comparer.GetHashCode(second); | ||
Assert.Equal(2.GetHashCode(), secondHashCode); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithOneTypeArgShouldCreateWhenComparerProvided() | ||
{ | ||
var innerComparer = new TestComparer<TestObject>((x, y) => x?.Value == y?.Value); | ||
var comparer = ValueTupleEqualityComparer.Create(innerComparer); | ||
ValueTuple<TestObject> left = new(new("testing")); | ||
ValueTuple<TestObject> right = new(new("testing")); | ||
var result = comparer.Equals(left, right); | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithTwoTypeArgsShouldCreateWhenNoComparerProvided() | ||
{ | ||
var comparer = ValueTupleEqualityComparer.Create<int, int>(null, null); | ||
ValueTuple<int, int> left = new(1, 2); | ||
ValueTuple<int, int> right = new(1, 2); | ||
var result = comparer.Equals(left, right); | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithTwoTypeArgsShouldCreateWhenComparerProvided() | ||
{ | ||
var innerComparerLeft = new TestComparer<TestObject>((x, y) => x?.Value == y?.Value); | ||
var innerComparerRight = new TestComparer<TestObject>((x, y) => x?.Value == y?.Value); | ||
var comparer = ValueTupleEqualityComparer.Create(innerComparerLeft, innerComparerRight); | ||
ValueTuple<TestObject, TestObject> left = new(new("1"), new("2")); | ||
ValueTuple<TestObject, TestObject> right = new(new("1"), new("2")); | ||
var result = comparer.Equals(left, right); | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void ValueTupleEqualityComparerWithTwoTypeArgsShouldGetHashCode() | ||
{ | ||
var comparer = ValueTupleEqualityComparer.Create<int, int>(null, null); | ||
ValueTuple<int, int> first = new(1, 2); | ||
var firstHashCode = comparer.GetHashCode(first); | ||
var expectedHashCode = HashCode.Combine(1.GetHashCode(), 2.GetHashCode()); | ||
Assert.Equal(expectedHashCode, firstHashCode); | ||
} | ||
} |