VirtualBox

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

Last change on this file since 30016 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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