public void Test(IHash a_hash, SpeedTestDataSourceRow a_row, bool a_bFast) { Random r = new Random(); int length = 10000000; { byte[] ar = new byte[length]; r.NextBytes(ar); if (a_bFast) { a_hash.ComputeByte(ar[0]); a_row.ByteSpeed = Measure(ar.Length, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeByte(ar[i]); }); } a_hash.ComputeBytes(ar.Take(100).ToArray()); a_row.BytesSpeed = Measure(ar.Length, () => { a_hash.ComputeBytes(ar); }); } { if (a_bFast) { char[] ar = (from n in Enumerable.Range(0, length / 2) select (char)r.Next()).ToArray(); a_hash.ComputeChar(ar[0]); a_row.CharSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeChar(ar[i]); }); a_hash.ComputeChars(ar.Take(100).ToArray()); a_row.CharsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeChars(ar); }); } } { if (a_bFast) { short[] ar = (from n in Enumerable.Range(0, length / 2) select (short)r.Next()).ToArray(); a_hash.ComputeShort(ar[0]); a_row.ShortSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeShort(ar[i]); }); a_hash.ComputeShorts(ar.Take(100).ToArray()); a_row.ShortsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeShorts(ar); }); } } { if (a_bFast) { ushort[] ar = (from n in Enumerable.Range(0, length / 2) select (ushort)r.Next()).ToArray(); a_hash.ComputeUShort(ar[0]); a_row.UShortSpeed = Measure(ar.Length * 2, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeUShort(ar[i]); }); a_hash.ComputeUShorts(ar.Take(100).ToArray()); a_row.UShortsSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeUShorts(ar); }); } } { if (a_bFast) { int[] ar = (from n in Enumerable.Range(0, length / 4) select r.Next()).ToArray(); a_hash.ComputeInt(ar[0]); a_row.IntSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeInt(ar[i]); }); a_hash.ComputeInts(ar.Take(100).ToArray()); a_row.IntsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeInts(ar); }); } } { if (a_bFast) { uint[] ar = (from n in Enumerable.Range(0, length / 4) select (uint)r.Next()).ToArray(); a_hash.ComputeUInt(ar[0]); a_row.UIntSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeUInt(ar[i]); }); a_hash.ComputeUInts(ar.Take(100).ToArray()); a_row.UIntsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeUInts(ar); }); } } { if (a_bFast) { long[] ar = (from n in Enumerable.Range(0, length / 8) select (long)(((ulong)r.Next() + (ulong)r.Next() * (ulong)Math.Pow(2, 32)))).ToArray(); a_hash.ComputeLong(ar[0]); a_row.LongSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeLong(ar[i]); }); a_hash.ComputeLongs(ar.Take(100).ToArray()); a_row.LongsSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeLongs(ar); }); } } { if (a_bFast) { ulong[] ar = (from n in Enumerable.Range(0, length / 8) select ((ulong)r.Next() + (ulong)r.Next() * (ulong)Math.Pow(2, 32))).ToArray(); a_hash.ComputeULong(ar[0]); a_row.ULongSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeULong(ar[i]); }); a_row.ULongsSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeULongs(ar); }); } } { if (a_bFast) { float[] ar = (from n in Enumerable.Range(0, length / 8) select (float)(r.NextDouble() * r.Next())).ToArray(); a_hash.ComputeFloat(ar[0]); a_row.FloatSpeed = Measure(ar.Length * 4, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeFloat(ar[i]); }); a_hash.ComputeFloats(ar.Take(100).ToArray()); a_row.FloatsSpeed = Measure(ar.Length * 4, () => { a_hash.ComputeFloats(ar); }); } } { if (a_bFast) { double[] ar = (from n in Enumerable.Range(0, length / 8) select r.NextDouble() * r.Next()).ToArray(); a_hash.ComputeDouble(ar[0]); a_row.DoubleSpeed = Measure(ar.Length * 8, () => { for (int i = 0; i < ar.Length; i++) a_hash.ComputeDouble(ar[i]); }); a_hash.ComputeDoubles(ar.Take(100).ToArray()); a_row.DoublesSpeed = Measure(ar.Length * 8, () => { a_hash.ComputeDoubles(ar); }); } } { if (a_bFast) { byte[] a = new byte[length]; r.NextBytes(a); string ar = new String((from b in a select (char)(b / 2 + 32)).ToArray()); a_hash.ComputeString(ar.Substring(0, 100)); a_row.StringSpeed = Measure(ar.Length * 2, () => { a_hash.ComputeString(ar); }); } } }
protected void TestHashImplementationValueType(IHash a_baseHashFunction, IHash a_hash) { for(int j = 0; j < 8; j++) { { byte v = m_random.NextByte(); Assert.AreEqual(a_baseHashFunction.ComputeByte(v), a_hash.ComputeByte(v), a_hash.Name); } { char v = m_random.NextChar(); Assert.AreEqual(a_baseHashFunction.ComputeChar(v), a_hash.ComputeChar(v), a_hash.Name); } { short v = m_random.NextShort(); Assert.AreEqual(a_baseHashFunction.ComputeShort(v), a_hash.ComputeShort(v), a_hash.Name); } { ushort v = m_random.NextUShort(); Assert.AreEqual(a_baseHashFunction.ComputeUShort(v), a_hash.ComputeUShort(v), a_hash.Name); } { int v = m_random.Next(); Assert.AreEqual(a_baseHashFunction.ComputeInt(v), a_hash.ComputeInt(v), a_hash.Name); } { uint v = m_random.NextUInt(); Assert.AreEqual(a_baseHashFunction.ComputeUInt(v), a_hash.ComputeUInt(v), a_hash.Name); } { long v = m_random.NextLong(); Assert.AreEqual(a_baseHashFunction.ComputeLong(v), a_hash.ComputeLong(v), a_hash.Name); } { ulong v = m_random.NextULong(); Assert.AreEqual(a_baseHashFunction.ComputeULong(v), a_hash.ComputeULong(v), a_hash.Name); } { double v = m_random.NextDoubleFull(); Assert.AreEqual(a_baseHashFunction.ComputeDouble(v), a_hash.ComputeDouble(v), a_hash.Name); } { float v = m_random.NextFloatFull(); Assert.AreEqual(a_baseHashFunction.ComputeFloat(v), a_hash.ComputeFloat(v), a_hash.Name); } } for(int j = 0; j < 2; j++) { for(int i = 2050; i <= (a_hash.BlockSize * 3 + 1); i++) { byte[] v = m_random.NextBytes(i); Assert.AreEqual(a_baseHashFunction.ComputeBytes(v), a_hash.ComputeBytes(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { char[] v = m_random.NextChars(i); Assert.AreEqual(a_baseHashFunction.ComputeChars(v), a_hash.ComputeChars(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { short[] v = m_random.NextShorts(i); Assert.AreEqual(a_baseHashFunction.ComputeShorts(v), a_hash.ComputeShorts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { ushort[] v = (from n in Enumerable.Range(0, i) select m_random.NextUShort()).ToArray(); Assert.AreEqual(a_baseHashFunction.ComputeUShorts(v), a_hash.ComputeUShorts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { int[] v = m_random.NextInts(i); Assert.AreEqual(a_baseHashFunction.ComputeInts(v), a_hash.ComputeInts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { uint[] v = m_random.NextUInts(i); Assert.AreEqual(a_baseHashFunction.ComputeUInts(v), a_hash.ComputeUInts(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 1; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { long[] v = m_random.NextLongs(i); Assert.AreEqual(a_baseHashFunction.ComputeLongs(v), a_hash.ComputeLongs(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { ulong[] v = m_random.NextULongs(i); Assert.AreEqual(a_baseHashFunction.ComputeULongs(v), a_hash.ComputeULongs(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 8; i++) { double[] v = m_random.NextDoublesFull(i); Assert.AreEqual(a_baseHashFunction.ComputeDoubles(v), a_hash.ComputeDoubles(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 4; i++) { float[] v = m_random.NextFloatsFull(i); Assert.AreEqual(a_baseHashFunction.ComputeFloats(v), a_hash.ComputeFloats(v), String.Format("{0}, {1}", a_hash.Name, i)); } for(int i = 0; i <= (a_hash.BlockSize * 3 + 1) / 2; i++) { string v = m_random.NextString(i); Assert.AreEqual(a_baseHashFunction.ComputeString(v), a_hash.ComputeString(v), String.Format("{0}, {1}", a_hash.Name, i)); } } }
private void TestFastHash32(IHash a_hash) { IFastHash32 fh = a_hash as IFastHash32; if (fh == null) { return; } for (int i = 0; i < 10; i++) { { var data = m_random.NextBytes((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeBytesFast(data); var h2 = a_hash.ComputeBytes(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInt(); var h1 = fh.ComputeIntFast(data); var h2 = a_hash.ComputeInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInt(); var h1 = fh.ComputeUIntFast(data); var h2 = a_hash.ComputeUInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextByte(); var h1 = fh.ComputeByteFast(data); var h2 = a_hash.ComputeByte(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChar(); var h1 = fh.ComputeCharFast(data); var h2 = a_hash.ComputeChar(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShort(); var h1 = fh.ComputeShortFast(data); var h2 = a_hash.ComputeShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShort(); var h1 = fh.ComputeUShortFast(data); var h2 = a_hash.ComputeUShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoubleFull(); var h1 = fh.ComputeDoubleFast(data); var h2 = a_hash.ComputeDouble(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatFull(); var h1 = fh.ComputeFloatFast(data); var h2 = a_hash.ComputeFloat(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLong(); var h1 = fh.ComputeLongFast(data); var h2 = a_hash.ComputeLong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULong(); var h1 = fh.ComputeULongFast(data); var h2 = a_hash.ComputeULong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUIntsFast(data); var h2 = a_hash.ComputeUInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeIntsFast(data); var h2 = a_hash.ComputeInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeLongsFast(data); var h2 = a_hash.ComputeLongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeULongsFast(data); var h2 = a_hash.ComputeULongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoublesFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeDoublesFast(data); var h2 = a_hash.ComputeDoubles(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatsFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeFloatsFast(data); var h2 = a_hash.ComputeFloats(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChars((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeCharsFast(data); var h2 = a_hash.ComputeChars(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUShortsFast(data); var h2 = a_hash.ComputeUShorts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextString((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeStringFast(data); var h2 = a_hash.ComputeString(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeShortsFast(data); var h2 = a_hash.ComputeShorts(data); Assert.AreEqual(h1, h2.GetInt()); } } }
private void TestFastHash32(IHash a_hash) { IFastHash32 fh = a_hash as IFastHash32; if (fh == null) return; for (int i=0; i<10; i++) { { var data = m_random.NextBytes((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeBytesFast(data); var h2 = a_hash.ComputeBytes(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInt(); var h1 = fh.ComputeIntFast(data); var h2 = a_hash.ComputeInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInt(); var h1 = fh.ComputeUIntFast(data); var h2 = a_hash.ComputeUInt(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextByte(); var h1 = fh.ComputeByteFast(data); var h2 = a_hash.ComputeByte(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChar(); var h1 = fh.ComputeCharFast(data); var h2 = a_hash.ComputeChar(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShort(); var h1 = fh.ComputeShortFast(data); var h2 = a_hash.ComputeShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShort(); var h1 = fh.ComputeUShortFast(data); var h2 = a_hash.ComputeUShort(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoubleFull(); var h1 = fh.ComputeDoubleFast(data); var h2 = a_hash.ComputeDouble(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatFull(); var h1 = fh.ComputeFloatFast(data); var h2 = a_hash.ComputeFloat(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLong(); var h1 = fh.ComputeLongFast(data); var h2 = a_hash.ComputeLong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULong(); var h1 = fh.ComputeULongFast(data); var h2 = a_hash.ComputeULong(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUIntsFast(data); var h2 = a_hash.ComputeUInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextInts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeIntsFast(data); var h2 = a_hash.ComputeInts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextLongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeLongsFast(data); var h2 = a_hash.ComputeLongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextULongs((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeULongsFast(data); var h2 = a_hash.ComputeULongs(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextDoublesFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeDoublesFast(data); var h2 = a_hash.ComputeDoubles(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextFloatsFull((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeFloatsFast(data); var h2 = a_hash.ComputeFloats(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextChars((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeCharsFast(data); var h2 = a_hash.ComputeChars(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextUShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeUShortsFast(data); var h2 = a_hash.ComputeUShorts(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextString((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeStringFast(data); var h2 = a_hash.ComputeString(data); Assert.AreEqual(h1, h2.GetInt()); } { var data = m_random.NextShorts((i == 0) ? 0 : (int)(m_random.NextUInt() % 200)); var h1 = fh.ComputeShortsFast(data); var h2 = a_hash.ComputeShorts(data); Assert.AreEqual(h1, h2.GetInt()); } } }