...

Source file src/github.com/golang/geo/s2/pointcompression_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  	"bytes"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  func TestFacesIterator(t *testing.T) {
    24  	iter := facesIterator{faces: []faceRun{{1, 1}, {2, 2}, {0, 1}}}
    25  	var got []int
    26  	for iter.next() {
    27  		got = append(got, iter.curFace)
    28  	}
    29  	want := []int{1, 2, 2, 0}
    30  	if !reflect.DeepEqual(got, want) {
    31  		t.Errorf("facesIterator produced sequence %v, want %v", got, want)
    32  	}
    33  }
    34  
    35  func makeSnappedPoints(nvertices int, level int) []Point {
    36  	const radiusKM = 0.1
    37  	center := PointFromCoords(1, 1, 1)
    38  	pts := regularPoints(center, kmToAngle(radiusKM), nvertices)
    39  	for i, pt := range pts {
    40  		id := CellFromPoint(pt).ID()
    41  		if level < id.Level() {
    42  			pts[i] = id.Parent(level).Point()
    43  		}
    44  	}
    45  	return pts
    46  }
    47  
    48  func TestPointsCompression(t *testing.T) {
    49  	loop100Mixed15 := makeSnappedPoints(100, MaxLevel)
    50  	for i := 0; i < 15; i++ {
    51  		loop100Mixed15[3*i] = CellFromPoint(loop100Mixed15[3*i]).ID().Parent(4).Point()
    52  	}
    53  
    54  	tests := []struct {
    55  		label string
    56  		pts   []Point
    57  		level int
    58  	}{
    59  		{"loop4", makeSnappedPoints(4, MaxLevel), MaxLevel},
    60  		{"loop4unsnapped", makeSnappedPoints(4, 4), MaxLevel},
    61  		// Radius is 100m, so points are about 141 meters apart.
    62  		// Snapping to level 14 will move them by < 47m.
    63  		{"loop4Level14", makeSnappedPoints(4, 14), 14},
    64  		{"loop100", makeSnappedPoints(100, MaxLevel), MaxLevel},
    65  		{"loop100Unsnapped", makeSnappedPoints(100, 100), MaxLevel},
    66  		{"loop100Mixed15", loop100Mixed15, MaxLevel},
    67  	}
    68  
    69  NextTest:
    70  	for _, tt := range tests {
    71  		sitis := (&Loop{vertices: tt.pts}).xyzFaceSiTiVertices()
    72  		var buf bytes.Buffer
    73  		e := &encoder{w: &buf}
    74  		encodePointsCompressed(e, sitis, tt.level)
    75  		if e.err != nil {
    76  			t.Errorf("encodePointsCompressed (%s): %v", tt.label, e.err)
    77  			continue
    78  		}
    79  
    80  		d := &decoder{r: &buf}
    81  		got := make([]Point, len(tt.pts))
    82  		decodePointsCompressed(d, tt.level, got)
    83  		if d.err != nil {
    84  			t.Errorf("decodePointsCompressed (%s): %v", tt.label, d.err)
    85  		}
    86  
    87  		for i, want := range tt.pts {
    88  			if !got[i].ApproxEqual(want) {
    89  				t.Errorf("decodePointsCompressed(encodePointsCompressed) for %s at %d: got %v want %v", tt.label, i, got[i], want)
    90  				continue NextTest
    91  			}
    92  		}
    93  	}
    94  }
    95  
    96  func TestZigzag(t *testing.T) {
    97  	tab := []struct {
    98  		signed   int32
    99  		unsigned uint32
   100  	}{
   101  		{0, 0}, {-1, 1}, {1, 2}, {-2, 3}, {2147483647, 4294967294}, {-2147483648, 4294967295},
   102  	}
   103  	for _, tt := range tab {
   104  		if s := zigzagDecode(tt.unsigned); s != tt.signed {
   105  			t.Errorf("zigzagDecode(%d) = %d, want %d", tt.unsigned, s, tt.signed)
   106  		}
   107  		if u := zigzagEncode(tt.signed); u != tt.unsigned {
   108  			t.Errorf("zigzagEncode(%d) = %d, want %d", tt.signed, u, tt.unsigned)
   109  		}
   110  	}
   111  }
   112  

View as plain text