Skip to content

Commit 4547674

Browse files
authored
Extend fuzz tests (#196)
Cover more methods for CellUnion and add a rudimentary fuzz test for polygon.
1 parent 39f9059 commit 4547674

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

s2/cellunion_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package s2
1616

1717
import (
18+
"bytes"
1819
"math"
1920
"math/rand"
2021
"reflect"
@@ -1065,3 +1066,39 @@ func BenchmarkCellUnionFromRange(b *testing.B) {
10651066
CellUnionFromRange(x, y)
10661067
}
10671068
}
1069+
1070+
// go test -fuzz=FuzzDecodeCellUnion github.com/golang/geo/s2
1071+
func FuzzDecodeCellUnion(f *testing.F) {
1072+
cu := CellUnion([]CellID{
1073+
CellID(0x33),
1074+
CellID(0x8e3748fab),
1075+
CellID(0x91230abcdef83427),
1076+
})
1077+
buf := new(bytes.Buffer)
1078+
if err := cu.Encode(buf); err != nil {
1079+
f.Errorf("error encoding %v: ", err)
1080+
}
1081+
f.Add(buf.Bytes())
1082+
1083+
f.Fuzz(func(t *testing.T, encoded []byte) {
1084+
var c CellUnion
1085+
if err := c.Decode(bytes.NewReader(encoded)); err != nil {
1086+
// Construction failed, no need to test further.
1087+
return
1088+
}
1089+
if got := c.ApproxArea(); got < 0 {
1090+
t.Errorf("ApproxArea() = %v, want >= 0. CellUnion: %v", got, c)
1091+
}
1092+
buf := new(bytes.Buffer)
1093+
if err := c.Encode(buf); err != nil {
1094+
// Re-encoding the cell union does not necessarily produce the same bytes, as there could be additional bytes following after n cells were read.
1095+
t.Errorf("encode() = %v. got %v, want %v. CellUnion: %v", err, buf.Bytes(), encoded, c)
1096+
}
1097+
if c.IsValid() {
1098+
c.Normalize()
1099+
if !c.IsNormalized() {
1100+
t.Errorf("IsNormalized() = false, want true. CellUnion: %v", c)
1101+
}
1102+
}
1103+
})
1104+
}

s2/encode_fuzz_test.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

s2/polygon_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package s2
1616

1717
import (
18+
"bytes"
1819
"math"
1920
"math/rand"
2021
"testing"
@@ -1205,3 +1206,26 @@ func TestPolygonInvert(t *testing.T) {
12051206
// TestNarrowGapRemoved
12061207
// TestCloselySpacedEdgeVerticesKept
12071208
// TestPolylineAssemblyBug
1209+
1210+
// go test -fuzz=FuzzDecodePolygon github.com/golang/geo/s2
1211+
func FuzzDecodePolygon(f *testing.F) {
1212+
for _, p := range []*Polygon{near0Polygon, near01Polygon, near30Polygon, near23Polygon, far01Polygon, far21Polygon, south0abPolygon} {
1213+
buf := new(bytes.Buffer)
1214+
if err := p.Encode(buf); err != nil {
1215+
f.Errorf("error encoding %v: ", err)
1216+
}
1217+
f.Add(buf.Bytes())
1218+
}
1219+
1220+
f.Fuzz(func(t *testing.T, encoded []byte) {
1221+
p := &Polygon{}
1222+
if err := p.Decode(bytes.NewReader(encoded)); err != nil {
1223+
// Construction failed, no need to test further.
1224+
return
1225+
}
1226+
if got := p.Area(); got < 0 {
1227+
t.Errorf("Area() = %v, want >= 0. Polygon: %v", got, p)
1228+
}
1229+
// TODO: Test more methods on Polygon.
1230+
})
1231+
}

0 commit comments

Comments
 (0)