Skip to content

Commit

Permalink
Copy default unicode hash function into the Tests project. Adjust ref…
Browse files Browse the repository at this point in the history
…erences

and usings.
  • Loading branch information
Kinematics committed Jan 11, 2017
1 parent 78f8134 commit e4dc206
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
55 changes: 55 additions & 0 deletions TallyUnitTest/Platform/UnicodeHashFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Globalization;

namespace NetTally.Tests.Platform
{
public static class UnicodeHashFunction
{
/// <summary>
/// Hashes the provided string using the given CompareOptions.
/// Doing this allows all custom compare options to be applied in determining the hash value.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="info">The CompareInfo object doing the unicode-aware comparison.</param>
/// <param name="options">The options to apply to the comparison.</param>
/// <returns>Returns the hash code for the string.</returns>
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;
}

/// <summary>
/// Generates a hash code for an array of bytes.
/// </summary>
/// <param name="keyData">The key data.</param>
/// <returns>Returns a hash code value.</returns>
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;
}
}
}
}
1 change: 1 addition & 0 deletions TallyUnitTest/TallyUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
</Choose>
<ItemGroup>
<Compile Include="ForumAdapterFactoryUnitTest.cs" />
<Compile Include="Platform\UnicodeHashFunction.cs" />
<Compile Include="PostComponentsTests.cs" />
<Compile Include="QuestTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion TallyUnitTest/VoteConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion TallyUnitTest/VoteCounterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e4dc206

Please sign in to comment.