Skip to content

Commit a3f1189

Browse files
rsneddsymonds
authored andcommitted
s2: Add some CellUnion construction test helpers.
Signed-off-by: David Symonds <dsymonds@golang.org>
1 parent d57a190 commit a3f1189

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

s2/s2_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ func randomCellID() CellID {
129129
return randomCellIDForLevel(randomUniformInt(maxLevel + 1))
130130
}
131131

132+
// randomCellUnion returns a CellUnion of the given size of randomly selected cells.
133+
func randomCellUnion(n int) CellUnion {
134+
var cu CellUnion
135+
for i := 0; i < n; i++ {
136+
cu = append(cu, randomCellID())
137+
}
138+
return cu
139+
}
140+
132141
// concentricLoopsPolygon constructs a polygon with the specified center as a
133142
// number of concentric loops and vertices per loop.
134143
func concentricLoopsPolygon(center Point, numLoops, verticesPerLoop int) *Polygon {

s2/textformat_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ func makeRect(s string) Rect {
143143
return rect
144144
}
145145

146+
// makeCellUnion returns a CellUnion from the given CellID token strings.
147+
func makeCellUnion(tokens ...string) CellUnion {
148+
var cu CellUnion
149+
150+
for _, t := range tokens {
151+
cu = append(cu, cellIDFromString(t))
152+
}
153+
return cu
154+
}
155+
146156
// makeLoop constructs a Loop from the input string.
147157
// The strings "empty" or "full" create an empty or full loop respectively.
148158
func makeLoop(s string) *Loop {

s2/textformat_test_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,42 @@ func TestTextFormatParseRect(t *testing.T) {
313313
}
314314
}
315315

316+
func TestTextFormatMakeCellUnion(t *testing.T) {
317+
tests := []struct {
318+
have []string
319+
want CellUnion
320+
}{
321+
{
322+
have: nil,
323+
want: CellUnion{},
324+
},
325+
{
326+
have: []string{},
327+
want: CellUnion{},
328+
},
329+
{
330+
have: []string{"google"},
331+
want: CellUnion{0},
332+
},
333+
{
334+
have: []string{"0/"},
335+
want: CellUnion{CellIDFromFace(0)},
336+
},
337+
{
338+
have: []string{"2/010", "2/011", "2/02"},
339+
want: CellUnion{0x4240000000000000, 0x42c0000000000000, 0x4500000000000000},
340+
},
341+
}
342+
343+
for _, test := range tests {
344+
got := makeCellUnion(test.have...)
345+
got.Normalize()
346+
if !got.Equal(test.want) {
347+
t.Errorf("makeCellUnion(%v) = %v, want %v", test.have, got, test.want)
348+
}
349+
}
350+
}
351+
316352
func TestTextFormatMakeLaxPolyline(t *testing.T) {
317353
l := makeLaxPolyline("-20:150, -20:151, -19:150")
318354

0 commit comments

Comments
 (0)