//Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java //Mattrixwv // Created: 02-01-22 //Modified: 08-10-24 package com.mattrixwv.matrix; import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; import org.junit.jupiter.api.Test; import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; import com.mattrixwv.matrix.exceptions.InvalidGeometryException; import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; import com.mattrixwv.matrix.exceptions.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestIntegerMatrix{ private static final int[][] negativeGrid2 = new int[][]{ {-1, -2}, {-3, -4} }; private static final int[][] negativeGrid10 = new int[][]{ { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20}, {-21, -22, -23, -24, -25, -26, -27, -28, -29, -30}, {-31, -32, -33, -34, -35, -36, -37, -38, -39, -40}, {-41, -42, -43, -44, -45, -46, -47, -48, -49, -50}, {-51, -52, -53, -54, -55, -56, -57, -58, -59, -60}, {-61, -62, -63, -64, -65, -66, -67, -68, -69, -70}, {-71, -72, -73, -74, -75, -76, -77, -78, -79, -80}, {-81, -82, -83, -84, -85, -86, -87, -88, -89, -90}, {-91, -92, -93, -94, -95, -96, -97, -98, -99, -100} }; private static final int[][] negativeGrid2x10 = new int[][]{ { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; private static final int[][] negativeGrid10x2 = new int[][]{ { -1, -2}, { -3, -4}, { -5, -6}, { -7, -8}, { -9, -10}, {-11, -12}, {-13, -14}, {-15, -16}, {-17, -18}, {-19, -20} }; //! Constructor @Test public void testConstructor_default(){ IntegerMatrix matrix = new IntegerMatrix(); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_fill0Rows(){ assertThrows(InvalidGeometryException.class, () -> { new IntegerMatrix(0, 1, 0); }); } @Test public void testConstructor_fill0Cols(){ assertThrows(InvalidGeometryException.class, () -> { new IntegerMatrix(1, 0, 0); }); } @Test public void testConstructor_fillSize2(){ IntegerMatrix matrix = new IntegerMatrix(2, 2, -1); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertEquals(-1, matrix.get(0, 0)); assertEquals(-1, matrix.get(0, 1)); assertEquals(-1, matrix.get(1, 0)); assertEquals(-1, matrix.get(1, 1)); } @Test public void testConstructor_fillSize10(){ IntegerMatrix matrix = new IntegerMatrix(10, 10, -1); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(int[] row : matrix.copyGrid()){ for(int num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_arraySize0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_arraySize2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testConstructor_arraySize10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testConstructor_arraySize2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testConstructor_arraySize10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testConstructor_arrayUnevenRows(){ int[][] grid = new int[][]{ {-1, -2, -3}, {-4, -5}, {-6, -7, -8, -9}, {-10, -11, -12} }; assertThrows(InvalidRowSizeException.class, () -> { new IntegerMatrix(grid); }); } @Test public void testConstructor_matrixSize0(){ IntegerMatrix originalMatrix = new IntegerMatrix(); IntegerMatrix matrix = new IntegerMatrix(originalMatrix); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_matrixSize2(){ IntegerMatrix originalMatrix = new IntegerMatrix(2, 2, -1); IntegerMatrix matrix = new IntegerMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(int[] row : matrix.copyGrid()){ for(int num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10(){ IntegerMatrix originalMatrix = new IntegerMatrix(10, 10, -1); IntegerMatrix matrix = new IntegerMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(int[] row : matrix.copyGrid()){ for(int num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize2x10(){ IntegerMatrix originalMatrix = new IntegerMatrix(2, 10, -1); IntegerMatrix matrix = new IntegerMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(int[] row : matrix.copyGrid()){ for(int num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10x2(){ IntegerMatrix originalMatrix = new IntegerMatrix(10, 2, -1); IntegerMatrix matrix = new IntegerMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(int[] row : matrix.copyGrid()){ for(int num : row){ assertEquals(-1, num); } } } //! setGrid() @Test public void testSetGrid_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(grid); assertEquals(2, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2(){ IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testSetGrid_size10(){ IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testSetGrid_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testSetGrid_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(); matrix.setGrid(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testCopyGrid_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testCopyGrid_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } //! isSquare() @Test public void testIsSquare_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertFalse(matrix.isSquare()); } //! laplaceExpansionHelper() @Test public void testLaplaceExpansionHelper_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); assertArrayEquals(new int[][]{{-4}}, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size2_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(-1, 0); }); } @Test public void testLaplaceExpansionHelper_size2_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(2, 0); }); } @Test public void testLaplaceExpansionHelper_size2_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, -1); }); } @Test public void testLaplaceExpansionHelper_size2_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, 2); }); } @Test public void testLaplaceExpansionHelper_size10_loc0x0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); IntegerMatrix result = matrix.laplaceExpansionHelper(0, 0); int[][] expectedGrid = new int[][]{ {-12, -13, -14, -15, -16, -17, -18, -19, -20}, {-22, -23, -24, -25, -26, -27, -28, -29, -30}, {-32, -33, -34, -35, -36, -37, -38, -39, -40}, {-42, -43, -44, -45, -46, -47, -48, -49, -50}, {-52, -53, -54, -55, -56, -57, -58, -59, -60}, {-62, -63, -64, -65, -66, -67, -68, -69, -70}, {-72, -73, -74, -75, -76, -77, -78, -79, -80}, {-82, -83, -84, -85, -86, -87, -88, -89, -90}, {-92, -93, -94, -95, -96, -97, -98, -99, -100} }; assertArrayEquals(expectedGrid, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size10_loc4x4(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); IntegerMatrix result = matrix.laplaceExpansionHelper(4, 4); int[][] expectedGrid = new int[][]{ { -1, -2, -3, -4, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -16, -17, -18, -19, -20}, {-21, -22, -23, -24, -26, -27, -28, -29, -30}, {-31, -32, -33, -34, -36, -37, -38, -39, -40}, {-51, -52, -53, -54, -56, -57, -58, -59, -60}, {-61, -62, -63, -64, -66, -67, -68, -69, -70}, {-71, -72, -73, -74, -76, -77, -78, -79, -80}, {-81, -82, -83, -84, -86, -87, -88, -89, -90}, {-91, -92, -93, -94, -96, -97, -98, -99, -100} }; assertArrayEquals(expectedGrid, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } //! get() @Test public void testGet_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(2, 0); }); } @Test public void testGet_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(-1, 0); }); } @Test public void testGet_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, 2); }); } @Test public void testGet_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, -1); }); } @Test public void testGet(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(-1, matrix.get(0, 0)); assertEquals(-2, matrix.get(0, 1)); assertEquals(-3, matrix.get(1, 0)); assertEquals(-4, matrix.get(1, 1)); } //! getRow() @Test public void testGetRow_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(2); }); } @Test public void testGetRow_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(-1); }); } @Test public void testGetRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(new int[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); assertArrayEquals(new int[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); } //! getColumn() @Test public void testGetCol_0Rows(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(0); }); } @Test public void testGetCol_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(2); }); } @Test public void testGetCol_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(-1); }); } @Test public void testGetCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(new int[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); assertArrayEquals(new int[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); } //! getNumRows() @Test public void testGetNumRows_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); } @Test public void testGetNumRows_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); } //! getNumCols() @Test public void testGetNumCols_size0(){ int[][] grid = new int[0][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(2, matrix.getNumCols()); } @Test public void testGetNumCols_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertEquals(2, matrix.getNumCols()); } //! set() @Test public void testSet_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(-1, 0, 0); }); } @Test public void testSet_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(2, 0, 0); }); } @Test public void testSet_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, -1, 0); }); } @Test public void testSet_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, 2, 0); }); } @Test public void testSet(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.set(0, 0, -5); matrix.set(0, 1, -6); matrix.set(1, 0, -7); matrix.set(1, 1, -8); assertEquals(-5, matrix.get(0, 0)); assertEquals(-6, matrix.get(0, 1)); assertEquals(-7, matrix.get(1, 0)); assertEquals(-8, matrix.get(1, 1)); } //! setRow() @Test public void testSetRow_array_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new int[]{0}); }); } @Test public void testSetRow_array_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new int[]{0}); }); } @Test public void testSetRow_array_nullArray(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, (int[])null); }); } @Test public void testSetRow_array_arrayLength0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new int[0]); }); } @Test public void testSetRow_array_invalidArrayLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new int[]{0, 0, 0}); }); } @Test public void testSetRow_array(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.setRow(0, new int[]{-5, -6}); matrix.setRow(1, new int[]{-7, -8}); assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetRow_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setRow(0, (IntegerMatrix)null); }); } @Test public void testSetRow_matrix_multipleRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new IntegerMatrix(new int[][]{{0, 0}, {0, 0}})); }); } @Test public void testSetRow_matrix_negativeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new IntegerMatrix(new int[][]{{0}})); }); } @Test public void testSetRow_matrix_largeRow(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new IntegerMatrix(new int[][]{{0}})); }); } @Test public void testSetRow_matrix_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new IntegerMatrix(new int[][]{{}})); }); } @Test public void testSetRow_matrix_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new IntegerMatrix(new int[][]{{0, 0, 0}})); }); } @Test public void testSetRow_matrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.setRow(0, new IntegerMatrix(new int[][]{{-5, -6}})); matrix.setRow(1, new IntegerMatrix(new int[][]{{-7, -8}})); assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! setCol() @Test public void testSetCol_array_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new int[]{0}); }); } @Test public void testSetCol_array_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new int[]{0}); }); } @Test public void testSetCol_array_nullArray(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, (int[])null); }); } @Test public void testSetCol_array_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new int[0]); }); } @Test public void testSetCol_array_invalidArrayLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new int[]{0, 0, 0}); }); } @Test public void testSetCol_array(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.setCol(0, new int[]{-5, -7}); matrix.setCol(1, new int[]{-6, -8}); assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetCol_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setCol(0, (IntegerMatrix)null); }); } @Test public void testSetCol_matrix_multipleCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testSetCol_matrix_negativeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new IntegerMatrix(new int[][]{{0}})); }); } @Test public void testSetCol_matrix_largeCol(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new IntegerMatrix(new int[][]{{0}})); }); } @Test public void testSetCol_matrix_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new IntegerMatrix(new int[][]{{}})); }); } @Test public void testSetCol_matrix_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new IntegerMatrix(new int[][]{{0, 0, 0}})); }); } @Test public void testSetCol_matrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.setCol(0, new IntegerMatrix(new int[][]{{-5}, {-7}})); matrix.setCol(1, new IntegerMatrix(new int[][]{{-6}, {-8}})); assertArrayEquals(new int[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! addRow() @Test public void testAddRow_array_nullArray(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((int[])null); }); } @Test public void testAddRow_array_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new int[0]); }); } @Test public void testAddRow_array_invalidArrayLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new int[]{0, 0, 0}); }); } @Test public void testAddRow_array(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.addRow(new int[]{-5, -6}); assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } @Test public void testAddRow_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((IntegerMatrix)null); }); } @Test public void testAddRow_matrix_multipleRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}})); }); } @Test public void testAddRow_matrix_noRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new IntegerMatrix(new int[0][0])); }); } @Test public void testAddRow_matrix_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new IntegerMatrix(new int[][]{{0, 0, 0}})); }); } @Test public void testAddRow_matrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.addRow(new IntegerMatrix(new int[][]{{-5, -6}})); assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } //! addCol() @Test public void testAddCol_array_nullArray(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((int[])null); }); } @Test public void testAddCol_array_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new int[0]); }); } @Test public void testAddCol_array_invalidArrayLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new int[]{0, 0, 0}); }); } @Test public void testAddCol_array(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.addCol(new int[]{-5, -6}); assertArrayEquals(new int[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } @Test public void testAddCol_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((IntegerMatrix)null); }); } @Test public void testAddCol_matrix_multipleCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testAddCol_matrix_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new IntegerMatrix(new int[][]{{}})); }); } @Test public void testAddCol_matrix_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new IntegerMatrix(new int[][]{{0, 0, 0}})); }); } @Test public void testAddCol_matrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix.addCol(new IntegerMatrix(new int[][]{{-5}, {-6}})); assertArrayEquals(new int[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } //! appendRight() @Test public void testAppendRight_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendRight((IntegerMatrix)null); }); } @Test public void testAppendRight_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new IntegerMatrix(new int[0][0])); }); } @Test public void testAppendRight_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new IntegerMatrix(new int[][]{{0, 0, 0}})); }); } @Test public void testAppendRight(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.appendRight(matrix); assertArrayEquals(new int[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); } //! appendBottom() @Test public void testAppendBottom_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendBottom((IntegerMatrix)null); }); } @Test public void testAppendBottom_length0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new IntegerMatrix(new int[0][0])); }); } @Test public void testAppendBottom_invalidLength(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new IntegerMatrix(new int[][]{{0}, {0}, {0}})); }); } @Test public void testAppendBottom(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.appendBottom(matrix); assertArrayEquals(new int[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); } //! add() @Test public void testAdd_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.add((IntegerMatrix)null); }); } @Test public void testAdd_matrix_fewRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testAdd_matrix_manyRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new IntegerMatrix(new int[][]{{0}, {0}})); }); } @Test public void testAdd_matrix_fewCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new IntegerMatrix(new int[][]{{0}})); }); } @Test public void testAdd_matrix_manyCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testAdd_matrix_size2(){ IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{{-4, -3}, {-2, -1}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.add(addMatrix); assertArrayEquals(new int[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); } @Test public void testAdd_matrix_size10(){ IntegerMatrix addMatrix = new IntegerMatrix(new int[][]{ {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} }); IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); matrix = matrix.add(addMatrix); assertArrayEquals(new int[][]{ {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101}, {-101, -101, -101, -101, -101, -101, -101, -101, -101, -101} }, matrix.copyGrid()); } @Test public void testAdd_scalar(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.add(1); assertArrayEquals(new int[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); } //! subtract() @Test public void testSubtract_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.subtract((IntegerMatrix)null); }); } @Test public void testSubtract_matrix_fewRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testSubtract_matrix_manyRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testSubtract_matrix_fewCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new IntegerMatrix(new int[][]{{0}, {0}})); }); } @Test public void testSubtract_matrix_manyCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testSubtract_matrix_size2(){ IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{{-4, -3}, {-2, -1}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.subtract(subMatrix); assertArrayEquals(new int[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); } @Test public void testSubtract_matrix_size10(){ IntegerMatrix subMatrix = new IntegerMatrix(new int[][]{ {-100, -99, -98, -97, -96, -95, -94, -93, -92, -91}, { -90, -89, -88, -87, -86, -85, -84, -83, -82, -81}, { -80, -79, -78, -77, -76, -75, -74, -73, -72, -71}, { -70, -69, -68, -67, -66, -65, -64, -63, -62, -61}, { -60, -59, -58, -57, -56, -55, -54, -53, -52, -51}, { -50, -49, -48, -47, -46, -45, -44, -43, -42, -41}, { -40, -39, -38, -37, -36, -35, -34, -33, -32, -31}, { -30, -29, -28, -27, -26, -25, -24, -23, -22, -21}, { -20, -19, -18, -17, -16, -15, -14, -13, -12, -11}, { -10, -9, -8, -7, -6, -5, -4, -3, -2, -1} }); IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); matrix = matrix.subtract(subMatrix); assertArrayEquals(new int[][]{ { 99, 97, 95, 93, 91, 89, 87, 85, 83, 81}, { 79, 77, 75, 73, 71, 69, 67, 65, 63, 61}, { 59, 57, 55, 53, 51, 49, 47, 45, 43, 41}, { 39, 37, 35, 33, 31, 29, 27, 25, 23, 21}, { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1}, { -1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, {-21, -23, -25, -27, -29, -31, -33, -35, -37, -39}, {-41, -43, -45, -47, -49, -51, -53, -55, -57, -59}, {-61, -63, -65, -67, -69, -71, -73, -75, -77, -79}, {-81, -83, -85, -87, -89, -91, -93, -95, -97, -99} }, matrix.copyGrid()); } @Test public void testSubtract_scalar(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.subtract(1); assertArrayEquals(new int[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); } //! multiply() @Test public void testMultiply_matrix_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.multiply((IntegerMatrix)null); }); } @Test public void testMultiply_matrix_manyRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testMultiply_matrix_fewRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testMultiply_matrix_square(){ IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new int[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); } @Test public void testMultiply_matrix_rectangle(){ IntegerMatrix mulMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new int[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); } @Test public void testMultiply_scalar(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); matrix = matrix.multiply(2); assertArrayEquals(new int[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); } //! pow @Test public void testPow_rectangle(){ IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1, 2, 3}, {4, 5, 6}}); assertThrows(InvalidGeometryException.class, () -> { matrix.pow(2); }); } @Test public void testPow_negative(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidScalarException.class, () -> { matrix.pow(-1); }); } @Test public void testPow_0(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix result = matrix.pow(0); assertEquals(new IntegerMatrix(2, 2, 1), result); } @Test public void testPow_2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix result = matrix.pow(2); assertEquals(new IntegerMatrix(new int[][]{{7, 10}, {15, 22}}), result); } @Test public void testPow_3(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix result = matrix.pow(3); assertEquals(new IntegerMatrix(new int[][]{{-37, -54}, {-81, -118}}), result); } //! dotProduct() @Test public void testDotProduct_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.dotProduct((IntegerMatrix)null); }); } @Test public void testDotProduct_fewRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testDotProduct_manyRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testDotProduct_size2(){ IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); int result = matrix.dotProduct(dotMatrix); assertEquals(-54, result); } @Test public void testDotProduct_size10(){ IntegerMatrix dotMatrix = new IntegerMatrix(new int[][]{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} }); IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); int result = matrix.dotProduct(dotMatrix); assertEquals(-2632750, result); } //! hadamardProduct() @Test public void testHadamardProduct_nullMatrix(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.hadamardProduct((IntegerMatrix)null); }); } @Test public void testHadamardProduct_fewRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0}})); }); } @Test public void testHadamardProduct_manyRows(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testHadamardProduct_fewCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0}, {0}})); }); } @Test public void testHadamardProduct_manyCols(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new IntegerMatrix(new int[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testHadamardProduct_size2(){ IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{{1, 2}, {3, 4}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new int[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); } @Test public void testHadamardProduct_size10(){ IntegerMatrix hadMatrix = new IntegerMatrix(new int[][]{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}, {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}, {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}, {51, 52, 53, 54, 55, 56, 57, 58, 59, 60}, {61, 62, 63, 64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77, 78, 79, 80}, {81, 82, 83, 84, 85, 86, 87, 88, 89, 90}, {91, 92, 93, 94, 95, 96, 97, 98, 99, 100} }); IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); IntegerMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new int[][]{ { -1, -4, -9, -16, -25, -36, -49, -64, -81, -100}, { -121, -144, -169, -196, -225, -256, -289, -324, -361, -400}, { -441, -484, -529, -576, -625, -676, -729, -784, -841, -900}, { -961, -1024, -1089, -1156, -1225, -1296, -1369, -1444, -1521, -1600}, {-1681, -1764, -1849, -1936, -2025, -2116, -2209, -2304, -2401, -2500}, {-2601, -2704, -2809, -2916, -3025, -3136, -3249, -3364, -3481, -3600}, {-3721, -3844, -3969, -4096, -4225, -4356, -4489, -4624, -4761, -4900}, {-5041, -5184, -5329, -5476, -5625, -5776, -5929, -6084, -6241, -6400}, {-6561, -6724, -6889, -7056, -7225, -7396, -7569, -7744, -7921, -8100}, {-8281, -8464, -8649, -8836, -9025, -9216, -9409, -9604, -9801, -10000} }, result.copyGrid()); } //! transpose() @Test public void testTranspose_size0(){ IntegerMatrix matrix = new IntegerMatrix(); IntegerMatrix result = matrix.transpose(); assertEquals(new IntegerMatrix(), result); } @Test public void testTranspose_size0x2(){ int[][] grid = new int[0][2]; IntegerMatrix matrix = new IntegerMatrix(grid); assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2x0(){ int[][] grid = new int[2][0]; IntegerMatrix matrix = new IntegerMatrix(grid); assertArrayEquals(new int[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(new int[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); } @Test public void testTranspose_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertArrayEquals(new int[][]{ {-1, -11, -21, -31, -41, -51, -61, -71, -81, -91}, {-2, -12, -22, -32, -42, -52, -62, -72, -82, -92}, {-3, -13, -23, -33, -43, -53, -63, -73, -83, -93}, {-4, -14, -24, -34, -44, -54, -64, -74, -84, -94}, {-5, -15, -25, -35, -45, -55, -65, -75, -85, -95}, {-6, -16, -26, -36, -46, -56, -66, -76, -86, -96}, {-7, -17, -27, -37, -47, -57, -67, -77, -87, -97}, {-8, -18, -28, -38, -48, -58, -68, -78, -88, -98}, {-9, -19, -29, -39, -49, -59, -69, -79, -89, -99}, {-10, -20, -30, -40, -50, -60, -70, -80, -90, -100} }, matrix.transpose().copyGrid()); } @Test public void testTranspose_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertArrayEquals(new int[][]{ {-1, -11}, {-2, -12}, {-3, -13}, {-4, -14}, {-5, -15}, {-6, -16}, {-7, -17}, {-8, -18}, {-9, -19}, {-10, -20} }, matrix.transpose().copyGrid()); } @Test public void testTranspose_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertArrayEquals(new int[][]{ {-1, -3, -5, -7, -9, -11, -13, -15, -17, -19}, {-2, -4, -6, -8, -10, -12, -14, -16, -18, -20} }, matrix.transpose().copyGrid()); } //! determinant() / det() @Test public void testDeterminant_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size1(){ IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); assertEquals(1, matrix.determinant()); assertEquals(1, matrix.det()); } @Test public void testDeterminant_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(-2, matrix.determinant()); assertEquals(-2, matrix.det()); } @Test public void testDeterminant_size3(){ IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } //! cofactor() / cof() @Test public void testCofactor_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size1(){ int[][] expectedGrid = new int[][]{{1}}; IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2(){ int[][] expectedGrid = new int[][]{{-4, 3}, {2, -1}}; IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size10(){ int[][] expectedGrid = new int[][]{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } //! adjoint() / adj() @Test public void testAdjoint_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size1(){ int[][] expectedGrid = new int[][]{{1}}; IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2(){ int[][] expectedGrid = new int[][]{{-4, 2}, {3, -1}}; IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size10(){ int[][] expectedGrid = new int[][]{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} }; IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } //! inverse() @Test public void testInverse_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size1(){ int[][] expectedGrid = new int[][]{{-1}}; IntegerMatrix matrix = new IntegerMatrix(new int[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size2(){ int[][] expectedGrid = new int[][]{{-2, 3}, {3, -4}}; IntegerMatrix matrix = new IntegerMatrix(new int[][]{{4, 3}, {3, 2}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertThrows(InvalidScalarException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size2x10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size10x2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } //! equals() @Test public void testEquals_null(){ IntegerMatrix matrix = new IntegerMatrix(); assertFalse(matrix.equals(null)); assertFalse(matrix.equals((IntegerMatrix)null)); } @Test public void testEquals_matrixObject(){ IntegerMatrix equalsMatrix = new IntegerMatrix(negativeGrid2); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertTrue(matrix.equals((Object)equalsMatrix)); } @Test public void testEquals_array(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertTrue(matrix.equals(negativeGrid2)); } @Test public void testEquals_invalidType(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertFalse(matrix.equals(1)); } @Test public void testEquals_manyRows(){ IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2}, {-3, -4}, {-5, -6}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewRows(){ IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_manyCols(){ IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1, -2, -3}, {-4, -5, -6}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewCols(){ IntegerMatrix equalsMatrix = new IntegerMatrix(new int[][]{{-1}, {-2}}); IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_notEquals(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix equalsMatrix = matrix.transpose(); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_equals(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); IntegerMatrix equalsMatrix = new IntegerMatrix(negativeGrid2); assertTrue(matrix.equals(equalsMatrix)); } @Test public void testEquals_self(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertTrue(matrix.equals(matrix)); } //! hashCode() @Test public void testHashCode_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size1(){ IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } //! toString() @Test public void testToString_size0(){ IntegerMatrix matrix = new IntegerMatrix(); assertEquals("[]", matrix.toString()); } @Test public void testToString_size1(){ IntegerMatrix matrix = new IntegerMatrix(new int[][]{{1}}); assertEquals("[1]", matrix.toString()); } @Test public void testToString_size2(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid2); assertEquals("[-1, -2]\n[-3, -4]", matrix.toString()); } @Test public void testToString_size10(){ IntegerMatrix matrix = new IntegerMatrix(negativeGrid10); assertEquals( "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]\n" + "[-11, -12, -13, -14, -15, -16, -17, -18, -19, -20]\n" + "[-21, -22, -23, -24, -25, -26, -27, -28, -29, -30]\n" + "[-31, -32, -33, -34, -35, -36, -37, -38, -39, -40]\n" + "[-41, -42, -43, -44, -45, -46, -47, -48, -49, -50]\n" + "[-51, -52, -53, -54, -55, -56, -57, -58, -59, -60]\n" + "[-61, -62, -63, -64, -65, -66, -67, -68, -69, -70]\n" + "[-71, -72, -73, -74, -75, -76, -77, -78, -79, -80]\n" + "[-81, -82, -83, -84, -85, -86, -87, -88, -89, -90]\n" + "[-91, -92, -93, -94, -95, -96, -97, -98, -99, -100]", matrix.toString()); } //! generateIdentity() @Test public void testGenerateIdentity_size0(){ assertThrows(InvalidGeometryException.class, () -> { IntegerMatrix.generateIdentity(0); }); } @Test public void testGenerateIdentity_negativeSize(){ assertThrows(InvalidGeometryException.class, () -> { IntegerMatrix.generateIdentity(-1); }); } @Test public void testGenerateIdentity_size2(){ IntegerMatrix matrix = IntegerMatrix.generateIdentity(2); assertArrayEquals(new int[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size3(){ IntegerMatrix matrix = IntegerMatrix.generateIdentity(3); assertArrayEquals(new int[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size10(){ IntegerMatrix matrix = IntegerMatrix.generateIdentity(10); assertArrayEquals(new int[][]{ {1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} }, matrix.copyGrid()); } }