Debian Codegraph @

debian/missing-sources/icu_locale_core/benches/iai_langid.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_locale_core::{langid, subtags::language, subtags::region, LanguageIdentifier};
6 use std::borrow::Cow;
7 use writeable::Writeable;
8
9 const LIDS: &[LanguageIdentifier] = &[
10 langid!("en"),
11 langid!("pl"),
12 langid!("fr-CA"),
13 langid!("zh-Hans"),
14 langid!("en-US"),
15 langid!("en-Latn-US"),
16 langid!("sr-Cyrl-BA"),
17 ];
18
19 const LIDS_STR: &[&str] = &[
20 "en",
21 "pl",
22 "fr-CA",
23 "zh-Hans",
24 "en-US",
25 "en-Latn-US",
26 "sr-Cyrl-BA",
27 ];
28
29 fn bench_langid_constr() {
30 // Tests the instructions required to construct a LID from an str.
31
32 let _: Vec<LanguageIdentifier> = LIDS_STR
33 .iter()
34 .map(|l| l.parse().expect("Failed to parse"))
35 .collect();
36 }
37
38 fn bench_langid_compare_components() {
39 // Tests the cost of comparing LID components.
40
41 let result = LIDS
42 .iter()
43 .filter(|l| l.language == language!("en") && l.region == Some(region!("US")))
44 .count();
45
46 assert_eq!(result, 2);
47 }
48
49 fn bench_langid_compare_components_str() {
50 // Tests the cost of comparing LID components to str.
51
52 let result = LIDS
53 .iter()
54 .filter(|l| {
55 l.language == language!("en") && l.region.map(|r| r == region!("US")).unwrap_or(false)
56 })
57 .count();
58
59 assert_eq!(result, 2);
60 }
61
62 fn bench_langid_strict_cmp() {
63 // Tests the cost of comparing a langid against byte strings.
64 use core::cmp::Ordering;
65
66 let lid = langid!("en-us");
67
68 let result = LIDS_STR
69 .iter()
70 .filter(|s| lid.strict_cmp(s.as_bytes()) == Ordering::Equal)
71 .count();
72
73 assert_eq!(result, 1);
74 }
75
76 fn bench_langid_matching() {
77 // Tests matching a LID against other LIDs.
78
79 let lid = langid!("en-us");
80
81 let count = LIDS.iter().filter(|l| lid == **l).count();
82 assert_eq!(count, 1);
83 }
84
85 fn bench_langid_matching_str() {
86 // Tests matching a LID against list of str.
87
88 let lid = langid!("en-us");
89
90 let count = LIDS_STR.iter().filter(|&l| lid.normalizing_eq(l)).count();
91 assert_eq!(count, 1);
92 }
93
94 fn bench_langid_serialize() {
95 // Tests serialization of LIDs.
96
97 let _: Vec<String> = LIDS.iter().map(|l| l.to_string()).collect();
98 }
99
100 fn bench_langid_serialize_writeable() {
101 // Tests serialization of LIDs.
102
103 let _: Vec<_> = LIDS.iter().map(|l| l.write_to_string()).collect();
104 }
105
106 fn bench_langid_canonicalize() {
107 // Tests canonicalization of strings.
108
109 let _: Vec<Cow<str>> = LIDS_STR
110 .iter()
111 .map(|l| LanguageIdentifier::normalize(l).expect("Normalization failed"))
112 .collect();
113 }
114
115 iai::main!(
116 bench_langid_constr,
117 bench_langid_compare_components,
118 bench_langid_compare_components_str,
119 bench_langid_strict_cmp,
120 bench_langid_matching,
121 bench_langid_matching_str,
122 bench_langid_serialize,
123 bench_langid_serialize_writeable,
124 bench_langid_canonicalize,
125 );