Debian Codegraph @

debian/missing-sources/litemap/tests/rkyv.rs

raw
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 litemap::LiteMap;
6 use rkyv::archived_root;
7 use rkyv::check_archived_root;
8 use rkyv::ser::serializers::AllocSerializer;
9 use rkyv::ser::Serializer;
10 use rkyv::util::AlignedBytes;
11 use rkyv::util::AlignedVec;
12 use rkyv::Deserialize;
13 use rkyv::Infallible;
14
15 const DATA: [(&str, &str); 11] = [
16 ("ar", "Arabic"),
17 ("bn", "Bangla"),
18 ("ccp", "Chakma"),
19 ("en", "English"),
20 ("es", "Spanish"),
21 ("fr", "French"),
22 ("ja", "Japanese"),
23 ("ru", "Russian"),
24 ("sr", "Serbian"),
25 ("th", "Thai"),
26 ("tr", "Turkish"),
27 ];
28
29 const RKYV: AlignedBytes<192> = AlignedBytes(if cfg!(target_endian = "little") {
30 [
31 74, 97, 112, 97, 110, 101, 115, 101, 97, 114, 0, 0, 0, 0, 0, 2, 65, 114, 97, 98, 105, 99,
32 0, 6, 98, 110, 0, 0, 0, 0, 0, 2, 66, 97, 110, 103, 108, 97, 0, 6, 99, 99, 112, 0, 0, 0, 0,
33 3, 67, 104, 97, 107, 109, 97, 0, 6, 101, 110, 0, 0, 0, 0, 0, 2, 69, 110, 103, 108, 105,
34 115, 104, 7, 101, 115, 0, 0, 0, 0, 0, 2, 83, 112, 97, 110, 105, 115, 104, 7, 102, 114, 0,
35 0, 0, 0, 0, 2, 70, 114, 101, 110, 99, 104, 0, 6, 106, 97, 0, 0, 0, 0, 0, 2, 8, 0, 0, 0,
36 144, 255, 255, 255, 114, 117, 0, 0, 0, 0, 0, 2, 82, 117, 115, 115, 105, 97, 110, 7, 115,
37 114, 0, 0, 0, 0, 0, 2, 83, 101, 114, 98, 105, 97, 110, 7, 116, 104, 0, 0, 0, 0, 0, 2, 84,
38 104, 97, 105, 0, 0, 0, 4, 116, 114, 0, 0, 0, 0, 0, 2, 84, 117, 114, 107, 105, 115, 104, 7,
39 80, 255, 255, 255, 11, 0, 0, 0,
40 ]
41 } else {
42 [
43 74, 97, 112, 97, 110, 101, 115, 101, 97, 114, 0, 0, 0, 0, 0, 2, 65, 114, 97, 98, 105, 99,
44 0, 6, 98, 110, 0, 0, 0, 0, 0, 2, 66, 97, 110, 103, 108, 97, 0, 6, 99, 99, 112, 0, 0, 0, 0,
45 3, 67, 104, 97, 107, 109, 97, 0, 6, 101, 110, 0, 0, 0, 0, 0, 2, 69, 110, 103, 108, 105,
46 115, 104, 7, 101, 115, 0, 0, 0, 0, 0, 2, 83, 112, 97, 110, 105, 115, 104, 7, 102, 114, 0,
47 0, 0, 0, 0, 2, 70, 114, 101, 110, 99, 104, 0, 6, 106, 97, 0, 0, 0, 0, 0, 2, 0, 0, 0, 8,
48 144, 255, 255, 255, 114, 117, 0, 0, 0, 0, 0, 2, 82, 117, 115, 115, 105, 97, 110, 7, 115,
49 114, 0, 0, 0, 0, 0, 2, 83, 101, 114, 98, 105, 97, 110, 7, 116, 104, 0, 0, 0, 0, 0, 2, 84,
50 104, 97, 105, 0, 0, 0, 4, 116, 114, 0, 0, 0, 0, 0, 2, 84, 117, 114, 107, 105, 115, 104, 7,
51 255, 255, 255, 80, 0, 0, 0, 11,
52 ]
53 });
54
55 type LiteMapOfStrings = LiteMap<String, String>;
56 type TupleVecOfStrings = Vec<(String, String)>;
57
58 fn generate() -> AlignedVec {
59 let map = DATA
60 .iter()
61 .map(|&(k, v)| (k.to_owned(), v.to_owned()))
62 .collect::<LiteMapOfStrings>();
63
64 let mut serializer = AllocSerializer::<4096>::default();
65 serializer
66 .serialize_value(&map.into_tuple_vec())
67 .expect("failed to archive test");
68 serializer.into_serializer().into_inner()
69 }
70
71 #[test]
72 fn rkyv_serialize() {
73 let serialized = generate();
74 assert_eq!(RKYV.0, serialized.as_slice());
75 }
76
77 #[test]
78 fn rkyv_archive() {
79 let archived = unsafe { archived_root::<TupleVecOfStrings>(&RKYV.0) };
80 let s = archived[0].1.as_str();
81 assert_eq!(s, "Arabic");
82 }
83
84 #[test]
85 fn rkyv_checked_archive() {
86 let archived = check_archived_root::<TupleVecOfStrings>(&RKYV.0).unwrap();
87 let s = archived[0].1.as_str();
88 assert_eq!(s, "Arabic");
89 }
90
91 #[test]
92 fn rkyv_deserialize() {
93 let archived = unsafe { archived_root::<TupleVecOfStrings>(&RKYV.0) };
94 let deserialized = archived.deserialize(&mut Infallible).unwrap();
95 // Safe because we are deserializing a buffer from a trusted source
96 let deserialized: LiteMapOfStrings = LiteMap::from_sorted_store_unchecked(deserialized);
97 assert_eq!(deserialized.get("tr").map(String::as_str), Some("Turkish"));
98 }