Speeds up file-equal assertions hugely

This commit is contained in:
benbierens 2023-06-07 10:56:25 +02:00
parent 677ead4fd3
commit b4f144a7fb
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
1 changed files with 31 additions and 9 deletions

View File

@ -1,5 +1,6 @@
using Logging;
using NUnit.Framework;
using System.Runtime.InteropServices;
using Utils;
namespace DistTestCore
@ -154,11 +155,33 @@ namespace DistTestCore
public string Label { get; }
public void AssertIsEqual(TestFile? actual)
{
var sw = Stopwatch.Begin(log);
try
{
AssertEqual(actual);
}
finally
{
sw.End($"{nameof(TestFile)}.{nameof(AssertIsEqual)}");
}
}
public string Describe()
{
if (!string.IsNullOrEmpty(Label)) return Label;
return $"'{Filename}' ({Formatter.FormatByteSize(GetFileSize())})";
}
private void AssertEqual(TestFile? actual)
{
if (actual == null) Assert.Fail("TestFile is null.");
if (actual == this || actual!.Filename == Filename) Assert.Fail("TestFile is compared to itself.");
Assert.That(actual.GetFileSize(), Is.EqualTo(GetFileSize()), "Files are not of equal length.");
Stopwatch.Measure(log, "sizes", () =>
{
Assert.That(actual.GetFileSize(), Is.EqualTo(GetFileSize()), "Files are not of equal length.");
});
using var streamExpected = new FileStream(Filename, FileMode.Open, FileAccess.Read);
using var streamActual = new FileStream(actual.Filename, FileMode.Open, FileAccess.Read);
@ -179,15 +202,14 @@ namespace DistTestCore
log.Log($"OK: '{Describe()}' is equal to '{actual.Describe()}'.");
return;
}
Assert.That(readActual, Is.EqualTo(readExpected), "Unable to read buffers of equal length.");
CollectionAssert.AreEqual(bytesExpected, bytesActual, "Files are not binary-equal.");
}
}
public string Describe()
{
if (!string.IsNullOrEmpty(Label)) return Label;
return $"'{Filename}' ({Formatter.FormatByteSize(GetFileSize())})";
Assert.That(readActual, Is.EqualTo(readExpected), "Unable to read buffers of equal length.");
for (var i = 0; i < readActual; i++)
{
if (bytesExpected[i] != bytesActual[i]) Assert.Fail("File contents not equal.");
}
}
}
private long GetFileSize()