File: serde.rs

package info (click to toggle)
librsvg 2.60.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140,720 kB
  • sloc: python: 2,913; ansic: 2,333; sh: 1,022; makefile: 95; xml: 14; javascript: 9
file content (39 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (41)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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");