Skip to content

Commit

Permalink
Add asserts into Matrix. Add more tests for Matrix & Interval classes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Oct 24, 2024
1 parent 3c5f116 commit db213dc
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/main/java/by/andd3dfx/math/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public double x(int i) {
return get(0, i);
}

public void setX(int index, double value) {
set(0, index, value);
public void setX(int i, double value) {
set(0, i, value);
}

public void set(int index, double value) {
data[index] = value;
public void set(int i, double value) {
assert (i >= 0 && i < data.length);
data[i] = value;
}

public double get(int i) {
Expand All @@ -50,15 +51,17 @@ public double y(int i) {
return get(1, i);
}

public void setY(int index, double value) {
set(1, index, value);
public void setY(int i, double value) {
set(1, i, value);
}

public void set(int i, int j, double value) {
assert (i >= 0 && i < m && j >= 0 && j < n);
data[i * n + j] = value;
}

public void set(int i, double[] arr) {
assert (i >= 0 && i < m && n == arr.length);
System.arraycopy(arr, 0, data, i * n, arr.length);
}

Expand All @@ -81,17 +84,16 @@ public Matrix fill(double d) {
}

public void swapLines(int m1, int m2) {
assert (m1 < m && m2 < m && m1 >= 0 && m2 >= 0);
assert (0 <= m1 && m1 < m && 0 <= m2 && m2 < m && m1 != m2);

for (int i = 0; i < n; i++) {
var tmp = data[m1 * n + i];
data[m1 * n + i] = data[m2 * n + i];
data[m2 * n + i] = tmp;
}
var buff = new double[n];
System.arraycopy(data, m1 * n, buff, 0, n);
System.arraycopy(data, m2 * n, data, m1 * n, n);
System.arraycopy(buff, 0, data, m2 * n, n);
}

public void swapCols(int n1, int n2) {
assert (n1 < n && n2 < n && n1 >= 0 && n2 >= 0);
assert (0 <= n1 && n1 < n && 0 <= n2 && n2 < n && n1 != n2);

for (int i = 0; i < n; i++) {
var tmp = data[i * n + n1];
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/by/andd3dfx/math/IntervalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class IntervalTest {

Expand Down Expand Up @@ -69,6 +70,14 @@ void x() {
assertThat(interval.x(5)).isEqualTo(12.0);
}

@Test
void xWithWrongParams() {
var interval = new Interval(2.0, 12.0, 20);

assertThrows(AssertionError.class, () -> interval.x(-1));
assertThrows(AssertionError.class, () -> interval.x(21));
}

@Test
void i() {
var interval = new Interval(2.0, 12.0, 5);
Expand All @@ -79,4 +88,12 @@ void i() {
assertThat(interval.i(11.9)).isEqualTo(4);
assertThat(interval.i(12.0)).isEqualTo(4);
}

@Test
void iWithWrongParams() {
var interval = new Interval(2.0, 12.0, 20);

assertThrows(AssertionError.class, () -> interval.i(1.9));
assertThrows(AssertionError.class, () -> interval.i(12.3));
}
}
85 changes: 84 additions & 1 deletion src/test/java/by/andd3dfx/math/MatrixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class MatrixTest {

Expand Down Expand Up @@ -30,6 +32,18 @@ void constructorWithTwoParams() {
assertThat(m.getN()).isEqualTo(7);
}

@Test
void constructorWithInvalidParams() {
assertThrows(AssertionError.class, () -> new Matrix(0));
assertThrows(AssertionError.class, () -> new Matrix(-1));
assertThrows(AssertionError.class, () -> new Matrix(0, 0));
assertThrows(AssertionError.class, () -> new Matrix(0, 5));
assertThrows(AssertionError.class, () -> new Matrix(-1, 5));
assertThrows(AssertionError.class, () -> new Matrix(5, 0));
assertThrows(AssertionError.class, () -> new Matrix(5, -1));
assertThrows(AssertionError.class, () -> new Matrix(-1, -1));
}

@Test
void setNGet() {
var m = new Matrix(3);
Expand All @@ -43,6 +57,38 @@ void setNGet() {
assertThat(m.get(0)).isEqualTo(7);
}

@Test
void getWithInvalidParams() {
var m = new Matrix(2, 3);

assertThrows(AssertionError.class, () -> m.get(-1));
assertThrows(AssertionError.class, () -> m.get(2 * 3));
assertThrows(AssertionError.class, () -> m.get(-1, -1));
assertThrows(AssertionError.class, () -> m.get(-1, 0));
assertThrows(AssertionError.class, () -> m.get(-1, 3));
assertThrows(AssertionError.class, () -> m.get(0, -1));
assertThrows(AssertionError.class, () -> m.get(0, 3));
assertThrows(AssertionError.class, () -> m.get(2, -1));
assertThrows(AssertionError.class, () -> m.get(2, 0));
assertThrows(AssertionError.class, () -> m.get(2, 3));
}

@Test
void setWithInvalidParams() {
var m = new Matrix(2, 3);

assertThrows(AssertionError.class, () -> m.set(-1, 21));
assertThrows(AssertionError.class, () -> m.set(2 * 3, 21));
assertThrows(AssertionError.class, () -> m.set(-1, -1, 21));
assertThrows(AssertionError.class, () -> m.set(-1, 0, 21));
assertThrows(AssertionError.class, () -> m.set(-1, 3, 21));
assertThrows(AssertionError.class, () -> m.set(0, -1, 21));
assertThrows(AssertionError.class, () -> m.set(0, 3, 21));
assertThrows(AssertionError.class, () -> m.set(2, -1, 21));
assertThrows(AssertionError.class, () -> m.set(2, 0, 21));
assertThrows(AssertionError.class, () -> m.set(2, 3, 21));
}

@Test
void min() {
var m = new Matrix(2, 2);
Expand Down Expand Up @@ -91,6 +137,18 @@ void swapLines() {
assertThat(m.get(2, 2)).isEqualTo(7);
}

@Test
void swapLinesWithInvalidParams() {
var m = new Matrix(2, 3);

assertThrows(AssertionError.class, () -> m.swapLines(0, -1));
assertThrows(AssertionError.class, () -> m.swapLines(0, 2));
assertThrows(AssertionError.class, () -> m.swapLines(-1, -2));
assertThrows(AssertionError.class, () -> m.swapLines(-1, 0));
assertThrows(AssertionError.class, () -> m.swapLines(-1, 2));
assertThrows(AssertionError.class, () -> m.swapLines(1, 1));
}

@Test
void swapCols() {
var m = new Matrix(3, 3);
Expand All @@ -117,6 +175,18 @@ void swapCols() {
assertThat(m.get(2, 2)).isEqualTo(0);
}

@Test
void swapColsWithInvalidParams() {
var m = new Matrix(3, 2);

assertThrows(AssertionError.class, () -> m.swapCols(0, -1));
assertThrows(AssertionError.class, () -> m.swapCols(0, 2));
assertThrows(AssertionError.class, () -> m.swapCols(-1, -2));
assertThrows(AssertionError.class, () -> m.swapCols(-1, 0));
assertThrows(AssertionError.class, () -> m.swapCols(-1, 2));
assertThrows(AssertionError.class, () -> m.swapCols(1, 1));
}

@Test
void setXSetY() {
var m = new Matrix(2, 4);
Expand Down Expand Up @@ -160,7 +230,7 @@ void fill() {
}

@Test
void copyArrayIntoDefinitePositionOfData() {
void setArrayIntoDefinitePositionOfDataArray() {
var m = new Matrix(2, 4);
m.fill(3);
m.set(0, 0, 5);
Expand All @@ -179,4 +249,17 @@ void copyArrayIntoDefinitePositionOfData() {
assertThat(m.get(1, 2)).isEqualTo(7);
assertThat(m.get(1, 3)).isEqualTo(6);
}

@Test
void setArrayWithWrongParams() {
var m = new Matrix(2, 3);

assertThrows(AssertionError.class, () -> m.set(-1, new double[7])); // wrong index (too small), wrong array size
assertThrows(AssertionError.class, () -> m.set(-1, new double[3])); // wrong index (too small)
assertThrows(AssertionError.class, () -> m.set(0, new double[7])); // wrong array size
assertDoesNotThrow(() -> m.set(0, new double[3]));
assertDoesNotThrow(() -> m.set(1, new double[3]));
assertThrows(AssertionError.class, () -> m.set(2, new double[3])); // wrong index (too big)
assertThrows(AssertionError.class, () -> m.set(2, new double[2])); // wrong index (too big), wrong array size
}
}

0 comments on commit db213dc

Please sign in to comment.