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 ).
6 langid
, subtags_language
as language
, subtags_region
as region
, LanguageIdentifier
,
8 use writeable
::Writeable
;
10 const LIDS
: &[LanguageIdentifier
] = &[
16 langid
!("en-Latn-US"),
17 langid
!("sr-Cyrl-BA"),
20 const LIDS_STR
: &[&str] = &[
30 fn bench_langid_constr() {
31 // Tests the instructions required to construct a LID from an str.
33 let _
: Vec
<LanguageIdentifier
> = LIDS_STR
35 .map(|l
| l
.parse().expect("Failed to parse"))
39 fn bench_langid_compare_components() {
40 // Tests the cost of comparing LID components.
44 .filter(|l
| l
.language
== language
!("en") && l
.region
== Some(region
!("US")))
47 assert_eq
!(result
, 2);
50 fn bench_langid_compare_components_str() {
51 // Tests the cost of comparing LID components to str.
56 l
.language
== language
!("en") && l
.region
.map(|r
| r
== region
!("US")).unwrap_or(false)
60 assert_eq
!(result
, 2);
63 fn bench_langid_strict_cmp() {
64 // Tests the cost of comparing a langid against byte strings.
65 use core
::cmp
::Ordering
;
67 let lid
= langid
!("en_us");
71 .filter(|s
| lid
.strict_cmp(s
.as_bytes()) == Ordering
::Equal
)
74 assert_eq
!(result
, 1);
77 fn bench_langid_matching() {
78 // Tests matching a LID against other LIDs.
80 let lid
= langid
!("en_us");
82 let count
= LIDS
.iter().filter(|l
| lid
== **l
).count();
86 fn bench_langid_matching_str() {
87 // Tests matching a LID against list of str.
89 let lid
= langid
!("en_us");
91 let count
= LIDS_STR
.iter().filter(|&l
| lid
.normalizing_eq(l
)).count();
95 fn bench_langid_serialize() {
96 // Tests serialization of LIDs.
98 let _
: Vec
<String
> = LIDS
.iter().map(|l
| l
.to_string()).collect();
101 fn bench_langid_serialize_writeable() {
102 // Tests serialization of LIDs.
104 let _
: Vec
<_
> = LIDS
.iter().map(|l
| l
.write_to_string()).collect();
107 fn bench_langid_canonicalize() {
108 // Tests canonicalization of strings.
110 let _
: Vec
<String
> = LIDS_STR
112 .map(|l
| LanguageIdentifier
::canonicalize(l
).expect("Canonicalization failed"))
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
,