diff --git a/TallyUnitTest/Platform/UnicodeHashFunction.cs b/TallyUnitTest/Platform/UnicodeHashFunction.cs new file mode 100644 index 00000000..e8e83ae6 --- /dev/null +++ b/TallyUnitTest/Platform/UnicodeHashFunction.cs @@ -0,0 +1,55 @@ +using System; +using System.Globalization; + +namespace NetTally.Tests.Platform +{ + public static class UnicodeHashFunction + { + /// + /// Hashes the provided string using the given CompareOptions. + /// Doing this allows all custom compare options to be applied in determining the hash value. + /// + /// The string. + /// The CompareInfo object doing the unicode-aware comparison. + /// The options to apply to the comparison. + /// Returns the hash code for the string. + public static int HashFunction(string str, CompareInfo info, CompareOptions options) + { + if (info == null) + throw new ArgumentNullException(nameof(info)); + + if (string.IsNullOrEmpty(str)) + return 0; + + SortKey sortOrder = info.GetSortKey(str, options); + + int hash = GetByteArrayHash(sortOrder.KeyData); + + return hash; + } + + /// + /// Generates a hash code for an array of bytes. + /// + /// The key data. + /// Returns a hash code value. + private static int GetByteArrayHash(byte[] keyData) + { + unchecked + { + const int p = 16777619; + int hash = (int)2166136261; + + for (int i = 0; i < keyData.Length; i++) + hash = (hash ^ keyData[i]) * p; + + hash += hash << 13; + hash ^= hash >> 7; + hash += hash << 3; + hash ^= hash >> 17; + hash += hash << 5; + return hash; + } + } + } +} diff --git a/TallyUnitTest/TallyUnitTest.csproj b/TallyUnitTest/TallyUnitTest.csproj index f805367f..f6aa5c56 100644 --- a/TallyUnitTest/TallyUnitTest.csproj +++ b/TallyUnitTest/TallyUnitTest.csproj @@ -54,6 +54,7 @@ + diff --git a/TallyUnitTest/VoteConstructorTests.cs b/TallyUnitTest/VoteConstructorTests.cs index acf3877f..d7001a7a 100644 --- a/TallyUnitTest/VoteConstructorTests.cs +++ b/TallyUnitTest/VoteConstructorTests.cs @@ -15,7 +15,7 @@ public class VoteConstructorTests [ClassInitialize] public static void ClassInit(TestContext context) { - StringUtility.InitStringComparers(DefaultUnicodeHashFunction.HashFunction); + StringUtility.InitStringComparers(Platform.UnicodeHashFunction.HashFunction); sampleQuest = new Quest(); } diff --git a/TallyUnitTest/VoteCounterTests.cs b/TallyUnitTest/VoteCounterTests.cs index 5eda138f..3c822d88 100644 --- a/TallyUnitTest/VoteCounterTests.cs +++ b/TallyUnitTest/VoteCounterTests.cs @@ -17,7 +17,7 @@ public class VoteCounterTests [ClassInitialize] public static void ClassInit(TestContext context) { - StringUtility.InitStringComparers(DefaultUnicodeHashFunction.HashFunction); + StringUtility.InitStringComparers(Platform.UnicodeHashFunction.HashFunction); sampleQuest = new Quest(); voteCounterRaw = VoteCounterImpl.Instance; }