Last active
August 29, 2015 13:56
-
-
Save devshorts/9241323 to your computer and use it in GitHub Desktop.
Investigating why NUnit Assert.AreEqual doesn't work for identitical tuples with arrays
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
> z;; | |
val it : int [] * int = ([|1|], 1) | |
> p;; | |
val it : int [] * int = ([|1|], 1) | |
> z.Equals(p);; | |
val it : bool = false | |
> z = p;; | |
val it : bool = true | |
> |
Special stuff for arrays in NUnit:
/// <summary>
/// Helper method to compare two arrays
/// </summary>
private bool ArraysEqual(Array x, Array y, ref Tolerance tolerance)
{
int rank = x.Rank;
if (rank != y.Rank)
return false;
for (int r = 1; r < rank; r++)
if (x.GetLength(r) != y.GetLength(r))
return false;
return EnumerablesEqual((IEnumerable)x, (IEnumerable)y, ref tolerance);
}
private bool EnumerablesEqual(IEnumerable x, IEnumerable y, ref Tolerance tolerance)
{
IEnumerator expectedEnum = x.GetEnumerator();
IEnumerator actualEnum = y.GetEnumerator();
int count;
for (count = 0; ; count++)
{
bool expectedHasData = expectedEnum.MoveNext();
bool actualHasData = actualEnum.MoveNext();
if (!expectedHasData && !actualHasData)
return true;
if (expectedHasData != actualHasData ||
!AreEqual(expectedEnum.Current, actualEnum.Current, ref tolerance))
{
FailurePoint fp = new FailurePoint();
fp.Position = count;
fp.ExpectedHasData = expectedHasData;
if (expectedHasData)
fp.ExpectedValue = expectedEnum.Current;
fp.ActualHasData = actualHasData;
if (actualHasData)
fp.ActualValue = actualEnum.Current;
failurePoints.Insert(0, fp);
return false;
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// working tuples
val t : int * int = (1, 2)
val t2 : int * int = (1, 2)
val it : int = 33
val it : int = 33