1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2014-2016, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST FOR UREGION */
8 /***********************************************************************
9 * Test cases ported from ICU4J ( RegionTest.java )
10 * to C++ (regiontst.cpp) to here.
11 * Try to keep them in sync if at all possible...!
12 ***********************************************************************/
13
14 #include "unicode/utypes.h"
15
16 #if !UCONFIG_NO_FORMATTING
17
18 #include <stdbool.h>
19
20 #include "unicode/ustring.h"
21 #include "unicode/uregion.h"
22 #include "unicode/uenum.h"
23 #include "cintltst.h"
24 #include "cmemory.h"
25 #include "cstring.h"
26
27 static void TestKnownRegions(void);
28 static void TestGetContainedRegions(void);
29 static void TestGroupingChildren(void);
30 static void TestGetContainedRegionsWithType(void);
31 static void TestGetContainingRegion(void);
32 static void TestGetContainingRegionWithType(void);
33 static void TestGetPreferredValues(void);
34 static void TestContains(void);
35
36 void addURegionTest(TestNode** root);
37
38 #define TESTCASE(x) addTest(root, &x, "tsformat/uregiontest/" #x)
39
addURegionTest(TestNode ** root)40 void addURegionTest(TestNode** root)
41 {
42 TESTCASE(TestKnownRegions);
43 TESTCASE(TestGetContainedRegions);
44 TESTCASE(TestGroupingChildren);
45 TESTCASE(TestGetContainedRegionsWithType);
46 TESTCASE(TestGetContainingRegion);
47 TESTCASE(TestGetContainingRegionWithType);
48 TESTCASE(TestGetPreferredValues);
49 TESTCASE(TestContains);
50 }
51
52 typedef struct KnownRegion {
53 const char *code;
54 int32_t numeric;
55 const char *parent;
56 URegionType type;
57 const char *containingContinent;
58 } KnownRegion;
59
60 static KnownRegion knownRegions[] = {
61 // Code, Num, Parent, Type, Containing Continent
62 { "TP" , 626, "035", URGN_TERRITORY, "142" },
63 { "001", 1, NULL , URGN_WORLD, NULL },
64 { "002", 2, "001", URGN_CONTINENT, NULL },
65 { "003", 3, NULL, URGN_GROUPING, NULL },
66 { "005", 5, "019", URGN_SUBCONTINENT, "019" },
67 { "009", 9, "001", URGN_CONTINENT, NULL},
68 { "011", 11, "002", URGN_SUBCONTINENT, "002" },
69 { "013", 13, "019", URGN_SUBCONTINENT, "019" },
70 { "014", 14, "002", URGN_SUBCONTINENT, "002" },
71 { "015", 15, "002", URGN_SUBCONTINENT, "002" },
72 { "017", 17, "002", URGN_SUBCONTINENT, "002" },
73 { "018", 18, "002", URGN_SUBCONTINENT, "002" },
74 { "019", 19, "001", URGN_CONTINENT, NULL },
75 { "021", 21, "019", URGN_SUBCONTINENT, "019" },
76 { "029", 29, "019", URGN_SUBCONTINENT, "019" },
77 { "030", 30, "142", URGN_SUBCONTINENT, "142" },
78 { "034", 34, "142", URGN_SUBCONTINENT, "142" },
79 { "035", 35, "142", URGN_SUBCONTINENT, "142" },
80 { "039", 39, "150", URGN_SUBCONTINENT, "150"},
81 { "053", 53, "009", URGN_SUBCONTINENT, "009" },
82 { "054", 54, "009", URGN_SUBCONTINENT, "009" },
83 { "057", 57, "009", URGN_SUBCONTINENT, "009" },
84 { "061", 61, "009", URGN_SUBCONTINENT, "009" },
85 { "142", 142, "001", URGN_CONTINENT, NULL },
86 { "143", 143, "142", URGN_SUBCONTINENT, "142" },
87 { "145", 145, "142", URGN_SUBCONTINENT, "142" },
88 { "150", 150, "001", URGN_CONTINENT, NULL },
89 { "151", 151, "150", URGN_SUBCONTINENT, "150" },
90 { "154", 154, "150", URGN_SUBCONTINENT, "150" },
91 { "155", 155, "150", URGN_SUBCONTINENT, "150" },
92 { "419", 419, NULL, URGN_GROUPING , NULL},
93 { "AC" , -1, "QO" , URGN_TERRITORY, "009" },
94 { "AD" , 20, "039", URGN_TERRITORY, "150" },
95 { "AE" , 784, "145", URGN_TERRITORY, "142" },
96 { "AF" , 4, "034", URGN_TERRITORY, "142" },
97 { "AG" , 28, "029", URGN_TERRITORY, "019" },
98 { "AI" , 660, "029", URGN_TERRITORY, "019" },
99 { "AL" , 8, "039", URGN_TERRITORY, "150" },
100 { "AM" , 51, "145", URGN_TERRITORY, "142" },
101 { "AN" , 530, NULL, URGN_DEPRECATED, NULL },
102 { "AO" , 24, "017", URGN_TERRITORY, "002" },
103 { "AQ" , 10, "QO" , URGN_TERRITORY, "009" },
104 { "AR" , 32, "005", URGN_TERRITORY, "019" },
105 { "AS" , 16, "061", URGN_TERRITORY, "009" },
106 { "AT" , 40, "155", URGN_TERRITORY, "150" },
107 { "AU" , 36, "053", URGN_TERRITORY, "009" },
108 { "AW" , 533, "029", URGN_TERRITORY, "019" },
109 { "AX" , 248, "154", URGN_TERRITORY, "150" },
110 { "AZ" , 31, "145", URGN_TERRITORY, "142" },
111 { "BA" , 70, "039", URGN_TERRITORY, "150" },
112 { "BB" , 52, "029", URGN_TERRITORY, "019" },
113 { "BD" , 50, "034", URGN_TERRITORY, "142" },
114 { "BE" , 56, "155", URGN_TERRITORY, "150" },
115 { "BF" , 854, "011", URGN_TERRITORY, "002" },
116 { "BG" , 100, "151", URGN_TERRITORY, "150" },
117 { "BH" , 48, "145", URGN_TERRITORY, "142" },
118 { "BI" , 108, "014", URGN_TERRITORY, "002" },
119 { "BJ" , 204, "011", URGN_TERRITORY, "002" },
120 { "BL" , 652, "029", URGN_TERRITORY, "019" },
121 { "BM" , 60, "021", URGN_TERRITORY, "019" },
122 { "BN" , 96, "035", URGN_TERRITORY, "142" },
123 { "BO" , 68, "005", URGN_TERRITORY, "019" },
124 { "BQ" , 535, "029", URGN_TERRITORY, "019" },
125 { "BR" , 76, "005", URGN_TERRITORY, "019" },
126 { "BS" , 44, "029", URGN_TERRITORY, "019" },
127 { "BT" , 64, "034", URGN_TERRITORY, "142" },
128 { "BU" , 104, "035", URGN_TERRITORY, "142" },
129 { "BV" , 74, "005", URGN_TERRITORY, "019" },
130 { "BW" , 72, "018", URGN_TERRITORY, "002" },
131 { "BY" , 112, "151", URGN_TERRITORY, "150" },
132 { "BZ" , 84, "013", URGN_TERRITORY, "019" },
133 { "CA" , 124, "021", URGN_TERRITORY, "019" },
134 { "CC" , 166, "053", URGN_TERRITORY, "009" },
135 { "CD" , 180, "017", URGN_TERRITORY, "002" },
136 { "CF" , 140, "017", URGN_TERRITORY, "002" },
137 { "CG" , 178, "017", URGN_TERRITORY, "002" },
138 { "CH" , 756, "155", URGN_TERRITORY, "150" },
139 { "CI" , 384, "011", URGN_TERRITORY, "002" },
140 { "CK" , 184, "061", URGN_TERRITORY, "009" },
141 { "CL" , 152, "005", URGN_TERRITORY, "019" },
142 { "CM" , 120, "017", URGN_TERRITORY, "002" },
143 { "CN" , 156, "030", URGN_TERRITORY, "142" },
144 { "CO" , 170, "005", URGN_TERRITORY, "019" },
145 { "CP" , -1 , "QO" , URGN_TERRITORY, "009" },
146 { "CR" , 188, "013", URGN_TERRITORY, "019" },
147 { "CU" , 192, "029", URGN_TERRITORY, "019" },
148 { "CV" , 132, "011", URGN_TERRITORY, "002" },
149 { "CW" , 531, "029", URGN_TERRITORY, "019" },
150 { "CX" , 162, "053", URGN_TERRITORY, "009" },
151 { "CY" , 196, "145", URGN_TERRITORY, "142" },
152 { "CZ" , 203, "151", URGN_TERRITORY, "150" },
153 { "DD" , 276, "155", URGN_TERRITORY, "150" },
154 { "DE" , 276, "155", URGN_TERRITORY, "150" },
155 { "DG" , -1 , "QO" , URGN_TERRITORY, "009" },
156 { "DJ" , 262, "014", URGN_TERRITORY, "002" },
157 { "DK" , 208, "154", URGN_TERRITORY, "150" },
158 { "DM" , 212, "029", URGN_TERRITORY, "019" },
159 { "DO" , 214, "029", URGN_TERRITORY, "019" },
160 { "DZ" , 12, "015", URGN_TERRITORY, "002" },
161 { "EA" , -1, "015", URGN_TERRITORY, "002" },
162 { "EC" , 218, "005", URGN_TERRITORY, "019" },
163 { "EE" , 233, "154", URGN_TERRITORY, "150" },
164 { "EG" , 818, "015", URGN_TERRITORY, "002" },
165 { "EH" , 732, "015", URGN_TERRITORY, "002" },
166 { "ER" , 232, "014", URGN_TERRITORY, "002" },
167 { "ES" , 724, "039", URGN_TERRITORY, "150" },
168 { "ET" , 231, "014", URGN_TERRITORY, "002" },
169 { "EU" , 967, NULL, URGN_GROUPING, NULL },
170 { "FI" , 246, "154", URGN_TERRITORY, "150" },
171 { "FJ" , 242, "054", URGN_TERRITORY, "009" },
172 { "FK" , 238, "005", URGN_TERRITORY, "019" },
173 { "FM" , 583, "057", URGN_TERRITORY, "009" },
174 { "FO" , 234, "154", URGN_TERRITORY, "150" },
175 { "FR" , 250, "155", URGN_TERRITORY, "150" },
176 { "FX" , 250, "155", URGN_TERRITORY, "150" },
177 { "GA" , 266, "017", URGN_TERRITORY, "002" },
178 { "GB" , 826, "154", URGN_TERRITORY, "150" },
179 { "GD" , 308, "029", URGN_TERRITORY, "019" },
180 { "GE" , 268, "145", URGN_TERRITORY, "142" },
181 { "GF" , 254, "005", URGN_TERRITORY, "019" },
182 { "GG" , 831, "154", URGN_TERRITORY, "150" },
183 { "GH" , 288, "011", URGN_TERRITORY, "002" },
184 { "GI" , 292, "039", URGN_TERRITORY, "150" },
185 { "GL" , 304, "021", URGN_TERRITORY, "019" },
186 { "GM" , 270, "011", URGN_TERRITORY, "002" },
187 { "GN" , 324, "011", URGN_TERRITORY, "002" },
188 { "GP" , 312, "029", URGN_TERRITORY, "019" },
189 { "GQ" , 226, "017", URGN_TERRITORY, "002" },
190 { "GR" , 300, "039", URGN_TERRITORY, "150" },
191 { "GS" , 239, "005", URGN_TERRITORY, "019" },
192 { "GT" , 320, "013", URGN_TERRITORY, "019" },
193 { "GU" , 316, "057", URGN_TERRITORY, "009" },
194 { "GW" , 624, "011", URGN_TERRITORY, "002" },
195 { "GY" , 328, "005", URGN_TERRITORY, "019" },
196 { "HK" , 344, "030", URGN_TERRITORY, "142" },
197 { "HM" , 334, "053", URGN_TERRITORY, "009" },
198 { "HN" , 340, "013", URGN_TERRITORY, "019" },
199 { "HR" , 191, "039", URGN_TERRITORY, "150" },
200 { "HT" , 332, "029", URGN_TERRITORY, "019" },
201 { "HU" , 348, "151", URGN_TERRITORY, "150" },
202 { "IC" , -1, "015", URGN_TERRITORY, "002" },
203 { "ID" , 360, "035", URGN_TERRITORY, "142" },
204 { "IE" , 372, "154", URGN_TERRITORY, "150" },
205 { "IL" , 376, "145", URGN_TERRITORY, "142" },
206 { "IM" , 833, "154", URGN_TERRITORY, "150" },
207 { "IN" , 356, "034", URGN_TERRITORY, "142" },
208 { "IO" , 86, "014", URGN_TERRITORY, "002" },
209 { "IQ" , 368, "145", URGN_TERRITORY, "142" },
210 { "IR" , 364, "034", URGN_TERRITORY, "142" },
211 { "IS" , 352, "154", URGN_TERRITORY, "150" },
212 { "IT" , 380, "039", URGN_TERRITORY, "150" },
213 { "JE" , 832, "154", URGN_TERRITORY, "150" },
214 { "JM" , 388, "029", URGN_TERRITORY, "019" },
215 { "JO" , 400, "145", URGN_TERRITORY, "142" },
216 { "JP" , 392, "030", URGN_TERRITORY, "142" },
217 { "KE" , 404, "014", URGN_TERRITORY, "002" },
218 { "KG" , 417, "143", URGN_TERRITORY, "142" },
219 { "KH" , 116, "035", URGN_TERRITORY, "142" },
220 { "KI" , 296, "057", URGN_TERRITORY, "009" },
221 { "KM" , 174, "014", URGN_TERRITORY, "002" },
222 { "KN" , 659, "029", URGN_TERRITORY, "019" },
223 { "KP" , 408, "030", URGN_TERRITORY, "142" },
224 { "KR" , 410, "030", URGN_TERRITORY, "142" },
225 { "KW" , 414, "145", URGN_TERRITORY, "142" },
226 { "KY" , 136, "029", URGN_TERRITORY, "019" },
227 { "KZ" , 398, "143", URGN_TERRITORY, "142" },
228 { "LA" , 418, "035", URGN_TERRITORY, "142" },
229 { "LB" , 422, "145", URGN_TERRITORY, "142" },
230 { "LC" , 662, "029", URGN_TERRITORY, "019" },
231 { "LI" , 438, "155", URGN_TERRITORY, "150" },
232 { "LK" , 144, "034", URGN_TERRITORY, "142" },
233 { "LR" , 430, "011", URGN_TERRITORY, "002" },
234 { "LS" , 426, "018", URGN_TERRITORY, "002" },
235 { "LT" , 440, "154", URGN_TERRITORY, "150" },
236 { "LU" , 442, "155", URGN_TERRITORY, "150" },
237 { "LV" , 428, "154", URGN_TERRITORY, "150" },
238 { "LY" , 434, "015", URGN_TERRITORY, "002" },
239 { "MA" , 504, "015", URGN_TERRITORY, "002" },
240 { "MC" , 492, "155", URGN_TERRITORY, "150" },
241 { "MD" , 498, "151", URGN_TERRITORY, "150" },
242 { "ME" , 499, "039", URGN_TERRITORY, "150" },
243 { "MF" , 663, "029", URGN_TERRITORY, "019" },
244 { "MG" , 450, "014", URGN_TERRITORY, "002" },
245 { "MH" , 584, "057", URGN_TERRITORY, "009" },
246 { "MK" , 807, "039", URGN_TERRITORY, "150" },
247 { "ML" , 466, "011", URGN_TERRITORY, "002" },
248 { "MM" , 104, "035", URGN_TERRITORY, "142" },
249 { "MN" , 496, "030", URGN_TERRITORY, "142" },
250 { "MO" , 446, "030", URGN_TERRITORY, "142" },
251 { "MP" , 580, "057", URGN_TERRITORY, "009" },
252 { "MQ" , 474, "029", URGN_TERRITORY, "019" },
253 { "MR" , 478, "011", URGN_TERRITORY, "002" },
254 { "MS" , 500, "029", URGN_TERRITORY, "019" },
255 { "MT" , 470, "039", URGN_TERRITORY, "150" },
256 { "MU" , 480, "014", URGN_TERRITORY, "002" },
257 { "MV" , 462, "034", URGN_TERRITORY, "142" },
258 { "MW" , 454, "014", URGN_TERRITORY, "002" },
259 { "MX" , 484, "013", URGN_TERRITORY, "019"},
260 { "MY" , 458, "035", URGN_TERRITORY, "142" },
261 { "MZ" , 508, "014", URGN_TERRITORY, "002" },
262 { "NA" , 516, "018", URGN_TERRITORY, "002" },
263 { "NC" , 540, "054", URGN_TERRITORY, "009" },
264 { "NE" , 562, "011", URGN_TERRITORY, "002" },
265 { "NF" , 574, "053", URGN_TERRITORY, "009" },
266 { "NG" , 566, "011", URGN_TERRITORY, "002" },
267 { "NI" , 558, "013", URGN_TERRITORY, "019" },
268 { "NL" , 528, "155", URGN_TERRITORY, "150" },
269 { "NO" , 578, "154", URGN_TERRITORY, "150" },
270 { "NP" , 524, "034", URGN_TERRITORY, "142" },
271 { "NR" , 520, "057", URGN_TERRITORY, "009" },
272 { "NT" , 536, NULL , URGN_DEPRECATED, NULL },
273 { "NU" , 570, "061", URGN_TERRITORY, "009" },
274 { "NZ" , 554, "053", URGN_TERRITORY, "009" },
275 { "OM" , 512, "145", URGN_TERRITORY, "142" },
276 { "PA" , 591, "013", URGN_TERRITORY, "019" },
277 { "PE" , 604, "005", URGN_TERRITORY, "019" },
278 { "PF" , 258, "061", URGN_TERRITORY, "009" },
279 { "PG" , 598, "054", URGN_TERRITORY, "009" },
280 { "PH" , 608, "035", URGN_TERRITORY, "142" },
281 { "PK" , 586, "034", URGN_TERRITORY, "142" },
282 { "PL" , 616, "151", URGN_TERRITORY, "150" },
283 { "PM" , 666, "021", URGN_TERRITORY, "019" },
284 { "PN" , 612, "061", URGN_TERRITORY, "009" },
285 { "PR" , 630, "029", URGN_TERRITORY, "019" },
286 { "PS" , 275, "145", URGN_TERRITORY, "142" },
287 { "PT" , 620, "039", URGN_TERRITORY, "150" },
288 { "PW" , 585, "057", URGN_TERRITORY, "009" },
289 { "PY" , 600, "005", URGN_TERRITORY, "019" },
290 { "QA" , 634, "145", URGN_TERRITORY, "142" },
291 { "QO" , 961, "009", URGN_SUBCONTINENT, "009" },
292 { "QU" , 967, NULL, URGN_GROUPING, NULL },
293 { "RE" , 638, "014", URGN_TERRITORY, "002" },
294 { "RO" , 642, "151", URGN_TERRITORY, "150" },
295 { "RS" , 688, "039", URGN_TERRITORY, "150" },
296 { "RU" , 643, "151", URGN_TERRITORY, "150" },
297 { "RW" , 646, "014", URGN_TERRITORY, "002" },
298 { "SA" , 682, "145", URGN_TERRITORY, "142" },
299 { "SB" , 90, "054", URGN_TERRITORY, "009" },
300 { "SC" , 690, "014", URGN_TERRITORY, "002" },
301 { "SD" , 729, "015", URGN_TERRITORY, "002" },
302 { "SE" , 752, "154", URGN_TERRITORY, "150" },
303 { "SG" , 702, "035", URGN_TERRITORY, "142" },
304 { "SH" , 654, "011", URGN_TERRITORY, "002" },
305 { "SI" , 705, "039", URGN_TERRITORY, "150" },
306 { "SJ" , 744, "154", URGN_TERRITORY, "150" },
307 { "SK" , 703, "151", URGN_TERRITORY, "150" },
308 { "SL" , 694, "011", URGN_TERRITORY, "002" },
309 { "SM" , 674, "039", URGN_TERRITORY, "150" },
310 { "SN" , 686, "011", URGN_TERRITORY, "002" },
311 { "SO" , 706, "014", URGN_TERRITORY, "002" },
312 { "SR" , 740, "005", URGN_TERRITORY, "019" },
313 { "SS" , 728, "014", URGN_TERRITORY, "002" },
314 { "ST" , 678, "017", URGN_TERRITORY, "002" },
315 { "SU" , 810, NULL , URGN_DEPRECATED , NULL},
316 { "SV" , 222, "013", URGN_TERRITORY, "019" },
317 { "SX" , 534, "029", URGN_TERRITORY, "019" },
318 { "SY" , 760, "145", URGN_TERRITORY, "142" },
319 { "SZ" , 748, "018", URGN_TERRITORY, "002" },
320 { "TA" , -1, "QO", URGN_TERRITORY, "009" },
321 { "TC" , 796, "029", URGN_TERRITORY, "019" },
322 { "TD" , 148, "017", URGN_TERRITORY, "002" },
323 { "TF" , 260, "014", URGN_TERRITORY, "002" },
324 { "TG" , 768, "011", URGN_TERRITORY, "002" },
325 { "TH" , 764, "035", URGN_TERRITORY, "142" },
326 { "TJ" , 762, "143", URGN_TERRITORY, "142" },
327 { "TK" , 772, "061", URGN_TERRITORY, "009" },
328 { "TL" , 626, "035", URGN_TERRITORY, "142" },
329 { "TM" , 795, "143", URGN_TERRITORY, "142" },
330 { "TN" , 788, "015", URGN_TERRITORY, "002" },
331 { "TO" , 776, "061", URGN_TERRITORY, "009" },
332 { "TP" , 626, "035", URGN_TERRITORY, "142" },
333 { "TR" , 792, "145", URGN_TERRITORY, "142" },
334 { "TT" , 780, "029", URGN_TERRITORY, "019" },
335 { "TV" , 798, "061", URGN_TERRITORY, "009" },
336 { "TW" , 158, "030", URGN_TERRITORY, "142" },
337 { "TZ" , 834, "014", URGN_TERRITORY, "002" },
338 { "UA" , 804, "151", URGN_TERRITORY, "150" },
339 { "UG" , 800, "014", URGN_TERRITORY, "002" },
340 { "UM" , 581, "057", URGN_TERRITORY, "009" },
341 { "US" , 840, "021", URGN_TERRITORY, "019" },
342 { "UY" , 858, "005", URGN_TERRITORY, "019" },
343 { "UZ" , 860, "143", URGN_TERRITORY, "142" },
344 { "VA" , 336, "039", URGN_TERRITORY, "150" },
345 { "VC" , 670, "029", URGN_TERRITORY, "019" },
346 { "VE" , 862, "005", URGN_TERRITORY, "019" },
347 { "VG" , 92, "029", URGN_TERRITORY, "019" },
348 { "VI" , 850, "029", URGN_TERRITORY, "019" },
349 { "VN" , 704, "035", URGN_TERRITORY, "142" },
350 { "VU" , 548, "054", URGN_TERRITORY, "009" },
351 { "WF" , 876, "061", URGN_TERRITORY, "009" },
352 { "WS" , 882, "061", URGN_TERRITORY, "009" },
353 { "YD" , 887, "145", URGN_TERRITORY, "142" },
354 { "YE" , 887, "145", URGN_TERRITORY, "142" },
355 { "YT" , 175, "014", URGN_TERRITORY, "002" },
356 { "ZA" , 710, "018", URGN_TERRITORY, "002" },
357 { "ZM" , 894, "014", URGN_TERRITORY, "002" },
358 { "ZR" , 180, "017", URGN_TERRITORY, "002" },
359 { "ZW" , 716, "014", URGN_TERRITORY, "002" },
360 { "ZZ" , 999, NULL , URGN_UNKNOWN, NULL },
361 { NULL , 0, NULL , URGN_UNKNOWN, NULL } /* terminator */
362 };
363
364
TestKnownRegions()365 static void TestKnownRegions() {
366 const KnownRegion * rd;
367 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
368 UErrorCode status = U_ZERO_ERROR;
369 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
370 if ( U_SUCCESS(status) ) {
371 int32_t n = uregion_getNumericCode(r);
372 int32_t e = rd->numeric;
373 if ( n != e ) {
374 log_err("ERROR: Numeric code mismatch for region %s. Expected:%d Got:%d\n", uregion_getRegionCode(r), e, n );
375 }
376 if (uregion_getType(r) != rd->type) {
377 log_err("ERROR: Expected region %s to be of type %d. Got: %d\n", uregion_getRegionCode(r), rd->type, uregion_getType(r) );
378 }
379 if ( e > 0 ) {
380 const URegion *ncRegion = uregion_getRegionFromNumericCode(e, &status);
381 if ( !uregion_areEqual(ncRegion, r) && e != 891 ) { // 891 is special case - CS and YU both deprecated codes for region 891
382 log_err("ERROR: Creating region %s by its numeric code returned a different region. Got: %s instead.\n",
383 uregion_getRegionCode(r), uregion_getRegionCode(ncRegion) );
384 }
385 }
386 } else {
387 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
388 }
389 }
390 }
391
TestGetContainedRegions()392 static void TestGetContainedRegions() {
393 const KnownRegion * rd;
394 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
395 UErrorCode status = U_ZERO_ERROR;
396 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
397 if ( U_SUCCESS(status) ) {
398 UEnumeration *containedRegions;
399 const char *crID;
400 if (uregion_getType(r) == URGN_GROUPING) {
401 continue;
402 }
403 containedRegions = uregion_getContainedRegions(r, &status);
404 if (containedRegions != NULL) {
405 while ((crID = uenum_next(containedRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
406 const URegion *cr = uregion_getRegionFromCode(crID, &status);
407 const URegion *containingRegion = (cr)? uregion_getContainingRegion(cr) : NULL;
408 if ( !containingRegion || !uregion_areEqual(containingRegion, r) ) {
409 log_err("ERROR: Region: %s contains region %s. Expected containing region of this region to be the original region, but got %s\n",
410 uregion_getRegionCode(r), uregion_getRegionCode(cr), (containingRegion)?uregion_getRegionCode(containingRegion):"NULL" );
411 }
412 }
413 uenum_close(containedRegions);
414 }
415 } else {
416 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
417 }
418 }
419 }
420
TestGroupingChildren()421 static void TestGroupingChildren() {
422 const char* testGroupings[] = {
423 "003", "021,013,029",
424 "419", "013,029,005",
425 "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"
426 };
427
428 for (int32_t i = 0; i < UPRV_LENGTHOF(testGroupings); i += 2) {
429 const char* groupingCode = testGroupings[i];
430 const char* expectedChildren = testGroupings[i + 1];
431
432 UErrorCode err = U_ZERO_ERROR;
433 const URegion* grouping = uregion_getRegionFromCode(groupingCode, &err);
434 if (U_SUCCESS(err)) {
435 UEnumeration* actualChildren = uregion_getContainedRegions(grouping, &err);
436 if (U_SUCCESS(err)) {
437 int32_t numActualChildren = uenum_count(actualChildren, &err);
438 int32_t numExpectedChildren = 0;
439 const char* expectedChildStart = expectedChildren;
440 const char* expectedChildEnd = NULL;
441 const char* actualChild = NULL;
442 while ((actualChild = uenum_next(actualChildren, NULL, &err)) != NULL && *expectedChildStart != '\0') {
443 expectedChildEnd = uprv_strchr(expectedChildStart, ',');
444 if (expectedChildEnd == NULL) {
445 expectedChildEnd = expectedChildStart + uprv_strlen(expectedChildStart);
446 }
447 if (uprv_strlen(actualChild) != (size_t)(expectedChildEnd - expectedChildStart) || uprv_strncmp(actualChild, expectedChildStart, expectedChildEnd - expectedChildStart) != 0) {
448 log_err("Mismatch in child list for %s at position %d: expected %s, got %s\n", groupingCode, i, expectedChildStart, actualChild);
449 }
450 expectedChildStart = (*expectedChildEnd != '\0') ? expectedChildEnd + 1 : expectedChildEnd;
451 ++numExpectedChildren;
452 }
453 if (expectedChildEnd == NULL) {
454 expectedChildEnd = expectedChildren;
455 }
456 while (expectedChildEnd != NULL && *expectedChildEnd != '\0') {
457 expectedChildEnd = uprv_strchr(expectedChildEnd + 1, ',');
458 ++numExpectedChildren;
459 }
460 if (numExpectedChildren != numActualChildren) {
461 log_err("Wrong number of children for %s: expected %d, got %d\n", groupingCode, numExpectedChildren, numActualChildren);
462 }
463 uenum_close(actualChildren);
464 } else {
465 log_err("Couldn't create iterator for children of %s\n", groupingCode);
466 }
467 } else {
468 log_err("Region %s not found\n", groupingCode);
469 }
470 }
471 }
472
TestGetContainedRegionsWithType()473 static void TestGetContainedRegionsWithType() {
474 const KnownRegion * rd;
475 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
476 UErrorCode status = U_ZERO_ERROR;
477 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
478 if ( U_SUCCESS(status) ) {
479 UEnumeration *containedRegions;
480 const char *crID;
481 if (uregion_getType(r) != URGN_CONTINENT) {
482 continue;
483 }
484 containedRegions = uregion_getContainedRegionsOfType(r, URGN_TERRITORY, &status);
485 if (containedRegions != NULL) {
486 while ((crID = uenum_next(containedRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
487 const URegion *cr = uregion_getRegionFromCode(crID, &status);
488 const URegion *containingRegion = (cr)? uregion_getContainingRegionOfType(cr, URGN_CONTINENT) : NULL;
489 if ( !containingRegion || !uregion_areEqual(containingRegion, r) ) {
490 log_err("ERROR: Continent: %s contains territory %s. Expected containing continent of this region to be the original region, but got %s\n",
491 uregion_getRegionCode(r), uregion_getRegionCode(cr), (containingRegion)?uregion_getRegionCode(containingRegion):"NULL" );
492 }
493 }
494 uenum_close(containedRegions);
495 }
496 } else {
497 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
498 }
499 }
500 }
501
TestGetContainingRegion()502 static void TestGetContainingRegion() {
503 const KnownRegion * rd;
504 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
505 UErrorCode status = U_ZERO_ERROR;
506 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
507 if ( U_SUCCESS(status) ) {
508 const URegion *c = uregion_getContainingRegion(r);
509 if (rd->parent == NULL) {
510 if ( c ) {
511 log_err("ERROR: Containing region for %s should have been NULL. Got: %s\n", uregion_getRegionCode(r), uregion_getRegionCode(c) );
512 }
513 } else {
514 const URegion *p = uregion_getRegionFromCode(rd->parent, &status);
515 if ( c == NULL || !uregion_areEqual(p, c) ) {
516 log_err("ERROR: Expected containing continent of region %s to be %s. Got: %s\n",
517 uregion_getRegionCode(r), (p)?uregion_getRegionCode(p):"NULL", (c)?uregion_getRegionCode(c):"NULL" );
518 }
519 }
520 } else {
521 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
522 }
523 }
524 }
525
TestGetContainingRegionWithType()526 static void TestGetContainingRegionWithType() {
527 const KnownRegion * rd;
528 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
529 UErrorCode status = U_ZERO_ERROR;
530 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
531 if ( U_SUCCESS(status) ) {
532 const URegion *c = uregion_getContainingRegionOfType(r, URGN_CONTINENT);
533 if (rd->containingContinent == NULL) {
534 if ( c != NULL) {
535 log_err("ERROR: Containing continent for %s should have been NULL. Got: %s\n", uregion_getRegionCode(r), uregion_getRegionCode(c) );
536 }
537 } else {
538 const URegion *p = uregion_getRegionFromCode(rd->containingContinent, &status);
539 if ( !uregion_areEqual(p, c) ) {
540 log_err("ERROR: Expected containing continent of region %s to be %s. Got: %s\n",
541 uregion_getRegionCode(r), (p)?uregion_getRegionCode(p):"NULL", (c)?uregion_getRegionCode(c):"NULL" );
542 }
543 }
544 } else {
545 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
546 }
547 }
548 }
549
550 static const char * expectPrefRegions0[] = { "AN","CW","SX","BQ",NULL }; /* Netherlands Antilles */
551 static const char * expectPrefRegions1[] = { "CS","RS","ME",NULL }; /* Serbia & Montenegro */
552 static const char * expectPrefRegions2[] = { "FQ","AQ","TF",NULL }; /* French Southern and Antarctic Territories */
553 static const char * expectPrefRegions3[] = { "NT","IQ","SA",NULL }; /* Neutral Zone */
554 static const char * expectPrefRegions4[] = { "PC","FM","MH","MP","PW",NULL }; /* Pacific Islands Trust Territory */
555 static const char * expectPrefRegions5[] = { "SU","RU","AM","AZ","BY","EE","GE","KZ","KG","LV","LT","MD","TJ","TM","UA","UZ",NULL }; /* Soviet Union */
556 static const char ** expectPrefRegionsTestData[] = {
557 expectPrefRegions0,
558 expectPrefRegions1,
559 expectPrefRegions2,
560 expectPrefRegions3,
561 expectPrefRegions4,
562 expectPrefRegions5,
563 NULL
564 };
565
TestGetPreferredValues()566 static void TestGetPreferredValues() {
567 const char *** testDataPtr = expectPrefRegionsTestData;
568 const char ** regionListPtr;
569 while ( (regionListPtr = *testDataPtr++) != NULL ) {
570 UErrorCode status = U_ZERO_ERROR;
571 const char * deprecatedCode = *regionListPtr++;
572 const URegion *r = uregion_getRegionFromCode(deprecatedCode, &status);
573 if ( U_SUCCESS(status) ) {
574 UEnumeration *preferredRegions = uregion_getPreferredValues(r, &status);
575 if ( U_SUCCESS(status) ) {
576 if (preferredRegions != NULL) {
577 const char * preferredCode;
578 while ( (preferredCode = *regionListPtr++) != NULL ) {
579 const char *check;
580 UBool found = false;
581 uenum_reset(preferredRegions, &status);
582 while ((check = uenum_next(preferredRegions, NULL, &status)) != NULL && U_SUCCESS(status) ) {
583 if ( !uprv_strcmp(check,preferredCode) ) {
584 found = true;
585 break;
586 }
587 }
588 if ( !found ) {
589 log_err("ERROR: uregion_getPreferredValues for region \"%s\" should have contained \"%s\" but it didn't.\n", uregion_getRegionCode(r), preferredCode);
590 }
591 }
592 uenum_close(preferredRegions);
593 }
594 } else {
595 log_err("ERROR: uregion_getPreferredValues failed for region %s.\n", uregion_getRegionCode(r));
596 }
597 } else {
598 log_data_err("ERROR: Known region %s was not recognized.\n", deprecatedCode);
599 }
600 }
601 }
602
TestContains()603 static void TestContains() {
604 const KnownRegion * rd;
605 for (rd = knownRegions; rd->code != NULL ; rd++ ) {
606 UErrorCode status = U_ZERO_ERROR;
607 const URegion *r = uregion_getRegionFromCode(rd->code, &status);
608 if ( U_SUCCESS(status) ) {
609 const URegion *c = uregion_getContainingRegion(r);
610 while (c != NULL) {
611 if ( !uregion_contains(c, r) ) {
612 log_err("ERROR: Region \"%s\" should have contained \"%s\" but it didn't.\n", uregion_getRegionCode(c), uregion_getRegionCode(r) );
613 }
614 c = uregion_getContainingRegion(c);
615 }
616 } else {
617 log_data_err("ERROR: Known region %s was not recognized.\n", rd->code);
618 }
619 }
620 }
621
622 #endif /* #if !UCONFIG_NO_FORMATTING */
623