Skip to content

Commit

Permalink
Sync tests for practice exercise space age (#2640)
Browse files Browse the repository at this point in the history
  • Loading branch information
manumafe98 authored Jan 18, 2024
1 parent c6bf83f commit c4d42d7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.math.BigDecimal;

class SpaceAge {

private enum Planet {
Expand All @@ -24,8 +22,6 @@ private double getRelativeOrbitalPeriod() {
}

private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0;
private static final int PRECISION = 2;

private double seconds;

SpaceAge(double seconds) {
Expand Down Expand Up @@ -65,9 +61,7 @@ private double getRelativeOrbitalPeriod() {
}

private double calculateAge(Planet planet) {
double age = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.getRelativeOrbitalPeriod());

return new BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue();
return (seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS) / planet.getRelativeOrbitalPeriod();
}

}
18 changes: 15 additions & 3 deletions exercises/practice/space-age/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

[80096d30-a0d4-4449-903e-a381178355d8]
description = "age on Neptune"

[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4]
description = "invalid planet causes error"
include = false
comment = "Excluded because the design of the exercise does not allow for arbitrary planets"
18 changes: 9 additions & 9 deletions exercises/practice/space-age/src/test/java/SpaceAgeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static org.assertj.core.api.Assertions.offset;

public class SpaceAgeTest {

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

assertThat(age.onEarth()).isEqualTo(31.69, within(MAXIMUM_DELTA));
assertThat(age.onEarth()).isEqualTo(31.69, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onMercury()).isEqualTo(280.88, within(MAXIMUM_DELTA));
assertThat(age.onMercury()).isEqualTo(280.88, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onVenus()).isEqualTo(9.78, within(MAXIMUM_DELTA));
assertThat(age.onVenus()).isEqualTo(9.78, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onMars()).isEqualTo(35.88, within(MAXIMUM_DELTA));
assertThat(age.onMars()).isEqualTo(35.88, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onJupiter()).isEqualTo(2.41, within(MAXIMUM_DELTA));
assertThat(age.onJupiter()).isEqualTo(2.41, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onSaturn()).isEqualTo(2.15, within(MAXIMUM_DELTA));
assertThat(age.onSaturn()).isEqualTo(2.15, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onUranus()).isEqualTo(0.46, within(MAXIMUM_DELTA));
assertThat(age.onUranus()).isEqualTo(0.46, offset(MAXIMUM_DELTA));
}

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

assertThat(age.onNeptune()).isEqualTo(0.35, within(MAXIMUM_DELTA));
assertThat(age.onNeptune()).isEqualTo(0.35, offset(MAXIMUM_DELTA));
}
}

0 comments on commit c4d42d7

Please sign in to comment.