Skip to content

Commit c4d42d7

Browse files
authored
Sync tests for practice exercise space age (#2640)
1 parent c6bf83f commit c4d42d7

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.math.BigDecimal;
2-
31
class SpaceAge {
42

53
private enum Planet {
@@ -24,8 +22,6 @@ private double getRelativeOrbitalPeriod() {
2422
}
2523

2624
private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0;
27-
private static final int PRECISION = 2;
28-
2925
private double seconds;
3026

3127
SpaceAge(double seconds) {
@@ -65,9 +61,7 @@ private double getRelativeOrbitalPeriod() {
6561
}
6662

6763
private double calculateAge(Planet planet) {
68-
double age = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.getRelativeOrbitalPeriod());
69-
70-
return new BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue();
64+
return (seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS) / planet.getRelativeOrbitalPeriod();
7165
}
7266

7367
}

exercises/practice/space-age/.meta/tests.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[84f609af-5a91-4d68-90a3-9e32d8a5cd34]
613
description = "age on Earth"
@@ -25,3 +32,8 @@ description = "age on Uranus"
2532

2633
[80096d30-a0d4-4449-903e-a381178355d8]
2734
description = "age on Neptune"
35+
36+
[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4]
37+
description = "invalid planet causes error"
38+
include = false
39+
comment = "Excluded because the design of the exercise does not allow for arbitrary planets"

exercises/practice/space-age/src/test/java/SpaceAgeTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import org.junit.jupiter.api.Test;
33

44
import static org.assertj.core.api.Assertions.assertThat;
5-
import static org.assertj.core.api.Assertions.within;
5+
import static org.assertj.core.api.Assertions.offset;
66

77
public class SpaceAgeTest {
88

@@ -12,62 +12,62 @@ public class SpaceAgeTest {
1212
public void ageOnEarth() {
1313
SpaceAge age = new SpaceAge(1000000000);
1414

15-
assertThat(age.onEarth()).isEqualTo(31.69, within(MAXIMUM_DELTA));
15+
assertThat(age.onEarth()).isEqualTo(31.69, offset(MAXIMUM_DELTA));
1616
}
1717

1818
@Disabled("Remove to run test")
1919
@Test
2020
public void ageOnMercury() {
2121
SpaceAge age = new SpaceAge(2134835688);
2222

23-
assertThat(age.onMercury()).isEqualTo(280.88, within(MAXIMUM_DELTA));
23+
assertThat(age.onMercury()).isEqualTo(280.88, offset(MAXIMUM_DELTA));
2424
}
2525

2626
@Disabled("Remove to run test")
2727
@Test
2828
public void ageOnVenus() {
2929
SpaceAge age = new SpaceAge(189839836);
3030

31-
assertThat(age.onVenus()).isEqualTo(9.78, within(MAXIMUM_DELTA));
31+
assertThat(age.onVenus()).isEqualTo(9.78, offset(MAXIMUM_DELTA));
3232
}
3333

3434
@Disabled("Remove to run test")
3535
@Test
3636
public void ageOnMars() {
3737
SpaceAge age = new SpaceAge(2129871239L);
3838

39-
assertThat(age.onMars()).isEqualTo(35.88, within(MAXIMUM_DELTA));
39+
assertThat(age.onMars()).isEqualTo(35.88, offset(MAXIMUM_DELTA));
4040
}
4141

4242
@Disabled("Remove to run test")
4343
@Test
4444
public void ageOnJupiter() {
4545
SpaceAge age = new SpaceAge(901876382);
4646

47-
assertThat(age.onJupiter()).isEqualTo(2.41, within(MAXIMUM_DELTA));
47+
assertThat(age.onJupiter()).isEqualTo(2.41, offset(MAXIMUM_DELTA));
4848
}
4949

5050
@Disabled("Remove to run test")
5151
@Test
5252
public void ageOnSaturn() {
5353
SpaceAge age = new SpaceAge(2000000000L);
5454

55-
assertThat(age.onSaturn()).isEqualTo(2.15, within(MAXIMUM_DELTA));
55+
assertThat(age.onSaturn()).isEqualTo(2.15, offset(MAXIMUM_DELTA));
5656
}
5757

5858
@Disabled("Remove to run test")
5959
@Test
6060
public void ageOnUranus() {
6161
SpaceAge age = new SpaceAge(1210123456L);
6262

63-
assertThat(age.onUranus()).isEqualTo(0.46, within(MAXIMUM_DELTA));
63+
assertThat(age.onUranus()).isEqualTo(0.46, offset(MAXIMUM_DELTA));
6464
}
6565

6666
@Disabled("Remove to run test")
6767
@Test
6868
public void ageOnNeptune() {
6969
SpaceAge age = new SpaceAge(1821023456L);
7070

71-
assertThat(age.onNeptune()).isEqualTo(0.35, within(MAXIMUM_DELTA));
71+
assertThat(age.onNeptune()).isEqualTo(0.35, offset(MAXIMUM_DELTA));
7272
}
7373
}

0 commit comments

Comments
 (0)