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