github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/geo/geographiclib/geographiclib_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package geographiclib
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/golang/geo/s2"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestInverse(t *testing.T) {
    21  	testCases := []struct {
    22  		desc     string
    23  		spheroid Spheroid
    24  		a, b     s2.LatLng
    25  
    26  		s12, az1, az2 float64
    27  	}{
    28  		{
    29  			desc:     "{0,0}, {1,1} on WGS84Spheroid",
    30  			spheroid: WGS84Spheroid,
    31  			a:        s2.LatLngFromDegrees(0, 0),
    32  			b:        s2.LatLngFromDegrees(1, 1),
    33  			s12:      156899.56829134029,
    34  			az1:      45.188040229358869,
    35  			az2:      45.196767321644863,
    36  		},
    37  	}
    38  	for _, tc := range testCases {
    39  		t.Run(tc.desc, func(t *testing.T) {
    40  			s12, az1, az2 := tc.spheroid.Inverse(tc.a, tc.b)
    41  			require.Equal(t, tc.s12, s12)
    42  			require.Equal(t, tc.az1, az1)
    43  			require.Equal(t, tc.az2, az2)
    44  		})
    45  	}
    46  }
    47  
    48  func TestInverseBatch(t *testing.T) {
    49  	testCases := []struct {
    50  		desc     string
    51  		spheroid Spheroid
    52  		points   []s2.Point
    53  		sum      float64
    54  	}{
    55  		{
    56  			desc:     "WKT from Wikipedia",
    57  			spheroid: WGS84Spheroid,
    58  			points: []s2.Point{
    59  				s2.PointFromLatLng(s2.LatLngFromDegrees(40, 40)),
    60  				s2.PointFromLatLng(s2.LatLngFromDegrees(45, 20)),
    61  				s2.PointFromLatLng(s2.LatLngFromDegrees(30, 45)),
    62  			},
    63  			sum: 4.477956179118822e+06,
    64  		},
    65  	}
    66  
    67  	for _, tc := range testCases {
    68  		t.Run(tc.desc, func(t *testing.T) {
    69  			sum := tc.spheroid.InverseBatch(tc.points)
    70  			require.Equal(t, tc.sum, sum)
    71  		})
    72  	}
    73  }
    74  
    75  func TestAreaAndPerimeter(t *testing.T) {
    76  	testCases := []struct {
    77  		desc      string
    78  		spheroid  Spheroid
    79  		points    []s2.Point
    80  		area      float64
    81  		perimeter float64
    82  	}{
    83  		{
    84  			desc:     "WKT from Wikipedia",
    85  			spheroid: WGS84Spheroid,
    86  			points: []s2.Point{
    87  				s2.PointFromLatLng(s2.LatLngFromDegrees(40, 40)),
    88  				s2.PointFromLatLng(s2.LatLngFromDegrees(45, 20)),
    89  				s2.PointFromLatLng(s2.LatLngFromDegrees(30, 45)),
    90  			},
    91  			area:      6.91638769184179e+11,
    92  			perimeter: 5.6770339842410665e+06,
    93  		},
    94  	}
    95  
    96  	for _, tc := range testCases {
    97  		t.Run(tc.desc, func(t *testing.T) {
    98  			area, perimeter := tc.spheroid.AreaAndPerimeter(tc.points)
    99  			require.Equal(t, tc.area, area)
   100  			require.Equal(t, tc.perimeter, perimeter)
   101  		})
   102  	}
   103  }