| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.imagery;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
|---|
| 5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
|---|
| 6 |
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 |
|
|---|
| 9 | import org.junit.jupiter.api.Test;
|
|---|
| 10 | import org.junit.jupiter.params.ParameterizedTest;
|
|---|
| 11 | import org.junit.jupiter.params.provider.ValueSource;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Unit tests for class {@link Shape}.
|
|---|
| 15 | */
|
|---|
| 16 | class ShapeTest {
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Tests string conversion
|
|---|
| 20 | */
|
|---|
| 21 | @Test
|
|---|
| 22 | void test() {
|
|---|
| 23 | Shape shape = new Shape();
|
|---|
| 24 | shape.addPoint("47.1", "11.1");
|
|---|
| 25 | shape.addPoint("47.2", "11.2");
|
|---|
| 26 | shape.addPoint("47.3", "11.3");
|
|---|
| 27 | String shapeString = "47.1 11.1 47.2 11.2 47.3 11.3";
|
|---|
| 28 | Shape fromString = new Shape(shapeString, " ");
|
|---|
| 29 | assertEquals(shape, fromString);
|
|---|
| 30 | assertEquals(shapeString, shape.encodeAsString(" "));
|
|---|
| 31 | assertEquals("47.1//11.1//47.2//11.2//47.3//11.3", shape.encodeAsString("//"));
|
|---|
| 32 | assertEquals("47.1,11.1,47.2,11.2,47.3,11.3;47.1,11.1,47.2,11.2,47.3,11.3", Shape.encodeAsString(Arrays.asList(shape, shape)));
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Check double edge cases
|
|---|
| 37 | * @param coordinate the coordinate to check
|
|---|
| 38 | */
|
|---|
| 39 | @ParameterizedTest
|
|---|
| 40 | @ValueSource(doubles = {
|
|---|
| 41 | // The double representation of 0.2575799 * 1e7 is 2575798.9999999995. Directly casting to int will round down.
|
|---|
| 42 | 0.2575799,
|
|---|
| 43 | // Check that 2575798.0000000005 is rounded down
|
|---|
| 44 | 0.2575798
|
|---|
| 45 | })
|
|---|
| 46 | void testDoubleEdgeCases(final double coordinate) {
|
|---|
| 47 | final Shape shape = new Shape();
|
|---|
| 48 | shape.addPoint(Double.toString(1), Double.toString(coordinate));
|
|---|
| 49 | shape.addPoint(Double.toString(coordinate), Double.toString(1));
|
|---|
| 50 | shape.addPoint(Double.toString(coordinate), Double.toString(coordinate));
|
|---|
| 51 | assertAll("Coordinates are not properly rounded on entry",
|
|---|
| 52 | () -> assertEquals(coordinate, shape.getPoints().get(0).getLon()),
|
|---|
| 53 | () -> assertEquals(coordinate, shape.getPoints().get(1).getLat()));
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|