Skip to content

Commit aad62de

Browse files
committed
Convert rounding to integer conversion, fix longitude wrap around.
1 parent fa77779 commit aad62de

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

visualbasic/OLCTests.bas

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ Private Function loadEncodingTestCSV() AS Variant
311311
End Function
312312

313313
' Check the degrees to integer conversions.
314+
' Due to floating point precision limitations, we may get values 1 less than expected.
314315
Sub TEST_IntegerConversion()
315316
Dim encodingTests As Variant
316317
Dim i As Integer
@@ -321,19 +322,19 @@ Sub TEST_IntegerConversion()
321322

322323
encodingTests = loadEncodingTestCSV()
323324

324-
For i = 0 To 302
325+
For i = 0 To 301
325326
tc = encodingTests(i)
326327
degrees = tc(0)
327328
want_integer = tc(2)
328329
got_integer = latitudeToInteger(degrees)
329-
If got_integer <> want_integer Then
330+
If got_integer < want_integer - 1 Or got_integer > want_integer Then
330331
MsgBox ("Encoding test " + CStr(i) + ": latitudeToInteger(" + CStr(degrees) + "): got " + CStr(got_integer) + ", want " + CStr(want_integer))
331332
Exit Sub
332333
End If
333334
degrees = tc(1)
334335
want_integer = tc(3)
335336
got_integer = longitudeToInteger(degrees)
336-
If got_integer <> want_integer Then
337+
If got_integer < want_integer - 1 Or got_integer > want_integer Then
337338
MsgBox ("Encoding test " + CStr(i) + ": longitudeToInteger(" + CStr(degrees) + "): got " + CStr(got_integer) + ", want " + CStr(want_integer))
338339
Exit Sub
339340
End If
@@ -355,7 +356,7 @@ Sub TEST_IntegerEncoding()
355356

356357
encodingTests = loadEncodingTestCSV()
357358

358-
For i = 0 To 302
359+
For i = 0 To 301
359360
tc = encodingTests(i)
360361
' Latitude and longitude are the integer values, not degrees.
361362
latitude = tc(2)

visualbasic/OpenLocationCode.bas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Private Function latitudeToInteger(ByVal latitude As Double) AS Double
388388
' Convert latitude into a positive integer clipped into the range 0-(just
389389
' under 180*2.5e7). Latitude 90 needs to be adjusted to be just less, so the
390390
' returned code can also be decoded.
391-
lat = Round(latitude * FINAL_LAT_PRECISION_)
391+
lat = Int(latitude * FINAL_LAT_PRECISION_)
392392
lat = lat + LATITUDE_MAX_ * FINAL_LAT_PRECISION_
393393
If lat < 0 Then
394394
lat = 0
@@ -403,10 +403,10 @@ End Function
403403
Private Function longitudeToInteger(ByVal longitude As Double) AS Double
404404
Dim lng As Double
405405
' Convert longitude into a positive integer and normalise it into the range 0-360*8.192e6.
406-
lng = Round(longitude * FINAL_LNG_PRECISION_)
406+
lng = Int(longitude * FINAL_LNG_PRECISION_)
407407
lng = lng + LONGITUDE_MAX_ * FINAL_LNG_PRECISION_
408408
If lng < 0 Then
409-
lng = doubleMod(lng, (2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_)) + 2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_
409+
lng = doubleMod(lng, (2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_))
410410
ElseIf lng >= 2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_ Then
411411
lng = doubleMod(lng, (2 * LONGITUDE_MAX_ * FINAL_LNG_PRECISION_))
412412
EndIf

visualbasic/update_tests.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function addEncodingTests() {
2525
STATEMENTS="$STATEMENTS testCases(${TEST_CASE_COUNTER}) = Array(${latd}, ${lngd}, ${lati}, ${lngi}, ${len}, \"${code}\")\n"
2626
TEST_CASE_COUNTER=$((TEST_CASE_COUNTER+1))
2727
done < ../test_data/encoding.csv
28+
# VB uses 0-based indexing for the array.
29+
TEST_CASE_MAX_INDEX=$((TEST_CASE_COUNTER-1))
2830

2931
# Add the VB function.
3032
echo -e "Private Function loadEncodingTestCSV() AS Variant\n\n Dim testCases(${TEST_CASE_COUNTER}) As Variant" >>"$VBA_TEST"
@@ -36,6 +38,7 @@ function addEncodingTests() {
3638
cat <<EOF >>"$VBA_TEST"
3739

3840
' Check the degrees to integer conversions.
41+
' Due to floating point precision limitations, we may get values 1 less than expected.
3942
Sub TEST_IntegerConversion()
4043
Dim encodingTests As Variant
4144
Dim i As Integer
@@ -46,19 +49,19 @@ Sub TEST_IntegerConversion()
4649

4750
encodingTests = loadEncodingTestCSV()
4851

49-
For i = 0 To ${TEST_CASE_COUNTER}
52+
For i = 0 To ${TEST_CASE_MAX_INDEX}
5053
tc = encodingTests(i)
5154
degrees = tc(0)
5255
want_integer = tc(2)
5356
got_integer = latitudeToInteger(degrees)
54-
If got_integer <> want_integer Then
57+
If got_integer < want_integer - 1 Or got_integer > want_integer Then
5558
MsgBox ("Encoding test " + CStr(i) + ": latitudeToInteger(" + CStr(degrees) + "): got " + CStr(got_integer) + ", want " + CStr(want_integer))
5659
Exit Sub
5760
End If
5861
degrees = tc(1)
5962
want_integer = tc(3)
6063
got_integer = longitudeToInteger(degrees)
61-
If got_integer <> want_integer Then
64+
If got_integer < want_integer - 1 Or got_integer > want_integer Then
6265
MsgBox ("Encoding test " + CStr(i) + ": longitudeToInteger(" + CStr(degrees) + "): got " + CStr(got_integer) + ", want " + CStr(want_integer))
6366
Exit Sub
6467
End If
@@ -80,7 +83,7 @@ Sub TEST_IntegerEncoding()
8083
8184
encodingTests = loadEncodingTestCSV()
8285
83-
For i = 0 To ${TEST_CASE_COUNTER}
86+
For i = 0 To ${TEST_CASE_MAX_INDEX}
8487
tc = encodingTests(i)
8588
' Latitude and longitude are the integer values, not degrees.
8689
latitude = tc(2)
@@ -208,6 +211,7 @@ Sub TEST_OLCLibrary()
208211
MsgBox ("TEST_OLCLibrary passes")
209212
End Sub
210213

214+
' Main test function. This will call the other test functions one by one.
211215
Sub TEST_All()
212216
TEST_OLCLibrary
213217

0 commit comments

Comments
 (0)