[PercentEncoding] Add minimal percent encoding/decoding functions
This commit is contained in:
parent
ed6dc601cc
commit
2eefad4152
4 changed files with 180 additions and 0 deletions
17
Tests/PercentEncoding/CMakeLists.txt
Normal file
17
Tests/PercentEncoding/CMakeLists.txt
Normal file
|
@ -0,0 +1,17 @@
|
|||
add_executable(TestPercentEncoding)
|
||||
|
||||
target_compile_features(TestPercentEncoding
|
||||
PRIVATE
|
||||
cxx_std_20
|
||||
)
|
||||
|
||||
target_link_libraries(TestPercentEncoding
|
||||
PRIVATE
|
||||
Garbage
|
||||
Catch2::Catch2WithMain
|
||||
)
|
||||
|
||||
target_sources(TestPercentEncoding
|
||||
PRIVATE
|
||||
Tests.cpp
|
||||
)
|
71
Tests/PercentEncoding/Tests.cpp
Normal file
71
Tests/PercentEncoding/Tests.cpp
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <Garbage/PercentEncoding.hpp>
|
||||
|
||||
TEST_CASE("Percent encoding/decoding tests")
|
||||
{
|
||||
SECTION("Empty span")
|
||||
{
|
||||
std::vector<std::uint8_t> data;
|
||||
|
||||
std::string encoded = Garbage::Percent::Encode(data);
|
||||
REQUIRE(encoded.size() == 0);
|
||||
|
||||
REQUIRE(Garbage::Percent::Validate(encoded));
|
||||
|
||||
std::vector<std::uint8_t> decoded = Garbage::Percent::Decode(encoded);
|
||||
REQUIRE(encoded.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Validation rejections")
|
||||
{
|
||||
constexpr std::string_view invalid1 = "Random chars";
|
||||
REQUIRE_FALSE(Garbage::Percent::Validate(invalid1));
|
||||
|
||||
constexpr std::string_view invalid2 = "%AFFAF%AF";
|
||||
REQUIRE_FALSE(Garbage::Percent::Validate(invalid2));
|
||||
}
|
||||
|
||||
SECTION("Encoding short data")
|
||||
{
|
||||
constexpr std::string_view source("日");
|
||||
std::vector<std::uint8_t> data(source.begin(), source.end());
|
||||
|
||||
std::string encoded = Garbage::Percent::Encode(data);
|
||||
|
||||
REQUIRE(encoded == "%E6%97%A5");
|
||||
REQUIRE(Garbage::Percent::Validate(encoded));
|
||||
}
|
||||
|
||||
SECTION("Decoding short data")
|
||||
{
|
||||
std::string data("%E6%97%A5");
|
||||
|
||||
REQUIRE(Garbage::Percent::Validate(data));
|
||||
|
||||
std::vector<std::uint8_t> decoded = Garbage::Percent::Decode(data);
|
||||
std::string destination(decoded.begin(), decoded.end());
|
||||
REQUIRE(destination == "日");
|
||||
}
|
||||
|
||||
SECTION("Encoding longer data")
|
||||
{
|
||||
constexpr std::string_view source("日男昼持賃竹田");
|
||||
std::vector<std::uint8_t> data(source.begin(), source.end());
|
||||
|
||||
std::string encoded = Garbage::Percent::Encode(data);
|
||||
|
||||
REQUIRE(encoded == "%E6%97%A5%E7%94%B7%E6%98%BC%E6%8C%81%E8%B3%83%E7%AB%B9%E7%94%B0");
|
||||
REQUIRE(Garbage::Percent::Validate(encoded));
|
||||
}
|
||||
|
||||
SECTION("Decoding longer data")
|
||||
{
|
||||
std::string data("%E6%97%A5%E7%94%B7%E6%98%BC%E6%8C%81%E8%B3%83%E7%AB%B9%E7%94%B0");
|
||||
|
||||
REQUIRE(Garbage::Percent::Validate(data));
|
||||
|
||||
std::vector<std::uint8_t> decoded = Garbage::Percent::Decode(data);
|
||||
std::string destination(decoded.begin(), decoded.end());
|
||||
REQUIRE(destination == "日男昼持賃竹田");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue