• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **********************************************************************
3 * Copyright (C) 1998-2014, International Business Machines Corporation
4 * and others.  All Rights Reserved.
5 **********************************************************************
6 *
7 * File cstrtest.c
8 *
9 * Modification History:
10 *
11 *   Date        Name        Description
12 *   07/13/2000  Madhu         created
13 *******************************************************************************
14 */
15 
16 #include "unicode/ustring.h"
17 #include "unicode/ucnv.h"
18 #include "cstring.h"
19 #include "uinvchar.h"
20 #include "cintltst.h"
21 #include "cmemory.h"
22 
23 static void TestAPI(void);
24 void addCStringTest(TestNode** root);
25 
26 static void TestInvariant(void);
27 static void TestCompareInvEbcdicAsAscii(void);
28 
addCStringTest(TestNode ** root)29 void addCStringTest(TestNode** root) {
30     addTest(root, &TestAPI,   "tsutil/cstrtest/TestAPI");
31     addTest(root, &TestInvariant,   "tsutil/cstrtest/TestInvariant");
32     addTest(root, &TestCompareInvEbcdicAsAscii, "tsutil/cstrtest/TestCompareInvEbcdicAsAscii");
33 }
34 
TestAPI(void)35 static void TestAPI(void)
36 {
37     int32_t intValue=0;
38     char src[30]="HELLO THERE", dest[30];
39     static const char *const abc="abcdefghijklmnopqrstuvwxyz", *const ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
40     const char *temp;
41     int32_t i;
42 
43     log_verbose("Testing uprv_tolower() and uprv_toupper()\n");
44     for(i=0; i<=26; ++i) {
45         dest[i]=uprv_tolower(abc[i]);
46     }
47     if(0!=strcmp(abc, dest)) {
48         log_err("uprv_tolower(abc) failed\n");
49     }
50 
51     for(i=0; i<=26; ++i) {
52         dest[i]=uprv_tolower(ABC[i]);
53     }
54     if(0!=strcmp(abc, dest)) {
55         log_err("uprv_tolower(ABC) failed\n");
56     }
57 
58     for(i=0; i<=26; ++i) {
59         dest[i]=uprv_toupper(abc[i]);
60     }
61     if(0!=strcmp(ABC, dest)) {
62         log_err("uprv_toupper(abc) failed\n");
63     }
64 
65     for(i=0; i<=26; ++i) {
66         dest[i]=uprv_toupper(ABC[i]);
67     }
68     if(0!=strcmp(ABC, dest)) {
69         log_err("uprv_toupper(ABC) failed\n");
70     }
71 
72     log_verbose("Testing the API in cstring\n");
73     T_CString_toLowerCase(src);
74     if(uprv_strcmp(src, "hello there") != 0){
75         log_err("FAIL: *** T_CString_toLowerCase() failed. Expected: \"hello there\", Got: \"%s\"\n", src);
76     }
77     T_CString_toUpperCase(src);
78     if(uprv_strcmp(src, "HELLO THERE") != 0){
79         log_err("FAIL: *** T_CString_toUpperCase() failed. Expected: \"HELLO THERE\", Got: \"%s\"\n", src);
80     }
81 
82     intValue=T_CString_stringToInteger("34556", 10);
83     if(intValue != 34556){
84         log_err("FAIL: ****T_CString_stringToInteger(\"34556\", 10) failed. Expected: 34556, Got: %d\n", intValue);
85     }
86     intValue=T_CString_stringToInteger("100", 16);
87     if(intValue != 256){
88         log_err("FAIL: ****T_CString_stringToInteger(\"100\", 16) failed. Expected: 256, Got: %d\n", intValue);
89     }
90     i = T_CString_integerToString(src, 34556, 10);
91     if(uprv_strcmp(src, "34556") != 0 || i != 5){
92         log_err("FAIL: ****integerToString(src, 34566, 10); failed. Expected: \"34556\", Got: %s\n", src);
93     }
94     i = T_CString_integerToString(src, 431, 16);
95     if(uprv_stricmp(src, "1AF") != 0 || i != 3){
96         log_err("FAIL: ****integerToString(src, 431, 16); failed. Expected: \"1AF\", Got: %s\n", src);
97     }
98     i = T_CString_int64ToString(src, U_INT64_MAX, 10);
99     if(uprv_strcmp(src,  "9223372036854775807") != 0 || i != 19){
100         log_err("FAIL: ****integerToString(src, 9223372036854775807, 10); failed. Got: %s\n", src);
101     }
102     i = T_CString_int64ToString(src, U_INT64_MAX, 16);
103     if(uprv_stricmp(src, "7FFFFFFFFFFFFFFF") != 0 || i != 16){
104         log_err("FAIL: ****integerToString(src, 7FFFFFFFFFFFFFFF, 16); failed. Got: %s\n", src);
105     }
106 
107     uprv_strcpy(src, "this is lower case");
108     if(uprv_stricmp(src, "THIS is lower CASE") != 0){
109         log_err("FAIL: *****uprv_stricmp() failed.");
110     }
111     if((intValue=uprv_stricmp(NULL, "first string is null") )!= -1){
112         log_err("FAIL: uprv_stricmp() where the first string is null failed. Expected: -1, returned %d\n", intValue);
113     }
114     if((intValue=uprv_stricmp("second string is null", NULL)) != 1){
115         log_err("FAIL: uprv_stricmp() where the second string is null failed. Expected: 1, returned %d\n", intValue);
116     }
117     if((intValue=uprv_stricmp(NULL, NULL)) != 0){
118         log_err("FAIL: uprv_stricmp(NULL, NULL) failed.  Expected:  0, returned %d\n", intValue);;
119     }
120     if((intValue=uprv_stricmp("", "")) != 0){
121         log_err("FAIL: uprv_stricmp(\"\", \"\") failed.  Expected:  0, returned %d\n", intValue);;
122     }
123     if((intValue=uprv_stricmp("", "abc")) != -1){
124         log_err("FAIL: uprv_stricmp(\"\", \"abc\") failed.  Expected: -1, returned %d\n", intValue);
125     }
126     if((intValue=uprv_stricmp("abc", "")) != 1){
127         log_err("FAIL: uprv_stricmp(\"abc\", \"\") failed.  Expected: 1, returned %d\n", intValue);
128     }
129 
130     temp=uprv_strdup("strdup");
131     if(uprv_strcmp(temp, "strdup") !=0 ){
132         log_err("FAIL: uprv_strdup() failed. Expected: \"strdup\", Got: %s\n", temp);
133     }
134     uprv_free((char *)temp);
135 
136     uprv_strcpy(src, "this is lower case");
137     if(uprv_strnicmp(src, "THIS", 4 ) != 0){
138         log_err("FAIL: *****uprv_strnicmp() failed.");
139     }
140     if((intValue=uprv_strnicmp(NULL, "first string is null", 10) )!= -1){
141         log_err("FAIL: uprv_strnicmp() where the first string is null failed. Expected: -1, returned %d\n", intValue);
142     }
143     if((intValue=uprv_strnicmp("second string is null", NULL, 10)) != 1){
144         log_err("FAIL: uprv_strnicmp() where the second string is null failed. Expected: 1, returned %d\n", intValue);
145     }
146     if((intValue=uprv_strnicmp(NULL, NULL, 10)) != 0){
147         log_err("FAIL: uprv_strnicmp(NULL, NULL, 10) failed.  Expected:  0, returned %d\n", intValue);;
148     }
149     if((intValue=uprv_strnicmp("", "", 10)) != 0){
150         log_err("FAIL: uprv_strnicmp(\"\", \"\") failed.  Expected:  0, returned %d\n", intValue);;
151     }
152     if((intValue=uprv_strnicmp("", "abc", 10)) != -1){
153         log_err("FAIL: uprv_stricmp(\"\", \"abc\", 10) failed.  Expected: -1, returned %d\n", intValue);
154     }
155     if((intValue=uprv_strnicmp("abc", "", 10)) != 1){
156         log_err("FAIL: uprv_strnicmp(\"abc\", \"\", 10) failed.  Expected: 1, returned %d\n", intValue);
157     }
158 
159 }
160 
161 /* test invariant-character handling */
162 static void
TestInvariant()163 TestInvariant() {
164     /* all invariant graphic chars and some control codes (not \n!) */
165     const char invariantChars[]=
166         "\t\r \"%&'()*+,-./"
167         "0123456789:;<=>?"
168         "ABCDEFGHIJKLMNOPQRSTUVWXYZ_"
169         "abcdefghijklmnopqrstuvwxyz";
170 
171     const UChar invariantUChars[]={
172         9, 0xd, 0x20, 0x22, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
173         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
174         0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
175         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5f,
176         0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
177         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0
178     };
179 
180     const char variantChars[]="\n!#$@[\\]^`{|}~";
181 
182     const UChar variantUChars[]={
183         0x0a, 0x21, 0x23, 0x24, 0x40, 0x5b, 0x5c, 0x5d, 0x5e, 0x60, 0x7b, 0x7c, 0x7d, 0x7e, 0
184     };
185 
186     const UChar nonASCIIUChars[]={ 0x80, 0xa0, 0x900, 0xff51 };
187 
188     UChar us[120];
189     char cs[120];
190 
191     int32_t i, length;
192 
193     /* make sure that all invariant characters convert both ways */
194     length=sizeof(invariantChars);
195     u_charsToUChars(invariantChars, us, length);
196     if(u_strcmp(us, invariantUChars)!=0) {
197         log_err("u_charsToUChars(invariantChars) failed\n");
198     }
199 
200     u_UCharsToChars(invariantUChars, cs, length);
201     if(strcmp(cs, invariantChars)!=0) {
202         log_err("u_UCharsToChars(invariantUChars) failed\n");
203     }
204 
205 
206     /*
207      * make sure that variant characters convert from source code literals to Unicode
208      * but not back to char *
209      */
210     length=sizeof(variantChars);
211     u_charsToUChars(variantChars, us, length);
212     if(u_strcmp(us, variantUChars)!=0) {
213         log_err("u_charsToUChars(variantChars) failed\n");
214     }
215 
216 #ifdef NDEBUG
217     /*
218      * Test u_UCharsToChars(variantUChars) only in release mode because it will
219      * cause an assertion failure in debug builds.
220      */
221     u_UCharsToChars(variantUChars, cs, length);
222     for(i=0; i<length; ++i) {
223         if(cs[i]!=0) {
224             log_err("u_UCharsToChars(variantUChars) converted the %d-th character to %02x instead of 00\n", i, cs[i]);
225         }
226     }
227 #endif
228 
229     /*
230      * Verify that invariant characters roundtrip from Unicode to the
231      * default converter and back.
232      */
233     {
234         UConverter *cnv;
235         UErrorCode errorCode;
236 
237         errorCode=U_ZERO_ERROR;
238         cnv=ucnv_open(NULL, &errorCode);
239         if(U_FAILURE(errorCode)) {
240             log_err("unable to open the default converter\n");
241         } else {
242             length=ucnv_fromUChars(cnv, cs, sizeof(cs), invariantUChars, -1, &errorCode);
243             if(U_FAILURE(errorCode)) {
244                 log_err("ucnv_fromUChars(invariantUChars) failed - %s\n", u_errorName(errorCode));
245             } else if(length!=sizeof(invariantChars)-1 || strcmp(cs, invariantChars)!=0) {
246                 log_err("ucnv_fromUChars(invariantUChars) failed\n");
247             }
248 
249             errorCode=U_ZERO_ERROR;
250             length=ucnv_toUChars(cnv, us, UPRV_LENGTHOF(us), invariantChars, -1, &errorCode);
251             if(U_FAILURE(errorCode)) {
252                 log_err("ucnv_toUChars(invariantChars) failed - %s\n", u_errorName(errorCode));
253             } else if(length!=UPRV_LENGTHOF(invariantUChars)-1 || u_strcmp(us, invariantUChars)!=0) {
254                 log_err("ucnv_toUChars(invariantChars) failed\n");
255             }
256 
257             ucnv_close(cnv);
258         }
259     }
260 
261     /* API tests */
262     if(!uprv_isInvariantString(invariantChars, -1)) {
263         log_err("uprv_isInvariantString(invariantChars) failed\n");
264     }
265     if(!uprv_isInvariantUString(invariantUChars, -1)) {
266         log_err("uprv_isInvariantUString(invariantUChars) failed\n");
267     }
268     if(!uprv_isInvariantString(invariantChars+strlen(invariantChars), 1)) {
269         log_err("uprv_isInvariantString(\"\\0\") failed\n");
270     }
271 
272     for(i=0; i<(sizeof(variantChars)-1); ++i) {
273         if(uprv_isInvariantString(variantChars+i, 1)) {
274             log_err("uprv_isInvariantString(variantChars[%d]) failed\n", i);
275         }
276         if(uprv_isInvariantUString(variantUChars+i, 1)) {
277             log_err("uprv_isInvariantUString(variantUChars[%d]) failed\n", i);
278         }
279     }
280 
281     for(i=0; i<UPRV_LENGTHOF(nonASCIIUChars); ++i) {
282         if(uprv_isInvariantUString(nonASCIIUChars+i, 1)) {
283             log_err("uprv_isInvariantUString(nonASCIIUChars[%d]) failed\n", i);
284         }
285     }
286 }
287 
getSign(int32_t n)288 static int32_t getSign(int32_t n) {
289     if(n<0) {
290         return -1;
291     } else if(n==0) {
292         return 0;
293     } else {
294         return 1;
295     }
296 }
297 
298 static void
TestCompareInvEbcdicAsAscii()299 TestCompareInvEbcdicAsAscii() {
300     static const char *const invStrings[][2]={
301         /* invariant-character strings in ascending ASCII order */
302         /* EBCDIC       native */
303         { "",             "" },
304         { "\x6c",         "%" },
305         { "\xf0",         "0" },
306         { "\xf0\xf0",     "00" },
307         { "\xf0\xf0\x81", "00a" },
308         { "\x7e",         "=" },
309         { "\xc1",         "A" },
310         { "\xc1\xf0\xf0", "A00" },
311         { "\xc1\xf0\xf0", "A00" },
312         { "\xc1\xc1",     "AA" },
313         { "\xc1\xc1\xf0", "AA0" },
314         { "\x6d",         "_" },
315         { "\x81",         "a" },
316         { "\x81\xf0\xf0", "a00" },
317         { "\x81\xf0\xf0", "a00" },
318         { "\x81\x81",     "aa" },
319         { "\x81\x81\xf0", "aa0" },
320         { "\x81\x81\x81", "aaa" },
321         { "\x81\x81\x82", "aab" }
322     };
323     int32_t i;
324     for(i=1; i<UPRV_LENGTHOF(invStrings); ++i) {
325         int32_t diff1, diff2;
326         /* compare previous vs. current */
327         diff1=getSign(uprv_compareInvEbcdicAsAscii(invStrings[i-1][0], invStrings[i][0]));
328         if(diff1>0 || (diff1==0 && 0!=uprv_strcmp(invStrings[i-1][0], invStrings[i][0]))) {
329             log_err("uprv_compareInvEbcdicAsAscii(%s, %s)=%hd is wrong\n",
330                     invStrings[i-1][1], invStrings[i][1], (short)diff1);
331         }
332         /* compare current vs. previous, should be inverse diff */
333         diff2=getSign(uprv_compareInvEbcdicAsAscii(invStrings[i][0], invStrings[i-1][0]));
334         if(diff2!=-diff1) {
335             log_err("uprv_compareInvEbcdicAsAscii(%s, %s)=%hd is wrong\n",
336                     invStrings[i][1], invStrings[i-1][1], (short)diff2);
337         }
338     }
339 }
340