lots of exercises in java... from https://github.com/exercism/java

PythagoreanTripletTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import java.util.Arrays;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6. import static org.junit.Assert.assertEquals;
  7. import static org.junit.Assert.assertFalse;
  8. import static org.junit.Assert.assertTrue;
  9. import org.junit.Test;
  10. import org.junit.Ignore;
  11. public class PythagoreanTripletTest {
  12. @Test
  13. public void shouldCalculateSum() {
  14. PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
  15. int expected = 12;
  16. int actual = triplet.calculateSum();
  17. assertEquals(expected, actual);
  18. }
  19. @Ignore("Remove to run test")
  20. @Test
  21. public void shouldCalculateProduct() {
  22. PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
  23. long expected = 60l;
  24. long actual = triplet.calculateProduct();
  25. assertEquals(expected, actual);
  26. }
  27. @Ignore("Remove to run test")
  28. @Test
  29. public void testIsPythagoreanOK() {
  30. PythagoreanTriplet triplet = new PythagoreanTriplet(3, 4, 5);
  31. assertTrue(triplet.isPythagorean());
  32. }
  33. @Ignore("Remove to run test")
  34. @Test
  35. public void testIsPythagoreanFail() {
  36. PythagoreanTriplet triplet = new PythagoreanTriplet(5, 6, 7);
  37. assertFalse(triplet.isPythagorean());
  38. }
  39. @Ignore("Remove to run test")
  40. @Test
  41. public void shouldMakeTripletsUpToTen() {
  42. List<Long> actual
  43. = PythagoreanTriplet
  44. .makeTripletsList()
  45. .withFactorsLessThanOrEqualTo(10)
  46. .build()
  47. .stream()
  48. .map(t -> t.calculateProduct())
  49. .sorted()
  50. .collect(Collectors.toList());
  51. List<Long> expected = Arrays.asList(60l, 480l);
  52. assertEquals(expected, actual);
  53. }
  54. @Ignore("Remove to run test")
  55. @Test
  56. public void shouldMakeTripletsElevenToTwenty() {
  57. List<Long> actual
  58. = PythagoreanTriplet
  59. .makeTripletsList()
  60. .withFactorsGreaterThanOrEqualTo(11)
  61. .withFactorsLessThanOrEqualTo(20)
  62. .build()
  63. .stream()
  64. .map(t -> t.calculateProduct())
  65. .sorted((p1, p2) -> Double.compare(p1, p2))
  66. .collect(Collectors.toList());
  67. List<Long> expected = Arrays.asList(3840l);
  68. assertEquals(expected, actual);
  69. }
  70. @Ignore("Remove to run test")
  71. @Test
  72. public void shouldMakeTripletsAndFilterOnSum() {
  73. List<Long> actual
  74. = PythagoreanTriplet
  75. .makeTripletsList()
  76. .withFactorsLessThanOrEqualTo(100)
  77. .thatSumTo(180)
  78. .build()
  79. .stream()
  80. .map(t -> t.calculateProduct())
  81. .sorted((p1, p2) -> Double.compare(p1, p2))
  82. .collect(Collectors.toList());
  83. List<Long> expected = Arrays.asList(118080l, 168480l, 202500l);
  84. assertEquals(expected, actual);
  85. }
  86. }