Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add tests for IStructuralEquatable/Comparable throwing NullReferenceException #13436

Merged
merged 1 commit into from
Nov 9, 2016
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
58 changes: 57 additions & 1 deletion src/System.Collections.Immutable/tests/ImmutableArrayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ public void IndexGetter()
Assert.Throws<InvalidOperationException>(() => ((IList<int>)s_emptyDefault)[0]);
Assert.Throws<InvalidOperationException>(() => ((IReadOnlyList<int>)s_emptyDefault)[0]);
}

[Fact]
public void Sort()
{
Expand Down Expand Up @@ -1256,6 +1256,28 @@ public void StructuralEquatableEqualsArrayInterop()
Assert.False(immArray.Equals(unequalArray, EqualityComparer<int>.Default));
}

[Fact]
public void IStructuralEquatable_Equals_NullComparerNonNullUnderlyingArray_ThrowsNullReferenceException()
{
// This was not fixed for compatability reasons. See #13410
IStructuralEquatable equatable = ImmutableArray.Create(1, 2, 3);

Assert.True(equatable.Equals(equatable, null));
Assert.Throws<NullReferenceException>(() => equatable.Equals(ImmutableArray.Create(1, 2, 3), null));
Assert.False(equatable.Equals(new ImmutableArray<int>(), null));
Assert.False(equatable.Equals(null, null));
}

[Fact]
public void IStructuralEquatable_Equals_NullComparerNullUnderlyingArray_Works()
{
IStructuralEquatable equatable = new ImmutableArray<int>();
Assert.True(equatable.Equals(equatable, null));
Assert.False(equatable.Equals(ImmutableArray.Create(1, 2, 3), null));
Assert.True(equatable.Equals(new ImmutableArray<int>(), null));
Assert.Throws<NullReferenceException>(() => equatable.Equals(null, null));
}

[Fact]
public void StructuralEquatableGetHashCodeDefault()
{
Expand All @@ -1276,6 +1298,20 @@ public void StructuralEquatableGetHashCode()
Assert.Equal(array.GetHashCode(EverythingEqual<int>.Default), immArray.GetHashCode(EverythingEqual<int>.Default));
}

[Fact]
public void IStructuralEquatable_GetHashCode_NullComparerNonNullUnderlyingArray_ThrowsArgumentNullException()
{
IStructuralEquatable equatable = ImmutableArray.Create(1, 2, 3);
Assert.Throws<ArgumentNullException>(() => equatable.GetHashCode(null));
}

[Fact]
public void IStructuralEquatable_GetHashCode_NullComparerNullUnderlyingArray_Works()
{
IStructuralEquatable equatable = new ImmutableArray<int>();
Assert.Equal(0, equatable.GetHashCode(null));
}

[Fact]
public void StructuralComparableDefault()
{
Expand Down Expand Up @@ -1323,6 +1359,26 @@ public void StructuralComparableArrayInterop()
Assert.Equal(array.CompareTo(equalArray, Comparer<int>.Default), immArray.CompareTo(equalArray, Comparer<int>.Default));
}

[Fact]
public void IStructuralComparable_NullComparerNonNullUnderlyingArray_ThrowsNullReferenceException()
{
// This was not fixed for compatability reasons. See #13410
IStructuralComparable comparable = ImmutableArray.Create(1, 2, 3);
Assert.Throws<NullReferenceException>(() => comparable.CompareTo(comparable, null));
Assert.Throws<NullReferenceException>(() => comparable.CompareTo(ImmutableArray.Create(1, 2, 3), null));
Assert.Throws<ArgumentException>("other", () => comparable.CompareTo(new ImmutableArray<int>(), null));
}

[Fact]
public void IStructuralComparable_NullComparerNullUnderlyingArray_Works()
{
IStructuralComparable comparable = new ImmutableArray<int>();
Assert.Equal(0, comparable.CompareTo(comparable, null));
Assert.Throws<ArgumentException>("other", () => comparable.CompareTo(null, null));
Assert.Throws<ArgumentException>("other", () => comparable.CompareTo(ImmutableArray.Create(1, 2, 3), null));
Assert.Equal(0, comparable.CompareTo(new ImmutableArray<int>(), null));
}

[Theory]
[InlineData(new int[0], 5)]
[InlineData(new int[] { 3 }, 5)]
Expand Down
21 changes: 17 additions & 4 deletions src/System.Runtime/tests/System/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,8 +2455,14 @@ public static void IStructuralComparable_Invalid()
Assert.Throws<ArgumentException>("other", () => comparable.CompareTo(new int[] { 1, 2, 3, 4 }, new IntegerComparer())); // Arrays have different lengths

Assert.Throws<ArgumentException>("other", () => comparable.CompareTo(123, new IntegerComparer())); // Other is not an array
}

Assert.Throws<NullReferenceException>(() => comparable.CompareTo(new int[] { 1, 2, 3 }, null)); // Comparer is null
[Fact]
public static void IStructuralComparable_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410
IStructuralComparable comparable = new int[] { 1, 2, 3 };
Assert.Throws<NullReferenceException>(() => comparable.CompareTo(new int[] { 1, 2, 3 }, null));
}

public static IEnumerable<object[]> IStructuralEquatable_TestData()
Expand Down Expand Up @@ -2495,11 +2501,18 @@ public static void IStructuralEquatable(Array array, object other, IEqualityComp
}

[Fact]
public static void IStructuralEquatable_Invalid()
public static void IStructuralEquatable_Equals_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410
IStructuralEquatable equatable = new int[] { 1, 2, 3 };
Assert.Throws<NullReferenceException>(() => equatable.Equals(new int[] { 1, 2, 3 }, null));
}

[Fact]
public static void IStructuralEquatable_GetHashCode_NullComparer_ThrowsArgumentNullException()
{
IStructuralEquatable equatable = new int[] { 1, 2, 3 };
Assert.Throws<NullReferenceException>(() => equatable.Equals(new int[] { 1, 2, 3 }, null)); // Comparer is null
Assert.Throws<ArgumentNullException>("comparer", () => equatable.GetHashCode(null)); // Comparer is null
Assert.Throws<ArgumentNullException>("comparer", () => equatable.GetHashCode(null));
}

[Theory]
Expand Down
26 changes: 26 additions & 0 deletions src/System.Runtime/tests/System/TupleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,39 @@ public void Equals_GetHashCode(TupleTestDriver<T1, T2, T3, T4, T5, T6, T7, T8, T

Assert.False(Tuple.Equals(null));
Assert.False(((IStructuralEquatable)Tuple).Equals(null));

IStructuralEquatable_Equals_NullComparer_ThrowsNullReferenceException();
IStructuralEquatable_GetHashCode_NullComparer_ThrowsNullReferenceException();
}

public void IStructuralEquatable_Equals_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410
IStructuralEquatable equatable = (IStructuralEquatable)Tuple;
Assert.Throws<NullReferenceException>(() => equatable.Equals(Tuple, null));
}

public void IStructuralEquatable_GetHashCode_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410
IStructuralEquatable equatable = (IStructuralEquatable)Tuple;
Assert.Throws<NullReferenceException>(() => equatable.GetHashCode(null));
}

public void CompareTo(TupleTestDriver<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> other, int expectedResult, int expectedStructuralResult)
{
Assert.Equal(expectedResult, ((IComparable)Tuple).CompareTo(other.Tuple));
Assert.Equal(expectedStructuralResult, ((IStructuralComparable)Tuple).CompareTo(other.Tuple, TestComparer.Instance));
Assert.Equal(1, ((IComparable)Tuple).CompareTo(null));

IStructuralComparable_NullComparer_ThrowsNullReferenceException();
}

public void IStructuralComparable_NullComparer_ThrowsNullReferenceException()
Copy link
Member

@jcouv jcouv Nov 8, 2016

Choose a reason for hiding this comment

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

Consider adding a [Fact] on this so it runs as its own test (no need to invoke it from method above). Same suggestion for similar methods.

Copy link
Author

Choose a reason for hiding this comment

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

Currently this is not possible, as no classes inherit from TupleTestDriver, so the [Fact] test will never run.

If you see the meat of the Tuple tests below, we have to call each test manually

Copy link
Author

Choose a reason for hiding this comment

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

Obvi, this should be cleaned up/fixed up, but for another PR

{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410
IStructuralComparable comparable = (IStructuralComparable)Tuple;
Assert.Throws<NullReferenceException>(() => comparable.CompareTo(Tuple, null));
}

public void NotEqual()
Expand Down
46 changes: 46 additions & 0 deletions src/System.ValueTuple/tests/ValueTupleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,59 @@ public void TestEquals_GetHashCode(ValueTupleTestDriver<T1, T2, T3, T4, T5, T6,

Assert.False(valueTuple.Equals(null));
Assert.False(((IStructuralEquatable)valueTuple).Equals(null));
IStructuralEquatable_Equals_NullComparer_ThrowsNullReferenceException();
IStructuralEquatable_GetHashCode_NullComparer_ThrowsNullReferenceException();
}

public void IStructuralEquatable_Equals_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13429
IStructuralEquatable equatable = (IStructuralEquatable)valueTuple;
if (valueTuple is ValueTuple)
{
Assert.True(equatable.Equals(valueTuple, null));
}
else
{
Assert.Throws<NullReferenceException>(() => equatable.Equals(valueTuple, null));
}
}

public void IStructuralEquatable_GetHashCode_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13429
IStructuralEquatable equatable = (IStructuralEquatable)valueTuple;
if (valueTuple is ValueTuple)
{
Assert.Equal(0, valueTuple.GetHashCode());
}
else
{
Assert.Throws<NullReferenceException>(() => equatable.GetHashCode(null));
}
}

public void TestCompareTo(ValueTupleTestDriver<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> other, int expectedResult, int expectedStructuralResult)
{
Assert.Equal(expectedResult, ((IComparable)valueTuple).CompareTo(other.valueTuple));
Assert.Equal(expectedStructuralResult, ((IStructuralComparable)valueTuple).CompareTo(other.valueTuple, DummyTestComparer.Instance));
Assert.Equal(1, ((IComparable)valueTuple).CompareTo(null));

IStructuralComparable_NullComparer_ThrowsNullReferenceException();
}

public void IStructuralComparable_NullComparer_ThrowsNullReferenceException()
{
// This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13429
IStructuralComparable comparable = (IStructuralComparable)valueTuple;
if (valueTuple is ValueTuple)
{
Assert.Equal(0, comparable.CompareTo(valueTuple, null));
}
else
{
Assert.Throws<NullReferenceException>(() => comparable.CompareTo(valueTuple, null));
}
}

public void TestNotEqual()
Expand Down