VirtualBox

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

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

iprt/ministring_cpp.h -> iprt/cpp/ministring.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.3 KB
Line 
1/* $Id: tstUtf8.cpp 25349 2009-12-13 17:23:32Z 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/cpp/ministring.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 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
472 if (rc == VINF_SUCCESS)
473 {
474 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
475 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
476 RTStrFree(pszUtf8);
477 }
478 else
479 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
480 RTUtf16Free(pwszUtf16);
481 }
482 else
483 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
484
485 /*
486 * Convert UTF-8 to CPs.
487 */
488 RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8");
489 PRTUNICP paCps;
490 rc = RTStrToUni(g_szAll, &paCps);
491 if (rc == VINF_SUCCESS)
492 {
493 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
494 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
495
496 size_t cCps;
497 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
498 if (rc == VINF_SUCCESS)
499 {
500 if (cCps != RT_ELEMENTS(g_uszAll) - 1)
501 RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
502 }
503 else
504 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
505
506 /** @todo RTCpsToUtf8 or something. */
507 }
508 else
509 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
510
511 /*
512 * Check the various string lengths.
513 */
514 RTTestSub(hTest, "Lengths");
515 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
516 size_t cuc2 = RTUtf16Len(g_wszAll);
517 if (cuc1 != cuc2)
518 RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
519 //size_t cuc3 = RTUniLen(g_uszAll);
520
521
522 /*
523 * Enumerate the strings.
524 */
525 RTTestSub(hTest, "Code Point Getters and Putters");
526 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
527 AssertRelease(pszPut1Base);
528 char *pszPut1 = pszPut1Base;
529 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
530 AssertRelease(pwszPut2Base);
531 PRTUTF16 pwszPut2 = pwszPut2Base;
532 const char *psz1 = g_szAll;
533 const char *psz2 = g_szAll;
534 PCRTUTF16 pwsz3 = g_wszAll;
535 PCRTUTF16 pwsz4 = g_wszAll;
536 for (;;)
537 {
538 /*
539 * getters
540 */
541 RTUNICP uc1;
542 rc = RTStrGetCpEx(&psz1, &uc1);
543 if (RT_FAILURE(rc))
544 {
545 RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
546 whereami(8, psz2 - &g_szAll[0]);
547 break;
548 }
549 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
550 if (pszPrev1 != psz2)
551 {
552 RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
553 whereami(8, psz2 - &g_szAll[0]);
554 break;
555 }
556 RTUNICP uc2 = RTStrGetCp(psz2);
557 if (uc2 != uc1)
558 {
559 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
560 whereami(8, psz2 - &g_szAll[0]);
561 break;
562 }
563 psz2 = RTStrNextCp(psz2);
564 if (psz2 != psz1)
565 {
566 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
567 whereami(8, psz2 - &g_szAll[0]);
568 break;
569 }
570
571 RTUNICP uc3;
572 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
573 if (RT_FAILURE(rc))
574 {
575 RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
576 whereami(16, pwsz4 - &g_wszAll[0]);
577 break;
578 }
579 if (uc3 != uc2)
580 {
581 RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
582 whereami(16, pwsz4 - &g_wszAll[0]);
583 break;
584 }
585 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
586 if (uc3 != uc4)
587 {
588 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
589 whereami(16, pwsz4 - &g_wszAll[0]);
590 break;
591 }
592 pwsz4 = RTUtf16NextCp(pwsz4);
593 if (pwsz4 != pwsz3)
594 {
595 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
596 whereami(8, pwsz4 - &g_wszAll[0]);
597 break;
598 }
599
600
601 /*
602 * putters
603 */
604 pszPut1 = RTStrPutCp(pszPut1, uc1);
605 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
606 {
607 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
608 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
609 whereami(8, psz2 - &g_szAll[0]);
610 break;
611 }
612
613 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
614 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
615 {
616 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
617 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
618 whereami(8, pwsz4 - &g_wszAll[0]);
619 break;
620 }
621
622
623 /* the end? */
624 if (!uc1)
625 break;
626 }
627
628 /* check output if we seems to have made it thru it all. */
629 if (psz2 == &g_szAll[sizeof(g_szAll)])
630 {
631 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
632 RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
633 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
634 RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
635 }
636
637 RTMemFree(pszPut1Base);
638 RTMemFree(pwszPut2Base);
639
640 RTTestSubDone(hTest);
641}
642
643
644/**
645 * Check case insensitivity.
646 */
647void test3(RTTEST hTest)
648{
649 RTTestSub(hTest, "Case Sensitivitity");
650
651 if ( RTUniCpToLower('a') != 'a'
652 || RTUniCpToLower('A') != 'a'
653 || RTUniCpToLower('b') != 'b'
654 || RTUniCpToLower('B') != 'b'
655 || RTUniCpToLower('Z') != 'z'
656 || RTUniCpToLower('z') != 'z'
657 || RTUniCpToUpper('c') != 'C'
658 || RTUniCpToUpper('C') != 'C'
659 || RTUniCpToUpper('z') != 'Z'
660 || RTUniCpToUpper('Z') != 'Z')
661 RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
662
663 if (RTUtf16ICmp(g_wszAll, g_wszAll))
664 RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
665
666 if (RTUtf16Cmp(g_wszAll, g_wszAll))
667 RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
668
669 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 };
670 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 };
671 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
672 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
673 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
674 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
675 )
676 RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
677
678 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
679 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
680 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
681 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
682 )
683 RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
684
685 RTTestSubDone(hTest);
686}
687
688
689/**
690 * Test the RTStr*Cmp functions.
691 */
692void TstRTStrXCmp(RTTEST hTest)
693{
694#define CHECK_DIFF(expr, op) \
695 do \
696 { \
697 int iDiff = expr; \
698 if (!(iDiff op 0)) \
699 RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
700 } while (0)
701
702/** @todo test the non-ascii bits. */
703
704 RTTestSub(hTest, "RTStrCmp");
705 CHECK_DIFF(RTStrCmp(NULL, NULL), == );
706 CHECK_DIFF(RTStrCmp(NULL, ""), < );
707 CHECK_DIFF(RTStrCmp("", NULL), > );
708 CHECK_DIFF(RTStrCmp("", ""), == );
709 CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
710 CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
711 CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
712 CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
713 CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
714 CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
715 CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
716
717
718 RTTestSub(hTest, "RTStrNCmp");
719 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
720 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
721 CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
722 CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
723 CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
724 CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
725 CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
726 CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
727 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
728 CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
729 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
730
731 CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
732 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
733 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
734
735
736 RTTestSub(hTest, "RTStrICmp");
737 CHECK_DIFF(RTStrICmp(NULL, NULL), == );
738 CHECK_DIFF(RTStrICmp(NULL, ""), < );
739 CHECK_DIFF(RTStrICmp("", NULL), > );
740 CHECK_DIFF(RTStrICmp("", ""), == );
741 CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
742 CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
743 CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
744 CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
745 CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
746
747 CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
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("AbCdEg", "aBcDeF"), > );
753 CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
754
755
756
757 RTTestSub(hTest, "RTStrNICmp");
758 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
759 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
760 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
761 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
762 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
763 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
764 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
765 CHECK_DIFF(RTStrNICmp("", "", 0), == );
766 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
767 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
768 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
769 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
770 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
771
772 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
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("AbCdEg", "aBcDeF", RTSTR_MAX), > );
778 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
779
780 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
781 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
782 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
783 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
784 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
785 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
786 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
787 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
788 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
789 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
790
791 RTTestSubDone(hTest);
792}
793
794
795
796/**
797 * Benchmark stuff.
798 */
799void Benchmarks(RTTEST hTest)
800{
801 static union
802 {
803 RTUTF16 wszBuf[sizeof(g_wszAll)];
804 char szBuf[sizeof(g_szAll)];
805 } s_Buf;
806
807 RTTestSub(hTest, "Benchmarks");
808/** @todo add RTTest* methods for reporting benchmark results. */
809 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
810 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
811 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
812 if (RT_SUCCESS(rc))
813 {
814 int i;
815 uint64_t u64Start = RTTimeNanoTS();
816 for (i = 0; i < 100; i++)
817 {
818 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
819 if (RT_FAILURE(rc))
820 {
821 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
822 break;
823 }
824 }
825 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
826 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
827 }
828
829 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
830 char *psz = &s_Buf.szBuf[0];
831 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
832 if (RT_SUCCESS(rc))
833 {
834 int i;
835 uint64_t u64Start = RTTimeNanoTS();
836 for (i = 0; i < 100; i++)
837 {
838 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
839 if (RT_FAILURE(rc))
840 {
841 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
842 break;
843 }
844 }
845 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
846 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
847 }
848
849 RTTestSubDone(hTest);
850}
851
852
853/**
854 * Tests RTStrStr and RTStrIStr.
855 */
856static void testStrStr(RTTEST hTest)
857{
858#define CHECK_NULL(expr) \
859 do { \
860 const char *pszRet = expr; \
861 if (pszRet != NULL) \
862 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
863 } while (0)
864
865#define CHECK(expr, expect) \
866 do { \
867 const char *pszRet = expr; \
868 if ( (pszRet != NULL && (expect) == NULL) \
869 || (pszRet == NULL && (expect) != NULL) \
870 || strcmp(pszRet, (expect)) \
871 ) \
872 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
873 } while (0)
874
875
876 RTTestSub(hTest, "RTStrStr");
877 CHECK(RTStrStr("abcdef", ""), "abcdef");
878 CHECK_NULL(RTStrStr("abcdef", NULL));
879 CHECK_NULL(RTStrStr(NULL, ""));
880 CHECK_NULL(RTStrStr(NULL, NULL));
881 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
882 CHECK(RTStrStr("abcdef", "b"), "bcdef");
883 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
884 CHECK(RTStrStr("abcdef", "cdef"), "cdef");
885 CHECK(RTStrStr("abcdef", "cde"), "cdef");
886 CHECK(RTStrStr("abcdef", "cd"), "cdef");
887 CHECK(RTStrStr("abcdef", "c"), "cdef");
888 CHECK(RTStrStr("abcdef", "f"), "f");
889 CHECK(RTStrStr("abcdef", "ef"), "ef");
890 CHECK(RTStrStr("abcdef", "e"), "ef");
891 CHECK_NULL(RTStrStr("abcdef", "z"));
892 CHECK_NULL(RTStrStr("abcdef", "A"));
893 CHECK_NULL(RTStrStr("abcdef", "F"));
894
895 RTTestSub(hTest, "RTStrIStr");
896 CHECK(RTStrIStr("abcdef", ""), "abcdef");
897 CHECK_NULL(RTStrIStr("abcdef", NULL));
898 CHECK_NULL(RTStrIStr(NULL, ""));
899 CHECK_NULL(RTStrIStr(NULL, NULL));
900 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
901 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
902 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
903 CHECK(RTStrIStr("abcdef", "b"), "bcdef");
904 CHECK(RTStrIStr("abcdef", "B"), "bcdef");
905 CHECK(RTStrIStr("abcdef", "bcdef"), "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", "cdef"), "cdef");
911 CHECK(RTStrIStr("abcdef", "cde"), "cdef");
912 CHECK(RTStrIStr("abcdef", "cd"), "cdef");
913 CHECK(RTStrIStr("abcdef", "c"), "cdef");
914 CHECK(RTStrIStr("abcdef", "f"), "f");
915 CHECK(RTStrIStr("abcdeF", "F"), "F");
916 CHECK(RTStrIStr("abcdef", "F"), "f");
917 CHECK(RTStrIStr("abcdef", "ef"), "ef");
918 CHECK(RTStrIStr("EeEef", "e"), "EeEef");
919 CHECK(RTStrIStr("EeEef", "E"), "EeEef");
920 CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
921 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
922 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
923 CHECK_NULL(RTStrIStr("EeEef", "z"));
924
925#undef CHECK
926#undef CHECK_NULL
927 RTTestSubDone(hTest);
928}
929
930
931void testMinistring(RTTEST hTest)
932{
933 RTTestSub(hTest, "class iprt::MiniString");
934
935#define CHECK(expr) \
936 do { \
937 if (!(expr)) \
938 RTTestFailed(hTest, "%d: FAILED %s", __LINE__, #expr); \
939 } while (0)
940
941#define CHECK_DUMP(expr, value) \
942 do { \
943 if (!(expr)) \
944 RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
945 } while (0)
946
947#define CHECK_DUMP_I(expr) \
948 do { \
949 if (!(expr)) \
950 RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
951 } while (0)
952
953 iprt::MiniString empty;
954 CHECK( (empty.length() == 0) );
955 CHECK( (empty.capacity() == 0) );
956
957 iprt::MiniString sixbytes("12345");
958 CHECK( (sixbytes.length() == 5) );
959 CHECK( (sixbytes.capacity() == 6) );
960
961 sixbytes.append("678");
962 CHECK( (sixbytes.length() == 8) );
963 CHECK( (sixbytes.capacity() == 9) );
964
965 char *psz = sixbytes.mutableRaw();
966 // 12345678
967 // ^
968 // 0123456
969 psz[6] = '\0';
970 sixbytes.jolt();
971 CHECK( (sixbytes.length() == 6) );
972 CHECK( (sixbytes.capacity() == 7) );
973
974 iprt::MiniString morebytes("tobereplaced");
975 morebytes = "newstring ";
976 morebytes.append(sixbytes);
977
978 CHECK_DUMP( (morebytes == "newstring 123456"), morebytes.c_str() );
979
980 iprt::MiniString third(morebytes);
981 third.reserve(100 * 1024); // 100 KB
982 CHECK_DUMP( (third == "newstring 123456"), morebytes.c_str() );
983 CHECK( (third.capacity() == 100 * 1024) );
984 CHECK( (third.length() == morebytes.length()) ); // must not have changed
985
986 iprt::MiniString copy1(morebytes);
987 iprt::MiniString copy2 = morebytes;
988 CHECK( (copy1 == copy2) );
989
990 copy1 = NULL;
991 CHECK( (copy1.length() == 0) );
992
993 copy1 = "";
994 CHECK( (copy1.length() == 0) );
995
996 CHECK( (iprt::MiniString("abc") < iprt::MiniString("def")) );
997 CHECK( (iprt::MiniString("abc") != iprt::MiniString("def")) );
998 CHECK_DUMP_I( (iprt::MiniString("def") > iprt::MiniString("abc")) );
999
1000 copy2.setNull();
1001 for (int i = 0;
1002 i < 100;
1003 ++i)
1004 {
1005 copy2.reserve(50); // should be ignored after 50 loops
1006 copy2.append("1");
1007 }
1008 CHECK( (copy2.length() == 100) );
1009
1010 copy2.setNull();
1011 for (int i = 0;
1012 i < 100;
1013 ++i)
1014 {
1015 copy2.reserve(50); // should be ignored after 50 loops
1016 copy2.append('1');
1017 }
1018 CHECK( (copy2.length() == 100) );
1019
1020#undef CHECK
1021}
1022
1023
1024void testLatin1(RTTEST hTest)
1025{
1026 RTTestSub(hTest, "Latin1 conversion functions");
1027
1028 /* Test Utf16 -> Latin1 */
1029 size_t cch_szAll = 0;
1030 size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
1031 RTTEST_CHECK(hTest, cbShort == 0);
1032 int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
1033 RTTEST_CHECK(hTest, (cch_szAll == 255));
1034 rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
1035 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1036 char *psz = NULL;
1037 RTUTF16 wszShort[256] = { 0 };
1038 for (unsigned i = 0; i < 255; ++i)
1039 wszShort[i] = i + 1;
1040 cbShort = RTUtf16CalcLatin1Len(wszShort);
1041 RTTEST_CHECK(hTest, cbShort == 255);
1042 rc = RTUtf16ToLatin1(wszShort, &psz);
1043 RTTEST_CHECK_RC_OK(hTest, rc);
1044 if (RT_SUCCESS(rc))
1045 {
1046 RTTEST_CHECK(hTest, (strlen(psz) == 255));
1047 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1048 if (psz[i] != (char) j)
1049 {
1050 RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
1051 break;
1052 }
1053 }
1054 RTStrFree(psz);
1055 rc = RTUtf16ToLatin1(g_wszAll, &psz);
1056 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1057 char sz[512];
1058 char *psz2 = &sz[0];
1059 size_t cchActual = 0;
1060 rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1061 &cchActual);
1062 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1063 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1064 (hTest, "cchActual=%lu\n", cchActual));
1065 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
1066 &cchActual);
1067 RTTEST_CHECK_RC_OK(hTest, rc);
1068 if (RT_SUCCESS(rc))
1069 {
1070 RTTEST_CHECK(hTest, (cchActual == 255));
1071 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1072 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1073 if (psz2[i] != (char) j)
1074 {
1075 RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
1076 break;
1077 }
1078 }
1079 rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
1080 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1081 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1082 (hTest, "cchActual=%lu\n", cchActual));
1083 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
1084 RTTEST_CHECK_RC_OK(hTest, rc);
1085 if (RT_SUCCESS(rc))
1086 {
1087 RTTEST_CHECK(hTest, (cchActual == 255));
1088 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1089 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1090 if ( ((j < 0x100) && (psz[i] != (char) j))
1091 || ((j > 0xff) && psz[i] != '?'))
1092 {
1093 RTTestFailed(hTest, "third conversion of g_wszAll to Latin1 failed at position %u\n", i);
1094 break;
1095 }
1096 }
1097 const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
1098 rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1099 &cchActual);
1100 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
1101 RTStrFree(psz);
1102
1103 /* Test Latin1 -> Utf16 */
1104 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1105 RTTEST_CHECK(hTest, (RTLatin1CalcUtf16Len(pszLat1) == 5));
1106 rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
1107 RTTEST_CHECK_RC_OK(hTest, rc);
1108 if (RT_SUCCESS(rc))
1109 RTTEST_CHECK(hTest, (cchActual == 3));
1110 rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
1111 RTTEST_CHECK_RC_OK(hTest, rc);
1112 if (RT_SUCCESS(rc))
1113 RTTEST_CHECK(hTest, (cchActual == 5));
1114 RTUTF16 *pwc = NULL;
1115 RTUTF16 wc[6];
1116 RTUTF16 *pwc2 = &wc[0];
1117 size_t cwActual = 0;
1118 rc = RTLatin1ToUtf16(pszLat1, &pwc);
1119 RTTEST_CHECK_RC_OK(hTest, rc);
1120 if (RT_SUCCESS(rc))
1121 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1122 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1123 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1124 RTUtf16Free(pwc);
1125 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, &cwActual);
1126 RTTEST_CHECK_RC_OK(hTest, rc);
1127 if (RT_SUCCESS(rc))
1128 {
1129 RTTEST_CHECK(hTest, (cwActual == 5));
1130 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1131 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1132 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1133 }
1134 RTUtf16Free(pwc);
1135 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, NULL);
1136 RTTEST_CHECK_RC_OK(hTest, rc);
1137 if (RT_SUCCESS(rc))
1138 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1139 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1140 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1141 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc),
1142 &cwActual);
1143 RTTEST_CHECK_RC_OK(hTest, rc);
1144 if (RT_SUCCESS(rc))
1145 {
1146 RTTEST_CHECK(hTest, (cwActual == 5));
1147 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1148 && (wc[2] == 0x40) && (wc[3] == 0x80)
1149 && (wc[4] == 0x81) && (wc[5] == '\0'));
1150 }
1151 rc = RTLatin1ToUtf16Ex(pszLat1, 3, &pwc2, RT_ELEMENTS(wc),
1152 &cwActual);
1153 RTTEST_CHECK_RC_OK(hTest, rc);
1154 if (RT_SUCCESS(rc))
1155 {
1156 RTTEST_CHECK(hTest, (cwActual == 3));
1157 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1158 && (wc[2] == 0x40) && (wc[3] == '\0'));
1159 }
1160 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc) - 1,
1161 &cwActual);
1162 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1163 /** @todo Either fix the documentation or fix the code - cchActual is
1164 * set to the number of bytes actually encoded. */
1165 RTTEST_CHECK(hTest, (cwActual == 5));
1166 RTTestSubDone(hTest);
1167}
1168
1169
1170static void testNoTransation(RTTEST hTest)
1171{
1172
1173 /*
1174 * Try trigger a VERR_NO_TRANSLATION error in convert to
1175 * current CP to latin-1.
1176 */
1177 const RTUTF16 s_swzTest1[] = { 0x2358, 0x2242, 0x2357, 0x2359, 0x22f9, 0x2c4e, 0x0030, 0x0060,
1178 0x0092, 0x00c1, 0x00f2, 0x1f80, 0x0088, 0x2c38, 0x2c30, 0x0000 };
1179 char *pszTest1;
1180 int rc = RTUtf16ToUtf8(s_swzTest1, &pszTest1);
1181 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
1182
1183 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP");
1184 char *pszOut;
1185 rc = RTStrUtf8ToCurrentCP(&pszOut, pszTest1);
1186 if (RT_SUCCESS(rc))
1187 {
1188 RTTESTI_CHECK(!strcmp(pszOut, pszTest1));
1189 RTTestIPrintf(RTTESTLVL_ALWAYS, "CurrentCP is UTF-8 or similar\n");
1190 RTStrFree(pszOut);
1191 }
1192 else
1193 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1194
1195 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
1196 rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
1197 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1198 if (RT_SUCCESS(rc))
1199 RTStrFree(pszOut);
1200
1201 RTStrFree(pszTest1);
1202 RTTestSubDone(hTest);
1203}
1204
1205
1206int main()
1207{
1208 /*
1209 * Init the runtime, test and say hello.
1210 */
1211 RTTEST hTest;
1212 int rc = RTTestInitAndCreate("tstUtf8", &hTest);
1213 if (rc)
1214 return rc;
1215 RTTestBanner(hTest);
1216
1217 /*
1218 * Run the test.
1219 */
1220 InitStrings();
1221 test1(hTest);
1222 test2(hTest);
1223 test3(hTest);
1224 TstRTStrXCmp(hTest);
1225 testStrStr(hTest);
1226 testMinistring(hTest);
1227 testLatin1(hTest);
1228 testNoTransation(hTest);
1229
1230 Benchmarks(hTest);
1231
1232 /*
1233 * Summary
1234 */
1235 return RTTestSummaryAndDestroy(hTest);
1236}
1237
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