Skip to content

Commit

Permalink
Added code for directly testing the back-end formatting of coordinates
Browse files Browse the repository at this point in the history
This code is finding the same bug already detected by the MapModeTests
albeit in a much more direct way without needing to instantiate the
user interface. Ideally, this should be a unit test that can be executed
on the host-side, but the code depends on the Location.convert() for
the actual coordinate conversions which is only available on the
Android platform itself.
  • Loading branch information
penguin359 committed Jan 5, 2022
1 parent d4b1335 commit f8051fa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
57 changes: 57 additions & 0 deletions androidTest/java/org/aprsdroid/app/CoordinateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.aprsdroid.app;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;

import org.aprsdroid.app.testing.CoordinateMatcher;
import org.junit.Test;

import scala.Tuple2;

public class CoordinateTest {
// Reference data generated from https://www.pgc.umn.edu/apps/convert/
private static final String providedNLatitude = "77° 15' 30\" N";
private static final float expectedNLatitude = 77.258333f;
private static final String providedELongitude = "164° 45' 15\" E";
private static final float expectedELongitude = 164.754167f;
private static final String providedSLatitude = "45° 30' 45\" S";
private static final float expectedSLatitude = -45.5125f;
private static final String providedWLongitude = "97° 20' 40\" W";
private static final float expectedWLongitude = -97.344444f;

@Test
public void givenLocationInNEHemisphere_whenFormattedAsDMSString_thenParseBackIntoDecimalValue() {
Tuple2<String, String> actual = AprsPacket$.MODULE$.formatCoordinates(expectedNLatitude, expectedELongitude);
float floatLatitude = CoordinateMatcher.matchLatitude(actual._1);
float floatLongitude = CoordinateMatcher.matchLongitude(actual._2);
assertThat("Latitude", (double) floatLatitude, closeTo((double) expectedNLatitude, 1e-7));
assertThat("Longitude", (double) floatLongitude, closeTo((double) expectedELongitude, 1e-7));
}

@Test
public void givenLocationInNWHemisphere_whenFormattedAsDMSString_thenParseBackIntoDecimalValue() {
Tuple2<String, String> actual = AprsPacket$.MODULE$.formatCoordinates(expectedNLatitude, expectedWLongitude);
float floatLatitude = CoordinateMatcher.matchLatitude(actual._1);
float floatLongitude = CoordinateMatcher.matchLongitude(actual._2);
assertThat("Latitude", (double) floatLatitude, closeTo((double) expectedNLatitude, 1e-7));
assertThat("Longitude", (double) floatLongitude, closeTo((double) expectedWLongitude, 1e-7));
}

@Test
public void givenLocationInSEHemisphere_whenFormattedAsDMSString_thenParseBackIntoDecimalValue() {
Tuple2<String, String> actual = AprsPacket$.MODULE$.formatCoordinates(expectedSLatitude, expectedELongitude);
float floatLatitude = CoordinateMatcher.matchLatitude(actual._1);
float floatLongitude = CoordinateMatcher.matchLongitude(actual._2);
assertThat("Latitude", (double) floatLatitude, closeTo((double) expectedSLatitude, 1e-7));
assertThat("Longitude", (double) floatLongitude, closeTo((double) expectedELongitude, 1e-7));
}

@Test
public void givenLocationInSWHemisphere_whenFormattedAsDMSString_thenParseBackIntoDecimalValue() {
Tuple2<String, String> actual = AprsPacket$.MODULE$.formatCoordinates(expectedSLatitude, expectedWLongitude);
float floatLatitude = CoordinateMatcher.matchLatitude(actual._1);
float floatLongitude = CoordinateMatcher.matchLongitude(actual._2);
assertThat("Latitude", (double) floatLatitude, closeTo((double) expectedSLatitude, 1e-7));
assertThat("Longitude", (double) floatLongitude, closeTo((double) expectedWLongitude, 1e-7));
}
}
20 changes: 5 additions & 15 deletions test/java/org/aprsdroid/app/CoordinateTest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package org.aprsdroid.app;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

import static org.hamcrest.MatcherAssert.assertThat;
import org.aprsdroid.app.testing.CoordinateMatcher;
import org.junit.Assert;
import org.junit.Test;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoordinateTest {
// Reference data generated from https://www.pgc.umn.edu/apps/convert/
private static final String providedNLatitude = "77° 15' 30\" N";
Expand All @@ -30,24 +20,24 @@ public class CoordinateTest {
@Test
public void givenDMSLatitudeInN_whenParsingString_ThenShouldMatchDecimal() {
float value = CoordinateMatcher.matchLatitude(providedNLatitude);
assertThat("Latitude", (double)value, closeTo((double)expectedNLatitude, 1e-7));
assertThat("Latitude", (double) value, closeTo((double) expectedNLatitude, 1e-7));
}

@Test
public void givenDMSLongitudeInE_whenParsingString_ThenShouldMatchDecimal() {
float value = CoordinateMatcher.matchLongitude(providedELongitude);
assertThat("Longitude", (double)value, closeTo((double)expectedELongitude, 1e-7));
assertThat("Longitude", (double) value, closeTo((double) expectedELongitude, 1e-7));
}

@Test
public void givenDMSLatitudeInS_whenParsingString_ThenShouldMatchDecimal() {
float value = CoordinateMatcher.matchLatitude(providedSLatitude);
assertThat("Latitude", (double)value, closeTo((double)expectedSLatitude, 1e-7));
assertThat("Latitude", (double) value, closeTo((double) expectedSLatitude, 1e-7));
}

@Test
public void givenDMSLongitudeInW_whenParsingString_ThenShouldMatchDecimal() {
float value = CoordinateMatcher.matchLongitude(providedWLongitude);
assertThat("Longitude", (double)value, closeTo((double)expectedWLongitude, 1e-7));
assertThat("Longitude", (double) value, closeTo((double) expectedWLongitude, 1e-7));
}
}

0 comments on commit f8051fa

Please sign in to comment.