Debian Codegraph @

debian/rust-vendor/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 #![allow(dead_code)]
6
7 use icu_collections::codepointtrie::planes::get_planes_trie;
8 use icu_collections::codepointtrie::*;
9 use zerovec::ZeroVec;
10
11 #[test]
12 fn planes_trie_deserialize_check_test() {
13 // Get expected planes trie from crate::planes::get_planes_trie()
14
15 let exp_planes_trie = get_planes_trie();
16
17 // Compute actual planes trie from planes.toml
18
19 let planes_enum_prop =
20 ::toml::from_str::<UnicodeEnumeratedProperty>(include_str!("data/cpt/planes.toml"))
21 .unwrap();
22
23 let code_point_trie_struct = planes_enum_prop.code_point_trie.trie_struct;
24
25 let trie_header = CodePointTrieHeader {
26 high_start: code_point_trie_struct.high_start,
27 shifted12_high_start: code_point_trie_struct.shifted12_high_start,
28 index3_null_offset: code_point_trie_struct.index3_null_offset,
29 data_null_offset: code_point_trie_struct.data_null_offset,
30 null_value: code_point_trie_struct.null_value,
31 trie_type: TrieType::try_from(code_point_trie_struct.trie_type_enum_val).unwrap_or_else(
32 |_| {
33 panic!(
34 "Could not parse trie_type serialized enum value in test data file: {}",
35 code_point_trie_struct.name
36 )
37 },
38 ),
39 };
40
41 let data = ZeroVec::from_slice_or_alloc(code_point_trie_struct.data_8.as_ref().unwrap());
42 let index = ZeroVec::from_slice_or_alloc(&code_point_trie_struct.index);
43 let trie_result = 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 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 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.
268 ///
269 /// `.iter_ranges()` returns an iterator that produces values
270 /// by calls to .`get_range`, and this checks if it matches the values in `check_ranges`.
271 fn test_check_ranges_get_ranges<T: TrieValue + Into<u32>>(
272 trie: &CodePointTrie<T>,
273 check_ranges: &[u32],
274 ) {
275 assert_eq!(
276 0,
277 check_ranges.len() % 2,
278 "check_ranges must have an even number of 32-bit values in (limit,value) pairs"
279 );
280
281 let mut trie_ranges = trie.iter_ranges();
282
283 let mut range_start: u32 = 0;
284 let check_range_tuples = check_ranges.chunks(2);
285 // Iterate over each check range
286 for range_tuple in check_range_tuples {
287 let range_limit = range_tuple[0];
288 let range_value = range_tuple[1];
289
290 // The check ranges array seems to start with a trivial range whose
291 // limit is zero. range_start is initialized to 0, so we can skip.
292 if range_limit == 0 {
293 continue;
294 }
295
296 let cpm_range = trie_ranges.next();
297 assert!(
298 cpm_range.is_some(),
299 "CodePointTrie iter_ranges() produces fewer ranges than the check_ranges field in testdata has"
300 );
301 let cpm_range = cpm_range.unwrap();
302 let cpmr_start = cpm_range.range.start();
303 let cpmr_end = cpm_range.range.end();
304 let cpmr_value: u32 = cpm_range.value.into();
305
306 assert_eq!(range_start, *cpmr_start);
307 assert_eq!(range_limit, *cpmr_end + 1);
308 assert_eq!(range_value, cpmr_value);
309
310 range_start = range_limit;
311 }
312
313 assert!(
314 trie_ranges.next().is_none(),
315 "CodePointTrie iter_ranges() produces more ranges than the check_ranges field in testdata has"
316 );
317 }
318
319 /// Run above tests that verify the validity of [`CodePointTrie`] methods
320 fn run_trie_tests<T: TrieValue + Into<u32>>(trie: &CodePointTrie<T>, check_ranges: &[u32]) {
321 check_trie(trie, check_ranges);
322 test_check_ranges_get_ranges(trie, check_ranges);
323 }
324
325 // The following structs might be useful later for de-/serialization of the
326 // main `CodePointTrie` struct in the corresponding data provider.
327
328 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
329 struct UnicodeEnumeratedProperty {
330 code_point_map: EnumPropCodePointMap,
331 code_point_trie: EnumPropSerializedCPT,
332 }
333
334 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
335 struct EnumPropCodePointMap {
336 data: EnumPropCodePointMapData,
337 }
338
339 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
340 struct EnumPropCodePointMapData {
341 long_name: String,
342 name: String,
343 ranges: Vec<(u32, u32, u32)>,
344 }
345
346 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
347 struct EnumPropSerializedCPT {
348 #[cfg_attr(any(feature = "serde", test), serde(rename = "struct"))]
349 trie_struct: EnumPropSerializedCPTStruct,
350 }
351
352 // These structs support the test data dumped as TOML files from ICU.
353 // Because the properties CodePointMap data will also be dumped from ICU
354 // using similar functions, some of these structs may be useful to refactor
355 // into main code at a later point.
356
357 #[cfg_attr(any(feature = "serde", test), derive(serde::Deserialize))]
358 struct EnumPropSerializedCPTStruct {
359 #[cfg_attr(any(feature = "serde", test), serde(skip))]
360 long_name: String,
361 name: String,
362 index: Vec<u16>,
363 data_8: Option<Vec<u8>>,
364 data_16: Option<Vec<u16>>,
365 data_32: Option<Vec<u32>>,
366 #[cfg_attr(any(feature = "serde", test), serde(skip))]
367 index_length: u32,
368 #[cfg_attr(any(feature = "serde", test), serde(skip))]
369 data_length: u32,
370 #[cfg_attr(any(feature = "serde", test), serde(rename = "highStart"))]
371 high_start: u32,
372 #[cfg_attr(any(feature = "serde", test), serde(rename = "shifted12HighStart"))]
373 shifted12_high_start: u16,
374 #[cfg_attr(any(feature = "serde", test), serde(rename = "type"))]
375 trie_type_enum_val: u8,
376 #[cfg_attr(any(feature = "serde", test), serde(rename = "valueWidth"))]
377 value_width_enum_val: u8,
378 #[cfg_attr(any(feature = "serde", test), serde(rename = "index3NullOffset"))]
379 index3_null_offset: u16,
380 #[cfg_attr(any(feature = "serde", test), serde(rename = "dataNullOffset"))]
381 data_null_offset: u32,
382 #[cfg_attr(any(feature = "serde", test), serde(rename = "nullValue"))]
383 null_value: u32,
384 }
385
386 // Given a .toml file dumped from ICU4C test data for UCPTrie, run the test
387 // data file deserialization into the test file struct, convert and construct
388 // the `CodePointTrie`, and test the constructed struct against the test file's
389 // "check ranges" (inversion map ranges) using `check_trie` to verify the
390 // validity of the `CodePointTrie`'s behavior for all code points.
391 #[allow(dead_code)]
392 fn run_deserialize_test_from_test_data(test_file: &str) {
393 // The following structs are specific to the TOML format files for dumped ICU
394 // test data.
395
396 #[derive(serde::Deserialize)]
397 struct TestFile {
398 code_point_trie: TestCodePointTrie,
399 }
400
401 #[derive(serde::Deserialize)]
402 struct TestCodePointTrie {
403 // The trie_struct field for test data files is dumped from the same source
404 // (ICU4C) using the same function (usrc_writeUCPTrie) as property data
405 // for the provider, so we can reuse the same struct here.
406 #[serde(rename(deserialize = "struct"))]
407 trie_struct: EnumPropSerializedCPTStruct,
408 #[serde(rename(deserialize = "testdata"))]
409 test_data: TestData,
410 }
411
412 #[derive(serde::Deserialize)]
413 struct TestData {
414 #[serde(rename(deserialize = "checkRanges"))]
415 check_ranges: Vec<u32>,
416 }
417
418 let test_file = ::toml::from_str::<TestFile>(test_file).unwrap();
419
420 let test_struct = test_file.code_point_trie.trie_struct;
421
422 println!(
423 "Running CodePointTrie reader logic test on test data file: {}",
424 test_struct.name
425 );
426
427 let trie_type_enum = match TrieType::try_from(test_struct.trie_type_enum_val) {
428 Ok(enum_val) => enum_val,
429 _ => {
430 panic!(
431 "Could not parse trie_type serialized enum value in test data file: {}",
432 test_struct.name
433 );
434 }
435 };
436
437 let trie_header = CodePointTrieHeader {
438 high_start: test_struct.high_start,
439 shifted12_high_start: test_struct.shifted12_high_start,
440 index3_null_offset: test_struct.index3_null_offset,
441 data_null_offset: test_struct.data_null_offset,
442 null_value: test_struct.null_value,
443 trie_type: trie_type_enum,
444 };
445
446 let index = ZeroVec::from_slice_or_alloc(&test_struct.index);
447
448 match (test_struct.data_8, test_struct.data_16, test_struct.data_32) {
449 (Some(data_8), _, _) => {
450 let data = ZeroVec::from_slice_or_alloc(&data_8);
451 let trie_result = CodePointTrie::try_new(trie_header, index, data);
452 assert!(trie_result.is_ok(), "Could not construct trie");
453 assert_eq!(
454 test_struct.value_width_enum_val,
455 ValueWidthEnum::Bits8 as u8
456 );
457 run_trie_tests(
458 &trie_result.unwrap(),
459 &test_file.code_point_trie.test_data.check_ranges,
460 );
461 }
462
463 (_, Some(data_16), _) => {
464 let data = ZeroVec::from_slice_or_alloc(&data_16);
465 let trie_result = CodePointTrie::try_new(trie_header, index, data);
466 assert!(trie_result.is_ok(), "Could not construct trie");
467 assert_eq!(
468 test_struct.value_width_enum_val,
469 ValueWidthEnum::Bits16 as u8
470 );
471 run_trie_tests(
472 &trie_result.unwrap(),
473 &test_file.code_point_trie.test_data.check_ranges,
474 );
475 }
476
477 (_, _, Some(data_32)) => {
478 let data = ZeroVec::from_slice_or_alloc(&data_32);
479 let trie_result = CodePointTrie::try_new(trie_header, index, data);
480 assert!(trie_result.is_ok(), "Could not construct trie");
481 assert_eq!(
482 test_struct.value_width_enum_val,
483 ValueWidthEnum::Bits32 as u8
484 );
485 run_trie_tests(
486 &trie_result.unwrap(),
487 &test_file.code_point_trie.test_data.check_ranges,
488 );
489 }
490
491 (_, _, _) => {
492 panic!("Could not match test trie data to a known value width or trie type");
493 }
494 };
495 }