Junit测试用例数组矩阵加法和乘法怎么写?

How to write Junit test case array matrix addition and multiplication?

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 测试

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 看起来像一个实用程序 class,您也可以将这些方法设为静态,您实际上并不需要任何地方的 MatrixAdd 实例。

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

/* 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));
    }
}