1/***********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2013-2015, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
6
7/***********************************************************************
8 * This testcase ported from ICU4J ( RegionTest.java ) to ICU4C        *
9 * Try to keep them in sync if at all possible...!                     *
10 ***********************************************************************/
11
12#include "unicode/utypes.h"
13#include "cstring.h"
14
15#if !UCONFIG_NO_FORMATTING
16
17#include "unicode/region.h"
18#include "regiontst.h"
19
20typedef struct KnownRegion {
21  const char *code;
22  int32_t numeric;
23  const char *parent;
24  URegionType type;
25  const char *containingContinent;
26} KnownRegion;
27
28static KnownRegion knownRegions[] = {
29    // Code, Num, Parent, Type,             Containing Continent
30    { "TP" , 626, "035", URGN_TERRITORY, "142" },
31    { "001", 1,  NULL ,  URGN_WORLD,        NULL },
32    { "002", 2,  "001",  URGN_CONTINENT,    NULL },
33    { "003", 3,  NULL,   URGN_GROUPING,     NULL },
34    { "005", 5,  "019",  URGN_SUBCONTINENT, "019" },
35    { "009", 9,  "001",  URGN_CONTINENT,    NULL},
36    { "011", 11, "002",  URGN_SUBCONTINENT, "002" },
37    { "013", 13, "019",  URGN_SUBCONTINENT, "019" },
38    { "014", 14, "002",  URGN_SUBCONTINENT, "002" },
39    { "015", 15, "002",  URGN_SUBCONTINENT, "002" },
40    { "017", 17, "002",  URGN_SUBCONTINENT, "002" },
41    { "018", 18, "002",  URGN_SUBCONTINENT, "002" },
42    { "019", 19, "001",  URGN_CONTINENT, NULL },
43    { "021", 21, "019",  URGN_SUBCONTINENT, "019" },
44    { "029", 29, "019",  URGN_SUBCONTINENT, "019" },
45    { "030", 30, "142",  URGN_SUBCONTINENT, "142" },
46    { "034", 34, "142",  URGN_SUBCONTINENT, "142" },
47    { "035", 35, "142",  URGN_SUBCONTINENT, "142" },
48    { "039", 39, "150",  URGN_SUBCONTINENT, "150"},
49    { "053", 53, "009",  URGN_SUBCONTINENT, "009" },
50    { "054", 54, "009",  URGN_SUBCONTINENT, "009" },
51    { "057", 57, "009",  URGN_SUBCONTINENT, "009" },
52    { "061", 61, "009",  URGN_SUBCONTINENT, "009" },
53    { "142", 142, "001", URGN_CONTINENT, NULL },
54    { "143", 143, "142", URGN_SUBCONTINENT, "142" },
55    { "145", 145, "142", URGN_SUBCONTINENT, "142" },
56    { "150", 150, "001", URGN_CONTINENT, NULL },
57    { "151", 151, "150", URGN_SUBCONTINENT, "150" },
58    { "154", 154, "150", URGN_SUBCONTINENT, "150" },
59    { "155", 155, "150", URGN_SUBCONTINENT, "150" },
60    { "419", 419, NULL,  URGN_GROUPING , NULL},
61    { "AC" ,  -1, "QO" , URGN_TERRITORY, "009" },
62    { "AD" ,  20, "039", URGN_TERRITORY, "150" },
63    { "AE" , 784, "145", URGN_TERRITORY, "142" },
64    { "AF" ,   4, "034", URGN_TERRITORY, "142" },
65    { "AG" ,  28, "029", URGN_TERRITORY, "019" },
66    { "AI" , 660, "029", URGN_TERRITORY, "019" },
67    { "AL" ,   8, "039", URGN_TERRITORY, "150" },
68    { "AM" ,  51, "145", URGN_TERRITORY, "142" },
69    { "AN" , 530, NULL,  URGN_DEPRECATED, NULL },
70    { "AO" ,  24, "017", URGN_TERRITORY, "002" },
71    { "AQ" ,  10, "QO" , URGN_TERRITORY, "009" },
72    { "AR" ,  32, "005", URGN_TERRITORY, "019" },
73    { "AS" ,  16, "061", URGN_TERRITORY, "009" },
74    { "AT" ,  40, "155", URGN_TERRITORY, "150" },
75    { "AU" ,  36, "053", URGN_TERRITORY, "009" },
76    { "AW" , 533, "029", URGN_TERRITORY, "019" },
77    { "AX" , 248, "154", URGN_TERRITORY, "150" },
78    { "AZ" ,  31, "145", URGN_TERRITORY, "142" },
79    { "BA" ,  70, "039", URGN_TERRITORY, "150" },
80    { "BB" ,  52, "029", URGN_TERRITORY, "019" },
81    { "BD" ,  50, "034", URGN_TERRITORY, "142" },
82    { "BE" ,  56, "155", URGN_TERRITORY, "150" },
83    { "BF" , 854, "011", URGN_TERRITORY, "002" },
84    { "BG" , 100, "151", URGN_TERRITORY, "150" },
85    { "BH" ,  48, "145", URGN_TERRITORY, "142" },
86    { "BI" , 108, "014", URGN_TERRITORY, "002" },
87    { "BJ" , 204, "011", URGN_TERRITORY, "002" },
88    { "BL" , 652, "029", URGN_TERRITORY, "019" },
89    { "BM" ,  60, "021", URGN_TERRITORY, "019" },
90    { "BN" ,  96, "035", URGN_TERRITORY, "142" },
91    { "BO" ,  68, "005", URGN_TERRITORY, "019" },
92    { "BQ" , 535, "029", URGN_TERRITORY, "019" },
93    { "BR" ,  76, "005", URGN_TERRITORY, "019" },
94    { "BS" ,  44, "029", URGN_TERRITORY, "019" },
95    { "BT" ,  64, "034", URGN_TERRITORY, "142" },
96    { "BU" , 104, "035", URGN_TERRITORY, "142" },
97    { "BV" ,  74, "QO" , URGN_TERRITORY, "009" },
98    { "BW" ,  72, "018", URGN_TERRITORY, "002" },
99    { "BY" , 112, "151", URGN_TERRITORY, "150" },
100    { "BZ" ,  84, "013", URGN_TERRITORY, "019" },
101    { "CA" , 124, "021", URGN_TERRITORY, "019" },
102    { "CC" , 166, "QO" , URGN_TERRITORY, "009" },
103    { "CD" , 180, "017", URGN_TERRITORY, "002" },
104    { "CF" , 140, "017", URGN_TERRITORY, "002" },
105    { "CG" , 178, "017", URGN_TERRITORY, "002" },
106    { "CH" , 756, "155", URGN_TERRITORY, "150" },
107    { "CI" , 384, "011", URGN_TERRITORY, "002" },
108    { "CK" , 184, "061", URGN_TERRITORY, "009" },
109    { "CL" , 152, "005", URGN_TERRITORY, "019" },
110    { "CM" , 120, "017", URGN_TERRITORY, "002" },
111    { "CN" , 156, "030", URGN_TERRITORY, "142" },
112    { "CO" , 170, "005", URGN_TERRITORY, "019" },
113    { "CP" , -1 , "QO" , URGN_TERRITORY, "009" },
114    { "CR" , 188, "013", URGN_TERRITORY, "019" },
115    { "CU" , 192, "029", URGN_TERRITORY, "019" },
116    { "CV" , 132, "011", URGN_TERRITORY, "002" },
117    { "CW" , 531, "029", URGN_TERRITORY, "019" },
118    { "CX" , 162, "QO" , URGN_TERRITORY, "009" },
119    { "CY" , 196, "145", URGN_TERRITORY, "142" },
120    { "CZ" , 203, "151", URGN_TERRITORY, "150" },
121    { "DD" , 276, "155", URGN_TERRITORY, "150" },
122    { "DE" , 276, "155", URGN_TERRITORY, "150" },
123    { "DG" , -1 , "QO" , URGN_TERRITORY, "009" },
124    { "DJ" , 262, "014", URGN_TERRITORY, "002" },
125    { "DK" , 208, "154", URGN_TERRITORY, "150" },
126    { "DM" , 212, "029", URGN_TERRITORY, "019" },
127    { "DO" , 214, "029", URGN_TERRITORY, "019" },
128    { "DZ" ,  12, "015", URGN_TERRITORY, "002" },
129    { "EA" ,  -1, "015", URGN_TERRITORY, "002" },
130    { "EC" , 218, "005", URGN_TERRITORY, "019" },
131    { "EE" , 233, "154", URGN_TERRITORY, "150" },
132    { "EG" , 818, "015", URGN_TERRITORY, "002" },
133    { "EH" , 732, "015", URGN_TERRITORY, "002" },
134    { "ER" , 232, "014", URGN_TERRITORY, "002" },
135    { "ES" , 724, "039", URGN_TERRITORY, "150" },
136    { "ET" , 231, "014", URGN_TERRITORY, "002" },
137    { "EU" , 967, NULL,  URGN_GROUPING, NULL },
138    { "FI" , 246, "154", URGN_TERRITORY, "150" },
139    { "FJ" , 242, "054", URGN_TERRITORY, "009" },
140    { "FK" , 238, "005", URGN_TERRITORY, "019" },
141    { "FM" , 583, "057", URGN_TERRITORY, "009" },
142    { "FO" , 234, "154", URGN_TERRITORY, "150" },
143    { "FR" , 250, "155", URGN_TERRITORY, "150" },
144    { "FX" , 250, "155", URGN_TERRITORY, "150" },
145    { "GA" , 266, "017", URGN_TERRITORY, "002" },
146    { "GB" , 826, "154", URGN_TERRITORY, "150" },
147    { "GD" , 308, "029", URGN_TERRITORY, "019" },
148    { "GE" , 268, "145", URGN_TERRITORY, "142" },
149    { "GF" , 254, "005", URGN_TERRITORY, "019" },
150    { "GG" , 831, "154", URGN_TERRITORY, "150" },
151    { "GH" , 288, "011", URGN_TERRITORY, "002" },
152    { "GI" , 292, "039", URGN_TERRITORY, "150" },
153    { "GL" , 304, "021", URGN_TERRITORY, "019" },
154    { "GM" , 270, "011", URGN_TERRITORY, "002" },
155    { "GN" , 324, "011", URGN_TERRITORY, "002" },
156    { "GP" , 312, "029", URGN_TERRITORY, "019" },
157    { "GQ" , 226, "017", URGN_TERRITORY, "002" },
158    { "GR" , 300, "039", URGN_TERRITORY, "150" },
159    { "GS" , 239, "QO" , URGN_TERRITORY, "009" },
160    { "GT" , 320, "013", URGN_TERRITORY, "019" },
161    { "GU" , 316, "057", URGN_TERRITORY, "009" },
162    { "GW" , 624, "011", URGN_TERRITORY, "002" },
163    { "GY" , 328, "005", URGN_TERRITORY, "019" },
164    { "HK" , 344, "030", URGN_TERRITORY, "142" },
165    { "HM" , 334, "QO" , URGN_TERRITORY, "009" },
166    { "HN" , 340, "013", URGN_TERRITORY, "019" },
167    { "HR" , 191, "039", URGN_TERRITORY, "150" },
168    { "HT" , 332, "029", URGN_TERRITORY, "019" },
169    { "HU" , 348, "151", URGN_TERRITORY, "150" },
170    { "IC" ,  -1, "015", URGN_TERRITORY, "002" },
171    { "ID" , 360, "035", URGN_TERRITORY, "142" },
172    { "IE" , 372, "154", URGN_TERRITORY, "150" },
173    { "IL" , 376, "145", URGN_TERRITORY, "142" },
174    { "IM" , 833, "154", URGN_TERRITORY, "150" },
175    { "IN" , 356, "034", URGN_TERRITORY, "142" },
176    { "IO" ,  86, "QO" , URGN_TERRITORY, "009" },
177    { "IQ" , 368, "145", URGN_TERRITORY, "142" },
178    { "IR" , 364, "034", URGN_TERRITORY, "142" },
179    { "IS" , 352, "154", URGN_TERRITORY, "150" },
180    { "IT" , 380, "039", URGN_TERRITORY, "150" },
181    { "JE" , 832, "154", URGN_TERRITORY, "150" },
182    { "JM" , 388, "029", URGN_TERRITORY, "019" },
183    { "JO" , 400, "145", URGN_TERRITORY, "142" },
184    { "JP" , 392, "030", URGN_TERRITORY, "142" },
185    { "KE" , 404, "014", URGN_TERRITORY, "002" },
186    { "KG" , 417, "143", URGN_TERRITORY, "142" },
187    { "KH" , 116, "035", URGN_TERRITORY, "142" },
188    { "KI" , 296, "057", URGN_TERRITORY, "009" },
189    { "KM" , 174, "014", URGN_TERRITORY, "002" },
190    { "KN" , 659, "029", URGN_TERRITORY, "019" },
191    { "KP" , 408, "030", URGN_TERRITORY, "142" },
192    { "KR" , 410, "030", URGN_TERRITORY, "142" },
193    { "KW" , 414, "145", URGN_TERRITORY, "142" },
194    { "KY" , 136, "029", URGN_TERRITORY, "019" },
195    { "KZ" , 398, "143", URGN_TERRITORY, "142" },
196    { "LA" , 418, "035", URGN_TERRITORY, "142" },
197    { "LB" , 422, "145", URGN_TERRITORY, "142" },
198    { "LC" , 662, "029", URGN_TERRITORY, "019" },
199    { "LI" , 438, "155", URGN_TERRITORY, "150" },
200    { "LK" , 144, "034", URGN_TERRITORY, "142" },
201    { "LR" , 430, "011", URGN_TERRITORY, "002" },
202    { "LS" , 426, "018", URGN_TERRITORY, "002" },
203    { "LT" , 440, "154", URGN_TERRITORY, "150" },
204    { "LU" , 442, "155", URGN_TERRITORY, "150" },
205    { "LV" , 428, "154", URGN_TERRITORY, "150" },
206    { "LY" , 434, "015", URGN_TERRITORY, "002" },
207    { "MA" , 504, "015", URGN_TERRITORY, "002" },
208    { "MC" , 492, "155", URGN_TERRITORY, "150" },
209    { "MD" , 498, "151", URGN_TERRITORY, "150" },
210    { "ME" , 499, "039", URGN_TERRITORY, "150" },
211    { "MF" , 663, "029", URGN_TERRITORY, "019" },
212    { "MG" , 450, "014", URGN_TERRITORY, "002" },
213    { "MH" , 584, "057", URGN_TERRITORY, "009" },
214    { "MK" , 807, "039", URGN_TERRITORY, "150" },
215    { "ML" , 466, "011", URGN_TERRITORY, "002" },
216    { "MM" , 104, "035", URGN_TERRITORY, "142" },
217    { "MN" , 496, "030", URGN_TERRITORY, "142" },
218    { "MO" , 446, "030", URGN_TERRITORY, "142" },
219    { "MP" , 580, "057", URGN_TERRITORY, "009" },
220    { "MQ" , 474, "029", URGN_TERRITORY, "019" },
221    { "MR" , 478, "011", URGN_TERRITORY, "002" },
222    { "MS" , 500, "029", URGN_TERRITORY, "019" },
223    { "MT" , 470, "039", URGN_TERRITORY, "150" },
224    { "MU" , 480, "014", URGN_TERRITORY, "002" },
225    { "MV" , 462, "034", URGN_TERRITORY, "142" },
226    { "MW" , 454, "014", URGN_TERRITORY, "002" },
227    { "MX" , 484, "013", URGN_TERRITORY, "019"},
228    { "MY" , 458, "035", URGN_TERRITORY, "142" },
229    { "MZ" , 508, "014", URGN_TERRITORY, "002" },
230    { "NA" , 516, "018", URGN_TERRITORY, "002" },
231    { "NC" , 540, "054", URGN_TERRITORY, "009" },
232    { "NE" , 562, "011", URGN_TERRITORY, "002" },
233    { "NF" , 574, "053", URGN_TERRITORY, "009" },
234    { "NG" , 566, "011", URGN_TERRITORY, "002" },
235    { "NI" , 558, "013", URGN_TERRITORY, "019" },
236    { "NL" , 528, "155", URGN_TERRITORY, "150" },
237    { "NO" , 578, "154", URGN_TERRITORY, "150" },
238    { "NP" , 524, "034", URGN_TERRITORY, "142" },
239    { "NR" , 520, "057", URGN_TERRITORY, "009" },
240    { "NT" , 536, NULL , URGN_DEPRECATED, NULL },
241    { "NU" , 570, "061", URGN_TERRITORY, "009" },
242    { "NZ" , 554, "053", URGN_TERRITORY, "009" },
243    { "OM" , 512, "145", URGN_TERRITORY, "142" },
244    { "PA" , 591, "013", URGN_TERRITORY, "019" },
245    { "PE" , 604, "005", URGN_TERRITORY, "019" },
246    { "PF" , 258, "061", URGN_TERRITORY, "009" },
247    { "PG" , 598, "054", URGN_TERRITORY, "009" },
248    { "PH" , 608, "035", URGN_TERRITORY, "142" },
249    { "PK" , 586, "034", URGN_TERRITORY, "142" },
250    { "PL" , 616, "151", URGN_TERRITORY, "150" },
251    { "PM" , 666, "021", URGN_TERRITORY, "019" },
252    { "PN" , 612, "061", URGN_TERRITORY, "009" },
253    { "PR" , 630, "029", URGN_TERRITORY, "019" },
254    { "PS" , 275, "145", URGN_TERRITORY, "142" },
255    { "PT" , 620, "039", URGN_TERRITORY, "150" },
256    { "PW" , 585, "057", URGN_TERRITORY, "009" },
257    { "PY" , 600, "005", URGN_TERRITORY, "019" },
258    { "QA" , 634, "145", URGN_TERRITORY, "142" },
259    { "QO" , 961, "009", URGN_SUBCONTINENT, "009" },
260    { "QU" , 967, NULL,  URGN_GROUPING, NULL },
261    { "RE" , 638, "014", URGN_TERRITORY, "002" },
262    { "RO" , 642, "151", URGN_TERRITORY, "150" },
263    { "RS" , 688, "039", URGN_TERRITORY, "150" },
264    { "RU" , 643, "151", URGN_TERRITORY, "150" },
265    { "RW" , 646, "014", URGN_TERRITORY, "002" },
266    { "SA" , 682, "145", URGN_TERRITORY, "142" },
267    { "SB" ,  90, "054", URGN_TERRITORY, "009" },
268    { "SC" , 690, "014", URGN_TERRITORY, "002" },
269    { "SD" , 729, "015", URGN_TERRITORY, "002" },
270    { "SE" , 752, "154", URGN_TERRITORY, "150" },
271    { "SG" , 702, "035", URGN_TERRITORY, "142" },
272    { "SH" , 654, "011", URGN_TERRITORY, "002" },
273    { "SI" , 705, "039", URGN_TERRITORY, "150" },
274    { "SJ" , 744, "154", URGN_TERRITORY, "150" },
275    { "SK" , 703, "151", URGN_TERRITORY, "150" },
276    { "SL" , 694, "011", URGN_TERRITORY, "002" },
277    { "SM" , 674, "039", URGN_TERRITORY, "150" },
278    { "SN" , 686, "011", URGN_TERRITORY, "002" },
279    { "SO" , 706, "014", URGN_TERRITORY, "002" },
280    { "SR" , 740, "005", URGN_TERRITORY, "019" },
281    { "SS" , 728, "015", URGN_TERRITORY, "002" },
282    { "ST" , 678, "017", URGN_TERRITORY, "002" },
283    { "SU" , 810, NULL , URGN_DEPRECATED , NULL},
284    { "SV" , 222, "013", URGN_TERRITORY, "019" },
285    { "SX" , 534, "029", URGN_TERRITORY, "019" },
286    { "SY" , 760, "145", URGN_TERRITORY, "142" },
287    { "SZ" , 748, "018", URGN_TERRITORY, "002" },
288    { "TA" ,  -1, "QO",  URGN_TERRITORY, "009" },
289    { "TC" , 796, "029", URGN_TERRITORY, "019" },
290    { "TD" , 148, "017", URGN_TERRITORY, "002" },
291    { "TF" , 260, "QO" , URGN_TERRITORY, "009" },
292    { "TG" , 768, "011", URGN_TERRITORY, "002" },
293    { "TH" , 764, "035", URGN_TERRITORY, "142" },
294    { "TJ" , 762, "143", URGN_TERRITORY, "142" },
295    { "TK" , 772, "061", URGN_TERRITORY, "009" },
296    { "TL" , 626, "035", URGN_TERRITORY, "142" },
297    { "TM" , 795, "143", URGN_TERRITORY, "142" },
298    { "TN" , 788, "015", URGN_TERRITORY, "002" },
299    { "TO" , 776, "061", URGN_TERRITORY, "009" },
300    { "TP" , 626, "035", URGN_TERRITORY, "142" },
301    { "TR" , 792, "145", URGN_TERRITORY, "142" },
302    { "TT" , 780, "029", URGN_TERRITORY, "019" },
303    { "TV" , 798, "061", URGN_TERRITORY, "009" },
304    { "TW" , 158, "030", URGN_TERRITORY, "142" },
305    { "TZ" , 834, "014", URGN_TERRITORY, "002" },
306    { "UA" , 804, "151", URGN_TERRITORY, "150" },
307    { "UG" , 800, "014", URGN_TERRITORY, "002" },
308    { "UM" , 581, "QO" , URGN_TERRITORY, "009" },
309    { "US" , 840, "021", URGN_TERRITORY, "019" },
310    { "UY" , 858, "005", URGN_TERRITORY, "019" },
311    { "UZ" , 860, "143", URGN_TERRITORY, "142" },
312    { "VA" , 336, "039", URGN_TERRITORY, "150" },
313    { "VC" , 670, "029", URGN_TERRITORY, "019" },
314    { "VE" , 862, "005", URGN_TERRITORY, "019" },
315    { "VG" ,  92, "029", URGN_TERRITORY, "019" },
316    { "VI" , 850, "029", URGN_TERRITORY, "019" },
317    { "VN" , 704, "035", URGN_TERRITORY, "142" },
318    { "VU" , 548, "054", URGN_TERRITORY, "009" },
319    { "WF" , 876, "061", URGN_TERRITORY, "009" },
320    { "WS" , 882, "061", URGN_TERRITORY, "009" },
321    { "YD" , 887, "145", URGN_TERRITORY, "142" },
322    { "YE" , 887, "145", URGN_TERRITORY, "142" },
323    { "YT" , 175, "014", URGN_TERRITORY, "002" },
324    { "ZA" , 710, "018", URGN_TERRITORY, "002" },
325    { "ZM" , 894, "014", URGN_TERRITORY, "002" },
326    { "ZR" , 180, "017", URGN_TERRITORY, "002" },
327    { "ZW" , 716, "014", URGN_TERRITORY, "002" },
328    { "ZZ" , 999, NULL , URGN_UNKNOWN, NULL }
329    };
330
331// *****************************************************************************
332// class RegionTest
333// *****************************************************************************
334
335
336RegionTest::RegionTest() {
337}
338
339RegionTest::~RegionTest() {
340}
341
342void
343RegionTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
344{
345   optionv = (par && *par=='v');
346
347   TESTCASE_AUTO_BEGIN;
348   TESTCASE_AUTO(TestKnownRegions);
349   TESTCASE_AUTO(TestGetInstanceString);
350   TESTCASE_AUTO(TestGetInstanceInt);
351   TESTCASE_AUTO(TestGetContainedRegions);
352   TESTCASE_AUTO(TestGetContainedRegionsWithType);
353   TESTCASE_AUTO(TestGetContainingRegion);
354   TESTCASE_AUTO(TestGetContainingRegionWithType);
355   TESTCASE_AUTO(TestGetPreferredValues);
356   TESTCASE_AUTO(TestContains);
357   TESTCASE_AUTO(TestAvailableTerritories);
358   TESTCASE_AUTO(TestNoContainedRegions);
359   TESTCASE_AUTO_END;
360}
361
362
363void RegionTest::TestKnownRegions() {
364
365    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
366        KnownRegion rd = knownRegions[i];
367        UErrorCode status = U_ZERO_ERROR;
368        const Region *r = Region::getInstance(rd.code,status);
369        if ( r ) {
370            int32_t n = r->getNumericCode();
371            int32_t e = rd.numeric;
372            if ( n != e ) {
373                errln("Numeric code mismatch for region %s.  Expected:%d Got:%d",r->getRegionCode(),e,n);
374            }
375
376            if (r->getType() != rd.type) {
377                errln("Expected region %s to be of type %d. Got: %d",r->getRegionCode(),rd.type,r->getType());
378            }
379
380            int32_t nc = rd.numeric;
381            if ( nc > 0 ) {
382                const Region *ncRegion = Region::getInstance(nc,status);
383                if ( *ncRegion != *r && nc != 891 ) { // 891 is special case - CS and YU both deprecated codes for region 891
384                    errln("Creating region %s by its numeric code returned a different region. Got: %s instead.",r->getRegionCode(),ncRegion->getRegionCode());
385                }
386             }
387        } else {
388            dataerrln("Known region %s was not recognized.",rd.code);
389        }
390    }
391}
392
393void RegionTest::TestGetInstanceString() {
394    typedef struct TestData {
395        const char *inputID;
396        const char *expectedID;
397        URegionType expectedType;
398    } TestData;
399
400    static TestData testData[] = {
401    //  Input ID, Expected ID, Expected Type
402        { "DE", "DE", URGN_TERRITORY },  // Normal region
403        { "QU", "EU", URGN_GROUPING },   // Alias to a grouping
404        { "DD", "DE", URGN_TERRITORY },  // Alias to a deprecated region (East Germany) with single preferred value
405        { "276", "DE", URGN_TERRITORY }, // Numeric code for Germany
406        { "278", "DE", URGN_TERRITORY }, // Numeric code for East Germany (Deprecated)
407        { "SU", "SU", URGN_DEPRECATED }, // Alias to a deprecated region with multiple preferred values
408        { "AN", "AN", URGN_DEPRECATED }, // Deprecated region with multiple preferred values
409        { "SVK", "SK", URGN_TERRITORY }  // 3-letter code - Slovakia
410    };
411
412
413    UErrorCode status = U_ZERO_ERROR;
414    const Region *r = Region::getInstance((const char *)NULL,status);
415    if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
416        errcheckln(status, "Calling Region::getInstance(NULL) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
417    }
418
419    status = U_ZERO_ERROR;
420    r = Region::getInstance("BOGUS",status);
421    if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
422        errcheckln(status, "Calling Region::getInstance(\"BOGUS\") should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
423    }
424
425
426    for (int32_t i = 0 ; i < UPRV_LENGTHOF(testData) ; i++ ) {
427        TestData data = testData[i];
428        status = U_ZERO_ERROR;
429        r = Region::getInstance(data.inputID,status);
430        const char *id;
431        URegionType type;
432        if ( r ) {
433            id = r->getRegionCode();
434            type = r->getType();
435        } else {
436            id = "NULL";
437            type = URGN_UNKNOWN;
438        }
439        if ( uprv_strcmp(id,data.expectedID)) {
440            dataerrln("Unexpected region ID for Region::getInstance(\"%s\"); Expected: %s Got: %s",data.inputID,data.expectedID,id);
441        }
442        if ( type != data.expectedType) {
443            dataerrln("Unexpected region type for Region::getInstance(\"%s\"); Expected: %d Got: %d",data.inputID,data.expectedType,type);
444        }
445    }
446}
447
448void RegionTest::TestGetInstanceInt() {
449    typedef struct TestData {
450        int32_t inputID;
451        const char *expectedID;
452        URegionType expectedType;
453    } TestData;
454
455    static TestData testData[] = {
456        //  Input ID, Expected ID, Expected Type
457        { 276, "DE",  URGN_TERRITORY }, // Numeric code for Germany
458        { 278, "DE",  URGN_TERRITORY }, // Numeric code for East Germany (Deprecated)
459        { 419, "419", URGN_GROUPING },  // Latin America
460        { 736, "SD",  URGN_TERRITORY }, // Sudan (pre-2011) - changed numeric code after South Sudan split off
461        { 729, "SD",  URGN_TERRITORY }, // Sudan (post-2011) - changed numeric code after South Sudan split off
462    };
463
464    UErrorCode status = U_ZERO_ERROR;
465    Region::getInstance(-123,status);
466    if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
467        errcheckln(status, "Calling Region::getInstance(-123) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
468    }
469
470    for (int32_t i = 0 ; i < UPRV_LENGTHOF(testData) ; i++ ) {
471        TestData data = testData[i];
472        status = U_ZERO_ERROR;
473        const Region *r = Region::getInstance(data.inputID,status);
474        const char *id;
475        URegionType type;
476        if ( r ) {
477            id = r->getRegionCode();
478            type = r->getType();
479        } else {
480            id = "NULL";
481            type = URGN_UNKNOWN;
482        }
483        if ( uprv_strcmp(data.expectedID,id)) {
484            dataerrln("Unexpected region ID for Region.getInstance(%d)); Expected: %s Got: %s",data.inputID,data.expectedID,id);
485        }
486        if ( data.expectedType != type) {
487            dataerrln("Unexpected region type for Region.getInstance(%d)); Expected: %d Got: %d",data.inputID,data.expectedType,type);
488        }
489    }
490}
491
492void RegionTest::TestGetContainedRegions() {
493    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
494        KnownRegion rd = knownRegions[i];
495        UErrorCode status = U_ZERO_ERROR;
496
497        const Region *r = Region::getInstance(rd.code,status);
498        if (r) {
499            if (r->getType() == URGN_GROUPING) {
500                continue;
501            }
502            StringEnumeration *containedRegions = r->getContainedRegions(status);
503            if (U_FAILURE(status)) {
504              errln("%s->getContainedRegions(status) failed: %s", r->getRegionCode(), u_errorName(status));
505              continue;
506            }
507            for ( int32_t i = 0 ; i < containedRegions->count(status); i++ ) {
508                const char *crID = containedRegions->next(NULL,status);
509                const Region *cr = Region::getInstance(crID,status);
510                const Region *containingRegion = cr ? cr->getContainingRegion() : NULL;
511                if ( !containingRegion || *containingRegion != *r ) {
512                    errln("Region: %s contains region %s. Expected containing region of this region to be the original region, but got %s",
513                        r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
514                }
515            }
516            delete containedRegions;
517        } else {
518            dataerrln("Known region %s was not recognized.",rd.code);
519        }
520    }
521}
522
523void RegionTest::TestGetContainedRegionsWithType() {
524    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
525        KnownRegion rd = knownRegions[i];
526        UErrorCode status = U_ZERO_ERROR;
527
528        const Region *r = Region::getInstance(rd.code,status);
529        if (r) {
530            if (r->getType() != URGN_CONTINENT) {
531                continue;
532            }
533            StringEnumeration *containedRegions = r->getContainedRegions(URGN_TERRITORY, status);
534            if (U_FAILURE(status)) {
535              errln("%s->getContainedRegions(URGN_TERRITORY, status) failed: %s", r->getRegionCode(), u_errorName(status));
536              continue;
537            }
538            for ( int32_t j = 0 ; j < containedRegions->count(status); j++ ) {
539                const char *crID = containedRegions->next(NULL,status);
540                const Region *cr = Region::getInstance(crID,status);
541                const Region *containingRegion = cr ? cr->getContainingRegion(URGN_CONTINENT) : NULL;
542                if ( !containingRegion || *containingRegion != *r ) {
543                    errln("Continent: %s contains territory %s. Expected containing continent of this region to be the original region, but got %s",
544                        r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
545                }
546            }
547            delete containedRegions;
548        } else {
549            dataerrln("Known region %s was not recognized.",rd.code);
550        }
551    }
552}
553
554void RegionTest::TestGetContainingRegion() {
555    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
556        KnownRegion rd = knownRegions[i];
557        UErrorCode status = U_ZERO_ERROR;
558        const Region *r = Region::getInstance(rd.code,status);
559        if (r) {
560            const Region *c = r->getContainingRegion();
561            if (rd.parent == NULL) {
562                if ( c ) {
563                    errln("Containing region for %s should have been NULL.  Got: %s",r->getRegionCode(),c->getRegionCode());
564                }
565            } else {
566                const Region *p = Region::getInstance(rd.parent,status);
567                if ( !c || *p != *c ) {
568                    errln("Expected containing continent of region %s to be %s. Got: %s",
569                        r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
570                }
571            }
572        } else {
573            dataerrln("Known region %s was not recognized.",rd.code);
574        }
575    }
576}
577
578void RegionTest::TestGetContainingRegionWithType() {
579    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
580        KnownRegion rd = knownRegions[i];
581        UErrorCode status = U_ZERO_ERROR;
582
583        const Region *r = Region::getInstance(rd.code,status);
584        if (r) {
585            const Region *c = r->getContainingRegion(URGN_CONTINENT);
586            if (rd.containingContinent == NULL) {
587                 if ( c != NULL) {
588                     errln("Containing continent for %s should have been NULL.  Got: %s",r->getRegionCode(), c->getRegionCode());
589                 }
590            } else {
591                const Region *p = Region::getInstance(rd.containingContinent,status);
592                if ( *p != *c ) {
593                    errln("Expected containing continent of region %s to be %s. Got: %s",
594                        r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
595                }
596            }
597        } else {
598            dataerrln("Known region %s was not recognized.",rd.code);
599        }
600    }
601}
602
603void RegionTest::TestGetPreferredValues() {
604    static const char *testData[6][17] = {
605        //  Input ID, Expected Preferred Values...
606        { "AN", "CW", "SX", "BQ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Netherlands Antilles
607        { "CS", "RS", "ME", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },     // Serbia & Montenegro
608        { "FQ", "AQ", "TF", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },     // French Southern and Antarctic Territories
609        { "NT", "IQ", "SA", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },     // Neutral Zone
610        { "PC", "FM", "MH", "MP", "PW", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, // Pacific Islands Trust Territory
611        { "SU", "RU", "AM", "AZ", "BY", "EE", "GE", "KZ", "KG", "LV", "LT", "MD", "TJ", "TM", "UA", "UZ" , NULL}, // Soviet Union
612    };
613
614    for ( int32_t i = 0 ; i < 6 ; i++ ) {
615        const char **data = testData[i];
616        UErrorCode status = U_ZERO_ERROR;
617        const Region *r = Region::getInstance(data[0],status);
618        if (r) {
619            StringEnumeration *preferredValues = r->getPreferredValues(status);
620            if (U_FAILURE(status)) {
621              errln("%s->getPreferredValues(status) failed: %s", r->getRegionCode(), u_errorName(status));
622              continue;
623            }
624            for ( int i = 1 ; data[i] ; i++ ) {
625                UBool found = FALSE;
626                preferredValues->reset(status);
627                while ( const char *check = preferredValues->next(NULL,status) ) {
628                    if ( !uprv_strcmp(check,data[i]) ) {
629                        found = TRUE;
630                        break;
631                    }
632                }
633                if ( !found ) {
634                    errln("Region::getPreferredValues() for region \"%s\" should have contained \"%s\" but it didn't.",r->getRegionCode(),data[i]);
635                }
636            }
637            delete preferredValues;
638        } else {
639            dataerrln("Known region %s was not recognized.",data[0]);
640        }
641    }
642}
643
644void RegionTest::TestContains() {
645    for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
646        KnownRegion rd = knownRegions[i];
647        UErrorCode status = U_ZERO_ERROR;
648
649        const Region *r = Region::getInstance(rd.code,status);
650        if (r) {
651            const Region *c = r->getContainingRegion();
652            while ( c ) {
653                if ( !c->contains(*r)) {
654                    errln("Region \"%s\" should have contained \"%s\" but it didn't.",c->getRegionCode(),r->getRegionCode());
655                }
656                c = c->getContainingRegion();
657            }
658        } else {
659            dataerrln("Known region %s was not recognized.",rd.code);
660        }
661    }
662}
663
664void RegionTest::TestAvailableTerritories() {
665    // Test to make sure that the set of territories contained in World and the set of all available
666    // territories are one and the same.
667    UErrorCode status = U_ZERO_ERROR;
668    StringEnumeration *availableTerritories = Region::getAvailable(URGN_TERRITORY, status);
669    if (U_FAILURE(status)) {
670        dataerrln("Region::getAvailable(URGN_TERRITORY,status) failed: %s", u_errorName(status));
671        return;
672    }
673    const Region *world = Region::getInstance("001",status);
674    if (U_FAILURE(status)) {
675        dataerrln("Region::getInstance(\"001\",status) failed: %s", u_errorName(status));
676        return;
677    }
678    StringEnumeration *containedInWorld = world->getContainedRegions(URGN_TERRITORY, status);
679    if (U_FAILURE(status)) {
680        errln("world->getContainedRegions(URGN_TERRITORY, status) failed: %s", u_errorName(status));
681        return;
682    }
683    if ( !availableTerritories || !containedInWorld || *availableTerritories != *containedInWorld ) {
684        char availableTerritoriesString[1024] = "";
685        char containedInWorldString[1024] = "";
686        if ( availableTerritories ) {
687            for (int32_t i = 0 ; i < availableTerritories->count(status) ; i++ ) {
688                if ( i > 0 ) {
689                    uprv_strcat(availableTerritoriesString," ");
690                }
691                uprv_strcat(availableTerritoriesString,availableTerritories->next(NULL,status));
692            }
693        } else {
694            uprv_strcpy(availableTerritoriesString,"NULL");
695        }
696        if ( containedInWorld ) {
697            for (int32_t i = 0 ; i < containedInWorld->count(status) ; i++ ) {
698                if ( i > 0 ) {
699                    uprv_strcat(containedInWorldString," ");
700                }
701                uprv_strcat(containedInWorldString,containedInWorld->next(NULL,status));
702            }
703        } else {
704            uprv_strcpy(containedInWorldString,"NULL");
705        }
706        errln("Available territories and all territories contained in world should be the same set.\nAvailable          = %s\nContained in World = %s",
707            availableTerritoriesString,containedInWorldString);
708    }
709    delete availableTerritories;
710    delete containedInWorld;
711}
712
713void RegionTest::TestNoContainedRegions(void) {
714  UErrorCode status = U_ZERO_ERROR;
715  const Region *region = Region::getInstance("BM",status);
716  if (U_FAILURE(status) || region == NULL) {
717      dataerrln("Fail called to Region::getInstance(\"BM\", status) - %s", u_errorName(status));
718      return;
719  }
720  StringEnumeration *containedRegions = region->getContainedRegions(status);
721  if (U_FAILURE(status)) {
722      errln("%s->getContainedRegions(status) failed: %s", region->getRegionCode(), u_errorName(status));
723      return;
724  }
725  const char *emptyStr = containedRegions->next(NULL, status);
726  if (U_FAILURE(status)||(emptyStr!=NULL)) {
727    errln("Error, 'BM' should have no subregions, but returned str=%p, err=%s\n", emptyStr, u_errorName(status));
728  } else {
729    logln("Success - BM has no subregions\n");
730  }
731  delete containedRegions;
732}
733
734#endif /* #if !UCONFIG_NO_FORMATTING */
735
736//eof
737