Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MathMatrix mat1 = new MathMatrix(1, 1, 2);
- int[][] e1 = new int[][] { { 2 } };
- printTestResult(get2DArray(mat1), e1, 1, "Constructor with size and initial val specified.");
- // test 2, specify size and values constructor
- mat1 = new MathMatrix(5, 2, -3);
- e1 = new int[][] { { -3, -3 }, { -3, -3 }, { -3, -3 }, { -3, -3 }, { -3, -3 } };
- printTestResult(get2DArray(mat1), e1, 2, "Constructor with size and initial val specified.");
- // test 3, deep copy constructor
- int[][] data1 = new int[][] { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
- mat1 = new MathMatrix(data1);
- data1[1][1] = 5;
- // alter data1. mat1 should be unchanged if deep copy made
- e1 = new int[][] { { 1, 2, 3 }, { 2, 5, 4 }, { 3, 4, 5 } };
- printTestResult(data1, e1, 3, "constructor with one parameter of type int[][]");
- // data1 altered. mat1 should be unchanged if deep copy made
- e1 = new int[][] { { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 } };
- printTestResult(get2DArray(mat1), e1, 3,
- "constructor with one parameter of type int[][]. Testing deep copy made.");
- // test 4, deep copy constructor
- data1 = new int[][] { { 0 } };
- mat1 = new MathMatrix(data1);
- data1 = new int[][] { { 1 } };
- // alter data1. mat1 should be unchanged if deep copy made
- e1 = new int[][] { { 1 } };
- printTestResult(data1, e1, 4, "constructor with one parameter of type int[][]");
- // data1 altered. mat1 should be unchanged if deep copy made
- e1 = new int[][] { { 0 } };
- printTestResult(get2DArray(mat1), e1, 4,
- "constructor with one parameter of type int[][]. Testing deep copy made.");
- // test 5, testing getNumRows()
- data1 = new int[][] { { 1, 1, 1, 1, 1, 1 } };
- mat1 = new MathMatrix(data1);
- if (mat1.getNumRows() == 1)
- System.out.println("Passed test 5, getNumRows().");
- else
- System.out.println("Failed test 5, getNumRows().");
- // test 6, testing getNumRows()
- data1 = new int[][] { { 1 }, { 0 }, { 0 }, { 0 }, { 0 } };
- mat1 = new MathMatrix(data1);
- if (mat1.getNumRows() == 5)
- System.out.println("Passed test 6, getNumRows().");
- else
- System.out.println("Failed test 6, getNumRows().");
- // test 7, testing getNumColumns()
- data1 = new int[][] { { 1, 1, 1, 1, 1, 1 } };
- mat1 = new MathMatrix(data1);
- if (mat1.getNumColumns() == 6)
- System.out.println("Passed test 7, getNumColumns().");
- else
- System.out.println("Failed test 7, getNumColumns().");
- // test 8, testing getNumColumns()
- data1 = new int[][] { { 1 }, { 0 }, { 0 }, { 0 }, { 0 } };
- mat1 = new MathMatrix(data1);
- if (mat1.getNumColumns() == 1)
- System.out.println("Passed test 8, getNumColumns().");
- else
- System.out.println("Failed test 8, getNumColumns().");
- // test 9, testing getVal()
- if (mat1.getVal(3, 0) == 0)
- System.out.println("Passed test 9, getVal().");
- else
- System.out.println("Failed test 9, getVal().");
- // test 10, testing getVal()
- mat1 = new MathMatrix(4, 5, -1000);
- if (mat1.getVal(3, 4) == -1000)
- System.out.println("Passed test 10, getVal()");
- else
- System.out.println("Failed test 10, getVal()");
- // test 11, testing add method
- data1 = new int[][] { { 2, 3, 4 }, { 5, 6, 7 }, { 8, 9, 10 } };
- mat1 = new MathMatrix(data1);
- MathMatrix mat2 = new MathMatrix(data1);
- MathMatrix mat3 = mat1.add(mat2);
- e1 = new int[][] { { 4, 6, 8 }, { 10, 12, 14 }, { 16, 18, 20 } };
- printTestResult(get2DArray(mat3), e1, 11, "add method. Testing mat3 correct result.");
- // test 12, testing add method
- mat1 = new MathMatrix(data1);
- int[][] data2 = new int[][] { { -2, -3, -4 }, { -5, -6, -7 }, { -8, -9, -10 } };
- mat2 = new MathMatrix(data2);
- mat3 = mat1.add(mat2);
- e1 = new int[][] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
- printTestResult(get2DArray(mat3), e1, 12, "add method. Testing mat3 correct result.");
- // test 13, testing subtract method
- data1 = new int[][] { { 2, 3, 4 }, { 5, 6, 7 }, { 8, 9, 10 } };
- mat1 = new MathMatrix(data1);
- mat2 = new MathMatrix(data1);
- mat3 = mat1.subtract(mat2);
- e1 = new int[][] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
- printTestResult(get2DArray(mat3), e1, 13, "subtract method. Testing mat3 correct result.");
- // test 14, testing subtract method
- mat1 = new MathMatrix(data1);
- data2 = new int[][] { { -2, -3, -4 }, { -5, -6, -7 }, { -8, -9, -10 } };
- mat2 = new MathMatrix(data2);
- mat3 = mat2.subtract(mat1);
- e1 = new int[][] { { -4, -6, -8 }, { -10, -12, -14 }, { -16, -18, -20 } };
- printTestResult(get2DArray(mat3), e1, 14, "subtract method. Testing mat3 correct result.");
- // test 15, testing multiplication method
- data1 = new int[][] { { 2, 3, 4, 5 }, { 1, 6, 7, 8 }, { 5, 2, 3, 4 } };
- data2 = new int[][] { { 7, 4, 3 }, { 2, 1, 7 }, { 6, 5, 4 }, { 8, 9, 1 } };
- mat1 = new MathMatrix(data1);
- mat2 = new MathMatrix(data2);
- mat3 = mat1.multiply(mat2);
- e1 = new int[][] { { 84, 76, 48 }, { 125, 117, 81 }, { 89, 73, 45 } };
- printTestResult(get2DArray(mat3), e1, 15, "multiply method. Testing mat3 correct result.");
- // test 16, testing multiplication method
- data1 = new int[][] { { 2, 3, 4 }, { 5, 6, 7 }, { 8, 9, 10 }, { 11, 12, 13 } };
- data2 = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
- mat1 = new MathMatrix(data1);
- mat2 = new MathMatrix(data2);
- mat3 = mat1.multiply(mat2);
- e1 = new int[][] { { 42, 51, 60 }, { 78, 96, 114 }, { 114, 141, 168 }, { 150, 186, 222 } };
- printTestResult(get2DArray(mat3), e1, 16, "multiply method. Testing mat3 correct result.");
- // test 17, testing getScaledMatrix()
- mat3 = mat1.getScaledMatrix(100);
- e1 = new int[][] { { 200, 300, 400 }, { 500, 600, 700 }, { 800, 900, 1000 }, { 1100, 1200, 1300 } };
- printTestResult(get2DArray(mat3), e1, 17, "getScaledMatrix method. Testing mat3 correct result.");
- // test 18, testing getScaledMatrix()
- data1 = new int[][] { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } };
- mat1 = new MathMatrix(data1);
- mat3 = mat1.getScaledMatrix(0);
- e1 = new int[][] { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
- printTestResult(get2DArray(mat3), e1, 18, "getScaledMatrix method. Testing mat3 correct result.");
- // test 19, testing transpose method
- data1 = new int[][] { { 1, 2, 3, 4 }, { 4, 3, 2, 1 } };
- mat1 = new MathMatrix(data1);
- mat2 = mat1.getTranspose();
- e1 = new int[][] { { 1, 4 }, { 2, 3 }, { 3, 2 }, { 4, 1 } };
- printTestResult(get2DArray(mat2), e1, 19, "getTranspose() method. Testing mat3 correct result.");
- // test 20, testing transpose method
- data1 = new int[][] { { 1 }, { 2 } };
- mat1 = new MathMatrix(data1);
- mat2 = mat1.getTranspose();
- e1 = new int[][] { { 1, 2 } };
- printTestResult(get2DArray(mat2), e1, 20, "getTranspose() method. Testing mat3 correct result.");
- // test 21, testing equals method
- mat1 = new MathMatrix(data1);
- mat2 = new MathMatrix(data1);
- if (mat1.equals(mat2))
- System.out.println("Passed test 21, equals method.");
- else
- System.out.println("Failed test 21, equals method.");
- // test 22, testing equals method
- data1 = new int[][] { { 0 } };
- mat1 = new MathMatrix(data1);
- mat2 = new MathMatrix(data2);
- if (!mat1.equals(mat2))
- System.out.println("Passed test 22, equals method.");
- else
- System.out.println("Failed test 22, equals method.");
- // test 23, testing toString method
- data1 = new int[][] { { 1, 100, 104, -100000 }, { 1000, 10, 55, 4 }, { 1, -1, 4, 0 } };
- mat1 = new MathMatrix(data1);
- String expected = "| 1 100 104 -100000|\n| 1000 10 55 4|\n| 1 -1 4 0|\n";
- if (mat1.toString().equals(expected))
- System.out.println("passed test 23, toString method.");
- else
- System.out.println("failed test 23, toString method.");
- // test 24, testing toString method
- data1 = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
- mat1 = new MathMatrix(data1);
- expected = "| 1 2 3|\n| 4 5 6|\n| 7 8 9|\n";
- if (mat1.toString().equals(expected))
- System.out.println("passed test 24, toString method.");
- else
- System.out.println("failed test 24, toString method.");
- // test 25, testing isTriangular method
- data1 = new int[][] { { 0 } };
- mat1 = new MathMatrix(data1);
- if (mat1.isUpperTriangular())
- System.out.println("Passed test 25, upperTriangular method.");
- else
- System.out.println("Failed test 25, upperTriangular method");
- // test 26, testing isTriangular method
- data1 = new int[][] { { 1, 0 }, { 0, 1 } };
- mat1 = new MathMatrix(data1);
- if (mat1.isUpperTriangular())
- System.out.println("Passed test 26, upperTriangular method.");
- else
- System.out.println("Failed test 26, upperTriangular method");
- // test 27, testing isTriangular method
- data1 = new int[][] { { 1, 1, 1 }, { 1, 0, 0 }, { 0, 0, 0 } };
- mat1 = new MathMatrix(data1);
- if (!mat1.isUpperTriangular())
- System.out.println("Passed test 27. upperTriangular method.");
- else
- System.out.println("Failed test 27, upperTriangular method.");
- mat1 = new MathMatrix(32213, 32213, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement