github.com/liucxer/courier@v1.7.1/h3/bbox_test.go (about) 1 package h3 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 ) 8 9 func assertBBox(t *testing.T, geofence *Geofence, expected *BBox, inside *GeoCoord, outside *GeoCoord) { 10 var result BBox 11 bboxFrom(geofence, &result) 12 require.True(t, bboxEquals(&result, expected), "Got expected bbox") 13 require.True(t, bboxContains(&result, inside), "Contains expected inside point") 14 require.True(t, !bboxContains(&result, outside), 15 "Does not contain expected outside point") 16 } 17 18 func Test_posLatPosLon(t *testing.T) { 19 verts := []GeoCoord{{0.8, 0.3}, {0.7, 0.6}, {1.1, 0.7}, {1.0, 0.2}} 20 geofence := Geofence{4, verts} 21 expected := BBox{1.1, 0.7, 0.7, 0.2} 22 inside := GeoCoord{0.9, 0.4} 23 outside := GeoCoord{0.0, 0.0} 24 assertBBox(t, &geofence, &expected, &inside, &outside) 25 } 26 27 func Test_negLatPosLon(t *testing.T) { 28 verts := []GeoCoord{{-0.3, 0.6}, {-0.4, 0.9}, {-0.2, 0.8}, {-0.1, 0.6}} 29 geofence := Geofence{4, verts} 30 expected := BBox{-0.1, -0.4, 0.9, 0.6} 31 inside := GeoCoord{-0.3, 0.8} 32 outside := GeoCoord{0.0, 0.0} 33 assertBBox(t, &geofence, &expected, &inside, &outside) 34 } 35 36 func Test_posLatNegLon(t *testing.T) { 37 verts := []GeoCoord{{0.7, -1.4}, {0.8, -0.9}, {1.0, -0.8}, {1.1, -1.3}} 38 geofence := Geofence{4, verts} 39 expected := BBox{1.1, 0.7, -0.8, -1.4} 40 inside := GeoCoord{0.9, -1.0} 41 outside := GeoCoord{0.0, 0.0} 42 assertBBox(t, &geofence, &expected, &inside, &outside) 43 } 44 45 func Test_negLatNegLon(t *testing.T) { 46 verts := []GeoCoord{ 47 {-0.4, -1.4}, {-0.3, -1.1}, {-0.1, -1.2}, {-0.2, -1.4}} 48 geofence := Geofence{4, verts} 49 expected := BBox{-0.1, -0.4, -1.1, -1.4} 50 inside := GeoCoord{-0.3, -1.2} 51 outside := GeoCoord{0.0, 0.0} 52 assertBBox(t, &geofence, &expected, &inside, &outside) 53 } 54 55 func Test_aroundZeroZero(t *testing.T) { 56 verts := []GeoCoord{{0.4, -0.4}, {0.4, 0.4}, {-0.4, 0.4}, {-0.4, -0.4}} 57 geofence := Geofence{4, verts} 58 expected := BBox{0.4, -0.4, 0.4, -0.4} 59 inside := GeoCoord{-0.1, -0.1} 60 outside := GeoCoord{1.0, -1.0} 61 assertBBox(t, &geofence, &expected, &inside, &outside) 62 } 63 64 func Test_transmeridian(t *testing.T) { 65 verts := []GeoCoord{{0.4, M_PI - 0.1}, 66 {0.4, -M_PI + 0.1}, 67 {-0.4, -M_PI + 0.1}, 68 {-0.4, M_PI - 0.1}} 69 geofence := Geofence{4, verts} 70 expected := BBox{0.4, -0.4, -M_PI + 0.1, M_PI - 0.1} 71 insideOnMeridian := GeoCoord{-0.1, M_PI} 72 outside := GeoCoord{1.0, M_PI - 0.5} 73 assertBBox(t, &geofence, &expected, &insideOnMeridian, &outside) 74 westInside := GeoCoord{0.1, M_PI - 0.05} 75 require.True(t, bboxContains(&expected, &westInside), 76 "Contains expected west inside point") 77 eastInside := GeoCoord{0.1, -M_PI + 0.05} 78 require.True(t, bboxContains(&expected, &eastInside), 79 "Contains expected east outside point") 80 westOutside := GeoCoord{0.1, M_PI - 0.5} 81 require.True(t, !bboxContains(&expected, &westOutside), 82 "Does not contain expected west outside point") 83 eastOutside := GeoCoord{0.1, -M_PI + 0.5} 84 require.True(t, !bboxContains(&expected, &eastOutside), 85 "Does not contain expected east outside point") 86 } 87 88 func Test_edgeOnNorthPole(t *testing.T) { 89 verts := []GeoCoord{{M_PI_2 - 0.1, 0.1}, 90 {M_PI_2 - 0.1, 0.8}, 91 {M_PI_2, 0.8}, 92 {M_PI_2, 0.1}} 93 geofence := Geofence{4, verts} 94 expected := BBox{M_PI_2, M_PI_2 - 0.1, 0.8, 0.1} 95 inside := GeoCoord{M_PI_2 - 0.01, 0.4} 96 outside := GeoCoord{M_PI_2, 0.9} 97 assertBBox(t, &geofence, &expected, &inside, &outside) 98 } 99 100 func Test_edgeOnSouthPole(t *testing.T) { 101 verts := []GeoCoord{{-M_PI_2 + 0.1, 0.1}, 102 {-M_PI_2 + 0.1, 0.8}, 103 {-M_PI_2, 0.8}, 104 {-M_PI_2, 0.1}} 105 geofence := Geofence{4, verts} 106 expected := BBox{-M_PI_2 + 0.1, -M_PI_2, 0.8, 0.1} 107 inside := GeoCoord{-M_PI_2 + 0.01, 0.4} 108 outside := GeoCoord{-M_PI_2, 0.9} 109 assertBBox(t, &geofence, &expected, &inside, &outside) 110 } 111 112 func Test_containsEdges(t *testing.T) { 113 bbox := BBox{0.1, -0.1, 0.2, -0.2} 114 points := []GeoCoord{ 115 {0.1, 0.2}, {0.1, 0.0}, {0.1, -0.2}, {0.0, 0.2}, 116 {-0.1, 0.2}, {-0.1, 0.0}, {-0.1, -0.2}, {0.0, -0.2}, 117 } 118 numPoints := 8 119 for i := 0; i < numPoints; i++ { 120 require.True(t, bboxContains(&bbox, &points[i]), "Contains edge point") 121 } 122 } 123 124 func Test_containsEdgesTransmeridian(t *testing.T) { 125 bbox := BBox{0.1, -0.1, -M_PI + 0.2, M_PI - 0.2} 126 points := []GeoCoord{ 127 {0.1, -M_PI + 0.2}, {0.1, M_PI}, {0.1, M_PI - 0.2}, 128 {0.0, -M_PI + 0.2}, {-0.1, -M_PI + 0.2}, {-0.1, M_PI}, 129 {-0.1, M_PI - 0.2}, {0.0, M_PI - 0.2}, 130 } 131 numPoints := 8 132 for i := 0; i < numPoints; i++ { 133 require.True(t, bboxContains(&bbox, &points[i]), "Contains transmeridian edge point") 134 } 135 } 136 137 func Test_bboxCenterBasicQuandrants(t *testing.T) { 138 var center GeoCoord 139 bbox1 := BBox{1.0, 0.8, 1.0, 0.8} 140 expected1 := GeoCoord{0.9, 0.9} 141 bboxCenter(&bbox1, ¢er) 142 require.True(t, geoAlmostEqual(¢er, &expected1), "pos/pos as expected") 143 bbox2 := BBox{-0.8, -1.0, 1.0, 0.8} 144 expected2 := GeoCoord{-0.9, 0.9} 145 bboxCenter(&bbox2, ¢er) 146 require.True(t, geoAlmostEqual(¢er, &expected2), "neg/pos as expected") 147 bbox3 := BBox{1.0, 0.8, -0.8, -1.0} 148 expected3 := GeoCoord{0.9, -0.9} 149 bboxCenter(&bbox3, ¢er) 150 require.True(t, geoAlmostEqual(¢er, &expected3), "pos/neg as expected") 151 bbox4 := BBox{-0.8, -1.0, -0.8, -1.0} 152 expected4 := GeoCoord{-0.9, -0.9} 153 bboxCenter(&bbox4, ¢er) 154 require.True(t, geoAlmostEqual(¢er, &expected4), "neg/neg as expected") 155 bbox5 := BBox{0.8, -0.8, 1.0, -1.0} 156 expected5 := GeoCoord{0.0, 0.0} 157 bboxCenter(&bbox5, ¢er) 158 require.True(t, geoAlmostEqual(¢er, &expected5), 159 "around origin as expected") 160 } 161 162 func Test_bboxCenterTransmeridian(t *testing.T) { 163 var center GeoCoord 164 165 bbox1 := BBox{1.0, 0.8, -M_PI + 0.3, M_PI - 0.1} 166 expected1 := GeoCoord{0.9, -M_PI + 0.1} 167 bboxCenter(&bbox1, ¢er) 168 require.True(t, geoAlmostEqual(¢er, &expected1), "skew east as expected") 169 bbox2 := BBox{1.0, 0.8, -M_PI + 0.1, M_PI - 0.3} 170 expected2 := GeoCoord{0.9, M_PI - 0.1} 171 bboxCenter(&bbox2, ¢er) 172 require.True(t, geoAlmostEqual(¢er, &expected2), "skew west as expected") 173 bbox3 := BBox{1.0, 0.8, -M_PI + 0.1, M_PI - 0.1} 174 expected3 := GeoCoord{0.9, M_PI} 175 bboxCenter(&bbox3, ¢er) 176 require.True(t, geoAlmostEqual(¢er, &expected3), 177 "on antimeridian as expected") 178 } 179 180 func Test_bboxIsTransmeridian(t *testing.T) { 181 bboxNormal := BBox{1.0, 0.8, 1.0, 0.8} 182 require.True(t, !bboxIsTransmeridian(&bboxNormal), 183 "Normal bbox not transmeridian") 184 bboxTransmeridian := BBox{1.0, 0.8, -M_PI + 0.3, M_PI - 0.1} 185 require.True(t, bboxIsTransmeridian(&bboxTransmeridian), "Transmeridian bbox is transmeridian") 186 } 187 188 func Test_bboxEquals(t *testing.T) { 189 bbox := BBox{1.0, 0.0, 1.0, 0.0} 190 north := bbox 191 north.north += 0.1 192 south := bbox 193 south.south += 0.1 194 east := bbox 195 east.east += 0.1 196 west := bbox 197 west.west += 0.1 198 require.True(t, bboxEquals(&bbox, &bbox), "Equals self") 199 require.True(t, !bboxEquals(&bbox, &north), "Not equals different north") 200 require.True(t, !bboxEquals(&bbox, &south), "Not equals different south") 201 require.True(t, !bboxEquals(&bbox, &east), "Not equals different east") 202 require.True(t, !bboxEquals(&bbox, &west), "Not equals different west") 203 }