...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2020 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  
    21  // allEdgesInShapeIndex returns the full list of edges in the given index in shapeID order.
    22  func allEdgesInShapeIndex(index *ShapeIndex) []Edge {
    23  	var result []Edge
    24  	// Iterator works over the shapes in shape ID order, so we don't just
    25  	// range over the map here because order is not guaranteed.
    26  	for i := 0; i < len(index.shapes); i++ {
    27  		shape := index.shapes[int32(i)]
    28  		if shape == nil {
    29  			continue
    30  		}
    31  		for j := 0; j < shape.NumEdges(); j++ {
    32  			result = append(result, shape.Edge(j))
    33  		}
    34  	}
    35  	return result
    36  }
    37  
    38  func verifyEdgeIterator(t *testing.T, index *ShapeIndex) {
    39  	expected := allEdgesInShapeIndex(index)
    40  	i := 0
    41  
    42  	for iter := NewEdgeIterator(index); !iter.Done(); iter.Next() {
    43  		if got, want := iter.Edge(), expected[i]; got != want {
    44  			t.Errorf("edge[%d] = %v, want %v", i, got, want)
    45  		}
    46  		i++
    47  	}
    48  }
    49  
    50  func TestShapeutilEdgeIteratorEmpty(t *testing.T) {
    51  	index := makeShapeIndex("##")
    52  	verifyEdgeIterator(t, index)
    53  }
    54  
    55  func TestShapeutilEdgeIteratorPoints(t *testing.T) {
    56  	index := makeShapeIndex("0:0|1:1##")
    57  	verifyEdgeIterator(t, index)
    58  }
    59  
    60  func TestShapeutilEdgeIteratorLines(t *testing.T) {
    61  	index := makeShapeIndex("#0:0,10:10|5:5,5:10|1:2,2:1#")
    62  	verifyEdgeIterator(t, index)
    63  }
    64  
    65  func TestShapeutilEdgeIteratorPolygons(t *testing.T) {
    66  	index := makeShapeIndex("##10:10,10:0,0:0|-10:-10,-10:0,0:0,0:-10")
    67  	verifyEdgeIterator(t, index)
    68  }
    69  
    70  func TestShapeutilEdgeIteratorCollection(t *testing.T) {
    71  	index := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
    72  	verifyEdgeIterator(t, index)
    73  }
    74  
    75  func TestShapeutilEdgeIteratorRemove(t *testing.T) {
    76  	index := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
    77  	index.Remove(index.Shape(0))
    78  
    79  	verifyEdgeIterator(t, index)
    80  }
    81  
    82  // edgeIteratorEq reports if two edge iterators are the same.
    83  func edgeIteratorEq(a, b *EdgeIterator) bool {
    84  	return a.shapeID == b.shapeID && a.edgeID == b.edgeID && a.index == b.index
    85  }
    86  
    87  // copyEdgeIterator copies the state of the given iterator into a new instance.
    88  func copyEdgeIterator(a *EdgeIterator) *EdgeIterator {
    89  	return &EdgeIterator{
    90  		index:    a.index,
    91  		shapeID:  a.shapeID,
    92  		edgeID:   a.edgeID,
    93  		numEdges: a.numEdges,
    94  	}
    95  }
    96  
    97  func TestShapeutilEdgeIteratorAssignmentAndEquality(t *testing.T) {
    98  	index1 := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
    99  
   100  	index2 := makeShapeIndex("1:1|7:2#1:1,2:2,3:3|2:2,1:7#10:10,10:0,0:0;20:20,20:10,10:10|15:15,15:0,0:0")
   101  
   102  	it1 := NewEdgeIterator(index1)
   103  	it2 := NewEdgeIterator(index2)
   104  
   105  	// The underlying indexes have the same data, but are not the same index.
   106  	if edgeIteratorEq(it1, it2) {
   107  		t.Errorf("edgeIterators equal but shouldn't be")
   108  	}
   109  
   110  	it1 = copyEdgeIterator(it2)
   111  	if !edgeIteratorEq(it1, it2) {
   112  		t.Errorf("edgeIterators not equal but should be")
   113  	}
   114  
   115  	it1.Next()
   116  	if edgeIteratorEq(it1, it2) {
   117  		t.Errorf("edgeIterators equal but shouldn't be after one is advanced. \n1: %+v\n2: %+v", it1, it2)
   118  	}
   119  
   120  	it2.Next()
   121  	if !edgeIteratorEq(it1, it2) {
   122  		t.Errorf("edgeIterators not equal but should be after both advanced same amount")
   123  	}
   124  }
   125  

View as plain text