File: test-options-parse.cpp

package info (click to toggle)
osm2pgsql 0.96.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,304 kB
  • sloc: cpp: 11,462; python: 543; sh: 98; makefile: 17
file content (441 lines) | stat: -rw-r--r-- 16,626 bytes parent folder | download
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
#include "options.hpp"
#include "middle-pgsql.hpp"
#include "middle-ram.hpp"
#include "output-pgsql.hpp"
#include "output-gazetteer.hpp"
#include "output-null.hpp"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdexcept>
#include <boost/format.hpp>
#include <boost/algorithm/string/predicate.hpp>

namespace alg = boost::algorithm;

#define len(x) sizeof(x)/sizeof(x[0])

void run_test(const char* test_name, void (*testfunc)())
{
    try
    {
        fprintf(stderr, "%s\n", test_name);
        testfunc();
    }
    catch(const std::exception& e)
    {
        fprintf(stderr, "%s\n", e.what());
        fprintf(stderr, "FAIL\n");
        exit(EXIT_FAILURE);
    }
    fprintf(stderr, "PASS\n");
}

void parse_fail(const int argc, const char* argv[], const std::string& fail_message)
{
    try
    {
        options_t options = options_t(argc, const_cast<char **>(argv));
        throw std::logic_error((boost::format("Expected '%1%'") % fail_message).str());
    }
    catch(const std::runtime_error& e)
    {
        if(!alg::icontains(e.what(), fail_message))
            throw std::logic_error((boost::format("Expected '%1%' but instead got '%2%'") % fail_message % e.what()).str());
    }
}

void test_insufficient_args()
{
    const char* argv[] = {"osm2pgsql", "-a", "-c", "--slim"};
    parse_fail(len(argv), argv, "usage error");
}

void test_incompatible_args()
{
    const char* a1[] = {"osm2pgsql", "-a", "-c", "--slim", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a1), a1, "options can not be used at the same time");

    const char* a2[] = {"osm2pgsql", "--drop", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a2), a2, "drop only makes sense with");

    const char* a3[] = {"osm2pgsql", "-j", "-k", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a3), a3, "you can not specify both");
}

void test_middles()
{
    const char* a1[] = {"osm2pgsql", "--slim", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options_t options = options_t(len(a1), const_cast<char **>(a1));
    std::shared_ptr<middle_t> mid = middle_t::create_middle(options.slim);
    if(dynamic_cast<middle_pgsql_t *>(mid.get()) == nullptr)
    {
        throw std::logic_error("Using slim mode we expected a pgsql middle");
    }

    const char* a2[] = {"osm2pgsql", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a2), const_cast<char **>(a2));
    mid = middle_t::create_middle(options.slim);
    if(dynamic_cast<middle_ram_t *>(mid.get()) == nullptr)
    {
        throw std::logic_error("Using without slim mode we expected a ram middle");
    }
}

void test_outputs()
{
    const char* a1[] = {"osm2pgsql", "-O", "pgsql", "--style", "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options_t options = options_t(len(a1), const_cast<char **>(a1));
    std::shared_ptr<middle_t> mid = middle_t::create_middle(options.slim);
    std::vector<std::shared_ptr<output_t> > outs = output_t::create_outputs(mid.get(), options);
    output_t* out = outs.front().get();
    if(dynamic_cast<output_pgsql_t *>(out) == nullptr)
    {
        throw std::logic_error("Expected a pgsql output");
    }

    const char* a2[] = {"osm2pgsql", "-O", "gazetteer", "--style", "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a2), const_cast<char **>(a2));
    mid = middle_t::create_middle(options.slim);
    outs = output_t::create_outputs(mid.get(), options);
    out = outs.front().get();
    if(dynamic_cast<output_gazetteer_t *>(out) == nullptr)
    {
        throw std::logic_error("Expected a gazetteer output");
    }

    const char* a3[] = {"osm2pgsql", "-O", "null", "--style", "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a3), const_cast<char **>(a3));
    mid = middle_t::create_middle(options.slim);
    outs = output_t::create_outputs(mid.get(), options);
    out = outs.front().get();
    if(dynamic_cast<output_null_t *>(out) == nullptr)
    {
        throw std::logic_error("Expected a null output");
    }

    const char* a4[] = {"osm2pgsql", "-O", "keine_richtige_ausgabe", "--style", "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a4), const_cast<char **>(a4));
    mid = middle_t::create_middle(options.slim);
    try
    {
        outs = output_t::create_outputs(mid.get(), options);
        out = outs.front().get();
        throw std::logic_error("Expected 'not recognised'");
    }
    catch(const std::runtime_error& e)
    {
        if(!alg::icontains(e.what(), "not recognised"))
            throw std::logic_error((boost::format("Expected 'not recognised' but instead got '%2%'") % e.what()).str());
    }
}

void test_parsing_tile_expiry_zoom_levels()
{
    const char *a1[] = {
        "osm2pgsql",     "-e",
        "8-12",          "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options_t options = options_t(len(a1), const_cast<char **>(a1));
    if (options.expire_tiles_zoom_min != 8)
        throw std::logic_error(
            (boost::format("Expected expire_tiles_zoom_min 8 but got '%1%'") %
             options.expire_tiles_zoom_min)
                .str());
    if (options.expire_tiles_zoom != 12)
        throw std::logic_error(
            (boost::format("Expected expire_tiles_zoom 12 but got '%1%'") %
             options.expire_tiles_zoom_min)
                .str());

    const char *a2[] = {"osm2pgsql",
                        "-e",
                        "12",
                        "--style",
                        "default.style",
                        "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a2), const_cast<char **>(a2));
    if (options.expire_tiles_zoom_min != 12)
        throw std::logic_error(
            (boost::format("Expected expire_tiles_zoom_min 12 but got '%1%'") %
             options.expire_tiles_zoom_min)
                .str());
    if (options.expire_tiles_zoom != 12)
        throw std::logic_error(
            (boost::format("Expected expire_tiles_zoom 12 but got '%1%'") %
             options.expire_tiles_zoom_min)
                .str());

    //  check if very high zoom levels are set back to still high but valid zoom levels
    const char *a3[] = {
        "osm2pgsql",     "-e",
        "33-35",         "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    options = options_t(len(a3), const_cast<char **>(a3));
    if (options.expire_tiles_zoom_min != 31)
        throw std::logic_error((boost::format("Expected expire_tiles_zoom_min "
                                              "set back to 31 but got '%1%'") %
                                options.expire_tiles_zoom_min)
                                   .str());
    if (options.expire_tiles_zoom != 31)
        throw std::logic_error(
            (boost::format(
                 "Expected expire_tiles_zoom set back to 31 but got '%1%'") %
             options.expire_tiles_zoom_min)
                .str());
}

void test_parsing_tile_expiry_zoom_levels_fails()
{
    const char *a1[] = {
        "osm2pgsql",     "-e",
        "8--12",         "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a1), a1, "Invalid maximum zoom level given for tile expiry");

    const char *a2[] = {
        "osm2pgsql",     "-e",
        "-8-12",         "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a2), a2,
               "Missing argument for option --expire-tiles. Zoom levels must be positive.");

    const char *a3[] = {"osm2pgsql", "-e", "--style", "default.style",
                        "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a3), a3,
               "Missing argument for option --expire-tiles. Zoom levels must be positive.");

    const char *a4[] = {
        "osm2pgsql",     "-e",
        "a-8",           "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a4), a4, "Bad argument for option --expire-tiles. Minimum zoom level must be larger than 0.");

    const char *a5[] = {
        "osm2pgsql",     "-e",
        "6:8",           "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a5), a5, "Minimum and maximum zoom level for tile expiry "
                            "must be separated by '-'.");

    const char *a6[] = {
        "osm2pgsql",     "-e",
        "6-0",           "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a6), a6,
               "Invalid maximum zoom level given for tile expiry.");

    const char *a7[] = {
        "osm2pgsql",     "-e",
        "6-9a",          "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a7), a7,
               "Invalid maximum zoom level given for tile expiry.");

    const char *a8[] = {
        "osm2pgsql",     "-e",
        "0-8",           "--style",
        "default.style", "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a8), a8,
               "Bad argument for option --expire-tiles. Minimum zoom level "
               "must be larger than 0.");

    const char *a9[] = {"osm2pgsql",
                        "-e",
                        "6-",
                        "--style",
                        "default.style",
                        "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a9), a9,
               "Invalid maximum zoom level given for tile "
               "expiry.");

    const char *a10[] = {"osm2pgsql",
                         "-e",
                         "-6",
                         "--style",
                         "default.style",
                         "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a10), a10,
               "Missing argument for option --expire-tiles. Zoom levels "
               "must be positive.");

    const char *a11[] = {"osm2pgsql",
                         "-e",
                         "0",
                         "--style",
                         "default.style",
                         "tests/liechtenstein-2013-08-03.osm.pbf"};
    parse_fail(len(a11), a11,
               "Bad argument for option --expire-tiles. Minimum zoom level "
               "must be larger than 0.");
}

int get_random_proj(std::vector<std::string>& args)
{
    int proj = rand() % 3;
    switch(proj)
    {
        case 1:
            args.push_back("-l");
            return PROJ_LATLONG;
        case 2:
            args.push_back("-m");
            return PROJ_SPHERE_MERC;
    }

    args.push_back("--proj");
    //nice contiguous block of valid epsgs here randomly use one of those..
    proj = (rand() % (2962 - 2308)) + 2308;
    args.push_back((boost::format("%1%") % proj).str());
    return proj;
}

std::string get_random_string(const int length)
{
    std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
    std::string result;
    result.resize(length);

    for (int i = 0; i < length; i++)
        result[i] = charset[rand() % charset.length()];

    return result;
}

template<typename T>
void add_arg_or_not(const char* arg, std::vector<std::string>& args, T& option)
{
    if(rand() % 2)
    {
        args.push_back(arg);
        option = 1;
    }
    else
        option = 0;
}

void add_arg_and_val_or_not(const char* arg, std::vector<std::string>& args, int option, const int val)
{
    if(rand() % 2)
    {
        args.push_back(arg);
        args.push_back((boost::format("%1%") % val).str());
        option = val;
    }
}

void add_arg_and_val_or_not(const char* arg, std::vector<std::string>& args, const char *option, std::string val)
{
    if(rand() % 2)
    {
        args.push_back(arg);
        args.push_back(val);
        option = val.c_str();
    }
}

void test_random_perms()
{

    for(int i = 0; i < 5; ++i)
    {
        options_t options;
        std::vector<std::string> args;
        args.push_back("osm2pgsql");

        //pick a projection
        options.projection.reset(reprojection::create_projection(get_random_proj(args)));

        //pick a style file
        std::string style = get_random_string(15);
        options.style = style.c_str();
        args.push_back("--style");
        args.push_back(style);

        add_arg_and_val_or_not("--cache", args, options.cache, rand() % 800);
        if (options.database_options.db) {
            add_arg_and_val_or_not("--database", args, options.database_options.db->c_str(), get_random_string(6));
        }
        if (options.database_options.username) {
            add_arg_and_val_or_not("--username", args, options.database_options.username->c_str(), get_random_string(6));
        }
        if (options.database_options.host) {
            add_arg_and_val_or_not("--host", args, options.database_options.host->c_str(), get_random_string(6));
        }
        //add_arg_and_val_or_not("--port", args, options.port, rand() % 9999);

        //--hstore-match-only
        //--hstore-column   Add an additional hstore (key/value) column containing all tags that start with the specified string, eg --hstore-column "name:" will produce an extra hstore column that contains all name:xx tags

        add_arg_or_not("--hstore-add-index", args, options.enable_hstore_index);

        //--tablespace-index    The name of the PostgreSQL tablespace where all indexes will be created. The following options allow more fine-grained control:
        //      --tablespace-main-data    tablespace for main tables
        //      --tablespace-main-index   tablespace for main table indexes
        //      --tablespace-slim-data    tablespace for slim mode tables
        //      --tablespace-slim-index   tablespace for slim mode indexes
        //                    (if unset, use db's default; -i is equivalent to setting
        //                    --tablespace-main-index and --tablespace-slim-index)

        add_arg_and_val_or_not("--number-processes", args, options.num_procs, rand() % 12);

        //add_arg_or_not("--disable-parallel-indexing", args, options.parallel_indexing);

        add_arg_or_not("--unlogged", args, options.unlogged);

        //--cache-strategy  Specifies the method used to cache nodes in ram. Available options are: dense chunk sparse optimized

        if (options.flat_node_file) {
            add_arg_and_val_or_not("--flat-nodes", args, options.flat_node_file->c_str(), get_random_string(15));
        }

        //--expire-tiles [min_zoom-]max_zoom    Create a tile expiry list.

        add_arg_and_val_or_not("--expire-output", args, options.expire_tiles_filename.c_str(), get_random_string(15));

        //--bbox        Apply a bounding box filter on the imported data Must be specified as: minlon,minlat,maxlon,maxlat e.g. --bbox -0.5,51.25,0.5,51.75

        add_arg_and_val_or_not("--prefix", args, options.prefix.c_str(), get_random_string(15));

        //--input-reader    Input frontend. auto, o5m, xml, pbf

        if (options.tag_transform_script) {
            add_arg_and_val_or_not("--tag-transform-script", args, options.tag_transform_script->c_str(), get_random_string(15));
        }
        add_arg_or_not("--extra-attributes", args, options.extra_attributes);
        add_arg_or_not("--multi-geometry", args, options.enable_multi);
        add_arg_or_not("--keep-coastlines", args, options.keep_coastlines);

        //add the input file
        args.push_back("tests/liechtenstein-2013-08-03.osm.pbf");

        const char** argv = new const char*[args.size() + 1];
        argv[args.size()] = nullptr;
        for(std::vector<std::string>::const_iterator arg = args.begin(); arg != args.end(); ++arg)
            argv[arg - args.begin()] = arg->c_str();
        options_t((int) args.size(), const_cast<char **>(argv));
        delete[] argv;
    }
}

int main(int argc, char *argv[])
{
    srand(0);

    //try each test if any fail we will exit
    run_test("test_insufficient_args", test_insufficient_args);
    run_test("test_incompatible_args", test_incompatible_args);
    run_test("test_middles", test_middles);
    run_test("test_outputs", test_outputs);
    run_test("test_random_perms", test_random_perms);
    run_test("test_parsing_tile_expiry_zoom_levels_fails",
             test_parsing_tile_expiry_zoom_levels_fails);
    run_test("test_parsing_tile_expiry_zoom_levels",
             test_parsing_tile_expiry_zoom_levels);

    //passed
    return 0;
}