Esempio n. 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));
 }
Esempio n. 2
0
        public void ConstructorsEquivalent()
        {
            ulong      ui1       = 0xdeadcafe;
            ulong      ui2       = 0xbaceba11;
            SpookyHash fromU     = new SpookyHash(ui1, ui2);
            SpookyHash fromZeroU = new SpookyHash();

            fromZeroU.Init(ui1, ui2);
            long       l1        = unchecked ((long)ui1);
            long       l2        = unchecked ((long)ui2);
            SpookyHash fromL     = new SpookyHash(l1, l2);
            SpookyHash fromZeroL = new SpookyHash();

            fromZeroL.Init(l1, l2);
            fromU.Update(MediumLengthString);
            fromZeroU.Update(MediumLengthString);
            fromL.Update(MediumLengthString);
            fromZeroL.Update(MediumLengthString);
            HashCode128 hash = fromU.Final();

            Assert.Equal(hash, fromZeroU.Final());
            Assert.Equal(hash, fromL.Final());
            Assert.Equal(hash, fromZeroL.Final());
            Assert.Equal(hash.ToString(), fromZeroL.Final().ToString());
        }
Esempio n. 3
0
        public unsafe void Hash128PtrOverloads()
        {
            ulong  seed1      = 238929482;
            ulong  seed2      = 19384920392;
            string testString = "This is a test string";
            ulong  hash1      = seed1;
            ulong  hash2      = seed2;

            fixed(void *ptr = testString)
            {
                int len = testString.Length * 2;

                SpookyHash.Hash128(ptr, len, ref hash1, ref hash2);
                HashCode128 hc = new HashCode128(hash1, hash2);

                Assert.Equal(hc, SpookyHash.Hash128((UIntPtr)ptr, len, seed1, seed2));
                Assert.Equal(hc, SpookyHash.Hash128((UIntPtr)ptr, len, (long)seed1, (long)seed2));
                Assert.Equal(hc, SpookyHash.Hash128((IntPtr)ptr, len, seed1, seed2));
                Assert.Equal(hc, SpookyHash.Hash128((IntPtr)ptr, len, (long)seed1, (long)seed2));
            }

            SpookyHash.Hash128(default(void *), 50, ref hash1, ref hash2);
            Assert.Equal(0UL, hash1);
            Assert.Equal(0UL, hash2);
            Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(UIntPtr.Zero, 50, seed1, seed2));
            Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(UIntPtr.Zero, 50, (long)seed1, (long)seed2));
            Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(IntPtr.Zero, 50, seed1, seed2));
            Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(IntPtr.Zero, 50, (long)seed1, (long)seed2));
        }
Esempio n. 4
0
        public void StringExtension()
        {
            string testString;

            using (Stream stm = GetStream())
                using (StreamReader tr = new StreamReader(stm))
                {
                    testString = tr.ReadToEnd();
                }
            SpookyHash sh = new SpookyHash();

            sh.Update(testString);
            HashCode128 h   = sh.Final();
            int         len = testString.Length;

            Assert.Equal(h, testString.SpookyHash128());
            Assert.Equal(h, testString.SpookyHash128(0, len, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(
                h,
                unchecked (testString.SpookyHash128(0, len, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(h, testString.SpookyHash128(0, len));
            HashCode128 hashSlice = testString.SpookyHash128(50, 100, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF);

            Assert.NotEqual(h, hashSlice);
            Assert.Equal(
                hashSlice,
                unchecked (testString.SpookyHash128(50, 100, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            long longHash = testString.SpookyHash64(0, len, unchecked ((long)0xDEADBEEFDEADBEEF));

            Assert.Equal(longHash, testString.SpookyHash64(unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(longHash, testString.SpookyHash64(0, len));
            Assert.Equal(longHash, testString.SpookyHash64());
            int hash = testString.SpookyHash32(0, len, unchecked ((int)0xDEADBEEF));

            Assert.Equal(hash, testString.SpookyHash32(unchecked ((int)0xDEADBEEF)));
            Assert.Equal(hash, testString.SpookyHash32(0, len));
            Assert.Equal(hash, testString.SpookyHash32());
            Assert.Equal(HashCode128.Zero, default(string).SpookyHash128());
            Assert.Equal(
                HashCode128.Zero,
                default(string).SpookyHash128(0, 200, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(
                HashCode128.Zero,
                unchecked (default(string).SpookyHash128(0, 200, (long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(HashCode128.Zero, default(string).SpookyHash128(0, 200));
            Assert.Equal(
                HashCode128.Zero,
                default(string).SpookyHash128(50, 100, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
            Assert.Equal(0, default(string).SpookyHash64(0, 200, unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash64(unchecked ((long)0xDEADBEEFDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash64(0, 200));
            Assert.Equal(0, default(string).SpookyHash64());
            Assert.Equal(0, default(string).SpookyHash32(0, 200, unchecked ((int)0xDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash32(unchecked ((int)0xDEADBEEF)));
            Assert.Equal(0, default(string).SpookyHash32(0, 200));
            Assert.Equal(0, default(string).SpookyHash32());
        }
        public static HashCode128 ToHashCode128(this IHashValue hashVal, bool strictMode = true)
        {
            if (hashVal is null)
            {
                return(HashCode128.Zero);
            }
            var hex = hashVal.GetHexString();

            return(strictMode ? HashCode128.Parse(hex) : HashCode128.ParseLoosely(hex));
        }
Esempio n. 6
0
        public void OutputTest()
        {
            var hash = HashCode128.Parse("123456789abcdef00fedcba987654321");

            hash.GetHexString(false).ShouldBe("123456789abcdef00fedcba987654321");
            hash.GetHexString(true).ShouldBe("123456789ABCDEF00FEDCBA987654321");

            hash.GetBinString(false).ShouldBe("10010001101000101011001111000100110101011110011011110111100000000111111101101110010111010100110000111011001010100001100100001");
            hash.GetBinString(true).ShouldBe("00010010001101000101011001111000100110101011110011011110111100000000111111101101110010111010100110000111011001010100001100100001");
        }
Esempio n. 7
0
#pragma warning disable 1718 //Yes, I'm testing the obvious!
        public void EqualsOps()
        {
            Assert.True(HashCode128.Zero == HashCode128.Zero);
            Assert.True(
                HashCode128.Parse("0123456789abcdef0123456789abcdef")
                == HashCode128.Parse("0123456789ABCDEF0123456789ABCDEF"));
            Assert.False(HashCode128.Zero != HashCode128.Zero);
            Assert.False(
                HashCode128.Parse("0123456789abcdef0123456789abcdef")
                != HashCode128.Parse("0123456789ABCDEF0123456789ABCDEF"));
            Assert.True(HashCode128.Parse("0123456789abcdef0123456789abcdef") != HashCode128.Zero);
            Assert.False(HashCode128.Parse("0123456789abcdef0123456789abcdef") == HashCode128.Zero);
        }
Esempio n. 8
0
        public void EqualsObj()
        {
            Assert.Equal(HashCode128.Zero, (object)HashCode128.Zero);
            object boxed = HashCode128.Parse("0123456789abcdef0123456789ABCDEF");

            Assert.True(boxed.Equals(HashCode128.Parse("0123456789ABCDEF0123456789ABCDEF")));
            Assert.False(boxed.Equals(HashCode128.Zero));
            Assert.False(boxed.Equals("not a hash code"));
            Assert.True(
                Equals(
                    HashCode128.Parse("fed c b a9876543210 0123456789ABCDEF"),
                    HashCode128.Parse("FE DCBA 98765 432 10 0123456789ABCD EF     ")));
        }
Esempio n. 9
0
        public void FinalsEquivalent()
        {
            SpookyHash sh = new SpookyHash();

            sh.Update("abcdefg");
            HashCode128 h128 = sh.Final();

            sh.Final(out ulong u1, out ulong u2);
            Assert.Equal(h128.UHash1, u1);
            Assert.Equal(h128.UHash2, u2);
            sh.Final(out long l1, out long l2);
            Assert.Equal(h128.Hash1, l1);
            Assert.Equal(h128.Hash2, l2);
        }
        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);
        }
Esempio n. 11
0
 public void BadFormatTooShortLow()
 {
     Assert.Throws <FormatException>(
         () => HashCode128.Parse("0123456789abcdef01234                                        "));
 }
Esempio n. 12
0
 public void BadFormatTooShortHigh()
 {
     Assert.Throws <FormatException>(() => HashCode128.Parse(new string(' ', 32)));
 }
Esempio n. 13
0
 public void BadFormatTooShortPadded()
 {
     Assert.Throws <FormatException>(
         () => HashCode128.Parse("1234                                                            "));
 }
Esempio n. 14
0
 public void BadFormatDouble0X()
 {
     Assert.Throws <FormatException>(
         () => HashCode128.Parse("0x0x123456543456765445612345654345676544561234565434567654456"));
 }
Esempio n. 15
0
 public void BadFormatLow()
 {
     Assert.Throws <FormatException>(
         () => HashCode128.Parse("0123456789f12323432343234324324323433232sdrtyrtyttytrty"));
 }
Esempio n. 16
0
 public void BadFormatHigh()
 {
     Assert.Throws <FormatException>(
         () => HashCode128.Parse(
             "76544561234565434567654456012sdfafasjkl;fdsafdk1234565434561234565434567654456"));
 }
Esempio n. 17
0
 public void BadFormat()
 {
     Assert.Throws <FormatException>(() => HashCode128.Parse("0123456780123457833943"));
 }
Esempio n. 18
0
 public void ArgumentNull()
 {
     Assert.Throws <ArgumentNullException>("s", () => HashCode128.Parse(null));
 }
Esempio n. 19
0
 public unsafe void Hash128PtrOverloads()
 {
     ulong seed1 = 238929482;
     ulong seed2 = 19384920392;
     string testString = "This is a test string";
     ulong hash1 = seed1;
     ulong hash2 = seed2;
     fixed(void* ptr = testString)
     {
         int len = testString.Length * 2;
         SpookyHash.Hash128(ptr, len, ref hash1, ref hash2);
         HashCode128 hc = new HashCode128(hash1, hash2);
         Assert.Equal(hc, SpookyHash.Hash128((UIntPtr)ptr, len, seed1, seed2));
         Assert.Equal(hc, SpookyHash.Hash128((UIntPtr)ptr, len, (long)seed1, (long)seed2));
         Assert.Equal(hc, SpookyHash.Hash128((IntPtr)ptr, len, seed1, seed2));
         Assert.Equal(hc, SpookyHash.Hash128((IntPtr)ptr, len, (long)seed1, (long)seed2));
     }
     void* nullPointer = null;
     SpookyHash.Hash128(nullPointer, 50, ref hash1, ref hash2);
     Assert.Equal(0UL, hash1);
     Assert.Equal(0UL, hash2);
     Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(UIntPtr.Zero, 50, seed1, seed2));
     Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(UIntPtr.Zero, 50, (long)seed1, (long)seed2));
     Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(IntPtr.Zero, 50, seed1, seed2));
     Assert.Equal(HashCode128.Zero, SpookyHash.Hash128(IntPtr.Zero, 50, (long)seed1, (long)seed2));
 }
Esempio n. 20
0
 public void ToStringTests()
 {
     Assert.Equal(
         "0123456789ABCDEF0123456789ABCDEF", HashCode128.Parse("0123456789abcdef0123456789ABCDEF").ToString());
 }