Skip to content

Commit

Permalink
fixed issue #6, prepared release 1.2.1 (#7)
Browse files Browse the repository at this point in the history
* fixed issue #6

* prepared release 1.2.1
  • Loading branch information
spkl authored Apr 5, 2021
1 parent 6bd0407 commit 760e5b2
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.mus filter=lfs diff=lfs merge=lfs -text
9 changes: 5 additions & 4 deletions src/Diffs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<Copyright>Copyright (c) 2021 Sebastian Fischer</Copyright>
<RepositoryUrl>https://github.com/spkl/Diffs</RepositoryUrl>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<Version>1.2.0</Version>
<FileVersion>1.2.1.0</FileVersion>
<Version>1.2.1</Version>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\keys\spkl.Diffs.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
Expand All @@ -22,8 +22,9 @@
This project uses Semantic Versioning (https://semver.org/).</Description>
<PackageTags>diff diffs difference algorithm ses shortest edit script myers</PackageTags>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageReleaseNotes>- Improved performance for .NET standard / Full Framework.
- Introduced IndexRange class.</PackageReleaseNotes>
<PackageReleaseNotes>This is a bugfix release fixing two critical issues:
- The VArray containing the endpoints of the furthest reaching paths was created too small, leading to an IndexOutOfRangeException in some cases.
- The result could contain items that indicate they exists on "ResultType.Both" sides, yet they contained two different values as AItem and BItem.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
18 changes: 6 additions & 12 deletions src/MyersDiff{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public MyersDiff(T[] aValues, T[] bValues, IEqualityComparer<T> comparer)
this.aRemoved = new bool[this.aValues.Length];
this.bAdded = new bool[this.bValues.Length];

int VMAX = aValues.Length + bValues.Length + 3;
int VMAX = (aValues.Length + bValues.Length) * 2;

this.Vf = VArray<int>.CreateFromTo(-VMAX, VMAX);
this.Vr = VArray<int>.CreateFromTo(-VMAX, VMAX);
Expand Down Expand Up @@ -242,13 +242,13 @@ private void LCS(IndexRange A, IndexRange B)
}
else
{
(_, int x, int y, int u, int v) = this.SMS(A, B);
(int x, int y) = this.SMS(A, B);
this.LCS(A.Range(0, x), B.Range(0, y));
this.LCS(A.Range(u), B.Range(v));
this.LCS(A.Range(x), B.Range(y));
}
}

private (int D, int x, int y, int u, int v) SMS(IndexRange A, IndexRange B)
private (int x, int y) SMS(IndexRange A, IndexRange B)
{
int N = A.Length;
int M = B.Length;
Expand Down Expand Up @@ -287,10 +287,7 @@ private void LCS(IndexRange A, IndexRange B)

if (deltaIsOdd && k >= delta - (D - 1) && k <= delta + D - 1 && this.Vf[k] >= this.Vr[k])
{
int length = 2 * D - 1;
int u = this.Vr[k];
int v = this.Vr[k] - k;
return (length, x, y, x, y);
return (x, y);
}
}

Expand Down Expand Up @@ -318,10 +315,7 @@ private void LCS(IndexRange A, IndexRange B)

if (deltaIsEven && k >= -D && k <= D && this.Vf[k] >= this.Vr[k])
{
int length = 2 * D;
int u = this.Vf[k];
int v = this.Vf[k] - k;
return (length, x, y, u, v);
return (x, y);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/Data/D54ACE32535F00233F43B243D0EF1EDE.mus
Git LFS file not shown
3 changes: 3 additions & 0 deletions test/Data/Smooth_Operator.mus
Git LFS file not shown
9 changes: 9 additions & 0 deletions test/Diffs.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
<ProjectReference Include="..\src\Diffs.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Data\D54ACE32535F00233F43B243D0EF1EDE.mus">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Smooth_Operator.mus">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions test/MyersDiffClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace spkl.Diffs.Test
Expand Down Expand Up @@ -70,5 +72,80 @@ public void ReferenceEditScript(ReferenceCase testCase)
{
Assert.That(new MyersDiff<string>(testCase.A, testCase.B).GetEditScript().ToArray(), Is.EqualTo(testCase.EditScript));
}

[Test]
[TestCase("D54ACE32535F00233F43B243D0EF1EDE.mus", "Smooth_Operator.mus")]
[TestCase("Smooth_Operator.mus", "D54ACE32535F00233F43B243D0EF1EDE.mus")]
public void ApplyDiffResult(string fileA, string fileB)
{
byte[] a = File.ReadAllBytes(Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", fileA));
byte[] b = File.ReadAllBytes(Path.Combine(TestContext.CurrentContext.TestDirectory, "Data", fileB));

MyersDiff<byte> d = new MyersDiff<byte>(a, b);
List<byte> result1 = ApplyEditScript(a, b, d);
List<byte> result2 = ApplyResults(d);

Assert.That(result1, Is.EqualTo(b), "Result constructed from edit script must match original file");
Assert.That(result2, Is.EqualTo(b), "Result constructed from result item list must match original file");
}

private static List<byte> ApplyEditScript(byte[] a, byte[] b, MyersDiff<byte> d)
{
List<byte> result = new List<byte>();
Queue<(int LineA, int LineB, int CountA, int CountB)> editScript = new Queue<(int, int, int, int)>(d.GetEditScript());
int currentLine = 0;
while (currentLine < a.Length)
{
if (editScript.Count > 0 && editScript.Peek().LineA == currentLine)
{
(int lineA, int lineB, int countA, int countB) = editScript.Dequeue();
for (int i = 0; i < countB; i++)
{
result.Add(b[lineB + i]);
}

if (countA == 0)
{
result.Add(a[currentLine]);
currentLine++;
}
else
{
currentLine = lineA + countA;
}
}
else
{
result.Add(a[currentLine]);
currentLine++;
}
}

return result;
}

private static List<byte> ApplyResults(MyersDiff<byte> d)
{
List<byte> result = new List<byte>();
foreach ((ResultType resultType, byte aItem, byte bItem) in d.GetResult())
{
switch (resultType)
{
case ResultType.A:
break;
case ResultType.B:
result.Add(bItem);
break;
case ResultType.Both:
Assert.That(aItem, Is.EqualTo(bItem), "When ResultType is Both, the AItem and BItem must be equal");
result.Add(aItem);
break;
default:
break;
}
}

return result;
}
}
}

0 comments on commit 760e5b2

Please sign in to comment.