#![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, decode_from_slice_with_config, encode_to_vec,
encode_to_vec_with_config, Decode, Encode,
};
#[derive(Debug, PartialEq, Clone, Encode, Decode)]
struct AdvRecord {
id: u32,
label: String,
score: f64,
}
mod interop_advanced_tests {
use super::*;
#[test]
fn test_adv_standard_config_encode_decode_roundtrip() {
let original = AdvRecord {
id: 42,
label: String::from("standard-roundtrip"),
score: std::f64::consts::PI,
};
let cfg = config::standard();
let bytes =
encode_to_vec_with_config(&original, cfg).expect("standard config encode failed");
let (decoded, consumed): (AdvRecord, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("standard config decode failed");
assert_eq!(
original, decoded,
"standard config roundtrip: value mismatch"
);
assert_eq!(
consumed,
bytes.len(),
"standard config: unconsumed bytes remain"
);
}
#[test]
fn test_adv_fixed_int_config_encode_decode_roundtrip() {
let original = AdvRecord {
id: u32::MAX,
label: String::from("fixed-int-roundtrip"),
score: std::f64::consts::E,
};
let cfg = config::standard().with_fixed_int_encoding();
let bytes =
encode_to_vec_with_config(&original, cfg).expect("fixed_int config encode failed");
let (decoded, consumed): (AdvRecord, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("fixed_int config decode failed");
assert_eq!(
original, decoded,
"fixed_int config roundtrip: value mismatch"
);
assert_eq!(
consumed,
bytes.len(),
"fixed_int config: unconsumed bytes remain"
);
}
#[test]
fn test_adv_big_endian_config_encode_decode_roundtrip() {
let original = AdvRecord {
id: 0x0102_0304,
label: String::from("big-endian-test"),
score: std::f64::consts::PI * std::f64::consts::E,
};
let cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let bytes =
encode_to_vec_with_config(&original, cfg).expect("big_endian config encode failed");
let (decoded, consumed): (AdvRecord, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("big_endian config decode failed");
assert_eq!(
original, decoded,
"big_endian config roundtrip: value mismatch"
);
assert_eq!(
consumed,
bytes.len(),
"big_endian config: unconsumed bytes remain"
);
}
#[test]
fn test_adv_standard_encode_fixed_int_decode_mismatch() {
let value: u64 = 100_000_000_000u64;
let std_bytes =
encode_to_vec_with_config(&value, config::standard()).expect("standard encode failed");
let fixed_cfg = config::standard().with_fixed_int_encoding();
let result: Result<(u64, usize), _> = decode_from_slice_with_config(&std_bytes, fixed_cfg);
match result {
Err(_) => {
}
Ok((decoded, _)) => {
assert_ne!(
decoded, value,
"decoding standard-encoded bytes with fixed-int config must not yield correct value"
);
}
}
}
#[test]
fn test_adv_fixed_int_encode_standard_decode_mismatch_for_large() {
let value: u64 = u64::MAX;
let fix_bytes =
encode_to_vec_with_config(&value, config::standard().with_fixed_int_encoding())
.expect("fixed_int encode failed");
let result: Result<(u64, usize), _> =
decode_from_slice_with_config(&fix_bytes, config::standard());
match result {
Err(_) => {
}
Ok((decoded, _)) => {
assert_ne!(
decoded, value,
"decoding fixed-int bytes with varint config must not yield u64::MAX correctly"
);
}
}
}
#[test]
fn test_adv_legacy_config_encode_decode_roundtrip() {
let original = AdvRecord {
id: 12345,
label: String::from("legacy-compat-roundtrip"),
score: std::f64::consts::E,
};
let cfg = config::legacy();
let bytes = encode_to_vec_with_config(&original, cfg).expect("legacy config encode failed");
let (decoded, consumed): (AdvRecord, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("legacy config decode failed");
assert_eq!(original, decoded, "legacy config roundtrip: value mismatch");
assert_eq!(
consumed,
bytes.len(),
"legacy config: unconsumed bytes remain"
);
}
#[test]
fn test_adv_legacy_config_u32_is_four_bytes_le() {
let value: u32 = 0x0102_0304;
let cfg = config::legacy();
let bytes = encode_to_vec_with_config(&value, cfg).expect("legacy u32 encode failed");
assert_eq!(
bytes.len(),
4,
"legacy config u32 must be exactly 4 bytes, got {}",
bytes.len()
);
assert_eq!(
bytes.as_slice(),
&[0x04u8, 0x03, 0x02, 0x01],
"legacy config u32 must be little-endian: [0x04, 0x03, 0x02, 0x01]"
);
let (decoded, _): (u32, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("legacy u32 decode failed");
assert_eq!(decoded, value, "legacy config u32 roundtrip mismatch");
}
#[test]
fn test_adv_standard_u64_300_is_three_bytes() {
let value: u64 = 300;
let bytes = encode_to_vec_with_config(&value, config::standard())
.expect("standard u64 300 encode failed");
assert_eq!(
bytes.len(),
3,
"standard varint u64=300 must be 3 bytes, got {}",
bytes.len()
);
let (decoded, consumed): (u64, usize) =
decode_from_slice_with_config(&bytes, config::standard())
.expect("standard u64 300 decode failed");
assert_eq!(decoded, 300u64, "standard u64=300 roundtrip mismatch");
assert_eq!(consumed, 3, "standard u64=300 must consume 3 bytes");
}
#[test]
fn test_adv_fixed_int_u64_300_is_eight_bytes() {
let value: u64 = 300;
let cfg = config::standard().with_fixed_int_encoding();
let bytes =
encode_to_vec_with_config(&value, cfg).expect("fixed_int u64 300 encode failed");
assert_eq!(
bytes.len(),
8,
"fixed_int u64=300 must be 8 bytes, got {}",
bytes.len()
);
let (decoded, consumed): (u64, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("fixed_int u64 300 decode failed");
assert_eq!(decoded, 300u64, "fixed_int u64=300 roundtrip mismatch");
assert_eq!(consumed, 8, "fixed_int u64=300 must consume 8 bytes");
}
#[test]
fn test_adv_big_endian_u32_0x01000000_is_1_0_0_0() {
let value: u32 = 0x0100_0000;
let cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let bytes = encode_to_vec_with_config(&value, cfg).expect("big_endian u32 encode failed");
assert_eq!(
bytes.as_slice(),
&[0x01u8, 0x00, 0x00, 0x00],
"big_endian u32=0x01000000 must encode as [1, 0, 0, 0]"
);
let (decoded, _): (u32, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("big_endian u32 decode failed");
assert_eq!(
decoded, value,
"big_endian u32=0x01000000 roundtrip mismatch"
);
}
#[test]
fn test_adv_standard_vec_u8_length_prefix_is_varint() {
let data: Vec<u8> = (0u8..10).collect();
let bytes = encode_to_vec_with_config(&data, config::standard())
.expect("standard Vec<u8> encode failed");
assert_eq!(
bytes.len(),
11,
"standard Vec<u8>[10] must be 11 bytes (1 varint prefix + 10 data), got {}",
bytes.len()
);
assert_eq!(
bytes[0], 10u8,
"first byte must be varint length=10, got {}",
bytes[0]
);
let (decoded, _): (Vec<u8>, usize) =
decode_from_slice_with_config(&bytes, config::standard())
.expect("standard Vec<u8> decode failed");
assert_eq!(decoded, data, "standard Vec<u8> roundtrip mismatch");
}
#[test]
fn test_adv_fixed_int_vec_u8_length_prefix_is_eight_bytes() {
let data: Vec<u8> = vec![0xAA, 0xBB, 0xCC, 0xDD];
let cfg = config::standard().with_fixed_int_encoding();
let bytes = encode_to_vec_with_config(&data, cfg).expect("fixed_int Vec<u8> encode failed");
assert_eq!(
bytes.len(),
12,
"fixed_int Vec<u8>[4] must be 12 bytes (8 u64 prefix + 4 data), got {}",
bytes.len()
);
assert_eq!(
&bytes[..8],
&[4u8, 0, 0, 0, 0, 0, 0, 0],
"fixed_int Vec<u8> prefix must be u64=4 LE"
);
let (decoded, _): (Vec<u8>, usize) =
decode_from_slice_with_config(&bytes, cfg).expect("fixed_int Vec<u8> decode failed");
assert_eq!(decoded, data, "fixed_int Vec<u8> roundtrip mismatch");
}
#[test]
fn test_adv_config_with_limit_works_with_encode_decode() {
let small_value: u8 = 1u8;
let lim_cfg = config::standard().with_limit::<8>();
let bytes = encode_to_vec_with_config(&small_value, lim_cfg)
.expect("encode within limit must succeed");
assert_eq!(bytes.len(), 1, "u8=1 must encode to 1 byte");
let (decoded, _): (u8, usize) = decode_from_slice_with_config(&bytes, lim_cfg)
.expect("decode within limit must succeed");
assert_eq!(decoded, small_value, "decoded value mismatch");
let big_data: Vec<u8> = (0u8..10).collect();
let unconstrained_bytes = encode_to_vec(&big_data).expect("unconstrained encode");
let over_limit_cfg = config::standard().with_limit::<9>();
let result: Result<(Vec<u8>, usize), _> =
decode_from_slice_with_config(&unconstrained_bytes, over_limit_cfg);
assert!(
result.is_err(),
"decoding Vec<u8>[10] with limit=9 must fail (claims 10 > 9)"
);
}
#[test]
fn test_adv_two_different_configs_produce_different_bytes() {
let value = AdvRecord {
id: 1000,
label: String::from("config-diff"),
score: std::f64::consts::PI,
};
let std_bytes =
encode_to_vec_with_config(&value, config::standard()).expect("standard encode failed");
let fix_bytes =
encode_to_vec_with_config(&value, config::standard().with_fixed_int_encoding())
.expect("fixed_int encode failed");
assert_ne!(
std_bytes, fix_bytes,
"standard and fixed_int configs must produce different byte sequences for same struct"
);
}
#[test]
fn test_adv_decode_with_mismatched_config_fails_or_wrong_value() {
let value: u32 = 0xDEAD_BEEF;
let be_cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let le_cfg = config::standard()
.with_little_endian()
.with_fixed_int_encoding();
let be_bytes =
encode_to_vec_with_config(&value, be_cfg).expect("big_endian u32 encode failed");
let result: Result<(u32, usize), _> = decode_from_slice_with_config(&be_bytes, le_cfg);
match result {
Err(_) => {
}
Ok((decoded, _)) => {
assert_ne!(
decoded, value,
"big-endian encoded u32 decoded with little-endian must not equal original"
);
assert_eq!(
decoded, 0xEFBE_ADDEu32,
"LE decode of BE 0xDEADBEEF must be 0xEFBEADDE"
);
}
}
}
#[test]
fn test_adv_f64_pi_roundtrips_correctly_for_both_endiannesses() {
let value: f64 = std::f64::consts::PI;
let le_cfg = config::standard()
.with_little_endian()
.with_fixed_int_encoding();
let be_cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let le_bytes = encode_to_vec_with_config(&value, le_cfg).expect("LE f64 encode failed");
let be_bytes = encode_to_vec_with_config(&value, be_cfg).expect("BE f64 encode failed");
assert_eq!(le_bytes.len(), 8, "LE f64 must be 8 bytes");
assert_eq!(be_bytes.len(), 8, "BE f64 must be 8 bytes");
let le_reversed: Vec<u8> = le_bytes.iter().copied().rev().collect();
assert_eq!(
be_bytes, le_reversed,
"BE f64 bytes must be the byte-reversal of LE f64 bytes"
);
let (le_decoded, _): (f64, usize) =
decode_from_slice_with_config(&le_bytes, le_cfg).expect("LE f64 decode failed");
let (be_decoded, _): (f64, usize) =
decode_from_slice_with_config(&be_bytes, be_cfg).expect("BE f64 decode failed");
assert_eq!(le_decoded, value, "LE f64 PI roundtrip mismatch");
assert_eq!(be_decoded, value, "BE f64 PI roundtrip mismatch");
}
#[test]
fn test_adv_config_clone_same_behavior_as_original() {
let original_cfg = config::standard()
.with_big_endian()
.with_fixed_int_encoding();
let cloned_cfg = original_cfg;
let value: u32 = 0xCAFE_BABE;
let bytes_orig =
encode_to_vec_with_config(&value, original_cfg).expect("original config encode failed");
let bytes_clone =
encode_to_vec_with_config(&value, cloned_cfg).expect("cloned config encode failed");
assert_eq!(
bytes_orig, bytes_clone,
"cloned config must produce identical bytes as original"
);
let (decoded_orig, _): (u32, usize) =
decode_from_slice_with_config(&bytes_orig, original_cfg)
.expect("original config decode failed");
let (decoded_clone, _): (u32, usize) =
decode_from_slice_with_config(&bytes_clone, cloned_cfg)
.expect("cloned config decode failed");
assert_eq!(decoded_orig, value, "original config roundtrip mismatch");
assert_eq!(decoded_clone, value, "cloned config roundtrip mismatch");
}
#[test]
fn test_adv_config_debug_output_contains_useful_info() {
let std_cfg = config::standard();
let debug_str = format!("{:?}", std_cfg);
assert!(
!debug_str.is_empty(),
"debug output of standard config must not be empty"
);
let fix_cfg = config::standard().with_fixed_int_encoding();
let fix_debug = format!("{:?}", fix_cfg);
assert!(
!fix_debug.is_empty(),
"debug output of fixed_int config must not be empty"
);
let be_cfg = config::standard().with_big_endian();
let be_debug = format!("{:?}", be_cfg);
assert!(
!be_debug.is_empty(),
"debug output of big_endian config must not be empty"
);
}
#[test]
fn test_adv_encode_to_file_and_decode_from_file_with_standard_config() {
let original = AdvRecord {
id: 9_999,
label: String::from("file-io-test"),
score: std::f64::consts::PI,
};
let mut file_path = std::env::temp_dir();
file_path.push("oxicode_interop_advanced_test_19.bin");
oxicode::encode_to_file(&original, &file_path).expect("encode_to_file failed");
let decoded: AdvRecord =
oxicode::decode_from_file(&file_path).expect("decode_from_file failed");
assert_eq!(
original, decoded,
"file encode/decode roundtrip: value mismatch"
);
let _ = std::fs::remove_file(&file_path);
}
#[test]
fn test_adv_byte_array_same_payload_length_standard_and_fixed_int() {
let arr: [u8; 4] = [1, 2, 3, 4];
let std_bytes = encode_to_vec_with_config(&arr, config::standard())
.expect("standard [u8;4] encode failed");
let fix_bytes =
encode_to_vec_with_config(&arr, config::standard().with_fixed_int_encoding())
.expect("fixed_int [u8;4] encode failed");
assert_eq!(
std_bytes.len(),
4,
"standard [u8;4] must be 4 bytes, got {}",
std_bytes.len()
);
assert_eq!(
fix_bytes.len(),
4,
"fixed_int [u8;4] must be 4 bytes, got {}",
fix_bytes.len()
);
assert_eq!(
std_bytes, fix_bytes,
"[u8;4] payload must be identical for standard and fixed_int configs"
);
let (decoded_std, _): ([u8; 4], usize) =
decode_from_slice(&std_bytes).expect("standard [u8;4] decode failed");
assert_eq!(decoded_std, arr, "standard [u8;4] roundtrip mismatch");
let (decoded_fix, _): ([u8; 4], usize) =
decode_from_slice_with_config(&fix_bytes, config::standard().with_fixed_int_encoding())
.expect("fixed_int [u8;4] decode failed");
assert_eq!(decoded_fix, arr, "fixed_int [u8;4] roundtrip mismatch");
}
}