Skip to content

Commit 44292bc

Browse files
committed
Add unit tests for PiecingItTogether exercise
1 parent 4efdb7f commit 44292bc

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import org.junit.jupiter.api.BeforeEach;
2+
import org.junit.jupiter.api.Disabled;
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
7+
8+
public class PiecingItTogetherTest {
9+
private PiecingItTogether solver;
10+
private double epsilon;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solver = new PiecingItTogether();
15+
epsilon = 1e-6;
16+
}
17+
18+
@Test
19+
public void test1000PiecesWithAspectRatio() {
20+
PartialJigsawInformation input = new PartialJigsawInformation(1000, null, null, null, null, 1.6, null);
21+
PartialJigsawInformation expected = new PartialJigsawInformation(1000, 126, 874, 25, 40, 1.6, "landscape");
22+
23+
PartialJigsawInformation actual = solver.getCompleteInformation(input);
24+
assertJigsawEqualsWithTolerance(actual, expected);
25+
}
26+
27+
@Disabled("Remove to run test")
28+
@Test
29+
public void testSquarePuzzleWith32Rows() {
30+
PartialJigsawInformation input = new PartialJigsawInformation(null, null, null, 32, null, null, "square");
31+
PartialJigsawInformation expected = new PartialJigsawInformation(1024, 124, 900, 32, 32, 1.0, "square");
32+
33+
PartialJigsawInformation actual = solver.getCompleteInformation(input);
34+
assertJigsawEqualsWithTolerance(actual, expected);
35+
}
36+
37+
@Disabled("Remove to run test")
38+
@Test
39+
public void testInsideAndAspectRatioOnly() {
40+
PartialJigsawInformation input = new PartialJigsawInformation(null, null, 324, null, null, 1.0, null);
41+
PartialJigsawInformation expected = new PartialJigsawInformation(400, 76, 324, 20, 20, 1.0, "square");
42+
43+
PartialJigsawInformation actual = solver.getCompleteInformation(input);
44+
assertJigsawEqualsWithTolerance(actual, expected);
45+
}
46+
47+
@Disabled("Remove to run test")
48+
@Test
49+
public void testLandscape1500WithRowsAndAspect() {
50+
PartialJigsawInformation input = new PartialJigsawInformation(null, null, null, 30, null, 1.6666666666666667, null);
51+
PartialJigsawInformation expected = new PartialJigsawInformation(1500, 156, 1344, 30, 50, 1.6666666666666667, "landscape");
52+
53+
PartialJigsawInformation actual = solver.getCompleteInformation(input);
54+
assertJigsawEqualsWithTolerance(actual, expected);
55+
}
56+
57+
@Disabled("Remove to run test")
58+
@Test
59+
public void test300PiecesPortraitWithBorder() {
60+
PartialJigsawInformation input = new PartialJigsawInformation(300, 70, null, null, null, null, "portrait");
61+
PartialJigsawInformation expected = new PartialJigsawInformation(300, 70, 230, 25, 12, 0.48, "portrait");
62+
63+
PartialJigsawInformation actual = solver.getCompleteInformation(input);
64+
assertJigsawEqualsWithTolerance(actual, expected);
65+
}
66+
67+
@Disabled("Remove to run test")
68+
@Test
69+
public void testInsufficientData() {
70+
PartialJigsawInformation input = new PartialJigsawInformation(1500, null, null, null, null, null, "landscape");
71+
72+
assertThatThrownBy(() -> solver.getCompleteInformation(input)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Insufficient data");
73+
}
74+
75+
@Disabled("Remove to run test")
76+
@Test
77+
public void testContradictoryData() {
78+
PartialJigsawInformation input = new PartialJigsawInformation(null, null, null, 100, 1000, null, "square");
79+
80+
assertThatThrownBy(() -> solver.getCompleteInformation(input)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Contradictory data");
81+
}
82+
83+
/**
84+
* Helper to compare two PartialJigsawInformation objects,
85+
* allowing a small tolerance when comparing aspect ratio doubles.
86+
*/
87+
private void assertJigsawEqualsWithTolerance(PartialJigsawInformation actual, PartialJigsawInformation expected) {
88+
assertThat(actual.getPieces()).isEqualTo(expected.getPieces());
89+
assertThat(actual.getBorder()).isEqualTo(expected.getBorder());
90+
assertThat(actual.getInside()).isEqualTo(expected.getInside());
91+
assertThat(actual.getRows()).isEqualTo(expected.getRows());
92+
assertThat(actual.getColumns()).isEqualTo(expected.getColumns());
93+
assertThat(actual.getFormat()).isEqualTo(expected.getFormat());
94+
95+
Double actualAspect = actual.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in actual result"));
96+
Double expectedAspect = expected.getAspectRatio().orElseThrow(() -> new AssertionError("Missing aspect ratio in expected result"));
97+
98+
assertThat(actualAspect).isCloseTo(expectedAspect, org.assertj.core.api.Assertions.within(epsilon));
99+
}
100+
}

0 commit comments

Comments
 (0)