public static void LastIndexOf_AllSubstrings(string s, string value, int startIndex, StringComparison comparisonType)
{
bool ignoringCase = comparisonType == StringComparison.OrdinalIgnoreCase || comparisonType == StringComparison.CurrentCultureIgnoreCase;
// First find the substring. We should be able to with all comparison types.
Assert.Equal(startIndex, s.LastIndexOf(value, comparisonType)); // in the whole string
Assert.Equal(startIndex, s.LastIndexOf(value, startIndex + value.Length - 1, comparisonType)); // starting at end of substring
Assert.Equal(startIndex, s.LastIndexOf(value, startIndex + value.Length, comparisonType)); // starting just beyond end of substring
if (startIndex + value.Length < s.Length)
{
Assert.Equal(startIndex, s.LastIndexOf(value, startIndex + value.Length + 1, comparisonType)); // starting a bit more beyond end of substring
}
if (startIndex + value.Length > 1)
{
Assert.Equal(-1, s.LastIndexOf(value, startIndex + value.Length - 2, comparisonType)); // starting before end of substring
}
// Shouldn't be able to find the substring if the count is less than substring's length
Assert.Equal(-1, s.LastIndexOf(value, s.Length - 1, value.Length - 1, comparisonType));
// Now double the source. Make sure we find the second copy of the substring.
int halfLen = s.Length;
s += s;
Assert.Equal(halfLen + startIndex, s.LastIndexOf(value, comparisonType));
// Now change the case of a letter.
s = s.ToUpperInvariant();
Assert.Equal(ignoringCase ? halfLen + startIndex : -1, s.LastIndexOf(value, comparisonType));
}