u64_dyn 0.1.4

Variable-length 64-bit integer codings that take at most 9 bytes.
Documentation
use u64_dyn::*;

#[test]
fn test_u64_dyn() {
    let cases = [
        (0u64, vec![0x00]),
        (0x7f, vec![0x7f]),
        (0x80, vec![0x80, 0x01]),
        (1337, vec![0xb9, 0x0a]),
        (42069, vec![0xd5, 0xc8, 0x02]),
        (0xffffffffffffffff, vec![0xff; 9]),
        (0x8000000000000000, vec![0x80; 9]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_u64_dyn(val), enc);
        assert_eq!(unpack_u64_dyn(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_u64_dyn_b() {
    let cases = [
        (0u64, vec![0x00]),
        (0x7f, vec![0x7f]),
        (0x80, vec![0x80, 0x00]),
        (1337, vec![0xb9, 0x09]),
        (42069, vec![0xd5, 0xc7, 0x01]),
        (0x123456789abcdef, vec![0xef, 0x9a, 0xae, 0xcc, 0xf7, 0xab, 0xd0, 0x90, 0x00]),
        (0xffffffffffffffff, vec![0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]),
        (0x8000000000000000, vec![0x80, 0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0x7e]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_u64_dyn_b(val), enc);
        assert_eq!(unpack_u64_dyn_b(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_u64_dyn_p() {
    let cases = [
        (0u64, vec![0x00]),
        (0x7f, vec![0x7f]),
        (0x80, vec![0x80, 0x02]),
        (1337, vec![0xb9, 0x14]),
        (42069, vec![0xd5, 0x22, 0x05]),
        (0xffffffffffffffff, vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
        (0x8000000000000000, vec![0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_u64_dyn_p(val), enc);
        assert_eq!(unpack_u64_dyn_p(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_u64_dyn_bp() {
    let cases = [
        (0u64, vec![0x00]),
        (0x7f, vec![0x7f]),
        (0x80, vec![0x80, 0x00]),
        (1337, vec![0xb9, 0x12]),
        (42069, vec![0xd5, 0x1e, 0x03]),
        (0xffffffffffffffff, vec![0xff, 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe]),
        (0x8000000000000000, vec![0xff, 0x80, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_u64_dyn_bp(val), enc);
        assert_eq!(unpack_u64_dyn_bp(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_i64_dyn_a() {
    let cases = [
        (0i64, vec![0x00]),
        (0x7f, vec![0xbf, 0x01]),
        (0x80, vec![0x80, 0x02]),
        (1337, vec![0xb9, 0x14]),
        (42069, vec![0x95, 0x91, 0x05]),
        (-1, vec![0x41]),
        (i64::MIN, vec![0x40]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_i64_dyn_a(val), enc);
        assert_eq!(unpack_i64_dyn_a(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_i64_dyn_b() {
    let cases = [
        (0i64, vec![0x00]),
        (0x7f, vec![0xbf, 0x00]),
        (0x80, vec![0x80, 0x01]),
        (1337, vec![0xb9, 0x13]),
        (42069, vec![0x95, 0x90, 0x04]),
        (-1, vec![0x40]),
        (i64::MIN, vec![0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_i64_dyn_b(val), enc);
        assert_eq!(unpack_i64_dyn_b(&enc), Some((val, enc.len())));
    }
}


#[test]
fn test_i64_dyn_p() {
    let cases = [
        (0i64, vec![0x00]),
        (0x7f, vec![0xbf, 0x02]),
        (0x80, vec![0x80, 0x04]),
        (1337, vec![0xb9, 0x28]),
        (42069, vec![0xd5, 0x44, 0x0a]),
        (-1, vec![0x40]),
        (i64::MIN, vec![0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_i64_dyn_p(val), enc);
        assert_eq!(unpack_i64_dyn_p(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_i64_dyn_bp() {
    let cases = [
        (0i64, vec![0x00]),
        (0x7f, vec![0xbf, 0x00]),
        (0x80, vec![0x80, 0x02]),
        (1337, vec![0xb9, 0x26]),
        (42069, vec![0xd5, 0x40, 0x08]),
        (-1, vec![0x40]),
        (i64::MIN, vec![0xff, 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe]),
    ];
    for (val, enc) in cases {
        assert_eq!(pack_i64_dyn_bp(val), enc);
        assert_eq!(unpack_i64_dyn_bp(&enc), Some((val, enc.len())));
    }
}

#[test]
fn test_truncated() {
    let truncated = [
        vec![],
        vec![0x80],
        vec![0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80],
    ];
    for enc in truncated {
        assert!(unpack_u64_dyn(&enc).is_none());
        assert!(unpack_i64_dyn_a(&enc).is_none());
        assert!(unpack_i64_dyn_b(&enc).is_none());
        assert!(unpack_u64_dyn_b(&enc).is_none());
    }
}

#[test]
fn test_truncated_p() {
    let truncated = [
        vec![],
        vec![0x80],
        vec![0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
    ];
    for enc in truncated {
        assert!(unpack_u64_dyn_p(&enc).is_none());
        assert!(unpack_u64_dyn_bp(&enc).is_none());
        assert!(unpack_i64_dyn_p(&enc).is_none());
        assert!(unpack_i64_dyn_bp(&enc).is_none());
    }
}

#[test]
fn test_overflow() {
    let enc = vec![0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe];
    assert!(unpack_u64_dyn_b(&enc).is_none());
    assert!(unpack_u64_dyn_bp(&enc).is_none());
    assert!(unpack_i64_dyn_b(&enc).is_none());
    assert!(unpack_i64_dyn_bp(&enc).is_none());
}