1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2013-2016, International Business Machines Corporation
6 * and others. All Rights Reserved.
7 ***********************************************************************/
8
9 /***********************************************************************
10 * This testcase ported from ICU4J ( RegionTest.java ) to ICU4C *
11 * Try to keep them in sync if at all possible...! *
12 ***********************************************************************/
13
14 #include "unicode/utypes.h"
15 #include "cstring.h"
16
17 #if !UCONFIG_NO_FORMATTING
18
19 #include "unicode/region.h"
20 #include "regiontst.h"
21
22 typedef struct KnownRegion {
23 const char *code;
24 int32_t numeric;
25 const char *parent;
26 URegionType type;
27 const char *containingContinent;
28 } KnownRegion;
29
30 static 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, "005", URGN_TERRITORY, "019" },
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, "053", 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, "053", 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, "005", URGN_TERRITORY, "019" },
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, "053", 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, "014", URGN_TERRITORY, "002" },
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, "014", 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, "014", URGN_TERRITORY, "002" },
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, "057", 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
RegionTest()338 RegionTest::RegionTest() {
339 }
340
~RegionTest()341 RegionTest::~RegionTest() {
342 }
343
344 void
runIndexedTest(int32_t index,UBool exec,const char * & name,char * par)345 RegionTest::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(TestGroupingChildren);
362 TESTCASE_AUTO_END;
363 }
364
365
TestKnownRegions()366 void RegionTest::TestKnownRegions() {
367
368 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
369 KnownRegion rd = knownRegions[i];
370 UErrorCode status = U_ZERO_ERROR;
371 const Region *r = Region::getInstance(rd.code,status);
372 if ( r ) {
373 int32_t n = r->getNumericCode();
374 int32_t e = rd.numeric;
375 if ( n != e ) {
376 errln("Numeric code mismatch for region %s. Expected:%d Got:%d",r->getRegionCode(),e,n);
377 }
378
379 if (r->getType() != rd.type) {
380 errln("Expected region %s to be of type %d. Got: %d",r->getRegionCode(),rd.type,r->getType());
381 }
382
383 int32_t nc = rd.numeric;
384 if ( nc > 0 ) {
385 const Region *ncRegion = Region::getInstance(nc,status);
386 if ( *ncRegion != *r && nc != 891 ) { // 891 is special case - CS and YU both deprecated codes for region 891
387 errln("Creating region %s by its numeric code returned a different region. Got: %s instead.",r->getRegionCode(),ncRegion->getRegionCode());
388 }
389 }
390 } else {
391 dataerrln("Known region %s was not recognized.",rd.code);
392 }
393 }
394 }
395
TestGetInstanceString()396 void RegionTest::TestGetInstanceString() {
397 typedef struct TestData {
398 const char *inputID;
399 const char *expectedID;
400 URegionType expectedType;
401 } TestData;
402
403 static TestData testData[] = {
404 // Input ID, Expected ID, Expected Type
405 { "DE", "DE", URGN_TERRITORY }, // Normal region
406 { "QU", "EU", URGN_GROUPING }, // Alias to a grouping
407 { "DD", "DE", URGN_TERRITORY }, // Alias to a deprecated region (East Germany) with single preferred value
408 { "276", "DE", URGN_TERRITORY }, // Numeric code for Germany
409 { "278", "DE", URGN_TERRITORY }, // Numeric code for East Germany (Deprecated)
410 { "SU", "SU", URGN_DEPRECATED }, // Alias to a deprecated region with multiple preferred values
411 { "AN", "AN", URGN_DEPRECATED }, // Deprecated region with multiple preferred values
412 { "SVK", "SK", URGN_TERRITORY } // 3-letter code - Slovakia
413 };
414
415
416 UErrorCode status = U_ZERO_ERROR;
417 const Region *r = Region::getInstance((const char *)NULL,status);
418 if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
419 errcheckln(status, "Calling Region::getInstance(NULL) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
420 }
421
422 status = U_ZERO_ERROR;
423 r = Region::getInstance("BOGUS",status);
424 if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
425 errcheckln(status, "Calling Region::getInstance(\"BOGUS\") should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
426 }
427
428
429 for (int32_t i = 0 ; i < UPRV_LENGTHOF(testData) ; i++ ) {
430 TestData data = testData[i];
431 status = U_ZERO_ERROR;
432 r = Region::getInstance(data.inputID,status);
433 const char *id;
434 URegionType type;
435 if ( r ) {
436 id = r->getRegionCode();
437 type = r->getType();
438 } else {
439 id = "NULL";
440 type = URGN_UNKNOWN;
441 }
442 if ( uprv_strcmp(id,data.expectedID)) {
443 dataerrln("Unexpected region ID for Region::getInstance(\"%s\"); Expected: %s Got: %s",data.inputID,data.expectedID,id);
444 }
445 if ( type != data.expectedType) {
446 dataerrln("Unexpected region type for Region::getInstance(\"%s\"); Expected: %d Got: %d",data.inputID,data.expectedType,type);
447 }
448 }
449 }
450
TestGetInstanceInt()451 void RegionTest::TestGetInstanceInt() {
452 typedef struct TestData {
453 int32_t inputID;
454 const char *expectedID;
455 URegionType expectedType;
456 } TestData;
457
458 static TestData testData[] = {
459 // Input ID, Expected ID, Expected Type
460 { 276, "DE", URGN_TERRITORY }, // Numeric code for Germany
461 { 278, "DE", URGN_TERRITORY }, // Numeric code for East Germany (Deprecated)
462 { 419, "419", URGN_GROUPING }, // Latin America
463 { 736, "SD", URGN_TERRITORY }, // Sudan (pre-2011) - changed numeric code after South Sudan split off
464 { 729, "SD", URGN_TERRITORY }, // Sudan (post-2011) - changed numeric code after South Sudan split off
465 };
466
467 UErrorCode status = U_ZERO_ERROR;
468 Region::getInstance(-123,status);
469 if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
470 errcheckln(status, "Calling Region::getInstance(-123) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
471 }
472
473 for (int32_t i = 0 ; i < UPRV_LENGTHOF(testData) ; i++ ) {
474 TestData data = testData[i];
475 status = U_ZERO_ERROR;
476 const Region *r = Region::getInstance(data.inputID,status);
477 const char *id;
478 URegionType type;
479 if ( r ) {
480 id = r->getRegionCode();
481 type = r->getType();
482 } else {
483 id = "NULL";
484 type = URGN_UNKNOWN;
485 }
486 if ( uprv_strcmp(data.expectedID,id)) {
487 dataerrln("Unexpected region ID for Region.getInstance(%d)); Expected: %s Got: %s",data.inputID,data.expectedID,id);
488 }
489 if ( data.expectedType != type) {
490 dataerrln("Unexpected region type for Region.getInstance(%d)); Expected: %d Got: %d",data.inputID,data.expectedType,type);
491 }
492 }
493 }
494
TestGetContainedRegions()495 void RegionTest::TestGetContainedRegions() {
496 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
497 KnownRegion rd = knownRegions[i];
498 UErrorCode status = U_ZERO_ERROR;
499
500 const Region *r = Region::getInstance(rd.code,status);
501 if (r) {
502 if (r->getType() == URGN_GROUPING) {
503 continue;
504 }
505 StringEnumeration *containedRegions = r->getContainedRegions(status);
506 if (U_FAILURE(status)) {
507 errln("%s->getContainedRegions(status) failed: %s", r->getRegionCode(), u_errorName(status));
508 continue;
509 }
510 for ( int32_t i = 0 ; i < containedRegions->count(status); i++ ) {
511 const char *crID = containedRegions->next(NULL,status);
512 const Region *cr = Region::getInstance(crID,status);
513 const Region *containingRegion = cr ? cr->getContainingRegion() : NULL;
514 if ( !containingRegion || *containingRegion != *r ) {
515 errln("Region: %s contains region %s. Expected containing region of this region to be the original region, but got %s",
516 r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
517 }
518 }
519 delete containedRegions;
520 } else {
521 dataerrln("Known region %s was not recognized.",rd.code);
522 }
523 }
524 }
525
TestGetContainedRegionsWithType()526 void RegionTest::TestGetContainedRegionsWithType() {
527 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
528 KnownRegion rd = knownRegions[i];
529 UErrorCode status = U_ZERO_ERROR;
530
531 const Region *r = Region::getInstance(rd.code,status);
532 if (r) {
533 if (r->getType() != URGN_CONTINENT) {
534 continue;
535 }
536 StringEnumeration *containedRegions = r->getContainedRegions(URGN_TERRITORY, status);
537 if (U_FAILURE(status)) {
538 errln("%s->getContainedRegions(URGN_TERRITORY, status) failed: %s", r->getRegionCode(), u_errorName(status));
539 continue;
540 }
541 for ( int32_t j = 0 ; j < containedRegions->count(status); j++ ) {
542 const char *crID = containedRegions->next(NULL,status);
543 const Region *cr = Region::getInstance(crID,status);
544 const Region *containingRegion = cr ? cr->getContainingRegion(URGN_CONTINENT) : NULL;
545 if ( !containingRegion || *containingRegion != *r ) {
546 errln("Continent: %s contains territory %s. Expected containing continent of this region to be the original region, but got %s",
547 r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
548 }
549 }
550 delete containedRegions;
551 } else {
552 dataerrln("Known region %s was not recognized.",rd.code);
553 }
554 }
555 }
556
TestGetContainingRegion()557 void RegionTest::TestGetContainingRegion() {
558 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
559 KnownRegion rd = knownRegions[i];
560 UErrorCode status = U_ZERO_ERROR;
561 const Region *r = Region::getInstance(rd.code,status);
562 if (r) {
563 const Region *c = r->getContainingRegion();
564 if (rd.parent == NULL) {
565 if ( c ) {
566 errln("Containing region for %s should have been NULL. Got: %s",r->getRegionCode(),c->getRegionCode());
567 }
568 } else {
569 const Region *p = Region::getInstance(rd.parent,status);
570 if ( !c || *p != *c ) {
571 errln("Expected containing continent of region %s to be %s. Got: %s",
572 r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
573 }
574 }
575 } else {
576 dataerrln("Known region %s was not recognized.",rd.code);
577 }
578 }
579 }
580
TestGetContainingRegionWithType()581 void RegionTest::TestGetContainingRegionWithType() {
582 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
583 KnownRegion rd = knownRegions[i];
584 UErrorCode status = U_ZERO_ERROR;
585
586 const Region *r = Region::getInstance(rd.code,status);
587 if (r) {
588 const Region *c = r->getContainingRegion(URGN_CONTINENT);
589 if (rd.containingContinent == NULL) {
590 if ( c != NULL) {
591 errln("Containing continent for %s should have been NULL. Got: %s",r->getRegionCode(), c->getRegionCode());
592 }
593 } else {
594 const Region *p = Region::getInstance(rd.containingContinent,status);
595 if ( *p != *c ) {
596 errln("Expected containing continent of region %s to be %s. Got: %s",
597 r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
598 }
599 }
600 } else {
601 dataerrln("Known region %s was not recognized.",rd.code);
602 }
603 }
604 }
605
TestGetPreferredValues()606 void RegionTest::TestGetPreferredValues() {
607 static const char *testData[6][17] = {
608 // Input ID, Expected Preferred Values...
609 { "AN", "CW", "SX", "BQ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Netherlands Antilles
610 { "CS", "RS", "ME", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Serbia & Montenegro
611 { "FQ", "AQ", "TF", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // French Southern and Antarctic Territories
612 { "NT", "IQ", "SA", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Neutral Zone
613 { "PC", "FM", "MH", "MP", "PW", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, // Pacific Islands Trust Territory
614 { "SU", "RU", "AM", "AZ", "BY", "EE", "GE", "KZ", "KG", "LV", "LT", "MD", "TJ", "TM", "UA", "UZ" , NULL}, // Soviet Union
615 };
616
617 for ( int32_t i = 0 ; i < 6 ; i++ ) {
618 const char **data = testData[i];
619 UErrorCode status = U_ZERO_ERROR;
620 const Region *r = Region::getInstance(data[0],status);
621 if (r) {
622 StringEnumeration *preferredValues = r->getPreferredValues(status);
623 if (U_FAILURE(status)) {
624 errln("%s->getPreferredValues(status) failed: %s", r->getRegionCode(), u_errorName(status));
625 continue;
626 }
627 for ( int i = 1 ; data[i] ; i++ ) {
628 UBool found = false;
629 preferredValues->reset(status);
630 while ( const char *check = preferredValues->next(NULL,status) ) {
631 if ( !uprv_strcmp(check,data[i]) ) {
632 found = true;
633 break;
634 }
635 }
636 if ( !found ) {
637 errln("Region::getPreferredValues() for region \"%s\" should have contained \"%s\" but it didn't.",r->getRegionCode(),data[i]);
638 }
639 }
640 delete preferredValues;
641 } else {
642 dataerrln("Known region %s was not recognized.",data[0]);
643 }
644 }
645 }
646
TestContains()647 void RegionTest::TestContains() {
648 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
649 KnownRegion rd = knownRegions[i];
650 UErrorCode status = U_ZERO_ERROR;
651
652 const Region *r = Region::getInstance(rd.code,status);
653 if (r) {
654 const Region *c = r->getContainingRegion();
655 while ( c ) {
656 if ( !c->contains(*r)) {
657 errln("Region \"%s\" should have contained \"%s\" but it didn't.",c->getRegionCode(),r->getRegionCode());
658 }
659 c = c->getContainingRegion();
660 }
661 } else {
662 dataerrln("Known region %s was not recognized.",rd.code);
663 }
664 }
665 }
666
TestAvailableTerritories()667 void RegionTest::TestAvailableTerritories() {
668 // Test to make sure that the set of territories contained in World and the set of all available
669 // territories are one and the same.
670 UErrorCode status = U_ZERO_ERROR;
671 StringEnumeration *availableTerritories = Region::getAvailable(URGN_TERRITORY, status);
672 if (U_FAILURE(status)) {
673 dataerrln("Region::getAvailable(URGN_TERRITORY,status) failed: %s", u_errorName(status));
674 return;
675 }
676 const Region *world = Region::getInstance("001",status);
677 if (U_FAILURE(status)) {
678 dataerrln("Region::getInstance(\"001\",status) failed: %s", u_errorName(status));
679 return;
680 }
681 StringEnumeration *containedInWorld = world->getContainedRegions(URGN_TERRITORY, status);
682 if (U_FAILURE(status)) {
683 errln("world->getContainedRegions(URGN_TERRITORY, status) failed: %s", u_errorName(status));
684 return;
685 }
686 if ( !availableTerritories || !containedInWorld || *availableTerritories != *containedInWorld ) {
687 char availableTerritoriesString[1024] = "";
688 char containedInWorldString[1024] = "";
689 if ( availableTerritories ) {
690 for (int32_t i = 0 ; i < availableTerritories->count(status) ; i++ ) {
691 if ( i > 0 ) {
692 uprv_strcat(availableTerritoriesString," ");
693 }
694 uprv_strcat(availableTerritoriesString,availableTerritories->next(NULL,status));
695 }
696 } else {
697 uprv_strcpy(availableTerritoriesString,"NULL");
698 }
699 if ( containedInWorld ) {
700 for (int32_t i = 0 ; i < containedInWorld->count(status) ; i++ ) {
701 if ( i > 0 ) {
702 uprv_strcat(containedInWorldString," ");
703 }
704 uprv_strcat(containedInWorldString,containedInWorld->next(NULL,status));
705 }
706 } else {
707 uprv_strcpy(containedInWorldString,"NULL");
708 }
709 errln("Available territories and all territories contained in world should be the same set.\nAvailable = %s\nContained in World = %s",
710 availableTerritoriesString,containedInWorldString);
711 }
712 delete availableTerritories;
713 delete containedInWorld;
714 }
715
TestNoContainedRegions(void)716 void RegionTest::TestNoContainedRegions(void) {
717 UErrorCode status = U_ZERO_ERROR;
718 const Region *region = Region::getInstance("BM",status);
719 if (U_FAILURE(status) || region == NULL) {
720 dataerrln("Fail called to Region::getInstance(\"BM\", status) - %s", u_errorName(status));
721 return;
722 }
723 StringEnumeration *containedRegions = region->getContainedRegions(status);
724 if (U_FAILURE(status)) {
725 errln("%s->getContainedRegions(status) failed: %s", region->getRegionCode(), u_errorName(status));
726 return;
727 }
728 const char *emptyStr = containedRegions->next(NULL, status);
729 if (U_FAILURE(status)||(emptyStr!=NULL)) {
730 errln("Error, 'BM' should have no subregions, but returned str=%p, err=%s\n", emptyStr, u_errorName(status));
731 } else {
732 logln("Success - BM has no subregions\n");
733 }
734 delete containedRegions;
735 }
736
TestGroupingChildren(void)737 void RegionTest::TestGroupingChildren(void) {
738 const char* testGroupings[] = {
739 "003", "021,013,029",
740 "419", "013,029,005",
741 "EU", "AT,BE,CY,CZ,DE,DK,EE,ES,FI,FR,GR,HR,HU,IE,IT,LT,LU,LV,MT,NL,PL,PT,SE,SI,SK,BG,RO"
742 };
743
744 for (int32_t i = 0; i < UPRV_LENGTHOF(testGroupings); i += 2) {
745 const char* groupingCode = testGroupings[i];
746 const char* expectedChildren = testGroupings[i + 1];
747
748 UErrorCode err = U_ZERO_ERROR;
749 const Region* grouping = Region::getInstance(groupingCode, err);
750 if (U_SUCCESS(err)) {
751 StringEnumeration* actualChildren = grouping->getContainedRegions(err);
752 if (U_SUCCESS(err)) {
753 int32_t numActualChildren = actualChildren->count(err);
754 int32_t numExpectedChildren = 0;
755 const char* expectedChildStart = expectedChildren;
756 const char* expectedChildEnd = NULL;
757 const char* actualChild = NULL;
758 while ((actualChild = actualChildren->next(NULL, err)) != NULL && *expectedChildStart != '\0') {
759 expectedChildEnd = uprv_strchr(expectedChildStart, ',');
760 if (expectedChildEnd == NULL) {
761 expectedChildEnd = expectedChildStart + uprv_strlen(expectedChildStart);
762 }
763 if (uprv_strlen(actualChild) != size_t(expectedChildEnd - expectedChildStart) || uprv_strncmp(actualChild, expectedChildStart, expectedChildEnd - expectedChildStart) != 0) {
764 errln("Mismatch in child list for %s at position %d: expected %s, got %s\n", groupingCode, numExpectedChildren, expectedChildStart, actualChild);
765 }
766 expectedChildStart = (*expectedChildEnd != '\0') ? expectedChildEnd + 1 : expectedChildEnd;
767 ++numExpectedChildren;
768 }
769 while (expectedChildEnd != NULL && *expectedChildEnd != '\0') {
770 expectedChildEnd = uprv_strchr(expectedChildEnd + 1, ',');
771 ++numExpectedChildren;
772 }
773 if (numExpectedChildren != numActualChildren) {
774 errln("Wrong number of children for %s: expected %d, got %d\n", groupingCode, numExpectedChildren, numActualChildren);
775 }
776 delete actualChildren;
777 } else {
778 errln("Couldn't create iterator for children of %s\n", groupingCode);
779 }
780 } else {
781 errln("Region %s not found\n", groupingCode);
782 }
783 }
784 }
785
786 #endif /* #if !UCONFIG_NO_FORMATTING */
787
788 //eof
789