//Matrix/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java //Mattrixwv // Created: 02-10-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 TestLongMatrix{ private static final long[][] negativeGrid2 = new long[][]{ {-1, -2}, {-3, -4} }; private static final long[][] negativeGrid10 = new long[][]{ { -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 long[][] negativeGrid2x10 = new long[][]{ { -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}, {-11, -12, -13, -14, -15, -16, -17, -18, -19, -20} }; private static final long[][] negativeGrid10x2 = new long[][]{ { -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(){ LongMatrix matrix = new LongMatrix(); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_fill0Rows(){ assertThrows(InvalidGeometryException.class, () -> { new LongMatrix(0, 1, 0); }); } @Test public void testConstructor_fill0Cols(){ assertThrows(InvalidGeometryException.class, () -> { new LongMatrix(1, 0, 0); }); } @Test public void testConstructor_fillSize2(){ LongMatrix matrix = new LongMatrix(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(){ LongMatrix matrix = new LongMatrix(10, 10, -1); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(long[] row : matrix.copyGrid()){ for(long num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_arraySize0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_arraySize2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testConstructor_arraySize10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testConstructor_arraySize2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testConstructor_arraySize10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testConstructor_arrayUnevenRows(){ long[][] grid = new long[][]{ {-1, -2, -3}, {-4, -5}, {-6, -7, -8, -9}, {-10, -11, -12} }; assertThrows(InvalidRowSizeException.class, () -> { new LongMatrix(grid); }); } @Test public void testConstructor_matrixSize0(){ LongMatrix originalMatrix = new LongMatrix(); LongMatrix matrix = new LongMatrix(originalMatrix); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testConstructor_matrixSize2(){ LongMatrix originalMatrix = new LongMatrix(2, 2, -1); LongMatrix matrix = new LongMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(long[] row : matrix.copyGrid()){ for(long num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10(){ LongMatrix originalMatrix = new LongMatrix(10, 10, -1); LongMatrix matrix = new LongMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(long[] row : matrix.copyGrid()){ for(long num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize2x10(){ LongMatrix originalMatrix = new LongMatrix(2, 10, -1); LongMatrix matrix = new LongMatrix(originalMatrix); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); for(long[] row : matrix.copyGrid()){ for(long num : row){ assertEquals(-1, num); } } } @Test public void testConstructor_matrixSize10x2(){ LongMatrix originalMatrix = new LongMatrix(10, 2, -1); LongMatrix matrix = new LongMatrix(originalMatrix); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); for(long[] row : matrix.copyGrid()){ for(long num : row){ assertEquals(-1, num); } } } //! setGrid() @Test public void testSetGrid_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(); matrix.setGrid(grid); assertEquals(0, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(); matrix.setGrid(grid); assertEquals(2, matrix.getNumRows()); assertEquals(0, matrix.getNumCols()); } @Test public void testSetGrid_size2(){ LongMatrix matrix = new LongMatrix(); matrix.setGrid(negativeGrid2); assertEquals(2, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testSetGrid_size10(){ LongMatrix matrix = new LongMatrix(); matrix.setGrid(negativeGrid10); assertEquals(10, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testSetGrid_size2x10(){ LongMatrix matrix = new LongMatrix(); matrix.setGrid(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); assertEquals(10, matrix.getNumCols()); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } @Test public void testSetGrid_size10x2(){ LongMatrix matrix = new LongMatrix(); matrix.setGrid(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); assertEquals(2, matrix.getNumCols()); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertArrayEquals(grid, matrix.copyGrid()); } @Test public void testCopyGrid_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(negativeGrid2, matrix.copyGrid()); } @Test public void testCopyGrid_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertArrayEquals(negativeGrid10, matrix.copyGrid()); } @Test public void testCopyGrid_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertArrayEquals(negativeGrid10x2, matrix.copyGrid()); } @Test public void testCopyGrid_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertArrayEquals(negativeGrid2x10, matrix.copyGrid()); } //! isSquare() @Test public void testIsSquare_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertTrue(matrix.isSquare()); } @Test public void testIsSquare_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertFalse(matrix.isSquare()); } @Test public void testIsSquare_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertFalse(matrix.isSquare()); } //! laplaceExpansionHelper() @Test public void testLaplaceExpansionHelper_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix result = matrix.laplaceExpansionHelper(0, 0); assertArrayEquals(new long[][]{{-4}}, result.copyGrid()); } @Test public void testLaplaceExpansionHelper_size2_negativeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(-1, 0); }); } @Test public void testLaplaceExpansionHelper_size2_largeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(2, 0); }); } @Test public void testLaplaceExpansionHelper_size2_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, -1); }); } @Test public void testLaplaceExpansionHelper_size2_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.laplaceExpansionHelper(0, 2); }); } @Test public void testLaplaceExpansionHelper_size10_loc0x0(){ LongMatrix matrix = new LongMatrix(negativeGrid10); LongMatrix result = matrix.laplaceExpansionHelper(0, 0); long[][] expectedGrid = new long[][]{ {-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(){ LongMatrix matrix = new LongMatrix(negativeGrid10); LongMatrix result = matrix.laplaceExpansionHelper(4, 4); long[][] expectedGrid = new long[][]{ { -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(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } @Test public void testLaplaceExpansionHelper_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.laplaceExpansionHelper(0, 0); }); } //! get() @Test public void testGet_largeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(2, 0); }); } @Test public void testGet_negativeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(-1, 0); }); } @Test public void testGet_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, 2); }); } @Test public void testGet_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.get(0, -1); }); } @Test public void testGet(){ LongMatrix matrix = new LongMatrix(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(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(2); }); } @Test public void testGetRow_negativeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getRow(-1); }); } @Test public void testGetRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(new long[][]{{-1, -2}}, matrix.getRow(0).copyGrid()); assertArrayEquals(new long[][]{{-3, -4}}, matrix.getRow(1).copyGrid()); } //! getColumn() @Test public void testGetCol_0Rows(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(0); }); } @Test public void testGetCol_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(2); }); } @Test public void testGetCol_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.getCol(-1); }); } @Test public void testGetCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(new long[][]{{-1}, {-3}}, matrix.getCol(0).copyGrid()); assertArrayEquals(new long[][]{{-2}, {-4}}, matrix.getCol(1).copyGrid()); } //! getNumRows() @Test public void testGetNumRows_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumRows()); } @Test public void testGetNumRows_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertEquals(10, matrix.getNumRows()); } @Test public void testGetNumRows_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertEquals(2, matrix.getNumRows()); } @Test public void testGetNumRows_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertEquals(10, matrix.getNumRows()); } //! getNumCols() @Test public void testGetNumCols_size0(){ long[][] grid = new long[0][0]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertEquals(0, matrix.getNumCols()); } @Test public void testGetNumCols_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals(2, matrix.getNumCols()); } @Test public void testGetNumCols_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertEquals(10, matrix.getNumCols()); } @Test public void testGetNumCols_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertEquals(2, matrix.getNumCols()); } //! set() @Test public void testSet_negativeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(-1, 0, 0); }); } @Test public void testSet_largeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(2, 0, 0); }); } @Test public void testSet_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, -1, 0); }); } @Test public void testSet_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.set(0, 2, 0); }); } @Test public void testSet(){ LongMatrix matrix = new LongMatrix(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(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new long[]{0}); }); } @Test public void testSetRow_array_largeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new long[]{0}); }); } @Test public void testSetRow_array_nullArray(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, (long[])null); }); } @Test public void testSetRow_array_arrayLength0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new long[0]); }); } @Test public void testSetRow_array_invalidArrayLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new long[]{0, 0, 0}); }); } @Test public void testSetRow_array(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.setRow(0, new long[]{-5, -6}); matrix.setRow(1, new long[]{-7, -8}); assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetRow_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setRow(0, (LongMatrix)null); }); } @Test public void testSetRow_matrix_multipleRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new LongMatrix(new long[][]{{0, 0}, {0, 0}})); }); } @Test public void testSetRow_matrix_negativeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(-1, new LongMatrix(new long[][]{{0}})); }); } @Test public void testSetRow_matrix_largeRow(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setRow(2, new LongMatrix(new long[][]{{0}})); }); } @Test public void testSetRow_matrix_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new LongMatrix(new long[][]{{}})); }); } @Test public void testSetRow_matrix_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setRow(0, new LongMatrix(new long[][]{{0, 0, 0}})); }); } @Test public void testSetRow_matrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.setRow(0, new LongMatrix(new long[][]{{-5, -6}})); matrix.setRow(1, new LongMatrix(new long[][]{{-7, -8}})); assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! setCol() @Test public void testSetCol_array_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new long[]{0}); }); } @Test public void testSetCol_array_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new long[]{0}); }); } @Test public void testSetCol_array_nullArray(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, (long[])null); }); } @Test public void testSetCol_array_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new long[0]); }); } @Test public void testSetCol_array_invalidArrayLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new long[]{0, 0, 0}); }); } @Test public void testSetCol_array(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.setCol(0, new long[]{-5, -7}); matrix.setCol(1, new long[]{-6, -8}); assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } @Test public void testSetCol_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.setCol(0, (LongMatrix)null); }); } @Test public void testSetCol_matrix_multipleCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testSetCol_matrix_negativeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(-1, new LongMatrix(new long[][]{{0}})); }); } @Test public void testSetCol_matrix_largeCol(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidCoordinatesException.class, () -> { matrix.setCol(2, new LongMatrix(new long[][]{{0}})); }); } @Test public void testSetCol_matrix_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new LongMatrix(new long[][]{{}})); }); } @Test public void testSetCol_matrix_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.setCol(0, new LongMatrix(new long[][]{{0, 0, 0}})); }); } @Test public void testSetCol_matrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.setCol(0, new LongMatrix(new long[][]{{-5}, {-7}})); matrix.setCol(1, new LongMatrix(new long[][]{{-6}, {-8}})); assertArrayEquals(new long[][]{{-5, -6}, {-7, -8}}, matrix.copyGrid()); } //! addRow() @Test public void testAddRow_array_nullArray(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((long[])null); }); } @Test public void testAddRow_array_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new long[0]); }); } @Test public void testAddRow_array_invalidArrayLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new long[]{0, 0, 0}); }); } @Test public void testAddRow_array(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.addRow(new long[]{-5, -6}); assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } @Test public void testAddRow_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addRow((LongMatrix)null); }); } @Test public void testAddRow_matrix_multipleRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new LongMatrix(new long[][]{{0, 0}, {0, 0}})); }); } @Test public void testAddRow_matrix_noRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new LongMatrix(new long[0][0])); }); } @Test public void testAddRow_matrix_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addRow(new LongMatrix(new long[][]{{0, 0, 0}})); }); } @Test public void testAddRow_matrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.addRow(new LongMatrix(new long[][]{{-5, -6}})); assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}, matrix.copyGrid()); } //! addCol() @Test public void testAddCol_array_nullArray(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((long[])null); }); } @Test public void testAddCol_array_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new long[0]); }); } @Test public void testAddCol_array_invalidArrayLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new long[]{0, 0, 0}); }); } @Test public void testAddCol_array(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.addCol(new long[]{-5, -6}); assertArrayEquals(new long[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } @Test public void testAddCol_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.addCol((LongMatrix)null); }); } @Test public void testAddCol_matrix_multipleCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testAddCol_matrix_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new LongMatrix(new long[][]{{}})); }); } @Test public void testAddCol_matrix_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.addCol(new LongMatrix(new long[][]{{0, 0, 0}})); }); } @Test public void testAddCol_matrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix.addCol(new LongMatrix(new long[][]{{-5}, {-6}})); assertArrayEquals(new long[][]{{-1, -2, -5}, {-3, -4, -6}}, matrix.copyGrid()); } //! appendRight() @Test public void testAppendRight_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendRight((LongMatrix)null); }); } @Test public void testAppendRight_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new LongMatrix(new long[0][0])); }); } @Test public void testAppendRight_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendRight(new LongMatrix(new long[][]{{0, 0, 0}})); }); } @Test public void testAppendRight(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.appendRight(matrix); assertArrayEquals(new long[][]{{-1, -2, -1, -2}, {-3, -4, -3, -4}}, matrix.copyGrid()); } //! appendBottom() @Test public void testAppendBottom_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.appendBottom((LongMatrix)null); }); } @Test public void testAppendBottom_length0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new LongMatrix(new long[0][0])); }); } @Test public void testAppendBottom_invalidLength(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.appendBottom(new LongMatrix(new long[][]{{0}, {0}, {0}})); }); } @Test public void testAppendBottom(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.appendBottom(matrix); assertArrayEquals(new long[][]{{-1, -2}, {-3, -4}, {-1, -2}, {-3, -4}}, matrix.copyGrid()); } //! add() @Test public void testAdd_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.add((LongMatrix)null); }); } @Test public void testAdd_matrix_fewRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testAdd_matrix_manyRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new LongMatrix(new long[][]{{0}, {0}})); }); } @Test public void testAdd_matrix_fewCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new LongMatrix(new long[][]{{0}})); }); } @Test public void testAdd_matrix_manyCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.add(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testAdd_matrix_size2(){ LongMatrix addMatrix = new LongMatrix(new long[][]{{-4, -3}, {-2, -1}}); LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.add(addMatrix); assertArrayEquals(new long[][]{{-5, -5}, {-5, -5}}, matrix.copyGrid()); } @Test public void testAdd_matrix_size10(){ LongMatrix addMatrix = new LongMatrix(new long[][]{ {-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} }); LongMatrix matrix = new LongMatrix(negativeGrid10); matrix = matrix.add(addMatrix); assertArrayEquals(new long[][]{ {-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(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.add(1); assertArrayEquals(new long[][]{{0, -1}, {-2, -3}}, matrix.copyGrid()); } //! subtract() @Test public void testSubtract_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.subtract((LongMatrix)null); }); } @Test public void testSubtract_matrix_fewRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testSubtract_matrix_manyRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testSubtract_matrix_fewCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new LongMatrix(new long[][]{{0}, {0}})); }); } @Test public void testSubtract_matrix_manyCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.subtract(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testSubtract_matrix_size2(){ LongMatrix subMatrix = new LongMatrix(new long[][]{{-4, -3}, {-2, -1}}); LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.subtract(subMatrix); assertArrayEquals(new long[][]{{3, 1}, {-1, -3}}, matrix.copyGrid()); } @Test public void testSubtract_matrix_size10(){ LongMatrix subMatrix = new LongMatrix(new long[][]{ {-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} }); LongMatrix matrix = new LongMatrix(negativeGrid10); matrix = matrix.subtract(subMatrix); assertArrayEquals(new long[][]{ { 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(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.subtract(1); assertArrayEquals(new long[][]{{-2, -3}, {-4, -5}}, matrix.copyGrid()); } //! multiply() @Test public void testMultiply_matrix_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.multiply((LongMatrix)null); }); } @Test public void testMultiply_matrix_manyRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testMultiply_matrix_fewRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.multiply(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testMultiply_matrix_square(){ LongMatrix mulMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new long[][]{{-7, -10}, {-15, -22}}, matrix.copyGrid()); } @Test public void testMultiply_matrix_rectangle(){ LongMatrix mulMatrix = new LongMatrix(new long[][]{{1, 2, 3}, {4, 5, 6}}); LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.multiply(mulMatrix); assertArrayEquals(new long[][]{{-9, -12, -15}, {-19, -26, -33}}, matrix.copyGrid()); } @Test public void testMultiply_scalar(){ LongMatrix matrix = new LongMatrix(negativeGrid2); matrix = matrix.multiply(2); assertArrayEquals(new long[][]{{-2, -4}, {-6, -8}}, matrix.copyGrid()); } //! pow @Test public void testPow_rectangle(){ LongMatrix matrix = new LongMatrix(new long[][]{{1, 2, 3}, {4, 5, 6}}); assertThrows(InvalidGeometryException.class, () -> { matrix.pow(2); }); } @Test public void testPow_negative(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidScalarException.class, () -> { matrix.pow(-1); }); } @Test public void testPow_0(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix result = matrix.pow(0); assertEquals(new LongMatrix(2, 2, 1), result); } @Test public void testPow_2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix result = matrix.pow(2); assertEquals(new LongMatrix(new long[][]{{7, 10}, {15, 22}}), result); } @Test public void testPow_3(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix result = matrix.pow(3); assertEquals(new LongMatrix(new long[][]{{-37, -54}, {-81, -118}}), result); } //! dotProduct() @Test public void testDotProduct_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.dotProduct((LongMatrix)null); }); } @Test public void testDotProduct_fewRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testDotProduct_manyRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.dotProduct(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testDotProduct_size2(){ LongMatrix dotMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); LongMatrix matrix = new LongMatrix(negativeGrid2); long result = matrix.dotProduct(dotMatrix); assertEquals(-54, result); } @Test public void testDotProduct_size10(){ LongMatrix dotMatrix = new LongMatrix(new long[][]{ { 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} }); LongMatrix matrix = new LongMatrix(negativeGrid10); long result = matrix.dotProduct(dotMatrix); assertEquals(-2632750, result); } //! hadamardProduct() @Test public void testHadamardProduct_nullMatrix(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(NullMatrixException.class, () -> { matrix.hadamardProduct((LongMatrix)null); }); } @Test public void testHadamardProduct_fewRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0}})); }); } @Test public void testHadamardProduct_manyRows(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0}, {0, 0}, {0, 0}})); }); } @Test public void testHadamardProduct_fewCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new LongMatrix(new long[][]{{0}, {0}})); }); } @Test public void testHadamardProduct_manyCols(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertThrows(InvalidGeometryException.class, () -> { matrix.hadamardProduct(new LongMatrix(new long[][]{{0, 0, 0}, {0, 0, 0}})); }); } @Test public void testHadamardProduct_size2(){ LongMatrix hadMatrix = new LongMatrix(new long[][]{{1, 2}, {3, 4}}); LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new long[][]{{-1, -4}, {-9, -16}}, result.copyGrid()); } @Test public void testHadamardProduct_size10(){ LongMatrix hadMatrix = new LongMatrix(new long[][]{ { 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} }); LongMatrix matrix = new LongMatrix(negativeGrid10); LongMatrix result = matrix.hadamardProduct(hadMatrix); assertArrayEquals(new long[][]{ { -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(){ LongMatrix matrix = new LongMatrix(); LongMatrix result = matrix.transpose(); assertEquals(new LongMatrix(), result); } @Test public void testTranspose_size0x2(){ long[][] grid = new long[0][2]; LongMatrix matrix = new LongMatrix(grid); assertArrayEquals(new long[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2x0(){ long[][] grid = new long[2][0]; LongMatrix matrix = new LongMatrix(grid); assertArrayEquals(new long[0][0], matrix.transpose().copyGrid()); } @Test public void testTranspose_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(new long[][]{{-1, -3}, {-2, -4}}, matrix.transpose().copyGrid()); } @Test public void testTranspose_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertArrayEquals(new long[][]{ {-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(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertArrayEquals(new long[][]{ {-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(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertArrayEquals(new long[][]{ {-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(){ LongMatrix matrix = new LongMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size1(){ LongMatrix matrix = new LongMatrix(new long[][]{{1}}); assertEquals(1, matrix.determinant()); assertEquals(1, matrix.det()); } @Test public void testDeterminant_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals(-2, matrix.determinant()); assertEquals(-2, matrix.det()); } @Test public void testDeterminant_size3(){ LongMatrix matrix = new LongMatrix(new long[][]{{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}}); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertEquals(0, matrix.determinant()); assertEquals(0, matrix.det()); } @Test public void testDeterminant_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } @Test public void testDeterminant_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.determinant(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.det(); }); } //! cofactor() / cof() @Test public void testCofactor_size0(){ LongMatrix matrix = new LongMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size1(){ long[][] expectedGrid = new long[][]{{1}}; LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2(){ long[][] expectedGrid = new long[][]{{-4, 3}, {2, -1}}; LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size10(){ long[][] expectedGrid = new long[][]{ {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}, }; LongMatrix matrix = new LongMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.cofactor().copyGrid()); assertArrayEquals(expectedGrid, matrix.cof().copyGrid()); } @Test public void testCofactor_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } @Test public void testCofactor_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.cofactor(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.cof(); }); } //! adjoint() / adj() @Test public void testAdjoint_size0(){ LongMatrix matrix = new LongMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size1(){ long[][] expectedGrid = new long[][]{{1}}; LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2(){ long[][] expectedGrid = new long[][]{{-4, 2}, {3, -1}}; LongMatrix matrix = new LongMatrix(negativeGrid2); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size10(){ long[][] expectedGrid = new long[][]{ {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} }; LongMatrix matrix = new LongMatrix(negativeGrid10); assertArrayEquals(expectedGrid, matrix.adjoint().copyGrid()); assertArrayEquals(expectedGrid, matrix.adj().copyGrid()); } @Test public void testAdjoint_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } @Test public void testAdjoint_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.adjoint(); }); assertThrows(InvalidGeometryException.class, () -> { matrix.adj(); }); } //! inverse() @Test public void testInverse_size0(){ LongMatrix matrix = new LongMatrix(); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size1(){ long[][] expectedGrid = new long[][]{{-1}}; LongMatrix matrix = new LongMatrix(new long[][]{{-1}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size2(){ long[][] expectedGrid = new long[][]{{-2, 3}, {3, -4}}; LongMatrix matrix = new LongMatrix(new long[][]{{4, 3}, {3, 2}}); assertArrayEquals(expectedGrid, matrix.inverse().copyGrid()); } @Test public void testInverse_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertThrows(InvalidScalarException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size2x10(){ LongMatrix matrix = new LongMatrix(negativeGrid2x10); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } @Test public void testInverse_size10x2(){ LongMatrix matrix = new LongMatrix(negativeGrid10x2); assertThrows(InvalidGeometryException.class, () -> { matrix.inverse(); }); } //! equals() @Test public void testEquals_null(){ LongMatrix matrix = new LongMatrix(); assertFalse(matrix.equals(null)); } @Test public void testEquals_matrixObject(){ LongMatrix equalsMatrix = new LongMatrix(negativeGrid2); LongMatrix matrix = new LongMatrix(negativeGrid2); assertTrue(matrix.equals((Object)equalsMatrix)); } @Test public void testEquals_array(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertTrue(matrix.equals(negativeGrid2)); } @Test public void testEquals_invalidType(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertFalse(matrix.equals(1)); } @Test public void testEquals_manyRows(){ LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2}, {-3, -4}, {-5, -6}}); LongMatrix matrix = new LongMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewRows(){ LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2}}); LongMatrix matrix = new LongMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_manyCols(){ LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1, -2, -3}, {-4, -5, -6}}); LongMatrix matrix = new LongMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_fewCols(){ LongMatrix equalsMatrix = new LongMatrix(new long[][]{{-1}, {-2}}); LongMatrix matrix = new LongMatrix(negativeGrid2); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_notEquals(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix equalsMatrix = matrix.transpose(); assertFalse(matrix.equals(equalsMatrix)); } @Test public void testEquals_equals(){ LongMatrix matrix = new LongMatrix(negativeGrid2); LongMatrix equalsMatrix = new LongMatrix(negativeGrid2); assertTrue(matrix.equals(equalsMatrix)); } @Test public void testEquals_self(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertTrue(matrix.equals(matrix)); } //! hashCode() @Test public void testHashCode_size0(){ LongMatrix matrix = new LongMatrix(); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size1(){ LongMatrix matrix = new LongMatrix(new long[][]{{1}}); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } @Test public void testHashCode_size10(){ LongMatrix matrix = new LongMatrix(negativeGrid10); assertEquals(Arrays.hashCode(matrix.grid), matrix.hashCode()); } //! toString() @Test public void testToString_size0(){ LongMatrix matrix = new LongMatrix(); assertEquals("[]", matrix.toString()); } @Test public void testToString_size1(){ LongMatrix matrix = new LongMatrix(new long[][]{{1}}); assertEquals("[1]", matrix.toString()); } @Test public void testToString_size2(){ LongMatrix matrix = new LongMatrix(negativeGrid2); assertEquals("[-1, -2]\n[-3, -4]", matrix.toString()); } @Test public void testToString_size10(){ LongMatrix matrix = new LongMatrix(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, () -> { LongMatrix.generateIdentity(0); }); } @Test public void testGenerateIdentity_negativeSize(){ assertThrows(InvalidGeometryException.class, () -> { LongMatrix.generateIdentity(-1); }); } @Test public void testGenerateIdentity_size2(){ LongMatrix matrix = LongMatrix.generateIdentity(2); assertArrayEquals(new long[][]{{1, 0}, {0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size3(){ LongMatrix matrix = LongMatrix.generateIdentity(3); assertArrayEquals(new long[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, matrix.copyGrid()); } @Test public void testGenerateIdentity_size10(){ LongMatrix matrix = LongMatrix.generateIdentity(10); assertArrayEquals(new long[][]{ {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()); } }