Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/tests/proquint.test.nu

Raw
use ../modules/proquint.nu

if (proquint 0x[]) != "" { error make {msg: "empty encoding changed"} }
if (proquint 0x[00]) != "bab" { error make {msg: "single zero byte must use a partial block"} }
if (proquint 0x[61]) != "kad" { error make {msg: "odd byte encoded incorrectly"} }
if (proquint 0x[61 00]) != "kahab" { error make {msg: "two-byte word encoded incorrectly"} }
if (proquint 0x[7f 00 00 01]) != "lusab-babad" { error make {msg: "IPv4 bytes differ from the paper's example"} }
if (proquint decode "") != 0x[] { error make {msg: "empty decoding changed"} }
if (proquint decode "lusab-babad") != 0x[7f 00 00 01] { error make {msg: "full-word decoding failed"} }
if (proquint decode "kad") != 0x[61] { error make {msg: "partial-word decoding failed"} }
if (0x[61 00 62] | proquint) != "kahab-kaf" { error make {msg: "pipelined encoding failed"} }
if ("kahab-kaf" | proquint decode) != 0x[61 00 62] { error make {msg: "pipelined decoding failed"} }

for number in 0..255 {
    let bytes = $number | into binary --compact --endian big
    if (proquint decode (proquint $bytes)) != $bytes {
        error make {msg: $"single-byte round trip failed for ($number)"}
    }
}

for bytes in [0x[00 00] 0x[ff ff] 0x[00 01 00] 0x[ff 00 01 ff 80] 0x[7f 00 00 01]] {
    if (proquint decode (proquint $bytes)) != $bytes {
        error make {msg: $"multi-byte round trip failed for ($bytes | encode hex)"}
    }
}

for invalid in ["ka" "kax" "kad-babab" "babab-" "-kad" "babab--kad" "babab-bav" "BABAB" "käd"] {
    let rejected = try { proquint decode $invalid; false } catch { true }
    if not $rejected { error make {msg: $"accepted invalid proquint: ($invalid)"} }
}

let rejected_text = try { "abc" | proquint; false } catch { true }
if not $rejected_text { error make {msg: "encoder accepted non-binary input"} }
let rejected_binary = try { 0x[61] | proquint decode; false } catch { true }
if not $rejected_binary { error make {msg: "decoder accepted non-text input"} }

"proquint tests passed"