diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
commit | 5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch) | |
tree | 35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /vendor/tinystr/tests/serde.rs | |
parent | Adding debian version 1.66.0+dfsg1-1. (diff) | |
download | rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tinystr/tests/serde.rs')
-rw-r--r-- | vendor/tinystr/tests/serde.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/vendor/tinystr/tests/serde.rs b/vendor/tinystr/tests/serde.rs new file mode 100644 index 000000000..282914e6f --- /dev/null +++ b/vendor/tinystr/tests/serde.rs @@ -0,0 +1,39 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +use tinystr::*; + +// Tests largely adapted from `tinystr` crate +// https://github.com/zbraniecki/tinystr/blob/4e4eab55dd6bded7f29a18b41452c506c461716c/tests/serde.rs + +macro_rules! test_roundtrip { + ($f:ident, $n:literal, $val:expr) => { + #[test] + fn $f() { + let tiny: TinyAsciiStr<$n> = $val.parse().unwrap(); + let json_string = serde_json::to_string(&tiny).unwrap(); + let expected_json = concat!("\"", $val, "\""); + assert_eq!(json_string, expected_json); + let recover: TinyAsciiStr<$n> = serde_json::from_str(&json_string).unwrap(); + assert_eq!(&*tiny, &*recover); + + let bin = bincode::serialize(&tiny).unwrap(); + assert_eq!(bin, &tiny.all_bytes()[..]); + let debin: TinyAsciiStr<$n> = bincode::deserialize(&bin).unwrap(); + assert_eq!(&*tiny, &*debin); + + let post = postcard::to_stdvec(&tiny).unwrap(); + assert_eq!(post, &tiny.all_bytes()[..]); + let unpost: TinyAsciiStr<$n> = postcard::from_bytes(&post).unwrap(); + assert_eq!(&*tiny, &*unpost); + } + }; +} + +test_roundtrip!(test_roundtrip4_1, 4, "en"); +test_roundtrip!(test_roundtrip4_2, 4, "Latn"); +test_roundtrip!(test_roundtrip8, 8, "calendar"); +test_roundtrip!(test_roundtrip16, 16, "verylongstring"); +test_roundtrip!(test_roundtrip10, 11, "shortstring"); +test_roundtrip!(test_roundtrip30, 24, "veryveryverylongstring"); |