VirtualBox

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

Last change on this file since 8245 was 8245, checked in by vboxsync, 16 years ago

rebranding: IPRT files again.

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