|
| 1 | +#include <iostream> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <cassert> |
| 6 | +#include <sstream> |
| 7 | +#include <stdexcept> |
| 8 | +#include <memory> |
| 9 | + |
| 10 | +#include "osmtypes.hpp" |
| 11 | +#include "osmdata.hpp" |
| 12 | +#include "output-pgsql.hpp" |
| 13 | +#include "options.hpp" |
| 14 | +#include "middle-pgsql.hpp" |
| 15 | +#include "middle-ram.hpp" |
| 16 | +#include "taginfo_impl.hpp" |
| 17 | + |
| 18 | +#include <sys/types.h> |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +#include <boost/lexical_cast.hpp> |
| 22 | + |
| 23 | +#include "tests/middle-tests.hpp" |
| 24 | +#include "tests/common-pg.hpp" |
| 25 | +#include "tests/common.hpp" |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +struct skip_test : public std::exception { |
| 30 | + const char *what() const noexcept { return "Test skipped."; } |
| 31 | +}; |
| 32 | + |
| 33 | +void run_test(const char* test_name, void (*testfunc)()) { |
| 34 | + try { |
| 35 | + fprintf(stderr, "%s\n", test_name); |
| 36 | + testfunc(); |
| 37 | + |
| 38 | + } catch (const skip_test &) { |
| 39 | + exit(77); // <-- code to skip this test. |
| 40 | + |
| 41 | + } catch (const std::exception& e) { |
| 42 | + fprintf(stderr, "%s\n", e.what()); |
| 43 | + fprintf(stderr, "FAIL\n"); |
| 44 | + exit(EXIT_FAILURE); |
| 45 | + } |
| 46 | + |
| 47 | + fprintf(stderr, "PASS\n"); |
| 48 | +} |
| 49 | +#define RUN_TEST(x) run_test(#x, &(x)) |
| 50 | + |
| 51 | +// "simple" test modeled on the basic regression test from |
| 52 | +// the python script. this is just to check everything is |
| 53 | +// working as expected before we start the complex stuff. |
| 54 | +void test_int4() { |
| 55 | + std::unique_ptr<pg::tempdb> db; |
| 56 | + |
| 57 | + try { |
| 58 | + db.reset(new pg::tempdb); |
| 59 | + } catch (const std::exception &e) { |
| 60 | + std::cerr << "Unable to setup database: " << e.what() << "\n"; |
| 61 | + throw skip_test(); |
| 62 | + } |
| 63 | + |
| 64 | + std::string proc_name("test-output-pgsql-int4"), input_file("-"); |
| 65 | + char *argv[] = { &proc_name[0], &input_file[0], nullptr }; |
| 66 | + |
| 67 | + options_t options = options_t(2, argv); |
| 68 | + options.database_options = db->database_options; |
| 69 | + options.num_procs = 1; |
| 70 | + options.slim = 1; |
| 71 | + options.prefix = "osm2pgsql_test"; |
| 72 | + options.style = "tests/test_output_pgsql_int4.style"; |
| 73 | + |
| 74 | + testing::run_osm2pgsql(options, "tests/test_output_pgsql_int4.osm", |
| 75 | + "xml"); |
| 76 | + |
| 77 | + db->assert_has_table("osm2pgsql_test_point"); |
| 78 | + db->assert_has_table("osm2pgsql_test_line"); |
| 79 | + db->assert_has_table("osm2pgsql_test_polygon"); |
| 80 | + db->assert_has_table("osm2pgsql_test_roads"); |
| 81 | + |
| 82 | + // First three nodes have population values that are out of range for int4 columns |
| 83 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 1"); |
| 84 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 2"); |
| 85 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 3"); |
| 86 | + // Check values that are valid for int4 columns, including limits |
| 87 | + db->check_count(2147483647, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 4"); |
| 88 | + db->check_count(10000, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 5"); |
| 89 | + db->check_count(-10000, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 6"); |
| 90 | + db->check_count(-2147483648, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 7"); |
| 91 | + // More out of range negative values |
| 92 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 8"); |
| 93 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 9"); |
| 94 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 10"); |
| 95 | + |
| 96 | + // Ranges are also parsed into int4 columns |
| 97 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 11"); |
| 98 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 12"); |
| 99 | + // Check values that are valid for int4 columns, including limits |
| 100 | + db->check_count(2147483647, "SELECT population FROM osm2pgsql_test_point WHERE osm_id =13"); |
| 101 | + db->check_count(15000, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 14"); |
| 102 | + db->check_count(-15000, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 15"); |
| 103 | + db->check_count(-2147483648, "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 16"); |
| 104 | + // More out of range negative values |
| 105 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 17"); |
| 106 | + db->check_string("", "SELECT population FROM osm2pgsql_test_point WHERE osm_id = 18"); |
| 107 | +} |
| 108 | + |
| 109 | +} // anonymous namespace |
| 110 | + |
| 111 | +int main(int argc, char *argv[]) { |
| 112 | + (void)argc; |
| 113 | + (void)argv; |
| 114 | + RUN_TEST(test_int4); |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments