public void Parsing() { Assert.False(HashCode64.TryParse(null, out HashCode64 hash)); Assert.False(HashCode64.TryParse("", out hash)); Assert.False(HashCode64.TryParse("123456789ABCDE", out hash)); Assert.False(HashCode64.TryParse("Well, this isn't likely to work, is it?", out hash)); Assert.False(HashCode64.TryParse("123456789abcdef01", out hash)); Assert.Equal(hash, HashCode64.Zero); Assert.Equal(default(HashCode64), hash); Assert.True(HashCode64.TryParse("1234abcd56789ef0", out hash)); Assert.Equal(hash, HashCode64.Parse(" 1234ABCD56789 EF0 ")); Assert.Equal(HashCode64.Parse("0000000000000000"), HashCode64.Zero); Assert.Equal(hash.GetHashCode(), HashCode64.Parse("1234abcd56789ef0").GetHashCode()); Assert.NotEqual(hash.GetHashCode(), HashCode64.Zero.GetHashCode()); Assert.Equal(0, HashCode64.Zero.GetHashCode()); Assert.Equal <uint>(0x1234abcd, hash.UHash1); Assert.Equal <uint>(0x56789ef0, hash.UHash2); Assert.Equal(0x1234abcd, hash.Hash1); Assert.Equal(0x56789ef0, hash.Hash2); Assert.Equal(hash, new HashCode64(0x1234abcdu, 0x56789ef0)); Assert.Equal(hash, new HashCode64(0x1234abcd, 0x56789ef0)); Assert.Equal(hash, HashCode64.Parse("0x1234abcd56789ef0")); Assert.Equal(hash, HashCode64.Parse("0x1234abcd56789ef0")); Assert.False(HashCode64.TryParse("x1234abcd56789ef0", out hash)); Assert.False(HashCode64.TryParse("0xx1234abcd56789ef0", out hash)); Assert.False(HashCode64.TryParse("1234xabcd6789ef0", out hash)); }
public static HashCode64 SafeHashCode64(this IHashValue hashVal, bool strictMode = true) { if (hashVal is null) { return(HashCode64.Zero); } var hex = hashVal.GetHexString(); return(strictMode ? HashCode64.TryParse(hex, out var hash) ? hash : HashCode64.Zero : HashCode64.TryParseLoosely(hex, out hash) ? hash : HashCode64.Zero); }