1 /**************************************************************************
2 * test_bucket.cxx -- unit-tests for SGBucket class
4 * Copyright (C) 2014 James Turner - <zakalawe@mac.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 **************************************************************************/
23 #include <simgear/compiler.h>
33 #include <simgear/bucket/newbucket.hxx>
34 #include <simgear/misc/test_macros.hxx>
36 void testBucketSpans()
38 COMPARE(sg_bucket_span(0.0), 0.125);
39 COMPARE(sg_bucket_span(-20), 0.125);
40 COMPARE(sg_bucket_span(-40), 0.25);
41 COMPARE(sg_bucket_span(89.9), 12.0);
42 COMPARE(sg_bucket_span(88.1), 4.0);
43 COMPARE(sg_bucket_span(-89.9), 12.0);
48 SGBucket b1(5.1, 55.05);
49 COMPARE(b1.get_chunk_lon(), 5);
50 COMPARE(b1.get_chunk_lat(), 55);
51 COMPARE(b1.get_x(), 0);
52 COMPARE(b1.get_y(), 0);
53 COMPARE(b1.gen_index(), 3040320);
54 COMPARE(b1.gen_base_path(), "e000n50/e005n55");
57 SGBucket b2(-10.1, -43.8);
58 COMPARE(b2.get_chunk_lon(), -11);
59 COMPARE(b2.get_chunk_lat(), -44);
60 COMPARE(b2.get_x(), 3);
61 COMPARE(b2.get_y(), 1); // latitude chunks numbered bottom to top, it seems
62 COMPARE(b2.gen_base_path(), "w020s50/w011s44");
65 SGBucket b3(123.48, 9.01);
66 COMPARE(b3.get_chunk_lon(), 123);
67 COMPARE(b3.get_chunk_lat(), 9);
68 COMPARE(b3.get_x(), 3);
69 COMPARE(b3.get_y(), 0);
70 COMPARE(b3.gen_base_path(), "e120n00/e123n09");
74 VERIFY(!defBuck.isValid());
77 VERIFY(!b3.isValid());
79 SGBucket atAntiMeridian(180.0, 12.3);
80 VERIFY(atAntiMeridian.isValid());
81 COMPARE(atAntiMeridian.get_chunk_lon(), -180);
82 COMPARE(atAntiMeridian.get_x(), 0);
84 SGBucket atAntiMeridian2(-180.0, -78.1);
85 VERIFY(atAntiMeridian2.isValid());
86 COMPARE(atAntiMeridian2.get_chunk_lon(), -180);
87 COMPARE(atAntiMeridian2.get_x(), 0);
89 // check comparisom operator overload
90 SGBucket b4(5.11, 55.1);
91 VERIFY(b1 == b4); // should be equal
93 VERIFY(b1 != defBuck);
96 // check wrapping/clipping of inputs
97 SGBucket wrapMeridian(-200.0, 45.0);
98 COMPARE(wrapMeridian.get_chunk_lon(), 160);
100 SGBucket clipPole(48.9, 91);
101 COMPARE(clipPole.get_chunk_lat(), 89);
106 SGBucket b1(0.0, 89.92);
107 SGBucket b2(10.0, 89.96);
108 COMPARE(b1.get_chunk_lat(), 89);
109 COMPARE(b1.get_chunk_lon(), 0);
110 COMPARE(b1.get_x(), 0);
111 COMPARE(b1.get_y(), 7);
113 COMPARE_EP(b1.get_highest_lat(), 90.0);
114 COMPARE_EP(b1.get_width_m(), 10.0);
116 COMPARE(b2.get_chunk_lat(), 89);
117 COMPARE(b2.get_chunk_lon(), 0);
118 COMPARE(b2.get_x(), 0);
119 COMPARE(b2.get_y(), 7);
121 COMPARE(b1.gen_index(), b2.gen_index());
123 SGGeod actualNorthPole1 = b1.get_corner(2);
124 SGGeod actualNorthPole2 = b1.get_corner(3);
125 COMPARE_EP(actualNorthPole1.getLatitudeDeg(), 90.0);
126 COMPARE_EP(actualNorthPole1.getLongitudeDeg(), 12.0);
127 COMPARE_EP(actualNorthPole2.getLatitudeDeg(), 90.0);
128 COMPARE_EP(actualNorthPole2.getLongitudeDeg(), 0.0);
130 SGBucket b3(-2, 89.88);
131 SGBucket b4(-7, 89.88);
132 COMPARE(b3.gen_index(), b4.gen_index());
135 SGBucket b5(-170, -89.88);
136 SGBucket b6(-179, -89.88);
138 COMPARE(b5.get_chunk_lat(), -90);
139 COMPARE(b5.get_chunk_lon(), -180);
140 COMPARE(b5.get_x(), 0);
141 COMPARE(b5.get_y(), 0);
142 COMPARE(b5.gen_index(), b6.gen_index());
143 COMPARE_EP(b5.get_highest_lat(), -90.0);
144 COMPARE_EP(b5.get_width_m(), 10.0);
146 SGGeod actualSouthPole1 = b5.get_corner(0);
147 SGGeod actualSouthPole2 = b5.get_corner(1);
148 COMPARE_EP(actualSouthPole1.getLatitudeDeg(), -90.0);
149 COMPARE_EP(actualSouthPole1.getLongitudeDeg(), -180);
150 COMPARE_EP(actualSouthPole2.getLatitudeDeg(), -90.0);
151 COMPARE_EP(actualSouthPole2.getLongitudeDeg(), -168);
153 SGBucket b7(200, 89.88);
154 COMPARE(b7.get_chunk_lon(), -168);
158 // test the tiles just below the pole (between 86 & 89 degrees N/S)
161 SGBucket b1(1, 88.5);
162 SGBucket b2(-1, 88.8);
163 COMPARE(b1.get_chunk_lon(), 0);
164 COMPARE(b1.get_chunk_lat(), 88);
165 VERIFY(b1.gen_index() != b2.gen_index());
167 SGBucket b3(176.1, 88.5);
168 COMPARE(b3.get_chunk_lon(), 176);
170 SGBucket b4(-178, 88.5);
171 COMPARE(b4.get_chunk_lon(), -180);
176 // bucket just below the 22 degree cutoff, so the next tile north
177 // is twice the width
178 SGBucket b1(-59.8, 21.9);
179 COMPARE(b1.get_chunk_lat(), 21);
180 COMPARE(b1.get_chunk_lon(), -60);
181 COMPARE(b1.get_x(), 1);
182 COMPARE(b1.get_y(), 7);
185 SGBucket b2(b1.sibling(0, 1));
186 COMPARE(b2.get_chunk_lat(), 22);
187 COMPARE(b2.get_chunk_lon(), -60);
188 COMPARE(b2.get_x(), 0);
189 COMPARE(b2.get_y(), 0);
191 COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
193 // offset vertically and horizontally. We compute horizontal (x)
194 // movement at the target latitude, so this should move 0.25 * -3 degrees,
195 // NOT 0.125 * -3 degrees.
196 SGBucket b3(b1.sibling(-3, 1));
197 COMPARE(b3.get_chunk_lat(), 22);
198 COMPARE(b3.get_chunk_lon(), -61);
199 COMPARE(b3.get_x(), 1);
200 COMPARE(b3.get_y(), 0);
202 COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
205 void testPolarOffset()
207 SGBucket b1(-11.7, -89.6);
208 COMPARE(b1.get_chunk_lat(), -90);
209 COMPARE(b1.get_chunk_lon(), -12);
210 COMPARE(b1.get_x(), 0);
211 COMPARE(b1.get_y(), 3);
213 // offset horizontally
214 SGBucket b2(b1.sibling(-2, 0));
215 COMPARE(b2.get_chunk_lat(), -90);
216 COMPARE(b2.get_chunk_lon(), -36);
217 COMPARE(b2.get_x(), 0);
218 COMPARE(b2.get_y(), 3);
220 COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
223 SGBucket b3(-170, 89.1);
224 SGBucket b4(b3.sibling(-1, 0));
225 COMPARE(b4.get_chunk_lat(), 89);
226 COMPARE(b4.get_chunk_lon(), 168);
227 COMPARE(b4.get_x(), 0);
228 COMPARE(b4.get_y(), 0);
230 COMPARE(b4.gen_index(), sgBucketOffset(-170, 89.1, -1, 0));
233 SGBucket b5(177, 87.3);
234 SGBucket b6(b5.sibling(1, 1));
235 COMPARE(b6.get_chunk_lat(), 87);
236 COMPARE(b6.get_chunk_lon(), -180);
237 COMPARE(b6.get_x(), 0);
238 COMPARE(b6.get_y(), 3);
240 COMPARE(b6.gen_index(), sgBucketOffset(177, 87.3, 1, 1));
242 // offset vertically towards the pole
243 SGBucket b7(b1.sibling(0, -5));
244 VERIFY(!b7.isValid());
246 VERIFY(!SGBucket(0, 90).sibling(0, 1).isValid());
249 // test behaviour of bucket-offset near the anti-meridian (180-meridian)
250 void testOffsetWrap()
253 SGBucket b1(-179.8, 16.8);
254 COMPARE(b1.get_chunk_lat(), 16);
255 COMPARE(b1.get_chunk_lon(), -180);
256 COMPARE(b1.get_x(), 1);
257 COMPARE(b1.get_y(), 6);
259 SGBucket b2(b1.sibling(-2, 0));
260 COMPARE(b2.get_chunk_lat(), 16);
261 COMPARE(b2.get_chunk_lon(), 179);
262 COMPARE(b2.get_x(), 7);
263 COMPARE(b2.get_y(), 6);
264 COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
269 int main(int argc, char* argv[])
280 cout << "all tests passed OK" << endl;