1
在單元測試中檢查矩陣值的最佳方法是什麼?在單元測試中檢查矩陣值的最佳方法是什麼?
我有一個方法,在我的項目,這樣的事情:
int[][] getMatrix()
{
int[][] ans = new int[1][];
ans[0] = new int[1];
ans[0][0] = 0;
return ans;
}
我想單元測試這個類使用Visual Studio的標準測試引擎,所以我創建單元測試,這樣的事情:
[TestMethod]
public void Test()
{
var result = getMatrix();
int[][] correct = new int[1][];
correct[0] = new int[1];
correct[0] = 0;
Assert.AreEqual(correct.Length, result.Length);
for (int i = 0; i < correct.Length; ++i)
{
CollectionAssert.AreEqual(correct[i], result[i]);
}
}
所以,在這裏我寫了一個斷言檢查數量在兩個矩陣的行都是平等的,用於CollectionAssert檢查矩陣中的對應行也相同。
我不喜歡這段代碼 - 我確定有一些比較矩陣的標準方式,整個循環可能會以更簡短明瞭的方式進行重構。
那麼,在單元測試中檢查矩陣值的最好方法是什麼?
非常感謝,我喜歡這個解決方案! – alexeykuzmin0