0

嗨,我正在为我的项目进行单元测试,我想检查一个名为 Measurementool 的抽象类中的 getmin 方法。该方法从当前矩阵返回选定区域矩阵的最小值。我的项目有一个矩形区域,实际上是 N*N 矩阵。

我需要矩阵内的最小值。我为调试编写单元测试并测试 getmin 值,但我没有看到实际值的期望值。selectedarea 矩阵用零填充,在测试方法中没有设置期望值。我认为,它考虑了两个矩阵。当新矩阵创建如下时,具有零值的矩阵是错误的:

MatrixHolder selectedArea = 
           new MatrixHolder(new double[areaHeight * areaWidth], areaWidth);

单元测试代码:

[TestMethod()]
public void GetMinTest()
{
    AreaMeasurmentTool target = new Rectangle(); 
    double[,] MatrixResult = new double[,] {
    {10, 0, 20, 10, 10},
    {12, 2, 30, 15, 16},
    {19, 0, 43, 10, 17},
    {80, 0, 65, 18, 13},
    {70, 4, 10, 10, 10}};
    double []SelectedArea1 = new double[9]{2, 30, 15, 1, 43, 10, 2, 65, 18};
    MatrixHolder currentMatrix = new MatrixHolder(SelectedArea1, 3); 
    double expected = 1; 
    double actual;
    actual = target.GetMin(currentMatrix);
    Assert.AreEqual(expected, actual);
}

和类MeasurementTool:

public virtual double GetMin(MatrixHolder currentMatrix)
{
    if (currentMatrix == null)
    {
        throw new Exception("the image Matrix cannot be null.");
    }

    double minValue = 0;

    SetSelectArea(currentMatrix);
    minValue = CalculateMin();
    minValue = Math.Round(minValue, 2);

    return minValue;
}

private void SetSelectArea(MatrixHolder currentMatrix)
{
    PointHolder start = new PointHolder(0, 0);
    PointHolder end = new PointHolder(0, 0);

    SetBoundries(start, end);


    int areaHeight = (end.Y - start.Y);
    int areaWidth = (end.X - start.X);

    MatrixHolder selectedArea = new MatrixHolder(new double[areaHeight * areaWidth], areaWidth);
    for (int i = 0; i < areaHeight; i++)
    {
        int sourceIndex = start.X + (currentMatrix.Witdh * (i + start.Y));
        int targetIndex = areaWidth * i;
        Array.Copy(currentMatrix.Values, sourceIndex
            , selectedArea.Values, targetIndex
            , areaWidth);
    }

    SelectedAreas = selectedArea;
}
4

0 回答 0