72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
|
#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 == "日男昼持賃竹田");
|
||
|
}
|
||
|
}
|