]> git.mxchange.org Git - simgear.git/blob - simgear/bucket/test_bucket.cxx
Add new sibling() helper to SGBucket
[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 testPolar()
47 {
48     SGBucket b1(0.0, 89.92);
49     SGBucket b2(10.0, 89.96);
50     COMPARE(b1.get_chunk_lat(), 89);
51     COMPARE(b1.get_chunk_lon(), 0);
52     COMPARE(b1.get_x(), 0);
53     COMPARE(b1.get_y(), 7);
54     
55     COMPARE(b1.gen_index(), b2.gen_index());
56     
57     SGBucket b3(-2, 89.88);
58     SGBucket b4(-7, 89.88);
59     COMPARE(b3.gen_index(), b4.gen_index());
60     
61     // south pole
62     SGBucket b5(-170, -89.88);
63     SGBucket b6(-179, -89.88);
64     
65     COMPARE(b5.get_chunk_lat(), -90);
66     COMPARE(b5.get_chunk_lon(), -180);
67     COMPARE(b5.get_x(), 0);
68     COMPARE(b5.get_y(), 0);
69     COMPARE(b5.gen_index(), b6.gen_index());
70     
71     // no automatic wrapping of these values occurs
72     SGBucket b7(200, 89.88);
73     COMPARE(b7.get_chunk_lon(), 192);
74
75 }
76
77 // test the tiles just below the pole (between 86 & 89 degrees N/S)
78 void testNearPolar()
79 {
80     SGBucket b1(1, 88.5);
81     SGBucket b2(-1, 88.8);
82     COMPARE(b1.get_chunk_lon(), 0);
83     COMPARE(b1.get_chunk_lat(), 88);
84     VERIFY(b1.gen_index() != b2.gen_index());
85     
86     SGBucket b3(176.1, 88.5);
87     COMPARE(b3.get_chunk_lon(), 176);
88     
89     SGBucket b4(-178, 88.5);
90     // the fix for the polar cap issue, causes this to report -180,
91     // not -184
92     COMPARE(b4.get_chunk_lon(), -180);
93 }
94
95 void testOffset()
96 {
97     // bucket just below the 22 degree cutoff, so the next tile north
98     // is twice the width
99     SGBucket b1(-59.8, 21.9);
100     COMPARE(b1.get_chunk_lat(), 21);
101     COMPARE(b1.get_chunk_lon(), -60);
102     COMPARE(b1.get_x(), 1);
103     COMPARE(b1.get_y(), 7);
104     
105     // offset vertically
106     SGBucket b2(b1.sibling(0, 1));
107     COMPARE(b2.get_chunk_lat(), 22);
108     COMPARE(b2.get_chunk_lon(), -60);
109     COMPARE(b2.get_x(), 0);
110     COMPARE(b2.get_y(), 0);
111
112     COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
113     
114     // offset vertically and horizontally. We compute horizontal (x)
115     // movement at the target latitude, so this should move 0.25 * -3 degrees,
116     // NOT 0.125 * -3 degrees.
117     SGBucket b3(b1.sibling(-3, 1));
118     COMPARE(b3.get_chunk_lat(), 22);
119     COMPARE(b3.get_chunk_lon(), -61);
120     COMPARE(b3.get_x(), 1);
121     COMPARE(b3.get_y(), 0);
122     
123     COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
124 }
125
126 void testPolarOffset()
127 {
128     SGBucket b1(-11.7, -89.6);
129     COMPARE(b1.get_chunk_lat(), -90);
130     COMPARE(b1.get_chunk_lon(), -12);
131     COMPARE(b1.get_x(), 0);
132     COMPARE(b1.get_y(), 3);
133     
134     // offset horizontally
135     SGBucket b2(b1.sibling(-2, 0));
136     COMPARE(b2.get_chunk_lat(), -90);
137     COMPARE(b2.get_chunk_lon(), -36);
138     COMPARE(b2.get_x(), 0);
139     COMPARE(b2.get_y(), 3);
140     
141     COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
142     
143 #if 0
144     // offset vertically towards the pole
145     SGBucket b3(b1.sibling(0, -5));
146     COMPARE(b3.get_chunk_lat(), -90);
147     COMPARE(b3.get_chunk_lon(), -12);
148     COMPARE(b3.get_x(), 0);
149     COMPARE(b3.get_y(), 0);
150     
151     COMPARE(b3.gen_index(), sgBucketOffset(-11.7, -89.6, 0, 0));
152 #endif
153 }
154
155 // test behaviour of bucket-offset near the anti-meridian (180-meridian)
156 void testOffsetWrap()
157 {
158     // near the equator
159     SGBucket b1(-179.8, 16.8);
160     COMPARE(b1.get_chunk_lat(), 16);
161     COMPARE(b1.get_chunk_lon(), -180);
162     COMPARE(b1.get_x(), 1);
163     COMPARE(b1.get_y(), 6);
164     
165     SGBucket b2(b1.sibling(-2, 0));
166     COMPARE(b2.get_chunk_lat(), 16);
167     COMPARE(b2.get_chunk_lon(), 179);
168     COMPARE(b2.get_x(), 7);
169     COMPARE(b2.get_y(), 6);
170     COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
171     
172     
173 }
174
175 int main(int argc, char* argv[])
176 {
177     testBucketSpans();
178     
179     SGBucket b1(5.1, 55.05);
180     COMPARE(b1.get_chunk_lon(), 5);
181     COMPARE(b1.get_chunk_lat(), 55);
182     COMPARE(b1.get_x(), 0);
183     COMPARE(b1.get_y(), 0);
184     COMPARE(b1.gen_index(), 3040320);
185     COMPARE(b1.gen_base_path(), "e000n50/e005n55");
186     
187     SGBucket b2(-10.1, -43.8);
188     COMPARE(b2.get_chunk_lon(), -11);
189     COMPARE(b2.get_chunk_lat(), -44);
190     COMPARE(b2.get_x(), 3);
191     COMPARE(b2.get_y(), 1); // latitude chunks numbered bottom to top, it seems
192     COMPARE(b2.gen_base_path(), "w020s50/w011s44");
193     
194     SGBucket b3(123.48, 9.01);
195     COMPARE(b3.get_chunk_lon(), 123);
196     COMPARE(b3.get_chunk_lat(), 9);
197     COMPARE(b3.get_x(), 3);
198     COMPARE(b3.get_y(), 0);
199     COMPARE(b3.gen_base_path(), "e120n00/e123n09");
200     
201     testPolar();
202     testNearPolar();
203     testOffset();
204     testOffsetWrap();
205     testPolarOffset();
206     
207     cout << "all tests passed OK" << endl;
208     return 0; // passed
209 }
210