private static Double GetAverageSpeed(Func <UInt32, Hash> hashInitializer, Int32 length, Int32 repetitions, Int32 align) { RandomXorShift r = new RandomXorShift(); Byte[] key = new Byte[length + 512]; unsafe { fixed(Byte *pin = key) { UInt64 pinValue = (UInt64)pin; UInt64 alignValue = ((pinValue + 255ul) & 0xFFFFFFFFFFFFFF00ul) + (UInt64)align; Int32 offset = (Int32)(alignValue - pinValue); List <Double> results = new List <Double>(repetitions); for (Int32 i = 0; i < repetitions; ++i) { r.NextBytes(key, offset, length); Hash hash = hashInitializer((UInt32)i); using (Clock clock = new Clock(CLOCK_MAXIMUM_IDLE_TIME)) { DateTime start = clock.GetTime(); hash.ComputeHash(key, offset, length); DateTime end = clock.GetTime(); Double ms = (end - start).TotalMilliseconds; Double bps = (length * 1000.0d) / ms; if (bps >= 0.0d) { results.Add(bps); } } } Double mean = MathUtilities.Mean(results); Double threshold = 2.0d * MathUtilities.StandardDeviation(results, mean); for (Int32 i = results.Count - 1; i >= 0; --i) { if (Math.Abs(results[i] - mean) > threshold) { results.RemoveAt(i); } } return(MathUtilities.Mean(results)); } } }
public void Test(UInt32 seed, Int32?sourceLegth, Int32 sourceOffset, Int32?destinationLength, Int32 destinationOffset, Int32 count, Object expectedResult) { Byte[] source; if (sourceLegth.HasValue) { source = new Byte[sourceLegth.Value]; RandomXorShift random = new RandomXorShift(seed); random.NextBytes(source); } else { source = null; } Byte[] destination = destinationLength.HasValue ? new Byte[destinationLength.Value] : null; Object actualResult; try { UnsafeBuffer.BlockCopy(source, sourceOffset, destination, destinationOffset, count); actualResult = destination; } catch (Exception e) { actualResult = e.GetType(); } if (expectedResult is Byte[] expectedBytes) { m_Output.WriteLine($"EXPECTED={{ {String.Join(", ", expectedBytes.Select(x => x.ToString()))} }}"); } else { Type expectedType = (Type)expectedResult; m_Output.WriteLine($"EXPECTED={((expectedType == null) ? String.Empty : expectedType.Name)}"); } if (actualResult is Byte[] actualBytes) { m_Output.WriteLine($"ACTUAL={{ {String.Join(", ", actualBytes.Select(x => x.ToString()))} }}"); } else { Type actualType = (Type)actualResult; m_Output.WriteLine($"ACTUAL={((actualType == null) ? String.Empty : actualType.Name)}"); } Assert.Equal(expectedResult, actualResult); }
public void BufferTest(UInt32 seed, Int32?bufferLength, Int32 offset, Int32 count, Object expectedResult) { RandomXorShift random = new RandomXorShift(seed); Byte[] buffer = bufferLength.HasValue ? new Byte[bufferLength.Value] : null; Object actualResult; try { random.NextBytes(buffer, offset, count); actualResult = buffer; } catch (Exception e) { actualResult = e.GetType(); } if (expectedResult is Byte[] expectedBytes) { m_Output.WriteLine($"EXPECTED={{ {String.Join(", ", expectedBytes.Select(x => x.ToString()))} }}"); } else { Type expectedType = (Type)expectedResult; m_Output.WriteLine($"EXPECTED={((expectedType == null) ? String.Empty : expectedType.Name)}"); } if (actualResult is Byte[] actualBytes) { m_Output.WriteLine($"ACTUAL={{ {String.Join(", ", actualBytes.Select(x => x.ToString()))} }}"); } else { Type actualType = (Type)actualResult; m_Output.WriteLine($"ACTUAL={((actualType == null) ? String.Empty : actualType.Name)}"); } Assert.Equal(expectedResult, actualResult); }