Add tests for xxHash3

This commit is contained in:
Oleksandr Melnyk 2022-06-13 09:30:33 +03:00
parent 2ca0ac682b
commit 7b8c7d15be
2 changed files with 76 additions and 6 deletions

View File

@ -0,0 +1,70 @@
using System;
using System.Text;
using Xunit;
namespace Standart.Hash.xxHash.Test
{
public class xxHash3Test
{
[Fact]
public void Compute_xxhash3_for_bytes()
{
// Arrange
var bytes = new byte[]
{
0xd2, 0x94, 0x29, 0xc9, 0x4c, 0xc5, 0x0f, 0xbb,
0xaa, 0xf4, 0x7c, 0xd5, 0x69, 0x5a, 0xa9, 0xbd,
0xaf, 0xd8, 0x3f, 0xfb, 0xca, 0x6a, 0xd4, 0x2c,
0x6c, 0x69, 0x7a, 0x5b, 0x0d, 0xe8, 0xd2, 0xb1,
0x41, 0xb3, 0x1b, 0x23, 0xdb, 0x8c, 0x25, 0xb4,
0x6c, 0xfb
};
var expected = 6698906707421582347UL;
// Act
var hash = xxHash3.ComputeHash(bytes, bytes.Length);
// Assert
Assert.Equal(expected, hash);
}
[Fact]
public void Compute_xxhash3_for_span()
{
// Arrange
var bytes = new byte[]
{
0xd2, 0x94, 0x29, 0xc9, 0x4c, 0xc5, 0x0f, 0xbb,
0xaa, 0xf4, 0x7c, 0xd5, 0x69, 0x5a, 0xa9, 0xbd,
0xaf, 0xd8, 0x3f, 0xfb, 0xca, 0x6a, 0xd4, 0x2c,
0x6c, 0x69, 0x7a, 0x5b, 0x0d, 0xe8, 0xd2, 0xb1,
0x41, 0xb3, 0x1b, 0x23, 0xdb, 0x8c, 0x25, 0xb4,
0x6c, 0xfb
};
var span = bytes.AsSpan();
var expected = 6698906707421582347UL;
// Act
var hash = xxHash3.ComputeHash(span, span.Length);
// Assert
Assert.Equal(expected, hash);
}
[Fact]
public void Compute_xxhash3_for_string()
{
// Arrange
var str = "veni vidi vici";
var bytes = Encoding.Unicode.GetBytes(str);
// Act
var hash1 = xxHash3.ComputeHash(str);
var hash2 = xxHash3.ComputeHash(bytes, bytes.Length);
// Assert
Assert.Equal(hash1, hash2);
}
}
}

View File

@ -68,17 +68,17 @@ namespace Standart.Hash.xxHash
/// <summary>
/// Compute xxHash for the string
/// </summary>
/// <param name="str">The source of data</param>
/// <param name="unicode">The source of data</param>
/// <param name="seed">The seed number</param>
/// <returns>hash</returns>
public static unsafe ulong ComputeHash(string str, ulong seed = 0)
public static unsafe ulong ComputeHash(string unicode, ulong seed = 0)
{
Debug.Assert(str != null);
Debug.Assert(unicode != null);
fixed (char* c = str)
fixed (char* c = unicode)
{
byte* ptr = (byte*) c;
int length = str.Length * 2;
int length = unicode.Length * 2;
return UnsafeComputeHash(ptr, length, seed);
}