@@ -17,6 +17,7 @@ package olc
17
17
import (
18
18
"bufio"
19
19
"encoding/csv"
20
+ "fmt"
20
21
"math"
21
22
"math/rand"
22
23
"os"
@@ -41,9 +42,10 @@ type (
41
42
}
42
43
43
44
encodingTest struct {
44
- lat , lng float64
45
- length int
46
- code string
45
+ latDeg , lngDeg float64
46
+ latInt , lngInt int64
47
+ length int
48
+ code string
47
49
}
48
50
49
51
decodingTest struct {
@@ -80,10 +82,12 @@ func init() {
80
82
defer wg .Done ()
81
83
for _ , cols := range mustReadLines ("encoding.csv" ) {
82
84
encoding = append (encoding , encodingTest {
83
- lat : mustFloat (cols [0 ]),
84
- lng : mustFloat (cols [1 ]),
85
- length : mustInt (cols [2 ]),
86
- code : cols [3 ],
85
+ latDeg : mustFloat (cols [0 ]),
86
+ lngDeg : mustFloat (cols [1 ]),
87
+ latInt : mustInt64 (cols [2 ]),
88
+ lngInt : mustInt64 (cols [3 ]),
89
+ length : mustInt (cols [4 ]),
90
+ code : cols [5 ],
87
91
})
88
92
}
89
93
}()
@@ -127,12 +131,39 @@ func TestCheck(t *testing.T) {
127
131
}
128
132
}
129
133
130
- func TestEncode (t * testing.T ) {
134
+ func TestEncodeDegrees (t * testing.T ) {
135
+ const allowedErrRate float64 = 0.05
136
+ var badCodes int
131
137
for i , elt := range encoding {
132
- got := Encode (elt .lat , elt .lng , elt .length )
138
+ got := Encode (elt .latDeg , elt .lngDeg , elt .length )
133
139
if got != elt .code {
134
- t .Errorf ("%d. got %q for (%v,%v,%d), wanted %q." , i , got , elt .lat , elt .lng , elt .length , elt .code )
135
- t .FailNow ()
140
+ fmt .Printf ("ENCODING DIFFERENCE %d. got %q for Encode(%v,%v,%d), wanted %q\n " , i , got , elt .latDeg , elt .lngDeg , elt .length , elt .code )
141
+ badCodes ++
142
+ }
143
+ }
144
+ if errRate := float64 (badCodes ) / float64 (len (encoding )); errRate > allowedErrRate {
145
+ t .Errorf ("Too many errors in encoding degrees (got %f, allowed %f)" , errRate , allowedErrRate )
146
+ }
147
+ }
148
+
149
+ func TestEncodeIntegers (t * testing.T ) {
150
+ for i , elt := range encoding {
151
+ got := integerEncode (elt .latInt , elt .lngInt , elt .length )
152
+ if got != elt .code {
153
+ t .Errorf ("%d. got %q for integerEncode(%v,%v,%d), wanted %q" , i , got , elt .latInt , elt .lngInt , elt .length , elt .code )
154
+ }
155
+ }
156
+ }
157
+
158
+ func TestConvertDegrees (t * testing.T ) {
159
+ for i , elt := range encoding {
160
+ got := latitudeAsInteger (elt .latDeg )
161
+ if got > elt .latInt || got < elt .latInt - 1 {
162
+ t .Errorf ("%d. got %d for latitudeAsInteger(%v), wanted %d" , i , got , elt .latDeg , elt .latInt )
163
+ }
164
+ got = longitudeAsInteger (elt .lngDeg )
165
+ if got > elt .lngInt || got < elt .lngInt - 1 {
166
+ t .Errorf ("%d. got %d for longitudeAsInteger(%v), wanted %d" , i , got , elt .lngDeg , elt .lngInt )
136
167
}
137
168
}
138
169
}
@@ -212,6 +243,14 @@ func mustInt(a string) int {
212
243
return f
213
244
}
214
245
246
+ func mustInt64 (a string ) int64 {
247
+ f , err := strconv .ParseInt (a , 10 , 64 )
248
+ if err != nil {
249
+ panic (err )
250
+ }
251
+ return f
252
+ }
253
+
215
254
func TestFuzzCrashers (t * testing.T ) {
216
255
for i , code := range []string {
217
256
"975722X9+88X29qqX297" +
0 commit comments