/// <summary> /// Display the failure information for two IEnumerables that did not match. /// </summary> /// <param name="writer">The MessageWriter on which to display</param> /// <param name="expected">The expected enumeration.</param> /// <param name="actual">The internalActual enumeration</param> /// <param name="depth">The depth of this failure in a set of nested collections</param> private void DisplayEnumerableDifferences(MessageWriter writer, IEnumerable expected, IEnumerable actual, int depth) { DisplayTypesAndSizes(writer, expected, actual, depth); if (comparer.FailurePoints.Count > depth) { FailurePoint failurePoint = comparer.FailurePoints[depth]; DisplayFailurePoint(writer, expected, actual, failurePoint, depth); if (failurePoint.ExpectedHasData && failurePoint.ActualHasData) { DisplayDifferences( writer, failurePoint.ExpectedValue, failurePoint.ActualValue, ++depth); } //else if (failurePoint.ActualHasData) //{ // writer.Write(" Extra: "); // writer.WriteCollectionElements(internalActual, failurePoint.Position, 3); //} //else //{ // writer.Write(" Missing: "); // writer.WriteCollectionElements(expected, failurePoint.Position, 3); //} } }
/// <summary> /// Displays a single line showing the point in the expected and internalActual /// arrays at which the comparison failed. If the arrays have different /// structures or dimensions, both values are shown. /// </summary> /// <param name="writer">The MessageWriter on which to display</param> /// <param name="expected">The expected array</param> /// <param name="actual">The internalActual array</param> /// <param name="failurePoint">Index of the failure point in the underlying collections</param> /// <param name="indent">The indentation level for the message line</param> private void DisplayFailurePoint(MessageWriter writer, IEnumerable expected, IEnumerable actual, FailurePoint failurePoint, int indent) { var expectedArray = expected as Array; var actualArray = actual as Array; int expectedRank = expectedArray != null ? expectedArray.Rank : 1; int actualRank = actualArray != null ? actualArray.Rank : 1; bool useOneIndex = expectedRank == actualRank; if (expectedArray != null && actualArray != null) { for (int r = 1; r < expectedRank && useOneIndex; r++) { if (expectedArray.GetLength(r) != actualArray.GetLength(r)) { useOneIndex = false; } } } int[] expectedIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(expected, failurePoint.Position); if (useOneIndex) { writer.WriteMessageLine(indent, ValuesDiffer_1, MsgUtils.GetArrayIndicesAsString(expectedIndices)); } else { int[] actualIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(actual, failurePoint.Position); writer.WriteMessageLine(indent, ValuesDiffer_2, MsgUtils.GetArrayIndicesAsString(expectedIndices), MsgUtils.GetArrayIndicesAsString(actualIndices)); } }
/// <summary> /// Display the failure information for two collections that did not match. /// </summary> /// <param name="writer">The MessageWriter on which to display</param> /// <param name="expected">The expected collection.</param> /// <param name="actual">The internalActual collection</param> /// <param name="depth">The depth of this failure in a set of nested collections</param> private void DisplayCollectionDifferences(MessageWriter writer, ICollection expected, ICollection actual, int depth) { DisplayTypesAndSizes(writer, expected, actual, depth); if (comparer.FailurePoints.Count > depth) { FailurePoint failurePoint = comparer.FailurePoints[depth]; DisplayFailurePoint(writer, expected, actual, failurePoint, depth); if (failurePoint.ExpectedHasData && failurePoint.ActualHasData) { DisplayDifferences( writer, failurePoint.ExpectedValue, failurePoint.ActualValue, ++depth); } else if (failurePoint.ActualHasData) { writer.Write(" Extra: "); writer.WriteCollectionElements(actual, failurePoint.Position, 3); } else { writer.Write(" Missing: "); writer.WriteCollectionElements(expected, failurePoint.Position, 3); } } }
private void DisplayStreamDifferences(MessageWriter writer, Stream expected, Stream actual, int depth) { if (expected.Length == actual.Length) { FailurePoint fp = comparer.FailurePoints[depth]; long offset = fp.Position; writer.WriteMessageLine(StreamsDiffer_1, expected.Length, offset); } else { writer.WriteMessageLine(StreamsDiffer_2, expected.Length, actual.Length); } }
private bool EnumerablesEqual(IEnumerable expected, IEnumerable actual, ref Tolerance tolerance) { if (recursionDetector.CheckRecursion(expected, actual)) { return(false); } IEnumerator expectedEnum = expected.GetEnumerator(); IEnumerator actualEnum = actual.GetEnumerator(); int count; for (count = 0;; count++) { bool expectedHasData = expectedEnum.MoveNext(); bool actualHasData = actualEnum.MoveNext(); if (!expectedHasData && !actualHasData) { return(true); } if (expectedHasData != actualHasData || !ObjectsEqual(expectedEnum.Current, actualEnum.Current, ref tolerance)) { var 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); } } }
private bool StreamsEqual(Stream expected, Stream actual) { if (expected == actual) { return(true); } if (!expected.CanRead) { throw new ArgumentException("Stream is not readable", "expected"); } if (!actual.CanRead) { throw new ArgumentException("Stream is not readable", "actual"); } if (!expected.CanSeek) { throw new ArgumentException("Stream is not seekable", "expected"); } if (!actual.CanSeek) { throw new ArgumentException("Stream is not seekable", "actual"); } if (expected.Length != actual.Length) { return(false); } var bufferExpected = new byte[BUFFER_SIZE]; var bufferActual = new byte[BUFFER_SIZE]; var binaryReaderExpected = new BinaryReader(expected); var binaryReaderActual = new BinaryReader(actual); long expectedPosition = expected.Position; long actualPosition = actual.Position; try { binaryReaderExpected.BaseStream.Seek(0, SeekOrigin.Begin); binaryReaderActual.BaseStream.Seek(0, SeekOrigin.Begin); for (long readByte = 0; readByte < expected.Length; readByte += BUFFER_SIZE) { binaryReaderExpected.Read(bufferExpected, 0, BUFFER_SIZE); binaryReaderActual.Read(bufferActual, 0, BUFFER_SIZE); for (int count = 0; count < BUFFER_SIZE; ++count) { if (bufferExpected[count] != bufferActual[count]) { var fp = new FailurePoint(); fp.Position = (int)readByte + count; failurePoints.Insert(0, fp); return(false); } } } } finally { expected.Position = expectedPosition; actual.Position = actualPosition; } return(true); }