C# 为什么 Assert.AreEqual(T obj1, Tobj2) 以相同的字节数组失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Assert.AreEqual(T obj1, Tobj2) fail with identical byte arrays
提问by David Anderson
I have two identical byte arrays in the following segment of code:
我在以下代码段中有两个相同的字节数组:
/// <summary>
///A test for Bytes
///</summary>
[TestMethod()]
public void BytesTest() {
byte[] bytes = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketData);
TransferEventArgs target = new TransferEventArgs(bytes);
byte[] expected = Encoding.UTF8.GetBytes(Properties.Resources.ExpectedPacketValue);
byte[] actual;
actual = target.Bytes;
Assert.AreEqual(expected, actual);
}
Both arrays are identical down to the very byte. In this scenario, why would Assert.AreEqual fail?
两个数组在字节上都是相同的。在这种情况下,为什么 Assert.AreEqual 会失败?
采纳答案by tvanfosson
Assert.Equals
tests using the Equals
method, which by default uses reference equality and, since they are different objects, they are not equal. You'll want to compare each byte in the array and verify that they are equal. One way to do this is convert them to something that implements ICollection and use CollectionAssert.AreEqual()instead.
Assert.Equals
使用该Equals
方法进行测试,该方法默认使用引用相等,并且由于它们是不同的对象,因此它们不相等。您需要比较数组中的每个字节并验证它们是否相等。一种方法是将它们转换为实现 ICollection 的东西并使用CollectionAssert.AreEqual()代替。
回答by Jon Skeet
Because arrays don't override Equals
.
因为数组不会覆盖Equals
.
You haven't said which test framework you're using, but basically it would be up to that framework to special-case arrays. You can always implement your own helper method to do that, of course. I've done that sometimes. For a quick and dirty hack, if you're using .NET 3.5 you can use the Enumerable.SequenceEqual
extension method:
您还没有说明您使用的是哪个测试框架,但基本上,对于特殊情况的数组,这取决于该框架。当然,您始终可以实现自己的辅助方法来做到这一点。我有时这样做过。对于快速而肮脏的 hack,如果您使用 .NET 3.5,您可以使用Enumerable.SequenceEqual
扩展方法:
Assert.IsTrue(actual.SequenceEqual(expected));
A custom helper method could give you more details about how they differ, of course. You might find the methods in MoreLINQ.TestExtensions
helpful, although they're fairly rough and ready too.
当然,自定义辅助方法可以为您提供有关它们有何不同的更多详细信息。您可能会发现这些方法MoreLINQ.TestExtensions
很有帮助,尽管它们也相当粗糙且准备就绪。
回答by JaredPar
The method Assert.AreEqual under the hood will end up defaulting to Object.Equals() for non-null values. The default implementation of Object.Equals() is referential equality. The 2 arrays are identical value wise but difference reference wise and hence will not be considered equal.
对于非空值,引擎盖下的方法 Assert.AreEqual 最终将默认为 Object.Equals()。Object.Equals() 的默认实现是引用相等。这两个数组在值方面是相同的,但在引用方面是不同的,因此不会被认为是相等的。
回答by José Brazeta
//Initialize your arrays here
byte[] array1 = new byte[0];
byte[] array2 = new byte[0];
Assert.AreEqual(System.Convert.ToBase64String(array1),
System.Convert.ToBase64String(array2));
回答by user2682078
byte[] a = new byte[] {x, y, z...};
byte[] b = new byte[] {x, y, z...};
assertArrayEquals(a , b );
will compare the stuff... It works for me..
将比较这些东西......它对我有用..
回答by Gh61
Created simple helper method:
创建简单的辅助方法:
private static void CompareArrays<T>(T[] expected, T[] actual)
{
Assert.AreEqual(expected == null, actual == null, "Expected {0}null value and {1}null found.", expected == null ? "" : "not", actual == null ? "" : "not");
if (expected == null || actual == null)
return;
Assert.AreEqual(expected.LongLength, actual.LongLength, "Expected Length is {0} actual: {1}", expected.LongLength, actual.LongLength);
for (int i = 0; i < expected.Length; i++)
{
Assert.AreEqual(expected[i], actual[i], "Values on index {0} are not equal. Expected {1} actual: {2}", i, expected[i], actual[i]);
}
}