diff options
author | Stephen Toub <stoub@microsoft.com> | 2016-10-08 10:05:30 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2016-10-08 10:05:30 +0300 |
commit | 1c5729278c76441f88e808f87bc2aaeab5b7fae0 (patch) | |
tree | 6ce00a5505669519899dd9c827e1ac5e031dba5c /src | |
parent | 669d61606d89949f43093cf5986c4991914f215a (diff) |
Use ConsoleKeyInfo.Key in ConsoleKeyInfo.GetHashCode (#12453)
Diffstat (limited to 'src')
-rw-r--r-- | src/System.Console/src/System/ConsoleKeyInfo.cs | 6 | ||||
-rw-r--r-- | src/System.Console/tests/ConsoleKeyInfoTests.cs | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/System.Console/src/System/ConsoleKeyInfo.cs b/src/System.Console/src/System/ConsoleKeyInfo.cs index 76fdb718ff..87bd61fa4f 100644 --- a/src/System.Console/src/System/ConsoleKeyInfo.cs +++ b/src/System.Console/src/System/ConsoleKeyInfo.cs @@ -70,7 +70,11 @@ namespace System public override int GetHashCode() { - return (int)_keyChar | (int)_mods; + // For all normal cases we can fit all bits losslessly into the hash code: + // _keyChar could be any 16-bit value (though is most commonly ASCII). Use all 16 bits without conflict. + // _key is 32-bit, but the ctor throws for anything over 255. Use those 8 bits without conflict. + // _mods only has enum defined values for 1,2,4: 3 bits. Use the remaining 8 bits. + return _keyChar | ((int)_key << 16) | ((int)_mods << 24); } } } diff --git a/src/System.Console/tests/ConsoleKeyInfoTests.cs b/src/System.Console/tests/ConsoleKeyInfoTests.cs index bc2165a636..8cef9f7b8f 100644 --- a/src/System.Console/tests/ConsoleKeyInfoTests.cs +++ b/src/System.Console/tests/ConsoleKeyInfoTests.cs @@ -56,6 +56,14 @@ namespace System.Tests Assert.True(left != right); } + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] // .NET Framework's hashing algorithm doesn't factor in CKI.Key + [Theory] + [MemberData(nameof(NotEqualConsoleKeyInfos))] + public void HashCodeNotEquals_DifferentData(ConsoleKeyInfo left, ConsoleKeyInfo right) + { + Assert.NotEqual(left.GetHashCode(), right.GetHashCode()); + } + [Fact] public void NotEquals_Object() { |