Changeset 9861 in josm


Ignore:
Timestamp:
2016-02-23T21:01:34+01:00 (6 years ago)
Author:
simon04
Message:

Unit test, Javadoc for LatLon

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r9796 r9861  
    205205
    206206    /**
    207      * Constructs a new {@link LatLon}
     207     * Constructs a new object representing the given latitude/longitude.
    208208     * @param lat the latitude, i.e., the north-south position in degrees
    209209     * @param lon the longitude, i.e., the east-west position in degrees
     
    217217    }
    218218
     219    /**
     220     * Constructs a new object for the given coordinate
     221     * @param coor the coordinate
     222     */
    219223    public LatLon(ICoordinate coor) {
    220224        this(coor.getLat(), coor.getLon());
     
    233237    public static final String NORTH = trc("compass", "N");
    234238
     239    /**
     240     * Formats the latitude part according to the given format
     241     * @param d the coordinate format to use
     242     * @return the formatted latitude
     243     */
    235244    public String latToString(CoordinateFormat d) {
    236245        switch(d) {
     
    254263    public static final String EAST = trc("compass", "E");
    255264
     265    /**
     266     * Formats the longitude part according to the given format
     267     * @param d the coordinate format to use
     268     * @return the formatted longitude
     269     */
    256270    public String lonToString(CoordinateFormat d) {
    257271        switch(d) {
     
    458472     *
    459473     * @return rounded value
    460      */
     474     * @deprecated Use {@link #roundToOsmPrecision(double)} instead
     475     */
     476    @Deprecated
    461477    public static double roundToOsmPrecisionStrict(double value) {
    462478        return roundToOsmPrecision(value);
     
    481497     *
    482498     * @return a clone of this lat LatLon
    483      */
     499     * @deprecated Use {@link #getRoundedToOsmPrecision()} instead
     500     */
     501    @Deprecated
    484502    public LatLon getRoundedToOsmPrecisionStrict() {
    485         return new LatLon(
    486                 roundToOsmPrecisionStrict(lat()),
    487                 roundToOsmPrecisionStrict(lon())
    488                 );
     503        return getRoundedToOsmPrecision();
    489504    }
    490505
     
    503518    }
    504519
     520    /**
     521     * Converts this latitude/longitude to an instance of {@link ICoordinate}.
     522     * @return a {@link ICoordinate} instance of this latitude/longitude
     523     */
    505524    public ICoordinate toCoordinate() {
    506525        return new org.openstreetmap.gui.jmapviewer.Coordinate(lat(), lon());
  • trunk/test/unit/org/openstreetmap/josm/data/coor/LatLonTest.java

    r9810 r9861  
    44import static org.junit.Assert.assertEquals;
    55
     6import java.util.Locale;
     7
     8import org.junit.Before;
    69import org.junit.Test;
     10import org.openstreetmap.josm.Main;
     11import org.openstreetmap.josm.data.projection.Projections;
    712
    813/**
     
    1015 */
    1116public class LatLonTest {
     17
     18    /**
     19     * Setup test.
     20     */
     21    @Before
     22    public void setUp() throws Exception {
     23        Locale.setDefault(Locale.GERMAN);
     24        Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
     25    }
    1226
    1327    private static final double EPSILON = 1e-6;
     
    2640
    2741    /**
    28      * Test of {@link LatLon#roundToOsmPrecisionStrict}
     42     * Test of {@link LatLon#roundToOsmPrecision}
    2943     */
    3044    @Test
    31     public void testRoundToOsmPrecisionStrict() {
     45    public void testRoundToOsmPrecision() {
    3246
    3347        for (double value : SAMPLE_VALUES) {
    34             assertEquals(LatLon.roundToOsmPrecisionStrict(value), value, 0);
     48            assertEquals(LatLon.roundToOsmPrecision(value), value, 0);
    3549        }
    3650
    37         assertEquals(LatLon.roundToOsmPrecisionStrict(0.0), 0.0, 0);
    38         assertEquals(LatLon.roundToOsmPrecisionStrict(-0.0), 0.0, 0);
     51        assertEquals(LatLon.roundToOsmPrecision(0.0), 0.0, 0);
     52        assertEquals(LatLon.roundToOsmPrecision(-0.0), 0.0, 0);
    3953
    40         assertEquals(LatLon.roundToOsmPrecisionStrict(0.12345678),  0.1234568, 0);
    41         assertEquals(LatLon.roundToOsmPrecisionStrict(0.123456789), 0.1234568, 0);
     54        assertEquals(LatLon.roundToOsmPrecision(0.12345678),  0.1234568, 0);
     55        assertEquals(LatLon.roundToOsmPrecision(0.123456789), 0.1234568, 0);
    4256
    43         assertEquals(LatLon.roundToOsmPrecisionStrict(1.12345678),  1.1234568, 0);
    44         assertEquals(LatLon.roundToOsmPrecisionStrict(1.123456789), 1.1234568, 0);
     57        assertEquals(LatLon.roundToOsmPrecision(1.12345678),  1.1234568, 0);
     58        assertEquals(LatLon.roundToOsmPrecision(1.123456789), 1.1234568, 0);
    4559
    46         assertEquals(LatLon.roundToOsmPrecisionStrict(10.12345678),  10.1234568, 0);
    47         assertEquals(LatLon.roundToOsmPrecisionStrict(10.123456789), 10.1234568, 0);
     60        assertEquals(LatLon.roundToOsmPrecision(10.12345678),  10.1234568, 0);
     61        assertEquals(LatLon.roundToOsmPrecision(10.123456789), 10.1234568, 0);
    4862
    49         assertEquals(LatLon.roundToOsmPrecisionStrict(100.12345678),  100.1234568, 0);
    50         assertEquals(LatLon.roundToOsmPrecisionStrict(100.123456789), 100.1234568, 0);
     63        assertEquals(LatLon.roundToOsmPrecision(100.12345678),  100.1234568, 0);
     64        assertEquals(LatLon.roundToOsmPrecision(100.123456789), 100.1234568, 0);
    5165
    52         assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000001),  100.0000000, 0);
    53         assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000001),  100.0000000, 0);
    54         assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000001),  100.0000000, 0);
    55         assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000001),  100.0000000, 0);
    56         assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000001),  100.0000000, 0);
    57         assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000001),  100.0000000, 0);
    58         assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000001),  100.0000000, 0);
    59         assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000000001),  100.0000000, 0);
    60         assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000000001),  100.0000000, 0);
    61         assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000000001),  100.0000000, 0);
    62         assertEquals(LatLon.roundToOsmPrecisionStrict(100.000000000000000001),  100.0000000, 0);
    63         assertEquals(LatLon.roundToOsmPrecisionStrict(100.0000000000000000001),  100.0000000, 0);
    64         assertEquals(LatLon.roundToOsmPrecisionStrict(100.00000000000000000001),  100.0000000, 0);
     66        assertEquals(LatLon.roundToOsmPrecision(100.00000001),  100.0000000, 0);
     67        assertEquals(LatLon.roundToOsmPrecision(100.000000001),  100.0000000, 0);
     68        assertEquals(LatLon.roundToOsmPrecision(100.0000000001),  100.0000000, 0);
     69        assertEquals(LatLon.roundToOsmPrecision(100.00000000001),  100.0000000, 0);
     70        assertEquals(LatLon.roundToOsmPrecision(100.000000000001),  100.0000000, 0);
     71        assertEquals(LatLon.roundToOsmPrecision(100.0000000000001),  100.0000000, 0);
     72        assertEquals(LatLon.roundToOsmPrecision(100.00000000000001),  100.0000000, 0);
     73        assertEquals(LatLon.roundToOsmPrecision(100.000000000000001),  100.0000000, 0);
     74        assertEquals(LatLon.roundToOsmPrecision(100.0000000000000001),  100.0000000, 0);
     75        assertEquals(LatLon.roundToOsmPrecision(100.00000000000000001),  100.0000000, 0);
     76        assertEquals(LatLon.roundToOsmPrecision(100.000000000000000001),  100.0000000, 0);
     77        assertEquals(LatLon.roundToOsmPrecision(100.0000000000000000001),  100.0000000, 0);
     78        assertEquals(LatLon.roundToOsmPrecision(100.00000000000000000001),  100.0000000, 0);
    6579
    66         assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999999999),  100.0000000, 0);
    67         assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999999999),  100.0000000, 0);
    68         assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999999999),  100.0000000, 0);
    69         assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999999),  100.0000000, 0);
    70         assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999999),  100.0000000, 0);
    71         assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999999),  100.0000000, 0);
    72         assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999999),  100.0000000, 0);
    73         assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999999),  100.0000000, 0);
    74         assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999999),  100.0000000, 0);
    75         assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999999),  100.0000000, 0);
    76         assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999999),  100.0000000, 0);
    77         assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999999),  100.0000000, 0);
    78         assertEquals(LatLon.roundToOsmPrecisionStrict(99.999999999),  100.0000000, 0);
    79         assertEquals(LatLon.roundToOsmPrecisionStrict(99.99999999),  100.0000000, 0);
    80         assertEquals(LatLon.roundToOsmPrecisionStrict(99.9999999),  99.9999999, 0);
     80        assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999999),  100.0000000, 0);
     81        assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999999),  100.0000000, 0);
     82        assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999999),  100.0000000, 0);
     83        assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999),  100.0000000, 0);
     84        assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999),  100.0000000, 0);
     85        assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999),  100.0000000, 0);
     86        assertEquals(LatLon.roundToOsmPrecision(99.999999999999999),  100.0000000, 0);
     87        assertEquals(LatLon.roundToOsmPrecision(99.99999999999999),  100.0000000, 0);
     88        assertEquals(LatLon.roundToOsmPrecision(99.9999999999999),  100.0000000, 0);
     89        assertEquals(LatLon.roundToOsmPrecision(99.999999999999),  100.0000000, 0);
     90        assertEquals(LatLon.roundToOsmPrecision(99.99999999999),  100.0000000, 0);
     91        assertEquals(LatLon.roundToOsmPrecision(99.9999999999),  100.0000000, 0);
     92        assertEquals(LatLon.roundToOsmPrecision(99.999999999),  100.0000000, 0);
     93        assertEquals(LatLon.roundToOsmPrecision(99.99999999),  100.0000000, 0);
     94        assertEquals(LatLon.roundToOsmPrecision(99.9999999),  99.9999999, 0);
    8195    }
    8296
     
    143157        assertEquals(270, Math.toDegrees(e.bearing(c)), EPSILON);
    144158    }
     159
     160    /**
     161     * Tests the methods {@link LatLon#latToString(CoordinateFormat)}, {@link LatLon#lonToString(CoordinateFormat)}.
     162     */
     163    @Test
     164    public void testFormatting() {
     165        LatLon c = new LatLon(47.000000, 19.000000);
     166        assertEquals("47.0", c.latToString(CoordinateFormat.DECIMAL_DEGREES));
     167        assertEquals("19.0", c.lonToString(CoordinateFormat.DECIMAL_DEGREES));
     168        assertEquals("47°00'00,0\"N", c.latToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
     169        assertEquals("19°00'00,0\"E", c.lonToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
     170        assertEquals("47°00,000'N", c.latToString(CoordinateFormat.NAUTICAL));
     171        assertEquals("19°00,000'E", c.lonToString(CoordinateFormat.NAUTICAL));
     172        assertEquals("5942074.0724311", c.latToString(CoordinateFormat.EAST_NORTH));
     173        assertEquals("2115070.3250722", c.lonToString(CoordinateFormat.EAST_NORTH));
     174    }
    145175}
Note: See TracChangeset for help on using the changeset viewer.