Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added seed #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Grassfed.MurmurHash3.Tests/MurmurHash3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,72 @@ public void TestComputeHash()
}
}


[Test]
public void TestComputeHashWithSeed()
{
// Arrange
var cases = new[]
{
new TestCase { Value = "Home grown, grass fed, cage free", ExpectedHash = "1b16d284ae75390af71b004016ab2d06" },
new TestCase { Value = "The quick brown fox jumps over the lazy dog.", ExpectedHash = "2ebd2021b2394d9f4bb893b5191eee81" },
new TestCase { Value = "", ExpectedHash = "b6c7ea11479f8c1580d238ecb1eac79c" },
new TestCase { Value = "\0", ExpectedHash = "29068023a3ec28eab1ddd5945f9457c1" },
new TestCase { Value = "\0\0", ExpectedHash = "7cf366b401a16715098268307e1fa337" },
new TestCase { Value = "hello", ExpectedHash = "fcd5490aaaa311b6414794f5e1c6a06f" },
new TestCase { Value = "hello\0", ExpectedHash = "6f71c82e3a2f01392680933d00de7f75" },
new TestCase { Value = "hello, world", ExpectedHash = "8010097803b33be170a66bb4287e8841" },
new TestCase { Value = "19 Jan 2038 at 3:14:07 AM", ExpectedHash = "6ae6dd7c190689652d2927d81c662d73" },
new TestCase { Value = "The quick brown fox jumps over the lazy dog...", Length = 44, ExpectedHash = "2ebd2021b2394d9f4bb893b5191eee81" },
new TestCase { Value = " The quick brown fox jumps over the lazy dog. ", Offset = 2, Length = 44, ExpectedHash = "2ebd2021b2394d9f4bb893b5191eee81" },
new TestCase { Value = "A", ExpectedHash = "2467a979f180ad428274b9612fe1d432" },
new TestCase { Value = "AB", ExpectedHash = "b7f6815e87957fccadb1c0db8512c61f" },
new TestCase { Value = "ABC", ExpectedHash = "f98ce65ca1ce765eba9704e918fcbaf0" },
new TestCase { Value = "ABCD", ExpectedHash = "00600387205556abfa398386850f4ff4" },
new TestCase { Value = "ABCDE", ExpectedHash = "090da08b3bbf58eae4eb81ea33fcf2e9" },
new TestCase { Value = "ABCDEF", ExpectedHash = "1bc25c55dbb5cb140c0840049874ec50" },
new TestCase { Value = "ABCDEFG", ExpectedHash = "4478a087b5e7a4632c4211cf3a46b948" },
new TestCase { Value = "ABCDEFGH", ExpectedHash = "70a04cc64c88cb9f4093eb95be662625" },
new TestCase { Value = "ABCDEFGHI", ExpectedHash = "c3137021621ab3275f9e8f6f1788abce" },
new TestCase { Value = "ABCDEFGHIJ", ExpectedHash = "4fd5f8a86500f201ebc504ac7b415e01" },
new TestCase { Value = "ABCDEFGHIJK", ExpectedHash = "fec9be80a2b4585d1eb9a4a9bcf696af" },
new TestCase { Value = "ABCDEFGHIJKL", ExpectedHash = "38a4b05e095153948f85f4c2722fad5f" },
new TestCase { Value = "ABCDEFGHIJKLM", ExpectedHash = "5d32abedd882cbe0e09bd4f3553d9ae1" },
new TestCase { Value = "ABCDEFGHIJKLMN", ExpectedHash = "76d7e2e14841d5d32dde5a3207f5fafc" },
new TestCase { Value = "ABCDEFGHIJKLMNO", ExpectedHash = "b7da604aa8d10e3c42d38fe487dd8a8c" },
new TestCase { Value = "ABCDEFGHIJKLMNOP", ExpectedHash = "5d491cb53d1a51aec425a384bf5a2a66" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQ", ExpectedHash = "a772014715040cc77851d7b64bae97cc" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQR", ExpectedHash = "1670cad3190a287e8995c40e2e6f5908" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRS", ExpectedHash = "b9344b45932b838e5bf67f3899de8762" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRST", ExpectedHash = "49d005b4ae6c1a5281378795bcdbcefd" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTU", ExpectedHash = "1d0ca18f3162525e0cf210b02bdeab1d" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTUV", ExpectedHash = "fa60d5c54a10f9fce31b968de3d0023d" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTUVW", ExpectedHash = "1b0c49a64cd1864f2409f7aeca06e8f3" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTUVWX", ExpectedHash = "667e0bbdb22dccc65068a41f4934842c" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTUVWXY", ExpectedHash = "56dbd94d78d39075964822c6b590da2f" },
new TestCase { Value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", ExpectedHash = "eee511be11ae9f18c9728b30efe52356" },
};

var h = new MurmurHash3(25);

foreach (var c in cases)
{
var bytes = Encoding.UTF8.GetBytes(c.Value);

// Act
byte[] val;
if (c.Offset == 0 && c.Length == 0)
val = h.ComputeHash(bytes);
else
val = h.ComputeHash(bytes, c.Offset, c.Length);

// Assert
var got = ByteArrayToHexString(val);
if (got != c.ExpectedHash)
Assert.Fail("'{0}' want: {1} got: {2}", c.Value, c.ExpectedHash, got);
}
}

private static string ByteArrayToHexString(byte[] bytes)
{
return string.Concat(Array.ConvertAll(bytes, x => x.ToString("x2")));
Expand Down
14 changes: 12 additions & 2 deletions Grassfed.MurmurHash3/MurmurHash3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ namespace Grassfed.MurmurHash3
/// </summary>
public class MurmurHash3
{
/// <param name="seed">The seed to compute the hash code</param>
public MurmurHash3(ulong seed = 0)
{
Seed = seed;
}

/// <summary>Gets the size, in bits, of the computed hash code.</summary>
/// <returns>The size, in bits, of the computed hash code.</returns>
public int HashSize => 128;

/// <summary>Gets the seed, that will be used to compute hash.</summary>
/// <returns>The seed, that will be used to compute hash.</returns>
public ulong Seed { get; }

/// <summary>Computes the hash value for the specified byte array.</summary>
/// <param name="buffer">The input to compute the hash code for.</param>
/// <returns>The computed hash code.</returns>
Expand Down Expand Up @@ -57,8 +67,8 @@ public unsafe byte[] ComputeHash(byte[] buffer, int offset, int count)

int nblocks = count / 16;

ulong h1 = 0;
ulong h2 = 0;
ulong h1 = Seed;
ulong h2 = Seed;

// body
fixed (byte* pbuffer = buffer)
Expand Down