| 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([ |
| 30 |
74, 97, 112, 97, 110, 101, 115, 101, 97, 114, 0, 0, 0, 0, 0, 2, 65, 114, 97, 98, 105, 99, 0, 6, |
| 31 |
98, 110, 0, 0, 0, 0, 0, 2, 66, 97, 110, 103, 108, 97, 0, 6, 99, 99, 112, 0, 0, 0, 0, 3, 67, |
| 32 |
104, 97, 107, 109, 97, 0, 6, 101, 110, 0, 0, 0, 0, 0, 2, 69, 110, 103, 108, 105, 115, 104, 7, |
| 33 |
101, 115, 0, 0, 0, 0, 0, 2, 83, 112, 97, 110, 105, 115, 104, 7, 102, 114, 0, 0, 0, 0, 0, 2, 70, |
| 34 |
114, 101, 110, 99, 104, 0, 6, 106, 97, 0, 0, 0, 0, 0, 2, 8, 0, 0, 0, 144, 255, 255, 255, 114, |
| 35 |
117, 0, 0, 0, 0, 0, 2, 82, 117, 115, 115, 105, 97, 110, 7, 115, 114, 0, 0, 0, 0, 0, 2, 83, 101, |
| 36 |
114, 98, 105, 97, 110, 7, 116, 104, 0, 0, 0, 0, 0, 2, 84, 104, 97, 105, 0, 0, 0, 4, 116, 114, |
| 37 |
0, 0, 0, 0, 0, 2, 84, 117, 114, 107, 105, 115, 104, 7, 80, 255, 255, 255, 11, 0, 0, 0, |
| 38 |
]); |
| 39 |
|
| 40 |
type LiteMapOfStrings = LiteMap<String, String>; |
| 41 |
type TupleVecOfStrings = Vec<(String, String)>; |
| 42 |
|
| 43 |
fn generate() -> AlignedVec { |
| 44 |
let map = DATA |
| 45 |
.iter() |
| 46 |
.map(|&(k, v)| (k.to_owned(), v.to_owned())) |
| 47 |
.collect::<LiteMapOfStrings>(); |
| 48 |
|
| 49 |
let mut serializer = AllocSerializer::<4096>::default(); |
| 50 |
serializer |
| 51 |
.serialize_value(&map.into_tuple_vec()) |
| 52 |
.expect("failed to archive test"); |
| 53 |
serializer.into_serializer().into_inner() |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn rkyv_serialize() { |
| 58 |
let serialized = generate(); |
| 59 |
assert_eq!(RKYV.0, serialized.as_slice()); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn rkyv_archive() { |
| 64 |
let archived = unsafe { archived_root::<TupleVecOfStrings>(&RKYV.0) }; |
| 65 |
let s = archived[0].1.as_str(); |
| 66 |
assert_eq!(s, "Arabic"); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn rkyv_checked_archive() { |
| 71 |
let archived = check_archived_root::<TupleVecOfStrings>(&RKYV.0).unwrap(); |
| 72 |
let s = archived[0].1.as_str(); |
| 73 |
assert_eq!(s, "Arabic"); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn rkyv_deserialize() { |
| 78 |
let archived = unsafe { archived_root::<TupleVecOfStrings>(&RKYV.0) }; |
| 79 |
let deserialized = archived.deserialize(&mut Infallible).unwrap(); |
| 80 |
// Safe because we are deserializing a buffer from a trusted source |
| 81 |
let deserialized: LiteMapOfStrings = LiteMap::from_sorted_store_unchecked(deserialized); |
| 82 |
assert_eq!(deserialized.get("tr").map(String::as_str), Some("Turkish")); |
| 83 |
} |