VirtualBox

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

Last change on this file since 23644 was 23223, checked in by vboxsync, 15 years ago

API: big medium handling change and lots of assorted other cleanups and fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.3 KB
Line 
1/* $Id: tstUtf8.cpp 23223 2009-09-22 15:50:03Z 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#include <iprt/test.h>
44#include <iprt/ministring_cpp.h>
45
46#include <stdlib.h> /** @todo use our random. */
47
48
49
50/**
51 * Generate a random codepoint for simple UTF-16 encoding.
52 */
53static RTUTF16 GetRandUtf16(void)
54{
55 RTUTF16 wc;
56 do
57 {
58 wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
59 } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
60 return wc;
61}
62
63
64/**
65 *
66 */
67static void test1(RTTEST hTest)
68{
69 static const char s_szBadString1[] = "Bad \xe0\x13\x0";
70 static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
71 int rc;
72 char *pszUtf8;
73 char *pszCurrent;
74 PRTUTF16 pwsz;
75 PRTUTF16 pwszRand;
76
77 /*
78 * Invalid UTF-8 to UCS-2 test.
79 */
80 RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16");
81 rc = RTStrToUtf16(s_szBadString1, &pwsz);
82 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
83 (hTest, "Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
84 rc = RTStrToUtf16(s_szBadString2, &pwsz);
85 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
86 (hTest, "Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
87
88 /*
89 * Test current CP convertion.
90 */
91 RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
92 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
93 srand((unsigned)RTTimeNanoTS());
94 for (int i = 0; i < 30; i++)
95 pwszRand[i] = GetRandUtf16();
96 pwszRand[30] = 0;
97
98 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
99 if (rc == VINF_SUCCESS)
100 {
101 rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
102 if (rc == VINF_SUCCESS)
103 {
104 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
105 if (rc == VINF_SUCCESS)
106 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
107 else
108 RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
109 __LINE__, rc);
110 }
111 else if (rc == VERR_NO_TRANSLATION)
112 RTTestPassed(hTest, "The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n");
113 else
114 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
115 __LINE__, rc);
116 }
117 else
118 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
119 __LINE__, rc);
120
121 /*
122 * Generate a new random string.
123 */
124 RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16");
125 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
126 srand((unsigned)RTTimeNanoTS());
127 for (int i = 0; i < 30; i++)
128 pwszRand[i] = GetRandUtf16();
129 pwszRand[30] = 0;
130 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
131 if (rc == VINF_SUCCESS)
132 {
133 rc = RTStrToUtf16(pszUtf8, &pwsz);
134 if (rc == VINF_SUCCESS)
135 {
136 int i;
137 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
138 /* nothing */;
139 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
140 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
141 else
142 {
143 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__);
144 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
145 }
146 }
147 else
148 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
149 __LINE__, rc);
150 }
151 else
152 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
153 __LINE__, rc);
154
155 /*
156 * Generate yet another random string and convert it to a buffer.
157 */
158 RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16");
159 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
160 srand((unsigned)RTTimeNanoTS());
161 for (int i = 0; i < 30; i++)
162 pwszRand[i] = GetRandUtf16();
163 pwszRand[30] = 0;
164
165 char szUtf8Array[120];
166 char *pszUtf8Array = szUtf8Array;
167 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
168 if (rc == 0)
169 {
170 rc = RTStrToUtf16(pszUtf8Array, &pwsz);
171 if (rc == 0)
172 {
173 int i;
174 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
175 ;
176 if (pwsz[i] == 0 && i >= 8)
177 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
178 else
179 {
180 RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
181 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
182 }
183 }
184 else
185 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
186 }
187 else
188 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
189
190 /*
191 * And again.
192 */
193 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
194 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
195 srand((unsigned)RTTimeNanoTS());
196 for (int i = 0; i < 30; i++)
197 pwszRand[i] = GetRandUtf16();
198 pwszRand[30] = 0;
199
200 RTUTF16 wszBuf[70];
201 PRTUTF16 pwsz2Buf = wszBuf;
202 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
203 if (rc == 0)
204 {
205 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
206 if (rc == 0)
207 {
208 int i;
209 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
210 ;
211 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
212 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
213 else
214 {
215 RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
216 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
217 }
218 }
219 else
220 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
221 }
222 else
223 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
224 __LINE__, rc);
225 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
226 srand((unsigned)RTTimeNanoTS());
227 for (int i = 0; i < 30; i++)
228 pwszRand[i] = GetRandUtf16();
229 pwszRand[30] = 0;
230
231 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
232 if (rc == VERR_BUFFER_OVERFLOW)
233 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
234 else
235 RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
236 __LINE__, rc);
237
238 /*
239 * last time...
240 */
241 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
242 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
243 srand((unsigned)RTTimeNanoTS());
244 for (int i = 0; i < 30; i++)
245 pwszRand[i] = GetRandUtf16();
246 pwszRand[30] = 0;
247
248 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
249 if (rc == VINF_SUCCESS)
250 {
251 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
252 if (rc == VERR_BUFFER_OVERFLOW)
253 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
254 else
255 RTTestFailed(hTest, "%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",
256 __LINE__, rc);
257 }
258 else
259 RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
260 __LINE__, rc);
261
262
263 RTTestSubDone(hTest);
264}
265
266
267static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
268static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
269static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
270
271static void whereami(int cBits, size_t off)
272{
273 if (cBits == 8)
274 {
275 if (off < 0x7f)
276 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
277 else if (off < 0xf7f)
278 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
279 else if (off < 0x27f7f)
280 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
281 else if (off < 0x2df79)
282 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
283 else if (off < 0x42df79)
284 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
285 else
286 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
287 }
288 else if (cBits == 16)
289 {
290 if (off < 0xd7ff*2)
291 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
292 else if (off < 0xf7fd*2)
293 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
294 else if (off < 0x20f7fd)
295 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
296 else
297 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
298 }
299 else
300 {
301 if (off < (0xd800 - 1) * sizeof(RTUNICP))
302 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
303 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
304 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
305 else
306 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
307 }
308}
309
310int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
311{
312 const uint8_t *pb1 = (const uint8_t *)pv1;
313 const uint8_t *pb2 = (const uint8_t *)pv2;
314 for (size_t off = 0; off < cb; off++)
315 {
316 if (pb1[off] != pb2[off])
317 {
318 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
319 whereami(cBits, off);
320 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
321 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
322 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
323 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
324 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
325 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
326 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
327 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
328 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
329 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
330 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
331 return 1;
332 }
333 }
334 return 0;
335}
336
337
338void InitStrings()
339{
340 /*
341 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
342 */
343 /* the simple code point array first */
344 unsigned i = 0;
345 RTUNICP uc = 1;
346 while (uc < 0xd800)
347 g_uszAll[i++] = uc++;
348 uc = 0xe000;
349 while (uc < 0xfffe)
350 g_uszAll[i++] = uc++;
351 uc = 0x10000;
352 while (uc < 0x110000)
353 g_uszAll[i++] = uc++;
354 g_uszAll[i++] = 0;
355 Assert(RT_ELEMENTS(g_uszAll) == i);
356
357 /* the utf-16 one */
358 i = 0;
359 uc = 1;
360 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
361 while (uc < 0xd800)
362 g_wszAll[i++] = uc++;
363 uc = 0xe000;
364 //RTPrintf(" %#x=%#x", i, uc);
365 while (uc < 0xfffe)
366 g_wszAll[i++] = uc++;
367 uc = 0x10000;
368 //RTPrintf(" %#x=%#x", i, uc);
369 while (uc < 0x110000)
370 {
371 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
372 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
373 uc++;
374 }
375 //RTPrintf(" %#x=%#x\n", i, uc);
376 g_wszAll[i++] = '\0';
377 Assert(RT_ELEMENTS(g_wszAll) == i);
378
379 /*
380 * The utf-8 one
381 */
382 i = 0;
383 uc = 1;
384 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
385 while (uc < 0x80)
386 g_szAll[i++] = uc++;
387 //RTPrintf(" %#x=%#x", i, uc);
388 while (uc < 0x800)
389 {
390 g_szAll[i++] = 0xc0 | (uc >> 6);
391 g_szAll[i++] = 0x80 | (uc & 0x3f);
392 Assert(!((uc >> 6) & ~0x1f));
393 uc++;
394 }
395 //RTPrintf(" %#x=%#x", i, uc);
396 while (uc < 0xd800)
397 {
398 g_szAll[i++] = 0xe0 | (uc >> 12);
399 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
400 g_szAll[i++] = 0x80 | (uc & 0x3f);
401 Assert(!((uc >> 12) & ~0xf));
402 uc++;
403 }
404 uc = 0xe000;
405 //RTPrintf(" %#x=%#x", i, uc);
406 while (uc < 0xfffe)
407 {
408 g_szAll[i++] = 0xe0 | (uc >> 12);
409 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
410 g_szAll[i++] = 0x80 | (uc & 0x3f);
411 Assert(!((uc >> 12) & ~0xf));
412 uc++;
413 }
414 uc = 0x10000;
415 //RTPrintf(" %#x=%#x", i, uc);
416 while (uc < 0x110000)
417 {
418 g_szAll[i++] = 0xf0 | (uc >> 18);
419 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
420 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
421 g_szAll[i++] = 0x80 | (uc & 0x3f);
422 Assert(!((uc >> 18) & ~0x7));
423 uc++;
424 }
425 //RTPrintf(" %#x=%#x\n", i, uc);
426 g_szAll[i++] = '\0';
427 Assert(RT_ELEMENTS(g_szAll) == i);
428}
429
430
431void test2(RTTEST hTest)
432{
433 /*
434 * Convert to UTF-8 and back.
435 */
436 RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16");
437 char *pszUtf8;
438 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
439 if (rc == VINF_SUCCESS)
440 {
441 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
442 RTTestFailed(hTest, "UTF-16 -> UTF-8 mismatch!");
443
444 PRTUTF16 pwszUtf16;
445 rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
446 if (rc == VINF_SUCCESS)
447 {
448 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
449 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
450 RTUtf16Free(pwszUtf16);
451 }
452 else
453 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
454 RTStrFree(pszUtf8);
455 }
456 else
457 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
458
459
460 /*
461 * Convert to UTF-16 and back. (just in case the above test fails)
462 */
463 RTTestSub(hTest, "UTF-8 -> UTF-16 -> UTF-8");
464 PRTUTF16 pwszUtf16;
465 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
466 if (rc == VINF_SUCCESS)
467 {
468 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
469 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
470
471 char *pszUtf8;
472 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
473 if (rc == VINF_SUCCESS)
474 {
475 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
476 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
477 RTStrFree(pszUtf8);
478 }
479 else
480 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
481 RTUtf16Free(pwszUtf16);
482 }
483 else
484 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
485
486 /*
487 * Convert UTF-8 to CPs.
488 */
489 RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8");
490 PRTUNICP paCps;
491 rc = RTStrToUni(g_szAll, &paCps);
492 if (rc == VINF_SUCCESS)
493 {
494 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
495 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
496
497 size_t cCps;
498 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
499 if (rc == VINF_SUCCESS)
500 {
501 if (cCps != RT_ELEMENTS(g_uszAll) - 1)
502 RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
503 }
504 else
505 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
506
507 /** @todo RTCpsToUtf8 or something. */
508 }
509 else
510 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
511
512 /*
513 * Check the various string lengths.
514 */
515 RTTestSub(hTest, "Lengths");
516 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
517 size_t cuc2 = RTUtf16Len(g_wszAll);
518 if (cuc1 != cuc2)
519 RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
520 //size_t cuc3 = RTUniLen(g_uszAll);
521
522
523 /*
524 * Enumerate the strings.
525 */
526 RTTestSub(hTest, "Code Point Getters and Putters");
527 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
528 AssertRelease(pszPut1Base);
529 char *pszPut1 = pszPut1Base;
530 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
531 AssertRelease(pwszPut2Base);
532 PRTUTF16 pwszPut2 = pwszPut2Base;
533 const char *psz1 = g_szAll;
534 const char *psz2 = g_szAll;
535 PCRTUTF16 pwsz3 = g_wszAll;
536 PCRTUTF16 pwsz4 = g_wszAll;
537 for (;;)
538 {
539 /*
540 * getters
541 */
542 RTUNICP uc1;
543 rc = RTStrGetCpEx(&psz1, &uc1);
544 if (RT_FAILURE(rc))
545 {
546 RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
547 whereami(8, psz2 - &g_szAll[0]);
548 break;
549 }
550 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
551 if (pszPrev1 != psz2)
552 {
553 RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
554 whereami(8, psz2 - &g_szAll[0]);
555 break;
556 }
557 RTUNICP uc2 = RTStrGetCp(psz2);
558 if (uc2 != uc1)
559 {
560 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
561 whereami(8, psz2 - &g_szAll[0]);
562 break;
563 }
564 psz2 = RTStrNextCp(psz2);
565 if (psz2 != psz1)
566 {
567 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
568 whereami(8, psz2 - &g_szAll[0]);
569 break;
570 }
571
572 RTUNICP uc3;
573 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
574 if (RT_FAILURE(rc))
575 {
576 RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
577 whereami(16, pwsz4 - &g_wszAll[0]);
578 break;
579 }
580 if (uc3 != uc2)
581 {
582 RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
583 whereami(16, pwsz4 - &g_wszAll[0]);
584 break;
585 }
586 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
587 if (uc3 != uc4)
588 {
589 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
590 whereami(16, pwsz4 - &g_wszAll[0]);
591 break;
592 }
593 pwsz4 = RTUtf16NextCp(pwsz4);
594 if (pwsz4 != pwsz3)
595 {
596 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
597 whereami(8, pwsz4 - &g_wszAll[0]);
598 break;
599 }
600
601
602 /*
603 * putters
604 */
605 pszPut1 = RTStrPutCp(pszPut1, uc1);
606 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
607 {
608 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
609 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
610 whereami(8, psz2 - &g_szAll[0]);
611 break;
612 }
613
614 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
615 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
616 {
617 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
618 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
619 whereami(8, pwsz4 - &g_wszAll[0]);
620 break;
621 }
622
623
624 /* the end? */
625 if (!uc1)
626 break;
627 }
628
629 /* check output if we seems to have made it thru it all. */
630 if (psz2 == &g_szAll[sizeof(g_szAll)])
631 {
632 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
633 RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
634 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
635 RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
636 }
637
638 RTMemFree(pszPut1Base);
639 RTMemFree(pwszPut2Base);
640
641 RTTestSubDone(hTest);
642}
643
644
645/**
646 * Check case insensitivity.
647 */
648void test3(RTTEST hTest)
649{
650 RTTestSub(hTest, "Case Sensitivitity");
651
652 if ( RTUniCpToLower('a') != 'a'
653 || RTUniCpToLower('A') != 'a'
654 || RTUniCpToLower('b') != 'b'
655 || RTUniCpToLower('B') != 'b'
656 || RTUniCpToLower('Z') != 'z'
657 || RTUniCpToLower('z') != 'z'
658 || RTUniCpToUpper('c') != 'C'
659 || RTUniCpToUpper('C') != 'C'
660 || RTUniCpToUpper('z') != 'Z'
661 || RTUniCpToUpper('Z') != 'Z')
662 RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
663
664 if (RTUtf16ICmp(g_wszAll, g_wszAll))
665 RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
666
667 if (RTUtf16Cmp(g_wszAll, g_wszAll))
668 RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
669
670 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 };
671 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 };
672 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
673 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
674 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
675 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
676 )
677 RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
678
679 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
680 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
681 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
682 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
683 )
684 RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
685
686 RTTestSubDone(hTest);
687}
688
689
690/**
691 * Test the RTStr*Cmp functions.
692 */
693void TstRTStrXCmp(RTTEST hTest)
694{
695#define CHECK_DIFF(expr, op) \
696 do \
697 { \
698 int iDiff = expr; \
699 if (!(iDiff op 0)) \
700 RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
701 } while (0)
702
703/** @todo test the non-ascii bits. */
704
705 RTTestSub(hTest, "RTStrCmp");
706 CHECK_DIFF(RTStrCmp(NULL, NULL), == );
707 CHECK_DIFF(RTStrCmp(NULL, ""), < );
708 CHECK_DIFF(RTStrCmp("", NULL), > );
709 CHECK_DIFF(RTStrCmp("", ""), == );
710 CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
711 CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
712 CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
713 CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
714 CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
715 CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
716 CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
717
718
719 RTTestSub(hTest, "RTStrNCmp");
720 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
721 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
722 CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
723 CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
724 CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
725 CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
726 CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
727 CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
728 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
729 CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
730 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
731
732 CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
733 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
734 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
735
736
737 RTTestSub(hTest, "RTStrICmp");
738 CHECK_DIFF(RTStrICmp(NULL, NULL), == );
739 CHECK_DIFF(RTStrICmp(NULL, ""), < );
740 CHECK_DIFF(RTStrICmp("", NULL), > );
741 CHECK_DIFF(RTStrICmp("", ""), == );
742 CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
743 CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
744 CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
745 CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
746 CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
747
748 CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
749 CHECK_DIFF(RTStrICmp("abcdef", "abcdeF"), ==);
750 CHECK_DIFF(RTStrICmp("ABCDEF", "abcdef"), ==);
751 CHECK_DIFF(RTStrICmp("abcdef", "ABCDEF"), ==);
752 CHECK_DIFF(RTStrICmp("AbCdEf", "aBcDeF"), ==);
753 CHECK_DIFF(RTStrICmp("AbCdEg", "aBcDeF"), > );
754 CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
755
756
757
758 RTTestSub(hTest, "RTStrNICmp");
759 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
760 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
761 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
762 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
763 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
764 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
765 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
766 CHECK_DIFF(RTStrNICmp("", "", 0), == );
767 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
768 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
769 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
770 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
771 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
772
773 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
774 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
775 CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
776 CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
777 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
778 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
779 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
780
781 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
782 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
783 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
784 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
785 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
786 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
787 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
788 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
789 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
790 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
791
792 RTTestSubDone(hTest);
793}
794
795
796
797/**
798 * Benchmark stuff.
799 */
800void Benchmarks(RTTEST hTest)
801{
802 static union
803 {
804 RTUTF16 wszBuf[sizeof(g_wszAll)];
805 char szBuf[sizeof(g_szAll)];
806 } s_Buf;
807
808 RTTestSub(hTest, "Benchmarks");
809/** @todo add RTTest* methods for reporting benchmark results. */
810 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
811 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
812 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
813 if (RT_SUCCESS(rc))
814 {
815 int i;
816 uint64_t u64Start = RTTimeNanoTS();
817 for (i = 0; i < 100; i++)
818 {
819 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
820 if (RT_FAILURE(rc))
821 {
822 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
823 break;
824 }
825 }
826 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
827 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
828 }
829
830 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
831 char *psz = &s_Buf.szBuf[0];
832 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
833 if (RT_SUCCESS(rc))
834 {
835 int i;
836 uint64_t u64Start = RTTimeNanoTS();
837 for (i = 0; i < 100; i++)
838 {
839 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
840 if (RT_FAILURE(rc))
841 {
842 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
843 break;
844 }
845 }
846 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
847 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
848 }
849
850 RTTestSubDone(hTest);
851}
852
853
854/**
855 * Tests RTStrStr and RTStrIStr.
856 */
857static void testStrStr(RTTEST hTest)
858{
859#define CHECK_NULL(expr) \
860 do { \
861 const char *pszRet = expr; \
862 if (pszRet != NULL) \
863 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
864 } while (0)
865
866#define CHECK(expr, expect) \
867 do { \
868 const char *pszRet = expr; \
869 if ( (pszRet != NULL && (expect) == NULL) \
870 || (pszRet == NULL && (expect) != NULL) \
871 || strcmp(pszRet, (expect)) \
872 ) \
873 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
874 } while (0)
875
876
877 RTTestSub(hTest, "RTStrStr");
878 CHECK(RTStrStr("abcdef", ""), "abcdef");
879 CHECK_NULL(RTStrStr("abcdef", NULL));
880 CHECK_NULL(RTStrStr(NULL, ""));
881 CHECK_NULL(RTStrStr(NULL, NULL));
882 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
883 CHECK(RTStrStr("abcdef", "b"), "bcdef");
884 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
885 CHECK(RTStrStr("abcdef", "cdef"), "cdef");
886 CHECK(RTStrStr("abcdef", "cde"), "cdef");
887 CHECK(RTStrStr("abcdef", "cd"), "cdef");
888 CHECK(RTStrStr("abcdef", "c"), "cdef");
889 CHECK(RTStrStr("abcdef", "f"), "f");
890 CHECK(RTStrStr("abcdef", "ef"), "ef");
891 CHECK(RTStrStr("abcdef", "e"), "ef");
892 CHECK_NULL(RTStrStr("abcdef", "z"));
893 CHECK_NULL(RTStrStr("abcdef", "A"));
894 CHECK_NULL(RTStrStr("abcdef", "F"));
895
896 RTTestSub(hTest, "RTStrIStr");
897 CHECK(RTStrIStr("abcdef", ""), "abcdef");
898 CHECK_NULL(RTStrIStr("abcdef", NULL));
899 CHECK_NULL(RTStrIStr(NULL, ""));
900 CHECK_NULL(RTStrIStr(NULL, NULL));
901 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
902 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
903 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
904 CHECK(RTStrIStr("abcdef", "b"), "bcdef");
905 CHECK(RTStrIStr("abcdef", "B"), "bcdef");
906 CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef");
907 CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef");
908 CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef");
909 CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef");
910 CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef");
911 CHECK(RTStrIStr("abcdef", "cdef"), "cdef");
912 CHECK(RTStrIStr("abcdef", "cde"), "cdef");
913 CHECK(RTStrIStr("abcdef", "cd"), "cdef");
914 CHECK(RTStrIStr("abcdef", "c"), "cdef");
915 CHECK(RTStrIStr("abcdef", "f"), "f");
916 CHECK(RTStrIStr("abcdeF", "F"), "F");
917 CHECK(RTStrIStr("abcdef", "F"), "f");
918 CHECK(RTStrIStr("abcdef", "ef"), "ef");
919 CHECK(RTStrIStr("EeEef", "e"), "EeEef");
920 CHECK(RTStrIStr("EeEef", "E"), "EeEef");
921 CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
922 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
923 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
924 CHECK_NULL(RTStrIStr("EeEef", "z"));
925
926#undef CHECK
927#undef CHECK_NULL
928 RTTestSubDone(hTest);
929}
930
931
932void testMinistring(RTTEST hTest)
933{
934 RTTestSub(hTest, "class iprt::MiniString");
935
936#define CHECK(expr) \
937 do { \
938 if (!(expr)) \
939 RTTestFailed(hTest, "%d: FAILED %s", __LINE__, #expr); \
940 } while (0)
941
942#define CHECK_DUMP(expr, value) \
943 do { \
944 if (!(expr)) \
945 RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
946 } while (0)
947
948#define CHECK_DUMP_I(expr) \
949 do { \
950 if (!(expr)) \
951 RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
952 } while (0)
953
954 iprt::MiniString empty;
955 CHECK( (empty.length() == 0) );
956 CHECK( (empty.capacity() == 0) );
957
958 iprt::MiniString sixbytes("12345");
959 CHECK( (sixbytes.length() == 5) );
960 CHECK( (sixbytes.capacity() == 6) );
961
962 sixbytes.append("678");
963 CHECK( (sixbytes.length() == 8) );
964 CHECK( (sixbytes.capacity() == 9) );
965
966 char *psz = sixbytes.mutableRaw();
967 // 12345678
968 // ^
969 // 0123456
970 psz[6] = '\0';
971 sixbytes.jolt();
972 CHECK( (sixbytes.length() == 6) );
973 CHECK( (sixbytes.capacity() == 7) );
974
975 iprt::MiniString morebytes("tobereplaced");
976 morebytes = "newstring ";
977 morebytes.append(sixbytes);
978
979 CHECK_DUMP( (morebytes == "newstring 123456"), morebytes.c_str() );
980
981 iprt::MiniString third(morebytes);
982 third.reserve(100 * 1024); // 100 KB
983 CHECK_DUMP( (third == "newstring 123456"), morebytes.c_str() );
984 CHECK( (third.capacity() == 100 * 1024) );
985 CHECK( (third.length() == morebytes.length()) ); // must not have changed
986
987 iprt::MiniString copy1(morebytes);
988 iprt::MiniString copy2 = morebytes;
989 CHECK( (copy1 == copy2) );
990
991 copy1 = NULL;
992 CHECK( (copy1.length() == 0) );
993
994 copy1 = "";
995 CHECK( (copy1.length() == 0) );
996
997 CHECK( (iprt::MiniString("abc") < iprt::MiniString("def")) );
998 CHECK( (iprt::MiniString("abc") != iprt::MiniString("def")) );
999 CHECK_DUMP_I( (iprt::MiniString("def") > iprt::MiniString("abc")) );
1000
1001 copy2.setNull();
1002 for (int i = 0;
1003 i < 100;
1004 ++i)
1005 {
1006 copy2.reserve(50); // should be ignored after 50 loops
1007 copy2.append("1");
1008 }
1009 CHECK( (copy2.length() == 100) );
1010
1011 copy2.setNull();
1012 for (int i = 0;
1013 i < 100;
1014 ++i)
1015 {
1016 copy2.reserve(50); // should be ignored after 50 loops
1017 copy2.append('1');
1018 }
1019 CHECK( (copy2.length() == 100) );
1020
1021#undef CHECK
1022}
1023
1024
1025void testLatin1(RTTEST hTest)
1026{
1027 RTTestSub(hTest, "Latin1 conversion functions");
1028
1029 /* Test Utf16 -> Latin1 */
1030 size_t cch_szAll = 0;
1031 size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
1032 RTTEST_CHECK(hTest, cbShort == 0);
1033 int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
1034 RTTEST_CHECK(hTest, (cch_szAll == 255));
1035 rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
1036 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1037 char *psz = NULL;
1038 RTUTF16 wszShort[256] = { 0 };
1039 for (unsigned i = 0; i < 255; ++i)
1040 wszShort[i] = i + 1;
1041 cbShort = RTUtf16CalcLatin1Len(wszShort);
1042 RTTEST_CHECK(hTest, cbShort == 255);
1043 rc = RTUtf16ToLatin1(wszShort, &psz);
1044 RTTEST_CHECK_RC_OK(hTest, rc);
1045 if (RT_SUCCESS(rc))
1046 {
1047 RTTEST_CHECK(hTest, (strlen(psz) == 255));
1048 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1049 if (psz[i] != (char) j)
1050 {
1051 RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
1052 break;
1053 }
1054 }
1055 RTStrFree(psz);
1056 rc = RTUtf16ToLatin1(g_wszAll, &psz);
1057 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1058 char sz[512];
1059 char *psz2 = &sz[0];
1060 size_t cchActual = 0;
1061 rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1062 &cchActual);
1063 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1064 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1065 (hTest, "cchActual=%lu\n", cchActual));
1066 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
1067 &cchActual);
1068 RTTEST_CHECK_RC_OK(hTest, rc);
1069 if (RT_SUCCESS(rc))
1070 {
1071 RTTEST_CHECK(hTest, (cchActual == 255));
1072 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1073 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1074 if (psz2[i] != (char) j)
1075 {
1076 RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
1077 break;
1078 }
1079 }
1080 rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
1081 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1082 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1083 (hTest, "cchActual=%lu\n", cchActual));
1084 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
1085 RTTEST_CHECK_RC_OK(hTest, rc);
1086 if (RT_SUCCESS(rc))
1087 {
1088 RTTEST_CHECK(hTest, (cchActual == 255));
1089 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1090 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1091 if ( ((j < 0x100) && (psz[i] != (char) j))
1092 || ((j > 0xff) && psz[i] != '?'))
1093 {
1094 RTTestFailed(hTest, "third conversion of g_wszAll to Latin1 failed at position %u\n", i);
1095 break;
1096 }
1097 }
1098 const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
1099 rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1100 &cchActual);
1101 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
1102 RTStrFree(psz);
1103
1104 /* Test Latin1 -> Utf16 */
1105 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1106 RTTEST_CHECK(hTest, (RTLatin1CalcUtf16Len(pszLat1) == 5));
1107 rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
1108 RTTEST_CHECK_RC_OK(hTest, rc);
1109 if (RT_SUCCESS(rc))
1110 RTTEST_CHECK(hTest, (cchActual == 3));
1111 rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
1112 RTTEST_CHECK_RC_OK(hTest, rc);
1113 if (RT_SUCCESS(rc))
1114 RTTEST_CHECK(hTest, (cchActual == 5));
1115 RTUTF16 *pwc = NULL;
1116 RTUTF16 wc[6];
1117 RTUTF16 *pwc2 = &wc[0];
1118 size_t cwActual = 0;
1119 rc = RTLatin1ToUtf16(pszLat1, &pwc);
1120 RTTEST_CHECK_RC_OK(hTest, rc);
1121 if (RT_SUCCESS(rc))
1122 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1123 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1124 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1125 RTUtf16Free(pwc);
1126 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, &cwActual);
1127 RTTEST_CHECK_RC_OK(hTest, rc);
1128 if (RT_SUCCESS(rc))
1129 {
1130 RTTEST_CHECK(hTest, (cwActual == 5));
1131 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1132 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1133 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1134 }
1135 RTUtf16Free(pwc);
1136 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, NULL);
1137 RTTEST_CHECK_RC_OK(hTest, rc);
1138 if (RT_SUCCESS(rc))
1139 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1140 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1141 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1142 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc),
1143 &cwActual);
1144 RTTEST_CHECK_RC_OK(hTest, rc);
1145 if (RT_SUCCESS(rc))
1146 {
1147 RTTEST_CHECK(hTest, (cwActual == 5));
1148 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1149 && (wc[2] == 0x40) && (wc[3] == 0x80)
1150 && (wc[4] == 0x81) && (wc[5] == '\0'));
1151 }
1152 rc = RTLatin1ToUtf16Ex(pszLat1, 3, &pwc2, RT_ELEMENTS(wc),
1153 &cwActual);
1154 RTTEST_CHECK_RC_OK(hTest, rc);
1155 if (RT_SUCCESS(rc))
1156 {
1157 RTTEST_CHECK(hTest, (cwActual == 3));
1158 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1159 && (wc[2] == 0x40) && (wc[3] == '\0'));
1160 }
1161 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc) - 1,
1162 &cwActual);
1163 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1164 /** @todo Either fix the documentation or fix the code - cchActual is
1165 * set to the number of bytes actually encoded. */
1166 RTTEST_CHECK(hTest, (cwActual == 5));
1167 RTTestSubDone(hTest);
1168}
1169
1170
1171static void testNoTransation(RTTEST hTest)
1172{
1173
1174 /*
1175 * Try trigger a VERR_NO_TRANSLATION error in convert to
1176 * current CP to latin-1.
1177 */
1178 const RTUTF16 s_swzTest1[] = { 0x2358, 0x2242, 0x2357, 0x2359, 0x22f9, 0x2c4e, 0x0030, 0x0060,
1179 0x0092, 0x00c1, 0x00f2, 0x1f80, 0x0088, 0x2c38, 0x2c30, 0x0000 };
1180 char *pszTest1;
1181 int rc = RTUtf16ToUtf8(s_swzTest1, &pszTest1);
1182 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
1183
1184 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP");
1185 char *pszOut;
1186 rc = RTStrUtf8ToCurrentCP(&pszOut, pszTest1);
1187 if (RT_SUCCESS(rc))
1188 {
1189 RTTESTI_CHECK(!strcmp(pszOut, pszTest1));
1190 RTTestIPrintf(RTTESTLVL_ALWAYS, "CurrentCP is UTF-8 or similar\n");
1191 RTStrFree(pszOut);
1192 }
1193 else
1194 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1195
1196 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
1197 rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
1198 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1199 if (RT_SUCCESS(rc))
1200 RTStrFree(pszOut);
1201
1202 RTStrFree(pszTest1);
1203 RTTestSubDone(hTest);
1204}
1205
1206
1207int main()
1208{
1209 /*
1210 * Init the runtime, test and say hello.
1211 */
1212 RTTEST hTest;
1213 int rc = RTTestInitAndCreate("tstUtf8", &hTest);
1214 if (rc)
1215 return rc;
1216 RTTestBanner(hTest);
1217
1218 /*
1219 * Run the test.
1220 */
1221 InitStrings();
1222 test1(hTest);
1223 test2(hTest);
1224 test3(hTest);
1225 TstRTStrXCmp(hTest);
1226 testStrStr(hTest);
1227 testMinistring(hTest);
1228 testLatin1(hTest);
1229 testNoTransation(hTest);
1230
1231 Benchmarks(hTest);
1232
1233 /*
1234 * Summary
1235 */
1236 return RTTestSummaryAndDestroy(hTest);
1237}
1238
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