Beispiel #1
0
 public void Parsing()
 {
     Assert.False(HashCode32.TryParse(null, out HashCode32 hash));
     Assert.False(HashCode32.TryParse("", out hash));
     Assert.False(HashCode32.TryParse("123456789ABCDE", out hash));
     Assert.False(HashCode32.TryParse("Well, this isn't likely to work, is it?", out hash));
     Assert.False(HashCode32.TryParse("123456789abcdef01", out hash));
     Assert.Equal(hash, HashCode32.Zero);
     Assert.Equal(default(HashCode32), hash);
     Assert.True(HashCode32.TryParse("12ab34cd", out hash));
     Assert.Equal(hash, HashCode32.Parse("  12AB 3 4 CD  "));
     Assert.Equal(HashCode32.Parse("00000000"), HashCode32.Zero);
     Assert.Equal(hash.GetHashCode(), HashCode32.Parse("12ab34cd").GetHashCode());
     Assert.NotEqual(hash.GetHashCode(), HashCode32.Zero.GetHashCode());
     Assert.Equal(0, HashCode32.Zero.GetHashCode());
     Assert.Equal <ushort>(0x12ab, hash.UHash1);
     Assert.Equal <ushort>(0x34cd, hash.UHash2);
     Assert.Equal(0x12ab, hash.Hash1);
     Assert.Equal(0x34cd, hash.Hash2);
     Assert.Equal(hash, new HashCode32((ushort)0x12ab, 0x34cd));
     Assert.Equal(hash, new HashCode32(0x12ab, 0x34cd));
     Assert.Equal(hash, HashCode32.Parse("0x12ab34cd"));
     Assert.Equal(hash, HashCode32.Parse("0x12ab34cd"));
     Assert.False(HashCode32.TryParse("x12ab34cd", out hash));
     Assert.False(HashCode32.TryParse("0xx12ab34cd", out hash));
     Assert.False(HashCode32.TryParse("12xb34cd", out hash));
 }
Beispiel #2
0
        public void OutputTest()
        {
            var hash = HashCode32.Parse("12ab34cd");

            hash.GetHexString(false).ShouldBe("12ab34cd");
            hash.GetHexString(true).ShouldBe("12AB34CD");

            hash.GetBinString(false).ShouldBe("10010101010110011010011001101");
            hash.GetBinString(true).ShouldBe("00010010101010110011010011001101");
        }
        public static HashCode32 ToHashCode32(this IHashValue hashVal, bool strictMode = true)
        {
            if (hashVal is null)
            {
                return(HashCode32.Zero);
            }
            var hex = hashVal.GetHexString();

            return(strictMode ? HashCode32.Parse(hex) : HashCode32.ParseLoosely(hex));
        }
Beispiel #4
0
#pragma warning disable 1718 //Yes, I'm testing the obvious!
        public void EqualsOps()
        {
            Assert.True(HashCode32.Zero == HashCode32.Zero);
            Assert.True(
                HashCode32.Parse("12ab34cd")
                == HashCode32.Parse("12AB34CD"));
            Assert.False(HashCode32.Zero != HashCode32.Zero);
            Assert.False(
                HashCode32.Parse("12ab34cd")
                != HashCode32.Parse("12AB34CD"));
            Assert.True(HashCode32.Parse("12ab34cd") != HashCode32.Zero);
            Assert.False(HashCode32.Parse("12ab34cd") == HashCode32.Zero);
        }
Beispiel #5
0
        public void EqualsObj()
        {
            Assert.Equal(HashCode32.Zero, (object)HashCode32.Zero);
            object boxed = HashCode32.Parse("01ab34cd");

            Assert.True(boxed.Equals(HashCode32.Parse("01AB34CD")));
            Assert.False(boxed.Equals(HashCode32.Zero));
            Assert.False(boxed.Equals("not a hash code"));
            Assert.True(
                Equals(
                    HashCode32.Parse("1 2 ab 3  4cd"),
                    HashCode32.Parse("12A  B 34C D      ")));
        }
        public static HashCode32 SafeHashCode32(this IHashValue hashVal, bool strictMode = true)
        {
            if (hashVal is null)
            {
                return(HashCode32.Zero);
            }

            var hex = hashVal.GetHexString();

            return(strictMode
                ? HashCode32.TryParse(hex, out var hash)
                    ? hash
                    : HashCode32.Zero
                : HashCode32.TryParseLoosely(hex, out hash)
                    ? hash
                    : HashCode32.Zero);
        }
Beispiel #7
0
 public void BadFormatTooShortLow()
 {
     Assert.Throws <FormatException>(
         () => HashCode32.Parse("0123456789abcdef01234                                        "));
 }
Beispiel #8
0
 public void BadFormatTooShortHigh()
 {
     Assert.Throws <FormatException>(() => HashCode32.Parse(new string(' ', 32)));
 }
Beispiel #9
0
 public void BadFormatTooShortPadded()
 {
     Assert.Throws <FormatException>(
         () => HashCode32.Parse("1234                                                            "));
 }
Beispiel #10
0
 public void BadFormatDouble0X()
 {
     Assert.Throws <FormatException>(
         () => HashCode32.Parse("0x0x123456543456765445612345654345676544561234565434567654456"));
 }
Beispiel #11
0
 public void BadFormatLow()
 {
     Assert.Throws <FormatException>(
         () => HashCode32.Parse("0123456789f12323432343234324324323433232sdrtyrtyttytrty"));
 }
Beispiel #12
0
 public void BadFormatHigh()
 {
     Assert.Throws <FormatException>(
         () => HashCode32.Parse(
             "76544561234565434567654456012sdfafasjkl;fdsafdk1234565434561234565434567654456"));
 }
Beispiel #13
0
 public void BadFormat()
 {
     Assert.Throws <FormatException>(() => HashCode32.Parse("0123456780123457833943"));
 }
Beispiel #14
0
 public void ArgumentNull()
 {
     Assert.Throws <ArgumentNullException>("s", () => HashCode32.Parse(null));
 }
Beispiel #15
0
 public void ToStringTests()
 {
     Assert.Equal(
         "12AB34CD", HashCode32.Parse("12ab34cd").ToString());
 }
 public static IHashValue ToHashValue(this HashCode32 hash)
 {
     return(new HashValue(hash.GetByteArray(), hash.BitLength));
 }