#![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::{decode_from_slice, encode_to_vec};
fn roundtrip_and_return<T>(value: T) -> (T, Vec<u8>)
where
T: oxicode::enc::Encode + oxicode::de::Decode + PartialEq + std::fmt::Debug,
{
let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
let (dec, consumed): (T, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(value, dec, "roundtrip mismatch");
assert_eq!(
consumed,
enc.len(),
"decode must consume exactly the encoded bytes"
);
(dec, enc)
}
#[test]
fn test_u8_min_roundtrip() {
let (dec, enc) = roundtrip_and_return(u8::MIN);
assert_eq!(dec, 0u8);
assert_eq!(enc.len(), 1, "u8::MIN must encode as 1 byte");
}
#[test]
fn test_u8_max_roundtrip() {
let (dec, enc) = roundtrip_and_return(u8::MAX);
assert_eq!(dec, 255u8);
assert_eq!(
enc.len(),
1,
"u8::MAX must encode as exactly 1 raw byte for u8 type"
);
}
#[test]
fn test_u16_min_roundtrip() {
let (dec, enc) = roundtrip_and_return(u16::MIN);
assert_eq!(dec, 0u16);
assert_eq!(enc.len(), 1, "u16::MIN (0) must encode as 1 byte");
}
#[test]
fn test_u16_max_roundtrip() {
let (dec, enc) = roundtrip_and_return(u16::MAX);
assert_eq!(dec, 65535u16);
assert_eq!(
enc.len(),
3,
"u16::MAX must encode as 3 bytes (marker + u16)"
);
}
#[test]
fn test_u32_min_roundtrip() {
let (dec, enc) = roundtrip_and_return(u32::MIN);
assert_eq!(dec, 0u32);
assert_eq!(enc.len(), 1, "u32::MIN (0) must encode as 1 byte");
}
#[test]
fn test_u32_max_roundtrip() {
let (dec, enc) = roundtrip_and_return(u32::MAX);
assert_eq!(dec, 4_294_967_295u32);
assert_eq!(
enc.len(),
5,
"u32::MAX must encode as 5 bytes (marker + u32)"
);
}
#[test]
fn test_u64_min_roundtrip() {
let (dec, enc) = roundtrip_and_return(u64::MIN);
assert_eq!(dec, 0u64);
assert_eq!(enc.len(), 1, "u64::MIN (0) must encode as 1 byte");
}
#[test]
fn test_u64_max_roundtrip() {
let (dec, enc) = roundtrip_and_return(u64::MAX);
assert_eq!(dec, 18_446_744_073_709_551_615u64);
assert_eq!(
enc.len(),
9,
"u64::MAX must encode as 9 bytes (marker + u64)"
);
}
#[test]
fn test_u128_min_roundtrip() {
let (dec, enc) = roundtrip_and_return(u128::MIN);
assert_eq!(dec, 0u128);
assert_eq!(enc.len(), 1, "u128::MIN (0) must encode as 1 byte");
}
#[test]
fn test_u128_max_roundtrip() {
let value = u128::MAX;
let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
let (dec, consumed): (u128, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(value, dec, "u128::MAX roundtrip failed");
assert_eq!(
consumed,
enc.len(),
"consumed byte count must match encoded length"
);
assert!(
enc.len() > 9,
"u128::MAX should require more than 9 bytes, got {}",
enc.len()
);
}
#[test]
fn test_i8_min_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i8::MIN);
assert_eq!(dec, -128i8);
}
#[test]
fn test_i8_max_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i8::MAX);
assert_eq!(dec, 127i8);
}
#[test]
fn test_i16_min_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i16::MIN);
assert_eq!(dec, -32768i16);
}
#[test]
fn test_i16_max_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i16::MAX);
assert_eq!(dec, 32767i16);
}
#[test]
fn test_i32_min_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i32::MIN);
assert_eq!(dec, -2_147_483_648i32);
}
#[test]
fn test_i32_max_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i32::MAX);
assert_eq!(dec, 2_147_483_647i32);
}
#[test]
fn test_i64_min_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i64::MIN);
assert_eq!(dec, -9_223_372_036_854_775_808i64);
}
#[test]
fn test_i64_max_roundtrip() {
let (dec, _enc) = roundtrip_and_return(i64::MAX);
assert_eq!(dec, 9_223_372_036_854_775_807i64);
}
#[test]
fn test_i128_min_roundtrip() {
let value = i128::MIN;
let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
let (dec, consumed): (i128, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(value, dec, "i128::MIN roundtrip failed");
assert_eq!(
consumed,
enc.len(),
"consumed byte count must match encoded length"
);
assert!(
enc.len() > 1,
"i128::MIN should require more than 1 byte to encode"
);
}
#[test]
fn test_i128_max_roundtrip() {
let value = i128::MAX;
let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
let (dec, consumed): (i128, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(value, dec, "i128::MAX roundtrip failed");
assert_eq!(
consumed,
enc.len(),
"consumed byte count must match encoded length"
);
assert!(
enc.len() > 1,
"i128::MAX should require more than 1 byte to encode"
);
}
#[test]
fn test_usize_max_roundtrip() {
let value = usize::MAX;
let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
let (dec, consumed): (usize, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(value, dec, "usize::MAX roundtrip failed");
assert_eq!(
consumed,
enc.len(),
"consumed byte count must match encoded length"
);
assert!(
enc.len() > 1,
"usize::MAX must require more than 1 byte to encode (got {} bytes)",
enc.len()
);
}
#[test]
fn test_u8_max_encodes_as_single_byte_0xff() {
let value: u8 = u8::MAX; let enc = encode_to_vec(&value).expect("encode_to_vec must not fail");
assert_eq!(
enc.len(),
1,
"u8::MAX must produce exactly 1 encoded byte, got {} bytes: {:?}",
enc.len(),
enc
);
assert_eq!(
enc[0], 0xFF,
"u8::MAX must encode as the single raw byte 0xFF, got 0x{:02X}",
enc[0]
);
let (dec, consumed): (u8, usize) =
decode_from_slice(&enc).expect("decode_from_slice must not fail");
assert_eq!(dec, 255u8, "0xFF must decode back to u8::MAX");
assert_eq!(consumed, 1, "decoding u8::MAX must consume exactly 1 byte");
}