-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from ColmBhandal/feature/array-compare
Feature/array compare
- Loading branch information
Showing
13 changed files
with
560 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace CsharpExtras.Compare.Array | ||
{ | ||
internal class ArrayComparisonResultImpl<TVal> : IComparisonResult | ||
{ | ||
private readonly int _indexBase; | ||
public bool IsEqual => MessageAndIsEqual.isEqual; | ||
public string Message => MessageAndIsEqual.message; | ||
|
||
private (string, bool)? _messageAndIsEqual; | ||
private (string message, bool isEqual) MessageAndIsEqual => _messageAndIsEqual ??= GetMessageAndIsEqual(); | ||
|
||
private IEnumerable<int> ThisShape { get; } | ||
private IEnumerable<int> OtherShape { get; } | ||
|
||
private (IEnumerable<int>, TVal)? FirstMismatchZeroBased { get; } | ||
|
||
private string? OtherValMistmachOrNull { get; } | ||
|
||
public ArrayComparisonResultImpl(IEnumerable<int> thisShape, | ||
IEnumerable<int> otherShape, | ||
int indexBase, | ||
(IEnumerable<int>, TVal)? firstMismatch, | ||
string? otherValMistmachOrNull) | ||
{ | ||
ThisShape = thisShape ?? throw new ArgumentNullException(nameof(thisShape)); | ||
OtherShape = otherShape ?? throw new ArgumentNullException(nameof(otherShape)); | ||
FirstMismatchZeroBased = firstMismatch; | ||
OtherValMistmachOrNull = otherValMistmachOrNull; | ||
_indexBase = indexBase; | ||
} | ||
|
||
private (string message, bool isEqual) GetMessageAndIsEqual() | ||
{ | ||
if (!Enumerable.SequenceEqual(ThisShape, OtherShape)) | ||
{ | ||
return ($"Shape mismatch. This array has shape {PrintTuple(ThisShape)}. " + | ||
$"Other array has shape {PrintTuple(OtherShape)}.", | ||
false); | ||
} | ||
if (FirstMismatchZeroBased is (IEnumerable<int> k, TVal v)) | ||
{ | ||
string otherVal = OtherValMistmachOrNull ?? "NOT FOUND"; | ||
return ($"Mismatch found at indices {PrintTuple(k.Select(i => i+_indexBase))}. " + | ||
$"This value: {v}. Other value: {otherVal}", false); | ||
} | ||
return ("Arrays are equal", true); | ||
} | ||
|
||
private string PrintTuple(IEnumerable<int> tuple) | ||
{ | ||
return string.Join(",", tuple.Select(i => i.ToString())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.