]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/test_bucket.cxx
Revised set_bucket implementation.
[simgear.git] / simgear / bucket / test_bucket.cxx
1 /**************************************************************************
2  * test_bucket.cxx -- unit-tests for SGBucket class
3  *
4  * Copyright (C) 2014  James Turner - <zakalawe@mac.com>
5  *
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.
10  *
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.
15  *
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.
19  *
20  * $Id$
21  **************************************************************************/
22
23 #include <simgear/compiler.h>
24
25 #include <iostream>
26 #include <cstdlib>
27 #include <cstring>
28
29 using std::cout;
30 using std::cerr;
31 using std::endl;
32
33 #include <simgear/bucket/newbucket.hxx>
34 #include <simgear/misc/test_macros.hxx>
35
36 void testBucketSpans()
37 {
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);
44 }
45
46 void testBasic()
47 {
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");
55     VERIFY(b1.isValid());
56     
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");
63     VERIFY(b2.isValid());
64     
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");
71     VERIFY(b3.isValid());
72     
73     SGBucket defBuck;
74     VERIFY(!defBuck.isValid());
75     
76     b3.make_bad();
77     VERIFY(!b3.isValid());
78
79     SGBucket atAntiMeridian(180.0, 12.3);
80     VERIFY(atAntiMeridian.isValid());
81     COMPARE(atAntiMeridian.get_chunk_lon(), -180);
82     COMPARE(atAntiMeridian.get_x(), 0);
83     
84     SGBucket atAntiMeridian2(-180.0, -78.1);
85     VERIFY(atAntiMeridian2.isValid());
86     COMPARE(atAntiMeridian2.get_chunk_lon(), -180);
87     COMPARE(atAntiMeridian2.get_x(), 0);
88     
89 // check comparisom operator overload
90     SGBucket b4(5.11, 55.1);
91     VERIFY(b1 == b4); // should be equal
92     VERIFY(b1 == b1);
93     VERIFY(b1 != defBuck);
94     VERIFY(b1 != b2);
95     
96 // check wrapping/clipping of inputs
97     SGBucket wrapMeridian(-200.0, 45.0);
98     COMPARE(wrapMeridian.get_chunk_lon(), 160);
99     
100     SGBucket clipPole(48.9, 91);
101     COMPARE(clipPole.get_chunk_lat(), 89);
102 }
103
104 void testPolar()
105 {
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);
112     
113     COMPARE(b2.get_chunk_lat(), 89);
114     COMPARE(b2.get_chunk_lon(), 0);
115     COMPARE(b2.get_x(), 0);
116     COMPARE(b2.get_y(), 7);
117     
118     COMPARE(b1.gen_index(), b2.gen_index());
119     
120     SGGeod actualNorthPole1 = b1.get_corner(2);
121     SGGeod actualNorthPole2 = b1.get_corner(3);
122     COMPARE_EP(actualNorthPole1.getLatitudeDeg(), 90.0);
123     COMPARE_EP(actualNorthPole1.getLongitudeDeg(), 12.0);
124     COMPARE_EP(actualNorthPole2.getLatitudeDeg(), 90.0);
125     COMPARE_EP(actualNorthPole2.getLongitudeDeg(), 0.0);
126
127     SGBucket b3(-2, 89.88);
128     SGBucket b4(-7, 89.88);
129     COMPARE(b3.gen_index(), b4.gen_index());
130     
131     // south pole
132     SGBucket b5(-170, -89.88);
133     SGBucket b6(-179, -89.88);
134     
135     COMPARE(b5.get_chunk_lat(), -90);
136     COMPARE(b5.get_chunk_lon(), -180);
137     COMPARE(b5.get_x(), 0);
138     COMPARE(b5.get_y(), 0);
139     COMPARE(b5.gen_index(), b6.gen_index());
140     
141     SGGeod actualSouthPole1 = b5.get_corner(0);
142     SGGeod actualSouthPole2 = b5.get_corner(1);
143     COMPARE_EP(actualSouthPole1.getLatitudeDeg(), -90.0);
144     COMPARE_EP(actualSouthPole1.getLongitudeDeg(), -180);
145     COMPARE_EP(actualSouthPole2.getLatitudeDeg(), -90.0);
146     COMPARE_EP(actualSouthPole2.getLongitudeDeg(), -168);
147     
148     SGBucket b7(200, 89.88);
149     COMPARE(b7.get_chunk_lon(), -168);
150
151 }
152
153 // test the tiles just below the pole (between 86 & 89 degrees N/S)
154 void testNearPolar()
155 {
156     SGBucket b1(1, 88.5);
157     SGBucket b2(-1, 88.8);
158     COMPARE(b1.get_chunk_lon(), 0);
159     COMPARE(b1.get_chunk_lat(), 88);
160     VERIFY(b1.gen_index() != b2.gen_index());
161
162     SGBucket b3(176.1, 88.5);
163     COMPARE(b3.get_chunk_lon(), 176);
164     
165     SGBucket b4(-178, 88.5);
166     COMPARE(b4.get_chunk_lon(), -180);
167 }
168
169 void testOffset()
170 {
171     // bucket just below the 22 degree cutoff, so the next tile north
172     // is twice the width
173     SGBucket b1(-59.8, 21.9);
174     COMPARE(b1.get_chunk_lat(), 21);
175     COMPARE(b1.get_chunk_lon(), -60);
176     COMPARE(b1.get_x(), 1);
177     COMPARE(b1.get_y(), 7);
178     
179     // offset vertically
180     SGBucket b2(b1.sibling(0, 1));
181     COMPARE(b2.get_chunk_lat(), 22);
182     COMPARE(b2.get_chunk_lon(), -60);
183     COMPARE(b2.get_x(), 0);
184     COMPARE(b2.get_y(), 0);
185
186     COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
187     
188     // offset vertically and horizontally. We compute horizontal (x)
189     // movement at the target latitude, so this should move 0.25 * -3 degrees,
190     // NOT 0.125 * -3 degrees.
191     SGBucket b3(b1.sibling(-3, 1));
192     COMPARE(b3.get_chunk_lat(), 22);
193     COMPARE(b3.get_chunk_lon(), -61);
194     COMPARE(b3.get_x(), 1);
195     COMPARE(b3.get_y(), 0);
196     
197     COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
198 }
199
200 void testPolarOffset()
201 {
202     SGBucket b1(-11.7, -89.6);
203     COMPARE(b1.get_chunk_lat(), -90);
204     COMPARE(b1.get_chunk_lon(), -12);
205     COMPARE(b1.get_x(), 0);
206     COMPARE(b1.get_y(), 3);
207     
208     // offset horizontally
209     SGBucket b2(b1.sibling(-2, 0));
210     COMPARE(b2.get_chunk_lat(), -90);
211     COMPARE(b2.get_chunk_lon(), -36);
212     COMPARE(b2.get_x(), 0);
213     COMPARE(b2.get_y(), 3);
214     
215     COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
216     
217 // offset and wrap
218     SGBucket b3(-170, 89.1);
219     SGBucket b4(b3.sibling(-1, 0));
220     COMPARE(b4.get_chunk_lat(), 89);
221     COMPARE(b4.get_chunk_lon(), 168);
222     COMPARE(b4.get_x(), 0);
223     COMPARE(b4.get_y(), 0);
224     
225     COMPARE(b4.gen_index(), sgBucketOffset(-170, 89.1, -1, 0));
226
227     
228     SGBucket b5(177, 87.3);
229     SGBucket b6(b5.sibling(1, 1));
230     COMPARE(b6.get_chunk_lat(), 87);
231     COMPARE(b6.get_chunk_lon(), -180);
232     COMPARE(b6.get_x(), 0);
233     COMPARE(b6.get_y(), 3);
234     
235     COMPARE(b6.gen_index(), sgBucketOffset(177, 87.3, 1, 1));
236
237     // offset vertically towards the pole
238     SGBucket b7(b1.sibling(0, -5));
239     VERIFY(!b7.isValid());
240     
241     VERIFY(!SGBucket(0, 90).sibling(0, 1).isValid());
242 }
243
244 // test behaviour of bucket-offset near the anti-meridian (180-meridian)
245 void testOffsetWrap()
246 {
247     // near the equator
248     SGBucket b1(-179.8, 16.8);
249     COMPARE(b1.get_chunk_lat(), 16);
250     COMPARE(b1.get_chunk_lon(), -180);
251     COMPARE(b1.get_x(), 1);
252     COMPARE(b1.get_y(), 6);
253     
254     SGBucket b2(b1.sibling(-2, 0));
255     COMPARE(b2.get_chunk_lat(), 16);
256     COMPARE(b2.get_chunk_lon(), 179);
257     COMPARE(b2.get_x(), 7);
258     COMPARE(b2.get_y(), 6);
259     COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
260     
261     
262 }
263
264 int main(int argc, char* argv[])
265 {
266     testBucketSpans();
267     
268     testBasic();
269     testPolar();
270     testNearPolar();
271     testOffset();
272     testOffsetWrap();
273     testPolarOffset();
274     
275     cout << "all tests passed OK" << endl;
276     return 0; // passed
277 }
278