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