c# - 为什么这些 ulong 数组不被认为是相等的?

标签 c# arrays mstest

<分区>

考虑函数:

public static ulong[] productFib(ulong prod)
    {
        ulong fib1 = 0;
        ulong fib2 = 1;

        while ((fib1 * fib2) < prod)
        {
            ulong temp = fib1;
            fib1 = fib2;
            fib2 = temp + fib2;

        }

        return new ulong[] { fib1, fib2, 1 };
    }

我正在研究一个 kata,其测试如下所示:

ulong[] r = new ulong[] { 55, 89, 1 };
Assert.AreEqual(r, Kata.productFib(4895));

在立即窗口中,我可以看到返回数组不仅是 ulong 类型的但具有我期望的 3 个元素,但 MSTest 失败:

Message: Assert.AreEqual failed. Expected:<System.UInt64[]>. Actual:<System.UInt64[]>.

测试断言或我的函数是否有问题会导致此测试失败?

谢谢

最佳答案

Assert.AreEqual() 使用传递参数的 Equals() 方法。数组只是比较它们的引用,而不是它们的内容。所以两个不同的数组实例永远不会通过这个测试。

你可以使用 linq SequenceEqual像那样:

Assert.IsTrue(r.SequenceEqual(Kata.productFib(4895)));

关于c# - 为什么这些 ulong 数组不被认为是相等的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45981896/

相关文章:

unit-testing - 比较单元测试中的文本

msbuild - teamcity 表示找不到 MSTests 的测试 xxx

C# 等同于生锈 "match"

c# - Resharper 提示测试项目中的得分方法不足

c++ - 在 Visual Studio 2013 C++ 中使用零大小的数组

php - 将json字符串的某些值转换为php中的整数

c# - LINQ to Entities 无法识别方法 'Newtonsoft.Json.Linq.JToken get_Item(System.String)' 方法,

visual-studio-2010 - 如何使用 mstest 基于 teamcity 中的类别运行测试

c# - 从顺序文件读取到结构数组

c# - 调用者未获得 'finished' 的“yield”枚举 - 会发生什么