Skip to content

Commit

Permalink
added unit tests, fixed issue of comparer not being used
Browse files Browse the repository at this point in the history
  • Loading branch information
spkl authored Jan 31, 2021
1 parent 440697a commit 1637315
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/src/bin
/src/obj
/test/bin
/test/obj
*.suo
VSWorkspaceState.json
slnx.sqlite
/src/.vs/Diffs/DesignTimeBuild
.vs
6 changes: 6 additions & 0 deletions src/Diffs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.30907.101
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Diffs", "Diffs.csproj", "{DBB54978-242F-4A05-BAB9-52289BD418D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Diffs.Test", "..\test\Diffs.Test.csproj", "{3CE2D8D3-C4FD-4AFF-BC85-877BE39AB7DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{DBB54978-242F-4A05-BAB9-52289BD418D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBB54978-242F-4A05-BAB9-52289BD418D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBB54978-242F-4A05-BAB9-52289BD418D5}.Release|Any CPU.Build.0 = Release|Any CPU
{3CE2D8D3-C4FD-4AFF-BC85-877BE39AB7DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CE2D8D3-C4FD-4AFF-BC85-877BE39AB7DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CE2D8D3-C4FD-4AFF-BC85-877BE39AB7DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CE2D8D3-C4FD-4AFF-BC85-877BE39AB7DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
34 changes: 17 additions & 17 deletions src/MyersDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,31 @@ public class MyersDiff<T>
/// <param name="aValues">Item sequence A.</param>
/// <param name="bValues">Item sequence B.</param>
public MyersDiff(T[] aValues, T[] bValues)
: this(aValues, bValues, null)
{
this.aValues = aValues;
this.bValues = bValues;
}

/// <summary>
/// Creates a new instance of the <see cref="MyersDiff{T}"/> class
/// and calculates the diff result of sequences A and B
/// using the provided <see cref="IEqualityComparer{T}"/> to determine item equality.
/// </summary>
/// <param name="aValues">Item sequence A.</param>
/// <param name="bValues">Item sequence B.</param>
/// <param name="comparer">The implementation to determine item equality.</param>
public MyersDiff(T[] aValues, T[] bValues, IEqualityComparer<T> comparer)
{
this.aValues = aValues ?? throw new ArgumentNullException(nameof(aValues));
this.bValues = bValues ?? throw new ArgumentNullException(nameof(bValues));
this.comparer = comparer;
this.aRemoved = new bool[this.aValues.Length];
this.bAdded = new bool[this.bValues.Length];

int VMAX = aValues.Length + bValues.Length + 3;

this.Vf = VArray<int>.CreateFromTo(-VMAX, VMAX);
this.Vr = VArray<int>.CreateFromTo(-VMAX, VMAX);

int[] aIndexes = new int[this.aValues.Length];
for (int i = 0; i < aIndexes.Length; i++)
{
Expand All @@ -55,20 +69,6 @@ public MyersDiff(T[] aValues, T[] bValues)
#endif
}

/// <summary>
/// Creates a new instance of the <see cref="MyersDiff{T}"/> class
/// and calculates the diff result of sequences A and B
/// using the provided <see cref="IEqualityComparer{T}"/> to determine item equality.
/// </summary>
/// <param name="aValues">Item sequence A.</param>
/// <param name="bValues">Item sequence B.</param>
/// <param name="comparer">The implementation to determine item equality.</param>
public MyersDiff(T[] aValues, T[] bValues, IEqualityComparer<T> comparer)
: this(aValues, bValues)
{
this.comparer = comparer;
}

private bool AreEqual(int aIndex, int bIndex)
{
if (this.comparer != null)
Expand Down
20 changes: 20 additions & 0 deletions test/Diffs.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net48;netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AssemblyName>spkl.Diffs.Test</AssemblyName>
<RootNamespace>spkl.Diffs.Test</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Diffs.csproj" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions test/MyersDiffClass.ReferenceCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace spkl.Diffs.Test
{
public partial class MyersDiffClass
{
public class ReferenceCase
{
public string[] A { get; set; }

public string AString
{
set
{
this.A = value.Split(',');
}
}

public string[] B { get; set; }

public string BString
{
set
{
this.B = value.Split(',');
}
}

public (ResultType ResultType, string AItem, string BItem)[] Result { get; set; }

public (int LineA, int LineB, int CountA, int CountB)[] EditScript { get; set; }

public override string ToString()
{
return $"{string.Join(",", this.A)} / {string.Join(",", this.B)}";
}
}
}
}
Loading

0 comments on commit 1637315

Please sign in to comment.