@@ -1584,28 +1584,59 @@ func TestLoopTurningAngle(t *testing.T) {
15841584}
15851585
15861586func TestLoopAreaAndCentroid (t * testing.T ) {
1587- var p Point
1588-
1589- if got , want := EmptyLoop ().Area (), 0.0 ; got != want {
1590- t .Errorf ("EmptyLoop.Area() = %v, want %v" , got , want )
1591- }
1592- if got , want := FullLoop ().Area (), 4 * math .Pi ; got != want {
1593- t .Errorf ("FullLoop.Area() = %v, want %v" , got , want )
1594- }
1595- if got := EmptyLoop ().Centroid (); ! p .ApproxEqual (got ) {
1596- t .Errorf ("EmptyLoop.Centroid() = %v, want %v" , got , p )
1597- }
1598- if got := FullLoop ().Centroid (); ! p .ApproxEqual (got ) {
1599- t .Errorf ("FullLoop.Centroid() = %v, want %v" , got , p )
1600- }
1601-
1602- if got , want := northHemi .Area (), 2 * math .Pi ; ! float64Eq (got , want ) {
1603- t .Errorf ("northHemi.Area() = %v, want %v" , got , want )
1587+ tests := []struct {
1588+ name string
1589+ loop * Loop
1590+ wantArea float64
1591+ wantCentroid Point
1592+ }{
1593+ {
1594+ name : "EmptyLoop" ,
1595+ loop : EmptyLoop (),
1596+ wantArea : 0.0 ,
1597+ wantCentroid : Point {},
1598+ },
1599+ {
1600+ name : "FullLoop" ,
1601+ loop : FullLoop (),
1602+ wantArea : 4 * math .Pi ,
1603+ wantCentroid : Point {},
1604+ },
1605+ {
1606+ name : "northHemi" ,
1607+ loop : northHemi ,
1608+ wantArea : 2 * math .Pi ,
1609+ wantCentroid : Point {}, // Centroid of a hemisphere is (0,0,0)
1610+ },
1611+ {
1612+ name : "eastHemi" ,
1613+ loop : eastHemi ,
1614+ wantArea : 2 * math .Pi ,
1615+ wantCentroid : Point {}, // Centroid of a hemisphere is (0,0,0)
1616+ },
1617+ {
1618+ name : "lineTriangle" ,
1619+ loop : lineTriangle ,
1620+ wantArea : 0 ,
1621+ wantCentroid : Point {},
1622+ },
1623+ {
1624+ name : "twoPoints" ,
1625+ loop : LoopFromPoints ([]Point {PointFromLatLng (LatLngFromDegrees (0 , 0 )), PointFromLatLng (LatLngFromDegrees (0 , 1 ))}),
1626+ wantArea : 0 ,
1627+ wantCentroid : Point {},
1628+ },
16041629 }
16051630
1606- eastHemiArea := eastHemi .Area ()
1607- if eastHemiArea < 2 * math .Pi - 1e-12 || eastHemiArea > 2 * math .Pi + 1e-12 {
1608- t .Errorf ("eastHemi.Area() = %v, want between [%v, %v]" , eastHemiArea , 2 * math .Pi - 1e-12 , 2 * math .Pi + 1e-12 )
1631+ for _ , test := range tests {
1632+ t .Run (test .name , func (t * testing.T ) {
1633+ if got := test .loop .Area (); ! float64Near (got , test .wantArea , epsilon ) {
1634+ t .Errorf ("Area() = %v, want %v" , got , test .wantArea )
1635+ }
1636+ if got := test .loop .Centroid (); ! got .ApproxEqual (test .wantCentroid ) {
1637+ t .Errorf ("Centroid() = %v, want %v" , got , test .wantCentroid )
1638+ }
1639+ })
16091640 }
16101641
16111642 // Construct spherical caps of random height, and approximate their boundary
0 commit comments