예제 #1
0
 public void Parsing()
 {
     Assert.False(HashCode128.TryParse(null, out HashCode128 hash));
     Assert.False(HashCode128.TryParse("", out hash));
     Assert.False(HashCode128.TryParse("123456789ABCDE", out hash));
     Assert.False(HashCode128.TryParse("Well, this isn't likely to work, is it?", out hash));
     Assert.False(HashCode128.TryParse("123456789abcdef01", out hash));
     Assert.Equal(hash, HashCode128.Zero);
     Assert.Equal(default(HashCode128), hash);
     Assert.True(HashCode128.TryParse("123456789abcdef00fedcba987654321", out hash));
     Assert.Equal(hash, HashCode128.Parse("  123456789ABCD EF0  0fe DCB a98 765 4321   "));
     Assert.Equal(HashCode128.Parse("00000000000000000000000000000000"), HashCode128.Zero);
     Assert.Equal(hash.GetHashCode(), HashCode128.Parse("123456789abcdef00fedcba987654321").GetHashCode());
     Assert.NotEqual(hash.GetHashCode(), HashCode128.Zero.GetHashCode());
     Assert.Equal(0, HashCode128.Zero.GetHashCode());
     Assert.Equal <ulong>(0x123456789abcdef0, hash.UHash1);
     Assert.Equal <ulong>(0x0fedcba987654321, hash.UHash2);
     Assert.Equal(0x123456789abcdef0, hash.Hash1);
     Assert.Equal(0x0fedcba987654321, hash.Hash2);
     Assert.Equal(hash, new HashCode128(0x123456789abcdef0u, 0x0fedcba987654321));
     Assert.Equal(hash, new HashCode128(0x123456789abcdef0, 0x0fedcba987654321));
     Assert.Equal(hash, HashCode128.Parse("0x123456789abcdef00fedcba987654321"));
     Assert.Equal(hash, HashCode128.Parse("0x123456789abcdef00fedcba987654321"));
     Assert.False(HashCode128.TryParse("x123456789abcdef00fedcba987654321", out hash));
     Assert.False(HashCode128.TryParse("0xx123456789abcdef00fedcba987654321", out hash));
     Assert.False(HashCode128.TryParse("1234x6789abcdef00fedcba987654321", out hash));
 }
        public static HashCode128 SafeHashCode128(this IHashValue hashVal, bool strictMode = true)
        {
            if (hashVal is null)
            {
                return(HashCode128.Zero);
            }

            var hex = hashVal.GetHexString();

            return(strictMode
                ? HashCode128.TryParse(hex, out var hash)
                    ? hash
                    : HashCode128.Zero
                : HashCode128.TryParseLoosely(hex, out hash)
                    ? hash
                    : HashCode128.Zero);
        }