Files
Matrix/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java
2022-07-09 02:00:12 -04:00

1853 lines
49 KiB
Java

//Matrix/src/test/java/com/mattrixwv/matrix/TestLongMatrix.java
//Mattrixwv
// Created: 02-10-22
//Modified: 07-09-22
package com.mattrixwv.matrix;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class TestLongMatrix{
//Grid 1x1
private static final long[][] grid1 = {
{1}
};
private static final long[][] transformGrid1_1 = {
{1}
};
private static final long[][] transformGrid1_2 = {
{2}
};
//Grid 2x2
private static final long[][] grid2 = {
{1, 2},
{1, 2}
};
private static final long[][] transformGrid2_1 = {
{1, 0},
{1, 0}
};
private static final long[][] transformGrid2_2 = {
{2, 3},
{2, 3}
};
//Grid 3x3
private static final long[][] grid3 = {
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
};
private static final long[][] transformGrid3_1 = {
{2, 1, 0},
{2, 1, 0},
{2, 1, 0}
};
private static final long[][] transformGrid3_2 = {
{2, 3, 4},
{2, 3, 4},
{2, 3, 4}
};
//Grid 4x4
private static final long[][] grid4 = {
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
};
private static final long[][] transformGrid4_1 = {
{3, 2, 1, 0},
{3, 2, 1, 0},
{3, 2, 1, 0},
{3, 2, 1, 0}
};
private static final long[][] transformGrid4_2 = {
{2, 3, 4, 5},
{2, 3, 4, 5},
{2, 3, 4, 5},
{2, 3, 4, 5}
};
//Grid 10x10
private static final long[][] grid10 = {
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
};
private static final long[][] transformGrid10_1 = {
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
};
private static final long[][] transformGrid10_2 = {
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
};
@Test
public void testConstructor(){
//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(){
//Invalid equals
LongMatrix matrix = new LongMatrix(grid1);
assertNotEquals(null, matrix);
assertNotEquals(new int[0], matrix);
//1x1
matrix = new LongMatrix(grid1);
boolean gridEquals = matrix.equals(matrix);
assertTrue(gridEquals);
@SuppressWarnings("unlikely-arg-type")
boolean gridEquals1 = matrix.equals(grid1);
assertTrue(gridEquals1);
//2x2
matrix = new LongMatrix(grid2);
boolean gridEquals2 = matrix.equals(matrix);
assertTrue(gridEquals2);
@SuppressWarnings("unlikely-arg-type")
boolean gridEquals21 = matrix.equals(grid2);
assertTrue(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);
boolean gridEquals3 = matrix.equals(matrix);
assertTrue(gridEquals3);
@SuppressWarnings("unlikely-arg-type")
boolean gridEquals31 = matrix.equals(grid3);
assertTrue(gridEquals31);
//4x4
matrix = new LongMatrix(grid4);
boolean gridEquals4 = matrix.equals(matrix);
assertTrue(gridEquals4);
@SuppressWarnings("unlikely-arg-type")
boolean gridEquals41 = matrix.equals(grid4);
assertTrue(gridEquals41);
//10x10
matrix = new LongMatrix(grid10);
boolean gridEquals10 = matrix.equals(matrix);
assertTrue(gridEquals10);
@SuppressWarnings("unlikely-arg-type")
boolean gridEquals101 = matrix.equals(grid10);
assertTrue(gridEquals101);
}
@Test
public void testGet(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
assertEquals(1, matrix.get(0, 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(1, matrix.get(0, 0));
//3x3
matrix = new LongMatrix(grid3);
assertEquals(1, matrix.get(0, 0));
//4x4
matrix = new LongMatrix(grid4);
assertEquals(1, matrix.get(0, 0));
//10x10
matrix = new LongMatrix(grid10);
assertEquals(1, matrix.get(0, 0));
}
@Test
public void testGetRow(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}});
assertEquals(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(correctMatrix, matrix.getRow(0));
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(new long[][]{{1, 2, 3}});
assertEquals(correctMatrix, matrix.getRow(0));
//4x4
matrix = new LongMatrix(grid4);
matrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(new long[][]{{1, 2, 3, 4}});
assertEquals(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(correctMatrix, matrix.getRow(0));
}
@Test
public void testGetCol(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}});
assertEquals(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}
});
assertEquals(correctMatrix, matrix.getCol(0));
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(new long[][]{
{1},
{1},
{1}
});
assertEquals(correctMatrix, matrix.getCol(0));
//4x4
matrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(new long[][]{
{1},
{1},
{1},
{1}
});
assertEquals(correctMatrix, matrix.getCol(0));
//10x10
matrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(new long[][]{
{1},
{1},
{1},
{1},
{1},
{1},
{1},
{1},
{1},
{1}
});
assertEquals(correctMatrix, matrix.getCol(0));
}
@Test
public void testSet(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
matrix.set(0, 0, 2);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}});
assertEquals(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
matrix = new LongMatrix(grid2);
matrix.set(0, 0, 3);
correctMatrix = new LongMatrix(new long[][]{
{3, 2},
{1, 2}
});
assertEquals(correctMatrix, matrix);
//3x3
matrix = new LongMatrix(grid3);
matrix.set(0, 0, 3);
correctMatrix = new LongMatrix(new long[][]{
{3, 2, 3},
{1, 2, 3},
{1, 2, 3},
});
assertEquals(correctMatrix, matrix);
//4x4
matrix = new LongMatrix(grid4);
matrix.set(0, 0, 3);
correctMatrix = new LongMatrix(new long[][]{
{3, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
});
assertEquals(correctMatrix, matrix);
//10x10
matrix = new LongMatrix(grid10);
matrix.set(0, 0, 3);
correctMatrix = new LongMatrix(new long[][]{
{3, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
});
assertEquals(correctMatrix, matrix);
}
@Test
public void testSetRow(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
matrix.setRow(0, new long[]{0});
LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}});
assertEquals(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(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(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(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},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
});
assertEquals(correctMatrix, matrix);
}
@Test
public void testSetCol(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
matrix.setCol(0, new long[]{1});
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}});
assertEquals(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(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(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(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, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10}
});
assertEquals(correctMatrix, matrix);
}
@Test
public void testAddRow(){
//0x0
LongMatrix matrix = new LongMatrix();
matrix.addRow(new long[]{0});
LongMatrix correctMatrix = new LongMatrix(new long[][]{{0}});
assertEquals(correctMatrix, matrix);
//1x1
matrix = new LongMatrix(grid1);
matrix.addRow(new long[]{1});
correctMatrix = new LongMatrix(new long[][]{{1}, {1}});
assertEquals(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
matrix = new LongMatrix(grid2);
matrix.addRow(new long[]{1, 2});
correctMatrix = new LongMatrix(new long[][]{
{1, 2},
{1, 2},
{1, 2}
});
assertEquals(correctMatrix, matrix);
//Matrix
matrix = new LongMatrix(grid2);
matrix.addRow(new LongMatrix(new long[][]{{0, 0}}));
correctMatrix = new LongMatrix(new long[][]{
{1, 2},
{1, 2},
{0, 0}
});
assertEquals(correctMatrix, matrix);
//3x3
matrix = new LongMatrix(grid3);
matrix.addRow(new long[]{1, 2, 3});
correctMatrix = new LongMatrix(new long[][]{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3}
});
assertEquals(correctMatrix, matrix);
//4x4
matrix = new LongMatrix(grid4);
matrix.addRow(new long[]{1, 2, 3, 4});
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}
});
assertEquals(correctMatrix, matrix);
//10x10
matrix = new LongMatrix(grid10);
matrix.addRow(new long[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
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},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
});
assertEquals(correctMatrix, matrix);
}
@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(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(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(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(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[][]{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
});
assertEquals(correctMatrix, matrix);
}
@Test
public void testAppendRight(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix secondMatrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1, 1}});
assertEquals(correctMatrix, matrix.appendRight(secondMatrix));
//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);
});
//2x2
matrix = new LongMatrix(grid2);
secondMatrix = new LongMatrix(grid2);
correctMatrix = new LongMatrix(new long[][]{
{1, 2, 1, 2},
{1, 2, 1, 2}
});
assertEquals(correctMatrix, matrix.appendRight(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(correctMatrix, matrix.appendRight(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(correctMatrix, matrix.appendRight(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},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
});
assertEquals(correctMatrix, matrix.appendRight(secondMatrix));
}
@Test
public void testAppendBottom(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix secondMatrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{
{1},
{1}
});
assertEquals(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(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(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(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},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
});
assertEquals(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
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix transformMatrix = new LongMatrix(transformGrid1_1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}});
assertEquals(correctMatrix, matrix.add(transformMatrix));
assertEquals(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);
correctMatrix = new LongMatrix(new long[][]{
{2, 2},
{2, 2}
});
assertEquals(correctMatrix, matrix.add(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{2, 3},
{2, 3}
});
assertEquals(correctMatrix, matrix.add(1));
//3x3
matrix = new LongMatrix(grid3);
transformMatrix = new LongMatrix(transformGrid3_1);
correctMatrix = new LongMatrix(new long[][]{
{3, 3, 3},
{3, 3, 3},
{3, 3, 3}
});
assertEquals(correctMatrix, matrix.add(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{2, 3, 4},
{2, 3, 4},
{2, 3, 4}
});
assertEquals(correctMatrix, matrix.add(1));
//4x4
matrix = new LongMatrix(grid4);
transformMatrix = new LongMatrix(transformGrid4_1);
correctMatrix = new LongMatrix(new long[][]{
{4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4}
});
assertEquals(correctMatrix, matrix.add(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{2, 3, 4, 5},
{2, 3, 4, 5},
{2, 3, 4, 5},
{2, 3, 4, 5}
});
assertEquals(correctMatrix, matrix.add(1));
//10x10
matrix = new LongMatrix(grid10);
transformMatrix = new LongMatrix(transformGrid10_1);
correctMatrix = new LongMatrix(new long[][]{
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
});
assertEquals(correctMatrix, matrix.add(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
});
assertEquals(correctMatrix, matrix.add(1));
}
@Test
public void testSubtraction(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix transformMatrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{
{0}
});
assertEquals(correctMatrix, matrix.subtract(transformMatrix));
assertEquals(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);
correctMatrix = new LongMatrix(2, 2, 0);
assertEquals(correctMatrix, matrix.subtract(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{0, 1},
{0, 1}
});
assertEquals(correctMatrix, matrix.subtract(1));
//3x3
matrix = new LongMatrix(grid3);
transformMatrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(3, 3, 0);
assertEquals(correctMatrix, matrix.subtract(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{0, 1, 2},
{0, 1, 2},
{0, 1, 2}
});
assertEquals(correctMatrix, matrix.subtract(1));
//4x4
matrix = new LongMatrix(grid4);
transformMatrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(4, 4, 0);
assertEquals(correctMatrix, matrix.subtract(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{0, 1, 2, 3},
{0, 1, 2, 3},
{0, 1, 2, 3},
{0, 1, 2, 3}
});
assertEquals(correctMatrix, matrix.subtract(1));
//10x10
matrix = new LongMatrix(grid10);
transformMatrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(10, 10, 0);
assertEquals(correctMatrix, matrix.subtract(transformMatrix));
correctMatrix = new LongMatrix(new long[][]{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
});
assertEquals(correctMatrix, matrix.subtract(1));
}
@Test
public void testMultiplication(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix transformMatrix = new LongMatrix(transformGrid1_2);
LongMatrix correctMatrix = new LongMatrix(transformGrid1_2);
assertEquals(correctMatrix, matrix.multiply(transformMatrix));
assertEquals(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);
correctMatrix = new LongMatrix(new long[][]{
{6, 9},
{6, 9}
});
assertEquals(correctMatrix, matrix.multiply(transformMatrix));
LongMatrix vector = new LongMatrix(new long[][]{
{2},
{3}
});
correctMatrix = new LongMatrix(new long[][]{
{8},
{8}
});
assertEquals(correctMatrix, matrix.multiply(vector));
correctMatrix = new LongMatrix(new long[][]{
{2, 4},
{2, 4}
});
assertEquals(correctMatrix, matrix.multiply(2));
//3x3
matrix = new LongMatrix(grid3);
transformMatrix = new LongMatrix(transformGrid3_2);
correctMatrix = new LongMatrix(new long[][]{
{12, 18, 24},
{12, 18, 24},
{12, 18, 24}
});
assertEquals(correctMatrix, matrix.multiply(transformMatrix));
vector = new LongMatrix(new long[][]{
{2},
{3},
{4}
});
correctMatrix = new LongMatrix(new long[][]{
{20},
{20},
{20}
});
assertEquals(correctMatrix, matrix.multiply(vector));
correctMatrix = new LongMatrix(new long[][]{
{2, 4, 6},
{2, 4, 6},
{2, 4, 6}
});
assertEquals(correctMatrix, matrix.multiply(2));
//4x4
matrix = new LongMatrix(grid4);
transformMatrix = new LongMatrix(transformGrid4_2);
correctMatrix = new LongMatrix(new long[][]{
{20, 30, 40, 50},
{20, 30, 40, 50},
{20, 30, 40, 50},
{20, 30, 40, 50},
});
assertEquals(correctMatrix, matrix.multiply(transformMatrix));
vector = new LongMatrix(new long[][]{
{2},
{3},
{4},
{5}
});
correctMatrix = new LongMatrix(new long[][]{
{40},
{40},
{40},
{40}
});
assertEquals(correctMatrix, matrix.multiply(vector));
correctMatrix = new LongMatrix(new long[][]{
{2, 4, 6, 8},
{2, 4, 6, 8},
{2, 4, 6, 8},
{2, 4, 6, 8}
});
assertEquals(correctMatrix, matrix.multiply(2));
//10x10
matrix = new LongMatrix(grid10);
transformMatrix = new LongMatrix(transformGrid10_2);
correctMatrix = new LongMatrix(new long[][]{
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605}
});
assertEquals(correctMatrix, matrix.multiply(transformMatrix));
vector = new LongMatrix(new long[][]{
{2},
{3},
{4},
{5},
{6},
{7},
{8},
{9},
{10},
{11}
});
correctMatrix = new LongMatrix(new long[][]{
{440},
{440},
{440},
{440},
{440},
{440},
{440},
{440},
{440},
{440}
});
assertEquals(correctMatrix, matrix.multiply(vector));
correctMatrix = new LongMatrix(new long[][]{
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
});
assertEquals(correctMatrix, matrix.multiply(2));
}
@Test
public void testDotProduct(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix transformMatrix = new LongMatrix(transformGrid1_2);
assertEquals(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);
assertEquals(30, matrix.dotProduct(transformMatrix));
//3x3
matrix = new LongMatrix(grid3);
transformMatrix = new LongMatrix(transformGrid3_2);
assertEquals(162, matrix.dotProduct(transformMatrix));
//4x4
matrix = new LongMatrix(grid4);
transformMatrix = new LongMatrix(transformGrid4_2);
assertEquals(560, matrix.dotProduct(transformMatrix));
//10x10
matrix = new LongMatrix(grid10);
transformMatrix = new LongMatrix(transformGrid10_2);
assertEquals(35750, matrix.dotProduct(transformMatrix));
}
@Test
public void testHadamardProduct(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix transformMatrix = new LongMatrix(transformGrid1_2);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{2}});
assertEquals(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);
correctMatrix = new LongMatrix(new long[][]{
{2, 6},
{2, 6}
});
assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix));
//3x3
matrix = new LongMatrix(grid3);
transformMatrix = new LongMatrix(transformGrid3_2);
correctMatrix = new LongMatrix(new long[][]{
{2, 6, 12},
{2, 6, 12},
{2, 6, 12}
});
assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix));
//4x4
matrix = new LongMatrix(grid4);
transformMatrix = new LongMatrix(transformGrid4_2);
correctMatrix = new LongMatrix(new long[][]{
{2, 6, 12, 20},
{2, 6, 12, 20},
{2, 6, 12, 20},
{2, 6, 12, 20}
});
assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix));
//10x10
matrix = new LongMatrix(grid10);
transformMatrix = new LongMatrix(transformGrid10_2);
correctMatrix = new LongMatrix(new long[][]{
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110}
});
assertEquals(correctMatrix, matrix.hadamardProduct(transformMatrix));
}
@Test
public void testTranspose(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}});
assertEquals(correctMatrix, matrix.transpose());
//2x2
matrix = new LongMatrix(grid2);
correctMatrix = new LongMatrix(new long[][]{
{1, 1},
{2, 2}
});
assertEquals(correctMatrix, matrix.transpose());
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(new long[][]{
{1, 1, 1},
{2, 2, 2},
{3, 3, 3}
});
assertEquals(correctMatrix, matrix.transpose());
//4x4
matrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(new long[][]{
{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}
});
assertEquals(correctMatrix, matrix.transpose());
//10x10
matrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(new long[][]{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
{7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
{8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
});
assertEquals(correctMatrix, matrix.transpose());
}
@Test
public void testDeterminant(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
assertEquals(1, matrix.determinant());
//Invalid determinants
LongMatrix testMatrix = new LongMatrix(new long[][]{{0, 0}});
assertThrows(InvalidGeometryException.class, () -> {
testMatrix.determinant();
});
//2x2
matrix = new LongMatrix(grid2);
assertEquals(0, matrix.determinant());
matrix = new LongMatrix(new long[][]{
{1, 4},
{4, 1}
});
assertEquals(-15, matrix.determinant());
//det
assertEquals(matrix.determinant(), matrix.det());
//3x3
matrix = new LongMatrix(grid3);
assertEquals(0, matrix.determinant());
matrix = new LongMatrix(new long[][]{
{1, 4, 2},
{2, 4, 1},
{4, 1, 2}
});
assertEquals(-21, matrix.determinant());
//4x4
matrix = new LongMatrix(grid4);
assertEquals(0, matrix.determinant());
matrix = new LongMatrix(new long[][]{
{1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}
});
assertEquals(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);
assertEquals(0, matrix.determinant());
matrix = new LongMatrix(new long[][]{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
{2, 3, 4, 5, 6, 7, 8, 9, 10, 1},
{3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
{4, 5, 6, 7, 8, 9, 10, 1, 2, 3},
{5, 6, 7, 8, 9, 10, 1, 2, 3, 4},
{6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
{7, 8, 9, 10, 1, 2, 3, 4, 5, 6},
{8, 9, 10, 1, 2, 3, 4, 5, 6, 7},
{9, 10, 1, 2, 3, 4, 5, 6, 7, 8},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
});
assertEquals(-10000000, matrix.determinant());
}
@Test
public void testCofactor(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(grid1);
assertEquals(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[][]{
{2, -1},
{-2, 1}
});
assertEquals(correctMatrix, matrix.cofactor());
//cof
assertEquals(matrix.cofactor(), matrix.cof());
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(3, 3, 0);
assertEquals(correctMatrix, matrix.cofactor());
matrix = new LongMatrix(new long[][]{
{1, 4, 2},
{2, 4, 1},
{4, 1, 2}
});
correctMatrix = new LongMatrix(new long[][]{
{7, 0, -14},
{-6, -6, 15},
{-4, 3, -4}
});
assertEquals(correctMatrix, matrix.cofactor());
//4x4
matrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(4, 4, 0);
assertEquals(correctMatrix, matrix.cofactor());
matrix = new LongMatrix(new long[][]{
{1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}
});
correctMatrix = new LongMatrix(new long[][]{
{-36, 4, 4, 44},
{4, 4, 44, -36},
{4, 44, -36, 4},
{44, -36, 4, 4}
});
assertEquals(correctMatrix, matrix.cofactor());
//10x10
//?Skipping 10x10 test because test took > 5s by itself
/*
matrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(10, 10, 0);
assertEquals(correctMatrix, matrix.cofactor());
*/
}
@Test
public void testPower(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(new long[][]{{1}});
assertEquals(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[][]{
{9, 18},
{9, 18}
});
assertEquals(correctMatrix, matrix.pow(3));
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(new long[][]{
{36, 72, 108},
{36, 72, 108},
{36, 72, 108}
});
assertEquals(correctMatrix, matrix.pow(3));
//4x4
//0
matrix = new LongMatrix(grid4);
correctMatrix = new LongMatrix(new long[][]{
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1},
{1, 1, 1, 1}
});
assertEquals(correctMatrix, matrix.pow(0));
//1
correctMatrix = new LongMatrix(new long[][]{
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4}
});
assertEquals(correctMatrix, matrix.pow(1));
//3
correctMatrix = new LongMatrix(new long[][]{
{100, 200, 300, 400},
{100, 200, 300, 400},
{100, 200, 300, 400},
{100, 200, 300, 400}
});
assertEquals(correctMatrix, matrix.pow(3));
//10x10
matrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(new long[][]{
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}
});
assertEquals(correctMatrix, matrix.pow(3));
}
@Test
public void testAdjoint(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(grid1);
assertEquals(correctMatrix, matrix.adjoint());
//2x2
matrix = new LongMatrix(grid2);
correctMatrix = new LongMatrix(new long[][]{
{2, -2},
{-1, 1}
});
assertEquals(correctMatrix, matrix.adjoint());
//adj
assertEquals(matrix.adjoint(), matrix.adj());
//3x3
matrix = new LongMatrix(grid3);
correctMatrix = new LongMatrix(3, 3, 0);
assertEquals(correctMatrix, matrix.adjoint());
//4x4
matrix = new LongMatrix(new long[][]{
{1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}
});
correctMatrix = new LongMatrix(new long[][]{
{-36, 4, 4, 44},
{4, 4, 44, -36},
{4, 44, -36, 4},
{44, -36, 4, 4}
});
assertEquals(correctMatrix, matrix.adjoint());
//10x10
//?Skipping 10x10 test because test took > 5s by itself
/*
matrix = new LongMatrix(grid10);
correctMatrix = new LongMatrix(10, 10, 0);
assertEquals(correctMatrix, matrix.adjoint());
*/
}
@Test
public void testInverse(){
//1x1
LongMatrix matrix = new LongMatrix(grid1);
LongMatrix correctMatrix = new LongMatrix(grid1);
assertEquals(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},
{4, 1}
});
correctMatrix = new LongMatrix(new long[][]{
{-0, 0},
{0, -0}
});
assertEquals(correctMatrix, matrix.inverse());
//3x3
matrix = new LongMatrix(new long[][]{
{1, 4, 2},
{2, 4, 1},
{4, 1, 2}
});
correctMatrix = new LongMatrix(new long[][]{
{-0, 0, 0},
{0, 0, -0},
{0, -0, 0}
});
assertEquals(correctMatrix, matrix.inverse());
//4x4
matrix = new LongMatrix(new long[][]{
{1, 2, 3, 4},
{2, 3, 4, 1},
{3, 4, 1, 2},
{4, 1, 2, 3}
});
correctMatrix = new LongMatrix(new long[][]{
{-0, 0, 0, 0},
{0, 0, 0, -0},
{0, 0, -0, 0},
{0, -0, 0, 0}
});
assertEquals(correctMatrix, matrix.inverse());
//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());
}
}