Debian Codegraph @

language/language_test.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package language
6
7 import (
8 "reflect"
9 "testing"
10 )
11
12 func TestTagSize(t *testing.T) {
13 typ := reflect.TypeFor[Tag]()
14 if typ.Size() > 24 {
15 t.Errorf("size of Tag was %d; want 24", typ.Size())
16 }
17 }
18
19 func TestIsRoot(t *testing.T) {
20 loc := Tag{}
21 if !loc.IsRoot() {
22 t.Errorf("unspecified should be root.")
23 }
24 for i, tt := range parseTests() {
25 loc, _ := Parse(tt.in)
26 undef := tt.lang == "und" && tt.script == "" && tt.region == "" && tt.ext == ""
27 if loc.IsRoot() != undef {
28 t.Errorf("%d: was %v; want %v", i, loc.IsRoot(), undef)
29 }
30 }
31 }
32
33 func TestEquality(t *testing.T) {
34 for i, tt := range parseTests() {
35 s := tt.in
36 tag := Make(s)
37 t1 := Make(tag.String())
38 if tag != t1 {
39 t.Errorf("%d:%s: equality test 1 failed\n got: %#v\nwant: %#v)", i, s, t1, tag)
40 }
41 t2, _ := Compose(tag)
42 if tag != t2 {
43 t.Errorf("%d:%s: equality test 2 failed\n got: %#v\nwant: %#v", i, s, t2, tag)
44 }
45 }
46 }
47
48 func TestString(t *testing.T) {
49 tests := []string{
50 "no-u-rg-dkzzzz",
51 }
52 for i, s := range tests {
53 tag := Make(s)
54 if tag.String() != s {
55 t.Errorf("%d:%s: got %s: want %s (%#v)", i, s, tag.String(), s, tag)
56 }
57 }
58 }
59
60 func TestMarshal(t *testing.T) {
61 testCases := []string{
62 // TODO: these values will change with each CLDR update. This issue
63 // will be solved if we decide to fix the indexes.
64 "und",
65 "ca-ES-valencia",
66 "ca-ES-valencia-u-va-posix",
67 "ca-ES-valencia-u-co-phonebk",
68 "ca-ES-valencia-u-co-phonebk-va-posix",
69 "x-klingon",
70 "en-US",
71 "en-US-u-va-posix",
72 "en",
73 "en-u-co-phonebk",
74 "en-001",
75 "sh",
76
77 "en-GB-u-rg-uszzzz",
78 "en-GB-u-rg-uszzzz-va-posix",
79 "en-GB-u-co-phonebk-rg-uszzzz",
80 // Invalid tags should also roundtrip.
81 "en-GB-u-co-phonebk-rg-uszz",
82 }
83 for _, tc := range testCases {
84 var tag Tag
85 err := tag.UnmarshalText([]byte(tc))
86 if err != nil {
87 t.Errorf("UnmarshalText(%q): unexpected error: %v", tc, err)
88 }
89 b, err := tag.MarshalText()
90 if err != nil {
91 t.Errorf("MarshalText(%q): unexpected error: %v", tc, err)
92 }
93 if got := string(b); got != tc {
94 t.Errorf("%s: got %q; want %q", tc, got, tc)
95 }
96 }
97 }
98
99 func TestBase(t *testing.T) {
100 tests := []struct {
101 loc, lang string
102 conf Confidence
103 }{
104 {"und", "en", Low},
105 {"x-abc", "und", No},
106 {"en", "en", Exact},
107 {"und-Cyrl", "ru", High},
108 // If a region is not included, the official language should be English.
109 {"und-US", "en", High},
110 // TODO: not-explicitly listed scripts should probably be und, No
111 // Modify addTags to return info on how the match was derived.
112 // {"und-Aghb", "und", No},
113 }
114 for i, tt := range tests {
115 loc, _ := Parse(tt.loc)
116 lang, conf := loc.Base()
117 if lang.String() != tt.lang {
118 t.Errorf("%d: language was %s; want %s", i, lang, tt.lang)
119 }
120 if conf != tt.conf {
121 t.Errorf("%d: confidence was %d; want %d", i, conf, tt.conf)
122 }
123 }
124 }
125
126 func TestParseBase(t *testing.T) {
127 tests := []struct {
128 in string
129 out string
130 ok bool
131 }{
132 {"en", "en", true},
133 {"EN", "en", true},
134 {"nld", "nl", true},
135 {"dut", "dut", true}, // bibliographic
136 {"aaj", "und", false}, // unknown
137 {"qaa", "qaa", true},
138 {"a", "und", false},
139 {"", "und", false},
140 {"aaaa", "und", false},
141 }
142 for i, tt := range tests {
143 x, err := ParseBase(tt.in)
144 if x.String() != tt.out || err == nil != tt.ok {
145 t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, x, err == nil, tt.out, tt.ok)
146 }
147 if y, _, _ := Raw.Make(tt.out).Raw(); x != y {
148 t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, x, y)
149 }
150 }
151 }
152
153 func TestScript(t *testing.T) {
154 tests := []struct {
155 loc, scr string
156 conf Confidence
157 }{
158 {"und", "Latn", Low},
159 {"en-Latn", "Latn", Exact},
160 {"en", "Latn", High},
161 {"sr", "Cyrl", Low},
162 {"kk", "Cyrl", High},
163 {"kk-CN", "Arab", Low},
164 {"cmn", "Hans", Low},
165 {"ru", "Cyrl", High},
166 {"ru-RU", "Cyrl", High},
167 {"yue", "Hant", Low},
168 {"x-abc", "Zzzz", Low},
169 {"und-zyyy", "Zyyy", Exact},
170 }
171 for i, tt := range tests {
172 loc, _ := Parse(tt.loc)
173 sc, conf := loc.Script()
174 if sc.String() != tt.scr {
175 t.Errorf("%d:%s: script was %s; want %s", i, tt.loc, sc, tt.scr)
176 }
177 if conf != tt.conf {
178 t.Errorf("%d:%s: confidence was %d; want %d", i, tt.loc, conf, tt.conf)
179 }
180 }
181 }
182
183 func TestParseScript(t *testing.T) {
184 tests := []struct {
185 in string
186 out string
187 ok bool
188 }{
189 {"Latn", "Latn", true},
190 {"zzzz", "Zzzz", true},
191 {"zyyy", "Zyyy", true},
192 {"Latm", "Zzzz", false},
193 {"Zzz", "Zzzz", false},
194 {"", "Zzzz", false},
195 {"Zzzxx", "Zzzz", false},
196 }
197 for i, tt := range tests {
198 x, err := ParseScript(tt.in)
199 if x.String() != tt.out || err == nil != tt.ok {
200 t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, x, err == nil, tt.out, tt.ok)
201 }
202 if err == nil {
203 if _, y, _ := Raw.Make("und-" + tt.out).Raw(); x != y {
204 t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, x, y)
205 }
206 }
207 }
208 }
209
210 func TestRegion(t *testing.T) {
211 tests := []struct {
212 loc, reg string
213 conf Confidence
214 }{
215 {"und", "US", Low},
216 {"en", "US", Low},
217 {"zh-Hant", "TW", Low},
218 {"en-US", "US", Exact},
219 {"cmn", "CN", Low},
220 {"ru", "RU", Low},
221 {"yue", "HK", Low},
222 {"x-abc", "ZZ", Low},
223 }
224 for i, tt := range tests {
225 loc, _ := Raw.Parse(tt.loc)
226 reg, conf := loc.Region()
227 if reg.String() != tt.reg {
228 t.Errorf("%d:%s: region was %s; want %s", i, tt.loc, reg, tt.reg)
229 }
230 if conf != tt.conf {
231 t.Errorf("%d:%s: confidence was %d; want %d", i, tt.loc, conf, tt.conf)
232 }
233 }
234 }
235
236 func TestEncodeM49(t *testing.T) {
237 tests := []struct {
238 m49 int
239 code string
240 ok bool
241 }{
242 {1, "001", true},
243 {840, "US", true},
244 {899, "ZZ", false},
245 }
246 for i, tt := range tests {
247 if r, err := EncodeM49(tt.m49); r.String() != tt.code || err == nil != tt.ok {
248 t.Errorf("%d:%d: was %s, %v; want %s, %v", i, tt.m49, r, err == nil, tt.code, tt.ok)
249 }
250 }
251 for i := 1; i <= 1000; i++ {
252 if r, err := EncodeM49(i); err == nil && r.M49() == 0 {
253 t.Errorf("%d has no error, but maps to undefined region", i)
254 }
255 }
256 }
257
258 func TestParseRegion(t *testing.T) {
259 tests := []struct {
260 in string
261 out string
262 ok bool
263 }{
264 {"001", "001", true},
265 {"840", "US", true},
266 {"899", "ZZ", false},
267 {"USA", "US", true},
268 {"US", "US", true},
269 {"BC", "ZZ", false},
270 {"C", "ZZ", false},
271 {"CCCC", "ZZ", false},
272 {"01", "ZZ", false},
273 }
274 for i, tt := range tests {
275 r, err := ParseRegion(tt.in)
276 if r.String() != tt.out || err == nil != tt.ok {
277 t.Errorf("%d:%s: was %s, %v; want %s, %v", i, tt.in, r, err == nil, tt.out, tt.ok)
278 }
279 if err == nil {
280 if _, _, y := Raw.Make("und-" + tt.out).Raw(); r != y {
281 t.Errorf("%d:%s: tag was %s; want %s", i, tt.in, r, y)
282 }
283 }
284 }
285 }
286
287 func TestIsCountry(t *testing.T) {
288 tests := []struct {
289 reg string
290 country bool
291 }{
292 {"US", true},
293 {"001", false},
294 {"958", false},
295 {"419", false},
296 {"203", true},
297 {"020", true},
298 {"900", false},
299 {"999", false},
300 {"QO", false},
301 {"EU", false},
302 {"AA", false},
303 {"XK", true},
304 }
305 for i, tt := range tests {
306 r, _ := ParseRegion(tt.reg)
307 if r.IsCountry() != tt.country {
308 t.Errorf("%d: IsCountry(%s) was %v; want %v", i, tt.reg, r.IsCountry(), tt.country)
309 }
310 }
311 }
312
313 func TestIsGroup(t *testing.T) {
314 tests := []struct {
315 reg string
316 group bool
317 }{
318 {"US", false},
319 {"001", true},
320 {"958", false},
321 {"419", true},
322 {"203", false},
323 {"020", false},
324 {"900", false},
325 {"999", false},
326 {"QO", true},
327 {"EU", true},
328 {"AA", false},
329 {"XK", false},
330 }
331 for i, tt := range tests {
332 r, _ := ParseRegion(tt.reg)
333 if r.IsGroup() != tt.group {
334 t.Errorf("%d: IsGroup(%s) was %v; want %v", i, tt.reg, r.IsGroup(), tt.group)
335 }
336 }
337 }
338
339 func TestContains(t *testing.T) {
340 tests := []struct {
341 enclosing, contained string
342 contains bool
343 }{
344 // A region contains itself.
345 {"US", "US", true},
346 {"001", "001", true},
347
348 // Direct containment.
349 {"001", "002", true},
350 {"039", "XK", true},
351 {"150", "XK", true},
352 {"EU", "AT", true},
353 {"QO", "AQ", true},
354
355 // Indirect containemnt.
356 {"001", "US", true},
357 {"001", "419", true},
358 {"001", "013", true},
359
360 // No containment.
361 {"US", "001", false},
362 {"155", "EU", false},
363 }
364 for i, tt := range tests {
365 r := MustParseRegion(tt.enclosing)
366 con := MustParseRegion(tt.contained)
367 if got := r.Contains(con); got != tt.contains {
368 t.Errorf("%d: %s.Contains(%s) was %v; want %v", i, tt.enclosing, tt.contained, got, tt.contains)
369 }
370 }
371 }
372
373 func TestRegionCanonicalize(t *testing.T) {
374 for i, tt := range []struct{ in, out string }{
375 {"UK", "GB"},
376 {"TP", "TL"},
377 {"QU", "EU"},
378 {"SU", "SU"},
379 {"VD", "VN"},
380 {"DD", "DE"},
381 } {
382 r := MustParseRegion(tt.in)
383 want := MustParseRegion(tt.out)
384 if got := r.Canonicalize(); got != want {
385 t.Errorf("%d: got %v; want %v", i, got, want)
386 }
387 }
388 }
389
390 func TestRegionTLD(t *testing.T) {
391 for _, tt := range []struct {
392 in, out string
393 ok bool
394 }{
395 {"EH", "EH", true},
396 {"FR", "FR", true},
397 {"TL", "TL", true},
398
399 // In ccTLD before in ISO.
400 {"GG", "GG", true},
401
402 // Non-standard assignment of ccTLD to ISO code.
403 {"GB", "UK", true},
404
405 // Exceptionally reserved in ISO and valid ccTLD.
406 {"UK", "UK", true},
407 {"AC", "AC", true},
408 {"EU", "EU", true},
409 {"SU", "SU", true},
410
411 // Exceptionally reserved in ISO and invalid ccTLD.
412 {"CP", "ZZ", false},
413 {"DG", "ZZ", false},
414 {"EA", "ZZ", false},
415 {"FX", "ZZ", false},
416 {"IC", "ZZ", false},
417 {"TA", "ZZ", false},
418
419 // Transitionally reserved in ISO (e.g. deprecated) but valid ccTLD as
420 // it is still being phased out.
421 {"AN", "AN", true},
422 {"TP", "TP", true},
423
424 // Transitionally reserved in ISO (e.g. deprecated) and invalid ccTLD.
425 // Defined in package language as it has a mapping in CLDR.
426 {"BU", "ZZ", false},
427 {"CS", "ZZ", false},
428 {"NT", "ZZ", false},
429 {"YU", "ZZ", false},
430 {"ZR", "ZZ", false},
431 // Not defined in package: SF.
432
433 // Indeterminately reserved in ISO.
434 // Defined in package language as it has a legacy mapping in CLDR.
435 {"DY", "ZZ", false},
436 {"RH", "ZZ", false},
437 {"VD", "ZZ", false},
438 // Not defined in package: EW, FL, JA, LF, PI, RA, RB, RC, RI, RL, RM,
439 // RN, RP, WG, WL, WV, and YV.
440
441 // Not assigned in ISO, but legacy definitions in CLDR.
442 {"DD", "ZZ", false},
443 {"YD", "ZZ", false},
444
445 // Normal mappings but somewhat special status in ccTLD.
446 {"BL", "BL", true},
447 {"MF", "MF", true},
448 {"BV", "BV", true},
449 {"SJ", "SJ", true},
450
451 // Have values when normalized, but not as is.
452 {"QU", "ZZ", false},
453
454 // ISO Private Use.
455 {"AA", "ZZ", false},
456 {"QM", "ZZ", false},
457 {"QO", "ZZ", false},
458 {"XA", "ZZ", false},
459 {"XK", "ZZ", false}, // Sometimes used for Kosovo, but invalid ccTLD.
460 } {
461 if tt.in == "" {
462 continue
463 }
464
465 r := MustParseRegion(tt.in)
466 var want Region
467 if tt.out != "ZZ" {
468 want = MustParseRegion(tt.out)
469 }
470 tld, err := r.TLD()
471 if got := err == nil; got != tt.ok {
472 t.Errorf("error(%v): got %v; want %v", r, got, tt.ok)
473 }
474 if tld != want {
475 t.Errorf("TLD(%v): got %v; want %v", r, tld, want)
476 }
477 }
478 }
479
480 func TestCanonicalize(t *testing.T) {
481 // TODO: do a full test using CLDR data in a separate regression test.
482 tests := []struct {
483 in, out string
484 option CanonType
485 }{
486 {"en-Latn", "en", SuppressScript},
487 {"sr-Cyrl", "sr-Cyrl", SuppressScript},
488 {"sh", "sr-Latn", Legacy},
489 {"sh-HR", "sr-Latn-HR", Legacy},
490 {"sh-Cyrl-HR", "sr-Cyrl-HR", Legacy},
491 {"tl", "fil", Legacy},
492 {"no", "no", Legacy},
493 {"no", "nb", Legacy | CLDR},
494 {"cmn", "cmn", Legacy},
495 {"cmn", "zh", Macro},
496 {"cmn-u-co-stroke", "zh-u-co-stroke", Macro},
497 {"yue", "yue", Macro},
498 {"nb", "no", Macro},
499 {"nb", "nb", Macro | CLDR},
500 {"no", "no", Macro},
501 {"no", "no", Macro | CLDR},
502 {"iw", "he", DeprecatedBase},
503 {"iw", "he", Deprecated | CLDR},
504 {"mo", "ro-MD", Deprecated}, // Adopted by CLDR as of version 25.
505 {"alb", "sq", Legacy}, // bibliographic
506 {"dut", "nl", Legacy}, // bibliographic
507 // As of CLDR 25, mo is no longer considered a legacy mapping.
508 {"mo", "mo", Legacy | CLDR},
509 {"und-AN", "und-AN", Deprecated},
510 {"und-YD", "und-YE", DeprecatedRegion},
511 {"und-YD", "und-YD", DeprecatedBase},
512 {"und-Qaai", "und-Zinh", DeprecatedScript},
513 {"und-Qaai", "und-Qaai", DeprecatedBase},
514 {"drh", "mn", All}, // drh -> khk -> mn
515
516 {"en-GB-u-rg-uszzzz", "en-GB-u-rg-uszzzz", Raw},
517 {"en-GB-u-rg-USZZZZ", "en-GB-u-rg-uszzzz", Raw},
518 // TODO: use different exact values for language and regional tag?
519 {"en-GB-u-rg-uszzzz-va-posix", "en-GB-u-rg-uszzzz-va-posix", Raw},
520 {"en-GB-u-rg-uszzzz-co-phonebk", "en-GB-u-co-phonebk-rg-uszzzz", Raw},
521 // Invalid region specifications are left as is.
522 {"en-GB-u-rg-usz", "en-GB-u-rg-usz", Raw},
523 {"en-GB-u-rg-usz-va-posix", "en-GB-u-rg-usz-va-posix", Raw},
524 {"en-GB-u-rg-usz-co-phonebk", "en-GB-u-co-phonebk-rg-usz", Raw},
525
526 // CVE-2020-28851
527 // invalid key-value pair of -u- extension.
528 {"ES-u-000-00", "es-u-000-00", Raw},
529 {"ES-u-000-00-v-00", "es-u-000-00-v-00", Raw},
530 // reordered and unknown extension.
531 {"ES-v-00-u-000-00", "es-u-000-00-v-00", Raw},
532 }
533 for i, tt := range tests {
534 in, _ := Raw.Parse(tt.in)
535 in, _ = tt.option.Canonicalize(in)
536 if in.String() != tt.out {
537 t.Errorf("%d:%s: was %s; want %s", i, tt.in, in.String(), tt.out)
538 }
539 }
540 // Test idempotence.
541 for _, base := range Supported.BaseLanguages() {
542 tag, _ := Raw.Compose(base)
543 got, _ := All.Canonicalize(tag)
544 want, _ := All.Canonicalize(got)
545 if got != want {
546 t.Errorf("idem(%s): got %s; want %s", tag, got, want)
547 }
548 }
549 }
550
551 func TestTypeForKey(t *testing.T) {
552 tests := []struct{ key, in, out string }{
553 {"co", "en", ""},
554 {"co", "en-u-abc", ""},
555 {"co", "en-u-co-phonebk", "phonebk"},
556 {"co", "en-u-co-phonebk-cu-aud", "phonebk"},
557 {"co", "x-foo-u-co-phonebk", ""},
558 {"va", "en-US-u-va-posix", "posix"},
559 {"rg", "en-u-rg-gbzzzz", "gbzzzz"},
560 {"nu", "en-u-co-phonebk-nu-arabic", "arabic"},
561 {"kc", "cmn-u-co-stroke", ""},
562 {"rg", "cmn-u-rg", ""},
563 {"rg", "cmn-u-rg-co-stroke", ""},
564 {"co", "cmn-u-rg-co-stroke", "stroke"},
565 {"co", "cmn-u-co-rg-gbzzzz", ""},
566 {"rg", "cmn-u-co-rg-gbzzzz", "gbzzzz"},
567 {"rg", "cmn-u-rg-gbzzzz-nlzzzz", "gbzzzz"},
568 }
569 for _, tt := range tests {
570 if v := Make(tt.in).TypeForKey(tt.key); v != tt.out {
571 t.Errorf("%q[%q]: was %q; want %q", tt.in, tt.key, v, tt.out)
572 }
573 }
574 }
575
576 func TestParent(t *testing.T) {
577 tests := []struct{ in, out string }{
578 // Strip variants and extensions first
579 {"de-u-co-phonebk", "de"},
580 {"de-1994", "de"},
581 {"de-Latn-1994", "de"}, // remove superfluous script.
582
583 // Ensure the canonical Tag for an entry is in the chain for base-script
584 // pairs.
585 {"zh-Hans", "zh"},
586
587 // Skip the script if it is the maximized version. CLDR files for the
588 // skipped tag are always empty.
589 {"zh-Hans-TW", "zh"},
590 {"zh-Hans-CN", "zh"},
591
592 // Insert the script if the maximized script is not the same as the
593 // maximized script of the base language.
594 {"zh-TW", "zh-Hant"},
595 {"zh-HK", "zh-Hant"},
596 {"zh-Hant-TW", "zh-Hant"},
597 {"zh-Hant-HK", "zh-Hant"},
598
599 // Non-default script skips to und.
600 // CLDR
601 {"az-Cyrl", "und"},
602 {"bs-Cyrl", "und"},
603 {"en-Dsrt", "und"},
604 {"ha-Arab", "und"},
605 {"mn-Mong", "und"},
606 {"pa-Arab", "und"},
607 {"shi-Latn", "und"},
608 {"sr-Latn", "und"},
609 {"uz-Arab", "und"},
610 {"uz-Cyrl", "und"},
611 {"vai-Latn", "und"},
612 {"zh-Hant", "und"},
613 // extra
614 {"nl-Cyrl", "und"},
615
616 // World english inherits from en-001.
617 {"en-150", "en-001"},
618 {"en-AU", "en-001"},
619 {"en-BE", "en-001"},
620 {"en-GG", "en-001"},
621 {"en-GI", "en-001"},
622 {"en-HK", "en-001"},
623 {"en-IE", "en-001"},
624 {"en-IM", "en-001"},
625 {"en-IN", "en-001"},
626 {"en-JE", "en-001"},
627 {"en-MT", "en-001"},
628 {"en-NZ", "en-001"},
629 {"en-PK", "en-001"},
630 {"en-SG", "en-001"},
631
632 // Spanish in Latin-American countries have es-419 as parent.
633 {"es-AR", "es-419"},
634 {"es-BO", "es-419"},
635 {"es-CL", "es-419"},
636 {"es-CO", "es-419"},
637 {"es-CR", "es-419"},
638 {"es-CU", "es-419"},
639 {"es-DO", "es-419"},
640 {"es-EC", "es-419"},
641 {"es-GT", "es-419"},
642 {"es-HN", "es-419"},
643 {"es-MX", "es-419"},
644 {"es-NI", "es-419"},
645 {"es-PA", "es-419"},
646 {"es-PE", "es-419"},
647 {"es-PR", "es-419"},
648 {"es-PY", "es-419"},
649 {"es-SV", "es-419"},
650 {"es-US", "es-419"},
651 {"es-UY", "es-419"},
652 {"es-VE", "es-419"},
653 // exceptions (according to CLDR)
654 {"es-CW", "es"},
655
656 // Inherit from pt-PT, instead of pt for these countries.
657 {"pt-AO", "pt-PT"},
658 {"pt-CV", "pt-PT"},
659 {"pt-GW", "pt-PT"},
660 {"pt-MO", "pt-PT"},
661 {"pt-MZ", "pt-PT"},
662 {"pt-ST", "pt-PT"},
663 {"pt-TL", "pt-PT"},
664
665 {"en-GB-u-co-phonebk-rg-uszzzz", "en-GB"},
666 {"en-GB-u-rg-uszzzz", "en-GB"},
667 {"en-US-u-va-posix", "en-US"},
668
669 // Difference between language and regional tag.
670 {"ca-ES-valencia", "ca-ES"},
671 {"ca-ES-valencia-u-rg-ptzzzz", "ca-ES"},
672 {"en-US-u-va-variant", "en-US"},
673 {"en-u-va-variant", "en"},
674 {"en-u-rg-gbzzzz", "en"},
675 {"en-US-u-rg-gbzzzz", "en-US"},
676 {"nl-US-u-rg-gbzzzz", "nl-US"},
677 }
678 for _, tt := range tests {
679 tag := Raw.MustParse(tt.in)
680 if p := Raw.MustParse(tt.out); p != tag.Parent() {
681 t.Errorf("%s: was %v; want %v", tt.in, tag.Parent(), p)
682 }
683 }
684 }
685
686 var (
687 // Tags without error that don't need to be changed.
688 benchBasic = []string{
689 "en",
690 "en-Latn",
691 "en-GB",
692 "za",
693 "zh-Hant",
694 "zh",
695 "zh-HK",
696 "ar-MK",
697 "en-CA",
698 "fr-CA",
699 "fr-CH",
700 "fr",
701 "lv",
702 "he-IT",
703 "tlh",
704 "ja",
705 "ja-Jpan",
706 "ja-Jpan-JP",
707 "de-1996",
708 "de-CH",
709 "sr",
710 "sr-Latn",
711 }
712 // Tags with extensions, not changes required.
713 benchExt = []string{
714 "x-a-b-c-d",
715 "x-aa-bbbb-cccccccc-d",
716 "en-x_cc-b-bbb-a-aaa",
717 "en-c_cc-b-bbb-a-aaa-x-x",
718 "en-u-co-phonebk",
719 "en-Cyrl-u-co-phonebk",
720 "en-US-u-co-phonebk-cu-xau",
721 "en-nedix-u-co-phonebk",
722 "en-t-t0-abcd",
723 "en-t-nl-latn",
724 "en-t-t0-abcd-x-a",
725 "en_t_pt_MLt",
726 "en-t-fr-est",
727 }
728 // Change, but not memory allocation required.
729 benchSimpleChange = []string{
730 "EN",
731 "i-klingon",
732 "en-latn",
733 "zh-cmn-Hans-CN",
734 "iw-NL",
735 }
736 // Change and memory allocation required.
737 benchChangeAlloc = []string{
738 "en-c_cc-b-bbb-a-aaa",
739 "en-u-cu-xua-co-phonebk",
740 "en-u-cu-xua-co-phonebk-a-cd",
741 "en-u-def-abc-cu-xua-co-phonebk",
742 "en-t-en-Cyrl-NL-1994",
743 "en-t-en-Cyrl-NL-1994-t0-abc-def",
744 }
745 // Tags that result in errors.
746 benchErr = []string{
747 // IllFormed
748 "x_A.-B-C_D",
749 "en-u-cu-co-phonebk",
750 "en-u-cu-xau-co",
751 "en-t-nl-abcd",
752 // Invalid
753 "xx",
754 "nl-Uuuu",
755 "nl-QB",
756 }
757 benchChange = append(benchSimpleChange, benchChangeAlloc...)
758 benchAll = append(append(append(benchBasic, benchExt...), benchChange...), benchErr...)
759 )
760
761 func doParse(b *testing.B, tag []string) {
762 for i := 0; i < b.N; i++ {
763 // Use the modulo instead of looping over all tags so that we get a somewhat
764 // meaningful ns/op.
765 Parse(tag[i%len(tag)])
766 }
767 }
768
769 func BenchmarkParse(b *testing.B) {
770 doParse(b, benchAll)
771 }
772
773 func BenchmarkParseBasic(b *testing.B) {
774 doParse(b, benchBasic)
775 }
776
777 func BenchmarkParseError(b *testing.B) {
778 doParse(b, benchErr)
779 }
780
781 func BenchmarkParseSimpleChange(b *testing.B) {
782 doParse(b, benchSimpleChange)
783 }
784
785 func BenchmarkParseChangeAlloc(b *testing.B) {
786 doParse(b, benchChangeAlloc)
787 }