Avoid reallocations in base64 encoding (#6951)

This commit is contained in:
Siarhei Fedartsou
2024-06-22 08:50:18 +02:00
committed by GitHub
parent e8da3d9231
commit 3d01d96036
3 changed files with 111 additions and 11 deletions
+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()