aboutsummaryrefslogtreecommitdiff
path: root/src/tests/multistresstest.cpp
blob: 6b7099cee90458d70591402d405a1e6474b5ee26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/******************************************************************************
 *
 * Project:  PROJ.4
 * Purpose:  Mainline program to stress test multithreaded PROJ.4 processing.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2010, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "proj.h"

#ifdef _WIN32
    #include <windows.h>
#else
    #include <pthread.h>
    #include <unistd.h>
#endif

#define num_threads    10
static int num_iterations = 1000000;
static int reinit_every_iteration=0;

typedef struct {
    const char *src_def;
    const char *dst_def;

    PJ_COORD src;
    PJ_COORD dst;

    int     dst_error;
    int     skip;
} TestItem;

static TestItem test_list[] = {
    {
        "+proj=utm +zone=11 +datum=WGS84",
        "+proj=latlong +datum=WGS84",
        proj_coord(150000.0, 3000000.0, 0.0, 0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=utm +zone=11 +datum=NAD83",
        "+proj=latlong +datum=NAD27",
        proj_coord(150000.0, 3000000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=utm +zone=11 +datum=NAD83",
        "+proj=latlong +nadgrids=@null +ellps=WGS84",
        proj_coord(150000.0, 3000000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=utm +zone=11 +datum=WGS84",
        "+proj=merc +datum=potsdam",
        proj_coord(150000.0, 3000000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=latlong +nadgrids=nzgd2kgrid0005.gsb",
        "+proj=latlong +datum=WGS84",
        proj_coord(150000.0, 3000000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=latlong +nadgrids=nzgd2kgrid0005.gsb",
        "+proj=latlong +datum=WGS84",
        proj_coord(170, -40, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=latlong +ellps=GRS80 +towgs84=2,3,5",
        "+proj=latlong +ellps=intl +towgs84=10,12,15",
        proj_coord(170, -40, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=eqc +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        "+proj=stere +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=cea +lat_ts=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=merc +lon_0=12 +k=0.999 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=bonne +lat_1=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=cass +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=nzmg +lat_0=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=gnom +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=ortho +lat_0=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=laea +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=aeqd +lat_0=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=eqdc +lat_1=20 +lat_2=5 +lat_0=11 +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+proj=mill +lat_0=11 +lon_0=12 +y_0=200000 +datum=WGS84 ",
        "+proj=moll +lon_0=12 +x_0=100000 +y_0=200000 +datum=WGS84 ",
        proj_coord(150000.0, 250000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        "+init=epsg:3309",
        "+init=epsg:4326",
        proj_coord(150000.0, 30000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    },
    {
        /* Bad projection (invalid ellipsoid parameter +R_A=0) */
        "+proj=utm +zone=11 +datum=WGS84",
        "+proj=merc +datum=potsdam +R_A=0",
        proj_coord(150000.0, 3000000.0, 0.0, 0.0),
        proj_coord(0.0, 0.0, 0.0, 0.0),
        0, 0
    }
};

static volatile int active_thread_count = 0;

/************************************************************************/
/*                             TestThread()                             */
/************************************************************************/

static void TestThread()

{
    int i, test_count = sizeof(test_list) / sizeof(TestItem);
    int repeat_count = num_iterations;
    int i_iter;

/* -------------------------------------------------------------------- */
/*      Initialize coordinate system definitions.                       */
/* -------------------------------------------------------------------- */
    PJ **pj_list;
    PJ_CONTEXT *ctx = proj_context_create();

    pj_list = (PJ **) calloc(test_count,sizeof(PJ*));

    if(!reinit_every_iteration)
    {
        for( i = 0; i < test_count; i++ )
        {
            TestItem *test = test_list + i;

            pj_list[i] = proj_create_crs_to_crs(
                ctx, test->src_def, test->dst_def, nullptr
            );
        }
    }

/* -------------------------------------------------------------------- */
/*      Perform tests - over and over.                                  */
/* -------------------------------------------------------------------- */

    for( i_iter = 0; i_iter < repeat_count; i_iter++ )
    {
        for( i = 0; i < test_count; i++ )
        {
            TestItem *test = test_list + i;

            if( reinit_every_iteration )
            {
                proj_context_use_proj4_init_rules(nullptr, true);
                pj_list[i] = proj_create_crs_to_crs(
                    ctx, test->src_def, test->dst_def, nullptr
                );

                {
                    int skipTest = (pj_list[i] == nullptr);

                    if ( skipTest != test->skip )
                        fprintf( stderr, "Threaded projection initialization does not match unthreaded initialization\n" );

                    if (skipTest)
                    {
                        proj_destroy( pj_list[i] );
                        continue;
                    }
                }
            }

            if ( test->skip )
                continue;

            PJ_COORD out = proj_trans(pj_list[i], PJ_FWD, test->src);

            int error = proj_errno(pj_list[i]);

            if( error != test->dst_error )
            {
                fprintf( stderr, "Got error %d, expected %d\n",
                         error, test->dst_error );
            }
            proj_errno_reset(pj_list[i]);

            if ( out.xyz.x != test->dst.xyz.x || out.xyz.y != test->dst.xyz.y || out.xyz.z != test->dst.xyz.z)
            //if( x != test->dst_x || y != test->dst_y || z != test->dst_z )
            {
                fprintf( stderr,
                         "Got %.15g,%.15g,%.15g\n"
                         "Expected %.15g,%.15g,%.15g\n"
                         "Diff %.15g,%.15g,%.15g\n",
                         out.xyz.x, out.xyz.y, out.xyz.z,
                         test->dst.xyz.x, test->dst.xyz.y, test->dst.xyz.z,
                         out.xyz.x-test->dst.xyz.x, out.xyz.y-test->dst.xyz.y, out.xyz.z-test->dst.xyz.z);
            }

            if( reinit_every_iteration )
            {
                proj_destroy( pj_list[i] );
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Cleanup                                                         */
/* -------------------------------------------------------------------- */
    if( !reinit_every_iteration )
    {
        for( i = 0; i < test_count; i++ )
        {
            proj_destroy( pj_list[i] );
        }
    }

    free( pj_list );

    proj_context_destroy( ctx );

    printf( "%d iterations of the %d tests complete in thread X\n",
            repeat_count, test_count );

    active_thread_count--;
}

#ifdef _WIN32
/************************************************************************/
/*                             WinTestThread()                        */
/************************************************************************/

static DWORD WINAPI WinTestThread( LPVOID lpParameter )

{
    TestThread();

    return 0;
}

#else
/************************************************************************/
/*                             PosixTestThread()                        */
/************************************************************************/

static void *PosixTestThread( void *pData )

{
    (void)pData;
    TestThread();
    return nullptr;
}
#endif

/************************************************************************/
/*                                main()                                */
/************************************************************************/
#ifdef _WIN32
static DWORD WINAPI do_main( LPVOID unused )
#else
static int do_main(void)
#endif
{
/* -------------------------------------------------------------------- */
/*      Our first pass is to establish the correct answers for all      */
/*      the tests.                                                      */
/* -------------------------------------------------------------------- */
    int i, test_count = sizeof(test_list) / sizeof(TestItem);

    for( i = 0; i < test_count; i++ )
    {
        TestItem *test = test_list + i;

        PJ *pj;

        proj_context_use_proj4_init_rules(nullptr, true);
        pj = proj_create_crs_to_crs(
            nullptr, test->src_def, test->dst_def, nullptr
        );

        if( pj == nullptr )
        {
            printf( "Unable to translate:\n%s\n  or\n%s\n", test->src_def, test->dst_def );
            test->skip = 1;
            proj_destroy(pj);
            continue;
        }


        PJ_COORD out = proj_trans(pj, PJ_FWD, test->src);
        test->dst = out;
        test->dst_error  = proj_errno(pj);

        proj_destroy(pj);

        test->skip = 0;

#ifdef nodef
        printf( "Test %d - output %.14g,%.14g,%g\n", i, test->dst.xyz.x, test->dst.xyz.y, test->dst.xyz.z );
#endif
    }

    printf( "%d tests initialized.\n", test_count );

/* -------------------------------------------------------------------- */
/*      Now launch a bunch of threads to repeat the tests.              */
/* -------------------------------------------------------------------- */
#ifdef _WIN32

    { //Scoped to workaround lack of c99 support in VS
        HANDLE ahThread[num_threads];

        for( i = 0; i < num_threads; i++ )
        {
            active_thread_count++;

            ahThread[i] = CreateThread(NULL, 0, WinTestThread, NULL, 0, NULL);

            if (ahThread[i] == 0)
            {
                printf( "Thread creation failed.");
                return 1;
            }
        }

        printf( "%d test threads launched.\n", num_threads );

        WaitForMultipleObjects(num_threads, ahThread, TRUE, INFINITE);
    }

#else
    {
    pthread_t ahThread[num_threads];
    pthread_attr_t hThreadAttr;

    pthread_attr_init( &hThreadAttr );
    pthread_attr_setdetachstate( &hThreadAttr, PTHREAD_CREATE_DETACHED );

    for( i = 0; i < num_threads; i++ )
    {
        active_thread_count++;

        pthread_create( &(ahThread[i]), &hThreadAttr,
            PosixTestThread, nullptr );
    }

    printf( "%d test threads launched.\n", num_threads );

    while( active_thread_count > 0 )
        sleep( 1 );
    }
#endif

    printf( "all tests complete.\n" );

    return 0;
}


int main( int argc, char **argv )
{
    int i;
    for(i=0;i<argc;i++)
    {
        if( strcmp(argv[i], "-reinit") == 0 )
            reinit_every_iteration = 1;
        else if( strcmp(argv[i], "-num_iterations") == 0 && i+1 < argc )
        {
            num_iterations = atoi(argv[i+1]);
            i++;
        }
    }

#ifdef _WIN32
    /* This is an incredible weirdness but with mingw cross-compiler */
    /* 1. - b/a; where double a = 6378206.4; and double b = 6356583.8; */
    /* does not evaluate the same in the main thread or in a thread forked */
    /* by CreateThread(), so run the main in a thread... */
    {
        HANDLE thread = CreateThread(NULL, 0, do_main, NULL, 0, NULL);
        WaitForSingleObject(thread, INFINITE);
        CloseHandle( thread );
    }
#else
    do_main();
#endif
    return 0;
}