VirtualBox

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

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

whole bunch: avoid runtime.h, include individual headers indead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 31.8 KB
Line 
1/* $Id: tstUtf8.cpp 14831 2008-11-30 10:31:16Z 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/initterm.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=%Rrc\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=%Rrc\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 %Rrc.\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 %Rrc.\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 %Rrc.\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 %Rrc.\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 %Rrc.\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 ;
201 if (pwsz[i] == 0 && i >= 8)
202 RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
203 else
204 {
205 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
206 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
207 g_cErrors++;
208 }
209 }
210 else
211 {
212 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n",
213 __LINE__, rc);
214 g_cErrors++;
215 }
216 }
217 else
218 {
219 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n",
220 __LINE__, rc);
221 g_cErrors++;
222 }
223
224 /*
225 * And again.
226 */
227 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
228 srand((unsigned)RTTimeNanoTS());
229 for (int i = 0; i < 30; i++)
230 pwszRand[i] = GetRandUtf16();
231 pwszRand[30] = 0;
232
233 RTUTF16 wszBuf[70];
234 PRTUTF16 pwsz2Buf = wszBuf;
235 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
236 if (rc == 0)
237 {
238 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
239 if (rc == 0)
240 {
241 int i;
242 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
243 ;
244 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
245 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
246 else
247 {
248 RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
249 RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
250 g_cErrors++;
251 }
252 }
253 else
254 {
255 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
256 __LINE__, rc);
257 g_cErrors++;
258 }
259 }
260 else
261 {
262 RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
263 __LINE__, rc);
264 g_cErrors++;
265 }
266 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
267 srand((unsigned)RTTimeNanoTS());
268 for (int i = 0; i < 30; i++)
269 pwszRand[i] = GetRandUtf16();
270 pwszRand[30] = 0;
271
272 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
273 if (rc == VERR_BUFFER_OVERFLOW)
274 RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
275 else
276 {
277 RTPrintf("tstUtf8: FAILURE - %d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
278 __LINE__, rc);
279 g_cErrors++;
280 }
281
282 /*
283 * last time...
284 */
285 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
286 srand((unsigned)RTTimeNanoTS());
287 for (int i = 0; i < 30; i++)
288 pwszRand[i] = GetRandUtf16();
289 pwszRand[30] = 0;
290
291 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
292 if (rc == VINF_SUCCESS)
293 {
294 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
295 if (rc == VERR_BUFFER_OVERFLOW)
296 RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
297 else
298 {
299 RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Rrc instead of VERR_BUFFER_OVERFLOW.\n",
300 __LINE__, rc);
301 g_cErrors++;
302 }
303 }
304 else
305 {
306 RTPrintf("tstUtf8: FAILURE - %d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
307 __LINE__, rc);
308 g_cErrors++;
309 }
310
311}
312
313
314static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
315static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
316static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
317
318static void whereami(int cBits, size_t off)
319{
320 if (cBits == 8)
321 {
322 if (off < 0x7f)
323 RTPrintf("UTF-8 U+%#x\n", off + 1);
324 else if (off < 0xf7f)
325 RTPrintf("UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
326 else if (off < 0x27f7f)
327 RTPrintf("UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
328 else if (off < 0x2df79)
329 RTPrintf("UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
330 else if (off < 0x42df79)
331 RTPrintf("UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
332 else
333 RTPrintf("UTF-8 ???\n");
334 }
335 else if (cBits == 16)
336 {
337 if (off < 0xd7ff*2)
338 RTPrintf("UTF-16 U+%#x\n", off / 2 + 1);
339 else if (off < 0xf7fd*2)
340 RTPrintf("UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
341 else if (off < 0x20f7fd)
342 RTPrintf("UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
343 else
344 RTPrintf("UTF-16 ???\n");
345 }
346 else
347 {
348 if (off < (0xd800 - 1) * sizeof(RTUNICP))
349 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
350 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
351 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
352 else
353 RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
354 }
355}
356
357int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
358{
359 const uint8_t *pb1 = (const uint8_t *)pv1;
360 const uint8_t *pb2 = (const uint8_t *)pv2;
361 for (size_t off = 0; off < cb; off++)
362 {
363 if (pb1[off] != pb2[off])
364 {
365 RTPrintf("mismatch at %#x: ", off);
366 whereami(cBits, off);
367 RTPrintf(" %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
368 RTPrintf("*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
369 RTPrintf(" %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
370 RTPrintf(" %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
371 RTPrintf(" %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
372 RTPrintf(" %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
373 RTPrintf(" %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
374 RTPrintf(" %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
375 RTPrintf(" %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
376 RTPrintf(" %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
377 RTPrintf(" %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
378 return 1;
379 }
380 }
381 return 0;
382}
383
384
385void InitStrings(void)
386{
387 /*
388 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
389 */
390 /* the simple code point array first */
391 unsigned i = 0;
392 RTUNICP uc = 1;
393 while (uc < 0xd800)
394 g_uszAll[i++] = uc++;
395 uc = 0xe000;
396 while (uc < 0xfffe)
397 g_uszAll[i++] = uc++;
398 uc = 0x10000;
399 while (uc < 0x110000)
400 g_uszAll[i++] = uc++;
401 g_uszAll[i++] = 0;
402 Assert(RT_ELEMENTS(g_uszAll) == i);
403
404 /* the utf-16 one */
405 i = 0;
406 uc = 1;
407 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
408 while (uc < 0xd800)
409 g_wszAll[i++] = uc++;
410 uc = 0xe000;
411 //RTPrintf(" %#x=%#x", i, uc);
412 while (uc < 0xfffe)
413 g_wszAll[i++] = uc++;
414 uc = 0x10000;
415 //RTPrintf(" %#x=%#x", i, uc);
416 while (uc < 0x110000)
417 {
418 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
419 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
420 uc++;
421 }
422 //RTPrintf(" %#x=%#x\n", i, uc);
423 g_wszAll[i++] = '\0';
424 Assert(RT_ELEMENTS(g_wszAll) == i);
425
426 /*
427 * The utf-8 one
428 */
429 i = 0;
430 uc = 1;
431 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
432 while (uc < 0x80)
433 g_szAll[i++] = uc++;
434 //RTPrintf(" %#x=%#x", i, uc);
435 while (uc < 0x800)
436 {
437 g_szAll[i++] = 0xc0 | (uc >> 6);
438 g_szAll[i++] = 0x80 | (uc & 0x3f);
439 Assert(!((uc >> 6) & ~0x1f));
440 uc++;
441 }
442 //RTPrintf(" %#x=%#x", i, uc);
443 while (uc < 0xd800)
444 {
445 g_szAll[i++] = 0xe0 | (uc >> 12);
446 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
447 g_szAll[i++] = 0x80 | (uc & 0x3f);
448 Assert(!((uc >> 12) & ~0xf));
449 uc++;
450 }
451 uc = 0xe000;
452 //RTPrintf(" %#x=%#x", i, uc);
453 while (uc < 0xfffe)
454 {
455 g_szAll[i++] = 0xe0 | (uc >> 12);
456 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
457 g_szAll[i++] = 0x80 | (uc & 0x3f);
458 Assert(!((uc >> 12) & ~0xf));
459 uc++;
460 }
461 uc = 0x10000;
462 //RTPrintf(" %#x=%#x", i, uc);
463 while (uc < 0x110000)
464 {
465 g_szAll[i++] = 0xf0 | (uc >> 18);
466 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
467 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
468 g_szAll[i++] = 0x80 | (uc & 0x3f);
469 Assert(!((uc >> 18) & ~0x7));
470 uc++;
471 }
472 //RTPrintf(" %#x=%#x\n", i, uc);
473 g_szAll[i++] = '\0';
474 Assert(RT_ELEMENTS(g_szAll) == i);
475}
476
477
478void test2(void)
479{
480 RTPrintf("tstUtf8: TEST 2\n");
481
482 /*
483 * Convert to UTF-8 and back.
484 */
485 RTPrintf("tstUtf8: #1: UTF-16 -> UTF-8 -> UTF-16...\n");
486 char *pszUtf8;
487 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
488 if (rc == VINF_SUCCESS)
489 {
490 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
491 {
492 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 mismatch!\n");
493 g_cErrors++;
494 }
495
496 PRTUTF16 pwszUtf16;
497 rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
498 if (rc == VINF_SUCCESS)
499 {
500 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
501 {
502 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed compare!\n");
503 g_cErrors++;
504 }
505 RTUtf16Free(pwszUtf16);
506 }
507 else
508 {
509 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
510 g_cErrors++;
511 }
512 RTStrFree(pszUtf8);
513 }
514 else
515 {
516 RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
517 g_cErrors++;
518 }
519
520
521 /*
522 * Convert to UTF-16 and back. (just in case the above test fails)
523 */
524 RTPrintf("tstUtf8: #2: UTF-8 -> UTF-16 -> UTF-8...\n");
525 PRTUTF16 pwszUtf16;
526 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
527 if (rc == VINF_SUCCESS)
528 {
529 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
530 {
531 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed compare!\n");
532 g_cErrors++;
533 }
534
535 char *pszUtf8;
536 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
537 if (rc == VINF_SUCCESS)
538 {
539 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
540 {
541 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed compare!\n");
542 g_cErrors++;
543 }
544 RTStrFree(pszUtf8);
545 }
546 else
547 {
548 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
549 g_cErrors++;
550 }
551 RTUtf16Free(pwszUtf16);
552 }
553 else
554 {
555 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
556 g_cErrors++;
557 }
558
559 /*
560 * Convert UTF-8 to CPs.
561 */
562 PRTUNICP paCps;
563 rc = RTStrToUni(g_szAll, &paCps);
564 if (rc == VINF_SUCCESS)
565 {
566 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
567 {
568 RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
569 g_cErrors++;
570 }
571
572 size_t cCps;
573 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
574 if (rc == VINF_SUCCESS)
575 {
576 if (cCps != RT_ELEMENTS(g_uszAll) - 1)
577 {
578 RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
579 g_cErrors++;
580 }
581 }
582 else
583 {
584 RTPrintf("tstUtf8: FAILURE - the full #3+: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
585 g_cErrors++;
586 }
587
588 /** @todo RTCpsToUtf8 or something. */
589 }
590 else
591 {
592 RTPrintf("tstUtf8: FAILURE - the full #3a: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
593 g_cErrors++;
594 }
595
596 /*
597 * Check the various string lengths.
598 */
599 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
600 size_t cuc2 = RTUtf16Len(g_wszAll);
601 if (cuc1 != cuc2)
602 {
603 RTPrintf("tstUtf8: FAILURE - cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
604 g_cErrors++;
605 }
606 //size_t cuc3 = RTUniLen(g_uszAll);
607
608
609 /*
610 * Enumerate the strings.
611 */
612 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
613 AssertRelease(pszPut1Base);
614 char *pszPut1 = pszPut1Base;
615 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
616 AssertRelease(pwszPut2Base);
617 PRTUTF16 pwszPut2 = pwszPut2Base;
618 const char *psz1 = g_szAll;
619 const char *psz2 = g_szAll;
620 PCRTUTF16 pwsz3 = g_wszAll;
621 PCRTUTF16 pwsz4 = g_wszAll;
622 for (;;)
623 {
624 /*
625 * getters
626 */
627 RTUNICP uc1;
628 rc = RTStrGetCpEx(&psz1, &uc1);
629 if (RT_FAILURE(rc))
630 {
631 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, psz2);
632 whereami(8, psz2 - &g_szAll[0]);
633 g_cErrors++;
634 break;
635 }
636 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
637 if (pszPrev1 != psz2)
638 {
639 RTPrintf("tstUtf8: FAILURE - RTStrPrevCp returned %p expected %p!\n", pszPrev1, psz2);
640 whereami(8, psz2 - &g_szAll[0]);
641 g_cErrors++;
642 break;
643 }
644 RTUNICP uc2 = RTStrGetCp(psz2);
645 if (uc2 != uc1)
646 {
647 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);
648 whereami(8, psz2 - &g_szAll[0]);
649 g_cErrors++;
650 break;
651 }
652 psz2 = RTStrNextCp(psz2);
653 if (psz2 != psz1)
654 {
655 RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetNext returned different next pointer!\n");
656 whereami(8, psz2 - &g_szAll[0]);
657 g_cErrors++;
658 break;
659 }
660
661 RTUNICP uc3;
662 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
663 if (RT_FAILURE(rc))
664 {
665 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, pwsz4);
666 whereami(16, pwsz4 - &g_wszAll[0]);
667 g_cErrors++;
668 break;
669 }
670 if (uc3 != uc2)
671 {
672 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);
673 whereami(16, pwsz4 - &g_wszAll[0]);
674 g_cErrors++;
675 break;
676 }
677 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
678 if (uc3 != uc4)
679 {
680 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);
681 whereami(16, pwsz4 - &g_wszAll[0]);
682 g_cErrors++;
683 break;
684 }
685 pwsz4 = RTUtf16NextCp(pwsz4);
686 if (pwsz4 != pwsz3)
687 {
688 RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");
689 whereami(8, pwsz4 - &g_wszAll[0]);
690 g_cErrors++;
691 break;
692 }
693
694
695 /*
696 * putters
697 */
698 pszPut1 = RTStrPutCp(pszPut1, uc1);
699 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
700 {
701 RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
702 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
703 whereami(8, psz2 - &g_szAll[0]);
704 g_cErrors++;
705 break;
706 }
707
708 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
709 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
710 {
711 RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
712 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
713 whereami(8, pwsz4 - &g_wszAll[0]);
714 g_cErrors++;
715 break;
716 }
717
718
719 /* the end? */
720 if (!uc1)
721 break;
722 }
723
724 /* check output if we seems to have made it thru it all. */
725 if (psz2 == &g_szAll[sizeof(g_szAll)])
726 {
727 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
728 {
729 RTPrintf("tstUtf8: FAILURE - RTStrPutCp encoded the string incorrectly.\n");
730 g_cErrors++;
731 }
732 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
733 {
734 RTPrintf("tstUtf8: FAILURE - RTUtf16PutCp encoded the string incorrectly.\n");
735 g_cErrors++;
736 }
737 }
738
739 RTMemFree(pszPut1Base);
740 RTMemFree(pwszPut2Base);
741}
742
743
744/**
745 * Check case insensitivity.
746 */
747void test3(void)
748{
749 RTPrintf("tstUtf8: TEST 3\n");
750
751 if ( RTUniCpToLower('a') != 'a'
752 || RTUniCpToLower('A') != 'a'
753 || RTUniCpToLower('b') != 'b'
754 || RTUniCpToLower('B') != 'b'
755 || RTUniCpToLower('Z') != 'z'
756 || RTUniCpToLower('z') != 'z'
757 || RTUniCpToUpper('c') != 'C'
758 || RTUniCpToUpper('C') != 'C'
759 || RTUniCpToUpper('z') != 'Z'
760 || RTUniCpToUpper('Z') != 'Z')
761 {
762 RTPrintf("tstUtf8: FAILURE - RTUniToUpper/Lower failed basic tests.\n");
763 g_cErrors++;
764 }
765
766 if (RTUtf16ICmp(g_wszAll, g_wszAll))
767 {
768 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the basic test.\n");
769 g_cErrors++;
770 }
771
772 if (RTUtf16Cmp(g_wszAll, g_wszAll))
773 {
774 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the basic test.\n");
775 g_cErrors++;
776 }
777
778 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 };
779 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 };
780 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
781 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
782 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
783 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
784 )
785 {
786 RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the alphabet test.\n");
787 g_cErrors++;
788 }
789
790 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
791 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
792 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
793 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
794 )
795 {
796 RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the alphabet test.\n");
797 g_cErrors++;
798 }
799}
800
801
802/**
803 * Test the RTStr*Cmp functions.
804 */
805void TstRTStrXCmp(void)
806{
807 RTPrintf("tstUtf8: TEST 4 - RTStr*Cmp\n");
808#define CHECK_DIFF(expr, op) \
809 do \
810 { \
811 int iDiff = expr; \
812 if (!(iDiff op 0)) \
813 { \
814 RTPrintf("tstUtf8(%d): failure - %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
815 g_cErrors++; \
816 } \
817 } while (0)
818
819/** @todo test the non-ascii bits. */
820
821 CHECK_DIFF(RTStrCmp(NULL, NULL), == );
822 CHECK_DIFF(RTStrCmp(NULL, ""), < );
823 CHECK_DIFF(RTStrCmp("", NULL), > );
824 CHECK_DIFF(RTStrCmp("", ""), == );
825 CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
826 CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
827 CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
828 CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
829 CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
830 CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
831 CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
832
833
834 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
835 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
836 CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
837 CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
838 CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
839 CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
840 CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
841 CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
842 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
843 CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
844 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
845
846 CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
847 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
848 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
849
850
851 CHECK_DIFF(RTStrICmp(NULL, NULL), == );
852 CHECK_DIFF(RTStrICmp(NULL, ""), < );
853 CHECK_DIFF(RTStrICmp("", NULL), > );
854 CHECK_DIFF(RTStrICmp("", ""), == );
855 CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
856 CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
857 CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
858 CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
859 CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
860
861 CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
862 CHECK_DIFF(RTStrICmp("abcdef", "abcdeF"), ==);
863 CHECK_DIFF(RTStrICmp("ABCDEF", "abcdef"), ==);
864 CHECK_DIFF(RTStrICmp("abcdef", "ABCDEF"), ==);
865 CHECK_DIFF(RTStrICmp("AbCdEf", "aBcDeF"), ==);
866 CHECK_DIFF(RTStrICmp("AbCdEg", "aBcDeF"), > );
867 CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
868
869
870
871 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
872 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
873 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
874 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
875 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
876 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
877 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
878 CHECK_DIFF(RTStrNICmp("", "", 0), == );
879 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
880 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
881 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
882 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
883 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
884
885 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
886 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
887 CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
888 CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
889 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
890 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
891 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
892
893 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
894 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
895 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
896 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
897 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
898 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
899 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
900 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
901 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
902 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
903}
904
905
906
907/**
908 * Benchmark stuff.
909 */
910void Benchmarks(void)
911{
912 RTPrintf("tstUtf8: BENCHMARKS\n");
913 static union
914 {
915 RTUTF16 wszBuf[sizeof(g_wszAll)];
916 char szBuf[sizeof(g_szAll)];
917 } s_Buf;
918
919 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
920 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
921 if (RT_SUCCESS(rc))
922 {
923 int i;
924 uint64_t u64Start = RTTimeNanoTS();
925 for (i = 0; i < 100; i++)
926 {
927 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
928 if (RT_FAILURE(rc))
929 {
930 RTPrintf("tstUtf8: UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
931 break;
932 }
933 }
934 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
935 RTPrintf("tstUtf8: UTF-8 -> UTF-16: %d in %RI64ns\n", i, u64Elapsed);
936 }
937
938 char *psz = &s_Buf.szBuf[0];
939 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
940 if (RT_SUCCESS(rc))
941 {
942 int i;
943 uint64_t u64Start = RTTimeNanoTS();
944 for (i = 0; i < 100; i++)
945 {
946 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
947 if (RT_FAILURE(rc))
948 {
949 RTPrintf("tstUtf8: UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
950 break;
951 }
952 }
953 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
954 RTPrintf("tstUtf8: UTF-16 -> UTF-8: %d in %RI64ns\n", i, u64Elapsed);
955 }
956
957}
958
959
960int main()
961{
962 RTR3Init();
963
964 InitStrings();
965 test1();
966 test2();
967 test3();
968 TstRTStrXCmp();
969 Benchmarks();
970
971 /*
972 * Summary
973 */
974 if (!g_cErrors)
975 RTPrintf("tstUtf8: SUCCESS\n");
976 else
977 RTPrintf("tstUtf8: FAILURE - %d errors!\n", g_cErrors);
978
979 return !!g_cErrors;
980}
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