Skip to content

Commit

Permalink
Remove self-check code.
Browse files Browse the repository at this point in the history
This is replaced with Xunit tests.
  • Loading branch information
bgrainger committed Feb 11, 2023
1 parent b7bb5ed commit 6ce2181
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 95 deletions.
102 changes: 9 additions & 93 deletions src/BsDiffTool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Diagnostics;
using BsDiff;

namespace BsDiffTool
{
class Program
public sealed class Program
{
public static void Main(string[] args)
{
Expand All @@ -14,102 +13,19 @@ public static void Main(string[] args)
return;
}

// check for special command-line switch that performs a self-test
if (args[0] == "--check")
CheckImplementation(args[1], args[2]);
else
{
var oldFile = args[0];
var newFile = args[1];
var patchFile = args[2];

try
{
using (var output = new FileStream(patchFile, FileMode.Create))
BinaryPatch.Create(File.ReadAllBytes(oldFile), File.ReadAllBytes(newFile), output);
}
catch (FileNotFoundException ex)
{
Console.Error.WriteLine("Could not open '{0}'.", ex.FileName);
}
}
}

// Creates a patch for oldFile and newFile with the reference C implementation and this C# code, then verifies that
// the patch applies correctly.
private static void CheckImplementation(string oldFile, string newFile)
{
// to test, download bsdiff and bspatch for Windows, e.g., from http://sites.inka.de/tesla/others.html#bsdiff
const string bsdiffPath = @"C:\Util\bsdiff.exe";
const string rbspatchPath = @"C:\Util\bspatch.exe";

var tempPath = Path.GetTempPath();
var referencePatchFileName = Path.Combine(tempPath, "reference.patch");
var portPatchFileName = Path.Combine(tempPath, "port.patch");
var oldFile = args[0];
var newFile = args[1];
var patchFile = args[2];

// run reference implementation
var referenceTime = Stopwatch.StartNew();
using (var process = Process.Start(bsdiffPath, QuotePaths(oldFile, newFile, referencePatchFileName)))
try
{
process.WaitForExit();
referenceTime.Stop();
using (var output = new FileStream(patchFile, FileMode.Create))
BinaryPatch.Create(File.ReadAllBytes(oldFile), File.ReadAllBytes(newFile), output);
}

// run C# implementation
var portTime = Stopwatch.StartNew();
using (var output = new FileStream(portPatchFileName, FileMode.Create))
BinaryPatch.Create(File.ReadAllBytes(oldFile), File.ReadAllBytes(newFile), output);
portTime.Stop();

Console.WriteLine("Patches created in {0} (reference) and {1} (C# port).", referenceTime.Elapsed, portTime.Elapsed);
Console.WriteLine("File sizes (in bytes) are {0:n0} (reference) and {1:n0} (C# port).", new FileInfo(referencePatchFileName).Length, new FileInfo(portPatchFileName).Length);

var outputFilePaths = new[] { "test-ref-ref.dat", "test-prt-ref.dat", "test-ref-prt.dat", "test-prt-prt.dat" }
.Select(fn => Path.Combine(tempPath, fn))
.ToArray();

Console.Write("Applying reference patch with reference binary...");
using (var process = Process.Start(rbspatchPath, QuotePaths(oldFile, outputFilePaths[0], referencePatchFileName)))
process.WaitForExit();
Console.WriteLine("done.");

Console.Write("Applying port patch with reference binary...");
using (var process = Process.Start(rbspatchPath, QuotePaths(oldFile, outputFilePaths[1], portPatchFileName)))
process.WaitForExit();
Console.WriteLine("done.");

Console.Write("Applying reference patch with port binary...");
using (var input = new FileStream(oldFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var output = new FileStream(outputFilePaths[2], FileMode.Create))
BinaryPatch.Apply(input, () => new FileStream(referencePatchFileName, FileMode.Open, FileAccess.Read, FileShare.Read), output);
Console.WriteLine("done.");

Console.Write("Applying port patch with port binary...");
using (var input = new FileStream(oldFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var output = new FileStream(outputFilePaths[3], FileMode.Create))
BinaryPatch.Apply(input, () => new FileStream(portPatchFileName, FileMode.Open, FileAccess.Read, FileShare.Read), output);
Console.WriteLine("done.");

var errors = 0;
var expectedOutput = File.ReadAllBytes(newFile);
foreach (var filePath in outputFilePaths)
catch (FileNotFoundException ex)
{
var actualOutput = File.ReadAllBytes(filePath);
if (!expectedOutput.SequenceEqual(actualOutput))
{
Console.Error.WriteLine("Incorrect results in {0}.", Path.GetFileName(filePath));
errors++;
}
Console.Error.WriteLine("Could not open '{0}'.", ex.FileName);
}

if (errors == 0)
Console.WriteLine("All patches are correct.");
}

// Returns a single string that contains all paths quoted and joined with spaces.
private static string QuotePaths(params string[] paths)
{
return "\"" + string.Join("\" \"", paths) + "\"";
}
}
}
4 changes: 2 additions & 2 deletions src/BsPatchTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace BsPatchTool;

class Program
public sealed class Program
{
static void Main(string[] args)
public static void Main(string[] args)
{
// check for correct usage
if (args.Length != 3)
Expand Down

0 comments on commit 6ce2181

Please sign in to comment.