bin-encode-decode 11.0.0

A high-performance binary encode and decode library that supports customizable character sets beyond Base64.
Documentation
use super::*;

#[test]
fn test_crypt_decode() {
    let mut en_decode: Charset<'_> = Charset::new();
    let test_str: &str = "test";
    let mut charset: String = String::new();
    for i in 0..26 {
        let ch: char = (b'a' + i) as char;
        charset.push(ch);
    }
    for i in 0..26 {
        let ch: char = (b'A' + i) as char;
        charset.push(ch);
    }
    for i in 0..10 {
        let ch: char = (b'0' + i) as char;
        charset.push(ch);
    }
    charset.push_str("_=");
    en_decode.charset(&charset);
    let encode: Result<String, EncodeError> = en_decode.encode(test_str);
    let decode: Result<String, DecodeError> = en_decode.decode(&encode.clone().unwrap());
    assert_eq!(decode.unwrap(), test_str);
}

#[test]
fn test_invalid_charsets() {
    let short_charset: &str = "abcdef";
    assert!(Encode::execute(short_charset, "abc").is_err());
    assert!(Decode::execute(short_charset, "aaaa").is_err());
    let dup_charset: String = "a".repeat(64);
    assert!(Encode::execute(&dup_charset, "abc").is_err());
    assert!(Decode::execute(&dup_charset, "aaaa").is_err());
    let too_many_unique: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=!";
    assert!(Encode::execute(too_many_unique, "abc").is_err());
    assert!(Decode::execute(too_many_unique, "aaaa").is_err());
    let short_unicode: &str = "αβγδεζηθ";
    assert!(Encode::execute(short_unicode, "abc").is_err());
    assert!(Decode::execute(short_unicode, "αααα").is_err());
}

#[test]
fn test_utf8_empty_and_general_decode_groups() {
    let ascii_charset: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=";
    let empty_enc: String = Encode::execute(ascii_charset, "").unwrap();
    assert_eq!(empty_enc, "");
    assert_eq!(Decode::execute(ascii_charset, &empty_enc).unwrap(), "");
    let payload: &str = "Hello, 世界! 🚀 bin-encode-decode 0123456789";
    let encoded: String = Encode::execute(ascii_charset, payload).unwrap();
    assert_eq!(Decode::execute(ascii_charset, &encoded).unwrap(), payload);
    let mut noisy: String = String::new();
    for (idx, ch) in encoded.chars().enumerate() {
        if idx % 5 == 0 {
            noisy.push('!');
        }
        noisy.push(ch);
    }
    assert_eq!(Decode::execute(ascii_charset, &noisy).unwrap(), payload);
    let combined: usize = ((b'A' as usize) << 16) | ((b'B' as usize) << 8) | (b'C' as usize);
    let bytes: &[u8] = ascii_charset.as_bytes();
    let mut three_byte_group: String = String::new();
    three_byte_group.push(bytes[(combined >> 18) & 0x3F] as char);
    three_byte_group.push(bytes[(combined >> 12) & 0x3F] as char);
    three_byte_group.push(bytes[(combined >> 6) & 0x3F] as char);
    three_byte_group.push(bytes[combined & 0x3F] as char);
    assert_eq!(
        Decode::execute(ascii_charset, &three_byte_group).unwrap(),
        "ABC"
    );
    let b1_only_combined: usize = ((b'Z' as usize) << 8) | (b'Y' as usize);
    let mut b1_group: String = String::new();
    b1_group.push(bytes[(b1_only_combined >> 18) & 0x3F] as char);
    b1_group.push(bytes[(b1_only_combined >> 12) & 0x3F] as char);
    b1_group.push(bytes[(b1_only_combined >> 6) & 0x3F] as char);
    b1_group.push(bytes[b1_only_combined & 0x3F] as char);
    assert_eq!(Decode::execute(ascii_charset, &b1_group).unwrap(), "ZY");
}

#[test]
fn test_unicode_charset_and_duplicate_suffix_charset() {
    let unicode_charset: String = (0x4E00u32..0x4E40u32).filter_map(char::from_u32).collect();
    let payload: &str = "Unicode charset roundtrip: 测试123";
    let encoded: String = Encode::execute(&unicode_charset, payload).unwrap();
    let decoded: String = Decode::execute(&unicode_charset, &encoded).unwrap();
    assert_eq!(decoded, payload);
    let dup_valid_charset: &str =
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=ab";
    let enc_dup: String = Encode::execute(dup_valid_charset, payload).unwrap();
    let dec_dup: String = Decode::execute(dup_valid_charset, &enc_dup).unwrap();
    assert_eq!(dec_dup, payload);
}

#[test]
fn test_long_charset_index_255_and_above() {
    let base: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
    let mut long_ascii: String = String::with_capacity(257);
    long_ascii.push_str(base);
    for _ in 0..192 {
        long_ascii.push('a');
    }
    long_ascii.push('=');
    assert_eq!(long_ascii.as_bytes()[255], b'=');
    let mut input_bytes: String = String::from("aa=d");
    input_bytes.push('a');
    input_bytes.push('a');
    input_bytes.push(base.as_bytes()[2] as char);
    input_bytes.push(base.as_bytes()[41] as char);
    assert_eq!(Decode::execute(&long_ascii, &input_bytes).unwrap(), "?é");
    long_ascii.push('=');
    assert_eq!(Decode::execute(&long_ascii, &input_bytes).unwrap(), "?é");
    let mut long_unicode: String = (0x4E00u32..0x4E3Fu32).filter_map(char::from_u32).collect();
    let u0: char = long_unicode.chars().next().unwrap();
    let u2: char = long_unicode.chars().nth(2).unwrap();
    let u3: char = long_unicode.chars().nth(3).unwrap();
    let u41: char = long_unicode.chars().nth(41).unwrap();
    for _ in 0..192 {
        long_unicode.push(u0);
    }
    let u255: char = char::from_u32(0x4E3F).unwrap();
    long_unicode.push(u255);
    long_unicode.push(u255);
    let unicode_encoded: String = [u0, u0, u255, u3, u0, u0, u2, u41].into_iter().collect();
    assert_eq!(
        Decode::execute(&long_unicode, &unicode_encoded).unwrap(),
        "?é"
    );
}