@@ -23,20 +23,24 @@ public class EncodingTest {
2323
2424 private static class TestData {
2525
26- private final double latitude ;
27- private final double longitude ;
26+ private final double latitudeDegrees ;
27+ private final double longitudeDegrees ;
28+ private final long latitudeInteger ;
29+ private final long longitudeInteger ;
2830 private final int length ;
2931 private final String code ;
3032
3133 public TestData (String line ) {
3234 String [] parts = line .split ("," );
33- if (parts .length != 4 ) {
35+ if (parts .length != 6 ) {
3436 throw new IllegalArgumentException ("Wrong format of testing data." );
3537 }
36- this .latitude = Double .parseDouble (parts [0 ]);
37- this .longitude = Double .parseDouble (parts [1 ]);
38- this .length = Integer .parseInt (parts [2 ]);
39- this .code = parts [3 ];
38+ this .latitudeDegrees = Double .parseDouble (parts [0 ]);
39+ this .longitudeDegrees = Double .parseDouble (parts [1 ]);
40+ this .latitudeInteger = Long .parseLong (parts [2 ]);
41+ this .longitudeInteger = Long .parseLong (parts [3 ]);
42+ this .length = Integer .parseInt (parts [4 ]);
43+ this .code = parts [5 ];
4044 }
4145 }
4246
@@ -56,14 +60,67 @@ public void setUp() throws Exception {
5660 }
5761
5862 @ Test
59- public void testEncodeFromLatLong () {
63+ public void testEncodeFromDegrees () {
64+ double allowedErrorRate = 0.05 ;
65+ int failedEncodings = 0 ;
66+ for (TestData testData : testDataList ) {
67+ String got =
68+ OpenLocationCode .encode (
69+ testData .latitudeDegrees , testData .longitudeDegrees , testData .length );
70+ if (!testData .code .equals (got )) {
71+ failedEncodings ++;
72+ System .out .printf (
73+ "ENCODING DIFFERENCE: encode(%f,%f,%d) got %s, want %s\n " ,
74+ testData .latitudeDegrees ,
75+ testData .longitudeDegrees ,
76+ testData .length ,
77+ got ,
78+ testData .code );
79+ }
80+ }
81+ double gotRate = (double ) failedEncodings / (double ) testDataList .size ();
82+ Assert .assertTrue (
83+ String .format (
84+ "Too many encoding errors (actual rate %f, allowed rate %f), see ENCODING DIFFERENCE"
85+ + " lines" ,
86+ gotRate , allowedErrorRate ),
87+ gotRate <= allowedErrorRate );
88+ }
89+
90+ @ Test
91+ public void testDegreesToIntegers () {
92+ for (TestData testData : testDataList ) {
93+ long [] got =
94+ OpenLocationCode .degreesToIntegers (testData .latitudeDegrees , testData .longitudeDegrees );
95+ Assert .assertTrue (
96+ String .format (
97+ "degreesToIntegers(%f, %f) returned latitude %d, expected %d" ,
98+ testData .latitudeDegrees ,
99+ testData .longitudeDegrees ,
100+ got [0 ],
101+ testData .latitudeInteger ),
102+ got [0 ] == testData .latitudeInteger || got [0 ] == testData .latitudeInteger - 1 );
103+ Assert .assertTrue (
104+ String .format (
105+ "degreesToIntegers(%f, %f) returned longitude %d, expected %d" ,
106+ testData .latitudeDegrees ,
107+ testData .longitudeDegrees ,
108+ got [1 ],
109+ testData .longitudeInteger ),
110+ got [1 ] == testData .longitudeInteger || got [1 ] == testData .longitudeInteger - 1 );
111+ }
112+ }
113+
114+ @ Test
115+ public void testEncodeFromIntegers () {
60116 for (TestData testData : testDataList ) {
61117 Assert .assertEquals (
62118 String .format (
63- "Latitude %f , longitude %f and length %d were wrongly encoded." ,
64- testData .latitude , testData .longitude , testData .length ),
119+ "Latitude %d , longitude %d and length %d were wrongly encoded." ,
120+ testData .latitudeInteger , testData .longitudeInteger , testData .length ),
65121 testData .code ,
66- OpenLocationCode .encode (testData .latitude , testData .longitude , testData .length ));
122+ OpenLocationCode .encodeIntegers (
123+ testData .latitudeInteger , testData .longitudeInteger , testData .length ));
67124 }
68125 }
69126}
0 commit comments