aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java
blob: c5fce0080da95e46ee2a702554d40901f00223b7 (plain)
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
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);
        }
    }
}