Debian Codegraph @

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