//Matrix/src/test/java/com/mattixwv/matrix/TestModMatrix.java //Mattrixwv // Created: 02-09-22 //Modified: 07-09-22 package com.mattrixwv.matrix; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; 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.InvalidScalarException; import com.mattrixwv.matrix.exceptions.NullMatrixException; public class TestModMatrix{ //Grid 1x1 private static final int[][] grid1 = { {1} }; private static final int[][] transformGrid1_1 = { {1} }; private static final int[][] transformGrid1_2 = { {2} }; //Grid 2x2 private static final int[][] grid2 = { {1, 2}, {1, 2} }; private static final int[][] transformGrid2_1 = { {1, 0}, {1, 0} }; private static final int[][] transformGrid2_2 = { {2, 3}, {2, 3} }; //Grid 3x3 private static final int[][] grid3 = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }; private static final int[][] transformGrid3_1 = { {2, 1, 0}, {2, 1, 0}, {2, 1, 0} }; private static final int[][] transformGrid3_2 = { {2, 3, 4}, {2, 3, 4}, {2, 3, 4} }; //Grid 4x4 private static final int[][] grid4 = { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }; private static final int[][] transformGrid4_1 = { {3, 2, 1, 0}, {3, 2, 1, 0}, {3, 2, 1, 0}, {3, 2, 1, 0} }; private static final int[][] transformGrid4_2 = { {2, 3, 4, 5}, {2, 3, 4, 5}, {2, 3, 4, 5}, {2, 3, 4, 5} }; //Grid 10x10 private static final int[][] grid10 = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }; private static final int[][] transformGrid10_1 = { {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} }; private static final int[][] transformGrid10_2 = { {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11} }; @Test public void testConstructor(){ assertThrows(InvalidScalarException.class, () -> { new ModMatrix(0); }); ModMatrix matrix = new ModMatrix(1); assertEquals(1, matrix.getMod()); } @Test public void testEquals(){ //Invalid equals ModMatrix matrix = new ModMatrix(grid1, 26); assertNotEquals(null, matrix); assertNotEquals(new double[0], matrix); //1x1 matrix = new ModMatrix(grid1, 26); assertTrue(matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals = matrix.equals(grid1); assertTrue(gridEquals); //2x2 matrix = new ModMatrix(grid2, 26); assertTrue(matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals2 = matrix.equals(grid2); assertTrue(gridEquals2); //false @SuppressWarnings("unlikely-arg-type") boolean gridEquals22 = matrix.equals(transformGrid2_1); assertFalse(gridEquals22); gridEquals2 = matrix.equals(new ModMatrix(grid3, 26)); assertFalse(gridEquals2); gridEquals = matrix.equals(new ModMatrix(new int[][]{ {0, 1, 2}, {0, 1, 2} }, 26)); assertFalse(gridEquals2); //3x3 matrix = new ModMatrix(grid3, 26); assertTrue(matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals3 = matrix.equals(grid3); assertTrue(gridEquals3); //4x4 matrix = new ModMatrix(grid4, 26); assertTrue(matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals4 = matrix.equals(grid4); assertTrue(gridEquals4); //10x10 matrix = new ModMatrix(grid10, 26); assertTrue(matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals10 = matrix.equals(grid10); assertTrue(gridEquals10); } @Test public void testGet(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); assertEquals(1, matrix.get(0, 0)); //mod assertEquals(26, matrix.getMod()); //Invalid gets final ModMatrix testMatrix = new ModMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(3, 3); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(-1, -1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(0, 3); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.get(0, -1); }); //2x2 matrix = new ModMatrix(grid2, 26); assertEquals(1, matrix.get(0, 0)); //3x3 matrix = new ModMatrix(grid3, 26); assertEquals(1, matrix.get(0, 0)); //4x4 matrix = new ModMatrix(grid4, 26); assertEquals(1, matrix.get(0, 0)); //10x10 matrix = new ModMatrix(grid10, 26); assertEquals(1, matrix.get(0, 0)); } @Test public void testGetRow(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, matrix.getRow(0)); //Invalid gets final ModMatrix testMatrix = new ModMatrix(matrix); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getRow(-1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getRow(3); }); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{{1, 2}}, 26); assertEquals(correctMatrix, matrix.getRow(0)); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{{1, 2, 3}}, 26); assertEquals(correctMatrix, matrix.getRow(0)); //4x4 matrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4}}, 26); assertEquals(correctMatrix, matrix.getRow(0)); //10x10 matrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 26); assertEquals(correctMatrix, matrix.getRow(0)); } @Test public void testGetCol(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, matrix.getCol(0)); //Invalid gets final ModMatrix testMatrix = new ModMatrix(26); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getCol(-1); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.getCol(3); }); final ModMatrix testMatrix2 = new ModMatrix(grid1, 26); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix2.getCol(3); }); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, }, 26); assertEquals(correctMatrix, matrix.getCol(0)); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, {1} }, 26); assertEquals(correctMatrix, matrix.getCol(0)); //4x4 matrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, {1}, {1} }, 26); assertEquals(correctMatrix, matrix.getCol(0)); //10x10 matrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1} }, 26); assertEquals(correctMatrix, matrix.getCol(0)); } @Test public void testSet(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); matrix.set(0, 0, 2); ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); assertEquals(correctMatrix, matrix); //Invalid sets final ModMatrix testMatrix = new ModMatrix(grid1, 26); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(-1, -1, 0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(2, 2, 0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(0, -1, 0); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.set(0, 2, 0); }); //2x2 matrix = new ModMatrix(grid2, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ {3, 2}, {1, 2} }, 26); assertEquals(correctMatrix, matrix); //3x3 matrix = new ModMatrix(grid3, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ {3, 2, 3}, {1, 2, 3}, {1, 2, 3}, }, 26); assertEquals(correctMatrix, matrix); //4x4 matrix = new ModMatrix(grid4, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ {3, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix); //10x10 matrix = new ModMatrix(grid10, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ {3, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals(correctMatrix, matrix); } @Test public void testSetRow(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); matrix.setRow(0, new int[]{0}); ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); assertEquals(correctMatrix, matrix); //Invalid setRows final ModMatrix testMatrix = new ModMatrix(grid1, 26); final int[] testGrid = {0, 0}; final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setRow(-1, testGrid); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setRow(2, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, (int[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.setRow(0, (ModMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setRow(0, testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); matrix.setRow(1, new int[]{2, 1}); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {2, 1} }, 26); assertEquals(correctMatrix, matrix); //Matrix ModMatrix matrix2 = new ModMatrix(new int[][]{{0, 0}}, 26); matrix.setRow(1, matrix2); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {0, 0} }, 26); assertEquals(correctMatrix, matrix); //Integer matrix IntegerMatrix matrix3 = new IntegerMatrix(new int[][]{{1, 1}}); matrix.setRow(1, matrix3); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {1, 1} }, 26); assertEquals(correctMatrix, matrix); //3x3 matrix = new ModMatrix(grid3, 26); matrix.setRow(0, new int[]{0, 1, 2}); correctMatrix = new ModMatrix(new int[][]{ {0, 1, 2}, {1, 2, 3}, {1, 2, 3} }, 26); assertEquals(correctMatrix, matrix); //4x4 matrix = new ModMatrix(grid4, 26); matrix.setRow(0, new int[]{4, 3, 2, 1}); correctMatrix = new ModMatrix(new int[][]{ {4, 3, 2, 1}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix); //10x10 matrix = new ModMatrix(grid10, 26); matrix.setRow(0, new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); correctMatrix = new ModMatrix(new int[][]{ {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals(correctMatrix, matrix); } @Test public void testSetCol(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); matrix.setCol(0, new int[]{1}); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, matrix); //Invalid setCols final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(26); final int[] testGrid = {0, 0}; assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(-1, testGrid); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(2, testGrid); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setCol(0, (int[])null); }); assertThrows(InvalidCoordinatesException.class, () -> { testMatrix.setCol(0, testGrid); }); assertThrows(NullMatrixException.class, () -> { testMatrix.setCol(0, (ModMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.setCol(0, testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); matrix.setCol(0, new int[]{3, 3}); correctMatrix = new ModMatrix(new int[][]{ {3, 2}, {3, 2} }, 26); assertEquals(correctMatrix, matrix); //Matrix ModMatrix vector = new ModMatrix(new int[][]{{0}, {0}}, 26); matrix.setCol(1, vector); correctMatrix = new ModMatrix(new int[][]{ {3, 0}, {3, 0} }, 26); assertEquals(correctMatrix, matrix); //Integer matrix IntegerMatrix vector2 = new IntegerMatrix(new int[][]{{1}, {1}}); matrix.setCol(1, vector2); correctMatrix = new ModMatrix(new int[][]{ {3, 1}, {3, 1} }, 26); assertEquals(correctMatrix, matrix); //3x3 matrix = new ModMatrix(grid3, 26); matrix.setCol(0, new int[]{0, 0, 0}); correctMatrix = new ModMatrix(new int[][]{ {0, 2, 3}, {0, 2, 3}, {0, 2, 3} }, 26); assertEquals(correctMatrix, matrix); //4x4 matrix = new ModMatrix(grid4, 26); matrix.setCol(0, new int[]{0, 0, 0, 0}); correctMatrix = new ModMatrix(new int[][]{ {0, 2, 3, 4}, {0, 2, 3, 4}, {0, 2, 3, 4}, {0, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix); //10x10 matrix = new ModMatrix(grid10, 26); matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); correctMatrix = new ModMatrix(new int[][]{ {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {0, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals(correctMatrix, matrix); } @Test public void testAddRow(){ //0x0 ModMatrix matrix = new ModMatrix(26); matrix.addRow(new int[]{0}); ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); assertEquals(correctMatrix, matrix); //1x1 matrix = new ModMatrix(grid1, 26); matrix.addRow(new int[]{1}); correctMatrix = new ModMatrix(new int[][]{{1}, {1}}, 26); assertEquals(correctMatrix, matrix); //Invalid addsd final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addRow(new int[]{0, 0}); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addRow((int[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addRow((ModMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addRow(testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); matrix.addRow(new int[]{1, 2}); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {1, 2}, {1, 2} }, 26); assertEquals(correctMatrix, matrix); //Matrix matrix = new ModMatrix(grid2, 26); matrix.addRow(new ModMatrix(new int[][]{{0, 0}}, 26)); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {1, 2}, {0, 0} }, 26); assertEquals(correctMatrix, matrix); //Integer matrix matrix = new ModMatrix(grid2, 26); matrix.addRow(new IntegerMatrix(new int[][]{{1, 1}})); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {1, 2}, {1, 1} }, 26); //3x3 matrix = new ModMatrix(grid3, 26); matrix.addRow(new int[]{1, 2, 3}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }, 26); assertEquals(correctMatrix, matrix); //4x4 matrix = new ModMatrix(grid4, 26); matrix.addRow(new int[]{1, 2, 3, 4}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix); //10x10 matrix = new ModMatrix(grid10, 26); matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals(correctMatrix, matrix); } @Test public void testAddCol(){ //0x0 ModMatrix matrix = new ModMatrix(26); matrix.addCol(new int[]{0}); ModMatrix correctMatrix = new ModMatrix(new int[][]{{0}}, 26); assertEquals(correctMatrix, matrix); //1x1 matrix = new ModMatrix(grid1, 26); matrix.addCol(new int[]{1}); correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); assertEquals(correctMatrix, matrix); //Invalid adds final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addCol(new int[]{0, 0}); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addCol((int[])null); }); assertThrows(NullMatrixException.class, () -> { testMatrix.addCol((ModMatrix)null); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.addCol(testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); matrix.addCol(new int[]{3, 3}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3}, {1, 2, 3} }, 26); assertEquals(correctMatrix, matrix); //Matrix matrix = new ModMatrix(grid2, 26); matrix.addCol(new ModMatrix(new int[][]{{0}, {0}}, 26)); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 0}, {1, 2, 0} }, 26); assertEquals(correctMatrix, matrix); //Integer matrix matrix = new ModMatrix(grid2, 26); matrix.addCol(new IntegerMatrix(new int[][]{{1}, {1}})); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 1}, {1, 2, 1} }, 26); assertEquals(correctMatrix, matrix); //3x3 matrix = new ModMatrix(grid3, 26); matrix.addCol(new int[]{4, 4, 4}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix); //4x4 matrix = new ModMatrix(grid4, 26); matrix.addCol(new int[]{5, 5, 5, 5}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5} }, 26); assertEquals(correctMatrix, matrix); //10x10 matrix = new ModMatrix(grid10, 26); matrix.addCol(new int[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} }, 26); assertEquals(correctMatrix, matrix); } @Test public void testAppendRight(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix secondMatrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); //Invalid appends final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.appendRight(testMatrix2); }); assertThrows(NullMatrixException.class, () -> { testMatrix.appendRight(null); }); //2x2 matrix = new ModMatrix(grid2, 26); secondMatrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 1, 2}, {1, 2, 1, 2} }, 26); assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); //Integer matrix IntegerMatrix thirdMatrix = new IntegerMatrix(grid2); assertEquals(correctMatrix, matrix.appendRight(thirdMatrix)); //3x3 matrix = new ModMatrix(grid3, 26); secondMatrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 1, 2, 3}, {1, 2, 3, 1, 2, 3}, {1, 2, 3, 1, 2, 3} }, 26); assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); //4x4 matrix = new ModMatrix(grid4, 26); secondMatrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 1, 2, 3, 4}, {1, 2, 3, 4, 1, 2, 3, 4}, {1, 2, 3, 4, 1, 2, 3, 4}, {1, 2, 3, 4, 1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); //10x10 matrix = new ModMatrix(grid10, 26); secondMatrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals(correctMatrix, matrix.appendRight(secondMatrix)); } @Test public void testAppendBottom(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix secondMatrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{ {1}, {1} }, 26); assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); //Invalid appends final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.appendBottom(testMatrix2); }); assertThrows(NullMatrixException.class, () -> { testMatrix.appendBottom(null); }); //2x2 matrix = new ModMatrix(grid2, 26); secondMatrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2}, {1, 2}, {1, 2}, {1, 2} }, 26); assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); //Integer matrix IntegerMatrix thirdMatrix = new IntegerMatrix(grid2); assertEquals(correctMatrix, matrix.appendBottom(thirdMatrix)); //3x3 matrix = new ModMatrix(grid3, 26); secondMatrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }, 26); assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 matrix = new ModMatrix(grid4, 26); secondMatrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 matrix = new ModMatrix(grid10, 26); secondMatrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, }, 26); assertEquals(correctMatrix, matrix.appendBottom(secondMatrix)); } @Test public void testIsSquare(){ ModMatrix matrix = new ModMatrix(26); assertFalse(matrix.isSquare()); matrix = new ModMatrix(2, 2, 0, 26); assertTrue(matrix.isSquare()); matrix = new ModMatrix(2, 3, 0, 26); assertFalse(matrix.isSquare()); } @Test public void testAddition(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix transformMatrix = new ModMatrix(transformGrid1_1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); assertEquals(correctMatrix, matrix.add(transformMatrix)); assertEquals(correctMatrix, matrix.add(1)); //Invalid adds final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); final ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.add(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.add(testMatrix3); }); //2x2 matrix = new ModMatrix(grid2, 26); transformMatrix = new ModMatrix(transformGrid2_1, 26); correctMatrix = new ModMatrix(new int[][]{ {2, 2}, {2, 2} }, 26); assertEquals(correctMatrix, matrix.add(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {2, 3}, {2, 3} }, 26); assertEquals(correctMatrix, matrix.add(1)); //Integer matrix IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_1); correctMatrix = new ModMatrix(new int[][]{ {2, 2}, {2, 2} }, 26); assertEquals(correctMatrix, matrix.add(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); transformMatrix = new ModMatrix(transformGrid3_1, 26); correctMatrix = new ModMatrix(new int[][]{ {3, 3, 3}, {3, 3, 3}, {3, 3, 3} }, 26); assertEquals(correctMatrix, matrix.add(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {2, 3, 4}, {2, 3, 4}, {2, 3, 4} }, 26); assertEquals(correctMatrix, matrix.add(1)); //4x4 matrix = new ModMatrix(grid4, 26); transformMatrix = new ModMatrix(transformGrid4_1, 26); correctMatrix = new ModMatrix(new int[][]{ {4, 4, 4, 4}, {4, 4, 4, 4}, {4, 4, 4, 4}, {4, 4, 4, 4} }, 26); assertEquals(correctMatrix, matrix.add(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {2, 3, 4, 5}, {2, 3, 4, 5}, {2, 3, 4, 5}, {2, 3, 4, 5} }, 26); assertEquals(correctMatrix, matrix.add(1)); //10x10 matrix = new ModMatrix(grid10, 26); transformMatrix = new ModMatrix(transformGrid10_1, 26); correctMatrix = new ModMatrix(new int[][]{ {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} }, 26); assertEquals(correctMatrix, matrix.add(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11} }, 26); assertEquals(correctMatrix, matrix.add(1)); } @Test public void testSubtraction(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix transformMatrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{ {0} }, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix)); assertEquals(correctMatrix, matrix.subtract(1)); //Invalid subtracts final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); final ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.subtract(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.subtract(testMatrix3); }); //2x2 matrix = new ModMatrix(grid2, 26); transformMatrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(2, 2, 0, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {0, 1}, {0, 1} }, 26); assertEquals(correctMatrix, matrix.subtract(1)); //Integer matrix IntegerMatrix transformMatrix2 = new IntegerMatrix(grid2); correctMatrix = new ModMatrix(2, 2, 0, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); transformMatrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(3, 3, 0, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {0, 1, 2}, {0, 1, 2}, {0, 1, 2} }, 26); assertEquals(correctMatrix, matrix.subtract(1)); //4x4 matrix = new ModMatrix(grid4, 26); transformMatrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(4, 4, 0, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3} }, 26); assertEquals(correctMatrix, matrix.subtract(1)); //10x10 matrix = new ModMatrix(grid10, 26); transformMatrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(10, 10, 0, 26); assertEquals(correctMatrix, matrix.subtract(transformMatrix)); correctMatrix = new ModMatrix(new int[][]{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} }, 26); assertEquals(correctMatrix, matrix.subtract(1)); } @Test public void testMultiplication(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); ModMatrix correctMatrix = new ModMatrix(transformGrid1_2, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix)); assertEquals(correctMatrix, matrix.multiply(2)); //Invalid multiplication final ModMatrix testMatrix = new ModMatrix(grid1, 26); final ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.multiply(testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); transformMatrix = new ModMatrix(transformGrid2_2, 26); correctMatrix = new ModMatrix(new int[][]{ {6, 9}, {6, 9} }, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix)); ModMatrix vector = new ModMatrix(new int[][]{ {2}, {3} }, 26); correctMatrix = new ModMatrix(new int[][]{ {8}, {8} }, 26); assertEquals(correctMatrix, matrix.multiply(vector)); correctMatrix = new ModMatrix(new int[][]{ {2, 4}, {2, 4} }, 26); assertEquals(correctMatrix, matrix.multiply(2)); //Integer matrix IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); correctMatrix = new ModMatrix(new int[][]{ {6, 9}, {6, 9} }, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); transformMatrix = new ModMatrix(transformGrid3_2, 26); correctMatrix = new ModMatrix(new int[][]{ {12, 18, 24}, {12, 18, 24}, {12, 18, 24} }, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix)); vector = new ModMatrix(new int[][]{ {2}, {3}, {4} }, 26); correctMatrix = new ModMatrix(new int[][]{ {20}, {20}, {20} }, 26); assertEquals(correctMatrix, matrix.multiply(vector)); correctMatrix = new ModMatrix(new int[][]{ {2, 4, 6}, {2, 4, 6}, {2, 4, 6} }, 26); assertEquals(correctMatrix, matrix.multiply(2)); //4x4 matrix = new ModMatrix(grid4, 26); transformMatrix = new ModMatrix(transformGrid4_2, 26); correctMatrix = new ModMatrix(new int[][]{ {20, 30, 40, 50}, {20, 30, 40, 50}, {20, 30, 40, 50}, {20, 30, 40, 50}, }, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix)); vector = new ModMatrix(new int[][]{ {2}, {3}, {4}, {5} }, 26); correctMatrix = new ModMatrix(new int[][]{ {40}, {40}, {40}, {40} }, 26); assertEquals(correctMatrix, matrix.multiply(vector)); correctMatrix = new ModMatrix(new int[][]{ {2, 4, 6, 8}, {2, 4, 6, 8}, {2, 4, 6, 8}, {2, 4, 6, 8} }, 26); assertEquals(correctMatrix, matrix.multiply(2)); //10x10 matrix = new ModMatrix(grid10, 26); transformMatrix = new ModMatrix(transformGrid10_2, 26); correctMatrix = new ModMatrix(new int[][]{ {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605}, {110, 165, 220, 275, 330, 385, 440, 495, 550, 605} }, 26); assertEquals(correctMatrix, matrix.multiply(transformMatrix)); vector = new ModMatrix(new int[][]{ {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11} }, 26); correctMatrix = new ModMatrix(new int[][]{ {440}, {440}, {440}, {440}, {440}, {440}, {440}, {440}, {440}, {440} }, 26); assertEquals(correctMatrix, matrix.multiply(vector)); correctMatrix = new ModMatrix(new int[][]{ {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} }, 26); assertEquals(correctMatrix, matrix.multiply(2)); } @Test public void testDotProduct(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); assertEquals(2, matrix.dotProduct(transformMatrix)); //Invalid products ModMatrix testMatrix = new ModMatrix(grid1, 26); ModMatrix testMatrix2 = new ModMatrix(grid2, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.dotProduct(testMatrix2); }); //2x2 matrix = new ModMatrix(grid2, 26); transformMatrix = new ModMatrix(transformGrid2_2, 26); assertEquals(30, matrix.dotProduct(transformMatrix)); //Integer matrix IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); assertEquals(30, matrix.dotProduct(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); transformMatrix = new ModMatrix(transformGrid3_2, 26); assertEquals(162, matrix.dotProduct(transformMatrix)); //4x4 matrix = new ModMatrix(grid4, 26); transformMatrix = new ModMatrix(transformGrid4_2, 26); assertEquals(560, matrix.dotProduct(transformMatrix)); //10x10 matrix = new ModMatrix(grid10, 26); transformMatrix = new ModMatrix(transformGrid10_2, 26); assertEquals(35750, matrix.dotProduct(transformMatrix)); } @Test public void testHadamardProduct(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); //Invalid hadamard products ModMatrix testMatrix = new ModMatrix(grid1, 26); ModMatrix testMatrix2 = new ModMatrix(new int[][]{{0}, {0}}, 26); ModMatrix testMatrix3 = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.hadamardProduct(testMatrix2); }); assertThrows(InvalidGeometryException.class, () -> { testMatrix.hadamardProduct(testMatrix3); }); //2x2 matrix = new ModMatrix(grid2, 26); transformMatrix = new ModMatrix(transformGrid2_2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, 6}, {2, 6} }, 26); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); //Integer matrix IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); transformMatrix = new ModMatrix(transformGrid3_2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, 6, 12}, {2, 6, 12}, {2, 6, 12} }, 26); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); //4x4 matrix = new ModMatrix(grid4, 26); transformMatrix = new ModMatrix(transformGrid4_2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, 6, 12, 20}, {2, 6, 12, 20}, {2, 6, 12, 20}, {2, 6, 12, 20} }, 26); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); //10x10 matrix = new ModMatrix(grid10, 26); transformMatrix = new ModMatrix(transformGrid10_2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110}, {2, 6, 12, 20, 30, 42, 56, 72, 90, 110} }, 26); assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix)); } @Test public void testTranspose(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, matrix.transpose()); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 1}, {2, 2} }, 26); assertEquals(correctMatrix, matrix.transpose()); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }, 26); assertEquals(correctMatrix, matrix.transpose()); //4x4 matrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4} }, 26); assertEquals(correctMatrix, matrix.transpose()); //10x10 matrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {4, 4, 4, 4, 4, 4, 4, 4, 4, 4}, {5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, {6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, {7, 7, 7, 7, 7, 7, 7, 7, 7, 7}, {8, 8, 8, 8, 8, 8, 8, 8, 8, 8}, {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, }, 26); assertEquals(correctMatrix, matrix.transpose()); } @Test public void testDeterminant(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); assertEquals(1, matrix.determinant()); //Invalid determinants ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.determinant(); }); //2x2 matrix = new ModMatrix(grid2, 26); assertEquals(0, matrix.determinant()); matrix = new ModMatrix(new int[][]{ {1, 4}, {4, 1} }, 26); assertEquals(-15, matrix.determinant()); //det assertEquals(matrix.determinant(), matrix.det()); //3x3 matrix = new ModMatrix(grid3, 26); assertEquals(0, matrix.determinant()); matrix = new ModMatrix(new int[][]{ {1, 4, 2}, {2, 4, 1}, {4, 1, 2} }, 26); assertEquals(-21, matrix.determinant()); //4x4 matrix = new ModMatrix(grid4, 26); assertEquals(0, matrix.determinant()); matrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {2, 3, 4, 1}, {3, 4, 1, 2}, {4, 1, 2, 3} }, 26); assertEquals(160, matrix.determinant()); //10x10 matrix = new ModMatrix(grid10, 26); assertEquals(0, matrix.determinant()); matrix = new ModMatrix(new int[][]{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 1}, {3, 4, 5, 6, 7, 8, 9, 10, 1, 2}, {4, 5, 6, 7, 8, 9, 10, 1, 2, 3}, {5, 6, 7, 8, 9, 10, 1, 2, 3, 4}, {6, 7, 8, 9, 10, 1, 2, 3, 4, 5}, {7, 8, 9, 10, 1, 2, 3, 4, 5, 6}, {8, 9, 10, 1, 2, 3, 4, 5, 6, 7}, {9, 10, 1, 2, 3, 4, 5, 6, 7, 8}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 1} }, 26); assertEquals(-10000000, matrix.determinant()); } @Test public void testCofactor(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(grid1, 26); assertEquals(correctMatrix, matrix.cofactor()); //Invalid cofactor ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.cofactor(); }); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, -1}, {-2, 1} }, 26); assertEquals(correctMatrix, matrix.cofactor()); //cof assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(3, 3, 0, 26); assertEquals(correctMatrix, matrix.cofactor()); matrix = new ModMatrix(new int[][]{ {1, 4, 2}, {2, 4, 1}, {4, 1, 2} }, 26); correctMatrix = new ModMatrix(new int[][]{ {7, 0, -14}, {-6, -6, 15}, {-4, 3, -4} }, 26); assertEquals(correctMatrix, matrix.cofactor()); //4x4 matrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(4, 4, 0, 26); assertEquals(correctMatrix, matrix.cofactor()); matrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {2, 3, 4, 1}, {3, 4, 1, 2}, {4, 1, 2, 3} }, 26); correctMatrix = new ModMatrix(new int[][]{ {-36, 4, 4, 44}, {4, 4, 44, -36}, {4, 44, -36, 4}, {44, -36, 4, 4} }, 26); assertEquals(correctMatrix, matrix.cofactor()); //10x10 //?Skipping 10x10 test because test took > 5s by itself /* matrix = new ModMatrix(grid10); correctMatrix = new ModMatrix(10, 10, 0); assertEquals(correctMatrix, matrix.cofactor()); */ } @Test public void testPower(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, matrix.pow(3)); //Invalid powers final ModMatrix testMatrix = new ModMatrix(new int[][]{{0}, {0}}, 26); final ModMatrix testMatrix2 = new ModMatrix(grid1, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.pow(1); }); assertThrows(InvalidScalarException.class, () -> { testMatrix2.pow(-1); }); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {9, 18}, {9, 18} }, 26); assertEquals(correctMatrix, matrix.pow(3)); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ {36, 72, 108}, {36, 72, 108}, {36, 72, 108} }, 26); assertEquals(correctMatrix, matrix.pow(3)); //4x4 //0 matrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1} }, 26); assertEquals(correctMatrix, matrix.pow(0)); //1 correctMatrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} }, 26); assertEquals(correctMatrix, matrix.pow(1)); //3 correctMatrix = new ModMatrix(new int[][]{ {100, 200, 300, 400}, {100, 200, 300, 400}, {100, 200, 300, 400}, {100, 200, 300, 400} }, 26); assertEquals(correctMatrix, matrix.pow(3)); //10x10 matrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}, {3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250} }, 26); assertEquals(correctMatrix, matrix.pow(3)); } @Test public void testAdjoint(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(grid1, 26); assertEquals(correctMatrix, matrix.adjoint()); //2x2 matrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ {2, -2}, {-1, 1} }, 26); assertEquals(correctMatrix, matrix.adjoint()); //adj assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(3, 3, 0, 26); assertEquals(correctMatrix, matrix.adjoint()); //4x4 matrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {2, 3, 4, 1}, {3, 4, 1, 2}, {4, 1, 2, 3} }, 26); correctMatrix = new ModMatrix(new int[][]{ {-36, 4, 4, 44}, {4, 4, 44, -36}, {4, 44, -36, 4}, {44, -36, 4, 4} }, 26); assertEquals(correctMatrix, matrix.adjoint()); //10x10 //?Skipping 10x10 test because test took > 5s by itself /* matrix = new ModMatrix(grid10); correctMatrix = new ModMatrix(10, 10, 0); assertEquals(correctMatrix, matrix.adjoint()); */ } @Test public void testInverse(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(grid1, 26); assertEquals(correctMatrix, matrix.inverse()); //Invalid inverse ModMatrix testMatrix = new ModMatrix(new int[][]{{0, 0}}, 26); ModMatrix testMatrix2 = new ModMatrix(new int[][]{ {1, 2, 3}, {1, 2, 3}, {1, 2, 3} }, 26); ModMatrix testMatrix3 = new ModMatrix(new int[][]{ {1, 1}, {0, 2} }, 26); assertThrows(InvalidGeometryException.class, () -> { testMatrix.inverse(); }); assertThrows(InvalidScalarException.class, () -> { testMatrix2.inverse(); }); assertThrows(InvalidScalarException.class, () -> { testMatrix3.inverse(); }); //2x2 matrix = new ModMatrix(new int[][]{ {1, 4}, {4, 1} }, 26); correctMatrix = new ModMatrix(new int[][]{ {19, 2}, {2, 19} }, 26); assertEquals(correctMatrix, matrix.inverse()); //3x3 matrix = new ModMatrix(new int[][]{ {1, 4, 2}, {2, 4, 1}, {4, 1, 2} }, 26); correctMatrix = new ModMatrix(new int[][]{ {17, 4, 20}, { 0, 4, 11}, {18, 3, 20} }, 26); assertEquals(correctMatrix, matrix.inverse()); //4x4 matrix = new ModMatrix(new int[][]{ {1, 2, 3, 4}, {4, 2, 1, 3}, {5, 1, 8, 7}, {5, 6, 4, 2} }, 26); correctMatrix = new ModMatrix(new int[][]{ {12, 24, 22, 19}, {21, 17, 3, 0}, { 9, 6, 22, 13}, {19, 7, 22, 5} }, 26); assertEquals(correctMatrix, matrix.inverse()); //10x10 //?Skipped 10x10 because it would take a long time to compute } @Test public void testGenerateIdentity(){ //0x0 assertThrows(InvalidGeometryException.class, () -> { ModMatrix.generateIdentity(0); }); //1x1 ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals(correctMatrix, ModMatrix.generateIdentity(1)); //2x2 correctMatrix = new ModMatrix(new int[][]{ {1, 0}, {0, 1} }, 26); assertEquals(correctMatrix, ModMatrix.generateIdentity(2)); //3x3 correctMatrix = new ModMatrix(new int[][]{ {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }, 26); assertEquals(correctMatrix, ModMatrix.generateIdentity(3)); //4x4 correctMatrix = new ModMatrix(new int[][]{ {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }, 26); assertEquals(correctMatrix, ModMatrix.generateIdentity(4)); //10x10 correctMatrix = new ModMatrix(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}, }, 26); assertEquals(correctMatrix, ModMatrix.generateIdentity(10)); } @Test public void testHashCode(){ ModMatrix matrix = new ModMatrix(26); assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); } @Test public void testToString(){ ModMatrix matrix = new ModMatrix(grid3, 26); String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]\nmod(26)"; assertEquals(matrixString, matrix.toString()); } }