mirror of
https://bitbucket.org/Mattrixwv/matrix.git
synced 2025-12-06 15:03:58 -05:00
1853 lines
58 KiB
Java
1853 lines
58 KiB
Java
//Matrix/src/test/java/com/mattrixwv/matrix/TestIntegerMatrix.java
|
|
//Mattrixwv
|
|
// Created: 02-01-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
|
|
private static final int[][] grid1 = {
|
|
{1}
|
|
};
|
|
private static final int[][] transformGrid1_1 = {
|
|
{1}
|
|
};
|
|
private static final int[][] transformGrid1_2 = {
|
|
{2}
|
|
};
|
|
|
|
//Grid 2x2
|
|
private static final int[][] grid2 = {
|
|
{1, 2},
|
|
{1, 2}
|
|
};
|
|
private static final int[][] transformGrid2_1 = {
|
|
{1, 0},
|
|
{1, 0}
|
|
};
|
|
private static final int[][] transformGrid2_2 = {
|
|
{2, 3},
|
|
{2, 3}
|
|
};
|
|
|
|
//Grid 3x3
|
|
private static final int[][] grid3 = {
|
|
{1, 2, 3},
|
|
{1, 2, 3},
|
|
{1, 2, 3}
|
|
};
|
|
private static final int[][] transformGrid3_1 = {
|
|
{2, 1, 0},
|
|
{2, 1, 0},
|
|
{2, 1, 0}
|
|
};
|
|
private static final int[][] transformGrid3_2 = {
|
|
{2, 3, 4},
|
|
{2, 3, 4},
|
|
{2, 3, 4}
|
|
};
|
|
|
|
//Grid 4x4
|
|
private static final int[][] grid4 = {
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4}
|
|
};
|
|
private static final int[][] transformGrid4_1 = {
|
|
{3, 2, 1, 0},
|
|
{3, 2, 1, 0},
|
|
{3, 2, 1, 0},
|
|
{3, 2, 1, 0}
|
|
};
|
|
private static final int[][] transformGrid4_2 = {
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5}
|
|
};
|
|
|
|
//Grid 10x10
|
|
private static final int[][] grid10 = {
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
};
|
|
private static final int[][] transformGrid10_1 = {
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
|
|
{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
|
|
};
|
|
private static final int[][] transformGrid10_2 = {
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
|
};
|
|
|
|
|
|
@Test
|
|
public void testConstructor(){
|
|
//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(){
|
|
//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")
|
|
boolean gridEquals1 = matrix.equals(grid1);
|
|
assertTrue("IntegerMatrix 1x1 failed equals int[][].", gridEquals1);
|
|
|
|
//2x2
|
|
matrix = new IntegerMatrix(grid2);
|
|
boolean gridEquals2 = matrix.equals(matrix);
|
|
assertTrue("IntegerMatrix 2x2 failed equals IntegerMatrix.", gridEquals2);
|
|
@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);
|
|
boolean gridEquals3 = matrix.equals(matrix);
|
|
assertTrue("IntegerMatrix 3x3 failed equals IntegerMatrix.", gridEquals3);
|
|
@SuppressWarnings("unlikely-arg-type")
|
|
boolean gridEquals31 = matrix.equals(grid3);
|
|
assertTrue("IntegerMatrix 3x3 failed equals int[][].", gridEquals31);
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
boolean gridEquals4 = matrix.equals(matrix);
|
|
assertTrue("IntegerMatrix 4x4 failed equals IntegerMatrix.", gridEquals4);
|
|
@SuppressWarnings("unlikely-arg-type")
|
|
boolean gridEquals41 = matrix.equals(grid4);
|
|
assertTrue("IntegerMatrix 4x4 failed equals int[][].", gridEquals41);
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
boolean gridEquals10 = matrix.equals(matrix);
|
|
assertTrue("IntegerMatrix = 10x10 failed equals IntegerMatrix.", gridEquals10);
|
|
@SuppressWarnings("unlikely-arg-type")
|
|
boolean gridEquals101 = matrix.equals(grid10);
|
|
assertTrue("IntegerMatrix 10x10 failed equals int[][].", gridEquals101);
|
|
}
|
|
|
|
@Test
|
|
public void testGet(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
assertEquals("IntegerMatrix 1x1 failed get.", 1, matrix.get(0, 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));
|
|
|
|
//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));
|
|
|
|
//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}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed getCol.", correctMatrix, matrix.getCol(0));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1},
|
|
{1},
|
|
{1}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed getCol.", correctMatrix, matrix.getCol(0));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed getCol.", correctMatrix, matrix.getCol(0));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1},
|
|
{1}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed getColumn.", correctMatrix, matrix.getCol(0));
|
|
}
|
|
|
|
@Test
|
|
public void testSet(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
matrix.set(0, 0, 2);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}});
|
|
assertEquals("IntegerMatrix 1x1 failed set.", 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
|
|
matrix = new IntegerMatrix(grid2);
|
|
matrix.set(0, 0, 3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3, 2},
|
|
{1, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed set.", correctMatrix, matrix);
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
matrix.set(0, 0, 3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3, 2, 3},
|
|
{1, 2, 3},
|
|
{1, 2, 3},
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed set.", correctMatrix, matrix);
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
matrix.set(0, 0, 3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed set.", correctMatrix, matrix);
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
matrix.set(0, 0, 3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix);
|
|
}
|
|
|
|
@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},
|
|
{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("IntegerMatrix 10x10 failed setRow.", correctMatrix, matrix);
|
|
}
|
|
|
|
@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, 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("IntegerMatrix 10x10 failed setColumn.", correctMatrix, matrix);
|
|
}
|
|
|
|
@Test
|
|
public void testAddRow(){
|
|
//0x0
|
|
IntegerMatrix matrix = new IntegerMatrix();
|
|
matrix.addRow(new int[]{0});
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{0}});
|
|
assertEquals(correctMatrix, matrix);
|
|
|
|
//1x1
|
|
matrix = new IntegerMatrix(grid1);
|
|
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
|
|
matrix = new IntegerMatrix(grid2);
|
|
matrix.addRow(new int[]{1, 2});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 2},
|
|
{1, 2},
|
|
{1, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed addRow.", correctMatrix, matrix);
|
|
//Matrix
|
|
matrix = new IntegerMatrix(grid2);
|
|
matrix.addRow(new IntegerMatrix(new int[][]{{0, 0}}));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 2},
|
|
{1, 2},
|
|
{0, 0}
|
|
});
|
|
assertEquals(correctMatrix, matrix);
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
matrix.addRow(new int[]{1, 2, 3});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3},
|
|
{1, 2, 3},
|
|
{1, 2, 3},
|
|
{1, 2, 3}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed addRow.", correctMatrix, matrix);
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
matrix.addRow(new int[]{1, 2, 3, 4});
|
|
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}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed addRow.", correctMatrix, matrix);
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
matrix.addRow(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
|
|
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},
|
|
{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("IntegerMatrix 10x10 failed addRow.", correctMatrix, matrix);
|
|
}
|
|
|
|
@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[][]{
|
|
{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("IntegerMatrix 10x10 failed addColumn.", correctMatrix, matrix);
|
|
}
|
|
|
|
@Test
|
|
public void testAppendRight(){
|
|
//1x1
|
|
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));
|
|
|
|
//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);
|
|
});
|
|
|
|
//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 appendRight.", correctMatrix, matrix.appendRight(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 appendRight.", correctMatrix, matrix.appendRight(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 appendRight.", correctMatrix, matrix.appendRight(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},
|
|
{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("IntegerMatrix 10x10 failed appendRight.", correctMatrix, matrix.appendRight(secondMatrix));
|
|
}
|
|
|
|
@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},
|
|
{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("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
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_1);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{2}});
|
|
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);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 2},
|
|
{2, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed add IntegerMatrix.", correctMatrix, matrix.add(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 3},
|
|
{2, 3}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed add scalar.", correctMatrix, matrix.add(1));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
transformMatrix = new IntegerMatrix(transformGrid3_1);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3, 3, 3},
|
|
{3, 3, 3},
|
|
{3, 3, 3}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed add IntegerMatrix.", correctMatrix, matrix.add(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 3, 4},
|
|
{2, 3, 4},
|
|
{2, 3, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed add scalar.", correctMatrix, matrix.add(1));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
transformMatrix = new IntegerMatrix(transformGrid4_1);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{4, 4, 4, 4},
|
|
{4, 4, 4, 4},
|
|
{4, 4, 4, 4},
|
|
{4, 4, 4, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed add IntegerMatrix.", correctMatrix, matrix.add(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5},
|
|
{2, 3, 4, 5}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed add scalar.", correctMatrix, matrix.add(1));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
transformMatrix = new IntegerMatrix(transformGrid10_1);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
|
|
});
|
|
assertEquals("IntegerMatrix 5x5 failed add IntegerMatrix.", correctMatrix, matrix.add(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed add IntegerMatrix.", correctMatrix, matrix.add(1));
|
|
}
|
|
|
|
@Test
|
|
public void testSubtraction(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix transformMatrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{
|
|
{0}
|
|
});
|
|
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);
|
|
correctMatrix = new IntegerMatrix(2, 2, 0);
|
|
assertEquals("IntegerMatrix 2x2 failed subtract IntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{0, 1},
|
|
{0, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed subtract scalar.", correctMatrix, matrix.subtract(1));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
transformMatrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(3, 3, 0);
|
|
assertEquals("IntegerMatrix 3x3 failed subtract IntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{0, 1, 2},
|
|
{0, 1, 2},
|
|
{0, 1, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed subtract scalar.", correctMatrix, matrix.subtract(1));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
transformMatrix = new IntegerMatrix(grid4);
|
|
correctMatrix = new IntegerMatrix(4, 4, 0);
|
|
assertEquals("IntegerMatrix 4x4 failed subtract IntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{0, 1, 2, 3},
|
|
{0, 1, 2, 3},
|
|
{0, 1, 2, 3},
|
|
{0, 1, 2, 3}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed subtract scalar.", correctMatrix, matrix.subtract(1));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
transformMatrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(10, 10, 0);
|
|
assertEquals("IntegerMatrix 10x10 failed subtract IntegerMatrix.", correctMatrix, matrix.subtract(transformMatrix));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed subtract scalar.", correctMatrix, matrix.subtract(1));
|
|
}
|
|
|
|
@Test
|
|
public void testMultiplication(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(transformGrid1_2);
|
|
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);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{6, 9},
|
|
{6, 9}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed multiplication IntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix));
|
|
IntegerMatrix vector = new IntegerMatrix(new int[][]{
|
|
{2},
|
|
{3}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{8},
|
|
{8}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed multiplication vector.", correctMatrix, matrix.multiply(vector));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 4},
|
|
{2, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed multiplication scalar.", correctMatrix, matrix.multiply(2));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
transformMatrix = new IntegerMatrix(transformGrid3_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{12, 18, 24},
|
|
{12, 18, 24},
|
|
{12, 18, 24}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed multiplication IntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix));
|
|
vector = new IntegerMatrix(new int[][]{
|
|
{2},
|
|
{3},
|
|
{4}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{20},
|
|
{20},
|
|
{20}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed multiplication vector.", correctMatrix, matrix.multiply(vector));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 4, 6},
|
|
{2, 4, 6},
|
|
{2, 4, 6}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed multiplication scalar.", correctMatrix, matrix.multiply(2));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
transformMatrix = new IntegerMatrix(transformGrid4_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{20, 30, 40, 50},
|
|
{20, 30, 40, 50},
|
|
{20, 30, 40, 50},
|
|
{20, 30, 40, 50},
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed multiplication IntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix));
|
|
vector = new IntegerMatrix(new int[][]{
|
|
{2},
|
|
{3},
|
|
{4},
|
|
{5}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{40},
|
|
{40},
|
|
{40},
|
|
{40}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed multiplication vector.", correctMatrix, matrix.multiply(vector));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 4, 6, 8},
|
|
{2, 4, 6, 8},
|
|
{2, 4, 6, 8},
|
|
{2, 4, 6, 8}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed multiplication scalar.", correctMatrix, matrix.multiply(2));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
transformMatrix = new IntegerMatrix(transformGrid10_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605},
|
|
{110, 165, 220, 275, 330, 385, 440, 495, 550, 605}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed multiplication IntegerMatrix.", correctMatrix, matrix.multiply(transformMatrix));
|
|
vector = new IntegerMatrix(new int[][]{
|
|
{2},
|
|
{3},
|
|
{4},
|
|
{5},
|
|
{6},
|
|
{7},
|
|
{8},
|
|
{9},
|
|
{10},
|
|
{11}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440},
|
|
{440}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed multiplication vector.", correctMatrix, matrix.multiply(vector));
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
|
|
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed multiplication scalar.", correctMatrix, matrix.multiply(2));
|
|
}
|
|
|
|
@Test
|
|
public void testDotProduct(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
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);
|
|
assertEquals("IntegerMatrix 2x2 failed dot product IntegerMatrix.", 30, matrix.dotProduct(transformMatrix));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
transformMatrix = new IntegerMatrix(transformGrid3_2);
|
|
assertEquals("IntegerMatrix 3x3 failed dot product IntegerMatrix.", 162, matrix.dotProduct(transformMatrix));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
transformMatrix = new IntegerMatrix(transformGrid4_2);
|
|
assertEquals("IntegerMatrix 4x4 failed dot product IntegerMatrix.", 560, matrix.dotProduct(transformMatrix));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
transformMatrix = new IntegerMatrix(transformGrid10_2);
|
|
assertEquals("IntegerMatrix 10x10 failed dot product IntegerMatrix.", 35750, matrix.dotProduct(transformMatrix));
|
|
}
|
|
|
|
@Test
|
|
public void testHadamardProduct(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix transformMatrix = new IntegerMatrix(transformGrid1_2);
|
|
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);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 6},
|
|
{2, 6}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
transformMatrix = new IntegerMatrix(transformGrid3_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 6, 12},
|
|
{2, 6, 12},
|
|
{2, 6, 12}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix));
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
transformMatrix = new IntegerMatrix(transformGrid4_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 6, 12, 20},
|
|
{2, 6, 12, 20},
|
|
{2, 6, 12, 20},
|
|
{2, 6, 12, 20}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
transformMatrix = new IntegerMatrix(transformGrid10_2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110},
|
|
{2, 6, 12, 20, 30, 42, 56, 72, 90, 110}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed hadamard product.", correctMatrix, matrix.hadamardProduct(transformMatrix));
|
|
}
|
|
|
|
@Test
|
|
public void testTranspose(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(new int[][]{{1}});
|
|
assertEquals("IntegerMatrix 1x1 failed transpose.", correctMatrix, matrix.transpose());
|
|
|
|
//2x2
|
|
matrix = new IntegerMatrix(grid2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 1},
|
|
{2, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed transpose.", correctMatrix, matrix.transpose());
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 1, 1},
|
|
{2, 2, 2},
|
|
{3, 3, 3}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed transpose.", correctMatrix, matrix.transpose());
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 1, 1, 1},
|
|
{2, 2, 2, 2},
|
|
{3, 3, 3, 3},
|
|
{4, 4, 4, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed transpose.", correctMatrix, matrix.transpose());
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
|
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
|
|
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
|
|
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
|
|
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
|
|
{6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
|
|
{7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
|
|
{8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
|
|
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
|
|
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed transpose.", correctMatrix, matrix.transpose());
|
|
}
|
|
|
|
@Test
|
|
public void testDeterminant(){
|
|
//1x1
|
|
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());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 4},
|
|
{4, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed determinant2.", -15, matrix.determinant());
|
|
//det
|
|
assertEquals(matrix.determinant(), matrix.det());
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
assertEquals("IntegerMatrix 3x3 failed determinant1.", 0, matrix.determinant());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 4, 2},
|
|
{2, 4, 1},
|
|
{4, 1, 2}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed determinant2.", -21, matrix.determinant());
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
assertEquals("IntegerMatrix 4x4 failed determiant1.", 0, matrix.determinant());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4},
|
|
{2, 3, 4, 1},
|
|
{3, 4, 1, 2},
|
|
{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);
|
|
assertEquals("IntegerMatrix 10x10 failed determinant1.", 0, matrix.determinant());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
{2, 3, 4, 5, 6, 7, 8, 9, 10, 1},
|
|
{3, 4, 5, 6, 7, 8, 9, 10, 1, 2},
|
|
{4, 5, 6, 7, 8, 9, 10, 1, 2, 3},
|
|
{5, 6, 7, 8, 9, 10, 1, 2, 3, 4},
|
|
{6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
|
|
{7, 8, 9, 10, 1, 2, 3, 4, 5, 6},
|
|
{8, 9, 10, 1, 2, 3, 4, 5, 6, 7},
|
|
{9, 10, 1, 2, 3, 4, 5, 6, 7, 8},
|
|
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed determinant2.", -10000000, matrix.determinant());
|
|
}
|
|
|
|
@Test
|
|
public void testCofactor(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
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[][]{
|
|
{2, -1},
|
|
{-2, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed cofactor.", correctMatrix, matrix.cofactor());
|
|
//cof
|
|
assertEquals(matrix.cofactor(), matrix.cof());
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(3, 3, 0);
|
|
assertEquals("IntegerMatrix 3x3 failed cofactor1.", correctMatrix, matrix.cofactor());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 4, 2},
|
|
{2, 4, 1},
|
|
{4, 1, 2}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{7, 0, -14},
|
|
{-6, -6, 15},
|
|
{-4, 3, -4}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed cofactor2.", correctMatrix, matrix.cofactor());
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(grid4);
|
|
correctMatrix = new IntegerMatrix(4, 4, 0);
|
|
assertEquals("IntegerMatrix 4x4 failed cofactor1.", correctMatrix, matrix.cofactor());
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4},
|
|
{2, 3, 4, 1},
|
|
{3, 4, 1, 2},
|
|
{4, 1, 2, 3}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{-36, 4, 4, 44},
|
|
{4, 4, 44, -36},
|
|
{4, 44, -36, 4},
|
|
{44, -36, 4, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed cofactor2.", correctMatrix, matrix.cofactor());
|
|
|
|
//10x10
|
|
//?Skipping 10x10 test because test took > 5s by itself
|
|
/*
|
|
matrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(10, 10, 0);
|
|
assertEquals("IntegerMatrix 10x10 failed cofactor1.", correctMatrix, matrix.cofactor());
|
|
*/
|
|
}
|
|
|
|
@Test
|
|
public void testPower(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
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[][]{
|
|
{9, 18},
|
|
{9, 18}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed power.", correctMatrix, matrix.pow(3));
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{36, 72, 108},
|
|
{36, 72, 108},
|
|
{36, 72, 108}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed power.", correctMatrix, matrix.pow(3));
|
|
|
|
//4x4
|
|
//0
|
|
matrix = new IntegerMatrix(grid4);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 1, 1, 1},
|
|
{1, 1, 1, 1},
|
|
{1, 1, 1, 1},
|
|
{1, 1, 1, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed power 0.", correctMatrix, matrix.pow(0));
|
|
//1
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4},
|
|
{1, 2, 3, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed power 1.", correctMatrix, matrix.pow(1));
|
|
//3
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{100, 200, 300, 400},
|
|
{100, 200, 300, 400},
|
|
{100, 200, 300, 400},
|
|
{100, 200, 300, 400}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed power 3.", correctMatrix, matrix.pow(3));
|
|
|
|
//10x10
|
|
matrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250},
|
|
{3025, 6050, 9075, 12100, 15125, 18150, 21175, 24200, 27225, 30250}
|
|
});
|
|
assertEquals("IntegerMatrix 10x10 failed power.", correctMatrix, matrix.pow(3));
|
|
}
|
|
|
|
@Test
|
|
public void testAdjoint(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
IntegerMatrix correctMatrix = new IntegerMatrix(grid1);
|
|
assertEquals("IntegerMatrix 1x1 failed adjoint.", correctMatrix, matrix.adjoint());
|
|
|
|
//2x2
|
|
matrix = new IntegerMatrix(grid2);
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{2, -2},
|
|
{-1, 1}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed adjoint.", correctMatrix, matrix.adjoint());
|
|
//adj
|
|
assertEquals(matrix.adjoint(), matrix.adj());
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(grid3);
|
|
correctMatrix = new IntegerMatrix(3, 3, 0);
|
|
assertEquals("IntegerMatrix 3x3 failed adjoint.", correctMatrix, matrix.adjoint());
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4},
|
|
{2, 3, 4, 1},
|
|
{3, 4, 1, 2},
|
|
{4, 1, 2, 3}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{-36, 4, 4, 44},
|
|
{4, 4, 44, -36},
|
|
{4, 44, -36, 4},
|
|
{44, -36, 4, 4}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed adjoint.", correctMatrix, matrix.adjoint());
|
|
|
|
//10x10
|
|
//?Skipping 10x10 test because test took > 5s by itself
|
|
/*
|
|
matrix = new IntegerMatrix(grid10);
|
|
correctMatrix = new IntegerMatrix(10, 10, 0);
|
|
assertEquals("IntegerMatrix 10x10 failed adjoint.", correctMatrix, matrix.adjoint());
|
|
*/
|
|
}
|
|
|
|
@Test
|
|
public void testInverse(){
|
|
//1x1
|
|
IntegerMatrix matrix = new IntegerMatrix(grid1);
|
|
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},
|
|
{4, 1}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{-0, 0},
|
|
{0, -0}
|
|
});
|
|
assertEquals("IntegerMatrix 2x2 failed inverse.", correctMatrix, matrix.inverse());
|
|
|
|
//3x3
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 4, 2},
|
|
{2, 4, 1},
|
|
{4, 1, 2}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{-0, 0, 0},
|
|
{0, 0, -0},
|
|
{0, -0, 0}
|
|
});
|
|
assertEquals("IntegerMatrix 3x3 failed inverse.", correctMatrix, matrix.inverse());
|
|
|
|
//4x4
|
|
matrix = new IntegerMatrix(new int[][]{
|
|
{1, 2, 3, 4},
|
|
{2, 3, 4, 1},
|
|
{3, 4, 1, 2},
|
|
{4, 1, 2, 3}
|
|
});
|
|
correctMatrix = new IntegerMatrix(new int[][]{
|
|
{-0, 0, 0, 0},
|
|
{0, 0, 0, -0},
|
|
{0, 0, -0, 0},
|
|
{0, -0, 0, 0}
|
|
});
|
|
assertEquals("IntegerMatrix 4x4 failed inverse.", correctMatrix, matrix.inverse());
|
|
|
|
//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());
|
|
}
|
|
}
|