I'm trying to create a Unit Test that compares two lists of string arrays.
I tried creating two of the exact same List<string[]> objects, but when I use CollectionAssert.AreEqual(expected, actual);, the test fails:
[TestMethod]
public void TestList()
{
List<string[]> expected = new List<string[]> {
new string[] { "John", "Smith", "200" },
new string[] { "John", "Doe", "-100" }
};
List<string[]> actual = new List<string[]> {
new string[] { "John", "Smith", "200" },
new string[] { "John", "Doe", "-100" }
};
CollectionAssert.AreEqual(expected, actual);
}
I've also tried Assert.IsTrue(expected.SequenceEqual(actual));, but that fails as well.
Both these methods work if I am comparing two Lists of strings or two arrays of strings, but they do not work when comparing two Lists of arrays of strings.
I'm assuming these methods are failing because they are comparing two Lists of object references instead of the array string values.
How can I compare the two List<string[]> objects and tell if they are really the same?
expected.Zip(actual, (e, a) => e.SequenceEqual(a)).All(x => x)..Zipnot comparing the full lengths..Contains(false)negated is not more (nor less) efficient than.All(x => x). Both "consume" the source until they find an entry which isfalse. The first compares each element tofalsewith the default equality comparer forbool. The second invokes the delegatepredicatewhich wraps the static IL method that comes out of the lambda arrowx => x, and checks the return values. Since the runtime will do inlining in either case, I think both will be equally fast (I did not measure). Agree with @Enigmativity.