我有一個 Matrix 類,另一個類使用該矩陣對其進行了一些更改。我想測驗兩個矩陣,一個來自矩陣類,另一個已經改變,所以我可以確認它們不一樣。
像這樣的東西。
[Fact]
public void MatrixFromMatrixIsntTheSameThanMatrixFromMineSweeper()
{
Matrix _matrix = new Matrix(4, 4);
MineSweeper mineSweeper = new MineSweeper(4, 4, 2);
mineSweeper.Matrix.Should().NotBe(_matrix);
}
問題是Be/NotBe似乎正在使用物件的參考,所以它總是回傳 false,因為它們不一樣。我也試過NotBeSameAs, NotBeEquivalentTo。
這些是 Matrix 和 MineSweeper 類。
矩陣類
public struct Coordinate
{
public int X;
public int Y;
public Coordinate(int x, int y)
=> (X, Y) = (x, y);
}
public class Matrix
{
private readonly int _M, _N;
private readonly Cell[,] _matrix;
private const char InitValue = '.';
public Matrix(int m, int n)
{
(_M, _N) = (m, n);
_matrix = new Cell[m, n];
Initialize();
}
private void Initialize()
{
for (int m = 0; m < _M; m )
for (int n = 0; n < _N; n )
_matrix[m, n] = new Cell(InitValue);
}
public Cell At(Coordinate coordinate)
=> _matrix[coordinate.X, coordinate.Y];
public void SetMine(Coordinate coordinate)
{
_matrix[coordinate.X, coordinate.Y] = new Cell('*');
}
}
掃雷類
public class MineSweeper
{
private readonly int _m, _n, _numMines;
public Matrix Matrix { get; }
public MineSweeper(int m, int n, int numMines)
{
(_m, _n, _numMines) = (m, n, numMines);
Matrix = new Matrix(m, n);
SetMines();
}
private void SetMines()
{
HashSet<Coordinate> minesSet = new HashSet<Coordinate>();
Random rnd = new Random();
while (minesSet.Count != _numMines)
minesSet.Add(new Coordinate(rnd.Next(0, _m), rnd.Next(0, _n)));
foreach (Coordinate coordinate in minesSet)
Matrix.SetMine(coordinate);
}
}
uj5u.com熱心網友回復:
查看您對元組的使用情況,您使用的是最新的 .NET 版本。這使您可以訪問record,我建議您將其用于您的Cell結構。
public record struct Cell (char Value);
記錄帶有隱含的建構式和相等比較。相等比較對您很重要,因為要比較兩個Matrix物件,您需要確保它們的內容Cell[,]相同。
您現在有兩個選擇:
1. 應該().Equal()
適用于IEnumerable. 因此,如果要比較兩個Matrix物件,則需要公開Cell[,]陣列的內容并比較兩者。您使用Equal()是因為您想比較專案的順序。不要使用,BeEquivalentTo()因為它不關心順序。有關不同的可列舉比較,請參見此處。
public class Matrix {
...
// Expose your array as an IEnumerable
public IEnumerable<Cell> Cells => _matrix.OfType<Cell>();
}
// And compare the contents in sequence
public void Test(){
// These have 1 Mine in the same location:
Matrix m1 = new(4,4); m1.SetMine(new(1,1));
Matrix m2 = new(4,4); m2.SetMine(new(1,1));
// 1 Mine in a different location:
Matrix xx = new(4,4); xx.SetMine(new(2,2));
// Same contents in the same order
m2.Cells.Should().Equal(m1.Cells);
// Same contents different order, are NOT Equal
xx.Cells.Should().NotEqual(m1.Cells);
// But are Equivalent
xx.Cells.Should().BeEquivalentTo(m1.Cells);
}
2. 應該().Be()
如果您想嚴格封裝,并且不想將您的內容公開Cell[,]為公共,那么您將不得不定義 2 個Matrix物件之間的相等比較。這允許您使用Be()which 將呼叫物件的Equals()方法。
(這是下面代碼的完整小提琴)
public class Matrix {
...
/* Easiest way to compare two Cell collections is to join
the chars into a string, and perform string comparison.
We could go through the two arrays and compare the contents
one by one, but a good programmer is a lazy one.
*/
public string CellsAsString() // private so the public can't see it
=> string.Concat(_matrix.OfType<Cell>().Select(c => c.Value));
/* Now Override the Equality comparison of the Object */
public override bool Equals(object other)
=> this.CellsAsString().Equals((other as Matrix)?.CellsAsString());
// Note that we can access private methods of another object
// as long as it's the same class as `this`.
}
您的比較現在只是:
Matrix m1 = new(4,4); m1.SetMine(new(1,1));
Matrix m2 = new(4,4); m2.SetMine(new(1,1));
Matrix xx = new(4,4); xx.SetMine(new(2, 2));
m2.Should().Be(m1);
xx.Should().NotBe(m1);
uj5u.com熱心網友回復:
通常我們推薦BeEquivalentTo,但由于您的Matrix類不公開任何公共欄位或屬性,唯一的其他選項是NPras在選項 2 中建議的選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/387531.html
上一篇:使用servlet和會話進行測驗時出現MissingMethodInvocationException
下一篇:斷言檢查元素是否存在于串列中
