gpt4 book ai didi

c# - 如果两个交错数组相等,如何进行单元测试?

转载 作者:行者123 更新时间:2023-11-30 15:36:58 24 4
gpt4 key购买 nike

我有一个手动提取日期的函数。

[TestMethod]
public void TestRemoveTwoDates()
{
String test = "at 06/2/2012 to 12/10/2012";
int[][] actual = String_Parser.RemoveTwoDates(test, "to");
int[][] expected = new int[2][];
expected[0] = new int[3] { 6, 2, 2012 };
expected[1] = new int[3] { 12, 10, 2012 };

CollectionAssert.AreEqual(expected, actual);
}

附言浏览 SO,我意识到返回数组不是好的做法,因为它们是可变的。但我仍然想知道除了使用循环来断言数组中的每个元素之外,我该如何测试它。

最佳答案

由于您不能使用使用引用而不是数组内容的 Hashcode,因此方法是在嵌套循环中迭代所有值,逐个比较值(如 stackoverflow.com/questions/2893297/iterate-multi-dimensional-array-with-nested-foreach-statement ),使用您在第一次不匹配时设置为 false 的 bool 值,并在您的单元测试中断言此 bool 值是 true

关于c# - 如果两个交错数组相等,如何进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com