【问题标题】:How to write Junit test case array matrix addition and multiplication?如何编写Junit测试用例数组矩阵加法和乘法?
【发布时间】:2021-10-13 03:59:42
【问题描述】:
package testMatrix;

public class MatrixAdd {
    public int [][] addtionOfArray(int [][] numbers){
        int length = numbers.length;
        int output[][] = new int[length][length];
        for(int i=0; i< length; i++){
            for(int j=0; i< length; j++){
                output[i][j] = numbers[i][j] + numbers[i][j];
            }
        }
        return output;
    }
}

如何编写数组矩阵加法的Junit测试?以及矩阵乘法的Junit测试

【问题讨论】:

  • 你想做什么样的乘法?叉积还是点积?此外,矩阵需要相同大小,因此很容易添加第二个相同大小的参数并使用output[i][j] = numbers1[i][j] + numbers2[i][j]
  • 我想要点积数组矩阵乘法。上面的代码和乘法怎么写Junit测试代码?
  • *我想知道上面的代码Junit测试和点积Junit测试代码怎么写?

标签: java matrix junit multiplication addition


【解决方案1】:

test 源代码集中,您可以使用类似的东西


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class MatrixAddTest {

    private MatrixAdd testee = MatrixAdd();

    @Test
    public void testAdditionOfArray(){
        int[][] expected = new int[][]{new int[]{2, 0}, new int[]{0, 2}};
        int[][] actual = testee.addtionOfArray(new int[][]{new int[]{1, 0}, new int[]{0, 1}});
        Assertions.assertEquals(expected, actual);
    }
}

但是,由于 MatrixAdd 看起来像一个实用程序类,您也可以将方法设为静态,您实际上不需要任何地方的 MatrixAdd 实例。

【讨论】:

  • 我从昨天开始尝试使用此代码,但收到错误```
  • 我从昨天开始尝试使用此代码,但收到错误。我认为我的代码在output[i][j] = numbers1[i][j] + numbers2[i][j]testAdditionOfArray 函数中存在问题。我还有一个问题,我们需要在测试代码中给出数组矩阵值,对吗?你是如何在 Junit 代码中编写 expectedactual 代码的。请澄清我的疑问。 @thinkgruen
  • 我们可以在单个 Junit 测试用例中编写不同的测试用例,对吗?你能帮我处理不同的测试用例吗,比如single column matrix, single row matrix and 2x3 matrix@thinkgruen
  • 到最后一个问题:您可以将多个断言添加到单个测试用例,是的,但是如果您为单个方法编写单个测试用例会更清楚。我不确定“你是如何写出预期和实际的”是什么意思——你几乎可以选择任何你喜欢的矩阵,所以我选择了一个 2x2 单位矩阵来测试。 actual 是使用这个 2x2 矩阵的方法的输出,expected 是矩阵乘以 2(因为这是你的加法的工作原理)
  • 通过添加这 2 个命令 import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; 抛出错误为“无法解析导入 org.junit.jupiter”。我在没有这两个命令的情况下运行代码,但我使用了 import static org.junit.Assert.*; import org.junit.Test; 这两个源文件。测试用例如何给出错误,为什么会发生请解释。谢谢
【解决方案2】:

请检查此示例并阅读 cmets 以更好地理解,我真的希望此代码对您有用,或者至少让您了解如何 矩阵乘法:

/* A naive recursive implementation that simply follows
the above optimal substructure property */
class MatrixChainMultiplication {
    // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
    static int MatrixChainOrder(int p[], int i, int j)
    {
        if (i == j)
            return 0;

        int min = Integer.MAX_VALUE;

        // place parenthesis at different places between
        // first and last matrix, recursively calculate
        // count of multiplications for each parenthesis
        // placement and return the minimum count
        for (int k = i; k < j; k++)
        {
            int count = MatrixChainOrder(p, i, k)
                        + MatrixChainOrder(p, k + 1, j)
                        + p[i - 1] * p[k] * p[j];

            if (count < min)
                min = count;
        }

        // Return minimum count
        return min;
    }

    // Driver code
    public static void main(String args[])
    {
        int arr[] = new int[] { 1, 2, 3, 4, 3 };
        int n = arr.length;

        System.out.println(
            "Minimum number of multiplications is "
            + MatrixChainOrder(arr, 1, n - 1));
    }
}

【讨论】:

  • 谢谢你的代码,我写了基本的矩阵乘法代码,和我上面写的代码格式相同(MatrixAdd)。但我想要 Junit 测试代码。如何测试矩阵加法和乘法的Junit测试。如果可能的话,请您帮我提供用于 add 和 mul 的 main 和 Junit 的完整代码。谢谢@Alva Santi
猜你喜欢
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多