From 193040b15a09e6d8bb7d769bc6ee12ffa20e8d4a Mon Sep 17 00:00:00 2001 From: quininer Date: Sat, 25 Nov 2023 11:09:13 +0800 Subject: [PATCH] test: Add display length test --- Cargo.toml | 4 ++-- tests/serde.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7379be9..fb82b5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cbor4ii" -version = "0.3.1" +version = "0.3.2" authors = ["quininer "] description = "CBOR: Concise Binary Object Representation" repository = "https://github.com/quininer/cbor4ii" @@ -26,7 +26,7 @@ serde_bytes = "0.11" data-encoding = "2" serde_cbor = "0.11" ciborium = "0.2" -criterion = "0.4" +criterion = "0.5" [package.metadata.docs.rs] all-features = true diff --git a/tests/serde.rs b/tests/serde.rs index 4f66527..8963156 100644 --- a/tests/serde.rs +++ b/tests/serde.rs @@ -479,3 +479,42 @@ fn test_regression_min_i64() { let min_i64: i64 = from_slice(&buf).unwrap(); assert_eq!(min_i64, i64::MIN); } + +#[test] +fn test_debug_ser_len() { + struct Test { + long: char, + short: char + } + + impl fmt::Display for Test { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = (0..257).map(|_| self.long).collect::(); + write!(f, "{}", s)?; + + for _ in 0..257 { + write!(f, "{}", self.short)?; + } + + Ok(()) + } + } + + impl Serialize for Test { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.collect_str(self) + } + } + + let test = Test { + long: 'l', + short: 's' + }; + + let buf = to_vec(Vec::new(), &test).unwrap(); + assert!(dbg!(buf.len()) < 777); // <= 0.3.1 + assert!(dbg!(buf.len()) <= 523); +}