Skip to content

Commit 7b8c7d1

Browse files
committed
Add tests for xxHash3
1 parent 2ca0ac6 commit 7b8c7d1

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Text;
3+
using Xunit;
4+
5+
namespace Standart.Hash.xxHash.Test
6+
{
7+
public class xxHash3Test
8+
{
9+
[Fact]
10+
public void Compute_xxhash3_for_bytes()
11+
{
12+
// Arrange
13+
var bytes = new byte[]
14+
{
15+
0xd2, 0x94, 0x29, 0xc9, 0x4c, 0xc5, 0x0f, 0xbb,
16+
0xaa, 0xf4, 0x7c, 0xd5, 0x69, 0x5a, 0xa9, 0xbd,
17+
0xaf, 0xd8, 0x3f, 0xfb, 0xca, 0x6a, 0xd4, 0x2c,
18+
0x6c, 0x69, 0x7a, 0x5b, 0x0d, 0xe8, 0xd2, 0xb1,
19+
0x41, 0xb3, 0x1b, 0x23, 0xdb, 0x8c, 0x25, 0xb4,
20+
0x6c, 0xfb
21+
};
22+
var expected = 6698906707421582347UL;
23+
24+
// Act
25+
var hash = xxHash3.ComputeHash(bytes, bytes.Length);
26+
27+
// Assert
28+
Assert.Equal(expected, hash);
29+
}
30+
31+
[Fact]
32+
public void Compute_xxhash3_for_span()
33+
{
34+
// Arrange
35+
var bytes = new byte[]
36+
{
37+
0xd2, 0x94, 0x29, 0xc9, 0x4c, 0xc5, 0x0f, 0xbb,
38+
0xaa, 0xf4, 0x7c, 0xd5, 0x69, 0x5a, 0xa9, 0xbd,
39+
0xaf, 0xd8, 0x3f, 0xfb, 0xca, 0x6a, 0xd4, 0x2c,
40+
0x6c, 0x69, 0x7a, 0x5b, 0x0d, 0xe8, 0xd2, 0xb1,
41+
0x41, 0xb3, 0x1b, 0x23, 0xdb, 0x8c, 0x25, 0xb4,
42+
0x6c, 0xfb
43+
};
44+
var span = bytes.AsSpan();
45+
var expected = 6698906707421582347UL;
46+
47+
// Act
48+
var hash = xxHash3.ComputeHash(span, span.Length);
49+
50+
// Assert
51+
Assert.Equal(expected, hash);
52+
}
53+
54+
55+
[Fact]
56+
public void Compute_xxhash3_for_string()
57+
{
58+
// Arrange
59+
var str = "veni vidi vici";
60+
var bytes = Encoding.Unicode.GetBytes(str);
61+
62+
// Act
63+
var hash1 = xxHash3.ComputeHash(str);
64+
var hash2 = xxHash3.ComputeHash(bytes, bytes.Length);
65+
66+
// Assert
67+
Assert.Equal(hash1, hash2);
68+
}
69+
}
70+
}

src/Standart.Hash.xxHash/xxHash3.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ public static unsafe ulong ComputeHash(ReadOnlySpan<byte> data, int length, ulon
6464
return UnsafeComputeHash(ptr, length, seed);
6565
}
6666
}
67-
67+
6868
/// <summary>
6969
/// Compute xxHash for the string
7070
/// </summary>
71-
/// <param name="str">The source of data</param>
71+
/// <param name="unicode">The source of data</param>
7272
/// <param name="seed">The seed number</param>
7373
/// <returns>hash</returns>
74-
public static unsafe ulong ComputeHash(string str, ulong seed = 0)
74+
public static unsafe ulong ComputeHash(string unicode, ulong seed = 0)
7575
{
76-
Debug.Assert(str != null);
76+
Debug.Assert(unicode != null);
7777

78-
fixed (char* c = str)
78+
fixed (char* c = unicode)
7979
{
8080
byte* ptr = (byte*) c;
81-
int length = str.Length * 2;
81+
int length = unicode.Length * 2;
8282

8383
return UnsafeComputeHash(ptr, length, seed);
8484
}

0 commit comments

Comments
 (0)