Tests improved

master
Inga 🏳‍🌈 7 years ago
parent 785b0195a3
commit 9f26b7241a
  1. 24
      Tests/Hasher.SHA1/HasherTests.cs

@ -8,10 +8,12 @@
public static class HasherTests
{
private const int MegabytesPerSecond = 500;
private const int MegabytesPerSecondLowSpeed = 200;
private const int MegabytesPerSecondHighSpeed = 1000;
// Test vectors taken from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
[Theory]
[InlineData("DA39A3EE 5E6B4B0D 3255BFEF 95601890 AFD80709", "")]
[InlineData("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", "abc")]
[InlineData("84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")]
public static async Task TestHashes(string expectedHash, string inputMessage)
@ -25,13 +27,14 @@
// Single-threaded SHA1 performance on i5-6500 is around 500MB/s
[Theory]
[InlineData(1, 0, 1)]
[InlineData(1000, 0, 1)]
[InlineData(1000000, 0.5 * 1000 / MegabytesPerSecond, 2 * 1000 / MegabytesPerSecond)]
[InlineData(10000000, 0.5 * 10000 / MegabytesPerSecond, 2 * 10000 / MegabytesPerSecond)]
[InlineData(100000000, 0.5 * 100000 / MegabytesPerSecond, 2 * 100000 / MegabytesPerSecond)]
[InlineData(1000000000, 0.5 * 1000000 / MegabytesPerSecond, 2 * 1000000 / MegabytesPerSecond)]
public static async Task TestPerformance(int length, int minMilliseconds, int maxMilliseconds)
[InlineData(0)]
[InlineData(1)]
[InlineData(1000)]
[InlineData(1000000)]
[InlineData(10000000)]
[InlineData(100000000)]
[InlineData(1000000000)]
public static async Task TestPerformance(int length)
{
var messageBytes = new byte[length];
for (var i = 0; i < length; i++)
@ -39,13 +42,16 @@
messageBytes[i] = (byte)((int)(Math.E * length + Math.PI * i) % 256);
}
var expectedTimeLow = length / (MegabytesPerSecondHighSpeed * 1000);
var expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000);
var hasher = new SHA1ContentHasher();
var stopwatch = new Stopwatch();
stopwatch.Start();
await hasher.ComputeHash(messageBytes);
stopwatch.Stop();
Assert.InRange(stopwatch.ElapsedMilliseconds, minMilliseconds, maxMilliseconds);
Assert.InRange(stopwatch.ElapsedMilliseconds, expectedTimeLow, expectedTimeHigh);
}
}
}

Loading…
Cancel
Save