summaryrefslogtreecommitdiff
path: root/tests/encode_decode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/encode_decode.rs')
-rw-r--r--tests/encode_decode.rs589
1 files changed, 589 insertions, 0 deletions
diff --git a/tests/encode_decode.rs b/tests/encode_decode.rs
new file mode 100644
index 0000000..92a1e71
--- /dev/null
+++ b/tests/encode_decode.rs
@@ -0,0 +1,589 @@
+use std::sync::Arc;
+use zr_protocol::codec::{decode::Decode, encode::Encode};
+
+fn roundtrip<T: Encode + Decode + PartialEq + Clone + std::fmt::Debug>(value: T) {
+ let mut buf = Vec::new();
+ let written = value.clone().encode(&mut buf).expect("encode failed");
+ let decoded = T::decode(&mut &buf[..]).expect("decode failed");
+ assert_eq!(written, buf.len(), "encode returned wrong byte count");
+ assert_eq!(decoded, value, "roundtrip value mismatch");
+}
+
+// ────────────────────── u8 ──────────────────────
+
+#[test]
+fn u8_zero() {
+ roundtrip(0u8);
+}
+
+#[test]
+fn u8_one() {
+ roundtrip(1u8);
+}
+
+#[test]
+fn u8_max() {
+ roundtrip(u8::MAX);
+}
+
+#[test]
+fn u8_arbitrary() {
+ roundtrip(42u8);
+}
+
+#[test]
+fn u8_bytes() {
+ let mut buf = Vec::new();
+ 255u8.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![255]);
+}
+
+// ────────────────────── u16 ─────────────────────
+
+#[test]
+fn u16_zero() {
+ roundtrip(0u16);
+}
+
+#[test]
+fn u16_one() {
+ roundtrip(1u16);
+}
+
+#[test]
+fn u16_max() {
+ roundtrip(u16::MAX);
+}
+
+#[test]
+fn u16_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 256u16.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x01, 0x00]);
+}
+
+#[test]
+fn u16_one_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 1u16.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x00, 0x01]);
+}
+
+#[test]
+fn u16_arbitrary() {
+ roundtrip(1337u16);
+}
+
+// ────────────────────── u32 ─────────────────────
+
+#[test]
+fn u32_zero() {
+ roundtrip(0u32);
+}
+
+#[test]
+fn u32_max() {
+ roundtrip(u32::MAX);
+}
+
+#[test]
+fn u32_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 0x01020304u32.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x01, 0x02, 0x03, 0x04]);
+}
+
+#[test]
+fn u32_arbitrary() {
+ roundtrip(429496729u32);
+}
+
+// ────────────────────── u64 ─────────────────────
+
+#[test]
+fn u64_zero() {
+ roundtrip(0u64);
+}
+
+#[test]
+fn u64_max() {
+ roundtrip(u64::MAX);
+}
+
+#[test]
+fn u64_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 0x0102030405060708u64.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
+}
+
+#[test]
+fn u64_arbitrary() {
+ roundtrip(999_999_999_999u64);
+}
+
+// ────────────────────── u128 ────────────────────
+
+#[test]
+fn u128_zero() {
+ roundtrip(0u128);
+}
+
+#[test]
+fn u128_max() {
+ roundtrip(u128::MAX);
+}
+
+#[test]
+fn u128_arbitrary() {
+ roundtrip(170141183460469231731687303715884105727u128);
+}
+
+// ────────────────────── usize ───────────────────
+
+#[test]
+fn usize_zero() {
+ roundtrip(0usize);
+}
+
+#[test]
+fn usize_max() {
+ roundtrip(usize::MAX);
+}
+
+#[test]
+fn usize_arbitrary() {
+ roundtrip(42usize);
+}
+
+// ────────────────────── i8 ──────────────────────
+
+#[test]
+fn i8_zero() {
+ roundtrip(0i8);
+}
+
+#[test]
+fn i8_positive() {
+ roundtrip(42i8);
+}
+
+#[test]
+fn i8_negative() {
+ roundtrip(-42i8);
+}
+
+#[test]
+fn i8_min() {
+ roundtrip(i8::MIN);
+}
+
+#[test]
+fn i8_max() {
+ roundtrip(i8::MAX);
+}
+
+#[test]
+fn i8_negative_bytes() {
+ let mut buf = Vec::new();
+ (-1i8).encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0xFF]);
+}
+
+// ────────────────────── i16 ─────────────────────
+
+#[test]
+fn i16_zero() {
+ roundtrip(0i16);
+}
+
+#[test]
+fn i16_positive() {
+ roundtrip(1234i16);
+}
+
+#[test]
+fn i16_negative() {
+ roundtrip(-1234i16);
+}
+
+#[test]
+fn i16_min() {
+ roundtrip(i16::MIN);
+}
+
+#[test]
+fn i16_max() {
+ roundtrip(i16::MAX);
+}
+
+#[test]
+fn i16_big_endian_negative() {
+ let mut buf = Vec::new();
+ (-1i16).encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0xFF, 0xFF]);
+}
+
+#[test]
+fn i16_negative_bytes() {
+ let mut buf = Vec::new();
+ (-256i16).encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0xFF, 0x00]);
+}
+
+// ────────────────────── i32 ─────────────────────
+
+#[test]
+fn i32_zero() {
+ roundtrip(0i32);
+}
+
+#[test]
+fn i32_positive() {
+ roundtrip(100000i32);
+}
+
+#[test]
+fn i32_negative() {
+ roundtrip(-100000i32);
+}
+
+#[test]
+fn i32_min() {
+ roundtrip(i32::MIN);
+}
+
+#[test]
+fn i32_max() {
+ roundtrip(i32::MAX);
+}
+
+// ────────────────────── i64 ─────────────────────
+
+#[test]
+fn i64_zero() {
+ roundtrip(0i64);
+}
+
+#[test]
+fn i64_positive() {
+ roundtrip(9_999_999_999i64);
+}
+
+#[test]
+fn i64_negative() {
+ roundtrip(-9_999_999_999i64);
+}
+
+#[test]
+fn i64_min() {
+ roundtrip(i64::MIN);
+}
+
+#[test]
+fn i64_max() {
+ roundtrip(i64::MAX);
+}
+
+// ────────────────────── i128 ────────────────────
+
+#[test]
+fn i128_zero() {
+ roundtrip(0i128);
+}
+
+#[test]
+fn i128_min() {
+ roundtrip(i128::MIN);
+}
+
+#[test]
+fn i128_max() {
+ roundtrip(i128::MAX);
+}
+
+#[test]
+fn i128_negative() {
+ roundtrip(-42i128);
+}
+
+// ────────────────────── isize ───────────────────
+
+#[test]
+fn isize_zero() {
+ roundtrip(0isize);
+}
+
+#[test]
+fn isize_positive() {
+ roundtrip(42isize);
+}
+
+#[test]
+fn isize_negative() {
+ roundtrip(-42isize);
+}
+
+// ────────────────────── f32 ─────────────────────
+
+#[test]
+fn f32_zero() {
+ roundtrip(0.0f32);
+}
+
+#[test]
+fn f32_positive() {
+ roundtrip(3.14f32);
+}
+
+#[test]
+fn f32_negative() {
+ roundtrip(-2.71f32);
+}
+
+#[test]
+fn f32_infinity() {
+ roundtrip(f32::INFINITY);
+}
+
+#[test]
+fn f32_neg_infinity() {
+ roundtrip(f32::NEG_INFINITY);
+}
+
+#[test]
+fn f32_nan() {
+ let mut buf = Vec::new();
+ f32::NAN.encode(&mut buf).unwrap();
+ let decoded = f32::decode(&mut &buf[..]).unwrap();
+ assert!(decoded.is_nan(), "NaN roundtrip failed");
+}
+
+#[test]
+fn f32_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 1.0f32.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x3F, 0x80, 0x00, 0x00]);
+}
+
+// ────────────────────── f64 ─────────────────────
+
+#[test]
+fn f64_zero() {
+ roundtrip(0.0f64);
+}
+
+#[test]
+fn f64_positive() {
+ roundtrip(3.141592653589793f64);
+}
+
+#[test]
+fn f64_negative() {
+ roundtrip(-2.718281828459045f64);
+}
+
+#[test]
+fn f64_infinity() {
+ roundtrip(f64::INFINITY);
+}
+
+#[test]
+fn f64_neg_infinity() {
+ roundtrip(f64::NEG_INFINITY);
+}
+
+#[test]
+fn f64_nan() {
+ let mut buf = Vec::new();
+ f64::NAN.encode(&mut buf).unwrap();
+ let decoded = f64::decode(&mut &buf[..]).unwrap();
+ assert!(decoded.is_nan(), "NaN roundtrip failed");
+}
+
+#[test]
+fn f64_big_endian_bytes() {
+ let mut buf = Vec::new();
+ 1.0f64.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
+}
+
+// ────────────────────── bool ────────────────────
+
+#[test]
+fn bool_true() {
+ roundtrip(true);
+}
+
+#[test]
+fn bool_false() {
+ roundtrip(false);
+}
+
+#[test]
+fn bool_true_byte() {
+ let mut buf = Vec::new();
+ true.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![1]);
+}
+
+#[test]
+fn bool_false_byte() {
+ let mut buf = Vec::new();
+ false.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0]);
+}
+
+#[test]
+fn bool_non_zero_is_true() {
+ let decoded = bool::decode(&mut &[0x42u8][..]).unwrap();
+ assert!(decoded, "any non-zero byte should decode as true");
+}
+
+// ────────────────────── [u8; S] ─────────────────
+
+#[test]
+fn array_1_byte() {
+ roundtrip([0xABu8; 1]);
+}
+
+#[test]
+fn array_4_bytes() {
+ roundtrip([0x01u8, 0x02, 0x03, 0x04]);
+}
+
+#[test]
+fn array_32_bytes() {
+ let data: [u8; 32] = std::array::from_fn(|i| i as u8);
+ roundtrip(data);
+}
+
+#[test]
+fn array_zeroes() {
+ roundtrip([0u8; 16]);
+}
+
+#[test]
+fn array_256_bytes() {
+ let data: [u8; 256] = std::array::from_fn(|i| (i % 256) as u8);
+ roundtrip(data);
+}
+
+// ────────────────────── Arc<[u8]> ───────────────
+
+#[test]
+fn arc_slice_simple() {
+ roundtrip(Arc::<[u8]>::from(vec![1, 2, 3]));
+}
+
+#[test]
+fn arc_slice_empty() {
+ roundtrip(Arc::<[u8]>::from(Vec::<u8>::new()));
+}
+
+#[test]
+fn arc_slice_large() {
+ let data: Vec<u8> = (0..1024).map(|i| (i % 256) as u8).collect();
+ roundtrip(Arc::<[u8]>::from(data));
+}
+
+// ────────────────────── Retour de bytes written ─
+
+#[test]
+fn encode_returns_correct_byte_count_u32() {
+ let mut buf = Vec::new();
+ let written = 42u32.encode(&mut buf).unwrap();
+ assert_eq!(written, 4);
+}
+
+#[test]
+fn encode_returns_correct_byte_count_bool() {
+ let mut buf = Vec::new();
+ let written = true.encode(&mut buf).unwrap();
+ assert_eq!(written, 1);
+}
+
+#[test]
+fn encode_returns_correct_byte_count_u128() {
+ let mut buf = Vec::new();
+ let written = 123u128.encode(&mut buf).unwrap();
+ assert_eq!(written, 16);
+}
+
+#[test]
+fn encode_returns_correct_byte_count_array() {
+ let mut buf = Vec::new();
+ let written = [1u8, 2, 3, 4, 5].encode(&mut buf).unwrap();
+ assert_eq!(written, 5);
+}
+
+// ────────────────────── Buffer trop court ───────
+
+#[test]
+fn decode_u16_from_empty_buffer_fails() {
+ let result = u16::decode(&mut &[][..]);
+ assert!(result.is_err(), "decoding u16 from empty buffer should fail");
+}
+
+#[test]
+fn decode_u32_from_too_short_buffer_fails() {
+ let result = u32::decode(&mut &[0x01, 0x02][..]);
+ assert!(result.is_err(), "decoding u32 from 2-byte buffer should fail");
+}
+
+#[test]
+fn decode_bool_from_empty_buffer_fails() {
+ let result = bool::decode(&mut &[][..]);
+ assert!(result.is_err(), "decoding bool from empty buffer should fail");
+}
+
+#[test]
+fn decode_array_from_short_buffer_fails() {
+ let result = <[u8; 4]>::decode(&mut &[0x01, 0x02][..]);
+ assert!(
+ result.is_err(),
+ "decoding [u8;4] from 2-byte buffer should fail"
+ );
+}
+
+// ────────────────────── Sérialisation exacte ────
+
+#[test]
+fn u16_258_bytes() {
+ let mut buf = Vec::new();
+ 258u16.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0x01, 0x02]);
+}
+
+#[test]
+fn u32_16909060_bytes() {
+ let mut buf = Vec::new();
+ 0x01020304u32.encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![1, 2, 3, 4]);
+}
+
+#[test]
+fn i16_minus_256_bytes() {
+ let mut buf = Vec::new();
+ (-256i16).encode(&mut buf).unwrap();
+ assert_eq!(buf, vec![0xFF, 0x00]);
+}
+
+// ────────────────────── Sérialisation multiple ──
+
+#[test]
+fn multiple_values_sequential_encode() {
+ let mut buf = Vec::new();
+ 1u8.encode(&mut buf).unwrap();
+ 2u16.encode(&mut buf).unwrap();
+ 3u32.encode(&mut buf).unwrap();
+ true.encode(&mut buf).unwrap();
+
+ assert_eq!(buf.len(), 1 + 2 + 4 + 1);
+
+ let mut reader = &buf[..];
+ assert_eq!(u8::decode(&mut reader).unwrap(), 1);
+ assert_eq!(u16::decode(&mut reader).unwrap(), 2);
+ assert_eq!(u32::decode(&mut reader).unwrap(), 3);
+ assert!(bool::decode(&mut reader).unwrap());
+}