Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f075d0f

Browse files
jnm2danmoseley
authored andcommitted
Expose String.GetHashCode(StringComparison) with tests (#19443)
* Added String.GetHashCode(StringComparison) overload * Added String.GetHashCode(StringComparison) tests
1 parent 544d12c commit f075d0f

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/System.Runtime/ref/System.Runtime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,7 @@ public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, in
20302030
public static string Format(string format, params object[] args) { throw null; }
20312031
public System.CharEnumerator GetEnumerator() { throw null; }
20322032
public override int GetHashCode() { throw null; }
2033+
public int GetHashCode(System.StringComparison comparisonType) { throw null; }
20332034
public int IndexOf(char value) { throw null; }
20342035
public int IndexOf(char value, int startIndex) { throw null; }
20352036
public int IndexOf(char value, int startIndex, int count) { throw null; }
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
Compat issues with assembly System.Runtime:
2-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.Char, System.Object[])' does not exist in the implementation but it does exist in the contract.
3-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.Char, System.String[])' does not exist in the implementation but it does exist in the contract.
4-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.String, System.Object[])' does not exist in the implementation but it does exist in the contract.
5-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.String, System.String[])' does not exist in the implementation but it does exist in the contract.
6-
Total Issues: 4
1+
Total Issues: 0

src/System.Runtime/src/ApiCompatBaseline.uapaot.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ MembersMustExist : Member 'System.String.EndsWith(System.Char)' does not exist i
44
MembersMustExist : Member 'System.String.Replace(System.String, System.String, System.Boolean, System.Globalization.CultureInfo)' does not exist in the implementation but it does exist in the contract.
55
MembersMustExist : Member 'System.String.Replace(System.String, System.String, System.StringComparison)' does not exist in the implementation but it does exist in the contract.
66
MembersMustExist : Member 'System.String.StartsWith(System.Char)' does not exist in the implementation but it does exist in the contract.
7-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.Char, System.Object[])' does not exist in the implementation but it does exist in the contract.
8-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.Char, System.String[])' does not exist in the implementation but it does exist in the contract.
9-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.String, System.Object[])' does not exist in the implementation but it does exist in the contract.
10-
MembersMustExist : Member 'System.Text.StringBuilder.AppendJoin(System.String, System.String[])' does not exist in the implementation but it does exist in the contract.
11-
Total Issues: 10
7+
MembersMustExist : Member 'System.String.GetHashCode(System.StringComparison)' does not exist in the implementation but it does exist in the contract.
8+
Total Issues: 7

src/System.Runtime/tests/System/StringTests.netcoreapp.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Collections.Generic;
66
using System.Globalization;
7+
using System.Linq;
78
using Xunit;
89

910
namespace System.Tests
@@ -275,5 +276,32 @@ public void Replace_NoSuchStringComparison_ThrowsArgumentException(StringCompari
275276
{
276277
Assert.Throws<ArgumentException>("comparisonType", () => "abc".Replace("abc", "def", comparisonType));
277278
}
279+
280+
281+
private static readonly StringComparison[] StringComparisons = (StringComparison[])Enum.GetValues(typeof(StringComparison));
282+
283+
284+
public static IEnumerable<object[]> GetHashCode_StringComparison_Data => StringComparisons.Select(value => new object[] { value });
285+
286+
[Theory]
287+
[MemberData(nameof(GetHashCode_StringComparison_Data))]
288+
public static void GetHashCode_StringComparison(StringComparison comparisonType)
289+
{
290+
Assert.Equal(StringComparer.FromComparison(comparisonType).GetHashCode("abc"), "abc".GetHashCode(comparisonType));
291+
}
292+
293+
294+
public static IEnumerable<object[]> GetHashCode_NoSuchStringComparison_ThrowsArgumentException_Data => new[]
295+
{
296+
new object[] { StringComparisons.Min() - 1 },
297+
new object[] { StringComparisons.Max() + 1 },
298+
};
299+
300+
[Theory]
301+
[MemberData(nameof(GetHashCode_NoSuchStringComparison_ThrowsArgumentException_Data))]
302+
public static void GetHashCode_NoSuchStringComparison_ThrowsArgumentException(StringComparison comparisonType)
303+
{
304+
Assert.Throws<ArgumentException>("comparisonType", () => "abc".GetHashCode(comparisonType));
305+
}
278306
}
279307
}

0 commit comments

Comments
 (0)