1#!./perl
2#
3# all.t - tests for all_* routines in
4#	Locale::Country
5#	Locale::Language
6#	Locale::Currency
7#	Locale::Script
8#
9# There are four tests. We get a list of all codes, convert to
10# language/country/currency, # convert back to code,
11# and check that they're the same. Then we do the same,
12# starting with list of languages/countries/currencies.
13#
14
15BEGIN {
16        chdir 't' if -d 't';
17        @INC = '../lib';
18}
19
20use Locale::Country;
21use Locale::Language;
22use Locale::Currency;
23use Locale::Script;
24
25print "1..20\n";
26
27my $code;
28my $language;
29my $country;
30my $ok;
31my $reverse;
32my $currency;
33my $script;
34
35
36#-----------------------------------------------------------------------
37# Old API - without codeset specified, default to ALPHA_2
38#-----------------------------------------------------------------------
39$ok = 1;
40foreach $code (all_country_codes())
41{
42    $country = code2country($code);
43    if (!defined $country)
44    {
45        $ok = 0;
46        last;
47    }
48    $reverse = country2code($country);
49    if (!defined $reverse)
50    {
51        $ok = 0;
52        last;
53    }
54    if ($reverse ne $code)
55    {
56        $ok = 0;
57        last;
58    }
59}
60print ($ok ? "ok 1\n" : "not ok 1\n");
61
62#-----------------------------------------------------------------------
63# code to country, back to code, for ALPHA2
64#-----------------------------------------------------------------------
65$ok = 1;
66foreach $code (all_country_codes(LOCALE_CODE_ALPHA_2))
67{
68    $country = code2country($code, LOCALE_CODE_ALPHA_2);
69    if (!defined $country)
70    {
71        $ok = 0;
72        last;
73    }
74    $reverse = country2code($country, LOCALE_CODE_ALPHA_2);
75    if (!defined $reverse)
76    {
77        $ok = 0;
78        last;
79    }
80    if ($reverse ne $code)
81    {
82        $ok = 0;
83        last;
84    }
85}
86print ($ok ? "ok 2\n" : "not ok 2\n");
87
88#-----------------------------------------------------------------------
89# code to country, back to code, for ALPHA3
90#-----------------------------------------------------------------------
91$ok = 1;
92foreach $code (all_country_codes(LOCALE_CODE_ALPHA_3))
93{
94    $country = code2country($code, LOCALE_CODE_ALPHA_3);
95    if (!defined $country)
96    {
97        $ok = 0;
98        last;
99    }
100    $reverse = country2code($country, LOCALE_CODE_ALPHA_3);
101    if (!defined $reverse)
102    {
103        $ok = 0;
104        last;
105    }
106    if ($reverse ne $code)
107    {
108        $ok = 0;
109        last;
110    }
111}
112print ($ok ? "ok 3\n" : "not ok 3\n");
113
114#-----------------------------------------------------------------------
115# code to country, back to code, for NUMERIC
116#-----------------------------------------------------------------------
117$ok = 1;
118foreach $code (all_country_codes(LOCALE_CODE_NUMERIC))
119{
120    $country = code2country($code, LOCALE_CODE_NUMERIC);
121    if (!defined $country)
122    {
123        $ok = 0;
124        last;
125    }
126    $reverse = country2code($country, LOCALE_CODE_NUMERIC);
127    if (!defined $reverse)
128    {
129        $ok = 0;
130        last;
131    }
132    if ($reverse ne $code)
133    {
134        $ok = 0;
135        last;
136    }
137}
138print ($ok ? "ok 4\n" : "not ok 4\n");
139
140
141#-----------------------------------------------------------------------
142# Old API - country to code, back to country, using default of ALPHA_2
143#-----------------------------------------------------------------------
144$ok = 1;
145foreach $country (all_country_names())
146{
147    $code = country2code($country);
148    if (!defined $code)
149    {
150        $ok = 0;
151        last;
152    }
153    $reverse = code2country($code);
154    if (!defined $reverse)
155    {
156        $ok = 0;
157        last;
158    }
159    if ($reverse ne $country)
160    {
161        $ok = 0;
162        last;
163    }
164}
165print ($ok ? "ok 5\n" : "not ok 5\n");
166
167#-----------------------------------------------------------------------
168# country to code, back to country, using LOCALE_CODE_ALPHA_2
169#-----------------------------------------------------------------------
170$ok = 1;
171foreach $country (all_country_names())
172{
173    $code = country2code($country, LOCALE_CODE_ALPHA_2);
174    if (!defined $code)
175    {
176        $ok = 0;
177        last;
178    }
179    $reverse = code2country($code, LOCALE_CODE_ALPHA_2);
180    if (!defined $reverse)
181    {
182        $ok = 0;
183        last;
184    }
185    if ($reverse ne $country)
186    {
187        $ok = 0;
188        last;
189    }
190}
191print ($ok ? "ok 6\n" : "not ok 6\n");
192
193#-----------------------------------------------------------------------
194# country to code, back to country, using LOCALE_CODE_ALPHA_3
195#-----------------------------------------------------------------------
196$ok = 1;
197foreach $country (all_country_names())
198{
199    $code = country2code($country, LOCALE_CODE_ALPHA_3);
200    if (!defined $code)
201    {
202	next if ($country eq 'Antarctica'
203		 || $country eq 'Bouvet Island'
204		 || $country eq 'Cocos (Keeling) Islands'
205		 || $country eq 'Christmas Island'
206		 || $country eq 'France, Metropolitan'
207		 || $country eq 'South Georgia and the South Sandwich Islands'
208		 || $country eq 'Heard Island and McDonald Islands'
209		 || $country eq 'British Indian Ocean Territory'
210		 || $country eq 'French Southern Territories'
211		 || $country eq 'United States Minor Outlying Islands'
212		 || $country eq 'Mayotte'
213		 || $country eq 'Zaire');
214        $ok = 0;
215        last;
216    }
217    $reverse = code2country($code, LOCALE_CODE_ALPHA_3);
218    if (!defined $reverse)
219    {
220        $ok = 0;
221        last;
222    }
223    if ($reverse ne $country)
224    {
225        $ok = 0;
226        last;
227    }
228}
229print ($ok ? "ok 7\n" : "not ok 7\n");
230
231#-----------------------------------------------------------------------
232# country to code, back to country, using LOCALE_CODE_NUMERIC
233#-----------------------------------------------------------------------
234$ok = 1;
235foreach $country (all_country_names())
236{
237    $code = country2code($country, LOCALE_CODE_NUMERIC);
238    if (!defined $code)
239    {
240	next if ($country eq 'Antarctica'
241		 || $country eq 'Bouvet Island'
242		 || $country eq 'Cocos (Keeling) Islands'
243		 || $country eq 'Christmas Island'
244		 || $country eq 'France, Metropolitan'
245		 || $country eq 'South Georgia and the South Sandwich Islands'
246		 || $country eq 'Heard Island and McDonald Islands'
247		 || $country eq 'British Indian Ocean Territory'
248		 || $country eq 'French Southern Territories'
249		 || $country eq 'United States Minor Outlying Islands'
250		 || $country eq 'Mayotte'
251		 || $country eq 'Zaire');
252        $ok = 0;
253        last;
254    }
255    $reverse = code2country($code, LOCALE_CODE_NUMERIC);
256    if (!defined $reverse)
257    {
258        $ok = 0;
259        last;
260    }
261    if ($reverse ne $country)
262    {
263        $ok = 0;
264        last;
265    }
266}
267print ($ok ? "ok 8\n" : "not ok 8\n");
268
269
270$ok = 1;
271foreach $code (all_language_codes())
272{
273    $language = code2language($code);
274    if (!defined $language)
275    {
276        $ok = 0;
277        last;
278    }
279    $reverse = language2code($language);
280    if (!defined $reverse)
281    {
282        $ok = 0;
283        last;
284    }
285    if ($reverse ne $code)
286    {
287        $ok = 0;
288        last;
289    }
290}
291print ($ok ? "ok 9\n" : "not ok 9\n");
292
293
294$ok = 1;
295foreach $language (all_language_names())
296{
297    $code = language2code($language);
298    if (!defined $code)
299    {
300        $ok = 0;
301        last;
302    }
303    $reverse = code2language($code);
304    if (!defined $reverse)
305    {
306        $ok = 0;
307        last;
308    }
309    if ($reverse ne $language)
310    {
311        $ok = 0;
312        last;
313    }
314}
315print ($ok ? "ok 10\n" : "not ok 10\n");
316
317$ok = 1;
318foreach $code (all_currency_codes())
319{
320    $currency = code2currency($code);
321    if (!defined $currency)
322    {
323        $ok = 0;
324        last;
325    }
326    $reverse = currency2code($currency);
327    if (!defined $reverse)
328    {
329        $ok = 0;
330        last;
331    }
332    #
333    # three special cases:
334    #	The Kwacha has two codes - used in Zambia and Malawi
335    #	The Russian Ruble has two codes - rub and rur
336    #	The Belarussian Ruble has two codes - byb and byr
337    if ($reverse ne $code
338	&& $code ne 'mwk' && $code ne 'zmk'
339	&& $code ne 'byr' && $code ne 'byb'
340	&& $code ne 'rub' && $code ne 'rur')
341    {
342        $ok = 0;
343        last;
344    }
345}
346print ($ok ? "ok 11\n" : "not ok 11\n");
347
348$ok = 1;
349foreach $currency (all_currency_names())
350{
351    $code = currency2code($currency);
352    if (!defined $code)
353    {
354        $ok = 0;
355        last;
356    }
357    $reverse = code2currency($code);
358    if (!defined $reverse)
359    {
360        $ok = 0;
361        last;
362    }
363    if ($reverse ne $currency)
364    {
365        $ok = 0;
366        last;
367    }
368}
369print ($ok ? "ok 12\n" : "not ok 12\n");
370
371#=======================================================================
372#
373#	Locale::Script tests
374#
375#=======================================================================
376
377#-----------------------------------------------------------------------
378# Old API - without codeset specified, default to ALPHA_2
379#-----------------------------------------------------------------------
380$ok = 1;
381foreach $code (all_script_codes())
382{
383    $script = code2script($code);
384    if (!defined $script)
385    {
386        $ok = 0;
387        last;
388    }
389    $reverse = script2code($script);
390    if (!defined $reverse)
391    {
392        $ok = 0;
393        last;
394    }
395    if ($reverse ne $code)
396    {
397        $ok = 0;
398        last;
399    }
400}
401print ($ok ? "ok 13\n" : "not ok 13\n");
402
403#-----------------------------------------------------------------------
404# code to script, back to code, for ALPHA2
405#-----------------------------------------------------------------------
406$ok = 1;
407foreach $code (all_script_codes(LOCALE_CODE_ALPHA_2))
408{
409    $script = code2script($code, LOCALE_CODE_ALPHA_2);
410    if (!defined $script)
411    {
412        $ok = 0;
413        last;
414    }
415    $reverse = script2code($script, LOCALE_CODE_ALPHA_2);
416    if (!defined $reverse)
417    {
418        $ok = 0;
419        last;
420    }
421    if ($reverse ne $code)
422    {
423        $ok = 0;
424        last;
425    }
426}
427print ($ok ? "ok 14\n" : "not ok 14\n");
428
429#-----------------------------------------------------------------------
430# code to script, back to code, for ALPHA3
431#-----------------------------------------------------------------------
432$ok = 1;
433foreach $code (all_script_codes(LOCALE_CODE_ALPHA_3))
434{
435    $script = code2script($code, LOCALE_CODE_ALPHA_3);
436    if (!defined $script)
437    {
438        $ok = 0;
439        last;
440    }
441    $reverse = script2code($script, LOCALE_CODE_ALPHA_3);
442    if (!defined $reverse)
443    {
444        $ok = 0;
445        last;
446    }
447    if ($reverse ne $code)
448    {
449        $ok = 0;
450        last;
451    }
452}
453print ($ok ? "ok 15\n" : "not ok 15\n");
454
455#-----------------------------------------------------------------------
456# code to script, back to code, for NUMERIC
457#-----------------------------------------------------------------------
458$ok = 1;
459foreach $code (all_script_codes(LOCALE_CODE_NUMERIC))
460{
461    $script = code2script($code, LOCALE_CODE_NUMERIC);
462    if (!defined $script)
463    {
464        $ok = 0;
465        last;
466    }
467    $reverse = script2code($script, LOCALE_CODE_NUMERIC);
468    if (!defined $reverse)
469    {
470        $ok = 0;
471        last;
472    }
473    if ($reverse ne $code)
474    {
475        $ok = 0;
476        last;
477    }
478}
479print ($ok ? "ok 16\n" : "not ok 16\n");
480
481
482#-----------------------------------------------------------------------
483# Old API - script to code, back to script, using default of ALPHA_2
484#-----------------------------------------------------------------------
485$ok = 1;
486foreach $script (all_script_names())
487{
488    $code = script2code($script);
489    if (!defined $code)
490    {
491        $ok = 0;
492        last;
493    }
494    $reverse = code2script($code);
495    if (!defined $reverse)
496    {
497        $ok = 0;
498        last;
499    }
500    if ($reverse ne $script)
501    {
502        $ok = 0;
503        last;
504    }
505}
506print ($ok ? "ok 17\n" : "not ok 17\n");
507
508#-----------------------------------------------------------------------
509# script to code, back to script, using LOCALE_CODE_ALPHA_2
510#-----------------------------------------------------------------------
511$ok = 1;
512foreach $script (all_script_names())
513{
514    $code = script2code($script, LOCALE_CODE_ALPHA_2);
515    if (!defined $code)
516    {
517        $ok = 0;
518        last;
519    }
520    $reverse = code2script($code, LOCALE_CODE_ALPHA_2);
521    if (!defined $reverse)
522    {
523        $ok = 0;
524        last;
525    }
526    if ($reverse ne $script)
527    {
528        $ok = 0;
529        last;
530    }
531}
532print ($ok ? "ok 18\n" : "not ok 18\n");
533
534#-----------------------------------------------------------------------
535# script to code, back to script, using LOCALE_CODE_ALPHA_3
536#-----------------------------------------------------------------------
537$ok = 1;
538foreach $script (all_script_names())
539{
540    $code = script2code($script, LOCALE_CODE_ALPHA_3);
541    if (!defined $code)
542    {
543        $ok = 0;
544        last;
545    }
546    $reverse = code2script($code, LOCALE_CODE_ALPHA_3);
547    if (!defined $reverse)
548    {
549        $ok = 0;
550        last;
551    }
552    if ($reverse ne $script)
553    {
554        $ok = 0;
555        last;
556    }
557}
558print ($ok ? "ok 19\n" : "not ok 19\n");
559
560#-----------------------------------------------------------------------
561# script to code, back to script, using LOCALE_CODE_NUMERIC
562#-----------------------------------------------------------------------
563$ok = 1;
564foreach $script (all_script_names())
565{
566    $code = script2code($script, LOCALE_CODE_NUMERIC);
567    if (!defined $code)
568    {
569        $ok = 0;
570        last;
571    }
572    $reverse = code2script($code, LOCALE_CODE_NUMERIC);
573    if (!defined $reverse)
574    {
575        $ok = 0;
576        last;
577    }
578    if ($reverse ne $script)
579    {
580        $ok = 0;
581        last;
582    }
583}
584print ($ok ? "ok 20\n" : "not ok 20\n");
585
586