wabbajack/Wabbajack.Hashing.xxHash64.Test/HashTests.cs

95 lines
2.5 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Data.HashFunction.xxHash;
using FsCheck.Xunit;
using Xunit;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Hashing.xxHash64.Test;
public class HashTests
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
private static readonly Hash Hash1 = new(1);
private static readonly Hash Hash1a = new(1);
private static readonly Hash Hash2 = new(2);
[Property(MaxTest = 1024)]
public void CompareResults(byte[] data)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var hash = new xxHashAlgorithm(0);
Assert.Equal(HashOld(data), hash.HashBytes(data));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Property(MaxTest = 1024 * 1024)]
public void ToFromBase64(ulong hash)
{
var a = new Hash(hash);
var b = Hash.FromBase64(a.ToBase64());
Assert.Equal(a, b);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Property(MaxTest = 1024 * 1024)]
public void ToFromBase64Span(ulong hash)
{
hash = ulong.MaxValue - hash;
Span<byte> data = stackalloc byte[12];
var a = new Hash(hash);
a.ToBase64(data);
var b = Hash.FromBase64(data);
Assert.Equal(a, b);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
private ulong HashOld(byte[] data)
{
var config = new xxHashConfig {HashSizeInBits = 64};
return BitConverter.ToUInt64(xxHashFactory.Instance.Create(config).ComputeHash(data).Hash);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void HashesAreEquatable()
{
Assert.Equal(Hash1, Hash1a);
Assert.True(Hash1 == Hash1a);
Assert.True(Hash1 != Hash2);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void HashesAreSortable()
{
Assert.Equal(0, Hash1.CompareTo(Hash1a));
Assert.Equal(-1, Hash1.CompareTo(Hash2));
Assert.Equal(1, Hash2.CompareTo(Hash1));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void HashesConvertToBase64()
{
Assert.Equal("AQAAAAAAAAA=", Hash1.ToBase64());
Assert.Equal("AQAAAAAAAAA=", Hash1.ToString());
Assert.Equal(Hash1, Hash.Interpret(Hash1.ToBase64()));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void HashesConvertToHex()
{
Assert.Equal("0100000000000000", Hash1.ToHex());
Assert.Equal(Hash1, Hash.Interpret(Hash1.ToHex()));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void HashesConvertToLong()
{
Assert.Equal(Hash1, Hash.Interpret(((long) Hash1).ToString()));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void MiscMethods()
{
Assert.Equal(1, Hash1.GetHashCode());
Assert.Equal(1, (long) Hash1);
Assert.Equal((ulong) 1, (ulong) Hash1);
Assert.True(Hash1.Equals(Hash1a));
Assert.True(Hash1.Equals((object) Hash1a));
Assert.NotEqual(Hash1, (object) 4);
Assert.Equal(Hash1, Hash.FromULong(1));
Assert.Equal(new byte[] {1, 0, 0, 0, 0, 0, 0, 0}, Hash1.ToArray());
2021-09-27 12:42:46 +00:00
}
}