...

Source file src/github.com/golang/geo/s2/shapeutil_test.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2017 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package s2
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/golang/geo/s1"
    21  )
    22  
    23  func TestShapeutilContainsBruteForceNoInterior(t *testing.T) {
    24  	// Defines a polyline that almost entirely encloses the point 0:0.
    25  	polyline := makeLaxPolyline("0:0, 0:1, 1:-1, -1:-1, -1e9:1")
    26  	point := parsePoint("0:0")
    27  	if containsBruteForce(polyline, point) {
    28  		t.Errorf("containsBruteForce(%v, %v) = true, want false", polyline, point)
    29  	}
    30  }
    31  
    32  func TestShapeutilContainsBruteForceContainsReferencePoint(t *testing.T) {
    33  	// Checks that containsBruteForce agrees with ReferencePoint.
    34  	polygon := makeLaxPolygon("0:0, 0:1, 1:-1, -1:-1, -1e9:1")
    35  	ref := polygon.ReferencePoint()
    36  	if got := containsBruteForce(polygon, ref.Point); got != ref.Contained {
    37  		t.Errorf("containsBruteForce(%v, %v) = %v, want %v", polygon, ref.Point, got, ref.Contained)
    38  	}
    39  }
    40  
    41  func TestShapeutilContainsBruteForceConsistentWithLoop(t *testing.T) {
    42  	// Checks that containsBruteForce agrees with Loop Contains.
    43  	loop := RegularLoop(parsePoint("89:-179"), s1.Angle(10)*s1.Degree, 100)
    44  	for i := 0; i < loop.NumVertices(); i++ {
    45  		if got, want := loop.ContainsPoint(loop.Vertex(i)),
    46  			containsBruteForce(loop, loop.Vertex(i)); got != want {
    47  			t.Errorf("loop.ContainsPoint(%v) = %v, containsBruteForce(shape, %v) = %v, should be the same", loop.Vertex(i), got, loop.Vertex(i), want)
    48  		}
    49  	}
    50  }
    51  
    52  func TestShapeutilRangeIteratorNext(t *testing.T) {
    53  	// Create an index with one point each on CellID faces 0, 1, and 2.
    54  	index := makeShapeIndex("0:0 | 0:90 | 90:0 # #")
    55  	it := newRangeIterator(index)
    56  
    57  	if got, want := it.cellID().Face(), 0; got != want {
    58  		t.Errorf("it.CellID().Face() = %v, want %v", got, want)
    59  	}
    60  	it.next()
    61  
    62  	if got, want := it.cellID().Face(), 1; got != want {
    63  		t.Errorf("it.CellID().Face() = %v, want %v", got, want)
    64  	}
    65  	it.next()
    66  
    67  	if got, want := it.cellID().Face(), 2; got != want {
    68  		t.Errorf("it.CellID().Face() = %v, want %v", got, want)
    69  	}
    70  	it.next()
    71  
    72  	if !it.done() {
    73  		t.Errorf("iterator over index of three items should be done after 3 calls to next")
    74  	}
    75  }
    76  
    77  func TestShapeutilRangeIteratorEmptyIndex(t *testing.T) {
    78  	empty := makeShapeIndex("# #")
    79  	nonEmpty := makeShapeIndex("0:0 # #")
    80  
    81  	emptyIter := newRangeIterator(empty)
    82  	nonEmptyIter := newRangeIterator(nonEmpty)
    83  
    84  	if !emptyIter.done() {
    85  		t.Errorf("the rangeIterator on an empty ShapeIndex should be done at creation")
    86  	}
    87  
    88  	emptyIter.seekTo(nonEmptyIter)
    89  	if !emptyIter.done() {
    90  		t.Errorf("seeking in the range iterator on an empty index to a cell should hit the end")
    91  	}
    92  
    93  	emptyIter.seekBeyond(nonEmptyIter)
    94  	if !emptyIter.done() {
    95  		t.Errorf("seeking in the range iterator on an empty index beyond a cell should hit the end")
    96  	}
    97  
    98  	emptyIter.seekTo(emptyIter)
    99  	if !emptyIter.done() {
   100  		t.Errorf("seeking in the range iterator on an empty index to a its current position should hit the end")
   101  	}
   102  
   103  	emptyIter.seekBeyond(emptyIter)
   104  	if !emptyIter.done() {
   105  		t.Errorf("seeking in the range iterator on an empty index beyond itself should hit the end")
   106  	}
   107  }
   108  

View as plain text