feat: add hex

This commit is contained in:
2026-01-29 23:22:17 +01:00
parent 110d1df50d
commit f49802a1a6
4 changed files with 179 additions and 0 deletions

81
tests/test_hex.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "catch.hpp"
#include <cpputils/hex.h>
#include <string>
using namespace cpputils::hex;
TEST_CASE("Hex conversion round-trip works", "[hex]") {
std::string original = "Hello, World!";
std::string hex = toHex(original);
std::string result = fromHex(hex);
REQUIRE(result == original);
}
TEST_CASE("Empty string conversion", "[hex]") {
std::string empty = "";
std::string hex = toHex(empty);
std::string result = fromHex(hex);
REQUIRE(hex.empty());
REQUIRE(result.empty());
}
TEST_CASE("Single character conversion", "[hex]") {
std::string single = "A";
std::string hex = toHex(single);
std::string result = fromHex(hex);
REQUIRE(result == single);
}
TEST_CASE("Non-printable ASCII characters", "[hex]") {
std::string nonPrintable;
for (char c = 0; c < 32; ++c) {
nonPrintable += c;
}
std::string hex = toHex(nonPrintable);
std::string result = fromHex(hex);
REQUIRE(result == nonPrintable);
}
TEST_CASE("Extended ASCII characters", "[hex]") {
std::string extended;
for (unsigned char c = 128; c < 255; ++c) {
extended += static_cast<char>(c);
}
std::string hex = toHex(extended);
std::string result = fromHex(hex);
REQUIRE(result == extended);
}
TEST_CASE("Hex string with lowercase and uppercase letters", "[hex]") {
std::string original = "abcABC123";
std::string hex = toHex(original);
std::string result = fromHex(hex);
REQUIRE(result == original);
}
TEST_CASE("Odd-length hex string should fail", "[hex]") {
std::string invalidHex = "A";
REQUIRE(fromHex(invalidHex).empty());
}
TEST_CASE("Hex string with invalid characters should fail", "[hex]") {
std::string invalidHex = "ZZ";
REQUIRE(fromHex(invalidHex).empty());
}
TEST_CASE("Large string conversion", "[hex]") {
std::string large(10000, 'x');
std::string hex = toHex(large);
std::string result = fromHex(hex);
REQUIRE(result == large);
}