]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/test/cintltst/custrtrn.c
ICU-6.2.14.tar.gz
[apple/icu.git] / icuSources / test / cintltst / custrtrn.c
... / ...
CommitLineData
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2001-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6/********************************************************************************
7*
8* File custrtrn.C
9*
10* Modification History:
11* Name Description
12* Ram String transformations test
13*********************************************************************************
14*/
15/****************************************************************************/
16
17
18#include <stdlib.h>
19#include <stdio.h>
20#include "unicode/utypes.h"
21#include "unicode/ustring.h"
22#include "unicode/ures.h"
23#include "ustr_imp.h"
24#include "cintltst.h"
25#include "cwchar.h"
26
27#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
28
29void addUCharTransformTest(TestNode** root);
30
31static void Test_UChar_UTF32_API(void);
32static void Test_UChar_UTF8_API(void);
33static void Test_UChar_WCHART_API(void);
34static void Test_widestrs(void);
35static void Test_WCHART_LongString(void);
36
37void
38addUCharTransformTest(TestNode** root)
39{
40 addTest(root, &Test_UChar_UTF32_API, "custrtrn/Test_UChar_UTF32_API");
41 addTest(root, &Test_UChar_UTF8_API, "custrtrn/Test_UChar_UTF8_API");
42 addTest(root, &Test_UChar_WCHART_API, "custrtrn/Test_UChar_WCHART_API");
43 addTest(root, &Test_widestrs, "custrtrn/Test_widestrs");
44 addTest(root, &Test_WCHART_LongString, "custrtrn/Test_WCHART_LongString");
45}
46
47static const UChar32 src32[]={
48 0x00A8, 0x3003, 0x3005, 0x2015, 0xFF5E, 0x2016, 0x2026, 0x2018, 0x000D, 0x000A,
49 0x2019, 0x201C, 0x201D, 0x3014, 0x3015, 0x3008, 0x3009, 0x300A, 0x000D, 0x000A,
50 0x300B, 0x300C, 0x300D, 0x300E, 0x300F, 0x3016, 0x3017, 0x3010, 0x000D, 0x000A,
51 0x3011, 0x00B1, 0x00D7, 0x00F7, 0x2236, 0x2227, 0x7FC1, 0x8956, 0x000D, 0x000A,
52 0x9D2C, 0x9D0E, 0x9EC4, 0x5CA1, 0x6C96, 0x837B, 0x5104, 0x5C4B, 0x000D, 0x000A,
53 0x61B6, 0x81C6, 0x6876, 0x7261, 0x4E59, 0x4FFA, 0x5378, 0x57F7, 0x000D, 0x000A,
54 0x57F4, 0x57F9, 0x57FA, 0x57FC, 0x5800, 0x5802, 0x5805, 0x5806, 0x000D, 0x000A,
55 0x580A, 0x581E, 0x6BB5, 0x6BB7, 0x6BBA, 0x6BBC, 0x9CE2, 0x977C, 0x000D, 0x000A,
56 0x6BBF, 0x6BC1, 0x6BC5, 0x6BC6, 0x6BCB, 0x6BCD, 0x6BCF, 0x6BD2, 0x000D, 0x000A,
57 0x6BD3, 0x6BD4, 0x6BD6, 0x6BD7, 0x6BD8, 0x6BDB, 0x6BEB, 0x6BEC, 0x000D, 0x000A,
58 0x6C05, 0x6C08, 0x6C0F, 0x6C11, 0x6C13, 0x6C23, 0x6C34, 0x0041, 0x000D, 0x000A,
59 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x000D, 0x000A,
60 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x000D, 0x000A,
61 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x000D, 0x000A,
62 0x005B, 0x9792, 0x9CCC, 0x9CCD, 0x9CCE, 0x9CCF, 0x9CD0, 0x9CD3, 0x000D, 0x000A,
63 0x9CD4, 0x9CD5, 0x9CD7, 0x9CD8, 0x9CD9, 0x9CDC, 0x9CDD, 0x9CDF, 0x000D, 0x000A,
64 0x9785, 0x9791, 0x00BD, 0x0390, 0x0385, 0x0386, 0x0388, 0x0389, 0x000D, 0x000A,
65 0x038E, 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x000D, 0x000A,
66 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x038A, 0x038C, 0x039C, 0x000D, 0x000A,
67 /* test non-BMP code points */
68 0x0002A699,
69 0x0002A69C, 0x0002A69D, 0x0002A69E, 0x0002A69F, 0x0002A6A0, 0x0002A6A5, 0x0002A6A6, 0x0002A6A7, 0x0002A6A8, 0x0002A6AB,
70 0x0002A6AC, 0x0002A6AD, 0x0002A6AE, 0x0002A6AF, 0x0002A6B0, 0x0002A6B1, 0x0002A6B3, 0x0002A6B5, 0x0002A6B6, 0x0002A6B7,
71 0x0002A6B8, 0x0002A6B9, 0x0002A6BA, 0x0002A6BB, 0x0002A6BC, 0x0002A6BD, 0x0002A6BE, 0x0002A6BF, 0x0002A6C0, 0x0002A6C1,
72 0x0002A6C2, 0x0002A6C3, 0x0002A6C4, 0x0002A6C8, 0x0002A6CA, 0x0002A6CB, 0x0002A6CD, 0x0002A6CE, 0x0002A6CF, 0x0002A6D0,
73 0x0002A6D1, 0x0002A6D2, 0x0002A6D3, 0x0002A6D4, 0x0002A6D5,
74
75 0x4DB3, 0x4DB4, 0x4DB5, 0x4E00, 0x4E00, 0x4E01, 0x4E02, 0x4E03, 0x000D, 0x000A,
76 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x33E0, 0x33E6, 0x000D, 0x000A,
77 0x4E05, 0x4E07, 0x4E04, 0x4E08, 0x4E08, 0x4E09, 0x4E0A, 0x4E0B, 0x000D, 0x000A,
78 0x4E0C, 0x0021, 0x0022, 0x0023, 0x0024, 0xFF40, 0xFF41, 0xFF42, 0x000D, 0x000A,
79 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47, 0xFF48, 0xFF49, 0xFF4A, 0x000D, 0x000A,0x0000
80};
81
82static const UChar src16[] = {
83 0x00A8, 0x3003, 0x3005, 0x2015, 0xFF5E, 0x2016, 0x2026, 0x2018, 0x000D, 0x000A,
84 0x2019, 0x201C, 0x201D, 0x3014, 0x3015, 0x3008, 0x3009, 0x300A, 0x000D, 0x000A,
85 0x300B, 0x300C, 0x300D, 0x300E, 0x300F, 0x3016, 0x3017, 0x3010, 0x000D, 0x000A,
86 0x3011, 0x00B1, 0x00D7, 0x00F7, 0x2236, 0x2227, 0x7FC1, 0x8956, 0x000D, 0x000A,
87 0x9D2C, 0x9D0E, 0x9EC4, 0x5CA1, 0x6C96, 0x837B, 0x5104, 0x5C4B, 0x000D, 0x000A,
88 0x61B6, 0x81C6, 0x6876, 0x7261, 0x4E59, 0x4FFA, 0x5378, 0x57F7, 0x000D, 0x000A,
89 0x57F4, 0x57F9, 0x57FA, 0x57FC, 0x5800, 0x5802, 0x5805, 0x5806, 0x000D, 0x000A,
90 0x580A, 0x581E, 0x6BB5, 0x6BB7, 0x6BBA, 0x6BBC, 0x9CE2, 0x977C, 0x000D, 0x000A,
91 0x6BBF, 0x6BC1, 0x6BC5, 0x6BC6, 0x6BCB, 0x6BCD, 0x6BCF, 0x6BD2, 0x000D, 0x000A,
92 0x6BD3, 0x6BD4, 0x6BD6, 0x6BD7, 0x6BD8, 0x6BDB, 0x6BEB, 0x6BEC, 0x000D, 0x000A,
93 0x6C05, 0x6C08, 0x6C0F, 0x6C11, 0x6C13, 0x6C23, 0x6C34, 0x0041, 0x000D, 0x000A,
94 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x000D, 0x000A,
95 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x000D, 0x000A,
96 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x000D, 0x000A,
97 0x005B, 0x9792, 0x9CCC, 0x9CCD, 0x9CCE, 0x9CCF, 0x9CD0, 0x9CD3, 0x000D, 0x000A,
98 0x9CD4, 0x9CD5, 0x9CD7, 0x9CD8, 0x9CD9, 0x9CDC, 0x9CDD, 0x9CDF, 0x000D, 0x000A,
99 0x9785, 0x9791, 0x00BD, 0x0390, 0x0385, 0x0386, 0x0388, 0x0389, 0x000D, 0x000A,
100 0x038E, 0x038F, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x000D, 0x000A,
101 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x038A, 0x038C, 0x039C, 0x000D, 0x000A,
102
103 /* test non-BMP code points */
104 0xD869, 0xDE99, 0xD869, 0xDE9C, 0xD869, 0xDE9D, 0xD869, 0xDE9E, 0xD869, 0xDE9F,
105 0xD869, 0xDEA0, 0xD869, 0xDEA5, 0xD869, 0xDEA6, 0xD869, 0xDEA7, 0xD869, 0xDEA8,
106 0xD869, 0xDEAB, 0xD869, 0xDEAC, 0xD869, 0xDEAD, 0xD869, 0xDEAE, 0xD869, 0xDEAF,
107 0xD869, 0xDEB0, 0xD869, 0xDEB1, 0xD869, 0xDEB3, 0xD869, 0xDEB5, 0xD869, 0xDEB6,
108 0xD869, 0xDEB7, 0xD869, 0xDEB8, 0xD869, 0xDEB9, 0xD869, 0xDEBA, 0xD869, 0xDEBB,
109 0xD869, 0xDEBC, 0xD869, 0xDEBD, 0xD869, 0xDEBE, 0xD869, 0xDEBF, 0xD869, 0xDEC0,
110 0xD869, 0xDEC1, 0xD869, 0xDEC2, 0xD869, 0xDEC3, 0xD869, 0xDEC4, 0xD869, 0xDEC8,
111 0xD869, 0xDECA, 0xD869, 0xDECB, 0xD869, 0xDECD, 0xD869, 0xDECE, 0xD869, 0xDECF,
112 0xD869, 0xDED0, 0xD869, 0xDED1, 0xD869, 0xDED2, 0xD869, 0xDED3, 0xD869, 0xDED4,
113 0xD869, 0xDED5,
114
115 0x4DB3, 0x4DB4, 0x4DB5, 0x4E00, 0x4E00, 0x4E01, 0x4E02, 0x4E03, 0x000D, 0x000A,
116 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x33E0, 0x33E6, 0x000D, 0x000A,
117 0x4E05, 0x4E07, 0x4E04, 0x4E08, 0x4E08, 0x4E09, 0x4E0A, 0x4E0B, 0x000D, 0x000A,
118 0x4E0C, 0x0021, 0x0022, 0x0023, 0x0024, 0xFF40, 0xFF41, 0xFF42, 0x000D, 0x000A,
119 0xFF43, 0xFF44, 0xFF45, 0xFF46, 0xFF47, 0xFF48, 0xFF49, 0xFF4A, 0x000D, 0x000A,0x0000
120};
121
122
123static void Test_UChar_UTF32_API(void){
124
125 UErrorCode err = U_ZERO_ERROR;
126 UChar uTemp[1];
127 UChar32 u32Temp[1];
128 UChar* uTarget=uTemp;
129 const UChar32* u32Src = src32;
130 int32_t u32SrcLen = sizeof(src32)/4;
131 int32_t uTargetLength = 0;
132 int32_t uDestLen=0;
133 const UChar* uSrc = src16;
134 int32_t uSrcLen = sizeof(src16)/2;
135 UChar32* u32Target = u32Temp;
136 uint32_t u32TargetLength =0;
137 int32_t u32DestLen =0;
138 UBool failed = FALSE;
139 int i= 0;
140 {
141 /* preflight */
142 u_strToUTF32(u32Target,u32TargetLength, &u32DestLen, uSrc, uSrcLen,&err);
143 if(err == U_BUFFER_OVERFLOW_ERROR){
144 err = U_ZERO_ERROR;
145 u32Target = (UChar32*) malloc (sizeof(uint32_t) * (u32DestLen+1));
146 u32TargetLength = u32DestLen+1;
147
148 u_strToUTF32(u32Target,u32TargetLength, &u32DestLen, uSrc, uSrcLen,&err);
149 }
150 else {
151 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
152 }
153 failed = FALSE;
154 /*for(i=0; i< u32DestLen; i++){
155 printf("0x%08X, ",uTarget[i]);
156 if(i%10==0){
157 printf("\n");
158 }
159 }*/
160 for(i=0; i< u32SrcLen; i++){
161 if(u32Target[i] != src32[i]){
162 log_verbose("u_strToUTF32() failed expected: \\U%08X got: \\U%08X at index: %i \n", src32[i], u32Target[i],i);
163 failed =TRUE;
164 }
165 }
166 if(failed){
167 log_err("u_strToUTF32() failed \n");
168 }
169
170 /* preflight */
171 u_strFromUTF32(uTarget,uTargetLength,&uDestLen,u32Src,u32SrcLen,&err);
172 if(err == U_BUFFER_OVERFLOW_ERROR){
173 err = U_ZERO_ERROR;
174 uTarget = (UChar*) malloc( sizeof(UChar) * (uDestLen+1));
175 uTargetLength = uDestLen+1;
176 u_strFromUTF32(uTarget,uTargetLength,&uDestLen,u32Src,u32SrcLen,&err);
177
178 }
179 /*for(i=0; i< uDestLen; i++){
180 printf("0x%04X, ",uTarget[i]);
181 if(i%10==0){
182 printf("\n");
183 }
184 }*/
185
186 for(i=0; i< uDestLen; i++){
187 if(uTarget[i] != src16[i]){
188 log_verbose("u_strFromUTF32() failed expected: \\U%08X got: \\U%08X at index: %i \n", src16[i] ,uTarget[i],i);
189 failed =TRUE;
190 }
191 }
192 if(failed){
193 log_err("u_strToUTF32() failed \n");
194 }
195
196 free(u32Target);
197 free(uTarget);
198 }
199 {
200 u32SrcLen = -1;
201 uTargetLength = 0;
202 uSrcLen =-1;
203 u32TargetLength=0;
204 failed = FALSE;
205
206 /* preflight */
207 u_strToUTF32(NULL,u32TargetLength, &u32DestLen, uSrc, uSrcLen,&err);
208 if(err == U_BUFFER_OVERFLOW_ERROR){
209 err = U_ZERO_ERROR;
210 u32Target = (UChar32*) malloc (sizeof(uint32_t) * (u32DestLen+1));
211 u32TargetLength = u32DestLen+1;
212
213 u_strToUTF32(u32Target,u32TargetLength, &u32DestLen, uSrc, uSrcLen,&err);
214 }
215 else {
216 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
217 }
218 failed = FALSE;
219
220 for(i=0; i< u32SrcLen; i++){
221 if(u32Target[i] != src32[i]){
222 log_verbose("u_strToUTF32() failed expected: \\U%08X got: \\U%08X \n", src32[i], u32Target[i]);
223 failed =TRUE;
224 }
225 }
226 if(failed){
227 log_err("u_strToUTF32() failed \n");
228 }
229
230 /* preflight */
231 u_strFromUTF32(NULL,uTargetLength,&uDestLen,u32Src,u32SrcLen,&err);
232 if(err == U_BUFFER_OVERFLOW_ERROR){
233 err = U_ZERO_ERROR;
234 uTarget = (UChar*) malloc( sizeof(UChar) * (uDestLen+1));
235 uTargetLength = uDestLen+1;
236 u_strFromUTF32(uTarget,uTargetLength,&uDestLen,u32Src,u32SrcLen,&err);
237
238 }
239
240 for(i=0; i< uDestLen; i++){
241 if(uTarget[i] != src16[i]){
242 log_verbose("u_strFromUTF32() failed expected: \\U%08X got: \\U%08X \n", src16[i] ,uTarget[i]);
243 failed =TRUE;
244 }
245 }
246 if(failed){
247 log_err("u_strToUTF32() failed \n");
248 }
249
250 free(u32Target);
251 free(uTarget);
252 }
253}
254
255
256static void Test_UChar_UTF8_API(void){
257
258 UErrorCode err = U_ZERO_ERROR;
259 UChar uTemp[1];
260 char u8Temp[1];
261 UChar* uTarget=uTemp;
262 const char* u8Src;
263 int32_t u8SrcLen = 0;
264 int32_t uTargetLength = 0;
265 int32_t uDestLen=0;
266 const UChar* uSrc = src16;
267 int32_t uSrcLen = sizeof(src16)/2;
268 char* u8Target = u8Temp;
269 int32_t u8TargetLength =0;
270 int32_t u8DestLen =0;
271 UBool failed = FALSE;
272 int i= 0;
273 {
274 /* preflight */
275 u_strToUTF8(u8Target,u8TargetLength, &u8DestLen, uSrc, uSrcLen,&err);
276 if(err == U_BUFFER_OVERFLOW_ERROR){
277 err = U_ZERO_ERROR;
278 u8Target = (char*) malloc (sizeof(uint8_t) * (u8DestLen+1));
279 u8TargetLength = u8DestLen;
280
281 u_strToUTF8(u8Target,u8TargetLength, &u8DestLen, uSrc, uSrcLen,&err);
282 if(U_FAILURE(err)){
283 log_err("u_strToUTF8 failed after preflight. Error: %s\n", u_errorName(err));
284 return;
285 }
286
287 }
288 else {
289 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
290 }
291 failed = FALSE;
292 /*for(i=0; i< u8DestLen; i++){
293 printf("0x%04X, ",u8Target[i]);
294 if(i%10==0){
295 printf("\n");
296 }
297 }*/
298 /*for(i=0; i< u8DestLen; i++){
299 if(u8Target[i] != src8[i]){
300 log_verbose("u_strToUTF8() failed expected: \\U%08X got: \\U%08X \n", src8[i], u8Target[i]);
301 failed =TRUE;
302 }
303 }
304 if(failed){
305 log_err("u_strToUTF8() failed \n");
306 }*/
307 u8Src = u8Target;
308 u8SrcLen = u8DestLen;
309
310 /* preflight */
311 u_strFromUTF8(uTarget,uTargetLength,&uDestLen,u8Src,u8SrcLen,&err);
312 if(err == U_BUFFER_OVERFLOW_ERROR){
313 err = U_ZERO_ERROR;
314 uTarget = (UChar*) malloc( sizeof(UChar) * (uDestLen+1));
315 uTargetLength = uDestLen;
316
317 u_strFromUTF8(uTarget,uTargetLength,&uDestLen,u8Src,u8SrcLen,&err);
318 }
319 else {
320 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
321 }
322 /*for(i=0; i< uDestLen; i++){
323 printf("0x%04X, ",uTarget[i]);
324 if(i%10==0){
325 printf("\n");
326 }
327 }*/
328
329 for(i=0; i< uSrcLen; i++){
330 if(uTarget[i] != src16[i]){
331 log_verbose("u_strFromUTF8() failed expected: \\u%04X got: \\u%04X at index: %i \n", src16[i] ,uTarget[i],i);
332 failed =TRUE;
333 }
334 }
335 if(failed){
336 log_err("u_strToUTF8() failed \n");
337 }
338
339 free(u8Target);
340 free(uTarget);
341 }
342 {
343 u8SrcLen = -1;
344 uTargetLength = 0;
345 uSrcLen =-1;
346 u8TargetLength=0;
347 failed = FALSE;
348 /* preflight */
349 u_strToUTF8(NULL,u8TargetLength, &u8DestLen, uSrc, uSrcLen,&err);
350 if(err == U_BUFFER_OVERFLOW_ERROR){
351 err = U_ZERO_ERROR;
352 u8Target = (char*) malloc (sizeof(uint8_t) * (u8DestLen+1));
353 u8TargetLength = u8DestLen;
354
355 u_strToUTF8(u8Target,u8TargetLength, &u8DestLen, uSrc, uSrcLen,&err);
356
357 }
358 else {
359 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
360 }
361 failed = FALSE;
362 /*for(i=0; i< u8DestLen; i++){
363 printf("0x%04X, ",u8Target[i]);
364 if(i%10==0){
365 printf("\n");
366 }
367 }*/
368 /*for(i=0; i< u8DestLen; i++){
369 if(u8Target[i] != src8[i]){
370 log_verbose("u_strToUTF8() failed expected: \\U%08X got: \\U%08X \n", src8[i], u8Target[i]);
371 failed =TRUE;
372 }
373 }
374 if(failed){
375 log_err("u_strToUTF8() failed \n");
376 }*/
377 u8Src = u8Target;
378 u8SrcLen = u8DestLen;
379
380 /* preflight */
381 u_strFromUTF8(NULL,uTargetLength,&uDestLen,u8Src,u8SrcLen,&err);
382 if(err == U_BUFFER_OVERFLOW_ERROR){
383 err = U_ZERO_ERROR;
384 uTarget = (UChar*) malloc( sizeof(UChar) * (uDestLen+1));
385 uTargetLength = uDestLen;
386
387 u_strFromUTF8(uTarget,uTargetLength,&uDestLen,u8Src,u8SrcLen,&err);
388 }
389 else {
390 log_err("Should have gotten U_BUFFER_OVERFLOW_ERROR");
391 }
392 /*for(i=0; i< uDestLen; i++){
393 printf("0x%04X, ",uTarget[i]);
394 if(i%10==0){
395 printf("\n");
396 }
397 }*/
398
399 for(i=0; i< uSrcLen; i++){
400 if(uTarget[i] != src16[i]){
401 log_verbose("u_strFromUTF8() failed expected: \\u%04X got: \\u%04X at index: %i \n", src16[i] ,uTarget[i],i);
402 failed =TRUE;
403 }
404 }
405 if(failed){
406 log_err("u_strToUTF8() failed \n");
407 }
408
409 free(u8Target);
410 free(uTarget);
411 }
412
413 /* test UTF-8 with single surrogates - illegal in Unicode 3.2 */
414 {
415 static const UChar
416 withLead16[]={ 0x1800, 0xd89a, 0x0061 },
417 withTrail16[]={ 0x1800, 0xdcba, 0x0061, 0 };
418 static const uint8_t
419 withLead8[]={ 0xe1, 0xa0, 0x80, 0xed, 0xa2, 0x9a, 0x61 },
420 withTrail8[]={ 0xe1, 0xa0, 0x80, 0xed, 0xb2, 0xba, 0x61 };
421 UChar out16[10];
422 char out8[10];
423
424 if(
425 (err=U_ZERO_ERROR, u_strToUTF8(out8, LENGTHOF(out8), NULL, withLead16, LENGTHOF(withLead16), &err), err!=U_INVALID_CHAR_FOUND) ||
426 (err=U_ZERO_ERROR, u_strToUTF8(out8, LENGTHOF(out8), NULL, withTrail16, -1, &err), err!=U_INVALID_CHAR_FOUND) ||
427 (err=U_ZERO_ERROR, u_strFromUTF8(out16, LENGTHOF(out16), NULL, (const char *)withLead8, LENGTHOF(withLead8), &err), err!=U_INVALID_CHAR_FOUND) ||
428 (err=U_ZERO_ERROR, u_strFromUTF8(out16, LENGTHOF(out16), NULL, (const char *)withTrail8, -1, &err), err!=U_INVALID_CHAR_FOUND)
429 ) {
430 log_err("error: u_strTo/FromUTF8(string with single surrogate) fails to report error\n");
431 }
432 }
433}
434static const uint16_t src16j[] = {
435 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x000D, 0x000A,
436 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x000D, 0x000A,
437 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x000D, 0x000A,
438 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x000D, 0x000A,
439 0x0000,
440 /* Test only ASCII */
441
442};
443static const uint16_t src16WithNulls[] = {
444 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0000,
445 0x0048, 0x0049, 0x004A, 0x000D, 0x000A, 0x0000,
446 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0000,
447 0x0050, 0x0051, 0x0052, 0x000D, 0x000A, 0x0000,
448 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0000,
449 0x0058, 0x0059, 0x005A, 0x000D, 0x000A, 0x0000,
450 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0000,
451 0x0058, 0x0059, 0x005A, 0x000D, 0x000A, 0x0000,
452 /* test only ASCII */
453 /*
454 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD,
455 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7,
456 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1,
457 0x00C2, 0x00C3, 0x00C4, 0x00C5, 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB,
458 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5,
459 0x00D6, 0x00D7, 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF,
460 0x00E0, 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9,
461 0x0054, 0x0000 */
462
463};
464static void Test_UChar_WCHART_API(void){
465 UErrorCode err = U_ZERO_ERROR;
466 const UChar* uSrc = src16j;
467 int32_t uSrcLen = sizeof(src16j)/2;
468 wchar_t* wDest = NULL;
469 int32_t wDestLen = 0;
470 int32_t reqLen= 0 ;
471 UBool failed = FALSE;
472 UChar* uDest = NULL;
473 int32_t uDestLen = 0;
474 int i =0;
475 {
476 /* pre-flight*/
477 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
478
479 if(err == U_BUFFER_OVERFLOW_ERROR){
480 err=U_ZERO_ERROR;
481 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
482 wDestLen = reqLen+1;
483 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
484 }
485
486 /* pre-flight */
487 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,reqLen,&err);
488
489
490 if(err == U_BUFFER_OVERFLOW_ERROR){
491 err =U_ZERO_ERROR;
492 uDest = (UChar*) malloc(sizeof(UChar) * (reqLen+1));
493 uDestLen = reqLen + 1;
494 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,reqLen,&err);
495 }else if(U_FAILURE(err)){
496
497 log_err("u_strFromWCS() failed. Error: %s \n", u_errorName(err));
498 return;
499 }
500
501 for(i=0; i< uSrcLen; i++){
502 if(uDest[i] != src16j[i]){
503 log_verbose("u_str*WCS() failed for unterminated string expected: \\u%04X got: \\u%04X at index: %i \n", src16j[i] ,uDest[i],i);
504 failed =TRUE;
505 }
506 }
507
508 if(U_FAILURE(err)){
509 failed = TRUE;
510 }
511 if(failed){
512 log_err("u_strToWCS() failed \n");
513 }
514 free(wDest);
515 free(uDest);
516
517
518 /* test with embeded nulls */
519 uSrc = src16WithNulls;
520 uSrcLen = sizeof(src16WithNulls)/2;
521 wDestLen =0;
522 uDestLen =0;
523 wDest = NULL;
524 uDest = NULL;
525 /* pre-flight*/
526 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
527
528 if(err == U_BUFFER_OVERFLOW_ERROR){
529 err=U_ZERO_ERROR;
530 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
531 wDestLen = reqLen+1;
532 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
533 }
534
535 /* pre-flight */
536 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,reqLen,&err);
537
538 if(err == U_BUFFER_OVERFLOW_ERROR){
539 err =U_ZERO_ERROR;
540 uDest = (UChar*) malloc(sizeof(UChar) * (reqLen+1));
541 uDestLen = reqLen + 1;
542 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,reqLen,&err);
543 }
544
545
546 for(i=0; i< uSrcLen; i++){
547 if(uDest[i] != src16WithNulls[i]){
548 log_verbose("u_str*WCS() failed for string with nulls expected: \\u%04X got: \\u%04X at index: %i \n", src16WithNulls[i] ,uDest[i],i);
549 failed =TRUE;
550 }
551 }
552
553 if(U_FAILURE(err)){
554 failed = TRUE;
555 }
556 if(failed){
557 log_err("u_strToWCS() failed \n");
558 }
559 free(wDest);
560 free(uDest);
561
562 }
563
564 {
565
566 uSrc = src16j;
567 uSrcLen = sizeof(src16j)/2;
568 wDestLen =0;
569 uDestLen =0;
570 wDest = NULL;
571 uDest = NULL;
572 wDestLen = 0;
573 /* pre-flight*/
574 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,-1,&err);
575
576 if(err == U_BUFFER_OVERFLOW_ERROR){
577 err=U_ZERO_ERROR;
578 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
579 wDestLen = reqLen+1;
580 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,-1,&err);
581 }
582 uDestLen = 0;
583 /* pre-flight */
584 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,-1,&err);
585
586 if(err == U_BUFFER_OVERFLOW_ERROR){
587 err =U_ZERO_ERROR;
588 uDest = (UChar*) malloc(sizeof(UChar) * (reqLen+1));
589 uDestLen = reqLen + 1;
590 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,-1,&err);
591 }
592
593
594 for(i=0; i< uSrcLen; i++){
595 if(uDest[i] != src16j[i]){
596 log_verbose("u_str*WCS() failed for null terminated string expected: \\u%04X got: \\u%04X at index: %i \n", src16j[i] ,uDest[i],i);
597 failed =TRUE;
598 }
599 }
600
601 if(U_FAILURE(err)){
602 failed = TRUE;
603 }
604 if(failed){
605 log_err("u_strToWCS() failed \n");
606 }
607 free(wDest);
608 free(uDest);
609 }
610
611 /*
612 * Test u_terminateWChars().
613 * All u_terminateXYZ() use the same implementation macro;
614 * we test this function to improve API coverage.
615 */
616 {
617 wchar_t buffer[10];
618
619 err=U_ZERO_ERROR;
620 buffer[3]=0x20ac;
621 wDestLen=u_terminateWChars(buffer, LENGTHOF(buffer), 3, &err);
622 if(err!=U_ZERO_ERROR || wDestLen!=3 || buffer[3]!=0) {
623 log_err("u_terminateWChars(buffer, all, 3, zero) failed: %s length %d [3]==U+%04x\n",
624 u_errorName(err), wDestLen, buffer[3]);
625 }
626
627 err=U_ZERO_ERROR;
628 buffer[3]=0x20ac;
629 wDestLen=u_terminateWChars(buffer, 3, 3, &err);
630 if(err!=U_STRING_NOT_TERMINATED_WARNING || wDestLen!=3 || buffer[3]!=0x20ac) {
631 log_err("u_terminateWChars(buffer, 3, 3, zero) failed: %s length %d [3]==U+%04x\n",
632 u_errorName(err), wDestLen, buffer[3]);
633 }
634
635 err=U_STRING_NOT_TERMINATED_WARNING;
636 buffer[3]=0x20ac;
637 wDestLen=u_terminateWChars(buffer, LENGTHOF(buffer), 3, &err);
638 if(err!=U_ZERO_ERROR || wDestLen!=3 || buffer[3]!=0) {
639 log_err("u_terminateWChars(buffer, all, 3, not-terminated) failed: %s length %d [3]==U+%04x\n",
640 u_errorName(err), wDestLen, buffer[3]);
641 }
642
643 err=U_ZERO_ERROR;
644 buffer[3]=0x20ac;
645 wDestLen=u_terminateWChars(buffer, 2, 3, &err);
646 if(err!=U_BUFFER_OVERFLOW_ERROR || wDestLen!=3 || buffer[3]!=0x20ac) {
647 log_err("u_terminateWChars(buffer, 2, 3, zero) failed: %s length %d [3]==U+%04x\n",
648 u_errorName(err), wDestLen, buffer[3]);
649 }
650 }
651}
652
653static void Test_widestrs()
654{
655 wchar_t ws[100];
656 UChar rts[100];
657 int32_t wcap = sizeof(ws) / sizeof(*ws);
658 int32_t wl;
659 int32_t rtcap = sizeof(rts) / sizeof(*rts);
660 int32_t rtl;
661 wchar_t *wcs;
662 UChar *cp;
663 const char *errname;
664 UChar ustr[] = {'h', 'e', 'l', 'l', 'o', 0};
665 int32_t ul = sizeof(ustr)/sizeof(*ustr) -1;
666 char astr[100];
667
668 UErrorCode err;
669
670 err = U_ZERO_ERROR;
671 wcs = u_strToWCS(ws, wcap, &wl, ustr, ul, &err);
672 if (U_FAILURE(err)) {
673 errname = u_errorName(err);
674 log_err("test_widestrs: u_strToWCS error: %s!\n",errname);
675 }
676 if(ul!=wl){
677 log_err("u_strToWCS: ustr = %s, ul = %d, ws = %S, wl = %d!\n", u_austrcpy(astr, ustr), ul, ws, wl);
678 }
679 err = U_ZERO_ERROR;
680 wl = (int32_t)uprv_wcslen(wcs);
681 cp = u_strFromWCS(rts, rtcap, &rtl, wcs, wl, &err);
682 if (U_FAILURE(err)) {
683 errname = u_errorName(err);
684 fprintf(stderr, "test_widestrs: ucnv_wcstombs error: %s!\n",errname);
685 }
686 if(wl != rtl){
687 log_err("u_strFromWCS: wcs = %S, wl = %d,rts = %s, rtl = %d!\n", wcs, wl, u_austrcpy(astr, rts), rtl);
688 }
689}
690
691static void
692Test_WCHART_LongString(){
693 UErrorCode status = U_ZERO_ERROR;
694 const char* testdatapath=loadTestData(&status);
695 UResourceBundle *theBundle = ures_open(testdatapath, "testtypes", &status);
696 int32_t strLen =0;
697 const UChar* str = ures_getStringByKey(theBundle, "testinclude",&strLen,&status);
698 const UChar* uSrc = str;
699 int32_t uSrcLen = strLen;
700 int32_t wDestLen =0, reqLen=0, i=0;
701 int32_t uDestLen =0;
702 wchar_t* wDest = NULL;
703 UChar* uDest = NULL;
704 UBool failed = FALSE;
705
706 if(U_FAILURE(status)){
707 log_err("Could not get testinclude resource from testtypes bundle. Error: %s\n",u_errorName(status));
708 return;
709 }
710
711 /* pre-flight*/
712 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,-1,&status);
713
714 if(status == U_BUFFER_OVERFLOW_ERROR){
715 status=U_ZERO_ERROR;
716 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
717 wDestLen = reqLen+1;
718 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,-1,&status);
719 }
720 uDestLen = 0;
721 /* pre-flight */
722 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,-1,&status);
723
724 if(status == U_BUFFER_OVERFLOW_ERROR){
725 status =U_ZERO_ERROR;
726 uDest = (UChar*) malloc(sizeof(UChar) * (reqLen+1));
727 uDestLen = reqLen + 1;
728 u_strFromWCS(uDest, uDestLen,&reqLen,wDest,-1,&status);
729 }
730
731
732 for(i=0; i< uSrcLen; i++){
733 if(uDest[i] != str[i]){
734 log_verbose("u_str*WCS() failed for null terminated string expected: \\u%04X got: \\u%04X at index: %i \n", src16j[i] ,uDest[i],i);
735 failed =TRUE;
736 }
737 }
738
739 if(U_FAILURE(status)){
740 failed = TRUE;
741 }
742 if(failed){
743 log_err("u_strToWCS() failed \n");
744 }
745 free(wDest);
746 free(uDest);
747 /* close the bundle */
748 ures_close(theBundle);
749
750}
751