Debian Codegraph @

third_party/rust/icu_collections/tests/cpt.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 icu_collections::codepointtrie::planes::get_planes_trie;
6 use icu_collections::codepointtrie::Error;
7 use icu_collections::codepointtrie::*;
8 use zerovec::ZeroVec;
9
10 #[test]
11 fn planes_trie_deserialize_check_test() {
12 // Get expected planes trie from crate::planes::get_planes_trie()
13
14 let exp_planes_trie = get_planes_trie();
15
16 // Compute actual planes trie from planes.toml
17
18 let planes_enum_prop =
19 ::toml::from_str::<UnicodeEnumeratedProperty>(include_str!("data/cpt/planes.toml"))
20 .unwrap();
21
22 let code_point_trie_struct = planes_enum_prop.code_point_trie.trie_struct;
23
24 let trie_header = CodePointTrieHeader {
25 high_start: code_point_trie_struct.high_start,
26 shifted12_high_start: code_point_trie_struct.shifted12_high_start,
27 index3_null_offset: code_point_trie_struct.index3_null_offset,
28 data_null_offset: code_point_trie_struct.data_null_offset,
29 null_value: code_point_trie_struct.null_value,
30 trie_type: TrieType::try_from(code_point_trie_struct.trie_type_enum_val).unwrap_or_else(
31 |_| {
32 panic!(
33 "Could not parse trie_type serialized enum value in test data file: {}",
34 code_point_trie_struct.name
35 )
36 },
37 ),
38 };
39
40 let data = ZeroVec::from_slice_or_alloc(code_point_trie_struct.data_8.as_ref().unwrap());
41 let index = ZeroVec::from_slice_or_alloc(&code_point_trie_struct.index);
42 let trie_result: Result<CodePointTrie<u8>, Error> =
43 CodePointTrie::try_new(trie_header, index, data);
44 let act_planes_trie = trie_result.unwrap();
45
46 // Get check ranges (inversion map-style sequence of range+value) and
47 // apply the trie validation test fn on expected and actual tries
48
49 let serialized_ranges: Vec<(u32, u32, u32)> = planes_enum_prop.code_point_map.data.ranges;
50 let mut check_ranges: Vec<u32> = vec![];
51 for range_tuple in serialized_ranges {
52 let range_end = range_tuple.1 + 1;
53 let value = range_tuple.2;
54 check_ranges.push(range_end);
55 check_ranges.push(value);
56 }
57
58 check_trie(&act_planes_trie, &check_ranges);
59 check_trie(&exp_planes_trie, &check_ranges);
60 }
61
62 #[test]
63 fn free_blocks_16() {
64 run_deserialize_test_from_test_data(include_str!("data/cpt/free-blocks.16.toml"));
65 }
66
67 #[test]
68 fn free_blocks_32() {
69 run_deserialize_test_from_test_data(include_str!("data/cpt/free-blocks.32.toml"));
70 }
71
72 #[test]
73 fn free_blocks_8() {
74 run_deserialize_test_from_test_data(include_str!("data/cpt/free-blocks.8.toml"));
75 }
76
77 #[test]
78 fn free_blocks_small16() {
79 run_deserialize_test_from_test_data(include_str!("data/cpt/free-blocks.small16.toml"));
80 }
81
82 #[test]
83 fn grow_data_16() {
84 run_deserialize_test_from_test_data(include_str!("data/cpt/grow-data.16.toml"));
85 }
86
87 #[test]
88 fn grow_data_32() {
89 run_deserialize_test_from_test_data(include_str!("data/cpt/grow-data.32.toml"));
90 }
91
92 #[test]
93 fn grow_data_8() {
94 run_deserialize_test_from_test_data(include_str!("data/cpt/grow-data.8.toml"));
95 }
96
97 #[test]
98 fn grow_data_small16() {
99 run_deserialize_test_from_test_data(include_str!("data/cpt/grow-data.small16.toml"));
100 }
101
102 #[test]
103 fn set1_16() {
104 run_deserialize_test_from_test_data(include_str!("data/cpt/set1.16.toml"));
105 }
106
107 #[test]
108 fn set1_32() {
109 run_deserialize_test_from_test_data(include_str!("data/cpt/set1.32.toml"));
110 }
111
112 #[test]
113 fn set1_8() {
114 run_deserialize_test_from_test_data(include_str!("data/cpt/set1.8.toml"));
115 }
116
117 #[test]
118 fn set1_small16() {
119 run_deserialize_test_from_test_data(include_str!("data/cpt/set1.small16.toml"));
120 }
121
122 #[test]
123 fn set2_overlap_16() {
124 run_deserialize_test_from_test_data(include_str!("data/cpt/set2-overlap.16.toml"));
125 }
126
127 #[test]
128 fn set2_overlap_32() {
129 run_deserialize_test_from_test_data(include_str!("data/cpt/set2-overlap.32.toml"));
130 }
131
132 #[test]
133 fn set2_overlap_small16() {
134 run_deserialize_test_from_test_data(include_str!("data/cpt/set2-overlap.small16.toml"));
135 }
136
137 #[test]
138 fn set3_initial_9_16() {
139 run_deserialize_test_from_test_data(include_str!("data/cpt/set3-initial-9.16.toml"));
140 }
141
142 #[test]
143 fn set3_initial_9_32() {
144 run_deserialize_test_from_test_data(include_str!("data/cpt/set3-initial-9.32.toml"));
145 }
146
147 #[test]
148 fn set3_initial_9_8() {
149 run_deserialize_test_from_test_data(include_str!("data/cpt/set3-initial-9.8.toml"));
150 }
151
152 #[test]
153 fn set3_initial_9_small16() {
154 run_deserialize_test_from_test_data(include_str!("data/cpt/set3-initial-9.small16.toml"));
155 }
156
157 #[test]
158 fn set_empty_16() {
159 run_deserialize_test_from_test_data(include_str!("data/cpt/set-empty.16.toml"));
160 }
161
162 #[test]
163 fn set_empty_32() {
164 run_deserialize_test_from_test_data(include_str!("data/cpt/set-empty.32.toml"));
165 }
166
167 #[test]
168 fn set_empty_8() {
169 run_deserialize_test_from_test_data(include_str!("data/cpt/set-empty.8.toml"));
170 }
171
172 #[test]
173 fn set_empty_small16() {
174 run_deserialize_test_from_test_data(include_str!("data/cpt/set-empty.small16.toml"));
175 }
176
177 #[test]
178 fn set_single_value_16() {
179 run_deserialize_test_from_test_data(include_str!("data/cpt/set-single-value.16.toml"));
180 }
181
182 #[test]
183 fn set_single_value_32() {
184 run_deserialize_test_from_test_data(include_str!("data/cpt/set-single-value.32.toml"));
185 }
186
187 #[test]
188 fn set_single_value_8() {
189 run_deserialize_test_from_test_data(include_str!("data/cpt/set-single-value.8.toml"));
190 }
191
192 #[test]
193 fn set_single_value_small16() {
194 run_deserialize_test_from_test_data(include_str!("data/cpt/set-single-value.small16.toml"));
195 }
196
197 #[test]
198 fn short_all_same_16() {
199 run_deserialize_test_from_test_data(include_str!("data/cpt/short-all-same.16.toml"));
200 }
201
202 #[test]
203 fn short_all_same_8() {
204 run_deserialize_test_from_test_data(include_str!("data/cpt/short-all-same.8.toml"));
205 }
206
207 #[test]
208 fn short_all_same_small16() {
209 run_deserialize_test_from_test_data(include_str!("data/cpt/short-all-same.small16.toml"));
210 }
211
212 #[test]
213 fn small0_in_fast_16() {
214 run_deserialize_test_from_test_data(include_str!("data/cpt/small0-in-fast.16.toml"));
215 }
216
217 #[test]
218 fn small0_in_fast_32() {
219 run_deserialize_test_from_test_data(include_str!("data/cpt/small0-in-fast.32.toml"));
220 }
221
222 #[test]
223 fn small0_in_fast_8() {
224 run_deserialize_test_from_test_data(include_str!("data/cpt/small0-in-fast.8.toml"));
225 }
226
227 #[test]
228 fn small0_in_fast_small16() {
229 run_deserialize_test_from_test_data(include_str!("data/cpt/small0-in-fast.small16.toml"));
230 }
231
232 /// The width of the elements in the data array of a [`CodePointTrie`].
233 /// See [`UCPTrieValueWidth`](https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/ucptrie_8h.html) in ICU4C.
234 #[derive(Clone, Copy, PartialEq)]
235 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
236 pub enum ValueWidthEnum {
237 Bits16 = 0,
238 Bits32 = 1,
239 Bits8 = 2,
240 }
241
242 /// Test .get() on CodePointTrie by iterating through each range in
243 /// check_ranges and assert that the associated
244 /// value matches the trie value for each code point in the range.
245 pub fn check_trie<T: TrieValue + Into<u32>>(trie: &CodePointTrie<T>, check_ranges: &[u32]) {
246 assert_eq!(
247 0,
248 check_ranges.len() % 2,
249 "check_ranges must have an even number of 32-bit values in (limit,value) pairs"
250 );
251
252 let mut i: u32 = 0;
253 let check_range_tuples = check_ranges.chunks(2);
254 // Iterate over each check range
255 for range_tuple in check_range_tuples {
256 let range_limit = range_tuple[0];
257 let range_value = range_tuple[1];
258 // Check all values in this range, one-by-one
259 while i < range_limit {
260 assert_eq!(range_value, trie.get32(i).into(), "trie_get({})", i,);
261 i += 1;
262 }
263 }
264 }
265
266 /// Test .get_range() / .iter_ranges() on CodePointTrie by calling
267 /// .iter_ranges() on the trie (which returns an iterator that produces values
268 /// by calls to .get_range) and see if it matches the values in check_ranges.
269 pub fn test_check_ranges_get_ranges<T: TrieValue + Into<u32>>(
270 trie: &CodePointTrie<T>,
271 check_ranges: &[u32],
272 ) {
273 assert_eq!(
274 0,
275 check_ranges.len() % 2,
276 "check_ranges must have an even number of 32-bit values in (limit,value) pairs"
277 );
278
279 let mut trie_ranges = trie.iter_ranges();
280
281 let mut range_start: u32 = 0;
282 let check_range_tuples = check_ranges.chunks(2);
283 // Iterate over each check range
284 for range_tuple in check_range_tuples {
285 let range_limit = range_tuple[0];
286 let range_value = range_tuple[1];
287
288 // The check ranges array seems to start with a trivial range whose
289 // limit is zero. range_start is initialized to 0, so we can skip.
290 if range_limit == 0 {
291 continue;
292 }
293
294 let cpm_range = trie_ranges.next();
295 assert!(cpm_range.is_some(), "CodePointTrie iter_ranges() produces fewer ranges than the check_ranges field in testdata has");
296 let cpm_range = cpm_range.unwrap();
297 let cpmr_start = cpm_range.range.start();
298 let cpmr_end = cpm_range.range.end();
299 let cpmr_value: u32 = cpm_range.value.into();
300
301 assert_eq!(range_start, *cpmr_start);
302 assert_eq!(range_limit, *cpmr_end + 1);
303 assert_eq!(range_value, cpmr_value);
304
305 range_start = range_limit;
306 }
307
308 assert!(trie_ranges.next().is_none(), "CodePointTrie iter_ranges() produces more ranges than the check_ranges field in testdata has");
309 }
310
311 /// Run above tests that verify the validity of CodePointTrie methods
312 pub fn run_trie_tests<T: TrieValue + Into<u32>>(trie: &CodePointTrie<T>, check_ranges: &[u32]) {
313 check_trie(trie, check_ranges);
314 test_check_ranges_get_ranges(trie, check_ranges);
315 }
316
317 // The following structs might be useful later for de-/serialization of the
318 // main `CodePointTrie` struct in the corresponding data provider.
319
320 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
321 pub struct UnicodeEnumeratedProperty {
322 pub code_point_map: EnumPropCodePointMap,
323 pub code_point_trie: EnumPropSerializedCPT,
324 }
325
326 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
327 pub struct EnumPropCodePointMap {
328 pub data: EnumPropCodePointMapData,
329 }
330
331 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
332 pub struct EnumPropCodePointMapData {
333 pub long_name: String,
334 pub name: String,
335 pub ranges: Vec<(u32, u32, u32)>,
336 }
337
338 #[allow(clippy::upper_case_acronyms)]
339 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
340 pub struct EnumPropSerializedCPT {
341 #[cfg_attr(any(feature = "serde", test), serde(rename = "struct"))]
342 pub trie_struct: EnumPropSerializedCPTStruct,
343 }
344
345 // These structs support the test data dumped as TOML files from ICU.
346 // Because the properties CodePointMap data will also be dumped from ICU
347 // using similar functions, some of these structs may be useful to refactor
348 // into main code at a later point.
349
350 #[allow(clippy::upper_case_acronyms)]
351 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
352 pub struct EnumPropSerializedCPTStruct {
353 #[cfg_attr(any(feature = "serde", test), serde(skip))]
354 pub long_name: String,
355 pub name: String,
356 pub index: Vec<u16>,
357 pub data_8: Option<Vec<u8>>,
358 pub data_16: Option<Vec<u16>>,
359 pub data_32: Option<Vec<u32>>,
360 #[cfg_attr(any(feature = "serde", test), serde(skip))]
361 pub index_length: u32,
362 #[cfg_attr(any(feature = "serde", test), serde(skip))]
363 pub data_length: u32,
364 #[cfg_attr(any(feature = "serde", test), serde(rename = "highStart"))]
365 pub high_start: u32,
366 #[cfg_attr(any(feature = "serde", test), serde(rename = "shifted12HighStart"))]
367 pub shifted12_high_start: u16,
368 #[cfg_attr(any(feature = "serde", test), serde(rename = "type"))]
369 pub trie_type_enum_val: u8,
370 #[cfg_attr(any(feature = "serde", test), serde(rename = "valueWidth"))]
371 pub value_width_enum_val: u8,
372 #[cfg_attr(any(feature = "serde", test), serde(rename = "index3NullOffset"))]
373 pub index3_null_offset: u16,
374 #[cfg_attr(any(feature = "serde", test), serde(rename = "dataNullOffset"))]
375 pub data_null_offset: u32,
376 #[cfg_attr(any(feature = "serde", test), serde(rename = "nullValue"))]
377 pub null_value: u32,
378 }
379
380 // Given a .toml file dumped from ICU4C test data for UCPTrie, run the test
381 // data file deserialization into the test file struct, convert and construct
382 // the `CodePointTrie`, and test the constructed struct against the test file's
383 // "check ranges" (inversion map ranges) using `check_trie` to verify the
384 // validity of the `CodePointTrie`'s behavior for all code points.
385 #[allow(dead_code)]
386 pub fn run_deserialize_test_from_test_data(test_file: &str) {
387 // The following structs are specific to the TOML format files for dumped ICU
388 // test data.
389
390 #[derive(serde::Deserialize)]
391 pub struct TestFile {
392 code_point_trie: TestCodePointTrie,
393 }
394
395 #[derive(serde::Deserialize)]
396 pub struct TestCodePointTrie {
397 // The trie_struct field for test data files is dumped from the same source
398 // (ICU4C) using the same function (usrc_writeUCPTrie) as property data
399 // for the provider, so we can reuse the same struct here.
400 #[serde(rename(deserialize = "struct"))]
401 trie_struct: EnumPropSerializedCPTStruct,
402 #[serde(rename(deserialize = "testdata"))]
403 test_data: TestData,
404 }
405
406 #[derive(serde::Deserialize)]
407 pub struct TestData {
408 #[serde(rename(deserialize = "checkRanges"))]
409 check_ranges: Vec<u32>,
410 }
411
412 let test_file = ::toml::from_str::<TestFile>(test_file).unwrap();
413
414 let test_struct = test_file.code_point_trie.trie_struct;
415
416 println!(
417 "Running CodePointTrie reader logic test on test data file: {}",
418 test_struct.name
419 );
420
421 let trie_type_enum = match TrieType::try_from(test_struct.trie_type_enum_val) {
422 Ok(enum_val) => enum_val,
423 _ => {
424 panic!(
425 "Could not parse trie_type serialized enum value in test data file: {}",
426 test_struct.name
427 );
428 }
429 };
430
431 let trie_header = CodePointTrieHeader {
432 high_start: test_struct.high_start,
433 shifted12_high_start: test_struct.shifted12_high_start,
434 index3_null_offset: test_struct.index3_null_offset,
435 data_null_offset: test_struct.data_null_offset,
436 null_value: test_struct.null_value,
437 trie_type: trie_type_enum,
438 };
439
440 let index = ZeroVec::from_slice_or_alloc(&test_struct.index);
441
442 match (test_struct.data_8, test_struct.data_16, test_struct.data_32) {
443 (Some(data_8), _, _) => {
444 let data = ZeroVec::from_slice_or_alloc(&data_8);
445 let trie_result: Result<CodePointTrie<u8>, Error> =
446 CodePointTrie::try_new(trie_header, index, data);
447 assert!(trie_result.is_ok(), "Could not construct trie");
448 assert_eq!(
449 test_struct.value_width_enum_val,
450 ValueWidthEnum::Bits8 as u8
451 );
452 run_trie_tests(
453 &trie_result.unwrap(),
454 &test_file.code_point_trie.test_data.check_ranges,
455 );
456 }
457
458 (_, Some(data_16), _) => {
459 let data = ZeroVec::from_slice_or_alloc(&data_16);
460 let trie_result: Result<CodePointTrie<u16>, Error> =
461 CodePointTrie::try_new(trie_header, index, data);
462 assert!(trie_result.is_ok(), "Could not construct trie");
463 assert_eq!(
464 test_struct.value_width_enum_val,
465 ValueWidthEnum::Bits16 as u8
466 );
467 run_trie_tests(
468 &trie_result.unwrap(),
469 &test_file.code_point_trie.test_data.check_ranges,
470 );
471 }
472
473 (_, _, Some(data_32)) => {
474 let data = ZeroVec::from_slice_or_alloc(&data_32);
475 let trie_result: Result<CodePointTrie<u32>, Error> =
476 CodePointTrie::try_new(trie_header, index, data);
477 assert!(trie_result.is_ok(), "Could not construct trie");
478 assert_eq!(
479 test_struct.value_width_enum_val,
480 ValueWidthEnum::Bits32 as u8
481 );
482 run_trie_tests(
483 &trie_result.unwrap(),
484 &test_file.code_point_trie.test_data.check_ranges,
485 );
486 }
487
488 (_, _, _) => {
489 panic!("Could not match test trie data to a known value width or trie type");
490 }
491 };
492 }