#![allow(
clippy::approx_constant,
clippy::useless_vec,
clippy::len_zero,
clippy::unnecessary_cast,
clippy::redundant_closure,
clippy::too_many_arguments,
clippy::type_complexity,
clippy::needless_borrow,
clippy::enum_variant_names,
clippy::upper_case_acronyms,
clippy::inconsistent_digit_grouping,
clippy::unit_cmp,
clippy::assertions_on_constants,
clippy::iter_on_single_items,
clippy::expect_fun_call,
clippy::redundant_pattern_matching,
variant_size_differences,
clippy::absurd_extreme_comparisons,
clippy::nonminimal_bool,
clippy::for_kv_map,
clippy::needless_range_loop,
clippy::single_match,
clippy::collapsible_if,
clippy::needless_return,
clippy::redundant_clone,
clippy::map_entry,
clippy::match_single_binding,
clippy::bool_comparison,
clippy::derivable_impls,
clippy::manual_range_contains,
clippy::needless_borrows_for_generic_args,
clippy::manual_map,
clippy::vec_init_then_push,
clippy::identity_op,
clippy::manual_flatten,
clippy::single_char_pattern,
clippy::search_is_some,
clippy::option_map_unit_fn,
clippy::while_let_on_iterator,
clippy::clone_on_copy,
clippy::box_collection,
clippy::redundant_field_names,
clippy::ptr_arg,
clippy::large_enum_variant,
clippy::match_ref_pats,
clippy::needless_pass_by_value,
clippy::unused_unit,
clippy::let_and_return,
clippy::suspicious_else_formatting,
clippy::manual_strip,
clippy::match_like_matches_macro,
clippy::from_over_into,
clippy::wrong_self_convention,
clippy::inherent_to_string,
clippy::new_without_default,
clippy::unnecessary_wraps,
clippy::field_reassign_with_default,
clippy::manual_find,
clippy::unnecessary_lazy_evaluations,
clippy::should_implement_trait,
clippy::missing_safety_doc,
clippy::unusual_byte_groupings,
clippy::bool_assert_comparison,
clippy::zero_prefixed_literal,
clippy::await_holding_lock,
clippy::manual_saturating_arithmetic,
clippy::explicit_counter_loop,
clippy::needless_lifetimes,
clippy::single_component_path_imports,
clippy::uninlined_format_args,
clippy::iter_cloned_collect,
clippy::manual_str_repeat,
clippy::excessive_precision,
clippy::precedence,
clippy::unnecessary_literal_unwrap
)]
use oxicode::{config, decode_from_slice, encode_to_vec};
#[test]
fn wire_u8_zero_is_single_zero_byte() {
let bytes = encode_to_vec(&0u8).expect("encode 0u8");
assert_eq!(bytes, &[0x00], "0u8 must encode as [0x00]");
let (val, _): (u8, _) = decode_from_slice(&bytes).expect("decode 0u8");
assert_eq!(val, 0u8);
}
#[test]
fn wire_u8_max_is_0xff() {
let bytes = encode_to_vec(&255u8).expect("encode 255u8");
assert_eq!(bytes, &[0xFF], "255u8 must encode as [0xFF]");
let (val, _): (u8, _) = decode_from_slice(&bytes).expect("decode 255u8");
assert_eq!(val, 255u8);
}
#[test]
fn wire_bool_true_is_0x01_false_is_0x00() {
let true_bytes = encode_to_vec(&true).expect("encode true");
assert_eq!(true_bytes, &[0x01], "true must encode as [0x01]");
let false_bytes = encode_to_vec(&false).expect("encode false");
assert_eq!(false_bytes, &[0x00], "false must encode as [0x00]");
let (t, _): (bool, _) = decode_from_slice(&true_bytes).expect("decode true");
let (f, _): (bool, _) = decode_from_slice(&false_bytes).expect("decode false");
assert!(t, "decoded true must be true");
assert!(!f, "decoded false must be false");
}
#[test]
fn wire_u32_zero_is_single_byte_varint() {
let bytes = encode_to_vec(&0u32).expect("encode 0u32");
assert_eq!(bytes, &[0x00], "0u32 must encode as 1-byte varint [0x00]");
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode 0u32");
assert_eq!(val, 0u32);
}
#[test]
fn wire_u32_250_is_single_byte_varint_max() {
let bytes = encode_to_vec(&250u32).expect("encode 250u32");
assert_eq!(
bytes,
&[0xFA],
"250u32 must encode as 1-byte varint [0xFA] (SINGLE_BYTE_MAX)"
);
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode 250u32");
assert_eq!(val, 250u32);
}
#[test]
fn wire_u32_251_is_three_byte_varint() {
let bytes = encode_to_vec(&251u32).expect("encode 251u32");
assert_eq!(
bytes,
&[0xFB, 0xFB, 0x00],
"251u32 must encode as [0xFB, 0xFB, 0x00] (marker + LE u16)"
);
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode 251u32");
assert_eq!(val, 251u32);
}
#[test]
fn wire_u32_65535_is_three_byte_varint() {
let bytes = encode_to_vec(&65535u32).expect("encode 65535u32");
assert_eq!(
bytes,
&[0xFB, 0xFF, 0xFF],
"65535u32 (u16::MAX) must encode as [0xFB, 0xFF, 0xFF]"
);
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode 65535u32");
assert_eq!(val, 65535u32);
}
#[test]
fn wire_u32_65536_is_five_byte_varint() {
let bytes = encode_to_vec(&65536u32).expect("encode 65536u32");
assert_eq!(
bytes,
&[0xFC, 0x00, 0x00, 0x01, 0x00],
"65536u32 must encode as [0xFC, 0x00, 0x00, 0x01, 0x00]"
);
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode 65536u32");
assert_eq!(val, 65536u32);
}
#[test]
fn wire_u32_max_is_five_bytes_starting_with_fc() {
let bytes = encode_to_vec(&u32::MAX).expect("encode u32::MAX");
assert_eq!(bytes.len(), 5, "u32::MAX must encode as exactly 5 bytes");
assert_eq!(
bytes[0], 0xFC,
"first byte of u32::MAX encoding must be 0xFC"
);
assert_eq!(
&bytes[1..],
&u32::MAX.to_le_bytes(),
"bytes[1..5] must be u32::MAX in little-endian order"
);
let (val, _): (u32, _) = decode_from_slice(&bytes).expect("decode u32::MAX");
assert_eq!(val, u32::MAX);
}
#[test]
fn wire_u64_zero_is_single_byte_varint() {
let bytes = encode_to_vec(&0u64).expect("encode 0u64");
assert_eq!(bytes, &[0x00], "0u64 must encode as 1-byte varint [0x00]");
let (val, _): (u64, _) = decode_from_slice(&bytes).expect("decode 0u64");
assert_eq!(val, 0u64);
}
#[test]
fn wire_u64_max_is_nine_bytes_starting_with_fd() {
let bytes = encode_to_vec(&u64::MAX).expect("encode u64::MAX");
assert_eq!(bytes.len(), 9, "u64::MAX must encode as exactly 9 bytes");
assert_eq!(
bytes[0], 0xFD,
"first byte of u64::MAX encoding must be 0xFD"
);
assert_eq!(
&bytes[1..],
&u64::MAX.to_le_bytes(),
"bytes[1..9] must be u64::MAX in little-endian order"
);
let (val, _): (u64, _) = decode_from_slice(&bytes).expect("decode u64::MAX");
assert_eq!(val, u64::MAX);
}
#[test]
fn wire_u128_max_is_seventeen_bytes_starting_with_fe() {
let bytes = encode_to_vec(&u128::MAX).expect("encode u128::MAX");
assert_eq!(bytes.len(), 17, "u128::MAX must encode as exactly 17 bytes");
assert_eq!(
bytes[0], 0xFE,
"first byte of u128::MAX encoding must be 0xFE"
);
assert_eq!(
&bytes[1..],
&u128::MAX.to_le_bytes(),
"bytes[1..17] must be u128::MAX in little-endian order"
);
let (val, _): (u128, _) = decode_from_slice(&bytes).expect("decode u128::MAX");
assert_eq!(val, u128::MAX);
}
#[test]
fn wire_string_hello_has_length_prefix_and_utf8_bytes() {
let bytes = encode_to_vec(&"hello".to_string()).expect("encode hello");
assert_eq!(
bytes,
&[0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F],
r#""hello" must encode as [0x05, 'h', 'e', 'l', 'l', 'o']"#
);
let (val, _): (String, _) = decode_from_slice(&bytes).expect("decode hello");
assert_eq!(val, "hello");
}
#[test]
fn wire_empty_vec_u8_is_single_zero_byte() {
let bytes = encode_to_vec(&Vec::<u8>::new()).expect("encode empty Vec<u8>");
assert_eq!(
bytes,
&[0x00],
"empty Vec<u8> must encode as [0x00] (length=0 varint, no elements)"
);
let (val, _): (Vec<u8>, _) = decode_from_slice(&bytes).expect("decode empty Vec<u8>");
assert!(val.is_empty(), "decoded Vec must be empty");
}
#[test]
fn wire_vec_u8_three_elements_has_length_prefix_and_elements() {
let bytes = encode_to_vec(&vec![1u8, 2u8, 3u8]).expect("encode vec![1,2,3]");
assert_eq!(
bytes,
&[0x03, 0x01, 0x02, 0x03],
"vec![1u8,2,3] must encode as [0x03, 0x01, 0x02, 0x03]"
);
let (val, _): (Vec<u8>, _) = decode_from_slice(&bytes).expect("decode vec![1,2,3]");
assert_eq!(val, vec![1u8, 2, 3]);
}
#[test]
fn wire_option_none_is_single_zero_byte() {
let bytes = encode_to_vec(&Option::<u32>::None).expect("encode None");
assert_eq!(
bytes,
&[0x00],
"Option::None must encode as [0x00] (discriminant 0)"
);
let (val, _): (Option<u32>, _) = decode_from_slice(&bytes).expect("decode None");
assert!(val.is_none(), "decoded value must be None");
}
#[test]
fn wire_option_some_42_is_0x01_0x2a() {
let bytes = encode_to_vec(&Some(42u32)).expect("encode Some(42u32)");
assert_eq!(
bytes,
&[0x01, 0x2A],
"Some(42u32) must encode as [0x01, 0x2A]"
);
let (val, _): (Option<u32>, _) = decode_from_slice(&bytes).expect("decode Some(42u32)");
assert_eq!(val, Some(42u32));
}
#[test]
fn wire_fixed_int_u32_42_is_four_le_bytes() {
let cfg = config::standard().with_fixed_int_encoding();
let bytes =
oxicode::encode_to_vec_with_config(&42u32, cfg).expect("encode 42u32 with fixed-int");
assert_eq!(
bytes,
&[0x2A, 0x00, 0x00, 0x00],
"fixed-int 42u32 must encode as 4 LE bytes [0x2A, 0x00, 0x00, 0x00]"
);
let (val, _): (u32, _) =
oxicode::decode_from_slice_with_config(&bytes, cfg).expect("decode fixed-int 42u32");
assert_eq!(val, 42u32);
}
#[test]
fn wire_big_endian_fixed_int_u32_42_is_four_be_bytes() {
let cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let bytes =
oxicode::encode_to_vec_with_config(&42u32, cfg).expect("encode 42u32 with BE fixed-int");
assert_eq!(
bytes,
&[0x00, 0x00, 0x00, 0x2A],
"BE fixed-int 42u32 must encode as [0x00, 0x00, 0x00, 0x2A]"
);
let (val, _): (u32, _) =
oxicode::decode_from_slice_with_config(&bytes, cfg).expect("decode BE fixed-int 42u32");
assert_eq!(val, 42u32);
}
#[test]
fn wire_i64_minus_one_zigzag_is_0x01() {
let bytes = encode_to_vec(&(-1i64)).expect("encode -1i64");
assert_eq!(
bytes,
&[0x01],
"-1i64 zigzag encodes to unsigned 1, which must be varint [0x01]"
);
let (val, _): (i64, _) = decode_from_slice(&bytes).expect("decode -1i64");
assert_eq!(val, -1i64);
}
#[test]
fn wire_i32_minus_64_zigzag_is_0x7f() {
let bytes = encode_to_vec(&(-64i32)).expect("encode -64i32");
assert_eq!(
bytes,
&[0x7F],
"-64i32 zigzag encodes to unsigned 127, which must be varint [0x7F]"
);
let (val, _): (i32, _) = decode_from_slice(&bytes).expect("decode -64i32");
assert_eq!(val, -64i32);
}
#[test]
fn wire_char_ascii_a_is_utf8_byte_0x41() {
let bytes = encode_to_vec(&'A').expect("encode 'A'");
assert_eq!(
bytes,
&[0x41],
"'A' must encode as single UTF-8 byte [0x41]"
);
let (val, _): (char, _) = decode_from_slice(&bytes).expect("decode 'A'");
assert_eq!(val, 'A');
}