diff options
author | Ahson Khan <ahkha@microsoft.com> | 2018-04-06 09:35:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-06 09:35:09 +0300 |
commit | f5d31619f821e7b4a0bcf7f648fe1dc2e4e2f09f (patch) | |
tree | f17fd844bfff1c74089b2bef7ea7aa8df01eaa91 | |
parent | 3cb6bb3de0d7edf30b93372c35aebbea6406d370 (diff) |
Fix the test gap in StringTests for IndexOf, LastIndexOf when string is empty. (#28876)
-rw-r--r-- | src/System.Runtime/tests/System/StringTests.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/System.Runtime/tests/System/StringTests.cs b/src/System.Runtime/tests/System/StringTests.cs index 023c805fe7..5cea77c216 100644 --- a/src/System.Runtime/tests/System/StringTests.cs +++ b/src/System.Runtime/tests/System/StringTests.cs @@ -1355,6 +1355,7 @@ namespace System.Tests [InlineData("!@#$", '#', 0, 4, 2)] [InlineData("!@#$", '$', 0, 4, 3)] [InlineData("!@#$%^&*", '%', 0, 8, 4)] + [InlineData("", 'H', 0, 0, -1)] public static void IndexOf_SingleLetter(string s, char target, int startIndex, int count, int expected) { bool safeForCurrentCulture = @@ -1420,6 +1421,34 @@ namespace System.Tests } } + [Fact] + public static void IndexOf_Match_SingleLetter() + { + Assert.Equal(-1, "".IndexOf('a')); + Assert.Equal(-1, "".AsSpan().IndexOf('a')); + + for (int length = 1; length < 32; length++) + { + char[] a = new char[length]; + for (int i = 0; i < length; i++) + { + a[i] = (char)(i + 1); + } + string str = new string(a); + ReadOnlySpan<char> span = new ReadOnlySpan<char>(a); + + for (int targetIndex = 0; targetIndex < length; targetIndex++) + { + char target = a[targetIndex]; + int idx = str.IndexOf(target); + Assert.Equal(targetIndex, idx); + + idx = span.IndexOf(target); + Assert.Equal(targetIndex, idx); + } + } + } + private static bool IsSafeForCurrentCultureComparisons(string str) { for (int i = 0; i < str.Length; i++) @@ -2053,6 +2082,7 @@ namespace System.Tests [InlineData("Hello", 'l', 0, 1, -1)] [InlineData("Hello", 'x', 3, 4, -1)] [InlineData("H" + SoftHyphen + "ello", 'H', 2, 3, 0)] + [InlineData("", 'H', 0, 0, -1)] public static void LastIndexOf_SingleLetter(string s, char value, int startIndex, int count, int expected) { if (count == s.Length) @@ -2073,6 +2103,34 @@ namespace System.Tests Assert.Equal(expected, s.LastIndexOf(value.ToString(), startIndex, count, StringComparison.OrdinalIgnoreCase)); } + [Fact] + public static void LastIndexOf_Match_SingleLetter() + { + Assert.Equal(-1, "".LastIndexOf('a')); + Assert.Equal(-1, "".AsSpan().LastIndexOf('a')); + + for (int length = 1; length < 32; length++) + { + char[] a = new char[length]; + for (int i = 0; i < length; i++) + { + a[i] = (char)(i + 1); + } + string str = new string(a); + ReadOnlySpan<char> span = new ReadOnlySpan<char>(a); + + for (int targetIndex = 0; targetIndex < length; targetIndex++) + { + char target = a[targetIndex]; + int idx = str.LastIndexOf(target); + Assert.Equal(targetIndex, idx); + + idx = span.LastIndexOf(target); + Assert.Equal(targetIndex, idx); + } + } + } + [Theory] [ActiveIssue("https://github.com/dotnet/coreclr/issues/2051", TestPlatforms.AnyUnix)] [InlineData("He\0lo", "He\0lo", 0)] |