Merge branch 'master' into sf-ankerl

This commit is contained in:
Siarhei Fedartsou
2024-06-25 17:53:09 +02:00
89 changed files with 1569 additions and 1017 deletions
+27 -4
View File
@@ -1,16 +1,39 @@
#ifndef UNIT_TESTS_TEMPORARY_FILE_HPP
#define UNIT_TESTS_TEMPORARY_FILE_HPP
#include <boost/filesystem.hpp>
#include <cstdlib>
#include <filesystem>
#include <random>
#include <string>
inline std::string random_string(std::string::size_type length)
{
static auto &chrs = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
thread_local static std::mt19937 rg{std::random_device{}()};
thread_local static std::uniform_int_distribution<std::string::size_type> pick(
0, sizeof(chrs) - 2);
std::string s;
s.reserve(length);
while (length--)
{
s += chrs[pick(rg)];
}
return s;
}
struct TemporaryFile
{
TemporaryFile() : path(boost::filesystem::unique_path()) {}
TemporaryFile() : path(std::filesystem::temp_directory_path() / random_string(8)) {}
TemporaryFile(const std::string &path) : path(path) {}
~TemporaryFile() { boost::filesystem::remove(path); }
~TemporaryFile() { std::filesystem::remove(path); }
boost::filesystem::path path;
std::filesystem::path path;
};
#endif // UNIT_TESTS_TEMPORARY_FILE_HPP
+94
View File
@@ -74,4 +74,98 @@ BOOST_AUTO_TEST_CASE(hint_encoding_decoding_roundtrip_bytewise)
reinterpret_cast<const unsigned char *>(&decoded)));
}
BOOST_AUTO_TEST_CASE(long_string_encoding)
{
using namespace osrm::engine;
std::string long_string(1000, 'A'); // String of 1000 'A's
std::string encoded = encodeBase64(long_string);
BOOST_CHECK_EQUAL(decodeBase64(encoded), long_string);
}
BOOST_AUTO_TEST_CASE(invalid_base64_decoding)
{
using namespace osrm::engine;
BOOST_CHECK_THROW(decodeBase64("Invalid!"), std::exception);
}
BOOST_AUTO_TEST_CASE(hint_serialization_size)
{
using namespace osrm::engine;
using namespace osrm::util;
const Coordinate coordinate;
const PhantomNode phantom;
const osrm::test::MockDataFacade<osrm::engine::routing_algorithms::ch::Algorithm> facade{};
const SegmentHint hint{phantom, facade.GetCheckSum()};
const auto base64 = hint.ToBase64();
BOOST_CHECK_EQUAL(base64.size(), 112);
}
BOOST_AUTO_TEST_CASE(extended_roundtrip_tests)
{
using namespace osrm::engine;
std::vector<std::string> test_strings = {
"Hello, World!", // Simple ASCII string
"1234567890", // Numeric string
"!@#$%^&*()_+", // Special characters
std::string(1000, 'A'), // Long repeating string
"¡Hola, mundo!", // Non-ASCII characters
"こんにちは、世界!", // Unicode characters
std::string("\x00\x01\x02\x03", 4), // Binary data
"a", // Single character
"ab", // Two characters
"abc", // Three characters (no padding in Base64)
std::string(190, 'x') // String that doesn't align with Base64 padding
};
for (const auto &test_str : test_strings)
{
std::string encoded = encodeBase64(test_str);
std::string decoded = decodeBase64(encoded);
BOOST_CHECK_EQUAL(decoded, test_str);
// Additional checks
BOOST_CHECK(encoded.find_first_not_of(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=") ==
std::string::npos);
if (test_str.length() % 3 != 0)
{
BOOST_CHECK(encoded.back() == '=');
}
}
}
BOOST_AUTO_TEST_CASE(roundtrip_with_url_safe_chars)
{
using namespace osrm::engine;
std::string original = "Hello+World/Nothing?Is:Impossible";
std::string encoded = encodeBase64(original);
// Replace '+' with '-' and '/' with '_'
std::replace(encoded.begin(), encoded.end(), '+', '-');
std::replace(encoded.begin(), encoded.end(), '/', '_');
std::string decoded = decodeBase64(encoded);
BOOST_CHECK_EQUAL(decoded, original);
}
BOOST_AUTO_TEST_CASE(roundtrip_stress_test)
{
using namespace osrm::engine;
std::string test_str;
for (int i = 0; i < 1000; ++i)
{
test_str += static_cast<char>(i % 256);
}
std::string encoded = encodeBase64(test_str);
std::string decoded = decodeBase64(encoded);
BOOST_CHECK_EQUAL(decoded, test_str);
}
BOOST_AUTO_TEST_SUITE_END()
+92
View File
@@ -45,4 +45,96 @@ BOOST_AUTO_TEST_CASE(polyline6_test_case)
decodePolyline<1000000>(encodePolyline<1000000>(coords.begin(), coords.end())).begin()));
}
BOOST_AUTO_TEST_CASE(empty_polyline_test)
{
using namespace osrm::engine;
using namespace osrm::util;
std::vector<Coordinate> empty_coords;
BOOST_CHECK_EQUAL(encodePolyline(empty_coords.begin(), empty_coords.end()), "");
BOOST_CHECK(decodePolyline("").empty());
}
BOOST_AUTO_TEST_CASE(polyline_single_point_test)
{
using namespace osrm::engine;
using namespace osrm::util;
const std::vector<Coordinate> coords({{FixedLongitude{-122414000}, FixedLatitude{37776000}}});
const std::string encoded = encodePolyline(coords.begin(), coords.end());
BOOST_CHECK_EQUAL(encoded, "_cqeFn~cjV");
const auto decoded = decodePolyline(encoded);
BOOST_CHECK_EQUAL(decoded.size(), 1);
BOOST_CHECK_EQUAL(decoded[0].lon, FixedLongitude{-122414000});
BOOST_CHECK_EQUAL(decoded[0].lat, FixedLatitude{37776000});
}
BOOST_AUTO_TEST_CASE(polyline_multiple_points_test)
{
using namespace osrm::engine;
using namespace osrm::util;
const std::vector<Coordinate> coords({{FixedLongitude{-122414000}, FixedLatitude{37776000}},
{FixedLongitude{-122420000}, FixedLatitude{37779000}},
{FixedLongitude{-122421000}, FixedLatitude{37780000}}});
const std::string encoded = encodePolyline(coords.begin(), coords.end());
BOOST_CHECK_EQUAL(encoded, "_cqeFn~cjVwQnd@gEfE");
const auto decoded = decodePolyline(encoded);
BOOST_CHECK_EQUAL(decoded.size(), 3);
for (size_t i = 0; i < coords.size(); ++i)
{
BOOST_CHECK_EQUAL(decoded[i].lon, coords[i].lon);
BOOST_CHECK_EQUAL(decoded[i].lat, coords[i].lat);
}
}
BOOST_AUTO_TEST_CASE(polyline_large_coordinate_difference_test)
{
using namespace osrm::engine;
using namespace osrm::util;
const std::vector<Coordinate> coords({{FixedLongitude{-179000000}, FixedLatitude{-89000000}},
{FixedLongitude{179000000}, FixedLatitude{89000000}}});
const std::string encoded = encodePolyline(coords.begin(), coords.end());
BOOST_CHECK_EQUAL(encoded, "~xe~O~|oca@_sl}`@_{`hcA");
const auto decoded = decodePolyline(encoded);
BOOST_CHECK_EQUAL(decoded.size(), 2);
for (size_t i = 0; i < coords.size(); ++i)
{
BOOST_CHECK_EQUAL(decoded[i].lon, coords[i].lon);
BOOST_CHECK_EQUAL(decoded[i].lat, coords[i].lat);
}
}
BOOST_AUTO_TEST_CASE(roundtrip)
{
using namespace osrm::engine;
using namespace osrm::util;
{
const auto encoded = "_chxEn`zvN\\\\]]";
const auto decoded = decodePolyline(encoded);
const auto reencoded = encodePolyline(decoded.begin(), decoded.end());
BOOST_CHECK_EQUAL(encoded, reencoded);
}
{
const auto encoded =
"gcneIpgxzRcDnBoBlEHzKjBbHlG`@`IkDxIiKhKoMaLwTwHeIqHuAyGXeB~Ew@fFjAtIzExF";
const auto decoded = decodePolyline(encoded);
const auto reencoded = encodePolyline(decoded.begin(), decoded.end());
BOOST_CHECK_EQUAL(encoded, reencoded);
}
{
const auto encoded = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
const auto decoded = decodePolyline(encoded);
const auto reencoded = encodePolyline(decoded.begin(), decoded.end());
BOOST_CHECK_EQUAL(encoded, reencoded);
}
}
BOOST_AUTO_TEST_SUITE_END()
@@ -1,11 +1,14 @@
#include "extractor/location_dependent_data.hpp"
#include "../common/range_tools.hpp"
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <random>
#include <vector>
BOOST_AUTO_TEST_SUITE(location_dependent_data_tests)
@@ -16,14 +19,13 @@ using point_t = LocationDependentData::point_t;
struct LocationDataFixture
{
LocationDataFixture(const std::string &json) : temporary_file(boost::filesystem::unique_path())
LocationDataFixture(const std::string &json)
{
std::ofstream file(temporary_file.string());
std::ofstream file(temporary_file.path.string());
file << json;
}
~LocationDataFixture() { remove(temporary_file); }
boost::filesystem::path temporary_file;
TemporaryFile temporary_file;
};
BOOST_AUTO_TEST_CASE(polygon_tests)
@@ -50,7 +52,7 @@ BOOST_AUTO_TEST_CASE(polygon_tests)
}
]})json");
LocationDependentData data({fixture.temporary_file});
LocationDependentData data({fixture.temporary_file.path});
BOOST_CHECK(data.GetPropertyIndexes(point_t(0, 0)).empty());
CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(1, 1)), 0);
@@ -86,7 +88,7 @@ BOOST_AUTO_TEST_CASE(multy_polygon_tests)
}
]})json");
LocationDependentData data({fixture.temporary_file});
LocationDependentData data({fixture.temporary_file.path});
BOOST_CHECK(data.GetPropertyIndexes(point_t(0, 2)).empty());
BOOST_CHECK(data.GetPropertyIndexes(point_t(0, -3)).empty());
@@ -117,7 +119,7 @@ BOOST_AUTO_TEST_CASE(polygon_merging_tests)
}
]})json");
LocationDependentData data({fixture.temporary_file});
LocationDependentData data({fixture.temporary_file.path});
CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-3, 3)), 0);
CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-3, 1)), 0);
@@ -153,7 +155,7 @@ BOOST_AUTO_TEST_CASE(staircase_polygon)
}
]})json");
LocationDependentData data({fixture.temporary_file});
LocationDependentData data({fixture.temporary_file.path});
// all corners
BOOST_CHECK(!data.GetPropertyIndexes(point_t(0, 0)).empty());
+1 -1
View File
@@ -3,9 +3,9 @@
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>
+2 -1
View File
@@ -4,9 +4,10 @@
#include <osrm/coordinate.hpp>
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
BOOST_AUTO_TEST_SUITE(raster_source)
using namespace osrm;
+1 -1
View File
@@ -3,10 +3,10 @@
#include "../common/range_tools.hpp"
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <cmath>
#include <filesystem>
BOOST_AUTO_TEST_SUITE(data_layout)
+1 -1
View File
@@ -5,9 +5,9 @@
#include "../common/range_tools.hpp"
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
#include <random>
BOOST_AUTO_TEST_SUITE(serialization)
+3 -2
View File
@@ -2,9 +2,10 @@
#include "util/geojson_validation.hpp"
#include "util/timezones.hpp"
#include <boost/filesystem/path.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
BOOST_AUTO_TEST_SUITE(timezoner)
using namespace osrm;
@@ -22,7 +23,7 @@ BOOST_AUTO_TEST_CASE(timezoner_test)
std::time_t now = time(0);
BOOST_CHECK_NO_THROW(Timezoner tz(json, now));
boost::filesystem::path test_path(TEST_DATA_DIR "/test.geojson");
std::filesystem::path test_path(TEST_DATA_DIR "/test.geojson");
BOOST_CHECK_NO_THROW(Timezoner tz(test_path, now));
// missing opening bracket
+2 -1
View File
@@ -3,9 +3,10 @@
#include "../common/range_tools.hpp"
#include "../common/temporary_file.hpp"
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <filesystem>
BOOST_AUTO_TEST_SUITE(serialization)
using namespace osrm;
+1 -1
View File
@@ -227,7 +227,7 @@ void sampling_verify_rtree(RTreeT &rtree,
}
template <typename RTreeT, typename FixtureT>
auto make_rtree(const boost::filesystem::path &path, FixtureT &fixture)
auto make_rtree(const std::filesystem::path &path, FixtureT &fixture)
{
return RTreeT(fixture.edges, fixture.coords, path);
}