]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cucdapi.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cucdapi.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include <string.h>
8 #include "unicode/utypes.h"
9 #include "unicode/uscript.h"
10 #include "unicode/uchar.h"
11 #include "cintltst.h"
12 #include "cucdapi.h"
13
14 void TestUScriptCodeAPI(){
15 int i =0;
16 int numErrors =0;
17 {
18 const char* testNames[]={
19 /* test locale */
20 "en", "en_US", "sr", "ta" , "te_IN",
21 "hi", "he", "ar",
22 /* test abbr */
23 "Hani", "Hang","Hebr","Hira",
24 "Knda","Kana","Khmr","Lao",
25 "Latn",/*"Latf","Latg",*/
26 "Mlym", "Mong",
27
28 /* test names */
29 "CYRILLIC","DESERET","DEVANAGARI","ETHIOPIC","GEORGIAN",
30 "GOTHIC", "GREEK", "GUJARATI", "COMMON", "INHERITED",
31 /* test lower case names */
32 "malayalam", "mongolian", "myanmar", "ogham", "old-italic",
33 "oriya", "runic", "sinhala", "syriac","tamil",
34 "telugu", "thaana", "thai", "tibetan",
35 /* test the bounds*/
36 "tagb", "arabic",
37 /* test bogus */
38 "asfdasd", "5464", "12235",
39 /* test the last index */
40 "zyyy", "YI",
41 '\0'
42 };
43 UScriptCode expected[] ={
44 /* locales should return */
45 USCRIPT_LATIN, USCRIPT_LATIN, USCRIPT_CYRILLIC, USCRIPT_TAMIL, USCRIPT_TELUGU,
46 USCRIPT_DEVANAGARI, USCRIPT_HEBREW, USCRIPT_ARABIC,
47 /* abbr should return */
48 USCRIPT_HAN, USCRIPT_HANGUL, USCRIPT_HEBREW, USCRIPT_HIRAGANA,
49 USCRIPT_KANNADA, USCRIPT_KATAKANA, USCRIPT_KHMER, USCRIPT_LAO,
50 USCRIPT_LATIN,/* USCRIPT_LATIN, USCRIPT_LATIN,*/
51 USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN,
52 /* names should return */
53 USCRIPT_CYRILLIC, USCRIPT_DESERET, USCRIPT_DEVANAGARI, USCRIPT_ETHIOPIC, USCRIPT_GEORGIAN,
54 USCRIPT_GOTHIC, USCRIPT_GREEK, USCRIPT_GUJARATI, USCRIPT_COMMON, USCRIPT_INHERITED,
55 /* lower case names should return */
56 USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN, USCRIPT_MYANMAR, USCRIPT_OGHAM, USCRIPT_OLD_ITALIC,
57 USCRIPT_ORIYA, USCRIPT_RUNIC, USCRIPT_SINHALA, USCRIPT_SYRIAC, USCRIPT_TAMIL,
58 USCRIPT_TELUGU, USCRIPT_THAANA, USCRIPT_THAI, USCRIPT_TIBETAN,
59 /* bounds */
60 USCRIPT_TAGBANWA, USCRIPT_ARABIC,
61 /* bogus names should return invalid code */
62 USCRIPT_INVALID_CODE, USCRIPT_INVALID_CODE, USCRIPT_INVALID_CODE,
63 USCRIPT_COMMON, USCRIPT_YI,
64 };
65
66 UErrorCode err = U_ZERO_ERROR;
67
68 const int32_t capacity = 10;
69
70 for( ; testNames[i]!='\0'; i++){
71 UScriptCode script[10]={USCRIPT_INVALID_CODE};
72 uscript_getCode(testNames[i],script,capacity, &err);
73 if( script[0] != expected[i]){
74 log_err("Error getting script code Got: %i Expected: %i for name %s\n",
75 script[0],expected[i],testNames[i]);
76 numErrors++;
77 }
78 }
79 if(numErrors >0 ){
80 log_data_err("Errors uchar_getScriptCode() : %i \n",numErrors);
81 }
82 }
83
84 {
85 UErrorCode err = U_ZERO_ERROR;
86 int32_t capacity=0;
87 UScriptCode jaCode[]={ USCRIPT_KATAKANA,USCRIPT_HIRAGANA,USCRIPT_HAN};
88 UScriptCode script[10]={USCRIPT_INVALID_CODE};
89 int32_t num = uscript_getCode("ja",script,capacity, &err);
90 /* preflight */
91 if(err==U_BUFFER_OVERFLOW_ERROR){
92 err = U_ZERO_ERROR;
93 capacity = 10;
94 num = uscript_getCode("ja",script,capacity, &err);
95 if(num!=(sizeof(jaCode)/sizeof(UScriptCode)) || script[0]!=jaCode[0] || script[1]!=jaCode[1]){
96 log_err("Errors uscript_getScriptCode() for Japaneese locale \n");
97 }
98 }else{
99 log_data_err("Errors in uscript_getScriptCode() expected error : %s got: %s \n",
100 "U_BUFFER_OVERFLOW_ERROR",
101 u_errorName(err));
102 }
103
104 }
105
106 {
107 UScriptCode testAbbr[]={
108 /* names should return */
109 USCRIPT_CYRILLIC, USCRIPT_DESERET, USCRIPT_DEVANAGARI, USCRIPT_ETHIOPIC, USCRIPT_GEORGIAN,
110 USCRIPT_GOTHIC, USCRIPT_GREEK, USCRIPT_GUJARATI,
111 };
112
113 const char* expectedNames[]={
114
115 /* test names */
116 "Cyrillic","Deseret","Devanagari","Ethiopic","Georgian",
117 "Gothic", "Greek", "Gujarati",
118 '\0'
119 };
120 i=0;
121 while(i<sizeof(testAbbr)/sizeof(UScriptCode)){
122 const char* name = uscript_getName(testAbbr[i]);
123 if(name == NULL) {
124 log_data_err("Couldn't get script name\n");
125 return;
126 }
127 numErrors=0;
128 if(strcmp(expectedNames[i],name)!=0){
129 log_err("Error getting abbreviations Got: %s Expected: %s\n",name,expectedNames[i]);
130 numErrors++;
131 }
132 if(numErrors > 0){
133 if(numErrors >0 ){
134 log_err("Errors uchar_getScriptAbbr() : %i \n",numErrors);
135 }
136 }
137 i++;
138 }
139
140 }
141
142 {
143 UScriptCode testAbbr[]={
144 /* abbr should return */
145 USCRIPT_HAN, USCRIPT_HANGUL, USCRIPT_HEBREW, USCRIPT_HIRAGANA,
146 USCRIPT_KANNADA, USCRIPT_KATAKANA, USCRIPT_KHMER, USCRIPT_LAO,
147 USCRIPT_LATIN,
148 USCRIPT_MALAYALAM, USCRIPT_MONGOLIAN,
149 };
150
151 const char* expectedAbbr[]={
152 /* test abbr */
153 "Hani", "Hang","Hebr","Hira",
154 "Knda","Kana","Khmr","Laoo",
155 "Latn",
156 "Mlym", "Mong",
157 '\0'
158 };
159 i=0;
160 while(i<sizeof(testAbbr)/sizeof(UScriptCode)){
161 const char* name = uscript_getShortName(testAbbr[i]);
162 numErrors=0;
163 if(strcmp(expectedAbbr[i],name)!=0){
164 log_err("Error getting abbreviations Got: %s Expected: %s\n",name,expectedAbbr[i]);
165 numErrors++;
166 }
167 if(numErrors > 0){
168 if(numErrors >0 ){
169 log_err("Errors uchar_getScriptAbbr() : %i \n",numErrors);
170 }
171 }
172 i++;
173 }
174
175 }
176 /* now test uscript_getScript() API */
177 {
178 #define MAX_ARRAY_SIZE 23
179 uint32_t codepoints[] = {
180 0x0000FF9D, /* USCRIPT_KATAKANA*/
181 0x0000FFBE, /* USCRIPT_HANGUL*/
182 0x0000FFC7, /* USCRIPT_HANGUL*/
183 0x0000FFCF, /* USCRIPT_HANGUL*/
184 0x0000FFD7, /* USCRIPT_HANGUL*/
185 0x0000FFDC, /* USCRIPT_HANGUL*/
186 0x00010300, /* USCRIPT_OLD_ITALIC*/
187 0x00010330, /* USCRIPT_GOTHIC*/
188 0x0001034A, /* USCRIPT_GOTHIC*/
189 0x00010400, /* USCRIPT_DESERET*/
190 0x00010428, /* USCRIPT_DESERET*/
191 0x0001D167, /* USCRIPT_INHERITED*/
192 0x0001D17B, /* USCRIPT_INHERITED*/
193 0x0001D185, /* USCRIPT_INHERITED*/
194 0x0001D1AA, /* USCRIPT_INHERITED*/
195 0x00020000, /* USCRIPT_HAN*/
196 0x00000D02, /* USCRIPT_MALAYALAM*/
197 0x00000D00, /* USCRIPT_COMMON */
198 0x00000000, /* USCRIPT_COMMON*/
199 0x0001D169, /* USCRIPT_INHERITED*/
200 0x0001D182, /* USCRIPT_INHERITED*/
201 0x0001D18B, /* USCRIPT_INHERITED*/
202 0x0001D1AD, /* USCRIPT_INHERITED*/
203 0x00110000, /* USCRIPT_INVALID_CODE */
204 };
205
206 UScriptCode expected[] = {
207 USCRIPT_KATAKANA ,
208 USCRIPT_HANGUL ,
209 USCRIPT_HANGUL ,
210 USCRIPT_HANGUL ,
211 USCRIPT_HANGUL ,
212 USCRIPT_HANGUL ,
213 USCRIPT_OLD_ITALIC,
214 USCRIPT_GOTHIC ,
215 USCRIPT_GOTHIC ,
216 USCRIPT_DESERET ,
217 USCRIPT_DESERET ,
218 USCRIPT_INHERITED,
219 USCRIPT_INHERITED,
220 USCRIPT_INHERITED,
221 USCRIPT_INHERITED,
222 USCRIPT_HAN ,
223 USCRIPT_MALAYALAM,
224 USCRIPT_COMMON,
225 USCRIPT_COMMON,
226 USCRIPT_INHERITED ,
227 USCRIPT_INHERITED ,
228 USCRIPT_INHERITED ,
229 USCRIPT_INHERITED ,
230 USCRIPT_INVALID_CODE,
231 };
232 UScriptCode code = USCRIPT_INVALID_CODE;
233 UErrorCode status = U_ZERO_ERROR;
234 UBool passed = TRUE;
235
236 i =0;
237 while(i< MAX_ARRAY_SIZE){
238 code = uscript_getScript(codepoints[i],&status);
239 if(U_SUCCESS(status)){
240 if( code != expected[i] ||
241 code != (UScriptCode)u_getIntPropertyValue(codepoints[i], UCHAR_SCRIPT)
242 ) {
243 log_err("uscript_getScript for codepoint \\U%08X failed\n",codepoints[i]);
244 passed = FALSE;
245 }
246 }else{
247 log_err("uscript_getScript for codepoint \\U%08X failed. Error: %s\n",
248 codepoints[i],u_errorName(status));
249 break;
250 }
251 i++;
252 }
253
254 if(passed==FALSE){
255 log_err("uscript_getScript failed.\n");
256 }
257 }
258 {
259 UScriptCode code= USCRIPT_INVALID_CODE;
260 UErrorCode status = U_ZERO_ERROR;
261 code = uscript_getScript(0x001D169,&status);
262 if(code != USCRIPT_INHERITED){
263 log_err("\\U001D169 is not contained in USCRIPT_INHERITED");
264 }
265 }
266 {
267 UScriptCode code= USCRIPT_INVALID_CODE;
268 UErrorCode status = U_ZERO_ERROR;
269 int32_t err = 0;
270
271 for(i = 0; i<=0x10ffff; i++){
272 code = uscript_getScript(i,&status);
273 if(code == USCRIPT_INVALID_CODE){
274 err++;
275 log_err("uscript_getScript for codepoint \\U%08X failed.\n", i);
276 }
277 }
278 if(err>0){
279 log_err("uscript_getScript failed for %d codepoints\n", err);
280 }
281 }
282 {
283 for(i=0; (UScriptCode)i< USCRIPT_CODE_LIMIT; i++){
284 const char* name = uscript_getName((UScriptCode)i);
285 if(name==NULL || strcmp(name,"")==0){
286 log_err("uscript_getName failed for code : %i\n",i);
287 }
288 }
289 }
290
291
292 }