internal static void Check(AddCopyCollection ac, params bool[] adds) { ac.Count.ShouldBe(adds.Length); for (int i = 0; i < adds.Length; i++) { IAddCopy addCopy = ac[i]; bool isAdd = addCopy.IsAdd; isAdd.ShouldBe(adds[i]); if (addCopy is Addition add) { add.IsAdd.ShouldBeTrue(); add.GetBytes().Length.ShouldBeGreaterThan(0); } else { Copy copy = (Copy)addCopy; copy.IsAdd.ShouldBeFalse(); copy.Length.ShouldBeGreaterThan(0); if (i == 0) { copy.BaseOffset.ShouldBe(0); } else { copy.BaseOffset.ShouldBeGreaterThan(0); } } } }
public void GetBytesTest() { AddCopyCollection ac = BinaryDiffTests.Diff("A", "BC"); BinaryDiffTests.Check(ac, true); Addition add = (Addition)ac[0]; Encoding.UTF8.GetString(add.GetBytes()).ShouldBe("BC"); }
private static void GetFileLines(string fileNameA, string fileNameB, out IList <string> a, out IList <string> b, out int leadingCharactersToIgnore, TextBinaryDiffArgs args, IDiffProgress progress) { a = null; b = null; leadingCharactersToIgnore = 0; CompareType compareType = args.CompareType; if (compareType == CompareType.Binary || (args.IsAuto && (DiffUtility.IsBinaryFile(fileNameA) || DiffUtility.IsBinaryFile(fileNameB)))) { using (FileStream fileA = File.OpenRead(fileNameA)) using (FileStream fileB = File.OpenRead(fileNameB)) { BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileA, fileB); BinaryDiffLines lines = new BinaryDiffLines(fileA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; } } if (compareType == CompareType.Xml || (args.IsAuto && (a == null || b == null))) { a = TryGetXmlLines(DiffUtility.GetXmlTextLines, fileNameA, fileNameA, !args.IsAuto, args, progress); // If A failed to parse with Auto, then there's no reason to try B. if (a != null) { b = TryGetXmlLines(DiffUtility.GetXmlTextLines, fileNameB, fileNameB, !args.IsAuto, args, progress); } // If we get here and the compare type was XML, then both // inputs parsed correctly, and both lists should be non-null. // If we get here and the compare type was Auto, then one // or both lists may be null, so we'll fallthrough to the text // handling logic. } if (a == null || b == null) { a = DiffUtility.GetFileTextLines(fileNameA, progress); b = DiffUtility.GetFileTextLines(fileNameB, progress); } }
public void ExecuteTest() { // A word in the middle changed, so this should be Copy, Add, Copy. AddCopyCollection ac = Diff("This is the first item in the sequence.", "This is the second item in the sequence."); Check(ac, false, true, false); // These are nothing alike, so the version file is all Copy. ac = Diff("Student", "Handbook"); Check(ac, true); // These two cases show how footprint length affects the diff. ac = Diff("Creatine pills", "Creative pills", footprintLength: 4); Check(ac, false, true, false); // Copy "Creati", Add "v", Copy "e pills". ac = Diff("Creatine pills", "Creative pills", footprintLength: 7); Check(ac, true, false); // Add "Creativ", Copy "e pills". }
public void GDiffTest() { AddCopyCollection ac = BinaryDiffTests.Diff("Creative", "Creating", footprintLength: 2); BinaryDiffTests.Check(ac, false, true); using MemoryStream memory = new(); ac.GDiff(memory); byte[] gdiff = memory.ToArray(); gdiff.Length.ShouldBe(13); // It always starts with "d1ff d1ff 4" (magic numbers and version). Check(gdiff, 0, 0xd1, 0xff, 0xd1, 0xff, 0x04); // Copy (code 249) 6 bytes ("Creati"). Check(gdiff, 5, 249, 0, 0, 6); // Add 2 bytes ("ng") and then EOF. Check(gdiff, 9, 2, (int)'n', (int)'g', 0); }
internal static AddCopyCollection Diff( string aContent, string bContent, int footprintLength = 8, int tableSize = 97, bool favorLastMatch = false) { BinaryDiff diff = new(); diff.FavorLastMatch = favorLastMatch; diff.FootprintLength = footprintLength; diff.TableSize = tableSize; using Stream aStream = new MemoryStream(Encoding.UTF8.GetBytes(aContent)); using Stream bStream = new MemoryStream(Encoding.UTF8.GetBytes(bContent)); AddCopyCollection result = diff.Execute(aStream, bStream); result.TotalByteLength.ShouldBe((int)bStream.Length); return(result); }
public void BinaryDiffLinesTest() { string baseText = "The first one"; AddCopyCollection ac = BinaryDiffTests.Diff(baseText, "The second one", footprintLength: 4); BinaryDiffTests.Check(ac, false, true, false); using MemoryStream baseStream = new(Encoding.UTF8.GetBytes(baseText)); BinaryDiffLines lines = new(baseStream, ac, 4); Check(lines.BaseLines, "00000000 54 68 65 20 The ", "00000004 66 69 72 73 firs", "00000008 74 t", "00000009 20 6F 6E 65 one"); Check(lines.VersionLines, "00000000 54 68 65 20 The ", "00000004 73 65 63 6F seco", "00000008 6E 64 nd", "0000000A 20 6F 6E 65 one"); }
private static void GetBinaryFileLines(FileCompInfo fileA, FileCompInfo fileB, TextBinaryDiffArgs args, IDiffProgress progress, out IList <string> a, out IList <string> b, out int leadingCharactersToIgnore) { a = new List <string>(); b = new List <string>(); leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; // Neither left nor right file exist or cannot be accessed if (fileA.FileExists == false && fileB.FileExists == false) { return; } Stream fileStreamA = null, fileStreamB = null; try { // Open the file or an internal empty stream to compare against if (fileA.FileExists) { fileStreamA = File.OpenRead(fileA.FileNamePath); } else { fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } // Open the file or an internal empty stream to compare against if (fileB.FileExists) { fileStreamB = File.OpenRead(fileB.FileNamePath); } else { fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress); BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; leadingCharactersToIgnore = BinaryDiffLines.PrefixLength; } finally { if (fileStreamA != null) { fileStreamA.Dispose(); } if (fileStreamB != null) { fileStreamB.Dispose(); } } return; }
/// <summary> /// Get Binary file contents rendered as text lines with line number marker at beginning of each line. /// </summary> /// <param name="fileA"></param> /// <param name="fileB"></param> /// <param name="args"></param> /// <param name="progress"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="leadingCharactersToIgnore">Leading number of characters to ignore for diff in each line. /// This space is used in binary diff to display 8 digit line number and 4 digit space.</param> private DiffBinaryTextResults GetBinaryFileLines(IFileInfo fileA, IFileInfo fileB , TextBinaryDiffArgs args , IDiffProgress progress) { // Neither left nor right file exist or cannot be accessed if (fileA.FileExists == false && fileB.FileExists == false) { return(new DiffBinaryTextResults(CompareType.Binary, new FileContentInfo(), new FileContentInfo())); } Stream fileStreamA = null, fileStreamB = null; IList <string> a = null, b = null; try { // Open the file or an internal empty stream to compare against if (fileA.FileExists) { fileStreamA = File.OpenRead(fileA.FullName); } else { fileStreamA = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } // Open the file or an internal empty stream to compare against if (fileB.FileExists) { fileStreamB = File.OpenRead(fileB.FullName); } else { fileStreamB = Assembly.GetExecutingAssembly().GetManifestResourceStream("AehnlichLib.Binaries.Resources.NonExistingFile.bin"); } BinaryDiff diff = new BinaryDiff { FootprintLength = args.BinaryFootprintLength }; AddCopyCollection addCopy = diff.Execute(fileStreamA, fileStreamB, progress); BinaryDiffLines lines = new BinaryDiffLines(fileStreamA, addCopy, args.BinaryFootprintLength); a = lines.BaseLines; b = lines.VersionLines; } finally { if (fileStreamA != null) { fileStreamA.Dispose(); } if (fileStreamB != null) { fileStreamB.Dispose(); } } FileContentInfo af = new FileContentInfo(true, a); FileContentInfo bf = new FileContentInfo(true, b); return(new DiffBinaryTextResults(CompareType.Binary, af, bf, BinaryDiffLines.PrefixLength)); }