diff options
Diffstat (limited to 'tests/src')
| -rw-r--r-- | tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java | 41 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/geopoint/UnitsTest.java | 103 |
2 files changed, 103 insertions, 41 deletions
diff --git a/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java b/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java deleted file mode 100644 index c5fce00..0000000 --- a/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package cgeo.geocaching.geopoint; - -import cgeo.CGeoTestCase; -import cgeo.geocaching.Settings; - -import java.util.regex.Pattern; - -public class HumanDistanceTest extends CGeoTestCase { - - private static void assertMatch(final String ok, final float distance) { - final String humanDistance = HumanDistance.getHumanDistance(distance); - if (!Pattern.compile('^' + ok + '$').matcher(humanDistance).find()) { - fail("getHumanDistance(" + distance + - ") [metric: " + (Settings.isUseMetricUnits() ? "yes" : "no") + - "] fails to match " + ok + ": " + humanDistance); - } - } - - // Make method non-static so that Settings is initialized - @SuppressWarnings("static-method") - public void testHumanDistance() { - assertEquals("?", HumanDistance.getHumanDistance(null)); - final boolean savedMetrics = Settings.isUseMetricUnits(); - try { - Settings.setUseMetricUnits(true); - assertMatch("123 km", 122.782f); - assertMatch("123 km", 123.456f); - assertMatch("12.3 km", 12.3456f); - assertMatch("1.23 km", 1.23456f); - assertMatch("123 m", 0.123456f); - Settings.setUseMetricUnits(false); - assertMatch("76.7 mi", 123.456f); - assertMatch("7.67 mi", 12.3456f); - assertMatch("0.77 mi", 1.23456f); - assertMatch("405 ft", 0.123456f); - assertMatch("40.5 ft", 0.0123456f); - } finally { - Settings.setUseMetricUnits(savedMetrics); - } - } -} diff --git a/tests/src/cgeo/geocaching/geopoint/UnitsTest.java b/tests/src/cgeo/geocaching/geopoint/UnitsTest.java new file mode 100644 index 0000000..10f1785 --- /dev/null +++ b/tests/src/cgeo/geocaching/geopoint/UnitsTest.java @@ -0,0 +1,103 @@ +package cgeo.geocaching.geopoint; + +import cgeo.CGeoTestCase; +import cgeo.geocaching.Settings; + +import org.apache.commons.lang3.StringUtils; + +public class UnitsTest extends CGeoTestCase { + + private static void assertDistance(final String expected, final float distance) { + final String actual = Units.getDistanceFromKilometers(distance); + if (!StringUtils.equals(expected, actual.replace(',', '.'))) { // make 1.2 the same as 1,2 + fail("getHumanDistance(" + distance + + ") [metric: " + (Settings.isUseMetricUnits() ? "yes" : "no") + + "] fails to match " + expected + ": " + actual); + } + } + + // Make method non-static so that Settings is initialized + @SuppressWarnings("static-method") + public void testDistance() { + assertEquals("?", Units.getDistanceFromKilometers(null)); + final boolean savedMetrics = Settings.isUseMetricUnits(); + try { + Settings.setUseMetricUnits(true); + assertDistance("123 km", 122.782f); + assertDistance("123 km", 123.456f); + assertDistance("12.3 km", 12.3456f); + assertDistance("1.23 km", 1.23456f); + assertDistance("123 m", 0.123456f); + Settings.setUseMetricUnits(false); + assertDistance("76.7 mi", 123.456f); + assertDistance("7.67 mi", 12.3456f); + assertDistance("0.77 mi", 1.23456f); + assertDistance("405 ft", 0.123456f); + assertDistance("40.5 ft", 0.0123456f); + } finally { + Settings.setUseMetricUnits(savedMetrics); + } + } + + // Make method non-static so that Settings is initialized + @SuppressWarnings("static-method") + public void testElevation() { + final boolean savedMetrics = Settings.isUseMetricUnits(); + try { + Settings.setUseMetricUnits(true); + assertElevation("↥ 123 m", 122.782f); + assertElevation("↥ 123 m", 123.456f); + assertElevation("↥ 12 m", 12.3456f); + assertElevation("↥ 1 m", 1.23456f); + assertElevation("↥ 2 m", 1.6f); + assertElevation("↥ 0 m", 0.123456f); + assertElevation("↧ 123 m", -122.782f); + assertElevation("↧ 123 m", -123.456f); + assertElevation("↧ 12 m", -12.3456f); + assertElevation("↧ 1 m", -1.23456f); + assertElevation("↧ 2 m", -1.6f); + assertElevation("↧ 0 m", -0.123456f); + Settings.setUseMetricUnits(false); + assertElevation("↥ 405 ft", 123.456f); + assertElevation("↥ 41 ft", 12.3456f); + } finally { + Settings.setUseMetricUnits(savedMetrics); + } + } + + private static void assertElevation(final String expected, final float meters) { + final String actual = Units.getElevation(meters); + if (!StringUtils.equals(expected, actual.replace(',', '.'))) { + fail("elevation " + actual + " does not match expected " + expected); + } + } + + // Make method non-static so that Settings is initialized + @SuppressWarnings("static-method") + public void testSpeed() { + assertEquals("?", Units.getDistanceFromKilometers(null)); + final boolean savedMetrics = Settings.isUseMetricUnits(); + try { + Settings.setUseMetricUnits(true); + assertSpeed("123 km/h", 122.782f); + assertSpeed("123 km/h", 123.456f); + assertSpeed("12.3 km/h", 12.3456f); + assertSpeed("1.23 km/h", 1.23456f); + assertSpeed("123 m/h", 0.123456f); + Settings.setUseMetricUnits(false); + assertSpeed("76.7 mph", 123.456f); + assertSpeed("7.67 mph", 12.3456f); + assertSpeed("0.77 mph", 1.23456f); + } finally { + Settings.setUseMetricUnits(savedMetrics); + } + } + + private static void assertSpeed(final String expected, final float kilometersPerHour) { + final String actual = Units.getSpeed(kilometersPerHour); + if (!StringUtils.equals(expected, actual.replace(',', '.'))) { + fail("speed " + actual + " does not match expected " + expected); + } + } + +} |
