From 4b738d28175dcbcf210d4829568981dada74fd8e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 3 Jul 2022 17:45:08 -0400 Subject: [PATCH] Updated for SonarQube findings --- .../mattrixwv/matrix/BigIntegerMatrix.java | 89 +- .../com/mattrixwv/matrix/DoubleMatrix.java | 103 +- .../com/mattrixwv/matrix/IntegerMatrix.java | 90 +- .../java/com/mattrixwv/matrix/LongMatrix.java | 90 +- .../java/com/mattrixwv/matrix/ModMatrix.java | 8 +- .../exceptions/NullMatrixException.java | 23 + .../matrix/TestBigIntegerMatrix.java | 889 +++++++++++++---- .../mattrixwv/matrix/TestDoubleMatrix.java | 927 ++++++++++++++---- .../mattrixwv/matrix/TestIntegerMatrix.java | 882 +++++++++++++---- .../com/mattrixwv/matrix/TestLongMatrix.java | 882 +++++++++++++---- .../com/mattrixwv/matrix/TestModMatrix.java | 875 +++++++++++++---- 11 files changed, 3896 insertions(+), 962 deletions(-) create mode 100644 src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java diff --git a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java index 70eff5d..c0cc6fc 100644 --- a/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/BigIntegerMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 06-29-22 +//Modified: 06-30-22 package com.mattrixwv.matrix; @@ -13,6 +13,7 @@ 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 BigIntegerMatrix{ @@ -142,9 +143,15 @@ public class BigIntegerMatrix{ if(row >= grid.length){ throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); } + else if(row < 0){ + throw new InvalidCoordinatesException("Row cannot be less than 0"); + } else if(col >= grid[0].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } + else if(col < 0){ + throw new InvalidCoordinatesException("Column cannot be less than 0"); + } //Return the location in the grid return grid[row][col]; @@ -163,12 +170,7 @@ public class BigIntegerMatrix{ return new BigIntegerMatrix(newRow); } public int getNumRows(){ - if(grid == null){ - return 0; - } - else{ - return grid.length; - } + return grid.length; } public BigIntegerMatrix getCol(int col){ //Make sure the column number is valid @@ -193,6 +195,7 @@ public class BigIntegerMatrix{ return 0; } } + //Sets public void set(int row, int col, BigInteger value){ //Make sure the row number is valid @@ -213,7 +216,10 @@ public class BigIntegerMatrix{ throw new InvalidCoordinatesException("Invalid row number " + row); } //Make sure the number of elements is valid - if(elements.length != getNumCols()){ + if(elements == null){ + throw new InvalidGeometryException("Row cannot be null"); + } + else if(elements.length != getNumCols()){ throw new InvalidGeometryException(elements.length, getNumCols()); } @@ -221,6 +227,10 @@ public class BigIntegerMatrix{ grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, BigIntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); @@ -235,7 +245,10 @@ public class BigIntegerMatrix{ throw new InvalidCoordinatesException("Invalid column number " + col); } //Make sure the number of elements is valid - if(elements.length != grid.length){ + if(elements == null){ + throw new InvalidGeometryException("Column cannot be null"); + } + else if(elements.length != grid.length){ throw new InvalidCoordinatesException(elements.length, grid.length); } @@ -245,18 +258,32 @@ public class BigIntegerMatrix{ } } public void setCol(int col, BigIntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column"); } + BigInteger[] vector = new BigInteger[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.grid[cnt][0]; + } + //Set the column - setCol(col, matrix.getCol(0)); + setCol(col, vector); } + //Adds public void addRow(BigInteger[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of columns is valid - if((grid.length == 0) || (getNumCols() == elements.length)){ + else if((grid.length == 0) || (getNumCols() == elements.length)){ BigInteger[][] newGrid = new BigInteger[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ @@ -272,8 +299,12 @@ public class BigIntegerMatrix{ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(BigIntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row - if(matrix.getNumRows() != 1){ + else if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row"); } @@ -281,9 +312,16 @@ public class BigIntegerMatrix{ addRow(matrix.grid[0]); } public void addCol(BigInteger[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of rows is valid if(grid.length == 0){ grid = new BigInteger[elements.length][1]; + for(int row = 0;row < grid.length;++row){ + grid[row][0] = elements[row]; + } } else if(grid.length == elements.length){ //Copy all existing data into the new grid @@ -297,17 +335,30 @@ public class BigIntegerMatrix{ } } public void addCol(BigIntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column"); } + BigInteger[] vector = new BigInteger[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.get(cnt, 0); + } + //Add the column - addCol(matrix.getCol(0)); + addCol(vector); } public BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of rows - if(getNumRows() != rightSide.getNumRows()){ + else if(getNumRows() != rightSide.getNumRows()){ throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows()); } @@ -327,8 +378,12 @@ public class BigIntegerMatrix{ return new BigIntegerMatrix(newGrid); } public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of columns - if(getNumCols() != rightSide.getNumCols()){ + else if(getNumCols() != rightSide.getNumCols()){ throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); } diff --git a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java index 60dd376..48fac2f 100644 --- a/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/matrix/DoubleMatrix.java //Mattrixwv // Created: 02-07-22 -//Modified: 06-29-22 +//Modified: 06-30-22 package com.mattrixwv.matrix; @@ -12,6 +12,7 @@ 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 DoubleMatrix{ @@ -146,9 +147,15 @@ public class DoubleMatrix{ if(row >= grid.length){ throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); } + else if(row < 0){ + throw new InvalidCoordinatesException("Row cannot be less than 0"); + } else if(col >= grid[0].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } + else if(col < 0){ + throw new InvalidCoordinatesException("Column cannot be less than 0"); + } //Return the location in the grid return grid[row][col]; @@ -167,12 +174,7 @@ public class DoubleMatrix{ return new DoubleMatrix(newRow); } public int getNumRows(){ - if(grid == null){ - return 0; - } - else{ - return grid.length; - } + return grid.length; } public DoubleMatrix getCol(int col){ //Make sure the column number is valid @@ -197,6 +199,7 @@ public class DoubleMatrix{ return 0; } } + //Sets public void set(int row, int col, double value){ //Make sure the row number is valid @@ -217,7 +220,10 @@ public class DoubleMatrix{ throw new InvalidCoordinatesException("Invalid row number " + row); } //Make sure the number of elements is valid - if(elements.length != getNumCols()){ + if(elements == null){ + throw new InvalidGeometryException("Row cannot be null"); + } + else if(elements.length != getNumCols()){ throw new InvalidGeometryException(elements.length, getNumCols()); } @@ -225,6 +231,10 @@ public class DoubleMatrix{ grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, DoubleMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); @@ -239,7 +249,10 @@ public class DoubleMatrix{ throw new InvalidCoordinatesException("Invalid column number " + col); } //Make sure the number of elements is valid - if(elements.length != grid.length){ + if(elements == null){ + throw new InvalidGeometryException("Column cannot be null"); + } + else if(elements.length != grid.length){ throw new InvalidCoordinatesException(elements.length, grid.length); } @@ -249,18 +262,32 @@ public class DoubleMatrix{ } } public void setCol(int col, DoubleMatrix matrix){ + //Make sure teh matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column"); } + double[] vector = new double[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.grid[cnt][0]; + } + //Set the column - setCol(col, matrix.getCol(0)); + setCol(col, vector); } + //Adds public void addRow(double[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of columns is valid - if((grid.length == 0) || (getNumCols() == elements.length)){ + else if((grid.length == 0) || (getNumCols() == elements.length)){ double[][] newGrid = new double[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ @@ -276,8 +303,12 @@ public class DoubleMatrix{ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(DoubleMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row - if(matrix.getNumRows() != 1){ + else if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row"); } @@ -285,9 +316,16 @@ public class DoubleMatrix{ addRow(matrix.grid[0]); } public void addCol(double[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of rows is valid if(grid.length == 0){ grid = new double[elements.length][1]; + for(int row = 0;row < grid.length;++row){ + grid[row][0] = elements[row]; + } } else if(grid.length == elements.length){ //Copy all existing data into the new grid @@ -301,17 +339,30 @@ public class DoubleMatrix{ } } public void addCol(DoubleMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column"); } + double[] vector = new double[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.get(cnt, 0); + } + //Add the column - addCol(matrix.getCol(0)); + addCol(vector); } public DoubleMatrix appendRight(DoubleMatrix rightSide){ + //Make sure teh matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of rows - if(getNumRows() != rightSide.getNumRows()){ + else if(getNumRows() != rightSide.getNumRows()){ throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows()); } @@ -331,8 +382,12 @@ public class DoubleMatrix{ return new DoubleMatrix(newGrid); } public DoubleMatrix appendBottom(DoubleMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of columns - if(getNumCols() != rightSide.getNumCols()){ + else if(getNumCols() != rightSide.getNumCols()){ throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); } @@ -379,20 +434,6 @@ public class DoubleMatrix{ throw new InvalidGeometryException("An identity matrix must have a size > 0"); } } - public static DoubleMatrix generateFilled(int size, double fill){ - //Create a grid with the correct size - double[][] newGrid = new double[size][size]; - - //Set each element in the grid - for(int row = 0;row < size;++row){ - for(int col = 0;col < size;++col){ - newGrid[row][col] = fill; - } - } - - //Return the new matrix - return new DoubleMatrix(newGrid); - } public DoubleMatrix add(DoubleMatrix rightSide){ //Make sure the matrices have compatable geometry if((getNumRows() != rightSide.getNumRows()) || (getNumCols() != rightSide.getNumCols())){ diff --git a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java index 2d16c04..402c3ed 100644 --- a/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/IntegerMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/IntegerMatrix.java //Mattrixwv // Created: 02-01-22 -//Modified: 06-29-22 +//Modified: 06-30-22 package com.mattrixwv.matrix; @@ -12,6 +12,7 @@ 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 IntegerMatrix{ @@ -141,9 +142,15 @@ public class IntegerMatrix{ if(row >= grid.length){ throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); } + else if(row < 0){ + throw new InvalidCoordinatesException("Row cannot be less than 0"); + } else if(col >= grid[0].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } + else if(col < 0){ + throw new InvalidCoordinatesException("Column cannot be less than 0"); + } //Return the location in the grid return grid[row][col]; @@ -162,12 +169,7 @@ public class IntegerMatrix{ return new IntegerMatrix(newRow); } public int getNumRows(){ - if(grid == null){ - return 0; - } - else{ - return grid.length; - } + return grid.length; } public IntegerMatrix getCol(int col){ //Make sure the column number is valid @@ -192,6 +194,7 @@ public class IntegerMatrix{ return 0; } } + //Sets public void set(int row, int col, int value){ //Make sure the row number is valid @@ -212,7 +215,10 @@ public class IntegerMatrix{ throw new InvalidCoordinatesException("Invalid row number " + row); } //Make sure the number of elements is valid - if(elements.length != getNumCols()){ + if(elements == null){ + throw new InvalidGeometryException("Row cannot be null"); + } + else if(elements.length != getNumCols()){ throw new InvalidGeometryException(elements.length, getNumCols()); } @@ -220,6 +226,10 @@ public class IntegerMatrix{ grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, IntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); @@ -234,7 +244,10 @@ public class IntegerMatrix{ throw new InvalidCoordinatesException("Invalid column number " + col); } //Make sure the number of elements is valid - if(elements.length != grid.length){ + if(elements == null){ + throw new InvalidGeometryException("Column cannot be null"); + } + else if(elements.length != grid.length){ throw new InvalidCoordinatesException(elements.length, grid.length); } @@ -244,18 +257,32 @@ public class IntegerMatrix{ } } public void setCol(int col, IntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column"); } + int[] vector = new int[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.grid[cnt][0]; + } + //Set the column - setCol(col, matrix.getCol(0)); + setCol(col, vector); } + //Adds public void addRow(int[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of columns is valid - if((grid.length == 0) || (getNumCols() == elements.length)){ + else if((grid.length == 0) || (getNumCols() == elements.length)){ int[][] newGrid = new int[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ @@ -271,8 +298,12 @@ public class IntegerMatrix{ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(IntegerMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row - if(matrix.getNumRows() != 1){ + else if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row"); } @@ -280,9 +311,16 @@ public class IntegerMatrix{ addRow(matrix.grid[0]); } public void addCol(int[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of rows is valid if(grid.length == 0){ grid = new int[elements.length][1]; + for(int row = 0;row < grid.length;++row){ + grid[row][0] = elements[row]; + } } else if(grid.length == elements.length){ //Copy all existing data into the new grid @@ -296,17 +334,30 @@ public class IntegerMatrix{ } } public void addCol(IntegerMatrix matrix){ + //Make sure teh matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column"); } + int[] vector = new int[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.get(cnt, 0); + } + //Add the column - addCol(matrix.getCol(0)); + addCol(vector); } public IntegerMatrix appendRight(IntegerMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of rows - if(getNumRows() != rightSide.getNumRows()){ + else if(getNumRows() != rightSide.getNumRows()){ throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows()); } @@ -326,8 +377,12 @@ public class IntegerMatrix{ return new IntegerMatrix(newGrid); } public IntegerMatrix appendBottom(IntegerMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of columns - if(getNumCols() != rightSide.getNumCols()){ + else if(getNumCols() != rightSide.getNumCols()){ throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); } @@ -568,6 +623,7 @@ public class IntegerMatrix{ } private int det4(){ int det = 0; + //Find the row/column with the largest number of 0's int zerosLocation = 0; int maxNumZeros = 0; diff --git a/src/main/java/com/mattrixwv/matrix/LongMatrix.java b/src/main/java/com/mattrixwv/matrix/LongMatrix.java index 7fc8439..64652c0 100644 --- a/src/main/java/com/mattrixwv/matrix/LongMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/LongMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/matrix/LongMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 06-29-22 +//Modified: 07-01-22 package com.mattrixwv.matrix; @@ -12,6 +12,7 @@ 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 LongMatrix{ @@ -141,9 +142,15 @@ public class LongMatrix{ if(row >= grid.length){ throw new InvalidCoordinatesException("Row cannot be greater than the number of rows"); } + else if(row < 0){ + throw new InvalidCoordinatesException("Row cannot be less than 0"); + } else if(col >= grid[0].length){ throw new InvalidCoordinatesException("Column cannot be greater than the number of columns"); } + else if(col < 0){ + throw new InvalidCoordinatesException("Column cannot be less than 0"); + } //Return the location in the grid return grid[row][col]; @@ -162,12 +169,7 @@ public class LongMatrix{ return new LongMatrix(newRow); } public int getNumRows(){ - if(grid == null){ - return 0; - } - else{ - return grid.length; - } + return grid.length; } public LongMatrix getCol(int col){ //Make sure the column number is valid @@ -192,6 +194,7 @@ public class LongMatrix{ return 0; } } + //Sets public void set(int row, int col, long value){ //Make sure the row number is valid @@ -212,7 +215,10 @@ public class LongMatrix{ throw new InvalidCoordinatesException("Invalid row number " + row); } //Make sure the number of elements is valid - if(elements.length != getNumCols()){ + if(elements == null){ + throw new InvalidGeometryException("Row cannot be null"); + } + else if(elements.length != getNumCols()){ throw new InvalidGeometryException(elements.length, getNumCols()); } @@ -220,6 +226,10 @@ public class LongMatrix{ grid[row] = Arrays.copyOf(elements, elements.length); } public void setRow(int row, LongMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row"); @@ -234,7 +244,10 @@ public class LongMatrix{ throw new InvalidCoordinatesException("Invalid column number " + col); } //Make sure the number of elements is valid - if(elements.length != grid.length){ + if(elements == null){ + throw new InvalidGeometryException("Column cannot be null"); + } + else if(elements.length != grid.length){ throw new InvalidCoordinatesException(elements.length, grid.length); } @@ -244,18 +257,32 @@ public class LongMatrix{ } } public void setCol(int col, LongMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column"); } + long[] vector = new long[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.grid[cnt][0]; + } + //Set the column - setCol(col, matrix.getCol(0)); + setCol(col, vector); } + //Adds public void addRow(long[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of columns is valid - if((grid.length == 0) || (getNumCols() == elements.length)){ + else if((grid.length == 0) || (getNumCols() == elements.length)){ long[][] newGrid = new long[grid.length + 1][elements.length]; //Copy all existing data into the new grid for(int row = 0;row < grid.length;++row){ @@ -271,8 +298,12 @@ public class LongMatrix{ grid[grid.length - 1] = Arrays.copyOf(elements, elements.length); } public void addRow(LongMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single row - if(matrix.getNumRows() != 1){ + else if(matrix.getNumRows() != 1){ throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row"); } @@ -280,9 +311,16 @@ public class LongMatrix{ addRow(matrix.grid[0]); } public void addCol(long[] elements){ + //Make sure the matrix isn't null + if(elements == null){ + throw new NullMatrixException(); + } //Make sure the number of rows is valid if(grid.length == 0){ grid = new long[elements.length][1]; + for(int row = 0;row < grid.length;++row){ + grid[row][0] = elements[row]; + } } else if(grid.length == elements.length){ //Copy all existing data into the new grid @@ -296,17 +334,30 @@ public class LongMatrix{ } } public void addCol(LongMatrix matrix){ + //Make sure the matrix isn't null + if(matrix == null){ + throw new NullMatrixException(); + } //Make sure the matrix has a single column - if(matrix.getNumCols() != 1){ + else if(matrix.getNumCols() != 1){ throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column"); } + long[] vector = new long[matrix.getNumRows()]; + for(int cnt = 0;cnt < matrix.getNumRows();++cnt){ + vector[cnt] = matrix.get(cnt, 0); + } + //Add the column - addCol(matrix.getCol(0)); + addCol(vector); } public LongMatrix appendRight(LongMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of rows - if(getNumRows() != rightSide.getNumRows()){ + else if(getNumRows() != rightSide.getNumRows()){ throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows()); } @@ -326,8 +377,12 @@ public class LongMatrix{ return new LongMatrix(newGrid); } public LongMatrix appendBottom(LongMatrix rightSide){ + //Make sure the matrix isn't null + if(rightSide == null){ + throw new NullMatrixException(); + } //Make sure the matrices have the same number of columns - if(getNumCols() != rightSide.getNumCols()){ + else if(getNumCols() != rightSide.getNumCols()){ throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols()); } @@ -568,6 +623,7 @@ public class LongMatrix{ } public long det4(){ long det = 0; + //Find the row/column with the largest number of 0's int zerosLocation = 0; int maxNumZeros = 0; diff --git a/src/main/java/com/mattrixwv/matrix/ModMatrix.java b/src/main/java/com/mattrixwv/matrix/ModMatrix.java index 381fe90..9dde910 100644 --- a/src/main/java/com/mattrixwv/matrix/ModMatrix.java +++ b/src/main/java/com/mattrixwv/matrix/ModMatrix.java @@ -1,7 +1,7 @@ //Matrix/src/main/java/com/mattrixwv/matrix/ModMatrix.java //Mattrixwv // Created: 02-09-22 -//Modified: 06-29-22 +//Modified: 07-01-22 package com.mattrixwv.matrix; @@ -32,6 +32,10 @@ public class ModMatrix extends IntegerMatrix{ return newValue; } protected int[] modValues(int[] values){ + if(values == null){ + throw new InvalidGeometryException("Array cannot be null"); + } + int[] newValues = new int[values.length]; for(int cnt = 0;cnt < values.length;++cnt){ @@ -319,7 +323,7 @@ public class ModMatrix extends IntegerMatrix{ if(rightSide == null){ return false; } - if(rightSide.getClass().equals(this.getClass())){ + else if(rightSide.getClass().equals(this.getClass())){ return equals((ModMatrix)rightSide); } else if(rightSide.getClass().equals(int[][].class)){ diff --git a/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java b/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java new file mode 100644 index 0000000..b25c6b4 --- /dev/null +++ b/src/main/java/com/mattrixwv/matrix/exceptions/NullMatrixException.java @@ -0,0 +1,23 @@ +//Matrix/src/main/java/com/mattrixwv/exceptions/NullMatrixException.java +//Mattrixwv +// Created: 06-30-22 +//Modified: 06-30-22 +package com.mattrixwv.matrix.exceptions; + + +public class NullMatrixException extends RuntimeException{ + public static final long serialVersionUID = 1; + + public NullMatrixException(){ + super(); + } + public NullMatrixException(String message){ + super(message); + } + public NullMatrixException(Throwable throwable){ + super(throwable); + } + public NullMatrixException(String message, Throwable throwable){ + super(message, throwable); + } +} diff --git a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java index 2cbac62..3b8b0a7 100644 --- a/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestBigIntegerMatrix.java @@ -1,17 +1,27 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestBigBigIntegerMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 02-10-22 +//Modified: 06-30-22 package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import java.math.BigInteger; +import java.util.Arrays; import org.junit.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 TestBigIntegerMatrix{ //Grid 1x1 @@ -115,10 +125,85 @@ public class TestBigIntegerMatrix{ }; + @Test + public void testConstructor(){ + //Default constructor + BigIntegerMatrix matrix = new BigIntegerMatrix(); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + + //Filler constructor + //0 rows + assertThrows(InvalidGeometryException.class, () -> { + new BigIntegerMatrix(0, 0, BigInteger.ZERO); + }); + //0 cols + assertThrows(InvalidGeometryException.class, () -> { + new BigIntegerMatrix(1, 0, BigInteger.ZERO); + }); + //Good values + matrix = new BigIntegerMatrix(2, 2, BigInteger.ZERO); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(BigInteger.ZERO, matrix.get(0, 0)); + assertEquals(BigInteger.ZERO, matrix.get(0, 1)); + assertEquals(BigInteger.ZERO, matrix.get(1, 0)); + assertEquals(BigInteger.ZERO, matrix.get(1, 1)); + + //Matrix constructor + matrix.set(0, 0, BigInteger.ONE); + matrix.set(0, 1, BigInteger.TWO); + matrix.set(1, 0, BigInteger.ONE); + matrix.set(1, 1, BigInteger.TWO); + BigIntegerMatrix matrix2 = new BigIntegerMatrix(matrix); + assertEquals(2, matrix2.getNumRows()); + assertEquals(2, matrix2.getNumCols()); + assertEquals(BigInteger.ONE, matrix2.get(0, 0)); + assertEquals(BigInteger.TWO, matrix2.get(0, 1)); + assertEquals(BigInteger.ONE, matrix2.get(1, 0)); + assertEquals(BigInteger.TWO, matrix2.get(1, 1)); + + //Array constructor + //0 length + BigInteger[][] grid = new BigInteger[0][0]; + matrix = new BigIntegerMatrix(grid); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //0 cols + grid = new BigInteger[1][0]; + matrix = new BigIntegerMatrix(grid); + assertEquals(1, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //Uneven rows + assertThrows(InvalidRowSizeException.class, () -> { + BigInteger[][] grid1 = new BigInteger[2][]; + grid1[0] = new BigInteger[1]; + grid1[0][0] = BigInteger.ZERO; + grid1[1] = new BigInteger[2]; + grid1[1][0] = BigInteger.ONE; + grid1[1][1] = BigInteger.TWO; + new BigIntegerMatrix(grid1); + }); + + //2x2 + grid = grid2; + matrix = new BigIntegerMatrix(grid); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(BigInteger.ONE, matrix.get(0, 0)); + assertEquals(BigInteger.TWO, matrix.get(0, 1)); + assertEquals(BigInteger.ONE, matrix.get(1, 0)); + assertEquals(BigInteger.TWO, matrix.get(1, 1)); + } + @Test public void testEquals(){ - //1x1 + //Invalid equals BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + assertNotEquals(matrix, null); + assertNotEquals(matrix, new int[0]); + + //1x1 boolean gridEquals = matrix.equals(matrix); assertTrue("BigIntegerMatrix 1x1 failed equals BigIntegerMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") @@ -132,6 +217,17 @@ public class TestBigIntegerMatrix{ @SuppressWarnings("unlikely-arg-type") boolean gridEquals21 = matrix.equals(grid2); assertTrue("BigIntegerMatrix 2x2 failed equals BigInteger[][].", gridEquals21); + //false + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals22 = matrix.equals(transformGrid2_1); + assertFalse(gridEquals22); + gridEquals2 = matrix.equals(new BigIntegerMatrix(grid3)); + assertFalse(gridEquals2); + gridEquals2 = matrix.equals(new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO} + })); + assertFalse(gridEquals2); //3x3 matrix = new BigIntegerMatrix(grid3); @@ -159,24 +255,105 @@ public class TestBigIntegerMatrix{ } @Test - public void testGets(){ + public void testGet(){ //1x1 BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); assertEquals("BigIntegerMatrix 1x1 failed get.", BigInteger.ONE, matrix.get(0, 0)); - //GetRow - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals("BigIntegerMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals("BigIntegerMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(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 BigIntegerMatrix(grid2); assertEquals("BigIntegerMatrix 2x2 failed get.", BigInteger.ONE, matrix.get(0, 0)); - //GetRow + + //3x3 + matrix = new BigIntegerMatrix(grid3); + assertEquals("BigIntegerMatrix 3x3 failed get.", BigInteger.ONE, matrix.get(0, 0)); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + assertEquals("BigIntegerMatrix 4x4 failed get.", BigInteger.ONE, matrix.get(0, 0)); + + //10x10 + matrix = new BigIntegerMatrix(grid10); + assertEquals("BigIntegerMatrix 10x10 failed get.", BigInteger.ONE, matrix.get(0, 0)); + } + + @Test + public void testGetRow(){ + //1x1 + BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + assertEquals("BigIntegerMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + + //Invalid gets + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(3); + }); + + //2x2 + matrix = new BigIntegerMatrix(grid2); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO}}); assertEquals("BigIntegerMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn + + //3x3 + matrix = new BigIntegerMatrix(grid3); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}}); + assertEquals("BigIntegerMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}}); + assertEquals("BigIntegerMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); + + //10x10 + matrix = new BigIntegerMatrix(grid10); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}}); + assertEquals("BigIntegerMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + + //getNumRows + assertEquals(10, matrix.getNumRows()); + } + + @Test + public void testGetCol(){ + //1x1 + BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + assertEquals("BigIntegerMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(3); + }); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix2.getCol(3); + }); + + //2x2 + matrix = new BigIntegerMatrix(grid2); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE}, {BigInteger.ONE} @@ -185,11 +362,6 @@ public class TestBigIntegerMatrix{ //3x3 matrix = new BigIntegerMatrix(grid3); - assertEquals("BigIntegerMatrix 3x3 failed get.", BigInteger.ONE, matrix.get(0, 0)); - //GetRow - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}}); - assertEquals("BigIntegerMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE}, {BigInteger.ONE}, @@ -199,11 +371,6 @@ public class TestBigIntegerMatrix{ //4x4 matrix = new BigIntegerMatrix(grid4); - assertEquals("BigIntegerMatrix 4x4 failed get.", BigInteger.ONE, matrix.get(0, 0)); - //GetRow - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}}); - assertEquals("BigIntegerMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE}, {BigInteger.ONE}, @@ -214,11 +381,6 @@ public class TestBigIntegerMatrix{ //10x10 matrix = new BigIntegerMatrix(grid10); - assertEquals("BigIntegerMatrix 10x10 failed get.", BigInteger.ONE, matrix.get(0, 0)); - //GetRow - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}}); - assertEquals("BigIntegerMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE}, {BigInteger.ONE}, @@ -232,27 +394,36 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE} }); assertEquals("BigIntegerMatrix 10x10 failed getColumn.", correctMatrix, matrix.getCol(0)); + + //getNumCols + assertEquals(0, testMatrix.getNumCols()); + assertEquals(10, matrix.getNumCols()); } @Test - public void testSets(){ + public void testSet(){ //1x1 - //Set BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); matrix.set(0, 0, BigInteger.TWO); BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.TWO}}); assertEquals("BigIntegerMatrix 1x1 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new BigInteger[]{BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); - assertEquals("BigIntegerMatrix 1x1 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new BigInteger[]{BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); - assertEquals("BigIntegerMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid sets + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(-1, -1, null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(2, 2, null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(0, -1, null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(0, 2, null); + }); //2x2 - //Set matrix = new BigIntegerMatrix(grid2); matrix.set(0, 0, BigInteger.valueOf(3)); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -260,24 +431,9 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO} }); assertEquals("BigIntegerMatrix 2x2 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(1, new BigInteger[]{BigInteger.TWO, BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO}, - {BigInteger.TWO, BigInteger.ONE} - }); - assertEquals("BigIntegerMatrix 2x2 failed set row.", correctMatrix, matrix); - //SetColumn - matrix = new BigIntegerMatrix(grid2); - matrix.setCol(0, new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(3), BigInteger.TWO}, - {BigInteger.valueOf(3), BigInteger.TWO} - }); - assertEquals("BigIntegerMatrix 2x2 failed set row.", correctMatrix, matrix); + //3x3 - //Set matrix = new BigIntegerMatrix(grid3); matrix.set(0, 0, BigInteger.valueOf(3)); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -286,25 +442,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, }); assertEquals("BigIntegerMatrix 3x3 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals("BigIntegerMatrix 3x3 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals("BigIntegerMatrix 3x3 failed setColumn.", correctMatrix, matrix); //4x4 - //Set matrix = new BigIntegerMatrix(grid4); matrix.set(0, 0, BigInteger.valueOf(3)); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -314,27 +453,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} }); assertEquals("BigIntegerMatrix 4x4 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals("BigIntegerMatrix 4x4 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals("BigIntegerMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 - //Set matrix = new BigIntegerMatrix(grid10); matrix.set(0, 0, BigInteger.valueOf(3)); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -350,7 +470,79 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} }); assertEquals("BigIntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetRow + } + + @Test + public void testSetRow(){ + //1x1 + BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + matrix.setRow(0, new BigInteger[]{BigInteger.ZERO}); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); + assertEquals("BigIntegerMatrix 1x1 failed setRow.", correctMatrix, matrix); + + //Invalid setRows + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigInteger[] testGrid = {BigInteger.ZERO, BigInteger.ZERO}; + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + 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, (BigInteger[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setRow(0, (BigIntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setRow(0, testMatrix2); + }); + + //2x2 + matrix = new BigIntegerMatrix(grid2); + matrix.setRow(1, new BigInteger[]{BigInteger.TWO, BigInteger.ONE}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.TWO, BigInteger.ONE} + }); + assertEquals("BigIntegerMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + BigIntegerMatrix matrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + matrix.setRow(1, matrix2); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO }, + {BigInteger.ZERO, BigInteger.ZERO} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new BigIntegerMatrix(grid3); + matrix.setRow(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} + }); + assertEquals("BigIntegerMatrix 3x3 failed setRow.", correctMatrix, matrix); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + }); + assertEquals("BigIntegerMatrix 4x4 failed setRow.", correctMatrix, matrix); + + //10x10 + matrix = new BigIntegerMatrix(grid10); matrix.setRow(0, new BigInteger[]{BigInteger.valueOf(10), BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.valueOf(10), BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, @@ -365,10 +557,83 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} }); assertEquals("BigIntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetColumn + } + + @Test + public void testSetCol(){ + //1x1 + BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + matrix.setCol(0, new BigInteger[]{BigInteger.ONE}); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + assertEquals("BigIntegerMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid setCols + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(); + final BigInteger[] testGrid = {BigInteger.ZERO, BigInteger.ZERO}; + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(-1, testGrid); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(2, testGrid); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, (BigInteger[])null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(0, testGrid); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setCol(0, (BigIntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, testMatrix2); + }); + + + //2x2 + matrix = new BigIntegerMatrix(grid2); + matrix.setCol(0, new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.valueOf(3), BigInteger.TWO}, + {BigInteger.valueOf(3), BigInteger.TWO} + }); + assertEquals("BigIntegerMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + BigIntegerMatrix vector = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); + matrix.setCol(1, vector); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.valueOf(3), BigInteger.ZERO}, + {BigInteger.valueOf(3), BigInteger.ZERO} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new BigIntegerMatrix(grid3); + matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)} + }); + assertEquals("BigIntegerMatrix 3x3 failed setColumn.", correctMatrix, matrix); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + }); + assertEquals("BigIntegerMatrix 4x4 failed setCol.", correctMatrix, matrix); + + //10x10 + matrix = new BigIntegerMatrix(grid10); matrix.setCol(0, new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ZERO, BigInteger.valueOf(9), BigInteger.valueOf(8), BigInteger.valueOf(7), BigInteger.valueOf(6), BigInteger.valueOf(5), BigInteger.valueOf(4), BigInteger.valueOf(3), BigInteger.TWO, BigInteger.ONE}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, @@ -383,21 +648,36 @@ public class TestBigIntegerMatrix{ } @Test - public void testAdds(){ + public void testAddRow(){ + //0x0 + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.addRow(new BigInteger[]{BigInteger.ZERO}); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); + assertEquals(correctMatrix, matrix); + //1x1 - //AddRow - BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); - matrix.addRow(new BigInteger[]{BigInteger.ONE}); - BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}, {BigInteger.ONE}}); - assertEquals("BigIntegerMatrix 1x1 failed addRow.", correctMatrix, matrix); - //AddColumn matrix = new BigIntegerMatrix(grid1); - matrix.addCol(new BigInteger[]{BigInteger.ONE}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.ONE}}); - assertEquals("BigIntegerMatrix 1x1 failed addCol.", correctMatrix, matrix); + matrix.addRow(new BigInteger[]{BigInteger.ONE}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}, {BigInteger.ONE}}); + assertEquals("BigIntegerMatrix 1x1 failed addRow.", correctMatrix, matrix); + + //Invalid adds + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((BigInteger[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((BigIntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(testMatrix2); + }); //2x2 - //AddRow matrix = new BigIntegerMatrix(grid2); matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -406,17 +686,17 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO} }); assertEquals("BigIntegerMatrix 2x2 failed addRow.", correctMatrix, matrix); - //AddColumn + //Matrix matrix = new BigIntegerMatrix(grid2); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); + matrix.addRow(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}})); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ZERO, BigInteger.ZERO} }); - assertEquals("BigIntegerMatrix 2x2 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //3x3 - //AddRow matrix = new BigIntegerMatrix(grid3); matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -426,18 +706,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} }); assertEquals("BigIntegerMatrix 3x3 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix = new BigIntegerMatrix(grid3); - matrix.addCol(new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals("BigIntegerMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 - //AddRow matrix = new BigIntegerMatrix(grid4); matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -448,19 +718,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} }); assertEquals("BigIntegerMatrix 4x4 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix.addCol(new BigInteger[]{BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5)}); - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)} - }); - assertEquals("BigIntegerMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 - //AddRow matrix = new BigIntegerMatrix(grid10); matrix.addRow(new BigInteger[]{BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -477,7 +736,77 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} }); assertEquals("BigIntegerMatrix 10x10 failed addRow.", correctMatrix, matrix); - //AddColumn + } + + @Test + public void testAddCol(){ + //0x0 + BigIntegerMatrix matrix = new BigIntegerMatrix(); + matrix.addCol(new BigInteger[]{BigInteger.ZERO}); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}}); + assertEquals(correctMatrix, matrix); + + //1x1 + matrix = new BigIntegerMatrix(grid1); + matrix.addCol(new BigInteger[]{BigInteger.ONE}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.ONE}}); + assertEquals("BigIntegerMatrix 1x1 failed addCol.", correctMatrix, matrix); + + //Invalid adds + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(new BigInteger[]{BigInteger.ZERO, BigInteger.ZERO}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((BigInteger[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((BigIntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(testMatrix2); + }); + + //2x2 + matrix = new BigIntegerMatrix(grid2); + matrix.addCol(new BigInteger[]{BigInteger.valueOf(3), BigInteger.valueOf(3)}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} + }); + assertEquals("BigIntegerMatrix 2x2 failed addCol.", correctMatrix, matrix); + //Matrix + matrix = new BigIntegerMatrix(grid2); + matrix.addCol(new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}})); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new BigIntegerMatrix(grid3); + matrix.addCol(new BigInteger[]{BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(4)}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + }); + assertEquals("BigIntegerMatrix 3x3 failed addCol.", correctMatrix, matrix); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + matrix.addCol(new BigInteger[]{BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5), BigInteger.valueOf(5)}); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5)} + }); + assertEquals("BigIntegerMatrix 4x4 failed addCol.", correctMatrix, matrix); + + //10x10 matrix = new BigIntegerMatrix(grid10); matrix.addCol(new BigInteger[]{BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11), BigInteger.valueOf(11)}); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -496,22 +825,24 @@ public class TestBigIntegerMatrix{ } @Test - public void testAppends(){ + public void testAppendRight(){ //1x1 - //appendRight BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); BigIntegerMatrix secondMatrix = new BigIntegerMatrix(grid1); BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE, BigInteger.ONE}}); assertEquals("BigIntegerMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE}, - {BigInteger.ONE} + + //Invalid appends + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendRight(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendRight(null); }); - assertEquals("BigIntegerMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 - //appendRight matrix = new BigIntegerMatrix(grid2); secondMatrix = new BigIntegerMatrix(grid2); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -519,17 +850,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.ONE, BigInteger.TWO} }); assertEquals("BigIntegerMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO}, - {BigInteger.ONE, BigInteger.TWO} - }); - assertEquals("BigIntegerMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 - //appendRight matrix = new BigIntegerMatrix(grid3); secondMatrix = new BigIntegerMatrix(grid3); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -538,19 +860,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} }); assertEquals("BigIntegerMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} - }); - assertEquals("BigIntegerMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 - //appendRight matrix = new BigIntegerMatrix(grid4); secondMatrix = new BigIntegerMatrix(grid4); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -560,21 +871,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} }); assertEquals("BigIntegerMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, - {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} - }); - assertEquals("BigIntegerMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 - //appendRight matrix = new BigIntegerMatrix(grid10); secondMatrix = new BigIntegerMatrix(grid10); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -590,7 +888,71 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)} }); assertEquals("BigIntegerMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom + } + + @Test + public void testAppendBottom(){ + //1x1 + BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix secondMatrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE}, + {BigInteger.ONE} + }); + assertEquals("BigIntegerMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //Invalid appends + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendBottom(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendBottom(null); + }); + + //2x2 + matrix = new BigIntegerMatrix(grid2); + secondMatrix = new BigIntegerMatrix(grid2); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ONE, BigInteger.TWO}, + {BigInteger.ONE, BigInteger.TWO} + }); + assertEquals("BigIntegerMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //3x3 + matrix = new BigIntegerMatrix(grid3); + secondMatrix = new BigIntegerMatrix(grid3); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} + }); + assertEquals("BigIntegerMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //4x4 + matrix = new BigIntegerMatrix(grid4); + secondMatrix = new BigIntegerMatrix(grid4); + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + }); + assertEquals("BigIntegerMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //10x10 + matrix = new BigIntegerMatrix(grid10); + secondMatrix = new BigIntegerMatrix(grid10); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(6), BigInteger.valueOf(7), BigInteger.valueOf(8), BigInteger.valueOf(9), BigInteger.valueOf(10)}, @@ -606,6 +968,18 @@ public class TestBigIntegerMatrix{ assertEquals("BigIntegerMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } + @Test + public void testIsSquare(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + assertFalse("BigIntegerMatrix failed is square", matrix.isSquare()); + + matrix = new BigIntegerMatrix(2, 2, BigInteger.ZERO); + assertTrue(matrix.isSquare()); + + matrix = new BigIntegerMatrix(2, 3, BigInteger.ZERO); + assertFalse(matrix.isSquare()); + } + @Test public void testAddition(){ //1x1 @@ -615,6 +989,17 @@ public class TestBigIntegerMatrix{ assertEquals("BigIntegerMatrix 1x1 failed add BigIntegerMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("BigIntegerMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(BigInteger.ONE)); + //Invalid adds + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); + final BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix3); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); transformMatrix = new BigIntegerMatrix(transformGrid2_1); @@ -705,6 +1090,17 @@ public class TestBigIntegerMatrix{ assertEquals("BigIntegerMatrix 1x1 failed subtract BigIntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("BigIntegerMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(BigInteger.ONE)); + //Invalid subtracts + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); + final BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix3); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); transformMatrix = new BigIntegerMatrix(grid2); @@ -770,6 +1166,13 @@ public class TestBigIntegerMatrix{ assertEquals("BigIntegerMatrix 1x1 failed multiplication BigIntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix)); assertEquals("BigIntegerMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(BigInteger.TWO)); + //Invalid multiplication + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.multiply(testMatrix2); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); transformMatrix = new BigIntegerMatrix(transformGrid2_2); @@ -914,6 +1317,13 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix transformMatrix = new BigIntegerMatrix(transformGrid1_2); assertEquals("BigIntegerMatrix 1x1 failed dot product BigIntegerMatrix.", BigInteger.TWO, matrix.dotProduct(transformMatrix)); + //Invalid products + BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.dotProduct(testMatrix2); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); transformMatrix = new BigIntegerMatrix(transformGrid2_2); @@ -943,6 +1353,17 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.TWO}}); assertEquals("BigIntegerMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + //Invalid hadamard products + BigIntegerMatrix testMatrix = new BigIntegerMatrix(grid1); + BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); + BigIntegerMatrix testMatrix3 = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix3); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); transformMatrix = new BigIntegerMatrix(transformGrid2_2); @@ -1048,6 +1469,12 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix matrix = new BigIntegerMatrix(grid1); assertEquals("BigIntegerMatrix 1x1 failed determinant.", BigInteger.ONE, matrix.determinant()); + //Invalid determinants + BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.determinant(); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); assertEquals("BigIntegerMatrix 2x2 failed determinant1.", BigInteger.ZERO, matrix.determinant()); @@ -1056,6 +1483,8 @@ public class TestBigIntegerMatrix{ {BigInteger.valueOf(4), BigInteger.ONE} }); assertEquals("BigIntegerMatrix 2x2 failed determinant2.", BigInteger.valueOf(-15), matrix.determinant()); + //det + assertEquals(matrix.determinant(), matrix.det()); //3x3 matrix = new BigIntegerMatrix(grid3); @@ -1077,6 +1506,22 @@ public class TestBigIntegerMatrix{ {BigInteger.valueOf(4), BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)} }); assertEquals("BigIntegerMatrix 4x4 failed determinant2.", BigInteger.valueOf(160), matrix.determinant()); + //Column + matrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(3), BigInteger.valueOf(4)} + }); + assertEquals(BigInteger.ZERO, matrix.determinant()); + //Column2 + matrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, + {BigInteger.ONE, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.ZERO, BigInteger.valueOf(4)} + }); + assertEquals(BigInteger.ZERO, matrix.determinant()); //10x10 matrix = new BigIntegerMatrix(grid10); @@ -1103,6 +1548,12 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix correctMatrix = new BigIntegerMatrix(grid1); assertEquals("BigIntegerMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + //Invalid cofactor + BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.cofactor(); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -1110,6 +1561,8 @@ public class TestBigIntegerMatrix{ {BigInteger.TWO.negate(), BigInteger.ONE} }); assertEquals("BigIntegerMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + //cof + assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new BigIntegerMatrix(grid3); @@ -1161,6 +1614,16 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); assertEquals("BigIntegerMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + //Invalid powers + final BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO}, {BigInteger.ZERO}}); + final BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(grid1); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.pow(1); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.pow(-1); + }); + //2x2 matrix = new BigIntegerMatrix(grid2); correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ @@ -1236,6 +1699,8 @@ public class TestBigIntegerMatrix{ {BigInteger.ONE.negate(), BigInteger.ONE} }); assertEquals("BigIntegerMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + //adj + assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new BigIntegerMatrix(grid3); @@ -1273,6 +1738,20 @@ public class TestBigIntegerMatrix{ BigIntegerMatrix correctMatrix = new BigIntegerMatrix(grid1); assertEquals("BigIntegerMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + //Invalid inverse + BigIntegerMatrix testMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ZERO, BigInteger.ZERO}}); + BigIntegerMatrix testMatrix2 = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)}, + {BigInteger.ZERO, BigInteger.TWO, BigInteger.valueOf(3)} + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.inverse(); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.inverse(); + }); + //2x2 matrix = new BigIntegerMatrix(new BigInteger[][]{ {BigInteger.ONE, BigInteger.valueOf(4)}, @@ -1315,4 +1794,68 @@ public class TestBigIntegerMatrix{ //10x10 //?Skipped 10x10 because it would take a long time to compute } + + @Test + public void testGenerateIdentity(){ + //0x0 + assertThrows(InvalidGeometryException.class, () -> { + BigIntegerMatrix.generateIdentity(0); + }); + + //1x1 + BigIntegerMatrix correctMatrix = new BigIntegerMatrix(new BigInteger[][]{{BigInteger.ONE}}); + assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(1)); + + //2x2 + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ONE } + }); + assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(2)); + + //3x3 + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } + }); + assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(3)); + + //4x4 + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } + }); + assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(4)); + + //10x10 + correctMatrix = new BigIntegerMatrix(new BigInteger[][]{ + {BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.ZERO}, + {BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE } + }); + assertEquals(correctMatrix, BigIntegerMatrix.generateIdentity(10)); + } + + @Test + public void testHashCode(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(); + assertEquals(Arrays.hashCode(new BigInteger[0][0]), matrix.hashCode()); + } + + @Test + public void testToString(){ + BigIntegerMatrix matrix = new BigIntegerMatrix(grid3); + String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; + assertEquals(matrixString, matrix.toString()); + } } diff --git a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java index dc9fcdc..ca39b42 100644 --- a/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestDoubleMatrix.java @@ -6,10 +6,21 @@ package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import java.util.Arrays; + import org.junit.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 TestDoubleMatrix{ //Grid 1x1 @@ -113,15 +124,94 @@ public class TestDoubleMatrix{ }; + @Test + public void testConstructor(){ + //Default constructor + DoubleMatrix matrix = new DoubleMatrix(); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + + //Filler constructor + //0 rows + assertThrows(InvalidGeometryException.class, () -> { + new DoubleMatrix(0, 0, 0.0); + }); + //0 cols + assertThrows(InvalidGeometryException.class, () -> { + new DoubleMatrix(1, 0, 0.0); + }); + //Good values + matrix = new DoubleMatrix(2, 2, 0.0); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(0.0, matrix.get(0, 0), 0.0001); + assertEquals(0.0, matrix.get(0, 1), 0.0001); + assertEquals(0.0, matrix.get(1, 0), 0.0001); + assertEquals(0.0, matrix.get(1, 1), 0.0001); + + //Matrix constructor + matrix.set(0, 0, 1.0); + matrix.set(0, 1, 2.0); + matrix.set(1, 0, 1.0); + matrix.set(1, 1, 2.0); + DoubleMatrix matrix2 = new DoubleMatrix(matrix); + assertEquals(2, matrix2.getNumRows()); + assertEquals(2, matrix2.getNumCols()); + assertEquals(1.0, matrix2.get(0, 0), 0.0001); + assertEquals(2.0, matrix2.get(0, 1), 0.0001); + assertEquals(1.0, matrix2.get(1, 0), 0.0001); + assertEquals(2.0, matrix2.get(1, 1), 0.0001); + + //Array constructor + //0 length + double[][] grid = new double[0][0]; + matrix = new DoubleMatrix(grid); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //0 cols + grid = new double[1][0]; + matrix = new DoubleMatrix(grid); + assertEquals(1, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //Uneven rows + assertThrows(InvalidRowSizeException.class, () -> { + double[][] grid1 = new double[2][]; + grid1[0] = new double[1]; + grid1[0][0] = 0.0; + grid1[1] = new double[2]; + grid1[1][0] = 1.0; + grid1[1][1] = 2.0; + new DoubleMatrix(grid1); + }); + + //2x2 + grid = grid2; + matrix = new DoubleMatrix(grid); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(0.5, matrix.get(0, 0), 0.0001); + assertEquals(1.5, matrix.get(0, 1), 0.0001); + assertEquals(0.5, matrix.get(1, 0), 0.0001); + assertEquals(1.5, matrix.get(1, 1), 0.0001); + } + @Test public void testEquals(){ - //1x1 + //Invalid equals DoubleMatrix matrix = new DoubleMatrix(grid1); + assertNotEquals(matrix, null); + assertNotEquals(matrix, new int[0]); + + //1x1 + matrix = new DoubleMatrix(grid1); boolean gridEquals = matrix.equals(matrix); assertTrue("DoubleMatrix 1x1 failed equals DoubleMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") boolean gridEquals1 = matrix.equals(grid1); assertTrue("DoubleMatrix 1x1 failed equals double[][].", gridEquals1); + //With delta + boolean gridEquals12 = matrix.equals(matrix, 0.0001); + assertTrue(gridEquals12); //2x2 matrix = new DoubleMatrix(grid2); @@ -130,6 +220,17 @@ public class TestDoubleMatrix{ @SuppressWarnings("unlikely-arg-type") boolean gridEquals21 = matrix.equals(grid2); assertTrue("DoubleMatrix 2x2 failed equals double[][].", gridEquals21); + //false + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals22 = matrix.equals(transformGrid2_1); + assertFalse(gridEquals22); + gridEquals2 = matrix.equals(new DoubleMatrix(grid3)); + assertFalse(gridEquals2); + gridEquals2 = matrix.equals(new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5} + })); + assertFalse(gridEquals2); //3x3 matrix = new DoubleMatrix(grid3); @@ -157,24 +258,102 @@ public class TestDoubleMatrix{ } @Test - public void testGets(){ + public void testGet(){ //1x1 DoubleMatrix matrix = new DoubleMatrix(grid1); assertEquals("DoubleMatrix 1x1 failed get.", 0.5, matrix.get(0, 0), 0.0000001); - //GetRow - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals("DoubleMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn - correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals("DoubleMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final DoubleMatrix testMatrix = new DoubleMatrix(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 DoubleMatrix(grid2); assertEquals("DoubleMatrix 2x2 failed get.", 0.5, matrix.get(0, 0), 0.0000001); - //GetRow + + //3x3 + matrix = new DoubleMatrix(grid3); + assertEquals("DoubleMatrix 3x3 failed get.", 0.5, matrix.get(0, 0), 0.0000001); + + //4x4 + matrix = new DoubleMatrix(grid4); + assertEquals("DoubleMatrix 4x4 failed get.", 0.5, matrix.get(0, 0), 0.0000001); + + //10x10 + matrix = new DoubleMatrix(grid10); + assertEquals("DoubleMatrix 10x10 failed get.", 0.5, matrix.get(0, 0), 0.0000001); + } + + @Test + public void testGetRow(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); + assertEquals("DoubleMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + + //Invalid gets + final DoubleMatrix testMatrix = new DoubleMatrix(matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(3); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5}}); assertEquals("DoubleMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn + + //3x3 + matrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); + assertEquals("DoubleMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + + //4x4 + matrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); + assertEquals("DoubleMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); + + //10x10 + matrix = new DoubleMatrix(grid10); + correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}}); + assertEquals("DoubleMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + } + + @Test + public void testGetCol(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); + assertEquals("DoubleMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final DoubleMatrix testMatrix = new DoubleMatrix(); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(3); + }); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix2.getCol(3); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5} @@ -183,11 +362,6 @@ public class TestDoubleMatrix{ //3x3 matrix = new DoubleMatrix(grid3); - assertEquals("DoubleMatrix 3x3 failed get.", 0.5, matrix.get(0, 0), 0.0000001); - //GetRow - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5}}); - assertEquals("DoubleMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, @@ -197,11 +371,6 @@ public class TestDoubleMatrix{ //4x4 matrix = new DoubleMatrix(grid4); - assertEquals("DoubleMatrix 4x4 failed get.", 0.5, matrix.get(0, 0), 0.0000001); - //GetRow - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5}}); - assertEquals("DoubleMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, @@ -212,11 +381,6 @@ public class TestDoubleMatrix{ //10x10 matrix = new DoubleMatrix(grid10); - assertEquals("DoubleMatrix 10x10 failed get.", 0.5, matrix.get(0, 0), 0.0000001); - //GetRow - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}}); - assertEquals("DoubleMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetCol correctMatrix = new DoubleMatrix(new double[][]{ {0.5}, {0.5}, @@ -233,49 +397,38 @@ public class TestDoubleMatrix{ } @Test - public void testSets(){ + public void testSet(){ //1x1 - //Set DoubleMatrix matrix = new DoubleMatrix(grid1); matrix.set(0, 0, 1.5); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.5}}); - assertEquals("DoubleMatrix 1x1 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new double[]{0.0}); - correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); - assertEquals("DoubleMatrix 1x1 failed setRow.", correctMatrix, matrix); - //SetCol - matrix.setCol(0, new double[]{0.5}); - correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); - assertEquals("DoubleMatrix 1x1 failed setCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); + + //Invalid sets + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(-1, -1, 0.0); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(2, 2, 0.0); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(0, -1, 0.0); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.set(0, 2, 0.0); + }); //2x2 - //Set matrix = new DoubleMatrix(grid2); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ {2.5, 1.5}, {0.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(1, new double[]{1.5, 0.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5}, - {1.5, 0.5} - }); - assertEquals("DoubleMatrix 2x2 failed setRow.", correctMatrix, matrix); - //SetCol - matrix = new DoubleMatrix(grid2); - matrix.setCol(0, new double[]{2.5, 2.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {2.5, 1.5}, - {2.5, 1.5} - }); - assertEquals("DoubleMatrix 2x2 failed setCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //3x3 - //Set matrix = new DoubleMatrix(grid3); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ @@ -283,26 +436,9 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new double[]{0, 0.5, 1.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0, 0.5, 1.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals("DoubleMatrix 3x3 failed setRow.", correctMatrix, matrix); - //SetCol - matrix.setCol(0, new double[]{0, 0, 0}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 0.5, 1.5}, - {0.0, 1.5, 2.5}, - {0.0, 1.5, 2.5} - }); - assertEquals("DoubleMatrix 3x3 failed setCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //4x4 - //Set matrix = new DoubleMatrix(grid4); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ @@ -311,28 +447,9 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {3.5, 2.5, 1.5, 0.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals("DoubleMatrix 4x4 failed setRow.", correctMatrix, matrix); - //SetCol - matrix.setCol(0, new double[]{0, 0, 0, 0}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 2.5, 1.5, 0.5}, - {0.0, 1.5, 2.5, 3.5}, - {0.0, 1.5, 2.5, 3.5}, - {0.0, 1.5, 2.5, 3.5} - }); - assertEquals("DoubleMatrix 4x4 failed setCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //10x10 - //Set matrix = new DoubleMatrix(grid10); matrix.set(0, 0, 2.5); correctMatrix = new DoubleMatrix(new double[][]{ @@ -347,8 +464,79 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); - assertEquals("DoubleMatrix 10x10 failed set.", correctMatrix, matrix); - //SetRow + assertEquals(correctMatrix, matrix); + } + + @Test + public void testSetRow(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + matrix.setRow(0, new double[]{0.0}); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); + assertEquals(correctMatrix, matrix); + + //Invalid setRows + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final double[] testGrid = {0.0, 0.0}; + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + 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, (double[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setRow(0, (DoubleMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setRow(0, testMatrix2); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); + matrix.setRow(1, new double[]{1.5, 0.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5}, + {1.5, 0.5} + }); + assertEquals(correctMatrix, matrix); + DoubleMatrix matrix2 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + matrix.setRow(1, matrix2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5}, + {0.0, 0.0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new DoubleMatrix(grid3); + matrix.setRow(0, new double[]{0, 0.5, 1.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 0.5, 1.5}, + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5} + }); + assertEquals(correctMatrix, matrix); + + //4x4 + matrix = new DoubleMatrix(grid4); + matrix.setRow(0, new double[]{3.5, 2.5, 1.5, 0.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {3.5, 2.5, 1.5, 0.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5} + }); + assertEquals(correctMatrix, matrix); + + //10x10 + matrix = new DoubleMatrix(grid10); matrix.setRow(0, new double[]{9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}); correctMatrix = new DoubleMatrix(new double[][]{ {9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, @@ -362,11 +550,83 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); - assertEquals("DoubleMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetCol + assertEquals(correctMatrix, matrix); + } + + @Test + public void testSetCol(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + matrix.setCol(0, new double[]{0.5}); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}}); + assertEquals(correctMatrix, matrix); + + //Invalid setCols + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(); + final double[] testGrid = {0.0, 0.0}; + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(-1, testGrid); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(2, testGrid); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, (double[])null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(0, testGrid); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setCol(0, (DoubleMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, testMatrix2); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); + matrix.setCol(0, new double[]{2.5, 2.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {2.5, 1.5}, + {2.5, 1.5} + }); + assertEquals(correctMatrix, matrix); + //Matrix + DoubleMatrix vector = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); + matrix.setCol(1, vector); + correctMatrix = new DoubleMatrix(new double[][]{ + {2.5, 0.0}, + {2.5, 0.0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new DoubleMatrix(grid3); + matrix.setCol(0, new double[]{0.0, 0.0, 0.0}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.5, 2.5}, + {0.0, 1.5, 2.5}, + {0.0, 1.5, 2.5} + }); + assertEquals(correctMatrix, matrix); + + //4x4 + matrix = new DoubleMatrix(grid4); + matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.0, 1.5, 2.5, 3.5}, + {0.0, 1.5, 2.5, 3.5}, + {0.0, 1.5, 2.5, 3.5}, + {0.0, 1.5, 2.5, 3.5} + }); + assertEquals(correctMatrix, matrix); + + //10x10 + matrix = new DoubleMatrix(grid10); matrix.setCol(0, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}); correctMatrix = new DoubleMatrix(new double[][]{ - {0.0, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 0.5}, + {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, @@ -377,25 +637,40 @@ public class TestDoubleMatrix{ {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.0, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); - assertEquals("DoubleMatrix 10x10 failed setCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); } @Test - public void testAdds(){ + public void testAddRow(){ + //0x0 + DoubleMatrix matrix = new DoubleMatrix(); + matrix.addRow(new double[]{0.0}); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); + assertEquals(correctMatrix, matrix); + //1x1 - //AddRow - DoubleMatrix matrix = new DoubleMatrix(grid1); - matrix.addRow(new double[]{0.5}); - DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); - assertEquals("DoubleMatrix 1x1 failed addRow.", correctMatrix, matrix); - //AddColumn matrix = new DoubleMatrix(grid1); - matrix.addCol(new double[]{0.5}); - correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); - assertEquals("DoubleMatrix 1x1 failed addCol.", correctMatrix, matrix); + matrix.addRow(new double[]{0.5}); + correctMatrix = new DoubleMatrix(new double[][]{{0.5}, {0.5}}); + assertEquals(correctMatrix, matrix); + + //Invalid adds + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(new double[]{0.0, 0.0}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((double[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((DoubleMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(testMatrix2); + }); //2x2 - //AddRow matrix = new DoubleMatrix(grid2); matrix.addRow(new double[]{0.5, 1.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -403,18 +678,18 @@ public class TestDoubleMatrix{ {0.5, 1.5}, {0.5, 1.5} }); - assertEquals("DoubleMatrix 2x2 failed addRow.", correctMatrix, matrix); - //AddColumn + assertEquals(correctMatrix, matrix); + //Matrix matrix = new DoubleMatrix(grid2); - matrix.addCol(new double[]{2.5, 2.5}); + matrix.addRow(new DoubleMatrix(new double[][]{{1.0, 2.0}})); correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} + {0.5, 1.5}, + {0.5, 1.5}, + {1.0, 2.0} }); - assertEquals("DoubleMatrix 2x2 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //3x3 - //AddRow matrix = new DoubleMatrix(grid3); matrix.addRow(new double[]{0.5, 1.5, 2.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -423,19 +698,9 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5}, {0.5, 1.5, 2.5} }); - assertEquals("DoubleMatrix 3x3 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix = new DoubleMatrix(grid3); - matrix.addCol(new double[]{3.5, 3.5, 3.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals("DoubleMatrix 3x3 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //4x4 - //AddRow matrix = new DoubleMatrix(grid4); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -445,20 +710,9 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5}, {0.5, 1.5, 2.5, 3.5} }); - assertEquals("DoubleMatrix 4x4 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix = new DoubleMatrix(grid4); - matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5}, - {0.5, 1.5, 2.5, 3.5, 4.5} - }); - assertEquals("DoubleMatrix 4x4 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //10x10 - //AddRow matrix = new DoubleMatrix(grid10); matrix.addRow(new double[]{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -474,8 +728,78 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); - assertEquals("DoubleMatrix 10x10 failed addRow.", correctMatrix, matrix); - //AddColumn + assertEquals(correctMatrix, matrix); + } + + @Test + public void testAddCol(){ + //0x0 + DoubleMatrix matrix = new DoubleMatrix(); + matrix.addCol(new double[]{0.0}); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.0}}); + assertEquals(correctMatrix, matrix); + + //1x1 + matrix = new DoubleMatrix(grid1); + matrix.addCol(new double[]{0.5}); + correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); + assertEquals(correctMatrix, matrix); + + //Invalid adds + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(new double[]{0.0, 0.}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((double[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((DoubleMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(testMatrix2); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); + matrix.addCol(new double[]{2.5, 2.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5} + }); + assertEquals(correctMatrix, matrix); + //Matrix + matrix = new DoubleMatrix(grid2); + matrix.addCol(new DoubleMatrix(new double[][]{{0.0}, {0.0}})); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 0.0}, + {0.5, 1.5, 0.0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new DoubleMatrix(grid3); + matrix.addCol(new double[]{3.5, 3.5, 3.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5} + }); + assertEquals(correctMatrix, matrix); + + //4x4 + matrix = new DoubleMatrix(grid4); + matrix.addCol(new double[]{4.5, 4.5, 4.5, 4.5}); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5, 4.5}, + {0.5, 1.5, 2.5, 3.5, 4.5}, + {0.5, 1.5, 2.5, 3.5, 4.5}, + {0.5, 1.5, 2.5, 3.5, 4.5} + }); + assertEquals(correctMatrix, matrix); + + //10x10 matrix = new DoubleMatrix(grid10); matrix.addCol(new double[]{10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5, 10.5}); correctMatrix = new DoubleMatrix(new double[][]{ @@ -490,26 +814,28 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5} }); - assertEquals("DoubleMatrix 10x10 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); } @Test - public void testAppends(){ + public void testAppendRight(){ //1x1 - //appendRight DoubleMatrix matrix = new DoubleMatrix(grid1); DoubleMatrix secondMatrix = new DoubleMatrix(grid1); DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.5, 0.5}}); assertEquals("DoubleMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5}, - {0.5} + + //Invalid appends + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendRight(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendRight(null); }); - assertEquals("DoubleMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 - //appendRight matrix = new DoubleMatrix(grid2); secondMatrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ @@ -517,17 +843,8 @@ public class TestDoubleMatrix{ {0.5, 1.5, 0.5, 1.5} }); assertEquals("DoubleMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5}, - {0.5, 1.5}, - {0.5, 1.5}, - {0.5, 1.5} - }); - assertEquals("DoubleMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 - //appendRight matrix = new DoubleMatrix(grid3); secondMatrix = new DoubleMatrix(grid3); correctMatrix = new DoubleMatrix(new double[][]{ @@ -536,19 +853,8 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5}, - {0.5, 1.5, 2.5} - }); - assertEquals("DoubleMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 - //appendRight matrix = new DoubleMatrix(grid4); secondMatrix = new DoubleMatrix(grid4); correctMatrix = new DoubleMatrix(new double[][]{ @@ -558,21 +864,8 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 0.5, 1.5, 2.5, 3.5} }); assertEquals("DoubleMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new DoubleMatrix(new double[][]{ - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5}, - {0.5, 1.5, 2.5, 3.5} - }); - assertEquals("DoubleMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 - //appendRight matrix = new DoubleMatrix(grid10); secondMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ @@ -588,7 +881,71 @@ public class TestDoubleMatrix{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5} }); assertEquals("DoubleMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom + } + + @Test + public void testAppendBottom(){ + //1x1 + DoubleMatrix matrix = new DoubleMatrix(grid1); + DoubleMatrix secondMatrix = new DoubleMatrix(grid1); + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{ + {0.5}, + {0.5} + }); + assertEquals("DoubleMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //Invalid appends + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendBottom(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendBottom(null); + }); + + //2x2 + matrix = new DoubleMatrix(grid2); + secondMatrix = new DoubleMatrix(grid2); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5}, + {0.5, 1.5}, + {0.5, 1.5}, + {0.5, 1.5} + }); + assertEquals("DoubleMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //3x3 + matrix = new DoubleMatrix(grid3); + secondMatrix = new DoubleMatrix(grid3); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5}, + {0.5, 1.5, 2.5} + }); + assertEquals("DoubleMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //4x4 + matrix = new DoubleMatrix(grid4); + secondMatrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5} + }); + assertEquals("DoubleMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //10x10 + matrix = new DoubleMatrix(grid10); + secondMatrix = new DoubleMatrix(grid10); correctMatrix = new DoubleMatrix(new double[][]{ {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5}, @@ -604,6 +961,18 @@ public class TestDoubleMatrix{ assertEquals("DoubleMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } + @Test + public void testIsSquare(){ + DoubleMatrix matrix = new DoubleMatrix(); + assertFalse(matrix.isSquare()); + + matrix = new DoubleMatrix(2, 2, 0.0); + assertTrue(matrix.isSquare()); + + matrix = new DoubleMatrix(2, 3, 0.0); + assertFalse(matrix.isSquare()); + } + @Test public void testAddition(){ //1x1 @@ -613,6 +982,17 @@ public class TestDoubleMatrix{ assertEquals("DoubleMatrix 1x1 failed add DoubleMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("DoubleMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(0.5)); + //Invalid adds + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); + final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix3); + }); + //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_1); @@ -701,6 +1081,17 @@ public class TestDoubleMatrix{ assertEquals("DoubleMatrix 1x1 failed subtract DoubleMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("DoubleMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(0.5)); + //Invalid subtracts + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); + final DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix3); + }); + //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(grid2); @@ -767,6 +1158,13 @@ public class TestDoubleMatrix{ correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); assertEquals("DoubleMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2.0)); + //Invalid multiplication + final DoubleMatrix testMatrix = new DoubleMatrix(grid1); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.multiply(testMatrix2); + }); + //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); @@ -911,6 +1309,13 @@ public class TestDoubleMatrix{ DoubleMatrix transformMatrix = new DoubleMatrix(transformGrid1_2); assertEquals("DoubleMatrix 1x1 failed dot product DoubleMatrix.", 0.75, matrix.dotProduct(transformMatrix), 0.0000001); + //Invalid products + DoubleMatrix testMatrix = new DoubleMatrix(grid1); + DoubleMatrix testMatrix2 = new DoubleMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.dotProduct(testMatrix2); + }); + //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); @@ -940,6 +1345,17 @@ public class TestDoubleMatrix{ DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.75}}); assertEquals("DoubleMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + //Invalid hadamard products + DoubleMatrix testMatrix = new DoubleMatrix(grid1); + DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); + DoubleMatrix testMatrix3 = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix3); + }); + //2x2 matrix = new DoubleMatrix(grid2); transformMatrix = new DoubleMatrix(transformGrid2_2); @@ -1045,6 +1461,12 @@ public class TestDoubleMatrix{ DoubleMatrix matrix = new DoubleMatrix(grid1); assertEquals("DoubleMatrix 1x1 failed determinant.", 0.5, matrix.determinant(), 0.0000001); + //Invalid determinants + DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.determinant(); + }); + //2x2 matrix = new DoubleMatrix(grid2); assertEquals("IntegerMatrix 2x2 failed determinant1.", 0, matrix.determinant(), 0.0000001); @@ -1053,6 +1475,8 @@ public class TestDoubleMatrix{ {3.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed determinant2.", -12.0, matrix.determinant(), 0.0000001); + //det + assertEquals(matrix.determinant(), matrix.det(), 0.0000001); //3x3 matrix = new DoubleMatrix(grid3); @@ -1074,6 +1498,22 @@ public class TestDoubleMatrix{ {3.5, 0.5, 1.5, 2.5} }); assertEquals("DoubleMatrix 4x4 failed determinant2.", 128.0, matrix.determinant(), 0.0000001); + //Column + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {0.5, 0.0, 2.5, 3.5}, + {0.5, 0.0, 2.5, 3.5}, + {0.0, 0.0, 2.5, 3.5} + }); + assertEquals(0.0, matrix.determinant(), 0.0000001); + //Column2 + matrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 0.0, 3.5}, + {0.5, 1.5, 0.0, 3.5}, + {0.5, 1.5, 0.0, 3.5}, + {0.0, 1.5, 0.0, 3.5} + }); + assertEquals(0.0, matrix.determinant(), 0.0000001); //10x10 matrix = new DoubleMatrix(grid10); @@ -1088,9 +1528,9 @@ public class TestDoubleMatrix{ {6.5, 7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5}, {7.5, 8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5}, {8.5, 9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5}, - {9.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5}, + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0}, }); - assertEquals("DoubleMatrix 10x10 failed determinatn2.", -5000000000.0, matrix.determinant(), 0.0000001); + assertEquals("DoubleMatrix 10x10 failed determinatn2.", -10000000.0, matrix.determinant(), 0.0000001); } @Test @@ -1100,6 +1540,12 @@ public class TestDoubleMatrix{ DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1}}); assertEquals("DoubleMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + //Invalid cofactor + DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.cofactor(); + }); + //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ @@ -1107,6 +1553,8 @@ public class TestDoubleMatrix{ {-1.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + //cof + assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new DoubleMatrix(grid3); @@ -1158,6 +1606,16 @@ public class TestDoubleMatrix{ DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{0.125}}); assertEquals("DoubleMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + //Invalid powers + final DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0}, {0.0}}); + final DoubleMatrix testMatrix2 = new DoubleMatrix(grid1); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.pow(1); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.pow(-1); + }); + //2x2 matrix = new DoubleMatrix(grid2); correctMatrix = new DoubleMatrix(new double[][]{ @@ -1176,7 +1634,24 @@ public class TestDoubleMatrix{ assertEquals("DoubleMatrix 3x3 failed power.", correctMatrix, matrix.pow(3)); //4x4 + //0 matrix = new DoubleMatrix(grid4); + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 1.0, 1.0, 1.0}, + {1.0, 1.0, 1.0, 1.0}, + {1.0, 1.0, 1.0, 1.0}, + {1.0, 1.0, 1.0, 1.0} + }); + assertEquals(correctMatrix, matrix.pow(0)); + //1 + correctMatrix = new DoubleMatrix(new double[][]{ + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5}, + {0.5, 1.5, 2.5, 3.5} + }); + assertEquals(correctMatrix, matrix.pow(1)); + //3 correctMatrix = new DoubleMatrix(new double[][]{ {32.0, 96.0, 160.0, 224.0}, {32.0, 96.0, 160.0, 224.0}, @@ -1216,6 +1691,8 @@ public class TestDoubleMatrix{ {-0.5, 0.5} }); assertEquals("DoubleMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + //adj + assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new DoubleMatrix(grid3); @@ -1253,6 +1730,20 @@ public class TestDoubleMatrix{ DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{2.0}}); assertEquals("DoubleMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + //Invalid inverse + DoubleMatrix testMatrix = new DoubleMatrix(new double[][]{{0.0, 0.0}}); + DoubleMatrix testMatrix2 = new DoubleMatrix(new double[][]{ + {1.0, 2.0, 3.0}, + {1.0, 2.0, 3.0}, + {1.0, 2.0, 3.0} + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.inverse(); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.inverse(); + }); + //2x2 matrix = new DoubleMatrix(new double[][]{ {0.5, 1.5}, @@ -1295,4 +1786,68 @@ public class TestDoubleMatrix{ //10x10 //?Skipped 10x10 because it would take a long time to compute } + + @Test + public void testGenerateIdentity(){ + //0x0 + assertThrows(InvalidGeometryException.class, () -> { + DoubleMatrix.generateIdentity(0); + }); + + //1x1 + DoubleMatrix correctMatrix = new DoubleMatrix(new double[][]{{1.0}}); + assertEquals(correctMatrix, DoubleMatrix.generateIdentity(1)); + + //2x2 + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 0.0}, + {0.0, 1.0} + }); + assertEquals(correctMatrix, DoubleMatrix.generateIdentity(2)); + + //3x3 + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 0.0, 0.0}, + {0.0, 1.0, 0.0}, + {0.0, 0.0, 1.0} + }); + assertEquals(correctMatrix, DoubleMatrix.generateIdentity(3)); + + //4x4 + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 0.0, 0.0, 0.0}, + {0.0, 1.0, 0.0, 0.0}, + {0.0, 0.0, 1.0, 0.0}, + {0.0, 0.0, 0.0, 1.0} + }); + assertEquals(correctMatrix, DoubleMatrix.generateIdentity(4)); + + //10x10 + correctMatrix = new DoubleMatrix(new double[][]{ + {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0}, + {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0} + }); + assertEquals(correctMatrix, DoubleMatrix.generateIdentity(10)); + } + + @Test + public void testHashCode(){ + DoubleMatrix matrix = new DoubleMatrix(); + assertEquals(Arrays.hashCode(new double[0][0]), matrix.hashCode()); + } + + @Test + public void testToString(){ + DoubleMatrix matrix = new DoubleMatrix(grid3); + String matrixString = "[0.5,1.5,2.5]\n[0.5,1.5,2.5]\n[0.5,1.5,2.5]"; + assertEquals(matrixString, matrix.toString()); + } } diff --git a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java index 6ad0c0a..a11b84c 100644 --- a/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java @@ -1,15 +1,26 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java //Mattrixwv // Created: 02-01-22 -//Modified: 02-09-22 +//Modified: 07-01-22 package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import java.util.Arrays; + import org.junit.Test; +import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException; +import com.mattrixwv.matrix.exceptions.InvalidGeometryException; +import com.mattrixwv.matrix.exceptions.InvalidRowSizeException; +import com.mattrixwv.matrix.exceptions.InvalidScalarException; +import com.mattrixwv.matrix.exceptions.NullMatrixException; + public class TestIntegerMatrix{ //Grid 1x1 @@ -113,10 +124,86 @@ public class TestIntegerMatrix{ }; + @Test + public void testConstructor(){ + //Default constructor + IntegerMatrix matrix = new IntegerMatrix(); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + + //Filler constructor + //0 rows + assertThrows(InvalidGeometryException.class, () -> { + new IntegerMatrix(0, 0, 0); + }); + //0 cols + assertThrows(InvalidGeometryException.class, () -> { + new IntegerMatrix(1, 0, 0); + }); + //Good values + matrix = new IntegerMatrix(2, 2, 0); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(0, matrix.get(0, 0)); + assertEquals(0, matrix.get(0, 1)); + assertEquals(0, matrix.get(1, 0)); + assertEquals(0, matrix.get(1, 1)); + + //Matrix constructor + matrix.set(0, 0, 1); + matrix.set(0, 1, 2); + matrix.set(1, 0, 1); + matrix.set(1, 1, 2); + IntegerMatrix matrix2 = new IntegerMatrix(matrix); + assertEquals(2, matrix2.getNumRows()); + assertEquals(2, matrix2.getNumCols()); + assertEquals(1, matrix2.get(0, 0)); + assertEquals(2, matrix2.get(0, 1)); + assertEquals(1, matrix2.get(1, 0)); + assertEquals(2, matrix2.get(1, 1)); + + //Array constructor + //0 length + int[][] grid = new int[0][0]; + matrix = new IntegerMatrix(grid); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //0 cols + grid = new int[1][0]; + matrix = new IntegerMatrix(grid); + assertEquals(1, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //Uneven rows + assertThrows(InvalidRowSizeException.class, () -> { + int[][] grid1 = new int[2][]; + grid1[0] = new int[1]; + grid1[0][0] = 0; + grid1[1] = new int[2]; + grid1[1][0] = 1; + grid1[1][1] = 2; + new IntegerMatrix(grid1); + }); + + //2x2 + grid = grid2; + matrix = new IntegerMatrix(grid); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(1, matrix.get(0, 0)); + assertEquals(2, matrix.get(0, 1)); + assertEquals(1, matrix.get(1, 0)); + assertEquals(2, matrix.get(1, 1)); + } + @Test public void testEquals(){ - //1x1 + //Invalid equals IntegerMatrix matrix = new IntegerMatrix(grid1); + assertNotEquals(matrix, null); + assertNotEquals(matrix, new double[0]); + + //1x1 + matrix = new IntegerMatrix(grid1); boolean gridEquals = matrix.equals(matrix); assertTrue("IntegerMatrix 1x1 failed equals IntegerMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") @@ -130,6 +217,17 @@ public class TestIntegerMatrix{ @SuppressWarnings("unlikely-arg-type") boolean gridEquals21 = matrix.equals(grid2); assertTrue("IntegerMatrix 2x2 failed equals int[][].", gridEquals21); + //false + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals22 = matrix.equals(transformGrid2_1); + assertFalse(gridEquals22); + gridEquals2 = matrix.equals(new IntegerMatrix(grid3)); + assertFalse(gridEquals2); + gridEquals = matrix.equals(new IntegerMatrix(new int[][]{ + {0, 1, 2}, + {0, 1, 2} + })); + assertFalse(gridEquals2); //3x3 matrix = new IntegerMatrix(grid3); @@ -157,24 +255,102 @@ public class TestIntegerMatrix{ } @Test - public void testGets(){ + public void testGet(){ //1x1 IntegerMatrix matrix = new IntegerMatrix(grid1); assertEquals("IntegerMatrix 1x1 failed get.", 1, matrix.get(0, 0)); - //GetRow - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn - correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final IntegerMatrix testMatrix = new IntegerMatrix(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 IntegerMatrix(grid2); assertEquals("IntegerMatrix 2x2 failed get.", 1, matrix.get(0, 0)); - //GetRow + + //3x3 + matrix = new IntegerMatrix(grid3); + assertEquals("IntegerMatrix 3x3 failed get.", 1, matrix.get(0, 0)); + + //4x4 + matrix = new IntegerMatrix(grid4); + assertEquals("IntegerMatrix 4x4 failed get.", 1, matrix.get(0, 0)); + + //10x10 + matrix = new IntegerMatrix(grid10); + assertEquals("IntegerMatrix 10x10 failed get.", 1, matrix.get(0, 0)); + } + + @Test + public void testGetRow(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals("IntegerMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + + //Invalid gets + final IntegerMatrix testMatrix = new IntegerMatrix(matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(3); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); correctMatrix = new IntegerMatrix(new int[][]{{1, 2}}); assertEquals("IntegerMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn + + //3x3 + matrix = new IntegerMatrix(grid3); + correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}}); + assertEquals("IntegerMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + + //4x4 + matrix = new IntegerMatrix(grid4); + correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4}}); + assertEquals("IntegerMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); + + //10x10 + matrix = new IntegerMatrix(grid10); + correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); + assertEquals("IntegerMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + } + + @Test + public void testGetCol(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals("IntegerMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final IntegerMatrix testMatrix = new IntegerMatrix(); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(3); + }); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix2.getCol(3); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1} @@ -183,11 +359,6 @@ public class TestIntegerMatrix{ //3x3 matrix = new IntegerMatrix(grid3); - assertEquals("IntegerMatrix 3x3 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3}}); - assertEquals("IntegerMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1}, @@ -197,11 +368,6 @@ public class TestIntegerMatrix{ //4x4 matrix = new IntegerMatrix(grid4); - assertEquals("IntegerMatrix 4x4 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4}}); - assertEquals("IntegerMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1}, @@ -212,11 +378,6 @@ public class TestIntegerMatrix{ //10x10 matrix = new IntegerMatrix(grid10); - assertEquals("IntegerMatrix 10x10 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new IntegerMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); - assertEquals("IntegerMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new IntegerMatrix(new int[][]{ {1}, {1}, @@ -233,24 +394,29 @@ public class TestIntegerMatrix{ } @Test - public void testSets(){ + public void testSet(){ //1x1 - //Set IntegerMatrix matrix = new IntegerMatrix(grid1); matrix.set(0, 0, 2); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); assertEquals("IntegerMatrix 1x1 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new int[]{0}); - correctMatrix = new IntegerMatrix(new int[][]{{0}}); - assertEquals("IntegerMatrix 1x1 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{1}); - correctMatrix = new IntegerMatrix(new int[][]{{1}}); - assertEquals("IntegerMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid sets + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + 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 - //Set matrix = new IntegerMatrix(grid2); matrix.set(0, 0, 3); correctMatrix = new IntegerMatrix(new int[][]{ @@ -258,24 +424,8 @@ public class TestIntegerMatrix{ {1, 2} }); assertEquals("IntegerMatrix 2x2 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(1, new int[]{2, 1}); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2}, - {2, 1} - }); - assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); - //SetColumn - matrix = new IntegerMatrix(grid2); - matrix.setCol(0, new int[]{3, 3}); - correctMatrix = new IntegerMatrix(new int[][]{ - {3, 2}, - {3, 2} - }); - assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); //3x3 - //Set matrix = new IntegerMatrix(grid3); matrix.set(0, 0, 3); correctMatrix = new IntegerMatrix(new int[][]{ @@ -284,25 +434,8 @@ public class TestIntegerMatrix{ {1, 2, 3}, }); assertEquals("IntegerMatrix 3x3 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new int[]{0, 1, 2}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals("IntegerMatrix 3x3 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{0, 0, 0}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 1, 2}, - {0, 2, 3}, - {0, 2, 3} - }); - assertEquals("IntegerMatrix 3x3 failed setColumn.", correctMatrix, matrix); //4x4 - //Set matrix = new IntegerMatrix(grid4); matrix.set(0, 0, 3); correctMatrix = new IntegerMatrix(new int[][]{ @@ -312,27 +445,8 @@ public class TestIntegerMatrix{ {1, 2, 3, 4} }); assertEquals("IntegerMatrix 4x4 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new int[]{4, 3, 2, 1}); - correctMatrix = new IntegerMatrix(new int[][]{ - {4, 3, 2, 1}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals("IntegerMatrix 4x4 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{0, 0, 0, 0}); - correctMatrix = new IntegerMatrix(new int[][]{ - {0, 3, 2, 1}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }); - assertEquals("IntegerMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 - //Set matrix = new IntegerMatrix(grid10); matrix.set(0, 0, 3); correctMatrix = new IntegerMatrix(new int[][]{ @@ -348,7 +462,79 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetRow + } + + @Test + public void testSetRow(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + matrix.setRow(0, new int[]{0}); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); + assertEquals("IntegerMatrix 1x1 failed setRow.", correctMatrix, matrix); + + //Invalid setRows + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final int[] testGrid = {0, 0}; + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + 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, (IntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setRow(0, testMatrix2); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); + matrix.setRow(1, new int[]{2, 1}); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2}, + {2, 1} + }); + assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + IntegerMatrix matrix2 = new IntegerMatrix(new int[][]{{0, 0}}); + matrix.setRow(1, matrix2); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2}, + {0, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new IntegerMatrix(grid3); + matrix.setRow(0, new int[]{0, 1, 2}); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 1, 2}, + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("IntegerMatrix 3x3 failed setRow.", correctMatrix, matrix); + + //4x4 + matrix = new IntegerMatrix(grid4); + matrix.setRow(0, new int[]{4, 3, 2, 1}); + correctMatrix = new IntegerMatrix(new int[][]{ + {4, 3, 2, 1}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }); + assertEquals("IntegerMatrix 4x4 failed setRow.", correctMatrix, matrix); + + //10x10 + matrix = new IntegerMatrix(grid10); matrix.setRow(0, new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); correctMatrix = new IntegerMatrix(new int[][]{ {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, @@ -363,10 +549,82 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetColumn + } + + @Test + public void testSetCol(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + matrix.setCol(0, new int[]{1}); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals("IntegerMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid setCols + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(); + 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, (IntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, testMatrix2); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); + matrix.setCol(0, new int[]{3, 3}); + correctMatrix = new IntegerMatrix(new int[][]{ + {3, 2}, + {3, 2} + }); + assertEquals("IntegerMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + IntegerMatrix vector = new IntegerMatrix(new int[][]{{0}, {0}}); + matrix.setCol(1, vector); + correctMatrix = new IntegerMatrix(new int[][]{ + {3, 0}, + {3, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new IntegerMatrix(grid3); + matrix.setCol(0, new int[]{0, 0, 0}); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 2, 3}, + {0, 2, 3}, + {0, 2, 3} + }); + assertEquals("IntegerMatrix 3x3 failed setColumn.", correctMatrix, matrix); + + //4x4 + matrix = new IntegerMatrix(grid4); + matrix.setCol(0, new int[]{0, 0, 0, 0}); + correctMatrix = new IntegerMatrix(new int[][]{ + {0, 2, 3, 4}, + {0, 2, 3, 4}, + {0, 2, 3, 4}, + {0, 2, 3, 4} + }); + assertEquals("IntegerMatrix 4x4 failed setCol.", correctMatrix, matrix); + + //10x10 + matrix = new IntegerMatrix(grid10); matrix.setCol(0, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); correctMatrix = new IntegerMatrix(new int[][]{ - {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + {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}, @@ -381,21 +639,36 @@ public class TestIntegerMatrix{ } @Test - public void testAdds(){ + public void testAddRow(){ + //0x0 + IntegerMatrix matrix = new IntegerMatrix(); + matrix.addRow(new int[]{0}); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); + assertEquals(correctMatrix, matrix); + //1x1 - //AddRow - IntegerMatrix matrix = new IntegerMatrix(grid1); - matrix.addRow(new int[]{1}); - IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}, {1}}); - assertEquals("IntegerMatrix 1x1 failed addRow.", correctMatrix, matrix); - //AddColumn matrix = new IntegerMatrix(grid1); - matrix.addCol(new int[]{1}); - correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); - assertEquals("IntegerMatrix 1x1 failed addCol.", correctMatrix, matrix); + matrix.addRow(new int[]{1}); + correctMatrix = new IntegerMatrix(new int[][]{{1}, {1}}); + assertEquals("IntegerMatrix 1x1 failed addRow.", correctMatrix, matrix); + + //Invalid adds + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(new int[]{0, 0}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((int[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((IntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(testMatrix2); + }); //2x2 - //AddRow matrix = new IntegerMatrix(grid2); matrix.addRow(new int[]{1, 2}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -404,17 +677,17 @@ public class TestIntegerMatrix{ {1, 2} }); assertEquals("IntegerMatrix 2x2 failed addRow.", correctMatrix, matrix); - //AddColumn + //Matrix matrix = new IntegerMatrix(grid2); - matrix.addCol(new int[]{3, 3}); + matrix.addRow(new IntegerMatrix(new int[][]{{0, 0}})); correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3} + {1, 2}, + {1, 2}, + {0, 0} }); - assertEquals("IntegerMatrix 2x2 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //3x3 - //AddRow matrix = new IntegerMatrix(grid3); matrix.addRow(new int[]{1, 2, 3}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -424,18 +697,8 @@ public class TestIntegerMatrix{ {1, 2, 3} }); assertEquals("IntegerMatrix 3x3 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix = new IntegerMatrix(grid3); - matrix.addCol(new int[]{4, 4, 4}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals("IntegerMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 - //AddRow matrix = new IntegerMatrix(grid4); matrix.addRow(new int[]{1, 2, 3, 4}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -446,19 +709,8 @@ public class TestIntegerMatrix{ {1, 2, 3, 4} }); assertEquals("IntegerMatrix 4x4 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix.addCol(new int[]{5, 5, 5, 5, 5}); - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5} - }); - assertEquals("IntegerMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 - //AddRow matrix = new IntegerMatrix(grid10); matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -475,7 +727,77 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("IntegerMatrix 10x10 failed addRow.", correctMatrix, matrix); - //AddColumn + } + + @Test + public void testAddCol(){ + //0x0 + IntegerMatrix matrix = new IntegerMatrix(); + matrix.addCol(new int[]{0}); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}}); + assertEquals(correctMatrix, matrix); + + //1x1 + matrix = new IntegerMatrix(grid1); + matrix.addCol(new int[]{1}); + correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); + assertEquals("IntegerMatrix 1x1 failed addCol.", correctMatrix, matrix); + + //Invalid adds + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(new int[]{0, 0}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((int[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((IntegerMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(testMatrix2); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); + matrix.addCol(new int[]{3, 3}); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("IntegerMatrix 2x2 failed addCol.", correctMatrix, matrix); + //Matrix + matrix = new IntegerMatrix(grid2); + matrix.addCol(new IntegerMatrix(new int[][]{{0}, {0}})); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2, 0}, + {1, 2, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new IntegerMatrix(grid3); + matrix.addCol(new int[]{4, 4, 4}); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }); + assertEquals("IntegerMatrix 3x3 failed addCol.", correctMatrix, matrix); + + //4x4 + matrix = new IntegerMatrix(grid4); + matrix.addCol(new int[]{5, 5, 5, 5}); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5} + }); + assertEquals("IntegerMatrix 4x4 failed addCol.", correctMatrix, matrix); + + //10x10 matrix = new IntegerMatrix(grid10); matrix.addCol(new int[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); correctMatrix = new IntegerMatrix(new int[][]{ @@ -494,22 +816,24 @@ public class TestIntegerMatrix{ } @Test - public void testAppends(){ + public void testAppendRight(){ //1x1 - //appendRight IntegerMatrix matrix = new IntegerMatrix(grid1); IntegerMatrix secondMatrix = new IntegerMatrix(grid1); IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1, 1}}); assertEquals("IntegerMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new IntegerMatrix(new int[][]{ - {1}, - {1} + + //Invalid appends + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendRight(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendRight(null); }); - assertEquals("IntegerMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 - //appendRight matrix = new IntegerMatrix(grid2); secondMatrix = new IntegerMatrix(grid2); correctMatrix = new IntegerMatrix(new int[][]{ @@ -517,17 +841,8 @@ public class TestIntegerMatrix{ {1, 2, 1, 2} }); assertEquals("IntegerMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals("IntegerMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 - //appendRight matrix = new IntegerMatrix(grid3); secondMatrix = new IntegerMatrix(grid3); correctMatrix = new IntegerMatrix(new int[][]{ @@ -536,19 +851,8 @@ public class TestIntegerMatrix{ {1, 2, 3, 1, 2, 3} }); assertEquals("IntegerMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new IntegerMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals("IntegerMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 - //appendRight matrix = new IntegerMatrix(grid4); secondMatrix = new IntegerMatrix(grid4); correctMatrix = new IntegerMatrix(new int[][]{ @@ -558,21 +862,8 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 1, 2, 3, 4} }); assertEquals("IntegerMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new IntegerMatrix(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} - }); - assertEquals("IntegerMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 - //appendRight matrix = new IntegerMatrix(grid10); secondMatrix = new IntegerMatrix(grid10); correctMatrix = new IntegerMatrix(new int[][]{ @@ -588,7 +879,71 @@ public class TestIntegerMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("IntegerMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom + } + + @Test + public void testAppendBottom(){ + //1x1 + IntegerMatrix matrix = new IntegerMatrix(grid1); + IntegerMatrix secondMatrix = new IntegerMatrix(grid1); + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{ + {1}, + {1} + }); + assertEquals("IntegerMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //Invalid appends + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendBottom(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendBottom(null); + }); + + //2x2 + matrix = new IntegerMatrix(grid2); + secondMatrix = new IntegerMatrix(grid2); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2}, + {1, 2}, + {1, 2}, + {1, 2} + }); + assertEquals("IntegerMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //3x3 + matrix = new IntegerMatrix(grid3); + secondMatrix = new IntegerMatrix(grid3); + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("IntegerMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //4x4 + matrix = new IntegerMatrix(grid4); + secondMatrix = new IntegerMatrix(grid4); + correctMatrix = new IntegerMatrix(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} + }); + assertEquals("IntegerMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //10x10 + matrix = new IntegerMatrix(grid10); + secondMatrix = new IntegerMatrix(grid10); correctMatrix = new IntegerMatrix(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}, @@ -604,6 +959,18 @@ public class TestIntegerMatrix{ assertEquals("IntegerMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } + @Test + public void testIsSquare(){ + IntegerMatrix matrix = new IntegerMatrix(); + assertFalse(matrix.isSquare()); + + matrix = new IntegerMatrix(2, 2, 0); + assertTrue(matrix.isSquare()); + + matrix = new IntegerMatrix(2, 3, 0); + assertFalse(matrix.isSquare()); + } + @Test public void testAddition(){ //1x1 @@ -613,6 +980,17 @@ public class TestIntegerMatrix{ assertEquals("IntegerMatrix 1x1 failed add IntegerMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("IntegerMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(1)); + //Invalid adds + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); + final IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix3); + }); + //2x2 matrix = new IntegerMatrix(grid2); transformMatrix = new IntegerMatrix(transformGrid2_1); @@ -703,6 +1081,17 @@ public class TestIntegerMatrix{ assertEquals("IntegerMatrix 1x1 failed subtract IntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("IntegerMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + //Invalid subtracts + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); + final IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix3); + }); + //2x2 matrix = new IntegerMatrix(grid2); transformMatrix = new IntegerMatrix(grid2); @@ -768,6 +1157,13 @@ public class TestIntegerMatrix{ assertEquals("IntegerMatrix 1x1 failed multiplication IntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix)); assertEquals("IntegerMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + //Invalid multiplication + final IntegerMatrix testMatrix = new IntegerMatrix(grid1); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.multiply(testMatrix2); + }); + //2x2 matrix = new IntegerMatrix(grid2); transformMatrix = new IntegerMatrix(transformGrid2_2); @@ -912,6 +1308,13 @@ public class TestIntegerMatrix{ IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2); assertEquals("IntegerMatrix 1x1 failed dot product IntegerMatrix.", 2, matrix.dotProduct(transformMatrix)); + //Invalid products + IntegerMatrix testMatrix = new IntegerMatrix(grid1); + IntegerMatrix testMatrix2 = new IntegerMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.dotProduct(testMatrix2); + }); + //2x2 matrix = new IntegerMatrix(grid2); transformMatrix = new IntegerMatrix(transformGrid2_2); @@ -941,6 +1344,17 @@ public class TestIntegerMatrix{ IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}}); assertEquals("IntegerMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + //Invalid hadamard products + IntegerMatrix testMatrix = new IntegerMatrix(grid1); + IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{{0}, {0}}); + IntegerMatrix testMatrix3 = new IntegerMatrix(new int[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix3); + }); + //2x2 matrix = new IntegerMatrix(grid2); transformMatrix = new IntegerMatrix(transformGrid2_2); @@ -1046,6 +1460,12 @@ public class TestIntegerMatrix{ IntegerMatrix matrix = new IntegerMatrix(grid1); assertEquals("IntegerMatrix 1x1 failed determinant.", 1, matrix.determinant()); + //Invalid determinants + IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.determinant(); + }); + //2x2 matrix = new IntegerMatrix(grid2); assertEquals("IntegerMatrix 2x2 failed determinant1.", 0, matrix.determinant()); @@ -1054,6 +1474,8 @@ public class TestIntegerMatrix{ {4, 1} }); assertEquals("IntegerMatrix 2x2 failed determinant2.", -15, matrix.determinant()); + //det + assertEquals(matrix.determinant(), matrix.det()); //3x3 matrix = new IntegerMatrix(grid3); @@ -1075,6 +1497,22 @@ public class TestIntegerMatrix{ {4, 1, 2, 3} }); assertEquals("IntegerMatrix 4x4 failed determinant2.", 160, matrix.determinant()); + //Column + matrix = new IntegerMatrix(new int[][]{ + {1, 2, 3, 4}, + {1, 0, 3, 4}, + {1, 0, 3, 4}, + {0, 0, 3, 4} + }); + assertEquals(0, matrix.determinant()); + //Column2 + matrix = new IntegerMatrix(new int[][]{ + {1, 2, 0, 4}, + {1, 2, 0, 4}, + {1, 2, 0, 4}, + {0, 2, 0, 4}, + }); + assertEquals(0, matrix.determinant()); //10x10 matrix = new IntegerMatrix(grid10); @@ -1101,6 +1539,12 @@ public class TestIntegerMatrix{ IntegerMatrix correctMatrix = new IntegerMatrix(grid1); assertEquals("IntegerMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + //Invalid cofactor + IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.cofactor(); + }); + //2x2 matrix = new IntegerMatrix(grid2); correctMatrix = new IntegerMatrix(new int[][]{ @@ -1108,6 +1552,8 @@ public class TestIntegerMatrix{ {-2, 1} }); assertEquals("IntegerMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + //cof + assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new IntegerMatrix(grid3); @@ -1159,6 +1605,16 @@ public class TestIntegerMatrix{ IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); assertEquals("IntegerMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + //Invalid powers + final IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0}, {0}}); + final IntegerMatrix testMatrix2 = new IntegerMatrix(grid1); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.pow(1); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.pow(-1); + }); + //2x2 matrix = new IntegerMatrix(grid2); correctMatrix = new IntegerMatrix(new int[][]{ @@ -1234,6 +1690,8 @@ public class TestIntegerMatrix{ {-1, 1} }); assertEquals("IntegerMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + //adj + assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new IntegerMatrix(grid3); @@ -1271,6 +1729,20 @@ public class TestIntegerMatrix{ IntegerMatrix correctMatrix = new IntegerMatrix(grid1); assertEquals("IntegerMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + //Invalid inverse + IntegerMatrix testMatrix = new IntegerMatrix(new int[][]{{0, 0}}); + IntegerMatrix testMatrix2 = new IntegerMatrix(new int[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.inverse(); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.inverse(); + }); + //2x2 matrix = new IntegerMatrix(new int[][]{ {1, 4}, @@ -1313,4 +1785,68 @@ public class TestIntegerMatrix{ //10x10 //?Skipped 10x10 because it would take a long time to compute } + + @Test + public void testGenerateIdentity(){ + //0x0 + assertThrows(InvalidGeometryException.class, () -> { + IntegerMatrix.generateIdentity(0); + }); + + //1x1 + IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}}); + assertEquals(correctMatrix, IntegerMatrix.generateIdentity(1)); + + //2x2 + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 0}, + {0, 1} + }); + assertEquals(correctMatrix, IntegerMatrix.generateIdentity(2)); + + //3x3 + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }); + assertEquals(correctMatrix, IntegerMatrix.generateIdentity(3)); + + //4x4 + correctMatrix = new IntegerMatrix(new int[][]{ + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }); + assertEquals(correctMatrix, IntegerMatrix.generateIdentity(4)); + + //10x10 + correctMatrix = new IntegerMatrix(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}, + }); + assertEquals(correctMatrix, IntegerMatrix.generateIdentity(10)); + } + + @Test + public void testHashCode(){ + IntegerMatrix matrix = new IntegerMatrix(); + assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); + } + + @Test + public void testToString(){ + IntegerMatrix matrix = new IntegerMatrix(grid3); + String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; + assertEquals(matrixString, matrix.toString()); + } } diff --git a/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java b/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java index 10fae9c..c0d655e 100644 --- a/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java @@ -1,15 +1,26 @@ //Matrix/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java //Mattrixwv // Created: 02-10-22 -//Modified: 02-10-22 +//Modified: 07-01-22 package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import java.util.Arrays; + import org.junit.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{ //Grid 1x1 @@ -113,10 +124,85 @@ public class TestLongMatrix{ }; + @Test + public void testConstructor(){ + //Default constructor + LongMatrix matrix = new LongMatrix(); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + + //Filler constructor + //0 rows + assertThrows(InvalidGeometryException.class, () -> { + new LongMatrix(0, 0, 0); + }); + assertThrows(InvalidGeometryException.class, () -> { + new LongMatrix(1, 0, 0); + }); + //Good values + matrix = new LongMatrix(2, 2, 0); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(0, matrix.get(0, 0)); + assertEquals(0, matrix.get(0, 1)); + assertEquals(0, matrix.get(1, 0)); + assertEquals(0, matrix.get(1, 1)); + + //Matrix constructor + matrix.set(0, 0, 1); + matrix.set(0, 1, 2); + matrix.set(1, 0, 1); + matrix.set(1, 1, 2); + LongMatrix matrix2 = new LongMatrix(matrix); + assertEquals(2, matrix2.getNumRows()); + assertEquals(2, matrix2.getNumCols()); + assertEquals(1, matrix2.get(0, 0)); + assertEquals(2, matrix2.get(0, 1)); + assertEquals(1, matrix.get(1, 0)); + assertEquals(2, matrix.get(1, 1)); + + //Array constructor + //0 length + long[][] grid = new long[0][0]; + matrix = new LongMatrix(grid); + assertEquals(0, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //0 cols + grid = new long[1][0]; + matrix = new LongMatrix(grid); + assertEquals(1, matrix.getNumRows()); + assertEquals(0, matrix.getNumCols()); + //Uneven rows + assertThrows(InvalidRowSizeException.class, () -> { + long[][] grid1 = new long[2][]; + grid1[0] = new long[1]; + grid1[0][0] = 0; + grid1[1] = new long[2]; + grid1[1][0] = 1; + grid1[1][1] = 2; + new LongMatrix(grid1); + }); + + //2x2 + grid = grid2; + matrix = new LongMatrix(grid); + assertEquals(2, matrix.getNumRows()); + assertEquals(2, matrix.getNumCols()); + assertEquals(1, matrix.get(0, 0)); + assertEquals(2, matrix.get(0, 1)); + assertEquals(1, matrix.get(1, 0)); + assertEquals(2, matrix.get(1, 1)); + } + @Test public void testEquals(){ - //1x1 + //Invalid equals LongMatrix matrix = new LongMatrix(grid1); + assertNotEquals(matrix, null); + assertNotEquals(matrix, new int[0]); + + //1x1 + matrix = new LongMatrix(grid1); boolean gridEquals = matrix.equals(matrix); assertTrue("LongMatrix 1x1 failed equals LongMatrix.", gridEquals); @SuppressWarnings("unlikely-arg-type") @@ -130,6 +216,17 @@ public class TestLongMatrix{ @SuppressWarnings("unlikely-arg-type") boolean gridEquals21 = matrix.equals(grid2); assertTrue("LongMatrix 2x2 failed equals long[][].", gridEquals21); + //false + @SuppressWarnings("unlikely-arg-type") + boolean gridEquals22 = matrix.equals(transformGrid2_1); + assertFalse(gridEquals22); + gridEquals2 = matrix.equals(new LongMatrix(grid3)); + assertFalse(gridEquals2); + gridEquals2 = matrix.equals(new LongMatrix(new long[][]{ + {0, 1, 2}, + {0, 1, 2} + })); + assertFalse(gridEquals2); //3x3 matrix = new LongMatrix(grid3); @@ -157,24 +254,103 @@ public class TestLongMatrix{ } @Test - public void testGets(){ + public void testGet(){ //1x1 LongMatrix matrix = new LongMatrix(grid1); assertEquals("LongMatrix 1x1 failed get.", 1, matrix.get(0, 0)); - //GetRow - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals("LongMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn - correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals("LongMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final LongMatrix testMatrix = new LongMatrix(grid1); + 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 LongMatrix(grid2); assertEquals("LongMatrix 2x2 failed get.", 1, matrix.get(0, 0)); - //GetRow + + //3x3 + matrix = new LongMatrix(grid3); + assertEquals("LongMatrix 3x3 failed get.", 1, matrix.get(0, 0)); + + //4x4 + matrix = new LongMatrix(grid4); + assertEquals("LongMatrix 4x4 failed get.", 1, matrix.get(0, 0)); + + //10x10 + matrix = new LongMatrix(grid10); + assertEquals("LongMatrix 10x10 failed get.", 1, matrix.get(0, 0)); + } + + @Test + public void testGetRow(){ + //1x1 + LongMatrix matrix = new LongMatrix(grid1); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); + assertEquals("LongMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); + + //Invalid gets + final LongMatrix testMatrix = new LongMatrix(matrix); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getRow(3); + }); + + //2x2 + matrix = new LongMatrix(grid2); correctMatrix = new LongMatrix(new long[][]{{1, 2}}); assertEquals("LongMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn + + //3x3 + matrix = new LongMatrix(grid3); + correctMatrix = new LongMatrix(new long[][]{{1, 2, 3}}); + assertEquals("LongMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + + //4x4 + matrix = new LongMatrix(grid4); + matrix = new LongMatrix(grid4); + correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4}}); + assertEquals("LongMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); + + //10x10 + matrix = new LongMatrix(grid10); + correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); + assertEquals("LongMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + } + + @Test + public void testGetCol(){ + //1x1 + LongMatrix matrix = new LongMatrix(grid1); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); + assertEquals("LongMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(0)); + + //Invalid gets + final LongMatrix testMatrix = new LongMatrix(); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(-1); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.getCol(3); + }); + final LongMatrix testMatrix2 = new LongMatrix(grid1); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix2.getCol(3); + }); + + //2x2 + matrix = new LongMatrix(grid2); correctMatrix = new LongMatrix(new long[][]{ {1}, {1} @@ -183,11 +359,6 @@ public class TestLongMatrix{ //3x3 matrix = new LongMatrix(grid3); - assertEquals("LongMatrix 3x3 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3}}); - assertEquals("LongMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new LongMatrix(new long[][]{ {1}, {1}, @@ -197,11 +368,6 @@ public class TestLongMatrix{ //4x4 matrix = new LongMatrix(grid4); - assertEquals("LongMatrix 4x4 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4}}); - assertEquals("LongMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new LongMatrix(new long[][]{ {1}, {1}, @@ -212,11 +378,6 @@ public class TestLongMatrix{ //10x10 matrix = new LongMatrix(grid10); - assertEquals("LongMatrix 10x10 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}); - assertEquals("LongMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new LongMatrix(new long[][]{ {1}, {1}, @@ -233,24 +394,29 @@ public class TestLongMatrix{ } @Test - public void testSets(){ + public void testSet(){ //1x1 - //Set LongMatrix matrix = new LongMatrix(grid1); matrix.set(0, 0, 2); LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}}); assertEquals("LongMatrix 1x1 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new long[]{0}); - correctMatrix = new LongMatrix(new long[][]{{0}}); - assertEquals("LongMatrix 1x1 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new long[]{1}); - correctMatrix = new LongMatrix(new long[][]{{1}}); - assertEquals("LongMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid sets + final LongMatrix testMatrix = new LongMatrix(grid1); + 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 - //Set matrix = new LongMatrix(grid2); matrix.set(0, 0, 3); correctMatrix = new LongMatrix(new long[][]{ @@ -258,24 +424,8 @@ public class TestLongMatrix{ {1, 2} }); assertEquals("LongMatrix 2x2 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(1, new long[]{2, 1}); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2}, - {2, 1} - }); - assertEquals("LongMatrix 2x2 failed set row.", correctMatrix, matrix); - //SetColumn - matrix = new LongMatrix(grid2); - matrix.setCol(0, new long[]{3, 3}); - correctMatrix = new LongMatrix(new long[][]{ - {3, 2}, - {3, 2} - }); - assertEquals("LongMatrix 2x2 failed set row.", correctMatrix, matrix); //3x3 - //Set matrix = new LongMatrix(grid3); matrix.set(0, 0, 3); correctMatrix = new LongMatrix(new long[][]{ @@ -284,25 +434,8 @@ public class TestLongMatrix{ {1, 2, 3}, }); assertEquals("LongMatrix 3x3 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new long[]{0, 1, 2}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals("LongMatrix 3x3 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new long[]{0, 0, 0}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 1, 2}, - {0, 2, 3}, - {0, 2, 3} - }); - assertEquals("LongMatrix 3x3 failed setColumn.", correctMatrix, matrix); //4x4 - //Set matrix = new LongMatrix(grid4); matrix.set(0, 0, 3); correctMatrix = new LongMatrix(new long[][]{ @@ -312,27 +445,8 @@ public class TestLongMatrix{ {1, 2, 3, 4} }); assertEquals("LongMatrix 4x4 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new long[]{4, 3, 2, 1}); - correctMatrix = new LongMatrix(new long[][]{ - {4, 3, 2, 1}, - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals("LongMatrix 4x4 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new long[]{0, 0, 0, 0}); - correctMatrix = new LongMatrix(new long[][]{ - {0, 3, 2, 1}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }); - assertEquals("LongMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 - //Set matrix = new LongMatrix(grid10); matrix.set(0, 0, 3); correctMatrix = new LongMatrix(new long[][]{ @@ -348,7 +462,79 @@ public class TestLongMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("LongMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetRow + } + + @Test + public void testSetRow(){ + //1x1 + LongMatrix matrix = new LongMatrix(grid1); + matrix.setRow(0, new long[]{0}); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); + assertEquals("LongMatrix 1x1 failed setRow.", correctMatrix, matrix); + + //Invalid setRows + final LongMatrix testMatrix = new LongMatrix(grid1); + final long[] testGrid = {0, 0}; + final LongMatrix testMatrix2 = new LongMatrix(grid2); + 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, (long[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setRow(0, (LongMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setRow(0, testMatrix2); + }); + + //2x2 + matrix = new LongMatrix(grid2); + matrix.setRow(1, new long[]{2, 1}); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2}, + {2, 1} + }); + assertEquals("LongMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + LongMatrix matrix2 = new LongMatrix(new long[][]{{0, 0}}); + matrix.setRow(1, matrix2); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2}, + {0, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new LongMatrix(grid3); + matrix.setRow(0, new long[]{0, 1, 2}); + correctMatrix = new LongMatrix(new long[][]{ + {0, 1, 2}, + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("LongMatrix 3x3 failed setRow.", correctMatrix, matrix); + + //4x4 + matrix = new LongMatrix(grid4); + matrix.setRow(0, new long[]{4, 3, 2, 1}); + correctMatrix = new LongMatrix(new long[][]{ + {4, 3, 2, 1}, + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }); + assertEquals("LongMatrix 4x4 failed setRow.", correctMatrix, matrix); + + //10x10 + matrix = new LongMatrix(grid10); matrix.setRow(0, new long[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); correctMatrix = new LongMatrix(new long[][]{ {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, @@ -363,10 +549,82 @@ public class TestLongMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("LongMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetColumn + } + + @Test + public void testSetCol(){ + //1x1 + LongMatrix matrix = new LongMatrix(grid1); + matrix.setCol(0, new long[]{1}); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); + assertEquals("LongMatrix 1x1 failed setCol.", correctMatrix, matrix); + + //Invalid setCols + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(); + final long[] testGrid = {0, 0}; + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(-1, testGrid); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(2, testGrid); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, (long[])null); + }); + assertThrows(InvalidCoordinatesException.class, () -> { + testMatrix.setCol(0, testGrid); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.setCol(0, (LongMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.setCol(0, testMatrix2); + }); + + //2x2 + matrix = new LongMatrix(grid2); + matrix.setCol(0, new long[]{3, 3}); + correctMatrix = new LongMatrix(new long[][]{ + {3, 2}, + {3, 2} + }); + assertEquals("LongMatrix 2x2 failed set row.", correctMatrix, matrix); + //Matrix + LongMatrix vector = new LongMatrix(new long[][]{{0}, {0}}); + matrix.setCol(1, vector); + correctMatrix = new LongMatrix(new long[][]{ + {3, 0}, + {3, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new LongMatrix(grid3); + matrix.setCol(0, new long[]{0, 0, 0}); + correctMatrix = new LongMatrix(new long[][]{ + {0, 2, 3}, + {0, 2, 3}, + {0, 2, 3} + }); + assertEquals("LongMatrix 3x3 failed setColumn.", correctMatrix, matrix); + + //4x4 + matrix = new LongMatrix(grid4); + matrix.setCol(0, new long[]{0, 0, 0, 0}); + correctMatrix = new LongMatrix(new long[][]{ + {0, 2, 3, 4}, + {0, 2, 3, 4}, + {0, 2, 3, 4}, + {0, 2, 3, 4} + }); + assertEquals("LongMatrix 4x4 failed setCol.", correctMatrix, matrix); + + //10x10 + matrix = new LongMatrix(grid10); matrix.setCol(0, new long[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); correctMatrix = new LongMatrix(new long[][]{ - {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + {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}, @@ -381,21 +639,36 @@ public class TestLongMatrix{ } @Test - public void testAdds(){ + public void testAddRow(){ + //0x0 + LongMatrix matrix = new LongMatrix(); + matrix.addRow(new long[]{0}); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); + assertEquals(correctMatrix, matrix); + //1x1 - //AddRow - LongMatrix matrix = new LongMatrix(grid1); - matrix.addRow(new long[]{1}); - LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}, {1}}); - assertEquals("LongMatrix 1x1 failed addRow.", correctMatrix, matrix); - //AddColumn matrix = new LongMatrix(grid1); - matrix.addCol(new long[]{1}); - correctMatrix = new LongMatrix(new long[][]{{1, 1}}); - assertEquals("LongMatrix 1x1 failed addCol.", correctMatrix, matrix); + matrix.addRow(new long[]{1}); + correctMatrix = new LongMatrix(new long[][]{{1}, {1}}); + assertEquals("LongMatrix 1x1 failed addRow.", correctMatrix, matrix); + + //Invalid adds + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(new long[]{0, 0}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((long[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addRow((LongMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addRow(testMatrix2); + }); //2x2 - //AddRow matrix = new LongMatrix(grid2); matrix.addRow(new long[]{1, 2}); correctMatrix = new LongMatrix(new long[][]{ @@ -404,17 +677,17 @@ public class TestLongMatrix{ {1, 2} }); assertEquals("LongMatrix 2x2 failed addRow.", correctMatrix, matrix); - //AddColumn + //Matrix matrix = new LongMatrix(grid2); - matrix.addCol(new long[]{3, 3}); + matrix.addRow(new LongMatrix(new long[][]{{0, 0}})); correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3} + {1, 2}, + {1, 2}, + {0, 0} }); - assertEquals("LongMatrix 2x2 failed addCol.", correctMatrix, matrix); + assertEquals(correctMatrix, matrix); //3x3 - //AddRow matrix = new LongMatrix(grid3); matrix.addRow(new long[]{1, 2, 3}); correctMatrix = new LongMatrix(new long[][]{ @@ -424,18 +697,8 @@ public class TestLongMatrix{ {1, 2, 3} }); assertEquals("LongMatrix 3x3 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix = new LongMatrix(grid3); - matrix.addCol(new long[]{4, 4, 4}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4}, - {1, 2, 3, 4}, - {1, 2, 3, 4} - }); - assertEquals("LongMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 - //AddRow matrix = new LongMatrix(grid4); matrix.addRow(new long[]{1, 2, 3, 4}); correctMatrix = new LongMatrix(new long[][]{ @@ -446,19 +709,8 @@ public class TestLongMatrix{ {1, 2, 3, 4} }); assertEquals("LongMatrix 4x4 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix.addCol(new long[]{5, 5, 5, 5, 5}); - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5}, - {1, 2, 3, 4, 5} - }); - assertEquals("LongMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 - //AddRow matrix = new LongMatrix(grid10); matrix.addRow(new long[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); correctMatrix = new LongMatrix(new long[][]{ @@ -475,7 +727,77 @@ public class TestLongMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("LongMatrix 10x10 failed addRow.", correctMatrix, matrix); - //AddColumn + } + + @Test + public void testAddCol(){ + //0x0 + LongMatrix matrix = new LongMatrix(); + matrix.addCol(new long[]{0}); + LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}}); + assertEquals(correctMatrix, matrix); + + //1x1 + matrix = new LongMatrix(grid1); + matrix.addCol(new long[]{1}); + correctMatrix = new LongMatrix(new long[][]{{1, 1}}); + assertEquals("LongMatrix 1x1 failed addCol.", correctMatrix, matrix); + + //Invalid adds + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(new long[]{0, 0}); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((long[])null); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.addCol((LongMatrix)null); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.addCol(testMatrix2); + }); + + //2x2 + matrix = new LongMatrix(grid2); + matrix.addCol(new long[]{3, 3}); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("LongMatrix 2x2 failed addCol.", correctMatrix, matrix); + //Matrix + matrix = new LongMatrix(grid2); + matrix.addCol(new LongMatrix(new long[][]{{0}, {0}})); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2, 0}, + {1, 2, 0} + }); + assertEquals(correctMatrix, matrix); + + //3x3 + matrix = new LongMatrix(grid3); + matrix.addCol(new long[]{4, 4, 4}); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2, 3, 4}, + {1, 2, 3, 4}, + {1, 2, 3, 4} + }); + assertEquals("LongMatrix 3x3 failed addCol.", correctMatrix, matrix); + + //4x4 + matrix = new LongMatrix(grid4); + matrix.addCol(new long[]{5, 5, 5, 5}); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5}, + {1, 2, 3, 4, 5} + }); + assertEquals("LongMatrix 4x4 failed addCol.", correctMatrix, matrix); + + //10x10 matrix = new LongMatrix(grid10); matrix.addCol(new long[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11}); correctMatrix = new LongMatrix(new long[][]{ @@ -494,22 +816,24 @@ public class TestLongMatrix{ } @Test - public void testAppends(){ + public void testAppendRight(){ //1x1 - //appendRight LongMatrix matrix = new LongMatrix(grid1); LongMatrix secondMatrix = new LongMatrix(grid1); LongMatrix correctMatrix = new LongMatrix(new long[][]{{1, 1}}); assertEquals("LongMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new LongMatrix(new long[][]{ - {1}, - {1} + + //Invalid appends + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendRight(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendRight(null); }); - assertEquals("LongMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //2x2 - //appendRight matrix = new LongMatrix(grid2); secondMatrix = new LongMatrix(grid2); correctMatrix = new LongMatrix(new long[][]{ @@ -517,17 +841,8 @@ public class TestLongMatrix{ {1, 2, 1, 2} }); assertEquals("LongMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new LongMatrix(new long[][]{ - {1, 2}, - {1, 2}, - {1, 2}, - {1, 2} - }); - assertEquals("LongMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //3x3 - //appendRight matrix = new LongMatrix(grid3); secondMatrix = new LongMatrix(grid3); correctMatrix = new LongMatrix(new long[][]{ @@ -536,19 +851,8 @@ public class TestLongMatrix{ {1, 2, 3, 1, 2, 3} }); assertEquals("LongMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new LongMatrix(new long[][]{ - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3}, - {1, 2, 3} - }); - assertEquals("LongMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 - //appendRight matrix = new LongMatrix(grid4); secondMatrix = new LongMatrix(grid4); correctMatrix = new LongMatrix(new long[][]{ @@ -558,21 +862,8 @@ public class TestLongMatrix{ {1, 2, 3, 4, 1, 2, 3, 4} }); assertEquals("LongMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new LongMatrix(new long[][]{ - {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} - }); - assertEquals("LongMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 - //appendRight matrix = new LongMatrix(grid10); secondMatrix = new LongMatrix(grid10); correctMatrix = new LongMatrix(new long[][]{ @@ -588,7 +879,71 @@ public class TestLongMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }); assertEquals("LongMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom + } + + @Test + public void testAppendBottom(){ + //1x1 + LongMatrix matrix = new LongMatrix(grid1); + LongMatrix secondMatrix = new LongMatrix(grid1); + LongMatrix correctMatrix = new LongMatrix(new long[][]{ + {1}, + {1} + }); + assertEquals("LongMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //Invalid appends + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.appendBottom(testMatrix2); + }); + assertThrows(NullMatrixException.class, () -> { + testMatrix.appendBottom(null); + }); + + //2x2 + matrix = new LongMatrix(grid2); + secondMatrix = new LongMatrix(grid2); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2}, + {1, 2}, + {1, 2}, + {1, 2} + }); + assertEquals("LongMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //3x3 + matrix = new LongMatrix(grid3); + secondMatrix = new LongMatrix(grid3); + correctMatrix = new LongMatrix(new long[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }); + assertEquals("LongMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //4x4 + matrix = new LongMatrix(grid4); + secondMatrix = new LongMatrix(grid4); + correctMatrix = new LongMatrix(new long[][]{ + {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} + }); + assertEquals("LongMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + + //10x10 + matrix = new LongMatrix(grid10); + secondMatrix = new LongMatrix(grid10); correctMatrix = new LongMatrix(new long[][]{ {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}, @@ -604,6 +959,18 @@ public class TestLongMatrix{ assertEquals("LongMatrix 10x10 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); } + @Test + public void testIsSquare(){ + LongMatrix matrix = new LongMatrix(); + assertFalse(matrix.isSquare()); + + matrix = new LongMatrix(2, 2, 0); + assertTrue(matrix.isSquare()); + + matrix = new LongMatrix(2, 3, 0); + assertFalse(matrix.isSquare()); + } + @Test public void testAddition(){ //1x1 @@ -613,6 +980,17 @@ public class TestLongMatrix{ assertEquals("LongMatrix 1x1 failed add LongMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("LongMatrix 1x1 failed add scalar.", correctMatrix, matrix.add(1)); + //Invalid adds + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); + final LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.add(testMatrix3); + }); + //2x2 matrix = new LongMatrix(grid2); transformMatrix = new LongMatrix(transformGrid2_1); @@ -703,6 +1081,17 @@ public class TestLongMatrix{ assertEquals("LongMatrix 1x1 failed subtract LongMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("LongMatrix 1x1 failed subtract scalar.", correctMatrix, matrix.subtract(1)); + //Invalid subtracts + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); + final LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.subtract(testMatrix3); + }); + //2x2 matrix = new LongMatrix(grid2); transformMatrix = new LongMatrix(grid2); @@ -768,6 +1157,13 @@ public class TestLongMatrix{ assertEquals("LongMatrix 1x1 failed multiplication LongMatrix.", correctMatrix, matrix.multiply(transformMatrix)); assertEquals("LongMatrix 1x1 failed multiplication scalar.", correctMatrix, matrix.multiply(2)); + //Invalid multiplication + final LongMatrix testMatrix = new LongMatrix(grid1); + final LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.multiply(testMatrix2); + }); + //2x2 matrix = new LongMatrix(grid2); transformMatrix = new LongMatrix(transformGrid2_2); @@ -912,6 +1308,13 @@ public class TestLongMatrix{ LongMatrix transformMatrix = new LongMatrix(transformGrid1_2); assertEquals("LongMatrix 1x1 failed dot product LongMatrix.", 2, matrix.dotProduct(transformMatrix)); + //Invalid products + LongMatrix testMatrix = new LongMatrix(grid1); + LongMatrix testMatrix2 = new LongMatrix(grid2); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.dotProduct(testMatrix2); + }); + //2x2 matrix = new LongMatrix(grid2); transformMatrix = new LongMatrix(transformGrid2_2); @@ -941,6 +1344,17 @@ public class TestLongMatrix{ LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}}); assertEquals("LongMatrix 1x1 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + //Invalid hadamard products + LongMatrix testMatrix = new LongMatrix(grid1); + LongMatrix testMatrix2 = new LongMatrix(new long[][]{{0}, {0}}); + LongMatrix testMatrix3 = new LongMatrix(new long[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix2); + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.hadamardProduct(testMatrix3); + }); + //2x2 matrix = new LongMatrix(grid2); transformMatrix = new LongMatrix(transformGrid2_2); @@ -1046,6 +1460,12 @@ public class TestLongMatrix{ LongMatrix matrix = new LongMatrix(grid1); assertEquals("LongMatrix 1x1 failed determinant.", 1, matrix.determinant()); + //Invalid determinants + LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.determinant(); + }); + //2x2 matrix = new LongMatrix(grid2); assertEquals("LongMatrix 2x2 failed determinant1.", 0, matrix.determinant()); @@ -1054,6 +1474,8 @@ public class TestLongMatrix{ {4, 1} }); assertEquals("LongMatrix 2x2 failed determinant2.", -15, matrix.determinant()); + //det + assertEquals(matrix.determinant(), matrix.det()); //3x3 matrix = new LongMatrix(grid3); @@ -1075,6 +1497,22 @@ public class TestLongMatrix{ {4, 1, 2, 3} }); assertEquals("LongMatrix 4x4 failed determinant2.", 160, matrix.determinant()); + //Column + matrix = new LongMatrix(new long[][]{ + {1, 2, 3, 4}, + {1, 0, 3, 4}, + {1, 0, 3, 4}, + {0, 0, 3, 4} + }); + assertEquals(0, matrix.determinant()); + //Column2 + matrix = new LongMatrix(new long[][]{ + {1, 2, 0, 4}, + {1, 2, 0, 4}, + {1, 2, 0, 4}, + {0, 2, 0, 4}, + }); + assertEquals(0, matrix.determinant()); //10x10 matrix = new LongMatrix(grid10); @@ -1101,6 +1539,12 @@ public class TestLongMatrix{ LongMatrix correctMatrix = new LongMatrix(grid1); assertEquals("LongMatrix 1x1 failed cofactor.", correctMatrix, matrix.cofactor()); + //Invalid cofactor + LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.cofactor(); + }); + //2x2 matrix = new LongMatrix(grid2); correctMatrix = new LongMatrix(new long[][]{ @@ -1108,6 +1552,8 @@ public class TestLongMatrix{ {-2, 1} }); assertEquals("LongMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + //cof + assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new LongMatrix(grid3); @@ -1159,6 +1605,16 @@ public class TestLongMatrix{ LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); assertEquals("LongMatrix 1x1 failed power.", correctMatrix, matrix.pow(3)); + //Invalid powers + final LongMatrix testMatrix = new LongMatrix(new long[][]{{0}, {0}}); + final LongMatrix testMatrix2 = new LongMatrix(grid1); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.pow(1); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.pow(-1); + }); + //2x2 matrix = new LongMatrix(grid2); correctMatrix = new LongMatrix(new long[][]{ @@ -1234,6 +1690,8 @@ public class TestLongMatrix{ {-1, 1} }); assertEquals("LongMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + //adj + assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new LongMatrix(grid3); @@ -1271,6 +1729,20 @@ public class TestLongMatrix{ LongMatrix correctMatrix = new LongMatrix(grid1); assertEquals("LongMatrix 1x1 failed inverse.", correctMatrix, matrix.inverse()); + //Invalid inverse + LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}}); + LongMatrix testMatrix2 = new LongMatrix(new long[][]{ + {1, 2, 3}, + {1, 2, 3}, + {1, 2, 3} + }); + assertThrows(InvalidGeometryException.class, () -> { + testMatrix.inverse(); + }); + assertThrows(InvalidScalarException.class, () -> { + testMatrix2.inverse(); + }); + //2x2 matrix = new LongMatrix(new long[][]{ {1, 4}, @@ -1313,4 +1785,68 @@ public class TestLongMatrix{ //10x10 //?Skipped 10x10 because it would take a long time to compute } + + @Test + public void testGenerateIdentity(){ + //0x0 + assertThrows(InvalidGeometryException.class, () -> { + LongMatrix.generateIdentity(0); + }); + + //1x1 + LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}}); + assertEquals(correctMatrix, LongMatrix.generateIdentity(1)); + + //2x2 + correctMatrix = new LongMatrix(new long[][]{ + {1, 0}, + {0, 1} + }); + assertEquals(correctMatrix, LongMatrix.generateIdentity(2)); + + //3x3 + correctMatrix = new LongMatrix(new long[][]{ + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }); + assertEquals(correctMatrix, LongMatrix.generateIdentity(3)); + + //4x4 + correctMatrix = new LongMatrix(new long[][]{ + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + }); + assertEquals(correctMatrix, LongMatrix.generateIdentity(4)); + + //10x10 + correctMatrix = new LongMatrix(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}, + }); + assertEquals(correctMatrix, LongMatrix.generateIdentity(10)); + } + + @Test + public void testHashCode(){ + LongMatrix matrix = new LongMatrix(); + assertEquals(Arrays.hashCode(new int[0][0]), matrix.hashCode()); + } + + @Test + public void testToString(){ + LongMatrix matrix = new LongMatrix(grid3); + String matrixString = "[1,2,3]\n[1,2,3]\n[1,2,3]"; + assertEquals(matrixString, matrix.toString()); + } } diff --git a/src/test/java/com/mattrixwv/matrix/TestModMatrix.java b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java index 7bf192b..13661da 100644 --- a/src/test/java/com/mattrixwv/matrix/TestModMatrix.java +++ b/src/test/java/com/mattrixwv/matrix/TestModMatrix.java @@ -6,10 +6,20 @@ package com.mattrixwv.matrix; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import java.util.Arrays; + import org.junit.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 @@ -113,10 +123,25 @@ public class TestModMatrix{ }; + @Test + public void testConstructor(){ + assertThrows(InvalidScalarException.class, () -> { + new ModMatrix(0); + }); + + ModMatrix matrix = new ModMatrix(1); + assertEquals(1, matrix.getMod()); + } + @Test public void testEquals(){ - //1x1 + //Invalid equals ModMatrix matrix = new ModMatrix(grid1, 26); + assertNotEquals(matrix, null); + assertNotEquals(matrix, new double[0]); + + //1x1 + matrix = new ModMatrix(grid1, 26); assertTrue("ModMatrix 1x1 failed equals ModMatrix.", matrix.equals(matrix)); @SuppressWarnings("unlikely-arg-type") boolean gridEquals = matrix.equals(grid1); @@ -128,6 +153,17 @@ public class TestModMatrix{ @SuppressWarnings("unlikely-arg-type") boolean gridEquals2 = matrix.equals(grid2); assertTrue("ModMatrix 2x2 failed equals int[][].", 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); @@ -152,24 +188,104 @@ public class TestModMatrix{ } @Test - public void testGets(){ + public void testGet(){ //1x1 ModMatrix matrix = new ModMatrix(grid1, 26); assertEquals("ModMatrix 1x1 failed get.", 1, matrix.get(0, 0)); - //GetRow - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals("ModMatrix 1x1 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn - correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals("ModMatrix 1x1 failed getCol.", correctMatrix, matrix.getCol(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("ModMatrix 2x2 failed get.", 1, matrix.get(0, 0)); - //GetRow + + //3x3 + matrix = new ModMatrix(grid3, 26); + assertEquals("ModMatrix 3x3 failed get.", 1, matrix.get(0, 0)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + assertEquals("ModMatrix 4x4 failed get.", 1, matrix.get(0, 0)); + + //10x10 + matrix = new ModMatrix(grid10, 26); + assertEquals("ModMatrix 10x10 failed get.", 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("ModMatrix 1x1 failed getRow.", 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("ModMatrix 2x2 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn + + //3x3 + matrix = new ModMatrix(grid3, 26); + correctMatrix = new ModMatrix(new int[][]{{1, 2, 3}}, 26); + assertEquals("ModMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); + + //4x4 + matrix = new ModMatrix(grid4, 26); + correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4}}, 26); + assertEquals("ModMatrix 4x4 failed getRow.", 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("ModMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); + } + + @Test + public void testGetCol(){ + //1x1 + ModMatrix matrix = new ModMatrix(grid1, 26); + ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); + assertEquals("ModMatrix 1x1 failed getCol.", 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}, @@ -178,11 +294,6 @@ public class TestModMatrix{ //3x3 matrix = new ModMatrix(grid3, 26); - assertEquals("ModMatrix 3x3 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3}}, 26); - assertEquals("ModMatrix 3x3 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetCol correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, @@ -192,11 +303,6 @@ public class TestModMatrix{ //4x4 matrix = new ModMatrix(grid4, 26); - assertEquals("ModMatrix 4x4 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4}}, 26); - assertEquals("ModMatrix 4x4 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetCol correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, @@ -207,11 +313,6 @@ public class TestModMatrix{ //10x10 matrix = new ModMatrix(grid10, 26); - assertEquals("ModMatrix 10x10 failed get.", 1, matrix.get(0, 0)); - //GetRow - correctMatrix = new ModMatrix(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 26); - assertEquals("ModMatrix 10x10 failed getRow.", correctMatrix, matrix.getRow(0)); - //GetColumn correctMatrix = new ModMatrix(new int[][]{ {1}, {1}, @@ -228,24 +329,29 @@ public class TestModMatrix{ } @Test - public void testSets(){ + public void testSet(){ //1x1 - //Set ModMatrix matrix = new ModMatrix(grid1, 26); matrix.set(0, 0, 2); ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); assertEquals("ModMatrix 1x1 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new int[]{0}); - correctMatrix = new ModMatrix(new int[][]{{0}}, 26); - assertEquals("ModMatrix 1x1 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{1}); - correctMatrix = new ModMatrix(new int[][]{{1}}, 26); - assertEquals("ModMatrix 1x1 failed setCol.", 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 - //Set matrix = new ModMatrix(grid2, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ @@ -253,24 +359,8 @@ public class TestModMatrix{ {1, 2} }, 26); assertEquals("ModMatrix 2x2 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(1, new int[]{2, 1}); - correctMatrix = new ModMatrix(new int[][]{ - {3, 2}, - {2, 1} - }, 26); - assertEquals("ModMatrix 2x2 failed set row.", correctMatrix, matrix); - //SetColumn - matrix = new ModMatrix(grid2, 26); - matrix.setCol(0, new int[]{3, 3}); - correctMatrix = new ModMatrix(new int[][]{ - {3, 2}, - {3, 2} - }, 26); - assertEquals("ModMatrix 2x2 failed set row.", correctMatrix, matrix); //3x3 - //Set matrix = new ModMatrix(grid3, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ @@ -279,25 +369,8 @@ public class TestModMatrix{ {1, 2, 3}, }, 26); assertEquals("ModMatrix 3x3 failed set.", correctMatrix, matrix); - //SetRow - matrix.setRow(0, new int[]{0, 1, 2}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 1, 2}, - {1, 2, 3}, - {1, 2, 3} - }, 26); - assertEquals("ModMatrix 3x3 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{0, 0, 0}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 1, 2}, - {0, 2, 3}, - {0, 2, 3} - }, 26); - assertEquals("ModMatrix 3x3 failed setColumn.", correctMatrix, matrix); //4x4 - //Set matrix = new ModMatrix(grid4, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ @@ -307,27 +380,8 @@ public class TestModMatrix{ {1, 2, 3, 4} }, 26); assertEquals("ModMatrix 4x4 failed set.", correctMatrix, matrix); - //SetRow - 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("ModMatrix 4x4 failed setRow.", correctMatrix, matrix); - //SetColumn - matrix.setCol(0, new int[]{0, 0, 0, 0}); - correctMatrix = new ModMatrix(new int[][]{ - {0, 3, 2, 1}, - {0, 2, 3, 4}, - {0, 2, 3, 4}, - {0, 2, 3, 4} - }, 26); - assertEquals("ModMatrix 4x4 failed setCol.", correctMatrix, matrix); //10x10 - //Set matrix = new ModMatrix(grid10, 26); matrix.set(0, 0, 3); correctMatrix = new ModMatrix(new int[][]{ @@ -343,7 +397,87 @@ public class TestModMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals("ModMatrix 10x10 failed set.", correctMatrix, matrix); - //SetRow + } + + @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("ModMatrix 1x1 failed setRow.", 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("ModMatrix 2x2 failed set row.", 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("ModMatrix 3x3 failed setRow.", 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("ModMatrix 4x4 failed setRow.", 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}, @@ -358,10 +492,90 @@ public class TestModMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals("ModMatrix 10x10 failed setRow.", correctMatrix, matrix); - //SetColumn + } + + @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("ModMatrix 1x1 failed setCol.", 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("ModMatrix 2x2 failed set row.", 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("ModMatrix 3x3 failed setColumn.", 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("ModMatrix 4x4 failed setCol.", 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, 9, 8, 7, 6, 5, 4, 3, 2, 1}, + {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}, @@ -376,21 +590,36 @@ public class TestModMatrix{ } @Test - public void testAdds(){ + 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 - //AddRow - ModMatrix matrix = new ModMatrix(grid1, 26); - matrix.addRow(new int[]{1}); - ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}, {1}}, 26); - assertEquals("ModMatrix 1x1 failed addRow.", correctMatrix, matrix); - //AddColumn matrix = new ModMatrix(grid1, 26); - matrix.addCol(new int[]{1}); - correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); - assertEquals("ModMatrix 1x1 failed addCol.", correctMatrix, matrix); + matrix.addRow(new int[]{1}); + correctMatrix = new ModMatrix(new int[][]{{1}, {1}}, 26); + assertEquals("ModMatrix 1x1 failed addRow.", 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 - //AddRow matrix = new ModMatrix(grid2, 26); matrix.addRow(new int[]{1, 2}); correctMatrix = new ModMatrix(new int[][]{ @@ -399,17 +628,25 @@ public class TestModMatrix{ {1, 2} }, 26); assertEquals("ModMatrix 2x2 failed addRow.", correctMatrix, matrix); - //AddColumn + //Matrix matrix = new ModMatrix(grid2, 26); - matrix.addCol(new int[]{3, 3}); + matrix.addRow(new ModMatrix(new int[][]{{0, 0}}, 26)); correctMatrix = new ModMatrix(new int[][]{ - {1, 2, 3}, - {1, 2, 3} + {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); - assertEquals("ModMatrix 2x2 failed addCol.", correctMatrix, matrix); //3x3 - //AddRow matrix = new ModMatrix(grid3, 26); matrix.addRow(new int[]{1, 2, 3}); correctMatrix = new ModMatrix(new int[][]{ @@ -419,18 +656,8 @@ public class TestModMatrix{ {1, 2, 3} }, 26); assertEquals("ModMatrix 3x3 failed addRow.", correctMatrix, matrix); - //AddColumn - 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("ModMatrix 3x3 failed addCol.", correctMatrix, matrix); //4x4 - //AddRow matrix = new ModMatrix(grid4, 26); matrix.addRow(new int[]{1, 2, 3, 4}); correctMatrix = new ModMatrix(new int[][]{ @@ -441,19 +668,8 @@ public class TestModMatrix{ {1, 2, 3, 4} }, 26); assertEquals("ModMatrix 4x4 failed addRow.", correctMatrix, matrix); - //AddColumn - matrix.addCol(new int[]{5, 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}, - {1, 2, 3, 4, 5} - }, 26); - assertEquals("ModMatrix 4x4 failed addCol.", correctMatrix, matrix); //10x10 - //AddRow matrix = new ModMatrix(grid10, 26); matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); correctMatrix = new ModMatrix(new int[][]{ @@ -470,7 +686,85 @@ public class TestModMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals("ModMatrix 10x10 failed addRow.", correctMatrix, matrix); - //AddColumn + } + + @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("ModMatrix 1x1 failed addCol.", 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("ModMatrix 2x2 failed addCol.", 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("ModMatrix 3x3 failed addCol.", 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("ModMatrix 4x4 failed addCol.", 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[][]{ @@ -489,22 +783,24 @@ public class TestModMatrix{ } @Test - public void testAppends(){ + public void testAppendRight(){ //1x1 - //appendRight ModMatrix matrix = new ModMatrix(grid1, 26); ModMatrix secondMatrix = new ModMatrix(grid1, 26); ModMatrix correctMatrix = new ModMatrix(new int[][]{{1, 1}}, 26); assertEquals("ModMatrix 1x1 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new ModMatrix(new int[][]{ - {1}, - {1} - }, 26); - assertEquals("ModMatrix 1x1 failed appendBottom.", correctMatrix, matrix.appendBottom(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 - //appendRight matrix = new ModMatrix(grid2, 26); secondMatrix = new ModMatrix(grid2, 26); correctMatrix = new ModMatrix(new int[][]{ @@ -512,17 +808,11 @@ public class TestModMatrix{ {1, 2, 1, 2} }, 26); assertEquals("ModMatrix 2x2 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - correctMatrix = new ModMatrix(new int[][]{ - {1, 2}, - {1, 2}, - {1, 2}, - {1, 2} - }, 26); - assertEquals("ModMatrix 2x2 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); + //Integer matrix + IntegerMatrix thirdMatrix = new IntegerMatrix(grid2); + assertEquals(correctMatrix, matrix.appendRight(thirdMatrix)); //3x3 - //appendRight matrix = new ModMatrix(grid3, 26); secondMatrix = new ModMatrix(grid3, 26); correctMatrix = new ModMatrix(new int[][]{ @@ -531,19 +821,8 @@ public class TestModMatrix{ {1, 2, 3, 1, 2, 3} }, 26); assertEquals("ModMatrix 3x3 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - 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("ModMatrix 3x3 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //4x4 - //appendRight matrix = new ModMatrix(grid4, 26); secondMatrix = new ModMatrix(grid4, 26); correctMatrix = new ModMatrix(new int[][]{ @@ -553,21 +832,8 @@ public class TestModMatrix{ {1, 2, 3, 4, 1, 2, 3, 4} }, 26); assertEquals("ModMatrix 4x4 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom - 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("ModMatrix 4x4 failed appendBottom.", correctMatrix, matrix.appendBottom(secondMatrix)); //10x10 - //appendRight matrix = new ModMatrix(grid10, 26); secondMatrix = new ModMatrix(grid10, 26); correctMatrix = new ModMatrix(new int[][]{ @@ -583,7 +849,74 @@ public class TestModMatrix{ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} }, 26); assertEquals("ModMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix)); - //appendBottom + } + + @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("ModMatrix 1x1 failed appendBottom.", 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("ModMatrix 2x2 failed appendBottom.", 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("ModMatrix 3x3 failed appendBottom.", 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("ModMatrix 4x4 failed appendBottom.", 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}, @@ -599,6 +932,18 @@ public class TestModMatrix{ assertEquals("ModMatrix 10x10 failed appendBottom.", 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 @@ -608,6 +953,17 @@ public class TestModMatrix{ assertEquals("ModMatrix 1x1 failed add ModMatrix.", correctMatrix, matrix.add(transformMatrix)); assertEquals("ModMatrix 1x1 failed add scalar.", 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); @@ -621,6 +977,13 @@ public class TestModMatrix{ {2, 3} }, 26); assertEquals("ModMatrix 2x2 failed add scalar.", 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); @@ -698,6 +1061,17 @@ public class TestModMatrix{ assertEquals("ModMatrix 1x1 failed subtract ModMatrix.", correctMatrix, matrix.subtract(transformMatrix)); assertEquals("ModMatrix 1x1 failed subtract scalar.", 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); @@ -708,6 +1082,10 @@ public class TestModMatrix{ {0, 1} }, 26); assertEquals("ModMatrix 2x2 failed subtract scalar.", 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); @@ -763,6 +1141,13 @@ public class TestModMatrix{ assertEquals("ModMatrix 1x1 failed multiplication ModMatrix.", correctMatrix, matrix.multiply(transformMatrix)); assertEquals("ModMatrix 1x1 failed multiplication scalar.", 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); @@ -785,6 +1170,13 @@ public class TestModMatrix{ {2, 4} }, 26); assertEquals("ModMatrix 2x2 failed multiplication scalar.", 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); @@ -907,10 +1299,20 @@ public class TestModMatrix{ ModMatrix transformMatrix = new ModMatrix(transformGrid1_2, 26); assertEquals("ModMatrix 1x1 failed dot product ModMatrix.", 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("ModMatrix 2x2 failed dot product ModMatrix.", 30, matrix.dotProduct(transformMatrix)); + //Integer matrix + IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); + assertEquals(30, matrix.dotProduct(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); @@ -936,6 +1338,17 @@ public class TestModMatrix{ ModMatrix correctMatrix = new ModMatrix(new int[][]{{2}}, 26); assertEquals("ModMatrix 1x1 failed hadamard product.", 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); @@ -944,6 +1357,9 @@ public class TestModMatrix{ {2, 6} }, 26); assertEquals("ModMatrix 2x2 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix)); + //Integer matrix + IntegerMatrix transformMatrix2 = new IntegerMatrix(transformGrid2_2); + assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix2)); //3x3 matrix = new ModMatrix(grid3, 26); @@ -1041,6 +1457,12 @@ public class TestModMatrix{ ModMatrix matrix = new ModMatrix(grid1, 26); assertEquals("ModMatrix 1x1 failed determinant.", 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("ModMatrix 2x2 failed determinant1.", 0, matrix.determinant()); @@ -1049,6 +1471,8 @@ public class TestModMatrix{ {4, 1} }, 26); assertEquals("ModMatrix 2x2 failed determinant2.", -15, matrix.determinant()); + //det + assertEquals(matrix.determinant(), matrix.det()); //3x3 matrix = new ModMatrix(grid3, 26); @@ -1096,6 +1520,12 @@ public class TestModMatrix{ ModMatrix correctMatrix = new ModMatrix(grid1, 26); assertEquals("ModMatrix 1x1 failed cofactor.", 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[][]{ @@ -1103,6 +1533,8 @@ public class TestModMatrix{ {-2, 1} }, 26); assertEquals("ModMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor()); + //cof + assertEquals(matrix.cofactor(), matrix.cof()); //3x3 matrix = new ModMatrix(grid3, 26); @@ -1154,6 +1586,16 @@ public class TestModMatrix{ ModMatrix correctMatrix = new ModMatrix(new int[][]{{1}}, 26); assertEquals("ModMatrix 1x1 failed power.", 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[][]{ @@ -1229,6 +1671,8 @@ public class TestModMatrix{ {-1, 1} }, 26); assertEquals("ModMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint()); + //adj + assertEquals(matrix.adjoint(), matrix.adj()); //3x3 matrix = new ModMatrix(grid3, 26); @@ -1266,6 +1710,27 @@ public class TestModMatrix{ ModMatrix correctMatrix = new ModMatrix(grid1, 26); assertEquals("ModMatrix 1x1 failed inverse.", 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}, @@ -1308,4 +1773,68 @@ public class TestModMatrix{ //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()); + } }