Skip to content

Commit 8212653

Browse files
authored
Merge pull request #1511 from joto/remove-lexical-cast
Remove use of boost::lexical_cast
2 parents 1a44b1b + 0d8d829 commit 8212653

File tree

7 files changed

+46
-38
lines changed

7 files changed

+46
-38
lines changed

tests/common-pg.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include <stdexcept>
1616
#include <string>
1717

18-
#include <boost/lexical_cast.hpp>
19-
2018
#include "format.hpp"
2119
#include "options.hpp"
2220
#include "pgsql.hpp"
@@ -26,6 +24,9 @@
2624
#include <process.h>
2725
#include <windows.h>
2826
#define getpid _getpid
27+
#else
28+
#include <sys/types.h>
29+
#include <unistd.h>
2930
#endif
3031

3132
namespace testing {
@@ -37,19 +38,31 @@ class conn_t : public pg_conn_t
3738
public:
3839
conn_t(std::string const &conninfo) : pg_conn_t(conninfo) {}
3940

40-
template <typename T>
41-
T require_scalar(std::string const &cmd) const
41+
std::string result_as_string(std::string const &cmd) const
4242
{
4343
pg_result_t const res = query(PGRES_TUPLES_OK, cmd);
4444
REQUIRE(res.num_tuples() == 1);
45+
return res.get_value_as_string(0, 0);
46+
}
47+
48+
int result_as_int(std::string const &cmd) const
49+
{
50+
return std::stoi(result_as_string(cmd));
51+
}
4552

46-
auto const str = res.get_value_as_string(0, 0);
47-
return boost::lexical_cast<T>(str);
53+
unsigned long result_as_ulong(std::string const &cmd) const
54+
{
55+
return std::stoul(result_as_string(cmd));
56+
}
57+
58+
double result_as_double(std::string const &cmd) const
59+
{
60+
return std::stod(result_as_string(cmd));
4861
}
4962

5063
void assert_double(double expected, std::string const &cmd) const
5164
{
52-
REQUIRE(Approx(expected).epsilon(0.01) == require_scalar<double>(cmd));
65+
REQUIRE(Approx(expected).epsilon(0.01) == result_as_double(cmd));
5366
}
5467

5568
void assert_null(std::string const &cmd) const
@@ -73,7 +86,7 @@ class conn_t : public pg_conn_t
7386
auto const query = "SELECT count(*) FROM {} {} {}"_format(
7487
table_name, (where.empty() ? "" : "WHERE"), where);
7588

76-
return require_scalar<unsigned long>(query);
89+
return result_as_ulong(query);
7790
}
7891

7992
void require_has_table(char const *table_name) const

tests/test-db-copy-mgr.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,13 @@ TEST_CASE("copy_mgr_t")
167167
"\"rr\rrr\",\"s\\\\l\"}"});
168168

169169
auto c = db.connect();
170-
CHECK(c.require_scalar<std::string>("SELECT a[4] FROM test_copy_mgr") ==
170+
CHECK(c.result_as_string("SELECT a[4] FROM test_copy_mgr") ==
171171
"with \"quote\"");
172-
CHECK(c.require_scalar<std::string>("SELECT a[5] FROM test_copy_mgr") ==
173-
"the\t");
174-
CHECK(c.require_scalar<std::string>("SELECT a[6] FROM test_copy_mgr") ==
172+
CHECK(c.result_as_string("SELECT a[5] FROM test_copy_mgr") == "the\t");
173+
CHECK(c.result_as_string("SELECT a[6] FROM test_copy_mgr") ==
175174
"line\nbreak");
176-
CHECK(c.require_scalar<std::string>("SELECT a[7] FROM test_copy_mgr") ==
177-
"rr\rrr");
178-
CHECK(c.require_scalar<std::string>("SELECT a[8] FROM test_copy_mgr") ==
179-
"s\\l");
175+
CHECK(c.result_as_string("SELECT a[7] FROM test_copy_mgr") == "rr\rrr");
176+
CHECK(c.result_as_string("SELECT a[8] FROM test_copy_mgr") == "s\\l");
180177
}
181178

182179
SECTION("Insert hashes")
@@ -194,7 +191,7 @@ TEST_CASE("copy_mgr_t")
194191
auto c = db.connect();
195192

196193
for (auto const &v : values) {
197-
auto const res = c.require_scalar<std::string>(
194+
auto const res = c.result_as_string(
198195
"SELECT h->'{}' FROM test_copy_mgr"_format(v.first));
199196
CHECK(res == v.second);
200197
}

tests/test-db-copy-thread.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ static testing::pg::tempdb_t db;
1818
static int table_count(testing::pg::conn_t const &conn,
1919
std::string const &where = "")
2020
{
21-
return conn.require_scalar<int>("SELECT count(*) FROM test_copy_thread " +
22-
where);
21+
return conn.result_as_int("SELECT count(*) FROM test_copy_thread " + where);
2322
}
2423

2524
TEST_CASE("db_copy_thread_t with db_deleter_by_id_t")
@@ -46,8 +45,8 @@ TEST_CASE("db_copy_thread_t with db_deleter_by_id_t")
4645
t.add_buffer(std::unique_ptr<db_cmd_t>(cmd.release()));
4746
t.sync_and_wait();
4847

49-
REQUIRE(conn.require_scalar<int>(
50-
"SELECT id FROM test_copy_thread") == 42);
48+
REQUIRE(conn.result_as_int("SELECT id FROM test_copy_thread") ==
49+
42);
5150
}
5251

5352
SECTION("add multiple rows and sync")
@@ -67,8 +66,7 @@ TEST_CASE("db_copy_thread_t with db_deleter_by_id_t")
6766
t.add_buffer(std::unique_ptr<db_cmd_t>(cmd.release()));
6867
t.finish();
6968

70-
REQUIRE(conn.require_scalar<int>(
71-
"SELECT id FROM test_copy_thread") == 2);
69+
REQUIRE(conn.result_as_int("SELECT id FROM test_copy_thread") == 2);
7270
}
7371
}
7472

tests/test-options-projection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ TEST_CASE("Projection setup")
8484

8585
auto conn = db.connect();
8686

87-
CHECK(conn.require_scalar<std::string>(
87+
CHECK(conn.result_as_string(
8888
"SELECT Find_SRID('public', 'planet_osm_roads', 'way')") == srid);
8989
}

tests/test-output-gazetteer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class gazetteer_fixture_t
232232
char const *cls, char const *column)
233233
{
234234
char const tchar = m_opl_factory.type();
235-
return conn.require_scalar<std::string>(
235+
return conn.result_as_string(
236236
"SELECT {} FROM place WHERE osm_type = '{}' AND osm_id = {}"
237237
" AND class = '{}'"_format(column, tchar, id, cls));
238238
}

tests/test-output-pgsql-int4.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ TEST_CASE("int4 conversion")
3737
conn.assert_null(population(3));
3838

3939
// Check values that are valid for int4 columns, including limits
40-
CHECK(2147483647 == conn.require_scalar<int>(population(4)));
41-
CHECK(10000 == conn.require_scalar<int>(population(5)));
42-
CHECK(-10000 == conn.require_scalar<int>(population(6)));
43-
CHECK(-2147483648 == conn.require_scalar<int>(population(7)));
40+
CHECK(2147483647 == conn.result_as_int(population(4)));
41+
CHECK(10000 == conn.result_as_int(population(5)));
42+
CHECK(-10000 == conn.result_as_int(population(6)));
43+
CHECK(-2147483648 == conn.result_as_int(population(7)));
4444

4545
// More out of range negative values
4646
conn.assert_null(population(8));
@@ -52,10 +52,10 @@ TEST_CASE("int4 conversion")
5252
conn.assert_null(population(12));
5353

5454
// Check values that are valid for int4 columns, including limits
55-
CHECK(2147483647 == conn.require_scalar<int>(population(13)));
56-
CHECK(15000 == conn.require_scalar<int>(population(14)));
57-
CHECK(-15000 == conn.require_scalar<int>(population(15)));
58-
CHECK(-2147483648 == conn.require_scalar<int>(population(16)));
55+
CHECK(2147483647 == conn.result_as_int(population(13)));
56+
CHECK(15000 == conn.result_as_int(population(14)));
57+
CHECK(-15000 == conn.result_as_int(population(15)));
58+
CHECK(-2147483648 == conn.result_as_int(population(16)));
5959

6060
// More out of range negative values
6161
conn.assert_null(population(17));
@@ -68,5 +68,5 @@ TEST_CASE("int4 conversion")
6868
conn.assert_null(population(22));
6969

7070
// Zero is a valid value
71-
CHECK(0 == conn.require_scalar<int>(population(23)));
71+
CHECK(0 == conn.result_as_int(population(23)));
7272
}

tests/test-output-pgsql-z_order.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ TEST_CASE("compute Z order")
2828
auto const sql = "SELECT highway FROM osm2pgsql_test_line"
2929
" WHERE layer IS NULL ORDER BY z_order DESC"
3030
" LIMIT 1 OFFSET {}"_format(i);
31-
REQUIRE(expected[i] == conn.require_scalar<std::string>(sql));
31+
REQUIRE(expected[i] == conn.result_as_string(sql));
3232
}
3333

34-
REQUIRE("residential" == conn.require_scalar<std::string>(
35-
"SELECT highway FROM osm2pgsql_test_line "
36-
"ORDER BY z_order DESC LIMIT 1 OFFSET 0"));
34+
REQUIRE("residential" ==
35+
conn.result_as_string("SELECT highway FROM osm2pgsql_test_line "
36+
"ORDER BY z_order DESC LIMIT 1 OFFSET 0"));
3737
}

0 commit comments

Comments
 (0)