• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * Copyright (c) 1997-2016, International Business Machines
5  * Corporation and others. All Rights Reserved.
6  ********************************************************************/
7 
8 #include <string.h>
9 #include "unicode/utypes.h"
10 #include "unicode/uscript.h"
11 #include "unicode/uchar.h"
12 #include "cintltst.h"
13 #include "cucdapi.h"
14 #include "cmemory.h"
15 
scriptsToString(const UScriptCode scripts[],int32_t length,char s[])16 static void scriptsToString(const UScriptCode scripts[], int32_t length, char s[]) {
17     int32_t i;
18     if(length == 0) {
19         strcpy(s, "(no scripts)");
20         return;
21     }
22     s[0] = 0;
23     for(i = 0; i < length; ++i) {
24         if(i > 0) {
25             strcat(s, " ");
26         }
27         strcat(s, uscript_getShortName(scripts[i]));
28     }
29 }
30 
assertEqualScripts(const char * msg,const UScriptCode scripts1[],int32_t length1,const UScriptCode scripts2[],int32_t length2,UErrorCode errorCode)31 static void assertEqualScripts(const char *msg,
32                                const UScriptCode scripts1[], int32_t length1,
33                                const UScriptCode scripts2[], int32_t length2,
34                                UErrorCode errorCode) {
35     char s1[80];
36     char s2[80];
37     if(U_FAILURE(errorCode)) {
38         log_err("Failed: %s - %s\n", msg, u_errorName(errorCode));
39         return;
40     }
41     scriptsToString(scripts1, length1, s1);
42     scriptsToString(scripts2, length2, s2);
43     if(0!=strcmp(s1, s2)) {
44         log_data_err("Failed: %s: expected %s but got %s\n", msg, s1, s2);
45     }
46 }
47 
TestUScriptCodeAPI()48 void TestUScriptCodeAPI(){
49     int i =0;
50     int numErrors =0;
51     {
52         const char* testNames[]={
53         /* test locale */
54         "en", "en_US", "sr", "ta" , "te_IN",
55         "hi", "he", "ar",
56         /* test abbr */
57         "Hani", "Hang","Hebr","Hira",
58         "Knda","Kana","Khmr","Lao",
59         "Latn",/*"Latf","Latg",*/
60         "Mlym", "Mong",
61 
62         /* test names */
63         "CYRILLIC","DESERET","DEVANAGARI","ETHIOPIC","GEORGIAN",
64         "GOTHIC",  "GREEK",  "GUJARATI", "COMMON", "INHERITED",
65         /* test lower case names */
66         "malayalam", "mongolian", "myanmar", "ogham", "old-italic",
67         "oriya",     "runic",     "sinhala", "syriac","tamil",
68         "telugu",    "thaana",    "thai",    "tibetan",
69         /* test the bounds*/
70         "tagb", "arabic",
71         /* test bogus */
72         "asfdasd", "5464", "12235",
73         /* test the last index */
74         "zyyy", "YI",
75         NULL
76         };
77         UScriptCode expected[] ={
78             /* locales should return */
79             USCRIPT_LATIN, USCRIPT_LATIN, USCRIPT_CYRILLIC, USCRIPT_TAMIL, USCRIPT_TELUGU,
80             USCRIPT_DEVANAGARI, USCRIPT_HEBREW, USCRIPT_ARABIC,
81             /* abbr should return */
82             USCRIPT_HAN, USCRIPT_HANGUL, USCRIPT_HEBREW, USCRIPT_HIRAGANA,
83             USCRIPT_KANNADA, USCRIPT_KATAKANA, USCRIPT_KHMER, USCRIPT_LAO,
84             USCRIPT_LATIN,/* USCRIPT_LATIN, USCRIPT_LATIN,*/
85             USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN,
86             /* names should return */
87             USCRIPT_CYRILLIC, USCRIPT_DESERET, USCRIPT_DEVANAGARI, USCRIPT_ETHIOPIC, USCRIPT_GEORGIAN,
88             USCRIPT_GOTHIC, USCRIPT_GREEK, USCRIPT_GUJARATI, USCRIPT_COMMON, USCRIPT_INHERITED,
89             /* lower case names should return */
90             USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN, USCRIPT_MYANMAR, USCRIPT_OGHAM, USCRIPT_OLD_ITALIC,
91             USCRIPT_ORIYA, USCRIPT_RUNIC, USCRIPT_SINHALA, USCRIPT_SYRIAC, USCRIPT_TAMIL,
92             USCRIPT_TELUGU, USCRIPT_THAANA, USCRIPT_THAI, USCRIPT_TIBETAN,
93             /* bounds */
94             USCRIPT_TAGBANWA, USCRIPT_ARABIC,
95             /* bogus names should return invalid code */
96             USCRIPT_INVALID_CODE, USCRIPT_INVALID_CODE, USCRIPT_INVALID_CODE,
97             USCRIPT_COMMON, USCRIPT_YI,
98         };
99 
100         UErrorCode err = U_ZERO_ERROR;
101 
102         const int32_t capacity = 10;
103 
104         for( ; testNames[i]!=NULL; i++){
105             UScriptCode script[10]={USCRIPT_INVALID_CODE};
106             uscript_getCode(testNames[i],script,capacity, &err);
107             if( script[0] != expected[i]){
108                    log_data_err("Error getting script code Got: %i  Expected: %i for name %s (Error code does not propagate if data is not present. Are you missing data?)\n",
109                        script[0],expected[i],testNames[i]);
110                    numErrors++;
111             }
112         }
113         if(numErrors >0 ){
114             log_data_err("Errors uchar_getScriptCode() : %i \n",numErrors);
115         }
116     }
117 
118     {
119         UErrorCode err = U_ZERO_ERROR;
120         int32_t capacity=0;
121         int32_t j;
122         UScriptCode jaCode[]={USCRIPT_KATAKANA, USCRIPT_HIRAGANA, USCRIPT_HAN };
123         UScriptCode script[10]={USCRIPT_INVALID_CODE};
124         int32_t num = uscript_getCode("ja",script,capacity, &err);
125         /* preflight */
126         if(err==U_BUFFER_OVERFLOW_ERROR){
127             err = U_ZERO_ERROR;
128             capacity = 10;
129             num = uscript_getCode("ja",script,capacity, &err);
130             if(num!=UPRV_LENGTHOF(jaCode)){
131                 log_err("Errors uscript_getScriptCode() for Japanese locale: num=%d, expected %d \n",
132                         num, UPRV_LENGTHOF(jaCode));
133             }
134             for(j=0;j<UPRV_LENGTHOF(jaCode);j++) {
135                 if(script[j]!=jaCode[j]) {
136                     log_err("Japanese locale: code #%d was %d (%s) but expected %d (%s)\n", j,
137                             script[j], uscript_getName(script[j]),
138                             jaCode[j], uscript_getName(jaCode[j]));
139 
140                 }
141             }
142         }else{
143             log_data_err("Errors in uscript_getScriptCode() expected error : %s got: %s \n",
144                 "U_BUFFER_OVERFLOW_ERROR",
145                  u_errorName(err));
146         }
147 
148     }
149     {
150         static const UScriptCode LATIN[1] = { USCRIPT_LATIN };
151         static const UScriptCode CYRILLIC[1] = { USCRIPT_CYRILLIC };
152         static const UScriptCode DEVANAGARI[1] = { USCRIPT_DEVANAGARI };
153         static const UScriptCode HAN[1] = { USCRIPT_HAN };
154         static const UScriptCode JAPANESE[3] = { USCRIPT_KATAKANA, USCRIPT_HIRAGANA, USCRIPT_HAN };
155         static const UScriptCode KOREAN[2] = { USCRIPT_HANGUL, USCRIPT_HAN };
156         static const UScriptCode HAN_BOPO[2] = { USCRIPT_HAN, USCRIPT_BOPOMOFO };
157         UScriptCode scripts[5];
158         UErrorCode err;
159         int32_t num;
160 
161         // Should work regardless of whether we have locale data for the language.
162         err = U_ZERO_ERROR;
163         num = uscript_getCode("tg", scripts, UPRV_LENGTHOF(scripts), &err);
164         assertEqualScripts("tg script: Cyrl", CYRILLIC, 1, scripts, num, err);  // Tajik
165         err = U_ZERO_ERROR;
166         num = uscript_getCode("xsr", scripts, UPRV_LENGTHOF(scripts), &err);
167         assertEqualScripts("xsr script: Deva", DEVANAGARI, 1, scripts, num, err);  // Sherpa
168 
169         // Multi-script languages.
170         err = U_ZERO_ERROR;
171         num = uscript_getCode("ja", scripts, UPRV_LENGTHOF(scripts), &err);
172         assertEqualScripts("ja scripts: Kana Hira Hani",
173                            JAPANESE, UPRV_LENGTHOF(JAPANESE), scripts, num, err);
174         err = U_ZERO_ERROR;
175         num = uscript_getCode("ko", scripts, UPRV_LENGTHOF(scripts), &err);
176         assertEqualScripts("ko scripts: Hang Hani",
177                            KOREAN, UPRV_LENGTHOF(KOREAN), scripts, num, err);
178         err = U_ZERO_ERROR;
179         num = uscript_getCode("zh", scripts, UPRV_LENGTHOF(scripts), &err);
180         assertEqualScripts("zh script: Hani", HAN, 1, scripts, num, err);
181         err = U_ZERO_ERROR;
182         num = uscript_getCode("zh-Hant", scripts, UPRV_LENGTHOF(scripts), &err);
183         assertEqualScripts("zh-Hant scripts: Hani Bopo", HAN_BOPO, 2, scripts, num, err);
184         err = U_ZERO_ERROR;
185         num = uscript_getCode("zh-TW", scripts, UPRV_LENGTHOF(scripts), &err);
186         assertEqualScripts("zh-TW scripts: Hani Bopo", HAN_BOPO, 2, scripts, num, err);
187 
188         // Ambiguous API, but this probably wants to return Latin rather than Rongorongo (Roro).
189         err = U_ZERO_ERROR;
190         num = uscript_getCode("ro-RO", scripts, UPRV_LENGTHOF(scripts), &err);
191         assertEqualScripts("ro-RO script: Latn", LATIN, 1, scripts, num, err);
192     }
193 
194     {
195         UScriptCode testAbbr[]={
196             /* names should return */
197             USCRIPT_CYRILLIC, USCRIPT_DESERET, USCRIPT_DEVANAGARI, USCRIPT_ETHIOPIC, USCRIPT_GEORGIAN,
198             USCRIPT_GOTHIC, USCRIPT_GREEK, USCRIPT_GUJARATI,
199         };
200 
201         const char* expectedNames[]={
202 
203             /* test names */
204             "Cyrillic","Deseret","Devanagari","Ethiopic","Georgian",
205             "Gothic",  "Greek",  "Gujarati",
206              NULL
207         };
208         i=0;
209         while(i<UPRV_LENGTHOF(testAbbr)){
210             const char* name = uscript_getName(testAbbr[i]);
211              if(name == NULL) {
212                log_data_err("Couldn't get script name\n");
213                return;
214              }
215             numErrors=0;
216             if(strcmp(expectedNames[i],name)!=0){
217                 log_err("Error getting abbreviations Got: %s Expected: %s\n",name,expectedNames[i]);
218                 numErrors++;
219             }
220             if(numErrors > 0){
221                 if(numErrors >0 ){
222                     log_err("Errors uchar_getScriptAbbr() : %i \n",numErrors);
223                 }
224             }
225             i++;
226         }
227 
228     }
229 
230     {
231         UScriptCode testAbbr[]={
232             /* abbr should return */
233             USCRIPT_HAN, USCRIPT_HANGUL, USCRIPT_HEBREW, USCRIPT_HIRAGANA,
234             USCRIPT_KANNADA, USCRIPT_KATAKANA, USCRIPT_KHMER, USCRIPT_LAO,
235             USCRIPT_LATIN,
236             USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN,
237         };
238 
239         const char* expectedAbbr[]={
240               /* test abbr */
241             "Hani", "Hang","Hebr","Hira",
242             "Knda","Kana","Khmr","Laoo",
243             "Latn",
244             "Mlym", "Mong",
245              NULL
246         };
247         i=0;
248         while(i<UPRV_LENGTHOF(testAbbr)){
249             const char* name = uscript_getShortName(testAbbr[i]);
250             numErrors=0;
251             if(strcmp(expectedAbbr[i],name)!=0){
252                 log_err("Error getting abbreviations Got: %s Expected: %s\n",name,expectedAbbr[i]);
253                 numErrors++;
254             }
255             if(numErrors > 0){
256                 if(numErrors >0 ){
257                     log_err("Errors uchar_getScriptAbbr() : %i \n",numErrors);
258                 }
259             }
260             i++;
261         }
262 
263     }
264     /* now test uscript_getScript() API */
265     {
266         uint32_t codepoints[] = {
267                 0x0000FF9D, /* USCRIPT_KATAKANA*/
268                 0x0000FFBE, /* USCRIPT_HANGUL*/
269                 0x0000FFC7, /* USCRIPT_HANGUL*/
270                 0x0000FFCF, /* USCRIPT_HANGUL*/
271                 0x0000FFD7, /* USCRIPT_HANGUL*/
272                 0x0000FFDC, /* USCRIPT_HANGUL*/
273                 0x00010300, /* USCRIPT_OLD_ITALIC*/
274                 0x00010330, /* USCRIPT_GOTHIC*/
275                 0x0001034A, /* USCRIPT_GOTHIC*/
276                 0x00010400, /* USCRIPT_DESERET*/
277                 0x00010428, /* USCRIPT_DESERET*/
278                 0x0001D167, /* USCRIPT_INHERITED*/
279                 0x0001D17B, /* USCRIPT_INHERITED*/
280                 0x0001D185, /* USCRIPT_INHERITED*/
281                 0x0001D1AA, /* USCRIPT_INHERITED*/
282                 0x00020000, /* USCRIPT_HAN*/
283                 0x00000D02, /* USCRIPT_MALAYALAM*/
284                 0x00000D00, /* USCRIPT_UNKNOWN (new Zzzz value in Unicode 5.0) */
285                 0x00000000, /* USCRIPT_COMMON*/
286                 0x0001D169, /* USCRIPT_INHERITED*/
287                 0x0001D182, /* USCRIPT_INHERITED*/
288                 0x0001D18B, /* USCRIPT_INHERITED*/
289                 0x0001D1AD, /* USCRIPT_INHERITED*/
290         };
291 
292         UScriptCode expected[] = {
293                 USCRIPT_KATAKANA ,
294                 USCRIPT_HANGUL ,
295                 USCRIPT_HANGUL ,
296                 USCRIPT_HANGUL ,
297                 USCRIPT_HANGUL ,
298                 USCRIPT_HANGUL ,
299                 USCRIPT_OLD_ITALIC,
300                 USCRIPT_GOTHIC ,
301                 USCRIPT_GOTHIC ,
302                 USCRIPT_DESERET ,
303                 USCRIPT_DESERET ,
304                 USCRIPT_INHERITED,
305                 USCRIPT_INHERITED,
306                 USCRIPT_INHERITED,
307                 USCRIPT_INHERITED,
308                 USCRIPT_HAN ,
309                 USCRIPT_MALAYALAM,
310                 USCRIPT_UNKNOWN,
311                 USCRIPT_COMMON,
312                 USCRIPT_INHERITED ,
313                 USCRIPT_INHERITED ,
314                 USCRIPT_INHERITED ,
315                 USCRIPT_INHERITED ,
316         };
317         UScriptCode code = USCRIPT_INVALID_CODE;
318         UErrorCode status = U_ZERO_ERROR;
319         UBool passed = TRUE;
320 
321         for(i=0; i<UPRV_LENGTHOF(codepoints); ++i){
322             code = uscript_getScript(codepoints[i],&status);
323             if(U_SUCCESS(status)){
324                 if( code != expected[i] ||
325                     code != (UScriptCode)u_getIntPropertyValue(codepoints[i], UCHAR_SCRIPT)
326                 ) {
327                     log_err("uscript_getScript for codepoint \\U%08X failed\n",codepoints[i]);
328                     passed = FALSE;
329                 }
330             }else{
331                 log_err("uscript_getScript for codepoint \\U%08X failed. Error: %s\n",
332                          codepoints[i],u_errorName(status));
333                 break;
334             }
335         }
336 
337         if(passed==FALSE){
338            log_err("uscript_getScript failed.\n");
339         }
340     }
341     {
342         UScriptCode code= USCRIPT_INVALID_CODE;
343         UErrorCode  status = U_ZERO_ERROR;
344         code = uscript_getScript(0x001D169,&status);
345         if(code != USCRIPT_INHERITED){
346             log_err("\\U001D169 is not contained in USCRIPT_INHERITED");
347         }
348     }
349     {
350         UScriptCode code= USCRIPT_INVALID_CODE;
351         UErrorCode  status = U_ZERO_ERROR;
352         int32_t err = 0;
353 
354         for(i = 0; i<=0x10ffff; i++){
355             code =  uscript_getScript(i,&status);
356             if(code == USCRIPT_INVALID_CODE){
357                 err++;
358                 log_err("uscript_getScript for codepoint \\U%08X failed.\n", i);
359             }
360         }
361         if(err>0){
362             log_err("uscript_getScript failed for %d codepoints\n", err);
363         }
364     }
365     {
366         for(i=0; (UScriptCode)i< USCRIPT_CODE_LIMIT; i++){
367             const char* name = uscript_getName((UScriptCode)i);
368             if(name==NULL || strcmp(name,"")==0){
369                 log_err("uscript_getName failed for code %i: name is NULL or \"\"\n",i);
370             }
371         }
372     }
373 
374     {
375         /*
376          * These script codes were originally added to ICU pre-3.6, so that ICU would
377          * have all ISO 15924 script codes. ICU was then based on Unicode 4.1.
378          * These script codes were added with only short names because we don't
379          * want to invent long names ourselves.
380          * Unicode 5 and later encode some of these scripts and give them long names.
381          * Whenever this happens, the long script names here need to be updated.
382          */
383         static const char* expectedLong[] = {
384             "Balinese", "Batak", "Blis", "Brahmi", "Cham", "Cirt", "Cyrs",
385             "Egyd", "Egyh", "Egyptian_Hieroglyphs",
386             "Geok", "Hans", "Hant", "Pahawh_Hmong", "Old_Hungarian", "Inds",
387             "Javanese", "Kayah_Li", "Latf", "Latg",
388             "Lepcha", "Linear_A", "Mandaic", "Maya", "Meroitic_Hieroglyphs",
389             "Nko", "Old_Turkic", "Old_Permic", "Phags_Pa", "Phoenician",
390             "Miao", "Roro", "Sara", "Syre", "Syrj", "Syrn", "Teng", "Vai", "Visp", "Cuneiform",
391             "Zxxx", "Unknown",
392             "Carian", "Jpan", "Tai_Tham", "Lycian", "Lydian", "Ol_Chiki", "Rejang", "Saurashtra", "SignWriting", "Sundanese",
393             "Moon", "Meetei_Mayek",
394             /* new in ICU 4.0 */
395             "Imperial_Aramaic", "Avestan", "Chakma", "Kore",
396             "Kaithi", "Manichaean", "Inscriptional_Pahlavi", "Psalter_Pahlavi", "Phlv",
397             "Inscriptional_Parthian", "Samaritan", "Tai_Viet",
398             "Zmth", "Zsym",
399             /* new in ICU 4.4 */
400             "Bamum", "Lisu", "Nkgb", "Old_South_Arabian",
401             /* new in ICU 4.6 */
402             "Bassa_Vah", "Duployan", "Elbasan", "Grantha", "Kpel",
403             "Loma", "Mende_Kikakui", "Meroitic_Cursive",
404             "Old_North_Arabian", "Nabataean", "Palmyrene", "Khudawadi", "Warang_Citi",
405             /* new in ICU 4.8 */
406             "Afak", "Jurc", "Mro", "Nshu", "Sharada", "Sora_Sompeng", "Takri", "Tangut", "Wole",
407             /* new in ICU 49 */
408             "Anatolian_Hieroglyphs", "Khojki", "Tirhuta",
409             /* new in ICU 52 */
410             "Caucasian_Albanian", "Mahajani",
411             /* new in ICU 54 */
412             "Ahom", "Hatran", "Modi", "Multani", "Pau_Cin_Hau", "Siddham",
413             // new in ICU 58
414             "Adlam", "Bhaiksuki", "Marchen", "Newa", "Osage", "Hanb", "Jamo", "Zsye"
415         };
416         static const char* expectedShort[] = {
417             "Bali", "Batk", "Blis", "Brah", "Cham", "Cirt", "Cyrs", "Egyd", "Egyh", "Egyp",
418             "Geok", "Hans", "Hant", "Hmng", "Hung", "Inds", "Java", "Kali", "Latf", "Latg",
419             "Lepc", "Lina", "Mand", "Maya", "Mero", "Nkoo", "Orkh", "Perm", "Phag", "Phnx",
420             "Plrd", "Roro", "Sara", "Syre", "Syrj", "Syrn", "Teng", "Vaii", "Visp", "Xsux",
421             "Zxxx", "Zzzz",
422             "Cari", "Jpan", "Lana", "Lyci", "Lydi", "Olck", "Rjng", "Saur", "Sgnw", "Sund",
423             "Moon", "Mtei",
424             /* new in ICU 4.0 */
425             "Armi", "Avst", "Cakm", "Kore",
426             "Kthi", "Mani", "Phli", "Phlp", "Phlv", "Prti", "Samr", "Tavt",
427             "Zmth", "Zsym",
428             /* new in ICU 4.4 */
429             "Bamu", "Lisu", "Nkgb", "Sarb",
430             /* new in ICU 4.6 */
431             "Bass", "Dupl", "Elba", "Gran", "Kpel", "Loma", "Mend", "Merc",
432             "Narb", "Nbat", "Palm", "Sind", "Wara",
433             /* new in ICU 4.8 */
434             "Afak", "Jurc", "Mroo", "Nshu", "Shrd", "Sora", "Takr", "Tang", "Wole",
435             /* new in ICU 49 */
436             "Hluw", "Khoj", "Tirh",
437             /* new in ICU 52 */
438             "Aghb", "Mahj",
439             /* new in ICU 54 */
440             "Ahom", "Hatr", "Modi", "Mult", "Pauc", "Sidd",
441             // new in ICU 58
442             "Adlm", "Bhks", "Marc", "Newa", "Osge", "Hanb", "Jamo", "Zsye"
443         };
444         int32_t j = 0;
445         if(UPRV_LENGTHOF(expectedLong)!=(USCRIPT_CODE_LIMIT-USCRIPT_BALINESE)) {
446             log_err("need to add new script codes in cucdapi.c!\n");
447             return;
448         }
449         for(i=USCRIPT_BALINESE; (UScriptCode)i<USCRIPT_CODE_LIMIT; i++, j++){
450             const char* name = uscript_getName((UScriptCode)i);
451             if(name==NULL || strcmp(name,expectedLong[j])!=0){
452                 log_err("uscript_getName failed for code %i: %s!=%s\n", i, name, expectedLong[j]);
453             }
454             name = uscript_getShortName((UScriptCode)i);
455             if(name==NULL || strcmp(name,expectedShort[j])!=0){
456                 log_err("uscript_getShortName failed for code %i: %s!=%s\n", i, name, expectedShort[j]);
457             }
458         }
459         for(i=0; i<UPRV_LENGTHOF(expectedLong); i++){
460             UScriptCode fillIn[5] = {USCRIPT_INVALID_CODE};
461             UErrorCode status = U_ZERO_ERROR;
462             int32_t len = 0;
463             len = uscript_getCode(expectedShort[i], fillIn, UPRV_LENGTHOF(fillIn), &status);
464             if(U_FAILURE(status)){
465                 log_err("uscript_getCode failed for script name %s. Error: %s\n",expectedShort[i], u_errorName(status));
466             }
467             if(len>1){
468                 log_err("uscript_getCode did not return expected number of codes for script %s. EXPECTED: 1 GOT: %i\n", expectedShort[i], len);
469             }
470             if(fillIn[0]!= (UScriptCode)(USCRIPT_BALINESE+i)){
471                 log_err("uscript_getCode did not return expected code for script %s. EXPECTED: %i GOT: %i\n", expectedShort[i], (USCRIPT_BALINESE+i), fillIn[0] );
472             }
473         }
474     }
475 
476     {
477         /* test characters which have Script_Extensions */
478         UErrorCode errorCode=U_ZERO_ERROR;
479         if(!(
480                 USCRIPT_COMMON==uscript_getScript(0x0640, &errorCode) &&
481                 USCRIPT_INHERITED==uscript_getScript(0x0650, &errorCode) &&
482                 USCRIPT_ARABIC==uscript_getScript(0xfdf2, &errorCode)) ||
483             U_FAILURE(errorCode)
484         ) {
485             log_err("uscript_getScript(character with Script_Extensions) failed\n");
486         }
487     }
488 }
489 
TestHasScript()490 void TestHasScript() {
491     if(!(
492         !uscript_hasScript(0x063f, USCRIPT_COMMON) &&
493         uscript_hasScript(0x063f, USCRIPT_ARABIC) &&  /* main Script value */
494         !uscript_hasScript(0x063f, USCRIPT_SYRIAC) &&
495         !uscript_hasScript(0x063f, USCRIPT_THAANA))
496     ) {
497         log_err("uscript_hasScript(U+063F, ...) is wrong\n");
498     }
499     if(!(
500         !uscript_hasScript(0x0640, USCRIPT_COMMON) &&  /* main Script value */
501         uscript_hasScript(0x0640, USCRIPT_ARABIC) &&
502         uscript_hasScript(0x0640, USCRIPT_SYRIAC) &&
503         !uscript_hasScript(0x0640, USCRIPT_THAANA))
504     ) {
505         log_err("uscript_hasScript(U+0640, ...) is wrong\n");
506     }
507     if(!(
508         !uscript_hasScript(0x0650, USCRIPT_INHERITED) &&  /* main Script value */
509         uscript_hasScript(0x0650, USCRIPT_ARABIC) &&
510         uscript_hasScript(0x0650, USCRIPT_SYRIAC) &&
511         !uscript_hasScript(0x0650, USCRIPT_THAANA))
512     ) {
513         log_err("uscript_hasScript(U+0650, ...) is wrong\n");
514     }
515     if(!(
516         !uscript_hasScript(0x0660, USCRIPT_COMMON) &&  /* main Script value */
517         uscript_hasScript(0x0660, USCRIPT_ARABIC) &&
518         !uscript_hasScript(0x0660, USCRIPT_SYRIAC) &&
519         uscript_hasScript(0x0660, USCRIPT_THAANA))
520     ) {
521         log_err("uscript_hasScript(U+0660, ...) is wrong\n");
522     }
523     if(!(
524         !uscript_hasScript(0xfdf2, USCRIPT_COMMON) &&
525         uscript_hasScript(0xfdf2, USCRIPT_ARABIC) &&  /* main Script value */
526         !uscript_hasScript(0xfdf2, USCRIPT_SYRIAC) &&
527         uscript_hasScript(0xfdf2, USCRIPT_THAANA))
528     ) {
529         log_err("uscript_hasScript(U+FDF2, ...) is wrong\n");
530     }
531     if(uscript_hasScript(0x0640, 0xaffe)) {
532         /* An unguarded implementation might go into an infinite loop. */
533         log_err("uscript_hasScript(U+0640, bogus 0xaffe) is wrong\n");
534     }
535 }
536 
scriptsContain(UScriptCode scripts[],int32_t length,UScriptCode script)537 static UBool scriptsContain(UScriptCode scripts[], int32_t length, UScriptCode script) {
538     UBool contain=FALSE;
539     int32_t prev=-1, i;
540     for(i=0; i<length; ++i) {
541         int32_t s=scripts[i];
542         if(s<=prev) {
543             log_err("uscript_getScriptExtensions() not in sorted order: %d %d\n", (int)prev, (int)s);
544         }
545         if(s==script) { contain=TRUE; }
546     }
547     return contain;
548 }
549 
TestGetScriptExtensions()550 void TestGetScriptExtensions() {
551     UScriptCode scripts[20];
552     int32_t length;
553     UErrorCode errorCode;
554 
555     /* errors and overflows */
556     errorCode=U_PARSE_ERROR;
557     length=uscript_getScriptExtensions(0x0640, scripts, UPRV_LENGTHOF(scripts), &errorCode);
558     if(errorCode!=U_PARSE_ERROR) {
559         log_err("uscript_getScriptExtensions(U+0640, U_PARSE_ERROR) did not preserve the UErrorCode - %s\n",
560               u_errorName(errorCode));
561     }
562     errorCode=U_ZERO_ERROR;
563     length=uscript_getScriptExtensions(0x0640, NULL, UPRV_LENGTHOF(scripts), &errorCode);
564     if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
565         log_err("uscript_getScriptExtensions(U+0640, NULL) did not set U_ILLEGAL_ARGUMENT_ERROR - %s\n",
566               u_errorName(errorCode));
567     }
568     errorCode=U_ZERO_ERROR;
569     length=uscript_getScriptExtensions(0x0640, scripts, -1, &errorCode);
570     if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) {
571         log_err("uscript_getScriptExtensions(U+0640, capacity<0) did not set U_ILLEGAL_ARGUMENT_ERROR - %s\n",
572               u_errorName(errorCode));
573     }
574     errorCode=U_ZERO_ERROR;
575     length=uscript_getScriptExtensions(0x0640, scripts, 0, &errorCode);
576     if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length<3) {
577         log_err("uscript_getScriptExtensions(U+0640, capacity=0: pure preflighting)=%d < 3 - %s\n",
578               (int)length, u_errorName(errorCode));
579     }
580     errorCode=U_ZERO_ERROR;
581     length=uscript_getScriptExtensions(0x0640, scripts, 1, &errorCode);
582     if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length<3) {
583         log_err("uscript_getScriptExtensions(U+0640, capacity=1: preflighting)=%d < 3 - %s\n",
584               (int)length, u_errorName(errorCode));
585     }
586     /* U+063F has only a Script code, no Script_Extensions. */
587     errorCode=U_ZERO_ERROR;
588     length=uscript_getScriptExtensions(0x063f, scripts, 0, &errorCode);
589     if(errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=1) {
590         log_err("uscript_getScriptExtensions(U+063F, capacity=0)=%d != 1 - %s\n",
591               (int)length, u_errorName(errorCode));
592     }
593 
594     /* invalid code points */
595     errorCode=U_ZERO_ERROR;
596     length=uscript_getScriptExtensions(-1, scripts, UPRV_LENGTHOF(scripts), &errorCode);
597     if(U_FAILURE(errorCode) || length!=1 || scripts[0]!=USCRIPT_UNKNOWN) {
598         log_err("uscript_getScriptExtensions(-1)=%d does not return {UNKNOWN} - %s\n",
599               (int)length, u_errorName(errorCode));
600     }
601     errorCode=U_ZERO_ERROR;
602     length=uscript_getScriptExtensions(0x110000, scripts, UPRV_LENGTHOF(scripts), &errorCode);
603     if(U_FAILURE(errorCode) || length!=1 || scripts[0]!=USCRIPT_UNKNOWN) {
604         log_err("uscript_getScriptExtensions(0x110000)=%d does not return {UNKNOWN} - %s\n",
605               (int)length, u_errorName(errorCode));
606     }
607 
608     /* normal usage */
609     errorCode=U_ZERO_ERROR;
610     length=uscript_getScriptExtensions(0x063f, scripts, 1, &errorCode);
611     if(U_FAILURE(errorCode) || length!=1 || scripts[0]!=USCRIPT_ARABIC) {
612         log_err("uscript_getScriptExtensions(U+063F, capacity=1)=%d does not return {ARABIC} - %s\n",
613               (int)length, u_errorName(errorCode));
614     }
615     errorCode=U_ZERO_ERROR;
616     length=uscript_getScriptExtensions(0x0640, scripts, UPRV_LENGTHOF(scripts), &errorCode);
617     if(U_FAILURE(errorCode) || length<3 ||
618             !scriptsContain(scripts, length, USCRIPT_ARABIC) ||
619             !scriptsContain(scripts, length, USCRIPT_SYRIAC) ||
620             !scriptsContain(scripts, length, USCRIPT_MANDAIC)) {
621         log_err("uscript_getScriptExtensions(U+0640)=%d failed - %s\n",
622               (int)length, u_errorName(errorCode));
623     }
624     errorCode=U_ZERO_ERROR;
625     length=uscript_getScriptExtensions(0xfdf2, scripts, UPRV_LENGTHOF(scripts), &errorCode);
626     if(U_FAILURE(errorCode) || length!=2 || scripts[0]!=USCRIPT_ARABIC || scripts[1]!=USCRIPT_THAANA) {
627         log_err("uscript_getScriptExtensions(U+FDF2)=%d failed - %s\n",
628               (int)length, u_errorName(errorCode));
629     }
630     errorCode=U_ZERO_ERROR;
631     length=uscript_getScriptExtensions(0xff65, scripts, UPRV_LENGTHOF(scripts), &errorCode);
632     if(U_FAILURE(errorCode) || length!=6 || scripts[0]!=USCRIPT_BOPOMOFO || scripts[5]!=USCRIPT_YI) {
633         log_err("uscript_getScriptExtensions(U+FF65)=%d failed - %s\n",
634               (int)length, u_errorName(errorCode));
635     }
636 }
637 
TestScriptMetadataAPI()638 void TestScriptMetadataAPI() {
639     /* API & code coverage. More testing in intltest/ucdtest.cpp. */
640     UErrorCode errorCode=U_ZERO_ERROR;
641     UChar sample[8];
642 
643     if(uscript_getSampleString(USCRIPT_LATIN, sample, UPRV_LENGTHOF(sample), &errorCode)!=1 ||
644             U_FAILURE(errorCode) ||
645             uscript_getScript(sample[0], &errorCode)!=USCRIPT_LATIN ||
646             sample[1]!=0) {
647         log_err("uscript_getSampleString(Latn) failed - %s\n", u_errorName(errorCode));
648     }
649     sample[0]=0xfffe;
650     if(uscript_getSampleString(USCRIPT_LATIN, sample, 0, &errorCode)!=1 ||
651             errorCode!=U_BUFFER_OVERFLOW_ERROR ||
652             sample[0]!=0xfffe) {
653         log_err("uscript_getSampleString(Latn, capacity=0) failed - %s\n", u_errorName(errorCode));
654     }
655     errorCode=U_ZERO_ERROR;
656     if(uscript_getSampleString(USCRIPT_INVALID_CODE, sample, UPRV_LENGTHOF(sample), &errorCode)!=0 ||
657             U_FAILURE(errorCode) ||
658             sample[0]!=0) {
659         log_err("uscript_getSampleString(invalid) failed - %s\n", u_errorName(errorCode));
660     }
661     sample[0]=0xfffe;
662     if(uscript_getSampleString(USCRIPT_CODE_LIMIT, sample, 0, &errorCode)!=0 ||
663             errorCode!=U_STRING_NOT_TERMINATED_WARNING ||
664             sample[0]!=0xfffe) {
665         log_err("uscript_getSampleString(limit, capacity=0) failed - %s\n", u_errorName(errorCode));
666     }
667 
668     if(uscript_getUsage(USCRIPT_LATIN)!=USCRIPT_USAGE_RECOMMENDED ||
669             uscript_getUsage(USCRIPT_YI)!=USCRIPT_USAGE_ASPIRATIONAL ||
670             uscript_getUsage(USCRIPT_CHEROKEE)!=USCRIPT_USAGE_LIMITED_USE ||
671             uscript_getUsage(USCRIPT_COPTIC)!=USCRIPT_USAGE_EXCLUDED ||
672             uscript_getUsage(USCRIPT_CIRTH)!=USCRIPT_USAGE_NOT_ENCODED ||
673             uscript_getUsage(USCRIPT_INVALID_CODE)!=USCRIPT_USAGE_NOT_ENCODED ||
674             uscript_getUsage(USCRIPT_CODE_LIMIT)!=USCRIPT_USAGE_NOT_ENCODED) {
675         log_err("uscript_getUsage() failed\n");
676     }
677 
678     if(uscript_isRightToLeft(USCRIPT_LATIN) ||
679             uscript_isRightToLeft(USCRIPT_CIRTH) ||
680             !uscript_isRightToLeft(USCRIPT_ARABIC) ||
681             !uscript_isRightToLeft(USCRIPT_HEBREW)) {
682         log_err("uscript_isRightToLeft() failed\n");
683     }
684 
685     if(uscript_breaksBetweenLetters(USCRIPT_LATIN) ||
686             uscript_breaksBetweenLetters(USCRIPT_CIRTH) ||
687             !uscript_breaksBetweenLetters(USCRIPT_HAN) ||
688             !uscript_breaksBetweenLetters(USCRIPT_THAI)) {
689         log_err("uscript_breaksBetweenLetters() failed\n");
690     }
691 
692     if(uscript_isCased(USCRIPT_CIRTH) ||
693             uscript_isCased(USCRIPT_HAN) ||
694             !uscript_isCased(USCRIPT_LATIN) ||
695             !uscript_isCased(USCRIPT_GREEK)) {
696         log_err("uscript_isCased() failed\n");
697     }
698 }
699 
TestBinaryValues()700 void TestBinaryValues() {
701     /*
702      * Unicode 5.1 explicitly defines binary property value aliases.
703      * Verify that they are all recognized.
704      */
705     static const char *const falseValues[]={ "N", "No", "F", "False" };
706     static const char *const trueValues[]={ "Y", "Yes", "T", "True" };
707     int32_t i;
708     for(i=0; i<UPRV_LENGTHOF(falseValues); ++i) {
709         if(FALSE!=u_getPropertyValueEnum(UCHAR_ALPHABETIC, falseValues[i])) {
710             log_data_err("u_getPropertyValueEnum(UCHAR_ALPHABETIC, \"%s\")!=FALSE (Are you missing data?)\n", falseValues[i]);
711         }
712     }
713     for(i=0; i<UPRV_LENGTHOF(trueValues); ++i) {
714         if(TRUE!=u_getPropertyValueEnum(UCHAR_ALPHABETIC, trueValues[i])) {
715             log_data_err("u_getPropertyValueEnum(UCHAR_ALPHABETIC, \"%s\")!=TRUE (Are you missing data?)\n", trueValues[i]);
716         }
717     }
718 }
719