Пример #1
0
        public unsafe void TestPieces()
        {
            var bufArr = new byte[BufferSize];
            for (int i = 0; i < BufferSize; ++i)
            {
                bufArr[i] = unchecked((byte)i);
            }
            for (int i = 0; i < BufferSize; ++i)
            {
                ulong a, b, c, d, seed1 = 1, seed2 = 2;
                var state = new SpookyHash();

                // all as one call
                a = seed1;
                b = seed2;
                fixed(byte* buf = bufArr)
                    SpookyHash.Hash128(buf, i, ref a, ref b);

                // all as one piece
                c = 0xdeadbeefdeadbeef;
                d = 0xbaceba11baceba11;
                state.Init(seed1, seed2);
                fixed(byte* buf = bufArr)
                    state.Update(buf, i);
                state.Final(out c, out d);

                Assert.Equal(a, c);
                Assert.Equal(b, d);

                for (int j = 0; j < i; ++j)
                {
                    c = seed1;
                    d = seed2;
                    state.Init(c, d);
                    fixed(byte* buf = bufArr)
                    {
                        state.Update(buf, j);
                        state.Update(buf + j, i - j);
                    }
                    state.Final(out c, out d);
                    Assert.Equal(a, c);
                    Assert.Equal(b, d);
                }
            }
        }
Пример #2
0
 public async static Task<ulong> SpookyHash64Async(this Stream stream, ulong seed, CancellationToken cancellationToken)
 {
     ExceptionHelper.CheckNotNull(stream);
     cancellationToken.ThrowIfCancellationRequested();
     var hash = new SpookyHash(seed, seed);
     var buffer = new byte[4096];
     for (int len = await stream.ReadAsync(buffer, 0, 4096, cancellationToken); len != 0; len = await stream.ReadAsync(buffer, 0, 4096, cancellationToken))
     {
         cancellationToken.ThrowIfCancellationRequested();
         hash.Update(buffer, 0, len);
     }
     hash.Final(out seed, out seed);
     return seed;
 }
Пример #3
0
        public static unsafe ulong SpookyHash64(
#if !NET_20 && !NET_30
            this
#endif
            Stream stream, ulong seed)
        {
            ExceptionHelper.CheckNotNull(stream);
            var hash = new SpookyHash(seed, seed);
            var buffer = new byte[4096];
            fixed(void* ptr = buffer)
                for(int len = stream.Read(buffer, 0, 4096); len != 0; len = stream.Read(buffer, 0, 4096))
                    hash.Update(ptr, len);
            hash.Final(out seed, out seed);
            return seed;
        }
Пример #4
0
 public void Serialize()
 {
     var sh = new SpookyHash();
     sh.Update(MediumLengthString);
     using(MemoryStream ms = new MemoryStream())
     {
         BinaryFormatter bf = new BinaryFormatter();
         bf.Serialize(ms, sh);
         ms.Seek(0, SeekOrigin.Begin);
         var copy = (SpookyHash)bf.Deserialize(ms);
         Assert.Equal(sh.Final(), copy.Final());
     }
 }
Пример #5
0
 public void SequenceOfStrings()
 {
     var sh = new SpookyHash();
     sh.Update(MediumLengthSequence());
     Assert.Equal(SpookyHasher.SpookyHash128(MediumLengthString), sh.Final());
 }
Пример #6
0
 public unsafe void FinalsEquivalent()
 {
     var sh = new SpookyHash();
     sh.Update("abcdefg");
     var h128 = sh.Final();
     ulong u1, u2;
     sh.Final(out u1, out u2);
     Assert.Equal(h128.UHash1, u1);
     Assert.Equal(h128.UHash2, u2);
     long l1, l2;
     sh.Final(out l1, out l2);
     Assert.Equal(h128.Hash1, l1);
     Assert.Equal(h128.Hash2, l2);
 }
Пример #7
0
 public void ConstructorsEquivalent()
 {
     ulong ui1 = 0xdeadcafe;
     ulong ui2 = 0xbaceba11;
     var fromU = new SpookyHash(ui1, ui2);
     var fromZU = new SpookyHash();
     fromZU.Init(ui1, ui2);
     long l1 = unchecked((long)ui1);
     long l2 = unchecked((long)ui2);
     var fromL = new SpookyHash(l1, l2);
     var fromZL = new SpookyHash();
     fromZL.Init(l1, l2);
     fromU.Update(MediumLengthString);
     fromZU.Update(MediumLengthString);
     fromL.Update(MediumLengthString);
     fromZL.Update(MediumLengthString);
     var hash = fromU.Final();
     Assert.Equal(hash, fromZU.Final());
     Assert.Equal(hash, fromL.Final());
     Assert.Equal(hash, fromZL.Final());
     Assert.Equal(hash.ToString(), fromZL.Final().ToString());
 }
Пример #8
0
 public void StringExtension()
 {
     string testString;
     using(var stm = GetStream())
     using(var tr = new StreamReader(stm))
         testString = tr.ReadToEnd();
     var sh = new SpookyHash();
     sh.Update(testString);
     var h = sh.Final();
     int len = testString.Length;
     Assert.Equal(h, SpookyHasher.SpookyHash128(testString));
     Assert.Equal(h, SpookyHasher.SpookyHash128(testString, 0, len, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF));
     Assert.Equal(
         h,
         unchecked(SpookyHasher.SpookyHash128(
             testString,
             0,
             len,
             (long)0xDEADBEEFDEADBEEF,
             (long)0xDEADBEEFDEADBEEF)));
     Assert.Equal(h, SpookyHasher.SpookyHash128(testString, 0, len));
     var hashSlice = SpookyHasher.SpookyHash128(testString, 50, 100, 0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF);
     Assert.NotEqual(h, hashSlice);
     Assert.Equal(
         hashSlice,
         unchecked(SpookyHasher.SpookyHash128(
             testString,
             50,
             100,
             (long)0xDEADBEEFDEADBEEF,
             (long)0xDEADBEEFDEADBEEF)));
     long longHash = SpookyHasher.SpookyHash64(testString, 0, len, unchecked((long)0xDEADBEEFDEADBEEF));
     Assert.Equal(longHash, SpookyHasher.SpookyHash64(testString, unchecked((long)0xDEADBEEFDEADBEEF)));
     Assert.Equal(longHash, SpookyHasher.SpookyHash64(testString, 0, len));
     Assert.Equal(longHash, SpookyHasher.SpookyHash64(testString));
     int hash = SpookyHasher.SpookyHash32(testString, 0, len, unchecked((int)0xDEADBEEF));
     Assert.Equal(hash, SpookyHasher.SpookyHash32(testString, unchecked((int)0xDEADBEEF)));
     Assert.Equal(hash, SpookyHasher.SpookyHash32(testString, 0, len));
     Assert.Equal(hash, SpookyHasher.SpookyHash32(testString));
     testString = null;
     Assert.Equal(HashCode128.Zero, SpookyHasher.SpookyHash128(testString));
     Assert.Equal(
         HashCode128.Zero,
         SpookyHasher.SpookyHash128(
             testString,
             0,
             200,
             0xDEADBEEFDEADBEEF,
             0xDEADBEEFDEADBEEF));
     Assert.Equal(
         HashCode128.Zero,
         unchecked(SpookyHasher.SpookyHash128(
             testString,
             0,
             200,
             (long)0xDEADBEEFDEADBEEF,
             (long)0xDEADBEEFDEADBEEF)));
     Assert.Equal(HashCode128.Zero, SpookyHasher.SpookyHash128(testString, 0, 200));
     Assert.Equal(
         HashCode128.Zero,
         SpookyHasher.SpookyHash128(
             testString,
             50,
             100,
             0xDEADBEEFDEADBEEF,
             0xDEADBEEFDEADBEEF));
     Assert.Equal(0, SpookyHasher.SpookyHash64(testString, 0, 200, unchecked((long)0xDEADBEEFDEADBEEF)));
     Assert.Equal(0, SpookyHasher.SpookyHash64(testString, unchecked((long)0xDEADBEEFDEADBEEF)));
     Assert.Equal(0, SpookyHasher.SpookyHash64(testString, 0, 200));
     Assert.Equal(0, SpookyHasher.SpookyHash64(testString));
     Assert.Equal(0, SpookyHasher.SpookyHash32(testString, 0, 200, unchecked((int)0xDEADBEEF)));
     Assert.Equal(0, SpookyHasher.SpookyHash32(testString, unchecked((int)0xDEADBEEF)));
     Assert.Equal(0, SpookyHasher.SpookyHash32(testString, 0, 200));
     Assert.Equal(0, SpookyHasher.SpookyHash32(testString));
 }