Speeds up file-equal assertions hugely
This commit is contained in:
parent
677ead4fd3
commit
b4f144a7fb
|
@ -1,5 +1,6 @@
|
||||||
using Logging;
|
using Logging;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using Utils;
|
using Utils;
|
||||||
|
|
||||||
namespace DistTestCore
|
namespace DistTestCore
|
||||||
|
@ -154,11 +155,33 @@ namespace DistTestCore
|
||||||
public string Label { get; }
|
public string Label { get; }
|
||||||
|
|
||||||
public void AssertIsEqual(TestFile? actual)
|
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 == null) Assert.Fail("TestFile is null.");
|
||||||
if (actual == this || actual!.Filename == Filename) Assert.Fail("TestFile is compared to itself.");
|
if (actual == this || actual!.Filename == Filename) Assert.Fail("TestFile is compared to itself.");
|
||||||
|
|
||||||
|
Stopwatch.Measure(log, "sizes", () =>
|
||||||
|
{
|
||||||
Assert.That(actual.GetFileSize(), Is.EqualTo(GetFileSize()), "Files are not of equal length.");
|
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 streamExpected = new FileStream(Filename, FileMode.Open, FileAccess.Read);
|
||||||
using var streamActual = new FileStream(actual.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()}'.");
|
log.Log($"OK: '{Describe()}' is equal to '{actual.Describe()}'.");
|
||||||
return;
|
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()
|
Assert.That(readActual, Is.EqualTo(readExpected), "Unable to read buffers of equal length.");
|
||||||
|
|
||||||
|
for (var i = 0; i < readActual; i++)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(Label)) return Label;
|
if (bytesExpected[i] != bytesActual[i]) Assert.Fail("File contents not equal.");
|
||||||
return $"'{Filename}' ({Formatter.FormatByteSize(GetFileSize())})";
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private long GetFileSize()
|
private long GetFileSize()
|
||||||
|
|
Loading…
Reference in New Issue