VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstUtf8.cpp@ 7418

Last change on this file since 7418 was 7418, checked in by vboxsync, 17 years ago

UCS-2 -> UTF-16.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 27.2 KB
Line 
1/* $Id: tstUtf8.cpp 7418 2008-03-10 16:01:58Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - UTF-8 and UTF-16 string conversions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/string.h>
31#include <iprt/uni.h>
32#include <iprt/runtime.h>
33#include <iprt/uuid.h>
34#include <iprt/time.h>
35#include <iprt/stream.h>
36#include <iprt/alloc.h>
37#include <iprt/assert.h>
38#include <iprt/err.h>
39
40#include <stdlib.h>
41
42
43/*******************************************************************************
44* Global Variables *
45*******************************************************************************/
46static int g_cErrors = 0;
47
48
49/**
50 * Generate a random codepoint for simple UTF-16 encoding.
51 */
52static RTUTF16 GetRandUtf16(void)
53{
54 RTUTF16 wc;
55 do
56 {
57 wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
58 } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
59 return wc;
60}
61
62
63/**
64 *
65 */
66static void test1(void)
67{
68 static const char s_szBadString1[] = "Bad \xe0\x13\x0";
69 static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
70 int rc;
71 char *pszUtf8;
72 char *pszCurrent;
73 PRTUTF16 pwsz;
74 PRTUTF16 pwszRand;
75
76 RTPrintf("tstUtf8: TEST 1\n");
77
78 /*
79 * Invalid UTF-8 to UCS-2 test.
80 */
81 rc = RTStrToUtf16(s_szBadString1, &pwsz);
82 if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
83 {
84 RTPrintf("tstUtf8: FAILURE - %d: Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
85 __LINE__, rc);
86 g_cErrors++;
87 }
88 rc = RTStrToUtf16(s_szBadString2, &pwsz);
89 if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
90 {
91 RTPrintf("tstUtf8: FAILURE - %d: Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
92 __LINE__, rc);
93 g_cErrors++;
94 }
95
96 /*
97 * Test current CP convertion.
98 */
99 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
100 srand((unsigned)RTTimeNanoTS());
101 for (int i = 0; i < 30; i++)
102 pwszRand[i] = GetRandUtf16();
103 pwszRand[30] = 0;
104
105 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
106 if (rc == VINF_SUCCESS)
107 {
108 rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
109 if (rc == VINF_SUCCESS)
110 {
111 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
112 if (rc == VINF_SUCCESS)
113 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
114 else
115 {
116 RTPrintf("tstUtf8: FAILURE - %d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
117 __LINE__, rc);
118 g_cErrors++;
119 }
120 }
121 else if (rc == VERR_NO_TRANSLATION)
122 RTPrintf("tstUtf8: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n");
123 else
124 {
125 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
126 __LINE__, rc);
127 g_cErrors++;
128 }
129 }
130 else
131 {
132 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
133 __LINE__, rc);
134 g_cErrors++;
135 }
136
137 /*
138 * Generate a new random string.
139 */
140 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
141 srand((unsigned)RTTimeNanoTS());
142 for (int i = 0; i < 30; i++)
143 pwszRand[i] = GetRandUtf16();
144 pwszRand[30] = 0;
145 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
146 if (rc == VINF_SUCCESS)
147 {
148 rc = RTStrToUtf16(pszUtf8, &pwsz);
149 if (rc == VINF_SUCCESS)
150 {
151 int i;
152 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
153 /* nothing */;
154 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
155 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
156 else
157 {
158 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.\n", __LINE__);
159 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
160 g_cErrors++;
161 }
162 }
163 else
164 {
165 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
166 __LINE__, rc);
167 g_cErrors++;
168 }
169 }
170 else
171 {
172 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
173 __LINE__, rc);
174 g_cErrors++;
175 }
176
177 /*
178 * Generate yet another random string and convert it to a buffer.
179 */
180 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
181 srand((unsigned)RTTimeNanoTS());
182 for (int i = 0; i < 30; i++)
183 pwszRand[i] = GetRandUtf16();
184 pwszRand[30] = 0;
185
186 char szUtf8Array[120];
187 char *pszUtf8Array = szUtf8Array;
188 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
189 if (rc == 0)
190 {
191 rc = RTStrToUtf16(pszUtf8Array, &pwsz);
192 if (rc == 0)
193 {
194 int i;
195 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++);
196 if (pwsz[i] == 0 && i >= 8)
197 RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
198 else
199 {
200 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
201 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
202 g_cErrors++;
203 }
204 }
205 else
206 {
207 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
208 __LINE__, rc);
209 g_cErrors++;
210 }
211 }
212 else
213 {
214 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
215 __LINE__, rc);
216 g_cErrors++;
217 }
218
219 /*
220 * And again.
221 */
222 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
223 srand((unsigned)RTTimeNanoTS());
224 for (int i = 0; i < 30; i++)
225 pwszRand[i] = GetRandUtf16();
226 pwszRand[30] = 0;
227
228 RTUTF16 wszBuf[70];
229 PRTUTF16 pwsz2Buf = wszBuf;
230 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
231 if (rc == 0)
232 {
233 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
234 if (rc == 0)
235 {
236 int i;
237 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++);
238 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
239 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
240 else
241 {
242 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
243 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
244 g_cErrors++;
245 }
246 }
247 else
248 {
249 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
250 __LINE__, rc);
251 g_cErrors++;
252 }
253 }
254 else
255 {
256 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
257 __LINE__, rc);
258 g_cErrors++;
259 }
260 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
261 srand((unsigned)RTTimeNanoTS());
262 for (int i = 0; i < 30; i++)
263 pwszRand[i] = GetRandUtf16();
264 pwszRand[30] = 0;
265
266 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
267 if (rc == VERR_BUFFER_OVERFLOW)
268 RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
269 else
270 {
271 RTPrintf("tstUtf8: FAILURE - %d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
272 __LINE__, rc);
273 g_cErrors++;
274 }
275
276 /*
277 * last time...
278 */
279 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
280 srand((unsigned)RTTimeNanoTS());
281 for (int i = 0; i < 30; i++)
282 pwszRand[i] = GetRandUtf16();
283 pwszRand[30] = 0;
284
285 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
286 if (rc == VINF_SUCCESS)
287 {
288 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
289 if (rc == VERR_BUFFER_OVERFLOW)
290 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
291 else
292 {
293 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Vrc instead of VERR_BUFFER_OVERFLOW.\n",
294 __LINE__, rc);
295 g_cErrors++;
296 }
297 }
298 else
299 {
300 RTPrintf("tstUtf8: FAILURE - %d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
301 __LINE__, rc);
302 g_cErrors++;
303 }
304
305}
306
307
308static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
309static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
310static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
311
312static void whereami(int cBits, size_t off)
313{
314 if (cBits == 8)
315 {
316 if (off < 0x7f)
317 RTPrintf("UTF-8 U+%#x\n", off + 1);
318 else if (off < 0xf7f)
319 RTPrintf("UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
320 else if (off < 0x27f7f)
321 RTPrintf("UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
322 else if (off < 0x2df79)
323 RTPrintf("UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
324 else if (off < 0x42df79)
325 RTPrintf("UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
326 else
327 RTPrintf("UTF-8 ???\n");
328 }
329 else if (cBits == 16)
330 {
331 if (off < 0xd7ff*2)
332 RTPrintf("UTF-16 U+%#x\n", off / 2 + 1);
333 else if (off < 0xf7fd*2)
334 RTPrintf("UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
335 else if (off < 0x20f7fd)
336 RTPrintf("UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
337 else
338 RTPrintf("UTF-16 ???\n");
339 }
340 else
341 {
342 if (off < (0xd800 - 1) * sizeof(RTUNICP))
343 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
344 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
345 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
346 else
347 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
348 }
349}
350
351int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
352{
353 const uint8_t *pb1 = (const uint8_t *)pv1;
354 const uint8_t *pb2 = (const uint8_t *)pv2;
355 for (size_t off = 0; off < cb; off++)
356 {
357 if (pb1[off] != pb2[off])
358 {
359 RTPrintf("mismatch at %#x: ", off);
360 whereami(cBits, off);
361 RTPrintf(" %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
362 RTPrintf("*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
363 RTPrintf(" %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
364 RTPrintf(" %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
365 RTPrintf(" %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
366 RTPrintf(" %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
367 RTPrintf(" %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
368 RTPrintf(" %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
369 RTPrintf(" %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
370 RTPrintf(" %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
371 RTPrintf(" %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
372 return 1;
373 }
374 }
375 return 0;
376}
377
378
379void InitStrings(void)
380{
381 /*
382 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
383 */
384 /* the simple code point array first */
385 unsigned i = 0;
386 RTUNICP uc = 1;
387 while (uc < 0xd800)
388 g_uszAll[i++] = uc++;
389 uc = 0xe000;
390 while (uc < 0xfffe)
391 g_uszAll[i++] = uc++;
392 uc = 0x10000;
393 while (uc < 0x110000)
394 g_uszAll[i++] = uc++;
395 g_uszAll[i++] = 0;
396 Assert(ELEMENTS(g_uszAll) == i);
397
398 /* the utf-16 one */
399 i = 0;
400 uc = 1;
401 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
402 while (uc < 0xd800)
403 g_wszAll[i++] = uc++;
404 uc = 0xe000;
405 //RTPrintf(" %#x=%#x", i, uc);
406 while (uc < 0xfffe)
407 g_wszAll[i++] = uc++;
408 uc = 0x10000;
409 //RTPrintf(" %#x=%#x", i, uc);
410 while (uc < 0x110000)
411 {
412 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
413 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
414 uc++;
415 }
416 //RTPrintf(" %#x=%#x\n", i, uc);
417 g_wszAll[i++] = '\0';
418 Assert(ELEMENTS(g_wszAll) == i);
419
420 /*
421 * The utf-8 one
422 */
423 i = 0;
424 uc = 1;
425 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
426 while (uc < 0x80)
427 g_szAll[i++] = uc++;
428 //RTPrintf(" %#x=%#x", i, uc);
429 while (uc < 0x800)
430 {
431 g_szAll[i++] = 0xc0 | (uc >> 6);
432 g_szAll[i++] = 0x80 | (uc & 0x3f);
433 Assert(!((uc >> 6) & ~0x1f));
434 uc++;
435 }
436 //RTPrintf(" %#x=%#x", i, uc);
437 while (uc < 0xd800)
438 {
439 g_szAll[i++] = 0xe0 | (uc >> 12);
440 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
441 g_szAll[i++] = 0x80 | (uc & 0x3f);
442 Assert(!((uc >> 12) & ~0xf));
443 uc++;
444 }
445 uc = 0xe000;
446 //RTPrintf(" %#x=%#x", i, uc);
447 while (uc < 0xfffe)
448 {
449 g_szAll[i++] = 0xe0 | (uc >> 12);
450 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
451 g_szAll[i++] = 0x80 | (uc & 0x3f);
452 Assert(!((uc >> 12) & ~0xf));
453 uc++;
454 }
455 uc = 0x10000;
456 //RTPrintf(" %#x=%#x", i, uc);
457 while (uc < 0x110000)
458 {
459 g_szAll[i++] = 0xf0 | (uc >> 18);
460 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
461 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
462 g_szAll[i++] = 0x80 | (uc & 0x3f);
463 Assert(!((uc >> 18) & ~0x7));
464 uc++;
465 }
466 //RTPrintf(" %#x=%#x\n", i, uc);
467 g_szAll[i++] = '\0';
468 Assert(ELEMENTS(g_szAll) == i);
469}
470
471
472void test2(void)
473{
474 RTPrintf("tstUtf8: TEST 2\n");
475
476 /*
477 * Convert to UTF-8 and back.
478 */
479 RTPrintf("tstUtf8: #1: UTF-16 -> UTF-8 -> UTF-16...\n");
480 char *pszUtf8;
481 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
482 if (rc == VINF_SUCCESS)
483 {
484 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
485 {
486 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 mismatch!\n");
487 g_cErrors++;
488 }
489
490 PRTUTF16 pwszUtf16;
491 rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
492 if (rc == VINF_SUCCESS)
493 {
494 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
495 {
496 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed compare!\n");
497 g_cErrors++;
498 }
499 RTUtf16Free(pwszUtf16);
500 }
501 else
502 {
503 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
504 g_cErrors++;
505 }
506 RTStrFree(pszUtf8);
507 }
508 else
509 {
510 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
511 g_cErrors++;
512 }
513
514
515 /*
516 * Convert to UTF-16 and back. (just in case the above test fails)
517 */
518 RTPrintf("tstUtf8: #2: UTF-8 -> UTF-16 -> UTF-8...\n");
519 PRTUTF16 pwszUtf16;
520 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
521 if (rc == VINF_SUCCESS)
522 {
523 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
524 {
525 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed compare!\n");
526 g_cErrors++;
527 }
528
529 char *pszUtf8;
530 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
531 if (rc == VINF_SUCCESS)
532 {
533 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
534 {
535 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed compare!\n");
536 g_cErrors++;
537 }
538 RTStrFree(pszUtf8);
539 }
540 else
541 {
542 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
543 g_cErrors++;
544 }
545 RTUtf16Free(pwszUtf16);
546 }
547 else
548 {
549 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
550 g_cErrors++;
551 }
552
553 /*
554 * Convert UTF-8 to CPs.
555 */
556 PRTUNICP paCps;
557 rc = RTStrToUni(g_szAll, &paCps);
558 if (rc == VINF_SUCCESS)
559 {
560 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
561 {
562 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
563 g_cErrors++;
564 }
565
566 size_t cCps;
567 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, ELEMENTS(g_uszAll), &cCps);
568 if (rc == VINF_SUCCESS)
569 {
570 if (cCps != ELEMENTS(g_uszAll) - 1)
571 {
572 RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, ELEMENTS(g_uszAll) - 1);
573 g_cErrors++;
574 }
575 }
576 else
577 {
578 RTPrintf("tstUtf8: FAILURE - the full #3+: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
579 g_cErrors++;
580 }
581
582 /** @todo RTCpsToUtf8 or something. */
583 }
584 else
585 {
586 RTPrintf("tstUtf8: FAILURE - the full #3a: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
587 g_cErrors++;
588 }
589
590 /*
591 * Check the various string lengths.
592 */
593 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
594 size_t cuc2 = RTUtf16Len(g_wszAll);
595 if (cuc1 != cuc2)
596 {
597 RTPrintf("tstUtf8: FAILURE - cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
598 g_cErrors++;
599 }
600 //size_t cuc3 = RTUniLen(g_uszAll);
601
602
603 /*
604 * Enumerate the strings.
605 */
606 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
607 AssertRelease(pszPut1Base);
608 char *pszPut1 = pszPut1Base;
609 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
610 AssertRelease(pwszPut2Base);
611 PRTUTF16 pwszPut2 = pwszPut2Base;
612 const char *psz1 = g_szAll;
613 const char *psz2 = g_szAll;
614 PCRTUTF16 pwsz3 = g_wszAll;
615 PCRTUTF16 pwsz4 = g_wszAll;
616 for (;;)
617 {
618 /*
619 * getters
620 */
621 RTUNICP uc1;
622 rc = RTStrGetCpEx(&psz1, &uc1);
623 if (RT_FAILURE(rc))
624 {
625 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, psz2);
626 whereami(8, psz2 - &g_szAll[0]);
627 g_cErrors++;
628 break;
629 }
630 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
631 if (pszPrev1 != psz2)
632 {
633 RTPrintf("tstUtf8: FAILURE - RTStrPrevCp returned %p expected %p!\n", pszPrev1, psz2);
634 whereami(8, psz2 - &g_szAll[0]);
635 g_cErrors++;
636 break;
637 }
638 RTUNICP uc2 = RTStrGetCp(psz2);
639 if (uc2 != uc1)
640 {
641 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);
642 whereami(8, psz2 - &g_szAll[0]);
643 g_cErrors++;
644 break;
645 }
646 psz2 = RTStrNextCp(psz2);
647 if (psz2 != psz1)
648 {
649 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetNext returned different next pointer!\n");
650 whereami(8, psz2 - &g_szAll[0]);
651 g_cErrors++;
652 break;
653 }
654
655 RTUNICP uc3;
656 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
657 if (RT_FAILURE(rc))
658 {
659 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, pwsz4);
660 whereami(16, pwsz4 - &g_wszAll[0]);
661 g_cErrors++;
662 break;
663 }
664 if (uc3 != uc2)
665 {
666 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);
667 whereami(16, pwsz4 - &g_wszAll[0]);
668 g_cErrors++;
669 break;
670 }
671 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
672 if (uc3 != uc4)
673 {
674 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);
675 whereami(16, pwsz4 - &g_wszAll[0]);
676 g_cErrors++;
677 break;
678 }
679 pwsz4 = RTUtf16NextCp(pwsz4);
680 if (pwsz4 != pwsz3)
681 {
682 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");
683 whereami(8, pwsz4 - &g_wszAll[0]);
684 g_cErrors++;
685 break;
686 }
687
688
689 /*
690 * putters
691 */
692 pszPut1 = RTStrPutCp(pszPut1, uc1);
693 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
694 {
695 RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
696 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
697 whereami(8, psz2 - &g_szAll[0]);
698 g_cErrors++;
699 break;
700 }
701
702 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
703 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
704 {
705 RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
706 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
707 whereami(8, pwsz4 - &g_wszAll[0]);
708 g_cErrors++;
709 break;
710 }
711
712
713 /* the end? */
714 if (!uc1)
715 break;
716 }
717
718 /* check output if we seems to have made it thru it all. */
719 if (psz2 == &g_szAll[sizeof(g_szAll)])
720 {
721 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
722 {
723 RTPrintf("tstUtf8: FAILURE - RTStrPutCp encoded the string incorrectly.\n");
724 g_cErrors++;
725 }
726 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
727 {
728 RTPrintf("tstUtf8: FAILURE - RTUtf16PutCp encoded the string incorrectly.\n");
729 g_cErrors++;
730 }
731 }
732
733 RTMemFree(pszPut1Base);
734 RTMemFree(pwszPut2Base);
735}
736
737
738/**
739 * Check case insensitivity.
740 */
741void test3(void)
742{
743 RTPrintf("tstUtf8: TEST 3\n");
744
745 if ( RTUniCpToLower('a') != 'a'
746 || RTUniCpToLower('A') != 'a'
747 || RTUniCpToLower('b') != 'b'
748 || RTUniCpToLower('B') != 'b'
749 || RTUniCpToLower('Z') != 'z'
750 || RTUniCpToLower('z') != 'z'
751 || RTUniCpToUpper('c') != 'C'
752 || RTUniCpToUpper('C') != 'C'
753 || RTUniCpToUpper('z') != 'Z'
754 || RTUniCpToUpper('Z') != 'Z')
755 {
756 RTPrintf("tstUtf8: FAILURE - RTUniToUpper/Lower failed basic tests.\n");
757 g_cErrors++;
758 }
759
760 if (RTUtf16ICmp(g_wszAll, g_wszAll))
761 {
762 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the basic test.\n");
763 g_cErrors++;
764 }
765
766 if (RTUtf16Cmp(g_wszAll, g_wszAll))
767 {
768 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the basic test.\n");
769 g_cErrors++;
770 }
771
772 static RTUTF16 s_wszTst1a[] = { 'a', 'B', 'c', 'D', 'E', 'f', 'g', 'h', 'i', 'j', 'K', 'L', 'm', 'N', 'o', 'P', 'q', 'r', 'S', 't', 'u', 'V', 'w', 'x', 'Y', 'Z', 0xc5, 0xc6, 0xf8, 0 };
773 static RTUTF16 s_wszTst1b[] = { 'A', 'B', 'c', 'd', 'e', 'F', 'G', 'h', 'i', 'J', 'k', 'l', 'M', 'n', 'O', 'p', 'Q', 'R', 's', 't', 'U', 'v', 'w', 'X', 'y', 'z', 0xe5, 0xe6, 0xd8, 0 };
774 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
775 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
776 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
777 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
778 )
779 {
780 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the alphabet test.\n");
781 g_cErrors++;
782 }
783
784 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
785 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
786 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
787 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
788 )
789 {
790 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the alphabet test.\n");
791 g_cErrors++;
792 }
793}
794
795
796/**
797 * Benchmark stuff.
798 */
799void Benchmarks(void)
800{
801 RTPrintf("tstUtf8: BENCHMARKS\n");
802 static union
803 {
804 RTUTF16 wszBuf[sizeof(g_wszAll)];
805 char szBuf[sizeof(g_szAll)];
806 } s_Buf;
807
808 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
809 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, ELEMENTS(s_Buf.wszBuf), NULL);
810 if (RT_SUCCESS(rc))
811 {
812 int i;
813 uint64_t u64Start = RTTimeNanoTS();
814 for (i = 0; i < 100; i++)
815 {
816 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, ELEMENTS(s_Buf.wszBuf), NULL);
817 if (RT_FAILURE(rc))
818 {
819 RTPrintf("tstUtf8: UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
820 break;
821 }
822 }
823 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
824 RTPrintf("tstUtf8: UTF-8 -> UTF-16: %d in %RI64ns\n", i, u64Elapsed);
825 }
826
827 char *psz = &s_Buf.szBuf[0];
828 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, ELEMENTS(s_Buf.szBuf), NULL);
829 if (RT_SUCCESS(rc))
830 {
831 int i;
832 uint64_t u64Start = RTTimeNanoTS();
833 for (i = 0; i < 100; i++)
834 {
835 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, ELEMENTS(s_Buf.szBuf), NULL);
836 if (RT_FAILURE(rc))
837 {
838 RTPrintf("tstUtf8: UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
839 break;
840 }
841 }
842 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
843 RTPrintf("tstUtf8: UTF-16 -> UTF-8: %d in %RI64ns\n", i, u64Elapsed);
844 }
845
846}
847
848
849int main()
850{
851 RTR3Init(false);
852
853 InitStrings();
854 test1();
855 test2();
856 test3();
857 Benchmarks();
858
859 /*
860 * Summary
861 */
862 if (!g_cErrors)
863 RTPrintf("tstUtf8: SUCCESS\n");
864 else
865 RTPrintf("tstUtf8: FAILURE - %d errors!\n", g_cErrors);
866
867 return !!g_cErrors;
868}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette