• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 postcard::ser_flavors::{AllocVec, Flavor};
6 use serde::Serialize;
7 use zerotrie::ZeroTriePerfectHash;
8 use zerotrie::ZeroTrieSimpleAscii;
9 use zerovec::ZeroMap;
10 
11 mod testdata {
12     include!("data/data.rs");
13 }
14 
15 #[test]
test_basic()16 fn test_basic() {
17     let bytes_ascii = testdata::basic::TRIE_ASCII;
18     let data_ascii = testdata::basic::DATA_ASCII;
19     let trie_ascii = ZeroTrieSimpleAscii::from_bytes(bytes_ascii);
20     let trie_phf_ascii = ZeroTriePerfectHash::from_bytes(bytes_ascii);
21 
22     let bytes_unicode = testdata::basic::TRIE_UNICODE;
23     let data_unicode = testdata::basic::DATA_UNICODE;
24     let trie_phf_unicode = ZeroTriePerfectHash::from_bytes(bytes_unicode);
25 
26     let bytes_binary = testdata::basic::TRIE_BINARY;
27     let data_binary = testdata::basic::DATA_BINARY;
28     let trie_phf_binary = ZeroTriePerfectHash::from_bytes(bytes_binary);
29 
30     // Check that the getter works
31     for (key, expected) in data_ascii {
32         let actual = match trie_ascii.get(key) {
33             Some(v) => v,
34             None => panic!("value should be in trie: {:?} => {}", key, expected),
35         };
36         assert_eq!(*expected, actual);
37         let actual = match trie_phf_ascii.get(key) {
38             Some(v) => v,
39             None => panic!("value should be in trie6: {:?} => {}", key, expected),
40         };
41         assert_eq!(*expected, actual);
42     }
43 
44     for (key, expected) in data_unicode {
45         let actual_unicode = match trie_phf_unicode.get(key) {
46             Some(v) => v,
47             None => panic!("value should be in trie6: {:?} => {}", key, expected),
48         };
49         assert_eq!(*expected, actual_unicode);
50     }
51 
52     for (key, expected) in data_binary {
53         let actual_bin6 = match trie_phf_binary.get(key) {
54             Some(v) => v,
55             None => panic!("value should be in trie6: {:?} => {}", key, expected),
56         };
57         assert_eq!(*expected, actual_bin6);
58     }
59 
60     // Compare the size to a postcard ZeroMap
61     let zm: ZeroMap<[u8], u32> = data_ascii.iter().map(|(a, b)| (*a, *b as u32)).collect();
62     let mut serializer = postcard::Serializer {
63         output: AllocVec::new(),
64     };
65     Serialize::serialize(&zm, &mut serializer).unwrap();
66     let zeromap_bytes = serializer
67         .output
68         .finalize()
69         .expect("Failed to finalize serializer output");
70 
71     assert_eq!(26, bytes_ascii.len());
72     assert_eq!(77, zeromap_bytes.len());
73 }
74