Skip to content
Snippets Groups Projects
Commit 8d6975c7 authored by Edvard Berdal Eek's avatar Edvard Berdal Eek
Browse files

Improve code coverage

parent 166dc363
No related branches found
No related tags found
1 merge request!20Created test classes for Factory and FileReader. Created equals methods
Pipeline #288113 passed
......@@ -90,6 +90,9 @@ public class ChaosGameDescription {
* @param transforms List of transformations to apply to the points
*/
private void validateTransforms(List<Transform2D> transforms) {
if (transforms == null){
throw new IllegalArgumentException("Transformations cannot be null");
}
if (transforms.size() > 4 || transforms.isEmpty()) {
throw new IllegalArgumentException("Number of transformations must be between 1 and 4");
}
......@@ -112,6 +115,7 @@ public class ChaosGameDescription {
}
public void setTransforms(List<Transform2D> transforms) {
validateTransforms(transforms);
this.transforms = transforms;
}
......@@ -128,7 +132,10 @@ public class ChaosGameDescription {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChaosGameDescription that = (ChaosGameDescription) o;
return Objects.equals(minCoords, that.minCoords) && Objects.equals(maxCoords, that.maxCoords) && Objects.equals(transforms, that.transforms) && Objects.equals(probabilities, that.probabilities);
return Objects.equals(minCoords, that.minCoords)
&& Objects.equals(maxCoords, that.maxCoords)
&& Objects.equals(transforms, that.transforms)
&& Objects.equals(probabilities, that.probabilities);
}
@Override
......
......@@ -66,27 +66,73 @@ class ChaosGameDescriptionTest {
assertEquals(probabilities, chaosGameDescription.getProbabilities());
}
@Test
void testSetTransforms() {
Matrix2x2 newMatrix = new Matrix2x2(9.9, 9.9, 9.9, 9.9);
Vector2D newVector = new Vector2D(9.9, 9.9);
List<Transform2D> newTransforms = Arrays.asList(new AffineTransform2D(newMatrix, newVector));
chaosGameDescription.setTransforms(newTransforms);
assertEquals(newTransforms, chaosGameDescription.getTransforms());
@Nested
@DisplayName("SetTransforms")
class SetTransforms {
@Test
@DisplayName("Set null transforms")
void testSetNullTransforms() {
assertThrows(IllegalArgumentException.class, () -> chaosGameDescription.setTransforms(null));
}
@Test
@DisplayName("Set empty transforms")
void testSetEmptyTransforms() {
assertThrows(IllegalArgumentException.class, () -> chaosGameDescription.setTransforms(List.of()));
}
@Test
@DisplayName("Set too many transforms")
void testSetTooManyTransforms() {
List<Transform2D> tooManyTransforms = Arrays.asList(
new AffineTransform2D(matrixA, vectorPartA),
new AffineTransform2D(matrixB, vectorPartB),
new AffineTransform2D(matrixA, vectorPartA),
new AffineTransform2D(matrixB, vectorPartB),
new AffineTransform2D(matrixA, vectorPartA)
);
assertThrows(IllegalArgumentException.class, () -> chaosGameDescription.setTransforms(tooManyTransforms));
}
@Test
@DisplayName("Set valid transforms")
void testSetValidTransforms() {
Matrix2x2 newMatrix = new Matrix2x2(9.9, 9.9, 9.9, 9.9);
Vector2D newVector = new Vector2D(9.9, 9.9);
List<Transform2D> newTransforms = Arrays.asList(new AffineTransform2D(newMatrix, newVector));
chaosGameDescription.setTransforms(newTransforms);
assertEquals(newTransforms, chaosGameDescription.getTransforms());
}
}
@Test
void testSetMinCoords() {
Vector2D newMinCoords = new Vector2D(-1, -1);
chaosGameDescription.setMinCoords(newMinCoords);
assertEquals(newMinCoords, chaosGameDescription.getMinCoords());
}
@Test
void testSetMaxCoords() {
Vector2D newMaxCoords = new Vector2D(2, 2);
chaosGameDescription.setMaxCoords(newMaxCoords);
assertEquals(newMaxCoords, chaosGameDescription.getMaxCoords());
@Nested
@DisplayName("Set coordinates")
class SetCoordinates {
@Test
@DisplayName("Set min coordinates")
void testSetMinCoords() {
Vector2D newMinCoords = new Vector2D(-1, -1);
chaosGameDescription.setMinCoords(newMinCoords);
assertEquals(newMinCoords, chaosGameDescription.getMinCoords());
}
@Test
@DisplayName("Set max coordinates")
void testSetMaxCoords() {
Vector2D newMaxCoords = new Vector2D(2, 2);
chaosGameDescription.setMaxCoords(newMaxCoords);
assertEquals(newMaxCoords, chaosGameDescription.getMaxCoords());
}
@Test
@DisplayName("Set min and max coordinates")
void testSetMinAndMaxCoords() {
Vector2D newMinCoords = new Vector2D(-99, -99);
assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription(newMinCoords, maxCoords, transforms, probabilities));
assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription(maxCoords, minCoords, transforms, probabilities));
assertThrows(IllegalArgumentException.class, () -> new ChaosGameDescription(minCoords, minCoords, transforms, probabilities));
}
}
@Test
......
......@@ -17,8 +17,8 @@ class ChaosGameTest {
private static ChaosGameDescription affineDescriptionWithProb;
private static ChaosGame instance;
@BeforeAll
static void setUp() {
@BeforeEach
void init() {
juliaDescription = new ChaosGameDescription(
new Vector2D(-1.6, -1),
new Vector2D(1.6, 1),
......@@ -39,11 +39,8 @@ class ChaosGameTest {
new AffineTransform2D(new Matrix2x2(-0.15, 0.28, 0.26, 0.24),
new Vector2D(0.0, 0.44))
), List.of(2, 84, 7, 7));
}
@BeforeEach
void init() {
instance = ChaosGame.getInstance(juliaDescription, 500, 500);
instance.setChaosGameDescription(juliaDescription);
instance.setSteps(0);
instance.setTotalSteps(0);
}
......@@ -52,8 +49,6 @@ class ChaosGameTest {
class SingletonTests {
@Test
void testSingletonInstanceIsNull() {
instance = null;
instance = ChaosGame.getInstance(juliaDescription, 500, 500);
assertNotNull(instance, "The singleton instance should not be null");
}
......@@ -152,6 +147,7 @@ class ChaosGameTest {
void runStepsWithProbabilities() {
instance.setChaosGameDescription(affineDescriptionWithProb);
instance.setSteps(10);
instance.runSteps();
assertEquals(10, instance.getSteps());
}
}
......@@ -181,4 +177,24 @@ class ChaosGameTest {
assertEquals(new Vector2D(1.6, 1), instance.getDescription().getMaxCoords(), "The max coords should be the same");
}
@Test
void registerAndNotifyObservers() {
Observer observer = () -> assertTrue(true, "Observer should be notified");
instance.registerObserver(observer);
instance.notifyObservers();
}
@Test
void removeObserver() {
Observer observer = () -> fail("Observer should not be notified");
instance.registerObserver(observer);
instance.removeObserver(observer);
instance.notifyObservers();
}
@AfterEach
void tearDown() {
instance = null;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment