Skip to content

Instantly share code, notes, and snippets.

@devshorts
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devshorts/9241323 to your computer and use it in GitHub Desktop.
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
> 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
>
@devshorts
Copy link
Author

// working tuples

let t = (1,2);;

val t : int * int = (1, 2)

let t2 = (1, 2);;

val t2 : int * int = (1, 2)

t.GetHashCode();;

val it : int = 33

t2.GetHashCode();;

val it : int = 33

@devshorts
Copy link
Author

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