From 56c6b1b54010d6dfea6bafb9d9facc7a1cbdaad2 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 18 Oct 2024 11:21:34 +0200 Subject: [PATCH] add serialization format test --- zenoh-ext/src/serialization.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/zenoh-ext/src/serialization.rs b/zenoh-ext/src/serialization.rs index 2df5eaf78..c55b69e81 100644 --- a/zenoh-ext/src/serialization.rs +++ b/zenoh-ext/src/serialization.rs @@ -642,4 +642,33 @@ mod tests { map.insert("hello".to_string(), "world".to_string()); serialize_deserialize!(HashMap, map); } + + macro_rules! check_binary_format { + ($expr:expr, $out:expr) => { + let payload = z_serialize(&$expr); + assert_eq!(payload.to_bytes(), $out); + }; + } + + #[test] + fn binary_format() { + let i1: i32 = 1234566; + check_binary_format!(i1, vec![134, 214, 18, 0]); + let i2: i32 = -49245; + check_binary_format!(i2, vec![163, 63, 255, 255]); + let s: &str = "test"; + check_binary_format!(s, vec![4, 116, 101, 115, 116]); + let t: (u16, f32, &str) = (500, 1234.0, "test"); + check_binary_format!(t, vec![244, 1, 0, 64, 154, 68, 4, 116, 101, 115, 116]); + let v: Vec = vec![-100, 500, 100000, -20000000]; + check_binary_format!( + v, + vec![ + 4, 156, 255, 255, 255, 255, 255, 255, 255, 244, 1, 0, 0, 0, 0, 0, 0, 160, 134, 1, + 0, 0, 0, 0, 0, 0, 211, 206, 254, 255, 255, 255, 255 + ] + ); + let vp: Vec<(&str, i16)> = vec![("s1", 10), ("s2", -10000)]; + check_binary_format!(vp, vec![2, 2, 115, 49, 10, 0, 2, 115, 50, 240, 216]); + } }