Skip to content

Commit aec67de

Browse files
committed
Add validity tests to cover edge cases
This improves Go coverage for optimizations I want to be sure are safe.
1 parent 83986da commit aec67de

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

go/olc.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ func CheckFull(code string) error {
167167
if firstLat := strings.IndexByte(Alphabet, upper(code[0])) * int(encBase); firstLat >= latMax*2 {
168168
return errors.New("latitude outside range")
169169
}
170-
if len(code) == 1 {
171-
return nil
172-
}
173170
if firstLong := strings.IndexByte(Alphabet, upper(code[1])) * int(encBase); firstLong >= lngMax*2 {
174171
return errors.New("longitude outside range")
175172
}

go/olc_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ func TestCheck(t *testing.T) {
131131
}
132132
}
133133

134+
func TestCheckShort(t *testing.T) {
135+
for i, elt := range validity {
136+
err := CheckShort(elt.code)
137+
got := err == nil
138+
if got != elt.isShort {
139+
t.Errorf("%d. %q validity is %t (err=%v), wanted %t.", i, elt.code, got, err, elt.isShort)
140+
}
141+
}
142+
}
143+
144+
func TestCheckFull(t *testing.T) {
145+
for i, elt := range validity {
146+
err := CheckFull(elt.code)
147+
got := err == nil
148+
if got != elt.isFull {
149+
t.Errorf("%d. %q validity is %t (err=%v), wanted %t.", i, elt.code, got, err, elt.isFull)
150+
}
151+
}
152+
}
153+
154+
func TestDecodeValidity(t *testing.T) {
155+
for i, elt := range validity {
156+
_, err := Decode(elt.code)
157+
got := err == nil
158+
if got != elt.isFull {
159+
t.Errorf("%d. %q validity is %t (err=%v), wanted %t.", i, elt.code, got, err, elt.isValid)
160+
}
161+
}
162+
}
163+
134164
func TestEncodeDegrees(t *testing.T) {
135165
const allowedErrRate float64 = 0.05
136166
var badCodes int

test_data/encoding.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
20.5,2.5,2762500000,1495040000,4,7FG40000+
2525
-89.9999375,-179.9999375,1562,512,10,22222222+22
2626
0.5,179.5,2262500000,2945024000,4,6VGX0000+
27+
0.5,179.5,2262500000,2945024000,3,6VGX0000+
2728
1,1,2275000000,1482752000,11,6FH32222+222
2829
################################################################################
2930
#

test_data/validityTests.csv

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
8fwc2345+,true,false,true
1515
8FWCX400+,true,false,true
1616
84000000+,true,false,true
17+
C2000000+,true,false,true
18+
2V000000+,true,false,true
1719
################################################################################
1820
#
1921
# Valid short codes:
@@ -29,7 +31,9 @@ WC2345+G6g,true,true,false
2931
#
3032
################################################################################
3133
G+,false,false,false
34+
G,false,false,false
3235
+,false,false,false
36+
45CC+0+,false,false,false
3337
8FWC2345+G,false,false,false
3438
8FWC2_45+G6,false,false,false
3539
8FWC2η45+G6,false,false,false
@@ -40,6 +44,14 @@ WC2300+G6g,false,false,false
4044
WC2345+G,false,false,false
4145
WC2300+,false,false,false
4246
84900000+,false,false,false
47+
# Cannot start with padding
48+
049VGJQF+VX7Q,false,false,false
49+
# Missing separator
50+
849VGJQFVX7Q,false,false,false
51+
# Latitude outside range
52+
F2000000+,true,false,false
53+
# Longitude outside range
54+
2W000000+,true,false,false
4355
################################################################################
4456
#
4557
# Validate that codes at and exceeding 15 digits are still valid when all their

0 commit comments

Comments
 (0)