Skip to content

Commit 70ccdcc

Browse files
Prince Rachit SinhaFridaTveit
authored andcommitted
test(PythagoreanTriplet): Add version and test case. (exercism#1561)
* test(PythagoreanTriplet): Add version and test case. 1. Add version file in .meta folder. 2. Add missing test cases a/c to canonical data. 3. Update source file logic to save triplet in order a<=b<=c. 4. Reformat test file (eclipse deafult formatting). Signed-off-by: Prince Rachit Sinha <prince.rachit@mayadata.io> * Address review comments Signed-off-by: princerachit <prince.rachit@mayadata.io> * Ignore all non initial tests Signed-off-by: princerachit <prince.rachit@mayadata.io> * Remove non-canonical cases and address review comments Signed-off-by: princerachit <prince.rachit@mayadata.io> * Remove Ignore from first test Signed-off-by: princerachit <prince.rachit@mayadata.io>
1 parent dd327fc commit 70ccdcc

3 files changed

Lines changed: 77 additions & 47 deletions

File tree

‎exercises/pythagorean-triplet/.meta/src/reference/java/PythagoreanTriplet.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public List<PythagoreanTriplet> build() {
102102
sqrt = Math.sqrt(n * n + m * m);
103103
floor = (int) Math.floor(sqrt);
104104
if (sqrt == floor) {
105-
triplets.add(new PythagoreanTriplet(m, n, floor));
105+
triplets.add(new PythagoreanTriplet(n, m, floor));
106106
}
107107
}
108108
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0
Lines changed: 75 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,122 @@
11

22
import java.util.Arrays;
33
import java.util.List;
4-
import java.util.stream.Collectors;
4+
import java.util.Collections;
55
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertFalse;
7-
import static org.junit.Assert.assertTrue;
86
import org.junit.Test;
97
import org.junit.Ignore;
108

119
public class PythagoreanTripletTest {
1210

1311
@Test
14-
public void shouldCalculateSum() {
15-
PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
16-
int expected = 12;
17-
int actual = triplet.calculateSum();
12+
public void tripletsWhoseSumIs12() {
13+
List<PythagoreanTriplet> actual
14+
= PythagoreanTriplet
15+
.makeTripletsList()
16+
.withFactorsLessThanOrEqualTo(12)
17+
.thatSumTo(12)
18+
.build();
19+
List<PythagoreanTriplet> expected
20+
= Collections.singletonList(new PythagoreanTriplet(3, 4, 5));
1821
assertEquals(expected, actual);
1922
}
2023

2124
@Ignore("Remove to run test")
2225
@Test
23-
public void shouldCalculateProduct() {
24-
PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
25-
long expected = 60L;
26-
long actual = triplet.calculateProduct();
26+
public void tripletsWhoseSumIs108() {
27+
List<PythagoreanTriplet> actual
28+
= PythagoreanTriplet
29+
.makeTripletsList()
30+
.withFactorsLessThanOrEqualTo(108)
31+
.thatSumTo(108)
32+
.build();
33+
List<PythagoreanTriplet> expected
34+
= Collections.singletonList(new PythagoreanTriplet(27, 36, 45));
2735
assertEquals(expected, actual);
2836
}
2937

3038
@Ignore("Remove to run test")
3139
@Test
32-
public void testIsPythagoreanOK() {
33-
PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
34-
assertTrue(triplet.isPythagorean());
40+
public void tripletsWhoseSumIs1000() {
41+
List<PythagoreanTriplet> actual
42+
= PythagoreanTriplet
43+
.makeTripletsList()
44+
.withFactorsLessThanOrEqualTo(1000)
45+
.thatSumTo(1000)
46+
.build();
47+
List<PythagoreanTriplet> expected
48+
= Collections.singletonList(new PythagoreanTriplet(200, 375, 425));
49+
assertEquals(expected, actual);
3550
}
3651

3752
@Ignore("Remove to run test")
3853
@Test
39-
public void testIsPythagoreanFail() {
40-
PythagoreanTriplet triplet = new PythagoreanTriplet(5, 6, 7);
41-
assertFalse(triplet.isPythagorean());
54+
public void tripletsWhoseSumIs1001() {
55+
List<PythagoreanTriplet> actual
56+
= PythagoreanTriplet
57+
.makeTripletsList()
58+
.withFactorsLessThanOrEqualTo(1001)
59+
.thatSumTo(1001)
60+
.build();
61+
List<PythagoreanTriplet> expected = Collections.emptyList();
62+
assertEquals(expected, actual);
4263
}
4364

4465
@Ignore("Remove to run test")
4566
@Test
46-
public void shouldMakeTripletsUpToTen() {
47-
List<Long> actual
67+
public void tripletsWhoseSumIs90() {
68+
List<PythagoreanTriplet> actual
4869
= PythagoreanTriplet
4970
.makeTripletsList()
50-
.withFactorsLessThanOrEqualTo(10)
51-
.build()
52-
.stream()
53-
.map(t -> t.calculateProduct())
54-
.sorted()
55-
.collect(Collectors.toList());
56-
List<Long> expected = Arrays.asList(60L, 480L);
71+
.withFactorsLessThanOrEqualTo(90)
72+
.thatSumTo(90)
73+
.build();
74+
List<PythagoreanTriplet> expected
75+
= Arrays.asList(
76+
new PythagoreanTriplet(9, 40, 41),
77+
new PythagoreanTriplet(15, 36, 39));
5778
assertEquals(expected, actual);
5879
}
5980

6081
@Ignore("Remove to run test")
6182
@Test
62-
public void shouldMakeTripletsElevenToTwenty() {
63-
List<Long> actual
83+
public void tripletsWhoseSumIs840() {
84+
List<PythagoreanTriplet> actual
6485
= PythagoreanTriplet
6586
.makeTripletsList()
66-
.withFactorsGreaterThanOrEqualTo(11)
67-
.withFactorsLessThanOrEqualTo(20)
68-
.build()
69-
.stream()
70-
.map(t -> t.calculateProduct())
71-
.sorted((p1, p2) -> Double.compare(p1, p2))
72-
.collect(Collectors.toList());
73-
List<Long> expected = Arrays.asList(3840L);
87+
.withFactorsLessThanOrEqualTo(840)
88+
.thatSumTo(840)
89+
.build();
90+
List<PythagoreanTriplet> expected
91+
= Arrays.asList(
92+
new PythagoreanTriplet(40, 399, 401),
93+
new PythagoreanTriplet(56, 390, 394),
94+
new PythagoreanTriplet(105, 360, 375),
95+
new PythagoreanTriplet(120, 350, 370),
96+
new PythagoreanTriplet(140, 336, 364),
97+
new PythagoreanTriplet(168, 315, 357),
98+
new PythagoreanTriplet(210, 280, 350),
99+
new PythagoreanTriplet(240, 252, 348));
74100
assertEquals(expected, actual);
75101
}
76102

77103
@Ignore("Remove to run test")
78104
@Test
79-
public void shouldMakeTripletsAndFilterOnSum() {
80-
List<Long> actual
105+
public void tripletsWhoseSumIs30000() {
106+
List<PythagoreanTriplet> actual
81107
= PythagoreanTriplet
82108
.makeTripletsList()
83-
.withFactorsLessThanOrEqualTo(100)
84-
.thatSumTo(180)
85-
.build()
86-
.stream()
87-
.map(t -> t.calculateProduct())
88-
.sorted((p1, p2) -> Double.compare(p1, p2))
89-
.collect(Collectors.toList());
90-
List<Long> expected = Arrays.asList(118080L, 168480L, 202500L);
109+
.withFactorsLessThanOrEqualTo(30000)
110+
.thatSumTo(30000)
111+
.build();
112+
List<PythagoreanTriplet> expected
113+
= Arrays.asList(
114+
new PythagoreanTriplet(1200, 14375, 14425),
115+
new PythagoreanTriplet(1875, 14000, 14125),
116+
new PythagoreanTriplet(5000, 12000, 13000),
117+
new PythagoreanTriplet(6000, 11250, 12750),
118+
new PythagoreanTriplet(7500, 10000, 12500));
91119
assertEquals(expected, actual);
92120
}
121+
93122
}

0 commit comments

Comments
 (0)