Files
c3c/test/unit/stdlib/encoding/hex.c3
Koni Marti b0c0fd7dc8 encoding: implement hex encoding (base16)
Implement hex encoding and decoding (base16) according to RFC 4648.
Add unit tests.

Link: https://www.rfc-editor.org/rfc/rfc4648
2024-11-25 11:41:22 +01:00

46 lines
911 B
Plaintext

module encoding::hex @test;
import std::encoding::hex;
struct TestCase
{
char[] dec;
char[] enc;
}
TestCase[] tests = {
{"", ""},
{{'g'}, "67"},
{{0,1,2,3,4,5,6,7}, "0001020304050607"},
{{8,9,10,11,12,13,14,15}, "08090a0b0c0d0e0f"},
{{0xf0, 0xf1, 0xf2, 0xf3}, "f0f1f2f3"},
{{0xe3, 0xa1}, "e3a1"},
{{0xe3, 0xa1}, "E3A1"},
};
fn void! encode() {
usz n;
char[64] buf;
foreach (t : tests) {
n = hex::encode_bytes(t.dec, buf[..])!;
String want = ((String)t.enc).temp_ascii_to_lower();
assert(want == buf[:n], "encode failed: got: %s, want: %s", buf[:n], want);
@pool()
{
assert(want == hex::encode_temp(t.dec)!);
};
}
}
fn void! decode() {
usz n;
char[64] buf;
foreach (t : tests) {
n = hex::decode_bytes(t.enc, buf[..])!;
assert(t.dec == buf[:n], "decode failed: got: %s, want: %s", buf[:n], t.dec);
@pool()
{
assert(t.dec == hex::decode_temp(t.enc)!);
};
}
}