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_END;
362 }
363
364
TestKnownRegions()365 void RegionTest::TestKnownRegions() {
366
367 for (int32_t i = 0 ; i < UPRV_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
TestGetInstanceString()395 void 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 errcheckln(status, "Calling Region::getInstance(NULL) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
419 }
420
421 status = U_ZERO_ERROR;
422 r = Region::getInstance("BOGUS",status);
423 if ( status != U_ILLEGAL_ARGUMENT_ERROR ) {
424 errcheckln(status, "Calling Region::getInstance(\"BOGUS\") should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
425 }
426
427
428 for (int32_t i = 0 ; i < UPRV_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
TestGetInstanceInt()450 void 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 errcheckln(status, "Calling Region::getInstance(-123) should have triggered an U_ILLEGAL_ARGUMENT_ERROR, but didn't. - %s", u_errorName(status));
470 }
471
472 for (int32_t i = 0 ; i < UPRV_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
TestGetContainedRegions()494 void RegionTest::TestGetContainedRegions() {
495 for (int32_t i = 0 ; i < UPRV_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(status);
505 if (U_FAILURE(status)) {
506 errln("%s->getContainedRegions(status) failed: %s", r->getRegionCode(), u_errorName(status));
507 continue;
508 }
509 for ( int32_t i = 0 ; i < containedRegions->count(status); i++ ) {
510 const char *crID = containedRegions->next(NULL,status);
511 const Region *cr = Region::getInstance(crID,status);
512 const Region *containingRegion = cr ? cr->getContainingRegion() : NULL;
513 if ( !containingRegion || *containingRegion != *r ) {
514 errln("Region: %s contains region %s. Expected containing region of this region to be the original region, but got %s",
515 r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
516 }
517 }
518 delete containedRegions;
519 } else {
520 dataerrln("Known region %s was not recognized.",rd.code);
521 }
522 }
523 }
524
TestGetContainedRegionsWithType()525 void RegionTest::TestGetContainedRegionsWithType() {
526 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
527 KnownRegion rd = knownRegions[i];
528 UErrorCode status = U_ZERO_ERROR;
529
530 const Region *r = Region::getInstance(rd.code,status);
531 if (r) {
532 if (r->getType() != URGN_CONTINENT) {
533 continue;
534 }
535 StringEnumeration *containedRegions = r->getContainedRegions(URGN_TERRITORY, status);
536 if (U_FAILURE(status)) {
537 errln("%s->getContainedRegions(URGN_TERRITORY, status) failed: %s", r->getRegionCode(), u_errorName(status));
538 continue;
539 }
540 for ( int32_t j = 0 ; j < containedRegions->count(status); j++ ) {
541 const char *crID = containedRegions->next(NULL,status);
542 const Region *cr = Region::getInstance(crID,status);
543 const Region *containingRegion = cr ? cr->getContainingRegion(URGN_CONTINENT) : NULL;
544 if ( !containingRegion || *containingRegion != *r ) {
545 errln("Continent: %s contains territory %s. Expected containing continent of this region to be the original region, but got %s",
546 r->getRegionCode(),cr->getRegionCode(),containingRegion?containingRegion->getRegionCode():"NULL");
547 }
548 }
549 delete containedRegions;
550 } else {
551 dataerrln("Known region %s was not recognized.",rd.code);
552 }
553 }
554 }
555
TestGetContainingRegion()556 void RegionTest::TestGetContainingRegion() {
557 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
558 KnownRegion rd = knownRegions[i];
559 UErrorCode status = U_ZERO_ERROR;
560 const Region *r = Region::getInstance(rd.code,status);
561 if (r) {
562 const Region *c = r->getContainingRegion();
563 if (rd.parent == NULL) {
564 if ( c ) {
565 errln("Containing region for %s should have been NULL. Got: %s",r->getRegionCode(),c->getRegionCode());
566 }
567 } else {
568 const Region *p = Region::getInstance(rd.parent,status);
569 if ( !c || *p != *c ) {
570 errln("Expected containing continent of region %s to be %s. Got: %s",
571 r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
572 }
573 }
574 } else {
575 dataerrln("Known region %s was not recognized.",rd.code);
576 }
577 }
578 }
579
TestGetContainingRegionWithType()580 void RegionTest::TestGetContainingRegionWithType() {
581 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
582 KnownRegion rd = knownRegions[i];
583 UErrorCode status = U_ZERO_ERROR;
584
585 const Region *r = Region::getInstance(rd.code,status);
586 if (r) {
587 const Region *c = r->getContainingRegion(URGN_CONTINENT);
588 if (rd.containingContinent == NULL) {
589 if ( c != NULL) {
590 errln("Containing continent for %s should have been NULL. Got: %s",r->getRegionCode(), c->getRegionCode());
591 }
592 } else {
593 const Region *p = Region::getInstance(rd.containingContinent,status);
594 if ( *p != *c ) {
595 errln("Expected containing continent of region %s to be %s. Got: %s",
596 r->getRegionCode(),p?p->getRegionCode():"NULL",c?c->getRegionCode():"NULL" );
597 }
598 }
599 } else {
600 dataerrln("Known region %s was not recognized.",rd.code);
601 }
602 }
603 }
604
TestGetPreferredValues()605 void RegionTest::TestGetPreferredValues() {
606 static const char *testData[6][17] = {
607 // Input ID, Expected Preferred Values...
608 { "AN", "CW", "SX", "BQ", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Netherlands Antilles
609 { "CS", "RS", "ME", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Serbia & Montenegro
610 { "FQ", "AQ", "TF", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // French Southern and Antarctic Territories
611 { "NT", "IQ", "SA", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, // Neutral Zone
612 { "PC", "FM", "MH", "MP", "PW", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, // Pacific Islands Trust Territory
613 { "SU", "RU", "AM", "AZ", "BY", "EE", "GE", "KZ", "KG", "LV", "LT", "MD", "TJ", "TM", "UA", "UZ" , NULL}, // Soviet Union
614 };
615
616 for ( int32_t i = 0 ; i < 6 ; i++ ) {
617 const char **data = testData[i];
618 UErrorCode status = U_ZERO_ERROR;
619 const Region *r = Region::getInstance(data[0],status);
620 if (r) {
621 StringEnumeration *preferredValues = r->getPreferredValues(status);
622 if (U_FAILURE(status)) {
623 errln("%s->getPreferredValues(status) failed: %s", r->getRegionCode(), u_errorName(status));
624 continue;
625 }
626 for ( int i = 1 ; data[i] ; i++ ) {
627 UBool found = FALSE;
628 preferredValues->reset(status);
629 while ( const char *check = preferredValues->next(NULL,status) ) {
630 if ( !uprv_strcmp(check,data[i]) ) {
631 found = TRUE;
632 break;
633 }
634 }
635 if ( !found ) {
636 errln("Region::getPreferredValues() for region \"%s\" should have contained \"%s\" but it didn't.",r->getRegionCode(),data[i]);
637 }
638 }
639 delete preferredValues;
640 } else {
641 dataerrln("Known region %s was not recognized.",data[0]);
642 }
643 }
644 }
645
TestContains()646 void RegionTest::TestContains() {
647 for (int32_t i = 0 ; i < UPRV_LENGTHOF(knownRegions) ; i++ ) {
648 KnownRegion rd = knownRegions[i];
649 UErrorCode status = U_ZERO_ERROR;
650
651 const Region *r = Region::getInstance(rd.code,status);
652 if (r) {
653 const Region *c = r->getContainingRegion();
654 while ( c ) {
655 if ( !c->contains(*r)) {
656 errln("Region \"%s\" should have contained \"%s\" but it didn't.",c->getRegionCode(),r->getRegionCode());
657 }
658 c = c->getContainingRegion();
659 }
660 } else {
661 dataerrln("Known region %s was not recognized.",rd.code);
662 }
663 }
664 }
665
TestAvailableTerritories()666 void RegionTest::TestAvailableTerritories() {
667 // Test to make sure that the set of territories contained in World and the set of all available
668 // territories are one and the same.
669 UErrorCode status = U_ZERO_ERROR;
670 StringEnumeration *availableTerritories = Region::getAvailable(URGN_TERRITORY, status);
671 if (U_FAILURE(status)) {
672 dataerrln("Region::getAvailable(URGN_TERRITORY,status) failed: %s", u_errorName(status));
673 return;
674 }
675 const Region *world = Region::getInstance("001",status);
676 if (U_FAILURE(status)) {
677 dataerrln("Region::getInstance(\"001\",status) failed: %s", u_errorName(status));
678 return;
679 }
680 StringEnumeration *containedInWorld = world->getContainedRegions(URGN_TERRITORY, status);
681 if (U_FAILURE(status)) {
682 errln("world->getContainedRegions(URGN_TERRITORY, status) failed: %s", u_errorName(status));
683 return;
684 }
685 if ( !availableTerritories || !containedInWorld || *availableTerritories != *containedInWorld ) {
686 char availableTerritoriesString[1024] = "";
687 char containedInWorldString[1024] = "";
688 if ( availableTerritories ) {
689 for (int32_t i = 0 ; i < availableTerritories->count(status) ; i++ ) {
690 if ( i > 0 ) {
691 uprv_strcat(availableTerritoriesString," ");
692 }
693 uprv_strcat(availableTerritoriesString,availableTerritories->next(NULL,status));
694 }
695 } else {
696 uprv_strcpy(availableTerritoriesString,"NULL");
697 }
698 if ( containedInWorld ) {
699 for (int32_t i = 0 ; i < containedInWorld->count(status) ; i++ ) {
700 if ( i > 0 ) {
701 uprv_strcat(containedInWorldString," ");
702 }
703 uprv_strcat(containedInWorldString,containedInWorld->next(NULL,status));
704 }
705 } else {
706 uprv_strcpy(containedInWorldString,"NULL");
707 }
708 errln("Available territories and all territories contained in world should be the same set.\nAvailable = %s\nContained in World = %s",
709 availableTerritoriesString,containedInWorldString);
710 }
711 delete availableTerritories;
712 delete containedInWorld;
713 }
714
TestNoContainedRegions(void)715 void RegionTest::TestNoContainedRegions(void) {
716 UErrorCode status = U_ZERO_ERROR;
717 const Region *region = Region::getInstance("BM",status);
718 if (U_FAILURE(status) || region == NULL) {
719 dataerrln("Fail called to Region::getInstance(\"BM\", status) - %s", u_errorName(status));
720 return;
721 }
722 StringEnumeration *containedRegions = region->getContainedRegions(status);
723 if (U_FAILURE(status)) {
724 errln("%s->getContainedRegions(status) failed: %s", region->getRegionCode(), u_errorName(status));
725 return;
726 }
727 const char *emptyStr = containedRegions->next(NULL, status);
728 if (U_FAILURE(status)||(emptyStr!=NULL)) {
729 errln("Error, 'BM' should have no subregions, but returned str=%p, err=%s\n", emptyStr, u_errorName(status));
730 } else {
731 logln("Success - BM has no subregions\n");
732 }
733 delete containedRegions;
734 }
735
736 #endif /* #if !UCONFIG_NO_FORMATTING */
737
738 //eof
739