1
exercism fetch java pythagorean-triplet

src/test/java/PythagoreanTripletTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Ignore;

public class PythagoreanTripletTest {

    @Test
    public void shouldCalculateSum() {
        PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
        final int expected = 12;
        final int actual = triplet.calculateSum();
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void shouldCalculateProduct() {
        PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
        final long expected = 60l;
        final long actual = triplet.calculateProduct();
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void testIsPythagoreanOK() {
        PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
        assertTrue(triplet.isPythagorean());
    }

    @Ignore("Remove to run test")
    @Test
    public void testIsPythagoreanFail() {
        PythagoreanTriplet triplet = new PythagoreanTriplet(5, 6, 7);
        assertFalse(triplet.isPythagorean());
    }

    @Ignore("Remove to run test")
    @Test
    public void shouldMakeTripletsUpToTen() {
        final List<Long> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(10)
                        .build()
                        .stream()
                        .map(t -> t.calculateProduct())
                        .sorted()
                        .collect(Collectors.toList());
        final List<Long> expected = Arrays.asList(60l, 480l);
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void shouldMakeTripletsElevenToTwenty() {
        final List<Long> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsGreaterThanOrEqualTo(11)
                        .withFactorsLessThanOrEqualTo(20)
                        .build()
                        .stream()
                        .map(t -> t.calculateProduct())
                        .sorted((p1, p2) -> Double.compare(p1, p2))
                        .collect(Collectors.toList());
        final List<Long> expected = Arrays.asList(3840l);
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void shouldMakeTripletsAndFilterOnSum() {
        final List<Long> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(100)
                        .thatSumTo(180)
                        .build()
                        .stream()
                        .map(t -> t.calculateProduct())
                        .sorted((p1, p2) -> Double.compare(p1, p2))
                        .collect(Collectors.toList());
        final List<Long> expected = Arrays.asList(118080l, 168480l, 202500l);
        assertEquals(expected, actual);
    }
}