Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #105. test: Added branch coverage for Matrix.multiply, for diff… #107

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 250 additions & 2 deletions test/com/jwetherell/algorithms/data_structures/test/MatrixTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,166 @@ public void testMatrix() {
Matrix<Integer> matrix9 = matrix7.multiply(matrix8);
assertTrue("Matrix multiplication error. matrix9="+matrix9+" result4"+result4, matrix9.equals(result4));
}

// Requirement: Matrix addition, subtraction, and multiplication should work correctly with Long values.
@Test
public void testMatrixLongOperations() {
Matrix<Long> matrix1 = new Matrix<Long>(4, 3);
matrix1.set(0, 0, Long.valueOf(14));
matrix1.set(0, 1, Long.valueOf(9));
matrix1.set(0, 2, Long.valueOf(3));
matrix1.set(1, 0, Long.valueOf(2));
matrix1.set(1, 1, Long.valueOf(11));
matrix1.set(1, 2, Long.valueOf(15));
matrix1.set(2, 0, Long.valueOf(0));
matrix1.set(2, 1, Long.valueOf(12));
matrix1.set(2, 2, Long.valueOf(17));
matrix1.set(3, 0, Long.valueOf(5));
matrix1.set(3, 1, Long.valueOf(2));
matrix1.set(3, 2, Long.valueOf(3));

Matrix<Long> matrix2 = new Matrix<Long>(3, 2);
matrix2.set(0, 0, Long.valueOf(12));
matrix2.set(0, 1, Long.valueOf(25));
matrix2.set(1, 0, Long.valueOf(9));
matrix2.set(1, 1, Long.valueOf(10));
matrix2.set(2, 0, Long.valueOf(8));
matrix2.set(2, 1, Long.valueOf(5));

// Result of multiplication
Long[][] array1 = new Long[][]{{Long.valueOf(273),Long.valueOf(455)},
{Long.valueOf(243),Long.valueOf(235)},
{Long.valueOf(244),Long.valueOf(205)},
{Long.valueOf(102),Long.valueOf(160)}};
Matrix<Long> result1 = new Matrix<Long>(4,2,array1);

Matrix<Long> matrix3 = matrix1.multiply(matrix2);
assertTrue("Matrix multiplication error. matrix3="+matrix3+" result1"+result1, matrix3.equals(result1));

int rows = 2;
int cols = 2;
int counter = 0;
Matrix<Long> matrix4 = new Matrix<Long>(rows, cols);
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
matrix4.set(r, c, Long.valueOf(counter++));

// Result of subtraction
Long[][] array2 = new Long[][]{{Long.valueOf(0),Long.valueOf(0)},
{Long.valueOf(0),Long.valueOf(0)}};
Matrix<Long> result2 = new Matrix<Long>(2,2,array2);

Matrix<Long> matrix5 = matrix4.subtract(matrix4);
assertTrue("Matrix subtraction error. matrix5="+matrix5+" result2"+result2, matrix5.equals(result2));

// Result of addition
Long[][] array3 = new Long[][]{{Long.valueOf(0),Long.valueOf(2)},
{Long.valueOf(4),Long.valueOf(6)}};
Matrix<Long> result3 = new Matrix<Long>(2,2,array3);

Matrix<Long> matrix6 = matrix4.add(matrix4);
assertTrue("Matrix addition error. matrix6="+matrix6+" result3"+result3, matrix6.equals(result3));

Matrix<Long> matrix7 = new Matrix<Long>(2, 2);
matrix7.set(0, 0, Long.valueOf(1));
matrix7.set(0, 1, Long.valueOf(2));
matrix7.set(1, 0, Long.valueOf(3));
matrix7.set(1, 1, Long.valueOf(4));

Matrix<Long> matrix8 = new Matrix<Long>(2, 2);
matrix8.set(0, 0, Long.valueOf(1));
matrix8.set(0, 1, Long.valueOf(2));
matrix8.set(1, 0, Long.valueOf(3));
matrix8.set(1, 1, Long.valueOf(4));

// Result of multiplication
Long[][] array4 = new Long[][]{{Long.valueOf(7),Long.valueOf(10)},
{Long.valueOf(15),Long.valueOf(22)}};
Matrix<Long> result4 = new Matrix<Long>(2,2,array4);

Matrix<Long> matrix9 = matrix7.multiply(matrix8);
assertTrue("Matrix multiplication error. matrix9="+matrix9+" result4"+result4, matrix9.equals(result4));
}

// Requirement: Matrix addition, subtraction, and multiplication should work correctly with BigInteger values.
@Test
public void testMatrixBigIntegerOperations() {
Matrix<BigInteger> matrix1 = new Matrix<BigInteger>(4, 3);
matrix1.set(0, 0, BigInteger.valueOf(14));
matrix1.set(0, 1, BigInteger.valueOf(9));
matrix1.set(0, 2, BigInteger.valueOf(3));
matrix1.set(1, 0, BigInteger.valueOf(2));
matrix1.set(1, 1, BigInteger.valueOf(11));
matrix1.set(1, 2, BigInteger.valueOf(15));
matrix1.set(2, 0, BigInteger.valueOf(0));
matrix1.set(2, 1, BigInteger.valueOf(12));
matrix1.set(2, 2, BigInteger.valueOf(17));
matrix1.set(3, 0, BigInteger.valueOf(5));
matrix1.set(3, 1, BigInteger.valueOf(2));
matrix1.set(3, 2, BigInteger.valueOf(3));

Matrix<BigInteger> matrix2 = new Matrix<BigInteger>(3, 2);
matrix2.set(0, 0, BigInteger.valueOf(12));
matrix2.set(0, 1, BigInteger.valueOf(25));
matrix2.set(1, 0, BigInteger.valueOf(9));
matrix2.set(1, 1, BigInteger.valueOf(10));
matrix2.set(2, 0, BigInteger.valueOf(8));
matrix2.set(2, 1, BigInteger.valueOf(5));

// Result of multiplication
BigInteger[][] array1 = new BigInteger[][]{{BigInteger.valueOf(273),BigInteger.valueOf(455)},
{BigInteger.valueOf(243),BigInteger.valueOf(235)},
{BigInteger.valueOf(244),BigInteger.valueOf(205)},
{BigInteger.valueOf(102),BigInteger.valueOf(160)}};
Matrix<BigInteger> result1 = new Matrix<BigInteger>(4,2,array1);

Matrix<BigInteger> matrix3 = matrix1.multiply(matrix2);
assertTrue("Matrix multiplication error. matrix3="+matrix3+" result1"+result1, matrix3.equals(result1));

int rows = 2;
int cols = 2;
int counter = 0;
Matrix<BigInteger> matrix4 = new Matrix<BigInteger>(rows, cols);
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
matrix4.set(r, c, BigInteger.valueOf(counter++));

// Result of subtraction
BigInteger[][] array2 = new BigInteger[][]{{BigInteger.valueOf(0),BigInteger.valueOf(0)},
{BigInteger.valueOf(0),BigInteger.valueOf(0)}};
Matrix<BigInteger> result2 = new Matrix<BigInteger>(2,2,array2);

Matrix<BigInteger> matrix5 = matrix4.subtract(matrix4);
assertTrue("Matrix subtraction error. matrix5="+matrix5+" result2"+result2, matrix5.equals(result2));

// Result of addition
BigInteger[][] array3 = new BigInteger[][]{{BigInteger.valueOf(0),BigInteger.valueOf(2)},
{BigInteger.valueOf(4),BigInteger.valueOf(6)}};
Matrix<BigInteger> result3 = new Matrix<BigInteger>(2,2,array3);

Matrix<BigInteger> matrix6 = matrix4.add(matrix4);
assertTrue("Matrix addition error. matrix6="+matrix6+" result3"+result3, matrix6.equals(result3));

Matrix<BigInteger> matrix7 = new Matrix<BigInteger>(2, 2);
matrix7.set(0, 0, BigInteger.valueOf(1));
matrix7.set(0, 1, BigInteger.valueOf(2));
matrix7.set(1, 0, BigInteger.valueOf(3));
matrix7.set(1, 1, BigInteger.valueOf(4));

Matrix<BigInteger> matrix8 = new Matrix<BigInteger>(2, 2);
matrix8.set(0, 0, BigInteger.valueOf(1));
matrix8.set(0, 1, BigInteger.valueOf(2));
matrix8.set(1, 0, BigInteger.valueOf(3));
matrix8.set(1, 1, BigInteger.valueOf(4));

// Result of multiplication
BigInteger[][] array4 = new BigInteger[][]{{BigInteger.valueOf(7),BigInteger.valueOf(10)},
{BigInteger.valueOf(15),BigInteger.valueOf(22)}};
Matrix<BigInteger> result4 = new Matrix<BigInteger>(2,2,array4);

Matrix<BigInteger> matrix9 = matrix7.multiply(matrix8);
assertTrue("Matrix multiplication error. matrix9="+matrix9+" result4"+result4, matrix9.equals(result4));
}

@Test
public void testIdentityMethod1() {
Expand All @@ -117,6 +277,7 @@ public void testIdentityMethod1() {
assertArrayEquals(expectedResult.getRow(1), matrix.getRow(1));
}

// Requirement: Matrix addition should work correctly with Long values.
@Test
public void testAddLong() {
Matrix<Long> matrix = new Matrix<Long>(2, 2);
Expand All @@ -137,6 +298,7 @@ public void testAddLong() {
assertArrayEquals(expectedResult.getRow(1), actualResult.getRow(1));
}

// Requirement: Matrix addition should work correctly with BigInteger values.
@Test
public void testAddBigInteger() {
Matrix<BigInteger> matrix = new Matrix<BigInteger>(2, 2);
Expand All @@ -157,6 +319,7 @@ public void testAddBigInteger() {
assertArrayEquals(expectedResult.getRow(1), actualResult.getRow(1));
}

// Requirement: Matrix addition should work correctly with Float values.
@Test
public void testAddFloat() {
Matrix<Float> matrix = new Matrix<Float>(1, 1);
Expand All @@ -178,6 +341,7 @@ public void testAddFloat() {
assertTrue(Float.compare(actual.get(0, 0), wrong2.get(0, 0)) != 0);
}

// Requirement: Matrix addition should work correctly with Double values.
@Test
public void testAddDouble() {
Matrix<Double> matrix = new Matrix<Double>(1, 1);
Expand All @@ -199,6 +363,7 @@ public void testAddDouble() {
assertTrue(Double.compare(actual.get(0, 0), wrong2.get(0, 0)) != 0);
}

// Requirement: Matrix addition should work correctly with BigDecimal values.
@Test
public void testAddBigDecimal() {
Matrix<BigDecimal> matrix = new Matrix<BigDecimal>(1, 1);
Expand All @@ -215,7 +380,8 @@ public void testAddBigDecimal() {
Matrix<BigDecimal> wrong2 = new Matrix<BigDecimal>(1, 1);
wrong2.set(0, 0, new BigDecimal(2.3));

assertTrue(actual.get(0, 0).compareTo(expected.get(0, 0)) == 0);
BigDecimal diff = actual.get(0, 0).subtract(expected.get(0, 0)).abs();
assertTrue((diff.compareTo(new BigDecimal(0.00001)) < 0));
assertTrue(actual.get(0, 0).compareTo(wrong1.get(0, 0)) != 0);
assertTrue(actual.get(0, 0).compareTo(wrong2.get(0, 0)) != 0);
}
Expand Down Expand Up @@ -349,4 +515,86 @@ public void testSubtractBigDecimal() {
assertTrue(actual.get(0, 0).compareTo(wrong1.get(0, 0)) != 0);
assertTrue(actual.get(0, 0).compareTo(wrong2.get(0, 0)) != 0);
}
}

// Requirement: Matrix multiplication should work correctly with Long values.
@Test
public void testMultiplyLong() {
Matrix<Long> matrix = new Matrix<Long>(1, 1);
matrix.set(0, 0, new Long(2));

Matrix<Long> actual = matrix.multiply(matrix);

Matrix<Long> expected = new Matrix<Long>(1, 1);
expected.set(0, 0, new Long(4));

Matrix<Long> wrong = new Matrix<Long>(1, 1);
wrong.set(0, 0, new Long(3));

assertTrue(Long.compare(actual.get(0, 0), expected.get(0, 0)) == 0);
assertTrue(Long.compare(actual.get(0, 0), wrong.get(0, 0)) != 0);
}

// Requirement: Matrix multiplication should work correctly with BigInteger values.
@Test
public void testMultiplyBigInteger() {
Matrix<BigInteger> matrix = new Matrix<BigInteger>(1, 1);
matrix.set(0, 0, BigInteger.valueOf(2));

Matrix<BigInteger> actual = matrix.multiply(matrix);

Matrix<BigInteger> expected = new Matrix<BigInteger>(1, 1);
expected.set(0, 0, BigInteger.valueOf(4));

Matrix<BigInteger> wrong = new Matrix<BigInteger>(1, 1);
wrong.set(0, 0, BigInteger.valueOf(3));

assertTrue(actual.get(0, 0).compareTo(expected.get(0, 0)) == 0);
assertTrue(actual.get(0, 0).compareTo(wrong.get(0, 0)) != 0);
}

// Requirement: Matrix multiplication should work correctly with Float values
@Test
public void testMultiplyFloat() {
Matrix<Float> matrix = new Matrix<Float>(1, 1);
matrix.set(0, 0, new Float(1.1));

Matrix<Float> actual = matrix.multiply(matrix);

Matrix<Float> expected = new Matrix<Float>(1, 1);
expected.set(0, 0, new Float(1.21));

Matrix<Float> wrong1 = new Matrix<Float>(1, 1);
wrong1.set(0, 0, new Float(1.2));

Matrix<Float> wrong2 = new Matrix<Float>(1, 1);
wrong2.set(0, 0, new Float(1.22));

assertTrue(Math.abs(actual.get(0, 0) - expected.get(0, 0)) < 0.00001);
assertTrue(Float.compare(actual.get(0, 0), wrong1.get(0, 0)) != 0);
assertTrue(Float.compare(actual.get(0, 0), wrong2.get(0, 0)) != 0);
}

// Requirement: Matrix multiplication should work correctly with BigDecimal values
@Test
public void testMultiplyBigDecimal() {
Matrix<BigDecimal> matrix = new Matrix<BigDecimal>(1, 1);
matrix.set(0, 0, new BigDecimal(1.1));

Matrix<BigDecimal> actual = matrix.multiply(matrix);

Matrix<BigDecimal> expected = new Matrix<BigDecimal>(1, 1);
expected.set(0, 0, new BigDecimal(1.21));

Matrix<BigDecimal> wrong1 = new Matrix<BigDecimal>(1, 1);
wrong1.set(0, 0, new BigDecimal(1.2));

Matrix<BigDecimal> wrong2 = new Matrix<BigDecimal>(1, 1);
wrong2.set(0, 0, new BigDecimal(1.22));

BigDecimal diff = actual.get(0, 0).subtract(expected.get(0, 0)).abs();
assertTrue((diff.compareTo(new BigDecimal(0.00001)) < 0));
assertTrue(actual.get(0, 0).compareTo(wrong1.get(0, 0)) != 0);
assertTrue(actual.get(0, 0).compareTo(wrong2.get(0, 0)) != 0);
}

}