public static void Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType, int expected)
{
bool hasNullInputs = (strA == null || strB == null);
bool indicesReferToEntireString = (strA != null && strB != null && indexA == 0 && indexB == 0 && (length == strB.Length || length == strA.Length));
bool skipNonComparisonOverloads = length != 0 && ((strA == null && indexA != 0) || (strB == null && indexB != 0));
if (hasNullInputs || indicesReferToEntireString)
{
if (comparisonType == StringComparison.CurrentCulture)
{
// Use Compare(string, string) or Compare(string, string, false) or CompareTo(string)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB)));
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, ignoreCase: false)));
if (strA != null)
{
Assert.Equal(expected, Math.Sign(strA.CompareTo(strB)));
IComparable iComparable = strA;
Assert.Equal(expected, Math.Sign(iComparable.CompareTo(strB)));
}
if (strB != null)
{
Assert.Equal(expected, -Math.Sign(strB.CompareTo(strA)));
IComparable iComparable = strB;
Assert.Equal(expected, -Math.Sign(iComparable.CompareTo(strA)));
}
}
else if (comparisonType == StringComparison.CurrentCultureIgnoreCase)
{
// Use Compare(string, string, true)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, ignoreCase: true)));
}
else if (comparisonType == StringComparison.Ordinal)
{
// Use CompareOrdinal(string, string)
Assert.Equal(expected, Math.Sign(string.CompareOrdinal(strA, strB)));
}
// Use CompareOrdinal(string, string, StringComparison)
Assert.Equal(expected, Math.Sign(string.Compare(strA, strB, comparisonType)));
}
if (comparisonType == StringComparison.CurrentCulture)
{
// This may have different behavior than the overload accepting a StringComparison
// for a combination of null/invalid inputs; see notes in Compare_Invalid for more
if (!skipNonComparisonOverloads)
{
// Use Compare(string, int, string, int, int) or Compare(string, int, string, int, int, false)
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length)));
// Uncomment when this is exposed in .NET Core (dotnet/corefx#10066)
// Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, ignoreCase: false)));
}
}
else if (comparisonType == StringComparison.CurrentCultureIgnoreCase)
{
// This may have different behavior than the overload accepting a StringComparison
// for a combination of null/invalid inputs; see notes in Compare_Invalid for more
if (!skipNonComparisonOverloads)
{
// Use Compare(string, int, string, int, int, true)
// Uncomment when this is exposed in .NET Core (dotnet/corefx#10066)
// Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, ignoreCase: true)));
}
}
else if (comparisonType == StringComparison.Ordinal)
{
// Use CompareOrdinal(string, int, string, int, int)
Assert.Equal(expected, Math.Sign(string.CompareOrdinal(strA, indexA, strB, indexB, length)));
}
// Use Compare(string, int, string, int, int, StringComparison)
Assert.Equal(expected, Math.Sign(string.Compare(strA, indexA, strB, indexB, length, comparisonType)));
}