Skip to content

Commit

Permalink
s2: Add some CellUnion construction test helpers.
Browse files Browse the repository at this point in the history
Signed-off-by: David Symonds <[email protected]>
  • Loading branch information
rsned authored and dsymonds committed Mar 19, 2020
1 parent d57a190 commit a3f1189
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions s2/s2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ func randomCellID() CellID {
return randomCellIDForLevel(randomUniformInt(maxLevel + 1))
}

// randomCellUnion returns a CellUnion of the given size of randomly selected cells.
func randomCellUnion(n int) CellUnion {
var cu CellUnion
for i := 0; i < n; i++ {
cu = append(cu, randomCellID())
}
return cu
}

// concentricLoopsPolygon constructs a polygon with the specified center as a
// number of concentric loops and vertices per loop.
func concentricLoopsPolygon(center Point, numLoops, verticesPerLoop int) *Polygon {
Expand Down
10 changes: 10 additions & 0 deletions s2/textformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ func makeRect(s string) Rect {
return rect
}

// makeCellUnion returns a CellUnion from the given CellID token strings.
func makeCellUnion(tokens ...string) CellUnion {
var cu CellUnion

for _, t := range tokens {
cu = append(cu, cellIDFromString(t))
}
return cu
}

// makeLoop constructs a Loop from the input string.
// The strings "empty" or "full" create an empty or full loop respectively.
func makeLoop(s string) *Loop {
Expand Down
36 changes: 36 additions & 0 deletions s2/textformat_test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,42 @@ func TestTextFormatParseRect(t *testing.T) {
}
}

func TestTextFormatMakeCellUnion(t *testing.T) {
tests := []struct {
have []string
want CellUnion
}{
{
have: nil,
want: CellUnion{},
},
{
have: []string{},
want: CellUnion{},
},
{
have: []string{"google"},
want: CellUnion{0},
},
{
have: []string{"0/"},
want: CellUnion{CellIDFromFace(0)},
},
{
have: []string{"2/010", "2/011", "2/02"},
want: CellUnion{0x4240000000000000, 0x42c0000000000000, 0x4500000000000000},
},
}

for _, test := range tests {
got := makeCellUnion(test.have...)
got.Normalize()
if !got.Equal(test.want) {
t.Errorf("makeCellUnion(%v) = %v, want %v", test.have, got, test.want)
}
}
}

func TestTextFormatMakeLaxPolyline(t *testing.T) {
l := makeLaxPolyline("-20:150, -20:151, -19:150")

Expand Down

0 comments on commit a3f1189

Please sign in to comment.