我有生成随机线性方程组的类
public class MatrixGenerator: IMatrixGenerator
{
private int vSize;
private int hSize;
private double[,] _matrix;
private double[] _right;
private double[] _solution;
private double maxValue;
public MatrixGenerator(int vSize, int hSize, double maxValue)
{
this.vSize = vSize;
this.hSize = hSize;
this.maxValue = maxValue;
_matrix = new double[vSize, hSize];
_right = new double[vSize];
_solution = new double[hSize];
}
public void Next()
{
_matrix = new double[vSize, hSize];
_right = new double[vSize];
Random r = new Random();
_solution = Enumerable.Repeat(0.0, hSize).Select(m => m = r.NextDouble()*maxValue).ToArray();
for (int i = 0; i < vSize; i++)
{
for (int j = 0; j < hSize; j++)
{
_matrix[i, j] = r.NextDouble() * maxValue;
}
for (int j = 0; j < hSize; j++)
{
_right[i] += _solution[j] * _matrix[i, j];
}
}
}
public double[,] Matrix
{
get { return _matrix; }
}
public double[] RightVector
{
get { return _right; }
}
public double[] SolutionVector
{
get { return _solution; }
}
}
和此类的 NUnit 测试:
[Test]
public void CanGenerateAnotherMatrixandVector()
{
MatrixGenerator mGen = new MatrixGenerator(vSize, hSize, maxValue);
mGen.Next();
double[,] firstMatrix = new double[mGen.Matrix.GetLength(0), mGen.Matrix.GetLength(1)];
double[] firstVector = new double[mGen.RightVector.GetLength(0)];
for (int i = 0; i < mGen.Matrix.GetLength(0); i++)
{
firstVector[i] = mGen.RightVector[i];
for (int j = 0; j < mGen.Matrix.GetLength(1); j++)
{
firstMatrix[i,j] = mGen.Matrix[i, j];
}
}
mGen.Next();
Assert.That(firstMatrix, Is.Not.EqualTo(mGen.Matrix));
Assert.That(firstVector, Is.Not.EqualTo(mGen.RightVector));
}
测试失败,但此代码有效。我尝试使用来自 TestDriven.Net 的调试器工具调试此测试,一切正常,测试通过。谁能描述我为什么这个测试失败了?