From 177df12c36ed23c2705af7b10cc6ac36a9c9aa73 Mon Sep 17 00:00:00 2001 From: cbhandal Date: Fri, 4 Mar 2022 18:15:01 +0000 Subject: [PATCH 01/10] Stub Compare methods Also tidied up some existing code --- .../Array/ArrayComparisonResultImpl.cs | 14 +++++++ .../Enumerable/OneBased/IOneBasedArray.cs | 2 + .../Enumerable/OneBased/OneBasedArray.cs | 8 ++++ .../Enumerable/OneBased/OneBasedArray2D.cs | 42 ++++++++++++------- CsharpExtras/Extensions/ArrayExtension.cs | 42 +++++++++++-------- CsharpExtras/Extensions/ArrayExtension2D.cs | 12 +++++- 6 files changed, 86 insertions(+), 34 deletions(-) create mode 100644 CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs diff --git a/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs new file mode 100644 index 0000000..b2ec8e9 --- /dev/null +++ b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CsharpExtras.Compare.Array +{ + //TODO: Implement + internal class ArrayComparisonResultImpl : IComparisonResult + { + public bool IsEqual => false; + + public string Message => "TODO"; + } +} diff --git a/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs index 7bb2c4d..4583d14 100644 --- a/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs @@ -4,6 +4,7 @@ using static CsharpExtras.Extensions.ArrayOrientationClass; using static CsharpExtras.Extensions.ArrayExtension; using CsharpExtras.Map.Dictionary.Collection; +using CsharpExtras.Compare; namespace CsharpExtras._Enumerable.OneBased { @@ -196,5 +197,6 @@ public interface IOneBasedArray : IEnumerable /// and the values of which are the result of applying the zipper across all values at the corresponding indices /// in all the arrays. IOneBasedArray ZipEnum(Func, TResult> zipper, IEnumerable> others); + IComparisonResult Compare(IOneBasedArray other); } } diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs index 04e1244..b2f4c7b 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs @@ -6,6 +6,8 @@ using static CsharpExtras.Extensions.ArrayOrientationClass; using CsharpExtras.Map.Dictionary.Collection; using System.Linq; +using CsharpExtras.Compare; +using CsharpExtras.Compare.Array; namespace CsharpExtras._Enumerable.OneBased { @@ -40,6 +42,12 @@ public TVal this[int oneBasedIndex] public TVal[] ZeroBasedEquivalent => _backingArray; + public IComparisonResult Compare(IOneBasedArray other) + { + //TODO: Implement + return new ArrayComparisonResultImpl(); + } + public IEnumerator GetEnumerator() { for(int i = 0; i < Length; i++) diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs index 4121e6e..13cc23b 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs @@ -1,4 +1,6 @@ -using CsharpExtras.Extensions; +using CsharpExtras.Compare; +using CsharpExtras.Compare.Array; +using CsharpExtras.Extensions; using System; using System.Collections; using System.Collections.Generic; @@ -8,27 +10,16 @@ namespace CsharpExtras._Enumerable.OneBased { class OneBasedArray2DImpl : IOneBasedArray2D { - public int LastUsedRow(Predicate isUsed) - { - int zeroBasedRow = ZeroBasedEquivalent.LastUsedRow(isUsed); - if (zeroBasedRow < 0) return zeroBasedRow; - return zeroBasedRow + 1; - } - public int LastUsedColumn(Predicate isUsed) - { - int zeroBasedColumn = ZeroBasedEquivalent.LastUsedColumn(isUsed); - if (zeroBasedColumn < 0) return zeroBasedColumn; - return zeroBasedColumn + 1; - } + public TVal[,] ZeroBasedEquivalent { get; } - public OneBasedArray2DImpl(TVal[,] backingArray) + public OneBasedArray2DImpl(int rows, int columns) : this(new TVal[rows, columns]) { - ZeroBasedEquivalent = backingArray; } - public OneBasedArray2DImpl(int rows, int columns) : this(new TVal[rows, columns]) + public OneBasedArray2DImpl(TVal[,] backingArray) { + ZeroBasedEquivalent = backingArray; } public TVal this[int oneBasedIndex0, int oneBasedIndex1] @@ -45,6 +36,25 @@ public OneBasedArray2DImpl(int rows, int columns) : this(new TVal[rows, columns] } } + public IComparisonResult Compare(IOneBasedArray other) + { + //TODO: Implement + return new ArrayComparisonResultImpl(); + } + + public int LastUsedRow(Predicate isUsed) + { + int zeroBasedRow = ZeroBasedEquivalent.LastUsedRow(isUsed); + if (zeroBasedRow < 0) return zeroBasedRow; + return zeroBasedRow + 1; + } + public int LastUsedColumn(Predicate isUsed) + { + int zeroBasedColumn = ZeroBasedEquivalent.LastUsedColumn(isUsed); + if (zeroBasedColumn < 0) return zeroBasedColumn; + return zeroBasedColumn + 1; + } + private void ValidateIndices(int oneBasedIndex0, int oneBasedIndex1) { int len0 = ZeroBasedEquivalent.GetLength(0); diff --git a/CsharpExtras/Extensions/ArrayExtension.cs b/CsharpExtras/Extensions/ArrayExtension.cs index 51ebad3..471f01a 100644 --- a/CsharpExtras/Extensions/ArrayExtension.cs +++ b/CsharpExtras/Extensions/ArrayExtension.cs @@ -1,4 +1,6 @@ -using CsharpExtras.Map.Dictionary; +using CsharpExtras.Compare; +using CsharpExtras.Compare.Array; +using CsharpExtras.Map.Dictionary; using CsharpExtras.Map.Dictionary.Collection; using System; using System.Collections.Generic; @@ -9,9 +11,15 @@ namespace CsharpExtras.Extensions { public static class ArrayExtension { + public static IComparisonResult Compare(this TVal[] arr, TVal[] other, Func isEqualValues) + { + //TODO: Implement + return new ArrayComparisonResultImpl(); + } + //Non-mvp: Test this /// A pair indicating the first element found and its index, or (-1, default) if nothing found. - public static (int index, T element)? FindFirstOccurrenceOfSet(this T[] arr, ISet set) + public static (int index, TVal element)? FindFirstOccurrenceOfSet(this TVal[] arr, ISet set) { return FindFirstOccurrenceOfSet(arr, set, 0, arr.Length); } @@ -20,11 +28,11 @@ public static (int index, T element)? FindFirstOccurrenceOfSet(this T[] arr, /// Start searching the array from this index inclusive i.e. don't look at lower indices /// Stop searching the array beyond this index, and don't include this index in the search /// A pair indicating the first element found and its index, or null if nothing found. - public static (int index, T element)? FindFirstOccurrenceOfSet(this T[] arr, ISet set, int startIndex, int endIndex) + public static (int index, TVal element)? FindFirstOccurrenceOfSet(this TVal[] arr, ISet set, int startIndex, int endIndex) { for (int i = startIndex; i < arr.Length && i < endIndex; i++) { - T element = arr[i]; + TVal element = arr[i]; if (set.Contains(element)) { return (i, element); @@ -40,18 +48,18 @@ public static (int index, T element)? FindFirstOccurrenceOfSet(this T[] arr, /// Index to start at (inclusive). Negative indices will be truncated to zero. /// Index before which to stop. Indices greater than array length will be truncated to the array length. /// - public static T[] SubArray(this T[] data, int startAt, int stopBefore) + public static TVal[] SubArray(this TVal[] data, int startAt, int stopBefore) { startAt = Math.Max(startAt, 0); stopBefore = Math.Min(stopBefore, data.Length); int length = stopBefore - startAt; - T[] result = new T[length]; + TVal[] result = new TVal[length]; Array.Copy(data, startAt, result, 0, length); return result; } - public static T[] SubArray(this T[] data, int startAt) + public static TVal[] SubArray(this TVal[] data, int startAt) { return data.SubArray(startAt, data.Length); } @@ -67,16 +75,16 @@ public static string[] RemoveBlankEntries(this string[] data) /// If ROW, then the 2D array created will have a single row, /// whose values will match that of the source array. Else the 2D array will have a single column, matching the source array. /// A new 2D array whose values match that of the original 1D array and which is oriented according to the given orientation. - public static T[,] To2DArray(this T[] array, ArrayOrientation orientation) + public static TVal[,] To2DArray(this TVal[] array, ArrayOrientation orientation) { - T[,] outputArray; + TVal[,] outputArray; if (orientation == ArrayOrientation.COLUMN) { - outputArray = new T[array.Length, 1]; + outputArray = new TVal[array.Length, 1]; } else { - outputArray = new T[1, array.Length]; + outputArray = new TVal[1, array.Length]; } for (int i = 0; i < array.Length; i++) @@ -94,21 +102,21 @@ public static string[] RemoveBlankEntries(this string[] data) } //TODO: Test - public static T[] DeepCopy(this T[] array) + public static TVal[] DeepCopy(this TVal[] array) { int length = array.Length; - T[] copy = new T[length]; + TVal[] copy = new TVal[length]; array.CopyTo(copy, 0); return copy; } - public static IDictionary> Inverse(this T[] array) + public static IDictionary> Inverse(this TVal[] array) { - IDictionary> inverseMap = new Dictionary>(); + IDictionary> inverseMap = new Dictionary>(); for (int index = 0; index < array.Length; index++) { - T value = array[index]; + TVal value = array[index]; if (inverseMap.ContainsKey(value)) { inverseMap[value].Add(index); @@ -122,7 +130,7 @@ public static IDictionary> Inverse(this T[] array) return inverseMap; } - public static IDictionary> FindDuplicateIndices(this T[] array) + public static IDictionary> FindDuplicateIndices(this TVal[] array) { return array.Inverse().FilterValues(lst => lst.Count > 1); } diff --git a/CsharpExtras/Extensions/ArrayExtension2D.cs b/CsharpExtras/Extensions/ArrayExtension2D.cs index fd9822b..420274b 100644 --- a/CsharpExtras/Extensions/ArrayExtension2D.cs +++ b/CsharpExtras/Extensions/ArrayExtension2D.cs @@ -1,4 +1,6 @@ -using System; +using CsharpExtras.Compare; +using CsharpExtras.Compare.Array; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; @@ -9,6 +11,14 @@ namespace CsharpExtras.Extensions public static class ArrayExtension2D { + + public static IComparisonResult Compare(this TVAl[,] arr, TVAl[,] other, + Func isEqualValues) + { + //TODO: Implement + return new ArrayComparisonResultImpl(); + } + /// A predicate on an entire column /// The last column where the predicate is true, or -1 if none found public static int LastColumnWhere(this TVal[,] array, Predicate p) From b7c727c495d6c502e1f479e82ad76429cad82d73 Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 11:00:48 +0000 Subject: [PATCH 02/10] 1d zero based tests --- .../Extensions/ArrayExtensionTest.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/CsharpExtrasTest/Extensions/ArrayExtensionTest.cs b/CsharpExtrasTest/Extensions/ArrayExtensionTest.cs index b1efb14..d1aaafd 100644 --- a/CsharpExtrasTest/Extensions/ArrayExtensionTest.cs +++ b/CsharpExtrasTest/Extensions/ArrayExtensionTest.cs @@ -6,12 +6,86 @@ using System.Collections.Generic; using System.Linq; using CsharpExtras.Map.Dictionary.Collection; +using CsharpExtras.Compare; namespace CsharpExtrasTest.Extensions { [TestFixture, Category("Unit")] public class ArrayExtensionTest { + [Test, TestCaseSource(nameof(ProviderForUnequalCompare))] + public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse + ((double[] array1, double[] array2, string label) testData) + { + //Act + IComparisonResult result = testData.array1.Compare(testData.array2, + (d1, d2) => d1 == d2); + + //Assert + Assert.IsFalse(result.IsEqual); + } + + private static IEnumerable<(double[] array1, double[] array2, string label)> + ProviderForUnequalCompare() + { + return new List<(double[] array1, double[] array2, string label)> + { + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {1.1, 0.501, 6.01}, + "Same length, different value" + ), + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {1.1, 0.5}, + "Different length, other a prefix" + ), + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {0.5, 6.01}, + "Different length, other a suffix" + ), + ( + new double[] {1.1, 0.5}, + new double[] {1.1, 0.5, 6.01}, + "Different length, this a prefix" + ), + ( + new double[] {0.5, 6.01}, + new double[] {1.1, 0.5, 6.01}, + "Different length, this a suffix" + ), + }; + } + + [Test] + public void GIVEN_DistinctEqualPopulatedArrays_WHEN_Compare_THEN_IsEqualTrue() + { + //Arrange + string[] array1 = new string[] {"Hello", "World", "42" }; + string[] array2 = new string[] { "Hello", "World", "42" }; + + //Act + IComparisonResult result = array1.Compare(array2, string.Equals); + + //Assert + Assert.IsTrue(result.IsEqual); + } + + [Test] + public void GIVEN_DistinctEmptyArrays_WHEN_CompareWithFalseValueComparer_THEN_IsEqualTrue() + { + //Arrange + string[] array1 = new string[] {}; + string[] array2 = new string[] { }; + + //Act + IComparisonResult result = array1.Compare(array2, (s1, s2) => false); + + //Assert + Assert.IsTrue(result.IsEqual, "Distinct empty arrays should be equal, even if the comparison funciton always returns false"); + } + [Test] public void GIVEN_ArrayAndEmptyOthers_WHEN_ZipEnumIgnoringOthers_THEN_ResultIsAsExpected() { From ac99cd9f88765d4fbfe64a258c8b20a10a4d20aa Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 11:14:46 +0000 Subject: [PATCH 03/10] 2D Zero-based compare --- .../Extensions/ArrayExtension2DTest.cs | 95 ++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs index 6dc994a..c1d79d5 100644 --- a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs +++ b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs @@ -1,4 +1,5 @@ -using CsharpExtras.Extensions; +using CsharpExtras.Compare; +using CsharpExtras.Extensions; using NUnit.Framework; using System; using System.Collections.Generic; @@ -9,6 +10,98 @@ namespace CsharpExtrasTest.Extensions [TestFixture, Category("Unit")] public class ArrayExtension2DTest { + [Test, TestCaseSource(nameof(ProviderForUnequalCompare))] + public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse + ((double[,] array1, double[,] array2, string label) testData) + { + //Act + IComparisonResult result = testData.array1.Compare(testData.array2, + (d1, d2) => d1 == d2); + + //Assert + Assert.IsFalse(result.IsEqual); + } + + private static IEnumerable<(double[,] array1, double[,] array2, string label)> + ProviderForUnequalCompare() + { + return new List<(double[,] array1, double[,] array2, string label)> + { + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + "Same shape, different value" + ), + + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2, 3}, + }, + "Different shape, other less rows" + ), + + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2}, + {1.1, 0.5}, + }, + "Different shape, other less columns" + ) + }; + } + + [Test] + public void GIVEN_DistinctEqualPopulatedArrays_WHEN_Compare_THEN_IsEqualTrue() + { + //Arrange + string[,] array1 = new string[,]{ + { "Hello", "World", "42" }, + { "Goodbye", "Universe", "43" } + }; + string[,] array2 = new string[,]{ + { "Hello", "World", "42" }, + { "Goodbye", "Universe", "43" } + }; + //Act + IComparisonResult result = array1.Compare(array2, string.Equals); + + //Assert + Assert.IsTrue(result.IsEqual); + } + + [Test] + public void GIVEN_DistinctEmptyArrays_WHEN_CompareWithFalseValueComparer_THEN_IsEqualTrue() + { + //Arrange + string[,] array1 = new string[,] { { } }; + string[,] array2 = new string[,] { { } }; + + //Act + IComparisonResult result = array1.Compare(array2, (s1, s2) => false); + + //Assert + Assert.IsTrue(result.IsEqual, "Distinct empty arrays should be equal, even if the comparison funciton always returns false"); + } [Test] public void GIVEN_ExceptionInZipAndEmptyArray_WHEN_ZipEnum_THEN_ResultIsEmpty() { From 7dc3c993a982479ee12c21d5f5ca068d647a4ce1 Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 11:22:03 +0000 Subject: [PATCH 04/10] one-baed 1D Compare tests --- .../Enumerable/OneBased/IOneBasedArray.cs | 10 ++- .../Enumerable/OneBased/OneBasedArray.cs | 2 +- .../Enumerable/OneBased/OneBasedArrayTest.cs | 82 ++++++++++++++++++- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs index 4583d14..d51afa3 100644 --- a/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/IOneBasedArray.cs @@ -197,6 +197,14 @@ public interface IOneBasedArray : IEnumerable /// and the values of which are the result of applying the zipper across all values at the corresponding indices /// in all the arrays. IOneBasedArray ZipEnum(Func, TResult> zipper, IEnumerable> others); - IComparisonResult Compare(IOneBasedArray other); + + /// + /// Compares this array to the other array value-by-value + /// + /// The other array against which to compare + /// A function which checks if values are equal + /// The result of the comparison, which can be used to determine if the arrays are equal according to the value-equality function passed. + /// If the arrays are of a different shape, then the comparison will always be unequal + IComparisonResult Compare(IOneBasedArray other, Func isEqualValues); } } diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs index b2f4c7b..6a9aa41 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs @@ -42,7 +42,7 @@ public TVal this[int oneBasedIndex] public TVal[] ZeroBasedEquivalent => _backingArray; - public IComparisonResult Compare(IOneBasedArray other) + public IComparisonResult Compare(IOneBasedArray other, Func isEqualValues) { //TODO: Implement return new ArrayComparisonResultImpl(); diff --git a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArrayTest.cs b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArrayTest.cs index c3633fa..52551b1 100644 --- a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArrayTest.cs +++ b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArrayTest.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using CsharpExtras.Extensions; +using CsharpExtras.Compare; namespace CsharpExtrasTest._Enumerable.OneBased { @@ -18,7 +19,86 @@ public class OneBasedArrayTest private const string Four = "Four"; private readonly ICsharpExtrasApi _api = new CsharpExtrasApi(); - + + [Test, TestCaseSource(nameof(ProviderForUnequalCompare))] + public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse + ((double[] array1Raw, double[] array2Raw, string label) testData) + { + //Arrange + IOneBasedArray array1 = _api.NewOneBasedArray(testData.array1Raw); + IOneBasedArray array2 = _api.NewOneBasedArray(testData.array2Raw); + + //Act + IComparisonResult result = array1.Compare(array2, + (d1, d2) => d1 == d2); + + //Assert + Assert.IsFalse(result.IsEqual); + } + + private static IEnumerable<(double[] array1, double[] array2, string label)> + ProviderForUnequalCompare() + { + return new List<(double[] array1, double[] array2, string label)> + { + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {1.1, 0.501, 6.01}, + "Same length, different value" + ), + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {1.1, 0.5}, + "Different length, other a prefix" + ), + ( + new double[] {1.1, 0.5, 6.01}, + new double[] {0.5, 6.01}, + "Different length, other a suffix" + ), + ( + new double[] {1.1, 0.5}, + new double[] {1.1, 0.5, 6.01}, + "Different length, this a prefix" + ), + ( + new double[] {0.5, 6.01}, + new double[] {1.1, 0.5, 6.01}, + "Different length, this a suffix" + ), + }; + } + + [Test] + public void GIVEN_DistinctEqualPopulatedArrays_WHEN_Compare_THEN_IsEqualTrue() + { + //Arrange + IOneBasedArray array1 = _api.NewOneBasedArray( + new string[] { "Hello", "World", "42" }); + IOneBasedArray array2 = _api.NewOneBasedArray( + new string[] { "Hello", "World", "42" }); + + //Act + IComparisonResult result = array1.Compare(array2, string.Equals); + + //Assert + Assert.IsTrue(result.IsEqual); + } + + [Test] + public void GIVEN_DistinctEmptyArrays_WHEN_CompareWithFalseValueComparer_THEN_IsEqualTrue() + { + //Arrange + IOneBasedArray array1 = _api.NewOneBasedArray(0); + IOneBasedArray array2 = _api.NewOneBasedArray(0); + + //Act + IComparisonResult result = array1.Compare(array2, (s1, s2) => false); + + //Assert + Assert.IsTrue(result.IsEqual, "Distinct empty arrays should be equal, even if the comparison funciton always returns false"); + } + [Test] public void GIVEN_ExceptionInZipAndEmptyArray_WHEN_ZipEnum_THEN_ResultIsEmpty() { From 591431dbe74a32f9473531e81cb76596d6bc9284 Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 11:28:31 +0000 Subject: [PATCH 05/10] 2d one-based tests --- .../Enumerable/OneBased/IOneBasedArray2D.cs | 12 ++- .../Enumerable/OneBased/OneBasedArray2D.cs | 3 +- CsharpExtras/Extensions/ArrayExtension2D.cs | 4 +- .../OneBased/OneBasedArray2DTest.cs | 98 +++++++++++++++++++ .../Extensions/ArrayExtension2DTest.cs | 1 + 5 files changed, 114 insertions(+), 4 deletions(-) diff --git a/CsharpExtras/Enumerable/OneBased/IOneBasedArray2D.cs b/CsharpExtras/Enumerable/OneBased/IOneBasedArray2D.cs index 2e646aa..553de84 100644 --- a/CsharpExtras/Enumerable/OneBased/IOneBasedArray2D.cs +++ b/CsharpExtras/Enumerable/OneBased/IOneBasedArray2D.cs @@ -1,4 +1,5 @@ -using System; +using CsharpExtras.Compare; +using System; using System.Collections.Generic; namespace CsharpExtras._Enumerable.OneBased @@ -81,5 +82,14 @@ public interface IOneBasedArray2D : IEnumerable /// and the values of which are the result of applying the zipper across all values at the corresponding indices /// in all the arrays. IOneBasedArray2D ZipEnum(Func, TResult> zipper, IEnumerable> others); + + /// + /// Compares this array to the other array value-by-value + /// + /// The other array against which to compare + /// A function which checks if values are equal + /// The result of the comparison, which can be used to determine if the arrays are equal according to the value-equality function passed. + /// If the arrays are of a different shape, then the comparison will always be unequal + IComparisonResult Compare(IOneBasedArray2D other, Func isEqualValues); } } diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs index 13cc23b..d9e931b 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs @@ -36,7 +36,8 @@ public OneBasedArray2DImpl(TVal[,] backingArray) } } - public IComparisonResult Compare(IOneBasedArray other) + public IComparisonResult Compare(IOneBasedArray2D other, + Func isEqualValues) { //TODO: Implement return new ArrayComparisonResultImpl(); diff --git a/CsharpExtras/Extensions/ArrayExtension2D.cs b/CsharpExtras/Extensions/ArrayExtension2D.cs index 420274b..fc05e10 100644 --- a/CsharpExtras/Extensions/ArrayExtension2D.cs +++ b/CsharpExtras/Extensions/ArrayExtension2D.cs @@ -12,8 +12,8 @@ public static class ArrayExtension2D { - public static IComparisonResult Compare(this TVAl[,] arr, TVAl[,] other, - Func isEqualValues) + public static IComparisonResult Compare(this TVal[,] arr, TVal[,] other, + Func isEqualValues) { //TODO: Implement return new ArrayComparisonResultImpl(); diff --git a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs index 96c00f7..4a5fc7b 100644 --- a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs +++ b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs @@ -6,6 +6,7 @@ using static CsharpExtras.Extensions.ArrayExtension; using System.Collections.Generic; using System.Linq; +using CsharpExtras.Compare; namespace CsharpExtrasTest._Enumerable.OneBased { @@ -19,6 +20,103 @@ public class OneBasedArray2DTest private readonly ICsharpExtrasApi _api = new CsharpExtrasApi(); + [Test, TestCaseSource(nameof(ProviderForUnequalCompare))] + public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse + ((double[,] array1, double[,] array2, string label) testData) + { + //Arrange + IOneBasedArray2D array1 = _api.NewOneBasedArray2D(testData.array1); + IOneBasedArray2D array2 = _api.NewOneBasedArray2D(testData.array2); + + //Act + IComparisonResult result = array1.Compare(array2, + (d1, d2) => d1 == d2); + + //Assert + Assert.IsFalse(result.IsEqual); + } + + private static IEnumerable<(double[,] array1, double[,] array2, string label)> + ProviderForUnequalCompare() + { + return new List<(double[,] array1, double[,] array2, string label)> + { + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + "Same shape, different value" + ), + + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2, 3}, + }, + "Different shape, other less rows" + ), + + ( + new double[,] + { + {1, 2, 3}, + {1.1, 0.5, 6.01}, + }, + new double[,] + { + {1, 2}, + {1.1, 0.5}, + }, + "Different shape, other less columns" + ) + }; + } + + [Test] + public void GIVEN_DistinctEqualPopulatedArrays_WHEN_Compare_THEN_IsEqualTrue() + { + //Arrange + IOneBasedArray2D array1 = _api.NewOneBasedArray2D(new string[,]{ + { "Hello", "World", "42" }, + { "Goodbye", "Universe", "43" } + }); + IOneBasedArray2D array2 = _api.NewOneBasedArray2D(new string[,]{ + { "Hello", "World", "42" }, + { "Goodbye", "Universe", "43" } + }); + //Act + IComparisonResult result = array1.Compare(array2, string.Equals); + + //Assert + Assert.IsTrue(result.IsEqual); + } + + [Test] + public void GIVEN_DistinctEmptyArrays_WHEN_CompareWithFalseValueComparer_THEN_IsEqualTrue() + { + //Arrange + IOneBasedArray2D array1 = _api.NewOneBasedArray2D(0, 0); + IOneBasedArray2D array2 = _api.NewOneBasedArray2D(0, 0); + + //Act + IComparisonResult result = array1.Compare(array2, (s1, s2) => false); + + //Assert + Assert.IsTrue(result.IsEqual, "Distinct empty arrays should be equal, even if the comparison funciton always returns false"); + } + [Test] public void GIVEN_ArrayAndEmptyOthers_WHEN_ZipEnumIgnoringOthers_THEN_ResultIsAsExpected() { diff --git a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs index c1d79d5..99dc7a2 100644 --- a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs +++ b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs @@ -102,6 +102,7 @@ public void GIVEN_DistinctEmptyArrays_WHEN_CompareWithFalseValueComparer_THEN_Is //Assert Assert.IsTrue(result.IsEqual, "Distinct empty arrays should be equal, even if the comparison funciton always returns false"); } + [Test] public void GIVEN_ExceptionInZipAndEmptyArray_WHEN_ZipEnum_THEN_ResultIsEmpty() { From 7ee1e26011a9ecbdd975ea6cfeeb457ba4ab079f Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 11:53:12 +0000 Subject: [PATCH 06/10] Implemented array comparison result --- .../Array/ArrayComparisonResultImpl.cs | 50 +++++++++++++++++-- .../Enumerable/OneBased/OneBasedArray.cs | 2 +- .../Enumerable/OneBased/OneBasedArray2D.cs | 2 +- CsharpExtras/Extensions/ArrayExtension.cs | 2 +- CsharpExtras/Extensions/ArrayExtension2D.cs | 2 +- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs index b2ec8e9..9b7993d 100644 --- a/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs +++ b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs @@ -1,14 +1,56 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace CsharpExtras.Compare.Array { - //TODO: Implement - internal class ArrayComparisonResultImpl : IComparisonResult + internal class ArrayComparisonResultImpl : IComparisonResult { - public bool IsEqual => false; + public bool IsEqual => MessageAndIsEqual.isEqual; + public string Message => MessageAndIsEqual.message; - public string Message => "TODO"; + private (string, bool)? _messageAndIsEqual; + private (string message, bool isEqual) MessageAndIsEqual => _messageAndIsEqual ??= GetMessageAndIsEqual(); + + private IEnumerable ThisShape { get; } + private IEnumerable OtherShape { get; } + + private (IEnumerable, TVal)? FirstMismatch { get; } + + private string? OtherValMistmachOrNull { get; } + + public ArrayComparisonResultImpl(IEnumerable thisShape, + IEnumerable otherShape, + (IEnumerable, TVal)? firstMismatch, + string? otherValMistmachOrNull) + { + ThisShape = thisShape ?? throw new ArgumentNullException(nameof(thisShape)); + OtherShape = otherShape ?? throw new ArgumentNullException(nameof(otherShape)); + FirstMismatch = firstMismatch; + OtherValMistmachOrNull = otherValMistmachOrNull; + } + + 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 (FirstMismatch is (IEnumerable k, TVal v)) + { + string otherVal = OtherValMistmachOrNull ?? "NOT FOUND"; + return ($"Mismatch found at indices {PrintTuple(k)}. " + + $"This value: {v}. Other value: {otherVal}", false); + } + return ("Arrays are equal", true); + } + + private string PrintTuple(IEnumerable tuple) + { + return string.Join(",", tuple.Select(i => i.ToString())); + } } } diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs index 6a9aa41..4d207f3 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs @@ -45,7 +45,7 @@ public TVal this[int oneBasedIndex] public IComparisonResult Compare(IOneBasedArray other, Func isEqualValues) { //TODO: Implement - return new ArrayComparisonResultImpl(); + return new ArrayComparisonResultImpl(); } public IEnumerator GetEnumerator() diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs index d9e931b..7db80ac 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs @@ -40,7 +40,7 @@ public IComparisonResult Compare(IOneBasedArray2D other, Func isEqualValues) { //TODO: Implement - return new ArrayComparisonResultImpl(); + return new ArrayComparisonResultImpl(); } public int LastUsedRow(Predicate isUsed) diff --git a/CsharpExtras/Extensions/ArrayExtension.cs b/CsharpExtras/Extensions/ArrayExtension.cs index 471f01a..241bc97 100644 --- a/CsharpExtras/Extensions/ArrayExtension.cs +++ b/CsharpExtras/Extensions/ArrayExtension.cs @@ -14,7 +14,7 @@ public static class ArrayExtension public static IComparisonResult Compare(this TVal[] arr, TVal[] other, Func isEqualValues) { //TODO: Implement - return new ArrayComparisonResultImpl(); + return new ArrayComparisonResultImpl(); } //Non-mvp: Test this diff --git a/CsharpExtras/Extensions/ArrayExtension2D.cs b/CsharpExtras/Extensions/ArrayExtension2D.cs index fc05e10..d7b38db 100644 --- a/CsharpExtras/Extensions/ArrayExtension2D.cs +++ b/CsharpExtras/Extensions/ArrayExtension2D.cs @@ -16,7 +16,7 @@ public static IComparisonResult Compare(this TVal[,] arr, TVal[,] other, Func isEqualValues) { //TODO: Implement - return new ArrayComparisonResultImpl(); + return new ArrayComparisonResultImpl(); } /// A predicate on an entire column From df69b1c91e317125a3cf2dd141bf04e973928d5f Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 13:20:04 +0000 Subject: [PATCH 07/10] Reusable compare for one/zero-based versions Stub for now --- .../Array/ArrayComparisonResultImpl.cs | 11 +++-- .../Enumerable/OneBased/OneBasedArray.cs | 6 +-- .../Enumerable/OneBased/OneBasedArray2D.cs | 8 ++-- CsharpExtras/Extensions/ArrayExtension.cs | 6 +-- CsharpExtras/Extensions/ArrayExtension2D.cs | 9 +--- .../Extensions/Helper/ArrayHelperFunc.cs | 47 +++++++++++++++++++ 6 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs diff --git a/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs index 9b7993d..b8aa600 100644 --- a/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs +++ b/CsharpExtras/Compare/Array/ArrayComparisonResultImpl.cs @@ -7,6 +7,7 @@ namespace CsharpExtras.Compare.Array { internal class ArrayComparisonResultImpl : IComparisonResult { + private readonly int _indexBase; public bool IsEqual => MessageAndIsEqual.isEqual; public string Message => MessageAndIsEqual.message; @@ -16,19 +17,21 @@ internal class ArrayComparisonResultImpl : IComparisonResult private IEnumerable ThisShape { get; } private IEnumerable OtherShape { get; } - private (IEnumerable, TVal)? FirstMismatch { get; } + private (IEnumerable, TVal)? FirstMismatchZeroBased { get; } private string? OtherValMistmachOrNull { get; } public ArrayComparisonResultImpl(IEnumerable thisShape, IEnumerable otherShape, + int indexBase, (IEnumerable, TVal)? firstMismatch, string? otherValMistmachOrNull) { ThisShape = thisShape ?? throw new ArgumentNullException(nameof(thisShape)); OtherShape = otherShape ?? throw new ArgumentNullException(nameof(otherShape)); - FirstMismatch = firstMismatch; + FirstMismatchZeroBased = firstMismatch; OtherValMistmachOrNull = otherValMistmachOrNull; + _indexBase = indexBase; } private (string message, bool isEqual) GetMessageAndIsEqual() @@ -39,10 +42,10 @@ public ArrayComparisonResultImpl(IEnumerable thisShape, $"Other array has shape {PrintTuple(OtherShape)}.", false); } - if (FirstMismatch is (IEnumerable k, TVal v)) + if (FirstMismatchZeroBased is (IEnumerable k, TVal v)) { string otherVal = OtherValMistmachOrNull ?? "NOT FOUND"; - return ($"Mismatch found at indices {PrintTuple(k)}. " + + return ($"Mismatch found at indices {PrintTuple(k.Select(i => i+_indexBase))}. " + $"This value: {v}. Other value: {otherVal}", false); } return ("Arrays are equal", true); diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs index 4d207f3..3479c44 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray.cs @@ -8,6 +8,7 @@ using System.Linq; using CsharpExtras.Compare; using CsharpExtras.Compare.Array; +using CsharpExtras.Extensions.Helper; namespace CsharpExtras._Enumerable.OneBased { @@ -43,10 +44,7 @@ public TVal this[int oneBasedIndex] public TVal[] ZeroBasedEquivalent => _backingArray; public IComparisonResult Compare(IOneBasedArray other, Func isEqualValues) - { - //TODO: Implement - return new ArrayComparisonResultImpl(); - } + => ZeroBasedEquivalent.Compare(other.ZeroBasedEquivalent, isEqualValues, 1); public IEnumerator GetEnumerator() { diff --git a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs index 7db80ac..c6dc517 100644 --- a/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs +++ b/CsharpExtras/Enumerable/OneBased/OneBasedArray2D.cs @@ -1,6 +1,7 @@ using CsharpExtras.Compare; using CsharpExtras.Compare.Array; using CsharpExtras.Extensions; +using CsharpExtras.Extensions.Helper; using System; using System.Collections; using System.Collections.Generic; @@ -37,11 +38,8 @@ public OneBasedArray2DImpl(TVal[,] backingArray) } public IComparisonResult Compare(IOneBasedArray2D other, - Func isEqualValues) - { - //TODO: Implement - return new ArrayComparisonResultImpl(); - } + Func isEqualValues) => + ZeroBasedEquivalent.Compare(other.ZeroBasedEquivalent, isEqualValues, 1); public int LastUsedRow(Predicate isUsed) { diff --git a/CsharpExtras/Extensions/ArrayExtension.cs b/CsharpExtras/Extensions/ArrayExtension.cs index 241bc97..003b718 100644 --- a/CsharpExtras/Extensions/ArrayExtension.cs +++ b/CsharpExtras/Extensions/ArrayExtension.cs @@ -1,5 +1,6 @@ using CsharpExtras.Compare; using CsharpExtras.Compare.Array; +using CsharpExtras.Extensions.Helper; using CsharpExtras.Map.Dictionary; using CsharpExtras.Map.Dictionary.Collection; using System; @@ -12,10 +13,7 @@ namespace CsharpExtras.Extensions public static class ArrayExtension { public static IComparisonResult Compare(this TVal[] arr, TVal[] other, Func isEqualValues) - { - //TODO: Implement - return new ArrayComparisonResultImpl(); - } + => arr.Compare(other, isEqualValues, 0); //Non-mvp: Test this /// A pair indicating the first element found and its index, or (-1, default) if nothing found. diff --git a/CsharpExtras/Extensions/ArrayExtension2D.cs b/CsharpExtras/Extensions/ArrayExtension2D.cs index d7b38db..beb9df5 100644 --- a/CsharpExtras/Extensions/ArrayExtension2D.cs +++ b/CsharpExtras/Extensions/ArrayExtension2D.cs @@ -1,5 +1,6 @@ using CsharpExtras.Compare; using CsharpExtras.Compare.Array; +using CsharpExtras.Extensions.Helper; using System; using System.Collections.Generic; using System.Linq; @@ -10,14 +11,8 @@ namespace CsharpExtras.Extensions { public static class ArrayExtension2D { - - public static IComparisonResult Compare(this TVal[,] arr, TVal[,] other, - Func isEqualValues) - { - //TODO: Implement - return new ArrayComparisonResultImpl(); - } + Func isEqualValues) => arr.Compare(other, isEqualValues, 0); /// A predicate on an entire column /// The last column where the predicate is true, or -1 if none found diff --git a/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs b/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs new file mode 100644 index 0000000..2aea2d8 --- /dev/null +++ b/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs @@ -0,0 +1,47 @@ +using CsharpExtras.Compare; +using CsharpExtras.Compare.Array; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CsharpExtras.Extensions.Helper +{ + internal static class ArrayHelperFunc + { + internal static IComparisonResult Compare(this TVal[] arr, TVal[] other, + Func isEqualValues, int indexBase) + { + int thisLength = arr.Length; + int otherLength = other.Length; + IEnumerable thisShape = thisLength.AsSingleton(); + IEnumerable otherShape = otherLength.AsSingleton(); + if (thisLength != otherLength) + { + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, null, null); + } + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, null, null); + } + + internal static IComparisonResult Compare(this TVal[,] arr, TVal[,] other, + Func isEqualValues, int indexBase) + { + IList thisShape = GetShape(arr); + IList otherShape = GetShape(other); + if (!Enumerable.SequenceEqual(thisShape, otherShape)) + { + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, null, null); + } + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, null, null); + } + + private static List GetShape(TVal[,] arr) + { + return new List { arr.GetLength(0), arr.GetLength(1) }; + } + } +} From 7e9477c0f73e6c069f24cd7852404dbeecf17526 Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 14:57:02 +0000 Subject: [PATCH 08/10] Fixed false-case tests The arrays supplied to the tests were equal, whereas the test is for unequal arrays --- CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs | 2 +- CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs index 4a5fc7b..2cba984 100644 --- a/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs +++ b/CsharpExtrasTest/Enumerable/OneBased/OneBasedArray2DTest.cs @@ -50,7 +50,7 @@ public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse new double[,] { {1, 2, 3}, - {1.1, 0.5, 6.01}, + {1.1, 0.501, 6.01}, }, "Same shape, different value" ), diff --git a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs index 99dc7a2..9b6235f 100644 --- a/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs +++ b/CsharpExtrasTest/Extensions/ArrayExtension2DTest.cs @@ -36,7 +36,7 @@ public void GIVEN_UnEqualArrays_WHEN_Compare_THEN_IsEqualFalse new double[,] { {1, 2, 3}, - {1.1, 0.5, 6.01}, + {1.1, 0.5003, 6.01}, }, "Same shape, different value" ), From d7a9403a3c03b2beb1f9c4a7875436d2c60e612b Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 14:57:18 +0000 Subject: [PATCH 09/10] Completed compare implementations for 1D and 2D cases --- .../Extensions/Helper/ArrayHelperFunc.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs b/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs index 2aea2d8..9c13997 100644 --- a/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs +++ b/CsharpExtras/Extensions/Helper/ArrayHelperFunc.cs @@ -21,6 +21,16 @@ internal static IComparisonResult Compare(this TVal[] arr, TVal[] other, return new ArrayComparisonResultImpl (thisShape, otherShape, indexBase, null, null); } + for(int arrayIndex = 0; arrayIndex < thisLength; arrayIndex++) + { + TVal thisValue = arr[arrayIndex]; + TVal otherValue = other[arrayIndex]; + if (!isEqualValues(thisValue, otherValue)) + { + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, (arrayIndex.AsSingleton(), thisValue), otherValue?.ToString()); + } + } return new ArrayComparisonResultImpl (thisShape, otherShape, indexBase, null, null); } @@ -35,6 +45,20 @@ internal static IComparisonResult Compare(this TVal[,] arr, TVal[,] other, return new ArrayComparisonResultImpl (thisShape, otherShape, indexBase, null, null); } + for (int rowIndex = 0; rowIndex < thisShape[0]; rowIndex++) + { + for(int colIndex=0; colIndex < thisShape[1]; colIndex++) + { + TVal thisValue = arr[rowIndex, colIndex]; + TVal otherValue = other[rowIndex, colIndex]; + if (!isEqualValues(thisValue, otherValue)) + { + return new ArrayComparisonResultImpl + (thisShape, otherShape, indexBase, (new List + { rowIndex, colIndex}, thisValue), otherValue?.ToString()); + } + } + } return new ArrayComparisonResultImpl (thisShape, otherShape, indexBase, null, null); } From b5c03c619bc4c2230b0d496c715712a2d630431e Mon Sep 17 00:00:00 2001 From: cbhandal Date: Mon, 7 Mar 2022 15:01:49 +0000 Subject: [PATCH 10/10] Changelog update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d09c49f..1068165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added zip methods for sparse array and sparse array 2D (#132) - Fixed bug: default values were not overwriting non-defaults for sparse arrays (#134) - Added ZipEnum methods to 1D and 2D arrays via extension methods and also added one-based equivalent functions (#136) +- Added Compare functions for comparing 1D and 2D arrays, both one and zero-based. (#138) ### Added - Support for writing a 1D one-based array to a 2D one-based array (#9)