|
| 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 | +func TestEmptyRegionUnionHasEmptyCap(t *testing.T) { |
| 22 | + var empty RegionUnion |
| 23 | + |
| 24 | + got := empty.CapBound() |
| 25 | + |
| 26 | + want := EmptyCap() |
| 27 | + if !got.ApproxEqual(want) { |
| 28 | + t.Errorf("empty region union cap = %v, want %v", got, want) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestEmptyRegionUnionHasEmptyBound(t *testing.T) { |
| 33 | + var empty RegionUnion |
| 34 | + |
| 35 | + got := empty.RectBound() |
| 36 | + |
| 37 | + want := EmptyRect() |
| 38 | + if !got.ApproxEqual(want) { |
| 39 | + t.Errorf("empty region union cap = %v, want %v", got, want) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestRegionUnionOfTwoPointsHasCorrectBound(t *testing.T) { |
| 44 | + got := twoPointsRegionUnion.RectBound() |
| 45 | + |
| 46 | + want := makeRect("-35:-40,35:40") |
| 47 | + if !got.ApproxEqual(want) { |
| 48 | + t.Errorf("%v.RectBound() = %v, want %v", twoPointsRegionUnion, got, want) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +var twoPointsRegionUnion = RegionUnion{ |
| 53 | + PointFromLatLng(LatLngFromDegrees(35, 40)), |
| 54 | + PointFromLatLng(LatLngFromDegrees(-35, -40)), |
| 55 | +} |
| 56 | + |
| 57 | +func TestRegionUnionOfTwoPointsIntersectsFace0(t *testing.T) { |
| 58 | + got := twoPointsRegionUnion.IntersectsCell(face0Cell) |
| 59 | + |
| 60 | + if !got { |
| 61 | + t.Errorf("%v.IntersectsCell(%v) = %v, want true", twoPointsRegionUnion, face0Cell, got) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestRegionUnionOfTwoPointsDoesNotContainFace0(t *testing.T) { |
| 66 | + got := twoPointsRegionUnion.ContainsCell(face0Cell) |
| 67 | + |
| 68 | + if got { |
| 69 | + t.Errorf("%v.ContainsCell(%v) = %v, want false", twoPointsRegionUnion, face0Cell, got) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +var face0Cell = CellFromCellID(CellIDFromFace(0)) |
| 74 | + |
| 75 | +func TestRegionUnionOfTwoContainsPoint(t *testing.T) { |
| 76 | + testCases := []struct { |
| 77 | + ll LatLng |
| 78 | + want bool |
| 79 | + }{ |
| 80 | + {LatLngFromDegrees(35, 40), true}, |
| 81 | + {LatLngFromDegrees(-35, -40), true}, |
| 82 | + {LatLngFromDegrees(0, 0), false}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tc := range testCases { |
| 86 | + got := twoPointsRegionUnion.ContainsPoint(PointFromLatLng(tc.ll)) |
| 87 | + |
| 88 | + if got != tc.want { |
| 89 | + t.Errorf("%v.ContainsPoint(%v) = %t, want %t", twoPointsRegionUnion, tc.ll, got, tc.want) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestTwoPointsRegionCovering(t *testing.T) { |
| 95 | + cov := NewRegionCoverer() |
| 96 | + cov.MaxCells = 1 |
| 97 | + |
| 98 | + got := cov.Covering(twoPointsRegionUnion) |
| 99 | + |
| 100 | + const wantLen = 1 |
| 101 | + if l := len(got); l != wantLen { |
| 102 | + t.Fatalf("covering = %v, len %v, want %v", got, l, wantLen) |
| 103 | + } |
| 104 | + want := CellIDFromFace(0) |
| 105 | + if g := got[0]; g != want { |
| 106 | + t.Errorf("covering[0] = %v, want %v", g, want) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Make sure RegionUnion implements Region. |
| 111 | +var _ Region = RegionUnion{} |
0 commit comments