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