| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use tinystr::*; |
| 6 |
|
| 7 |
// Tests largely adapted from `tinystr` crate |
| 8 |
// https://github.com/zbraniecki/tinystr/blob/4e4eab55dd6bded7f29a18b41452c506c461716c/tests/serde.rs |
| 9 |
|
| 10 |
macro_rules! test_roundtrip { |
| 11 |
($f:ident, $n:literal, $val:expr) => { |
| 12 |
#[test] |
| 13 |
fn $f() { |
| 14 |
let tiny: TinyAsciiStr<$n> = $val.parse().unwrap(); |
| 15 |
let json_string = serde_json::to_string(&tiny).unwrap(); |
| 16 |
let expected_json = concat!("\"", $val, "\""); |
| 17 |
assert_eq!(json_string, expected_json); |
| 18 |
let recover: TinyAsciiStr<$n> = serde_json::from_str(&json_string).unwrap(); |
| 19 |
assert_eq!(&*tiny, &*recover); |
| 20 |
|
| 21 |
let bin = bincode::serialize(&tiny).unwrap(); |
| 22 |
assert_eq!(bin, &tiny.all_bytes()[..]); |
| 23 |
let debin: TinyAsciiStr<$n> = bincode::deserialize(&bin).unwrap(); |
| 24 |
assert_eq!(&*tiny, &*debin); |
| 25 |
|
| 26 |
let post = postcard::to_stdvec(&tiny).unwrap(); |
| 27 |
assert_eq!(post, &tiny.all_bytes()[..]); |
| 28 |
let unpost: TinyAsciiStr<$n> = postcard::from_bytes(&post).unwrap(); |
| 29 |
assert_eq!(&*tiny, &*unpost); |
| 30 |
} |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
test_roundtrip!(test_roundtrip4_1, 4, "en"); |
| 35 |
test_roundtrip!(test_roundtrip4_2, 4, "Latn"); |
| 36 |
test_roundtrip!(test_roundtrip8, 8, "calendar"); |
| 37 |
test_roundtrip!(test_roundtrip16, 16, "verylongstring"); |
| 38 |
test_roundtrip!(test_roundtrip10, 11, "shortstring"); |
| 39 |
test_roundtrip!(test_roundtrip30, 24, "veryveryverylongstring"); |