VirtualBox

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

Last change on this file since 93174 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 57.6 KB
Line 
1/* $Id: tstUtf8.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - UTF-8 and UTF-16 string conversions.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/string.h>
32#include <iprt/latin1.h>
33#include <iprt/utf16.h>
34
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/env.h>
38#include <iprt/err.h>
39#include <iprt/rand.h>
40#include <iprt/stream.h>
41#include <iprt/test.h>
42#include <iprt/time.h>
43#include <iprt/uni.h>
44#include <iprt/uuid.h>
45
46
47
48/**
49 * Generate a random codepoint for simple UTF-16 encoding.
50 */
51static RTUTF16 GetRandUtf16(void)
52{
53 RTUTF16 wc;
54 do
55 {
56 wc = (RTUTF16)RTRandU32Ex(1, 0xfffd);
57 } while (wc >= 0xd800 && wc <= 0xdfff);
58 return wc;
59}
60
61
62/**
63 *
64 */
65static void test1(RTTEST hTest)
66{
67 static const char s_szBadString1[] = "Bad \xe0\x13\x0";
68 static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
69 int rc;
70 char *pszUtf8;
71 char *pszCurrent;
72 PRTUTF16 pwsz;
73 PRTUTF16 pwszRand;
74
75 /*
76 * Invalid UTF-8 to UCS-2 test.
77 */
78 RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16");
79 rc = RTStrToUtf16(s_szBadString1, &pwsz);
80 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
81 (hTest, "Conversion of first bad UTF-8 string to UTF-16 apparently succeeded. It shouldn't. rc=%Rrc\n", rc));
82 rc = RTStrToUtf16(s_szBadString2, &pwsz);
83 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
84 (hTest, "Conversion of second bad UTF-8 strings to UTF-16 apparently succeeded. It shouldn't. rc=%Rrc\n", rc));
85
86 /*
87 * Test current CP conversion.
88 */
89 RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
90 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
91 for (int i = 0; i < 30; i++)
92 pwszRand[i] = GetRandUtf16();
93 pwszRand[30] = 0;
94
95 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
96 if (rc == VINF_SUCCESS)
97 {
98 rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
99 if (rc == VINF_SUCCESS)
100 {
101 RTStrFree(pszUtf8);
102 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
103 if (rc == VINF_SUCCESS)
104 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
105 else
106 RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
107 __LINE__, rc);
108 if (RT_SUCCESS(rc))
109 RTStrFree(pszUtf8);
110 RTStrFree(pszCurrent);
111 }
112 else
113 {
114 if (rc == VERR_NO_TRANSLATION)
115 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");
116 else if (rc == VWRN_NO_TRANSLATION)
117 RTTestPassed(hTest, "The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VWRN_NO_TRANSLATION. This is probably as it should be.\n");
118 else
119 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
120 __LINE__, rc);
121 if (RT_SUCCESS(rc))
122 RTStrFree(pszCurrent);
123 RTStrFree(pszUtf8);
124 }
125 }
126 else
127 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
128 __LINE__, rc);
129 RTMemFree(pwszRand);
130
131 /*
132 * Generate a new random string.
133 */
134 RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16");
135 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
136 for (int i = 0; i < 30; i++)
137 pwszRand[i] = GetRandUtf16();
138 pwszRand[30] = 0;
139 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
140 if (rc == VINF_SUCCESS)
141 {
142 rc = RTStrToUtf16(pszUtf8, &pwsz);
143 if (rc == VINF_SUCCESS)
144 {
145 int i;
146 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
147 /* nothing */;
148 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
149 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
150 else
151 {
152 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__);
153 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
154 }
155 RTUtf16Free(pwsz);
156 }
157 else
158 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
159 __LINE__, rc);
160 RTStrFree(pszUtf8);
161 }
162 else
163 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
164 __LINE__, rc);
165 RTMemFree(pwszRand);
166
167 /*
168 * Generate yet another random string and convert it to a buffer.
169 */
170 RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16");
171 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
172 for (int i = 0; i < 30; i++)
173 pwszRand[i] = GetRandUtf16();
174 pwszRand[30] = 0;
175
176 char szUtf8Array[120];
177 char *pszUtf8Array = szUtf8Array;
178 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
179 if (rc == 0)
180 {
181 rc = RTStrToUtf16(pszUtf8Array, &pwsz);
182 if (rc == 0)
183 {
184 int i;
185 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
186 ;
187 if (pwsz[i] == 0 && i >= 8)
188 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
189 else
190 {
191 RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
192 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
193 }
194 RTUtf16Free(pwsz);
195 }
196 else
197 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
198 }
199 else
200 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
201 RTMemFree(pwszRand);
202
203 /*
204 * And again.
205 */
206 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
207 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
208 for (int i = 0; i < 30; i++)
209 pwszRand[i] = GetRandUtf16();
210 pwszRand[30] = 0;
211
212 RTUTF16 wszBuf[70];
213 PRTUTF16 pwsz2Buf = wszBuf;
214 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
215 if (rc == 0)
216 {
217 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
218 if (rc == 0)
219 {
220 int i;
221 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
222 ;
223 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
224 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
225 else
226 {
227 RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
228 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
229 }
230 }
231 else
232 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
233 RTStrFree(pszUtf8);
234 }
235 else
236 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
237 __LINE__, rc);
238 RTMemFree(pwszRand);
239
240 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
241 for (int i = 0; i < 30; i++)
242 pwszRand[i] = GetRandUtf16();
243 pwszRand[30] = 0;
244
245 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
246 if (rc == VERR_BUFFER_OVERFLOW)
247 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
248 else
249 RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
250 __LINE__, rc);
251 RTMemFree(pwszRand);
252
253 /*
254 * last time...
255 */
256 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
257 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
258 for (int i = 0; i < 30; i++)
259 pwszRand[i] = GetRandUtf16();
260 pwszRand[30] = 0;
261
262 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
263 if (rc == VINF_SUCCESS)
264 {
265 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
266 if (rc == VERR_BUFFER_OVERFLOW)
267 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
268 else
269 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",
270 __LINE__, rc);
271 RTStrFree(pszUtf8);
272 }
273 else
274 RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
275 __LINE__, rc);
276 RTMemFree(pwszRand);
277
278 RTTestSubDone(hTest);
279}
280
281
282static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
283static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
284static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
285
286static void whereami(int cBits, size_t off)
287{
288 if (cBits == 8)
289 {
290 if (off < 0x7f)
291 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
292 else if (off < 0xf7f)
293 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
294 else if (off < 0x27f7f)
295 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
296 else if (off < 0x2df79)
297 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
298 else if (off < 0x42df79)
299 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
300 else
301 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
302 }
303 else if (cBits == 16)
304 {
305 if (off < 0xd7ff*2)
306 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
307 else if (off < 0xf7fd*2)
308 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
309 else if (off < 0x20f7fd)
310 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
311 else
312 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
313 }
314 else
315 {
316 if (off < (0xd800 - 1) * sizeof(RTUNICP))
317 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
318 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
319 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
320 else
321 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
322 }
323}
324
325int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
326{
327 const uint8_t *pb1 = (const uint8_t *)pv1;
328 const uint8_t *pb2 = (const uint8_t *)pv2;
329 for (size_t off = 0; off < cb; off++)
330 {
331 if (pb1[off] != pb2[off])
332 {
333 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
334 whereami(cBits, off);
335 if (off > 0)
336 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
337 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
338 for (size_t i = 1; i < 10; i++)
339 if (off + i < cb)
340 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+i, pb1[off+i], pb2[off+i]);
341 return 1;
342 }
343 }
344 return 0;
345}
346
347
348void InitStrings()
349{
350 /*
351 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
352 */
353 /* the simple code point array first */
354 unsigned i = 0;
355 RTUNICP uc = 1;
356 while (uc < 0xd800)
357 g_uszAll[i++] = uc++;
358 uc = 0xe000;
359 while (uc < 0xfffe)
360 g_uszAll[i++] = uc++;
361 uc = 0x10000;
362 while (uc < 0x110000)
363 g_uszAll[i++] = uc++;
364 g_uszAll[i++] = 0;
365 Assert(RT_ELEMENTS(g_uszAll) == i);
366
367 /* the utf-16 one */
368 i = 0;
369 uc = 1;
370 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
371 while (uc < 0xd800)
372 g_wszAll[i++] = uc++;
373 uc = 0xe000;
374 //RTPrintf(" %#x=%#x", i, uc);
375 while (uc < 0xfffe)
376 g_wszAll[i++] = uc++;
377 uc = 0x10000;
378 //RTPrintf(" %#x=%#x", i, uc);
379 while (uc < 0x110000)
380 {
381 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
382 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
383 uc++;
384 }
385 //RTPrintf(" %#x=%#x\n", i, uc);
386 g_wszAll[i++] = '\0';
387 Assert(RT_ELEMENTS(g_wszAll) == i);
388
389 /*
390 * The utf-8 one
391 */
392 i = 0;
393 uc = 1;
394 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
395 while (uc < 0x80)
396 g_szAll[i++] = uc++;
397 //RTPrintf(" %#x=%#x", i, uc);
398 while (uc < 0x800)
399 {
400 g_szAll[i++] = 0xc0 | (uc >> 6);
401 g_szAll[i++] = 0x80 | (uc & 0x3f);
402 Assert(!((uc >> 6) & ~0x1f));
403 uc++;
404 }
405 //RTPrintf(" %#x=%#x", i, uc);
406 while (uc < 0xd800)
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 = 0xe000;
415 //RTPrintf(" %#x=%#x", i, uc);
416 while (uc < 0xfffe)
417 {
418 g_szAll[i++] = 0xe0 | (uc >> 12);
419 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
420 g_szAll[i++] = 0x80 | (uc & 0x3f);
421 Assert(!((uc >> 12) & ~0xf));
422 uc++;
423 }
424 uc = 0x10000;
425 //RTPrintf(" %#x=%#x", i, uc);
426 while (uc < 0x110000)
427 {
428 g_szAll[i++] = 0xf0 | (uc >> 18);
429 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
430 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
431 g_szAll[i++] = 0x80 | (uc & 0x3f);
432 Assert(!((uc >> 18) & ~0x7));
433 uc++;
434 }
435 //RTPrintf(" %#x=%#x\n", i, uc);
436 g_szAll[i++] = '\0';
437 Assert(RT_ELEMENTS(g_szAll) == i);
438}
439
440
441void test2(RTTEST hTest)
442{
443 /*
444 * Convert to UTF-8 and back.
445 */
446 RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16");
447 char *pszUtf8;
448 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
449 if (rc == VINF_SUCCESS)
450 {
451 pszUtf8[0] = 1;
452 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
453 RTTestFailed(hTest, "UTF-16 -> UTF-8 mismatch!");
454
455 PRTUTF16 pwszUtf16;
456 rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
457 if (rc == VINF_SUCCESS)
458 {
459 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
460 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
461 RTUtf16Free(pwszUtf16);
462 }
463 else
464 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
465 RTStrFree(pszUtf8);
466 }
467 else
468 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
469
470
471 /*
472 * Convert to UTF-16 and back. (just in case the above test fails)
473 */
474 RTTestSub(hTest, "UTF-8 -> UTF-16 -> UTF-8");
475 PRTUTF16 pwszUtf16;
476 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
477 if (rc == VINF_SUCCESS)
478 {
479 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
480 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
481
482 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
483 if (rc == VINF_SUCCESS)
484 {
485 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
486 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
487 RTStrFree(pszUtf8);
488 }
489 else
490 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
491 RTUtf16Free(pwszUtf16);
492 }
493 else
494 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
495
496 /*
497 * Convert UTF-8 to CPs.
498 */
499 RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8");
500 PRTUNICP paCps;
501 rc = RTStrToUni(g_szAll, &paCps);
502 if (rc == VINF_SUCCESS)
503 {
504 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
505 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
506
507 size_t cCps;
508 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
509 if (rc == VINF_SUCCESS)
510 {
511 if (cCps != RT_ELEMENTS(g_uszAll) - 1)
512 RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
513 }
514 else
515 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
516
517 /** @todo RTCpsToUtf8 or something. */
518 RTUniFree(paCps);
519 }
520 else
521 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
522
523 /*
524 * Check the various string lengths.
525 */
526 RTTestSub(hTest, "Lengths");
527 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
528 size_t cuc2 = RTUtf16Len(g_wszAll);
529 if (cuc1 != cuc2)
530 RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
531 //size_t cuc3 = RTUniLen(g_uszAll);
532
533
534 /*
535 * Enumerate the strings.
536 */
537 RTTestSub(hTest, "Code Point Getters and Putters");
538 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
539 AssertRelease(pszPut1Base);
540 char *pszPut1 = pszPut1Base;
541 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
542 AssertRelease(pwszPut2Base);
543 PRTUTF16 pwszPut2 = pwszPut2Base;
544 const char *psz1 = g_szAll;
545 const char *psz2 = g_szAll;
546 PCRTUTF16 pwsz3 = g_wszAll;
547 PCRTUTF16 pwsz4 = g_wszAll;
548 for (;;)
549 {
550 /*
551 * getters
552 */
553 RTUNICP uc1;
554 rc = RTStrGetCpEx(&psz1, &uc1);
555 if (RT_FAILURE(rc))
556 {
557 RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
558 whereami(8, psz2 - &g_szAll[0]);
559 break;
560 }
561 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
562 if (pszPrev1 != psz2)
563 {
564 RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
565 whereami(8, psz2 - &g_szAll[0]);
566 break;
567 }
568 RTUNICP uc2 = RTStrGetCp(psz2);
569 if (uc2 != uc1)
570 {
571 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
572 whereami(8, psz2 - &g_szAll[0]);
573 break;
574 }
575 psz2 = RTStrNextCp(psz2);
576 if (psz2 != psz1)
577 {
578 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
579 whereami(8, psz2 - &g_szAll[0]);
580 break;
581 }
582
583 RTUNICP uc3;
584 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
585 if (RT_FAILURE(rc))
586 {
587 RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
588 whereami(16, pwsz4 - &g_wszAll[0]);
589 break;
590 }
591 if (uc3 != uc2)
592 {
593 RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
594 whereami(16, pwsz4 - &g_wszAll[0]);
595 break;
596 }
597 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
598 if (uc3 != uc4)
599 {
600 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
601 whereami(16, pwsz4 - &g_wszAll[0]);
602 break;
603 }
604 pwsz4 = RTUtf16NextCp(pwsz4);
605 if (pwsz4 != pwsz3)
606 {
607 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
608 whereami(8, pwsz4 - &g_wszAll[0]);
609 break;
610 }
611
612
613 /*
614 * putters
615 */
616 pszPut1 = RTStrPutCp(pszPut1, uc1);
617 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
618 {
619 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
620 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
621 whereami(8, psz2 - &g_szAll[0]);
622 break;
623 }
624
625 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
626 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
627 {
628 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
629 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
630 whereami(8, pwsz4 - &g_wszAll[0]);
631 break;
632 }
633
634
635 /* the end? */
636 if (!uc1)
637 break;
638 }
639
640 /* check output if we seems to have made it thru it all. */
641 if (psz2 == &g_szAll[sizeof(g_szAll)])
642 {
643 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
644 RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
645 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
646 RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
647 }
648
649 RTMemFree(pszPut1Base);
650 RTMemFree(pwszPut2Base);
651
652 RTTestSubDone(hTest);
653}
654
655
656/**
657 * Check case insensitivity.
658 */
659void test3(RTTEST hTest)
660{
661 RTTestSub(hTest, "Case Sensitivity");
662
663 if ( RTUniCpToLower('a') != 'a'
664 || RTUniCpToLower('A') != 'a'
665 || RTUniCpToLower('b') != 'b'
666 || RTUniCpToLower('B') != 'b'
667 || RTUniCpToLower('Z') != 'z'
668 || RTUniCpToLower('z') != 'z'
669 || RTUniCpToUpper('c') != 'C'
670 || RTUniCpToUpper('C') != 'C'
671 || RTUniCpToUpper('z') != 'Z'
672 || RTUniCpToUpper('Z') != 'Z')
673 RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
674
675 if (RTUtf16ICmp(g_wszAll, g_wszAll))
676 RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
677
678 if (RTUtf16Cmp(g_wszAll, g_wszAll))
679 RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
680
681 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 };
682 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 };
683 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
684 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
685 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
686 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
687 )
688 RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
689
690 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
691 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
692 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
693 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
694 )
695 RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
696
697 RTTestSubDone(hTest);
698}
699
700
701/**
702 * Test the RTStr*Cmp functions.
703 */
704void TstRTStrXCmp(RTTEST hTest)
705{
706#define CHECK_DIFF(expr, op) \
707 do \
708 { \
709 int iDiff = expr; \
710 if (!(iDiff op 0)) \
711 RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
712 } while (0)
713
714/** @todo test the non-ascii bits. */
715
716 RTTestSub(hTest, "RTStrCmp");
717 CHECK_DIFF(RTStrCmp(NULL, NULL), == );
718 CHECK_DIFF(RTStrCmp(NULL, ""), < );
719 CHECK_DIFF(RTStrCmp("", NULL), > );
720 CHECK_DIFF(RTStrCmp("", ""), == );
721 CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
722 CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
723 CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
724 CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
725 CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
726 CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
727 CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
728
729
730 RTTestSub(hTest, "RTStrNCmp");
731 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
732 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
733 CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
734 CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
735 CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
736 CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
737 CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
738 CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
739 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
740 CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
741 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
742
743 CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
744 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
745 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
746
747
748 RTTestSub(hTest, "RTStrICmp");
749 CHECK_DIFF(RTStrICmp(NULL, NULL), == );
750 CHECK_DIFF(RTStrICmp(NULL, ""), < );
751 CHECK_DIFF(RTStrICmp("", NULL), > );
752 CHECK_DIFF(RTStrICmp("", ""), == );
753 CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
754 CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
755 CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
756 CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
757 CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
758
759 CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
760 CHECK_DIFF(RTStrICmp("abcdef", "abcdeF"), ==);
761 CHECK_DIFF(RTStrICmp("ABCDEF", "abcdef"), ==);
762 CHECK_DIFF(RTStrICmp("abcdef", "ABCDEF"), ==);
763 CHECK_DIFF(RTStrICmp("AbCdEf", "aBcDeF"), ==);
764 CHECK_DIFF(RTStrICmp("AbCdEg", "aBcDeF"), > );
765 CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
766
767
768 RTTestSub(hTest, "RTStrICmpAscii");
769 CHECK_DIFF(RTStrICmpAscii(NULL, NULL), == );
770 CHECK_DIFF(RTStrICmpAscii(NULL, ""), < );
771 CHECK_DIFF(RTStrICmpAscii("", NULL), > );
772 CHECK_DIFF(RTStrICmpAscii("", ""), == );
773 CHECK_DIFF(RTStrICmpAscii("abcdef", "abcdef"), == );
774 CHECK_DIFF(RTStrICmpAscii("abcdef", "abcde"), > );
775 CHECK_DIFF(RTStrICmpAscii("abcde", "abcdef"), < );
776 CHECK_DIFF(RTStrICmpAscii("abcdeg", "abcdef"), > );
777 CHECK_DIFF(RTStrICmpAscii("abcdef", "abcdeg"), < );
778
779 CHECK_DIFF(RTStrICmpAscii("abcdeF", "abcdef"), ==);
780 CHECK_DIFF(RTStrICmpAscii("abcdef", "abcdeF"), ==);
781 CHECK_DIFF(RTStrICmpAscii("ABCDEF", "abcdef"), ==);
782 CHECK_DIFF(RTStrICmpAscii("abcdef", "ABCDEF"), ==);
783 CHECK_DIFF(RTStrICmpAscii("AbCdEf", "aBcDeF"), ==);
784 CHECK_DIFF(RTStrICmpAscii("AbCdEg", "aBcDeF"), > );
785 CHECK_DIFF(RTStrICmpAscii("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
786
787
788 RTTestSub(hTest, "RTStrNICmp");
789 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
790 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
791 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
792 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
793 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
794 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
795 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
796 CHECK_DIFF(RTStrNICmp("", "", 0), == );
797 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
798 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
799 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
800 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
801 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
802
803 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
804 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
805 CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
806 CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
807 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
808 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
809 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
810
811 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
812 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
813 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
814 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
815 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
816 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
817 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
818 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
819 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
820 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
821
822 RTTestSubDone(hTest);
823}
824
825
826
827/**
828 * Check UTF-8 encoding purging.
829 */
830void TstRTStrPurgeEncoding(RTTEST hTest)
831{
832 RTTestSub(hTest, "RTStrPurgeEncoding");
833
834 /*
835 * Test some good strings.
836 */
837 char sz1[] = "1234567890wertyuiopsdfghjklzxcvbnm";
838 char sz1Copy[sizeof(sz1)];
839 memcpy(sz1Copy, sz1, sizeof(sz1));
840
841 RTTESTI_CHECK_RETV(RTStrPurgeEncoding(sz1) == 0);
842 RTTESTI_CHECK_RETV(!memcmp(sz1, sz1Copy, sizeof(sz1)));
843
844 char *pszAll = RTStrDup(g_szAll);
845 if (pszAll)
846 {
847 RTTESTI_CHECK(RTStrPurgeEncoding(pszAll) == 0);
848 RTTESTI_CHECK(!memcmp(pszAll, g_szAll, sizeof(g_szAll)));
849 RTStrFree(pszAll);
850 }
851
852 /*
853 * Test some bad stuff.
854 */
855 struct
856 {
857 size_t cErrors;
858 unsigned char szIn[5];
859 const char *pszExpect;
860 } aTests[] =
861 {
862 { 0, { '1', '2', '3', '4', '\0' }, "1234" },
863 { 1, { 0x80, '2', '3', '4', '\0' }, "?234" },
864 { 1, { '1', 0x80, '3', '4', '\0' }, "1?34" },
865 { 1, { '1', '2', 0x80, '4', '\0' }, "12?4" },
866 { 1, { '1', '2', '3', 0x80, '\0' }, "123?" },
867 { 2, { 0x80, 0x81, '3', '4', '\0' }, "??34" },
868 { 2, { '1', 0x80, 0x81, '4', '\0' }, "1??4" },
869 { 2, { '1', '2', 0x80, 0x81, '\0' }, "12??" },
870 };
871 for (size_t i = 0; i < RT_ELEMENTS(aTests); i++)
872 {
873 size_t cErrors = RTStrPurgeEncoding((char *)aTests[i].szIn);
874 if (cErrors != aTests[i].cErrors)
875 RTTestFailed(hTest, "#%u: cErrors=%u expected %u\n", i, cErrors, aTests[i].cErrors);
876 else if (strcmp((char *)aTests[i].szIn, aTests[i].pszExpect))
877 RTTestFailed(hTest, "#%u: %.5Rhxs expected %.5Rhxs (%s)\n", i, aTests[i].szIn, aTests[i].pszExpect, aTests[i].pszExpect);
878 }
879
880 RTTestSubDone(hTest);
881}
882
883
884/**
885 * Check string sanitising.
886 */
887void TstRTStrPurgeComplementSet(RTTEST hTest)
888{
889 RTTestSub(hTest, "RTStrPurgeComplementSet");
890 RTUNICP aCpSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
891 '\0' };
892 RTUNICP aCpBadSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
893 '7', '\0' }; /* Contains an incomplete pair. */
894 struct
895 {
896 const char *pcszIn;
897 const char *pcszOut;
898 PCRTUNICP pcCpSet;
899 char chReplacement;
900 ssize_t cExpected;
901 }
902 aTests[] =
903 {
904 { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
905 { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
906 "123_54wert__trew___4321", aCpSet, '_', 3 },
907 { "hjhj8766", "????????", aCpSet, '?', 8 },
908 { "123\xf0\xa4\xad\xa2""4", "123____4", aCpSet, '_', 1 },
909 { "\xff", "\xff", aCpSet, '_', -1 },
910 { "____", "____", aCpBadSet, '_', -1 }
911 };
912 enum { MAX_IN_STRING = 256 };
913
914 for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
915 {
916 char szCopy[MAX_IN_STRING];
917 ssize_t cReplacements;
918 AssertRC(RTStrCopy(szCopy, RT_ELEMENTS(szCopy), aTests[i].pcszIn));
919 RTTestDisableAssertions(hTest);
920 cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet, aTests[i].chReplacement);
921 RTTestRestoreAssertions(hTest);
922 if (cReplacements != aTests[i].cExpected)
923 RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
924 (long long) aTests[i].cExpected,
925 (long long) cReplacements);
926 if (strcmp(aTests[i].pcszOut, szCopy))
927 RTTestFailed(hTest, "#%u: expected %s, actual %s\n", i,
928 aTests[i].pcszOut, szCopy);
929 }
930}
931
932
933/**
934 * Check string sanitising.
935 */
936void TstRTUtf16PurgeComplementSet(RTTEST hTest)
937{
938 RTTestSub(hTest, "RTUtf16PurgeComplementSet");
939 RTUNICP aCpSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
940 '\0' };
941 RTUNICP aCpBadSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
942 '7', '\0' }; /* Contains an incomplete pair. */
943 struct
944 {
945 const char *pcszIn;
946 const char *pcszOut;
947 size_t cwc; /* Zero means the strings are Utf-8. */
948 PCRTUNICP pcCpSet;
949 char chReplacement;
950 ssize_t cExpected;
951 }
952 aTests[] =
953 {
954 { "1234werttrew4321", "1234werttrew4321", 0, aCpSet, '_', 0 },
955 { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
956 "123_54wert_trew_4321", 0, aCpSet, '_', 3 },
957 { "hjhj8766", "????????", 0, aCpSet, '?', 8 },
958 { "123\xf0\xa4\xad\xa2""4", "123__4", 0, aCpSet, '_', 1 },
959 { "\xff\xff\0", "\xff\xff\0", 2, aCpSet, '_', -1 },
960 { "\xff\xff\0", "\xff\xff\0", 2, aCpSet, '_', -1 },
961 { "____", "____", 0, aCpBadSet, '_', -1 }
962 };
963 enum { MAX_IN_STRING = 256 };
964
965 for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
966 {
967 RTUTF16 wszInCopy[MAX_IN_STRING], *pwszInCopy = wszInCopy;
968 RTUTF16 wszOutCopy[MAX_IN_STRING], *pwszOutCopy = wszOutCopy;
969 ssize_t cReplacements;
970 if (!aTests[i].cwc)
971 {
972 AssertRC(RTStrToUtf16Ex(aTests[i].pcszIn, RTSTR_MAX, &pwszInCopy,
973 RT_ELEMENTS(wszInCopy), NULL));
974 AssertRC(RTStrToUtf16Ex(aTests[i].pcszOut, RTSTR_MAX, &pwszOutCopy,
975 RT_ELEMENTS(wszOutCopy), NULL));
976 }
977 else
978 {
979 Assert(aTests[i].cwc <= RT_ELEMENTS(wszInCopy));
980 memcpy(wszInCopy, aTests[i].pcszIn, aTests[i].cwc * 2);
981 memcpy(wszOutCopy, aTests[i].pcszOut, aTests[i].cwc * 2);
982 }
983
984 RTTestDisableAssertions(hTest);
985 cReplacements = RTUtf16PurgeComplementSet(wszInCopy, aTests[i].pcCpSet, aTests[i].chReplacement);
986 RTTestRestoreAssertions(hTest);
987
988 if (cReplacements != aTests[i].cExpected)
989 RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
990 (long long) aTests[i].cExpected,
991 (long long) cReplacements);
992 if (RTUtf16Cmp(wszInCopy, wszOutCopy))
993 RTTestFailed(hTest, "#%u: expected %ls, actual %ls\n", i,
994 wszOutCopy, wszInCopy);
995 }
996}
997
998
999/**
1000 * Benchmark stuff.
1001 */
1002void Benchmarks(RTTEST hTest)
1003{
1004 static union
1005 {
1006 RTUTF16 wszBuf[sizeof(g_wszAll)];
1007 char szBuf[sizeof(g_szAll)];
1008 } s_Buf;
1009
1010 RTTestSub(hTest, "Benchmarks");
1011/** @todo add RTTest* methods for reporting benchmark results. */
1012 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
1013 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
1014 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
1015 if (RT_SUCCESS(rc))
1016 {
1017 int i;
1018 uint64_t u64Start = RTTimeNanoTS();
1019 for (i = 0; i < 100; i++)
1020 {
1021 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
1022 if (RT_FAILURE(rc))
1023 {
1024 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
1025 break;
1026 }
1027 }
1028 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
1029 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
1030 }
1031
1032 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
1033 char *psz = &s_Buf.szBuf[0];
1034 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
1035 if (RT_SUCCESS(rc))
1036 {
1037 int i;
1038 uint64_t u64Start = RTTimeNanoTS();
1039 for (i = 0; i < 100; i++)
1040 {
1041 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
1042 if (RT_FAILURE(rc))
1043 {
1044 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
1045 break;
1046 }
1047 }
1048 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
1049 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
1050 }
1051
1052 RTTestSubDone(hTest);
1053}
1054
1055
1056/**
1057 * Tests RTStrEnd
1058 */
1059static void testStrEnd(RTTEST hTest)
1060{
1061 RTTestSub(hTest, "RTStrEnd");
1062
1063 static char const s_szEmpty[1] = "";
1064 RTTESTI_CHECK(RTStrEnd(s_szEmpty, 0) == NULL);
1065 RTTESTI_CHECK(RTStrEnd(s_szEmpty, 1) == &s_szEmpty[0]);
1066 for (size_t i = 0; i < _1M; i++)
1067 RTTESTI_CHECK(RTStrEnd(s_szEmpty, ~i) == &s_szEmpty[0]);
1068
1069 /* Check the implementation won't ever overshoot the '\0' in the input in
1070 anyway that may lead to a SIGSEV. (VC++ 14.1 does this) */
1071 size_t const cchStr = 1023;
1072 char *pszStr = (char *)RTTestGuardedAllocTail(hTest, cchStr + 1);
1073 memset(pszStr, ' ', cchStr);
1074 char * const pszStrEnd = &pszStr[cchStr];
1075 *pszStrEnd = '\0';
1076 RTTEST_CHECK_RETV(hTest, strlen(pszStr) == cchStr);
1077
1078 for (size_t off = 0; off <= cchStr; off++)
1079 {
1080 RTTEST_CHECK(hTest, RTStrEnd(&pszStr[off], cchStr + 1 - off) == pszStrEnd);
1081 RTTEST_CHECK(hTest, RTStrEnd(&pszStr[off], RTSTR_MAX) == pszStrEnd);
1082
1083 RTTEST_CHECK(hTest, memchr(&pszStr[off], '\0', cchStr + 1 - off) == pszStrEnd);
1084 RTTEST_CHECK(hTest, strchr(&pszStr[off], '\0') == pszStrEnd);
1085 RTTEST_CHECK(hTest, strchr(&pszStr[off], '?') == NULL);
1086
1087 size_t cchMax = 0;
1088 for (; cchMax <= cchStr - off; cchMax++)
1089 {
1090 const char *pszRet = RTStrEnd(&pszStr[off], cchMax);
1091 if (pszRet != NULL)
1092 {
1093 RTTestFailed(hTest, "off=%zu cchMax=%zu: %p, expected NULL\n", off, cchMax, pszRet);
1094 break;
1095 }
1096 }
1097 for (; cchMax <= _8K; cchMax++)
1098 {
1099 const char *pszRet = RTStrEnd(&pszStr[off], cchMax);
1100 if (pszRet != pszStrEnd)
1101 {
1102 RTTestFailed(hTest, "off=%zu cchMax=%zu: off by %p\n", off, cchMax, pszRet);
1103 break;
1104 }
1105 }
1106 }
1107 RTTestGuardedFree(hTest, pszStr);
1108}
1109
1110
1111/**
1112 * Tests RTStrStr and RTStrIStr.
1113 */
1114static void testStrStr(RTTEST hTest)
1115{
1116#define CHECK_NULL(expr) \
1117 do { \
1118 const char *pszRet = expr; \
1119 if (pszRet != NULL) \
1120 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
1121 } while (0)
1122
1123#define CHECK(expr, expect) \
1124 do { \
1125 const char *pszRet = expr; \
1126 const char *pszExpect = (expect); \
1127 if ( (pszRet != NULL && pszExpect == NULL) \
1128 || (pszRet == NULL && pszExpect != NULL) \
1129 || strcmp(pszRet, pszExpect) \
1130 ) \
1131 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, pszExpect); \
1132 } while (0)
1133
1134
1135 RTTestSub(hTest, "RTStrStr");
1136 CHECK(RTStrStr("abcdef", ""), "abcdef");
1137 CHECK_NULL(RTStrStr("abcdef", NULL));
1138 CHECK_NULL(RTStrStr(NULL, ""));
1139 CHECK_NULL(RTStrStr(NULL, NULL));
1140 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
1141 CHECK(RTStrStr("abcdef", "b"), "bcdef");
1142 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
1143 CHECK(RTStrStr("abcdef", "cdef"), "cdef");
1144 CHECK(RTStrStr("abcdef", "cde"), "cdef");
1145 CHECK(RTStrStr("abcdef", "cd"), "cdef");
1146 CHECK(RTStrStr("abcdef", "c"), "cdef");
1147 CHECK(RTStrStr("abcdef", "f"), "f");
1148 CHECK(RTStrStr("abcdef", "ef"), "ef");
1149 CHECK(RTStrStr("abcdef", "e"), "ef");
1150 CHECK_NULL(RTStrStr("abcdef", "z"));
1151 CHECK_NULL(RTStrStr("abcdef", "A"));
1152 CHECK_NULL(RTStrStr("abcdef", "F"));
1153
1154 RTTestSub(hTest, "RTStrIStr");
1155 CHECK(RTStrIStr("abcdef", ""), "abcdef");
1156 CHECK_NULL(RTStrIStr("abcdef", NULL));
1157 CHECK_NULL(RTStrIStr(NULL, ""));
1158 CHECK_NULL(RTStrIStr(NULL, NULL));
1159 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
1160 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
1161 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
1162 CHECK(RTStrIStr("abcdef", "b"), "bcdef");
1163 CHECK(RTStrIStr("abcdef", "B"), "bcdef");
1164 CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef");
1165 CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef");
1166 CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef");
1167 CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef");
1168 CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef");
1169 CHECK(RTStrIStr("abcdef", "cdef"), "cdef");
1170 CHECK(RTStrIStr("abcdef", "cde"), "cdef");
1171 CHECK(RTStrIStr("abcdef", "cd"), "cdef");
1172 CHECK(RTStrIStr("abcdef", "c"), "cdef");
1173 CHECK(RTStrIStr("abcdef", "f"), "f");
1174 CHECK(RTStrIStr("abcdeF", "F"), "F");
1175 CHECK(RTStrIStr("abcdef", "F"), "f");
1176 CHECK(RTStrIStr("abcdef", "ef"), "ef");
1177 CHECK(RTStrIStr("EeEef", "e"), "EeEef");
1178 CHECK(RTStrIStr("EeEef", "E"), "EeEef");
1179 CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
1180 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
1181 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
1182 CHECK_NULL(RTStrIStr("EeEef", "z"));
1183
1184#undef CHECK
1185#undef CHECK_NULL
1186 RTTestSubDone(hTest);
1187}
1188
1189
1190void testUtf8Latin1(RTTEST hTest)
1191{
1192 RTTestSub(hTest, "Latin-1 <-> Utf-8 conversion functions");
1193
1194 /* Test Utf8 -> Latin1 */
1195 size_t cch_szAll = 0;
1196 size_t cbShort = RTStrCalcLatin1Len(g_szAll);
1197 RTTEST_CHECK(hTest, cbShort == 0);
1198 int rc = RTStrCalcLatin1LenEx(g_szAll, 383, &cch_szAll);
1199 RTTEST_CHECK(hTest, (cch_szAll == 255));
1200 rc = RTStrCalcLatin1LenEx(g_szAll, RTSTR_MAX, &cch_szAll);
1201 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1202 char *psz = NULL;
1203 char szShort[256] = { 0 };
1204 memcpy(szShort, g_szAll, 255);
1205 cbShort = RTStrCalcLatin1Len(szShort);
1206 RTTEST_CHECK(hTest, cbShort == 191);
1207 rc = RTStrToLatin1(szShort, &psz);
1208 RTTEST_CHECK_RC_OK(hTest, rc);
1209 if (RT_SUCCESS(rc))
1210 {
1211 RTTEST_CHECK(hTest, (strlen(psz) == 191));
1212 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1213 if (psz[i] != (char) j)
1214 {
1215 RTTestFailed(hTest, "conversion of g_szAll to Latin1 failed at position %u\n", i);
1216 break;
1217 }
1218 }
1219 RTStrFree(psz);
1220 rc = RTStrToLatin1(g_szAll, &psz);
1221 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1222 char sz[512];
1223 char *psz2 = &sz[0];
1224 size_t cchActual = 0;
1225 rc = RTStrToLatin1Ex(g_szAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1226 &cchActual);
1227 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1228 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1229 (hTest, "cchActual=%lu\n", cchActual));
1230 rc = RTStrToLatin1Ex(g_szAll, 383, &psz2, sizeof(sz),
1231 &cchActual);
1232 RTTEST_CHECK_RC_OK(hTest, rc);
1233 if (RT_SUCCESS(rc))
1234 {
1235 RTTEST_CHECK(hTest, (cchActual == 255));
1236 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1237 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1238 if (psz2[i] != (char) j)
1239 {
1240 RTTestFailed(hTest, "second conversion of g_szAll to Latin1 failed at position %u\n", i);
1241 break;
1242 }
1243 }
1244 rc = RTStrToLatin1Ex(g_szAll, 129, &psz2, 128, &cchActual);
1245 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1246 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1247 (hTest, "cchActual=%lu\n", cchActual));
1248 rc = RTStrToLatin1Ex(g_szAll, 383, &psz, 0, &cchActual);
1249 RTTEST_CHECK_RC_OK(hTest, rc);
1250 if (RT_SUCCESS(rc))
1251 {
1252 RTTEST_CHECK(hTest, (cchActual == 255));
1253 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1254 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1255 if ( ((j < 0x100) && (psz[i] != (char) j))
1256 || ((j > 0xff) && psz[i] != '?'))
1257 {
1258 RTTestFailed(hTest, "third conversion of g_szAll to Latin1 failed at position %u\n", i);
1259 break;
1260 }
1261 }
1262 const char *pszBad = "Hello\xDC\xD8";
1263 rc = RTStrToLatin1Ex(pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1264 &cchActual);
1265 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF8_ENCODING);
1266 RTStrFree(psz);
1267
1268 /* Test Latin1 -> Utf8 */
1269 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1270 RTTEST_CHECK(hTest, RTLatin1CalcUtf8Len(pszLat1) == 7);
1271 rc = RTLatin1CalcUtf8LenEx(pszLat1, 3, &cchActual);
1272 RTTEST_CHECK_RC_OK(hTest, rc);
1273 if (RT_SUCCESS(rc))
1274 RTTEST_CHECK(hTest, cchActual == 3);
1275 rc = RTLatin1CalcUtf8LenEx(pszLat1, RTSTR_MAX, &cchActual);
1276 RTTEST_CHECK_RC_OK(hTest, rc);
1277 if (RT_SUCCESS(rc))
1278 RTTEST_CHECK(hTest, cchActual == 7);
1279 char *pch = NULL;
1280 char ch[8];
1281 char *pch2 = &ch[0];
1282 cchActual = 0;
1283 rc = RTLatin1ToUtf8(pszLat1, &pch);
1284 RTTEST_CHECK_RC_OK(hTest, rc);
1285 if (RT_SUCCESS(rc))
1286 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1287 RTStrFree(pch);
1288 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, &cchActual);
1289 RTTEST_CHECK_RC_OK(hTest, rc);
1290 if (RT_SUCCESS(rc))
1291 {
1292 RTTEST_CHECK(hTest, (cchActual == 7));
1293 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1294 }
1295 RTStrFree(pch);
1296 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, NULL);
1297 RTTEST_CHECK_RC_OK(hTest, rc);
1298 if (RT_SUCCESS(rc))
1299 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1300 RTStrFree(pch);
1301 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch),
1302 &cchActual);
1303 RTTEST_CHECK_RC_OK(hTest, rc);
1304 if (RT_SUCCESS(rc))
1305 {
1306 RTTEST_CHECK(hTest, (cchActual == 7));
1307 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40\xC2\x80\xC2\x81"));
1308 }
1309 rc = RTLatin1ToUtf8Ex(pszLat1, 3, &pch2, RT_ELEMENTS(ch),
1310 &cchActual);
1311 RTTEST_CHECK_RC_OK(hTest, rc);
1312 if (RT_SUCCESS(rc))
1313 {
1314 RTTEST_CHECK(hTest, (cchActual == 3));
1315 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40"));
1316 }
1317 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch) - 1,
1318 &cchActual);
1319 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1320 RTTEST_CHECK(hTest, (cchActual == 7));
1321 RTTestSubDone(hTest);
1322}
1323
1324
1325void testUtf16Latin1(RTTEST hTest)
1326{
1327 RTTestSub(hTest, "Latin-1 <-> Utf-16 conversion functions");
1328
1329 /* Test Utf16 -> Latin1 */
1330 size_t cch_szAll = 0;
1331 size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
1332 RTTEST_CHECK(hTest, cbShort == 0);
1333 int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
1334 RTTEST_CHECK(hTest, (cch_szAll == 255));
1335 rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
1336 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1337 char *psz = NULL;
1338 RTUTF16 wszShort[256] = { 0 };
1339 for (unsigned i = 0; i < 255; ++i)
1340 wszShort[i] = i + 1;
1341 cbShort = RTUtf16CalcLatin1Len(wszShort);
1342 RTTEST_CHECK(hTest, cbShort == 255);
1343 rc = RTUtf16ToLatin1(wszShort, &psz);
1344 RTTEST_CHECK_RC_OK(hTest, rc);
1345 if (RT_SUCCESS(rc))
1346 {
1347 RTTEST_CHECK(hTest, (strlen(psz) == 255));
1348 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1349 if (psz[i] != (char) j)
1350 {
1351 RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
1352 break;
1353 }
1354 }
1355 RTStrFree(psz);
1356 rc = RTUtf16ToLatin1(g_wszAll, &psz);
1357 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1358 char sz[512];
1359 char *psz2 = &sz[0];
1360 size_t cchActual = 0;
1361 rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1362 &cchActual);
1363 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1364 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1365 (hTest, "cchActual=%lu\n", cchActual));
1366 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
1367 &cchActual);
1368 RTTEST_CHECK_RC_OK(hTest, rc);
1369 if (RT_SUCCESS(rc))
1370 {
1371 RTTEST_CHECK(hTest, (cchActual == 255));
1372 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1373 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1374 if (psz2[i] != (char) j)
1375 {
1376 RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
1377 break;
1378 }
1379 }
1380 rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
1381 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1382 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1383 (hTest, "cchActual=%lu\n", cchActual));
1384 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
1385 RTTEST_CHECK_RC_OK(hTest, rc);
1386 if (RT_SUCCESS(rc))
1387 {
1388 RTTEST_CHECK(hTest, (cchActual == 255));
1389 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1390 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1391 if ( ((j < 0x100) && (psz[i] != (char) j))
1392 || ((j > 0xff) && psz[i] != '?'))
1393 {
1394 RTTestFailed(hTest, "third conversion of g_wszAll to Latin1 failed at position %u\n", i);
1395 break;
1396 }
1397 }
1398 const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
1399 rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1400 &cchActual);
1401 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
1402 RTStrFree(psz);
1403
1404 /* Test Latin1 -> Utf16 */
1405 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1406 RTTEST_CHECK(hTest, RTLatin1CalcUtf16Len(pszLat1) == 5);
1407 rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
1408 RTTEST_CHECK_RC_OK(hTest, rc);
1409 if (RT_SUCCESS(rc))
1410 RTTEST_CHECK(hTest, cchActual == 3);
1411 rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
1412 RTTEST_CHECK_RC_OK(hTest, rc);
1413 if (RT_SUCCESS(rc))
1414 RTTEST_CHECK(hTest, cchActual == 5);
1415 RTUTF16 *pwc = NULL;
1416 RTUTF16 wc[6];
1417 RTUTF16 *pwc2 = &wc[0];
1418 size_t cwActual = 0;
1419 rc = RTLatin1ToUtf16(pszLat1, &pwc);
1420 RTTEST_CHECK_RC_OK(hTest, rc);
1421 if (RT_SUCCESS(rc))
1422 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1423 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1424 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1425 RTUtf16Free(pwc);
1426 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, &cwActual);
1427 RTTEST_CHECK_RC_OK(hTest, rc);
1428 if (RT_SUCCESS(rc))
1429 {
1430 RTTEST_CHECK(hTest, (cwActual == 5));
1431 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1432 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1433 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1434 }
1435 RTUtf16Free(pwc);
1436 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, NULL);
1437 RTTEST_CHECK_RC_OK(hTest, rc);
1438 if (RT_SUCCESS(rc))
1439 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1440 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1441 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1442 RTUtf16Free(pwc);
1443 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc),
1444 &cwActual);
1445 RTTEST_CHECK_RC_OK(hTest, rc);
1446 if (RT_SUCCESS(rc))
1447 {
1448 RTTEST_CHECK(hTest, (cwActual == 5));
1449 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1450 && (wc[2] == 0x40) && (wc[3] == 0x80)
1451 && (wc[4] == 0x81) && (wc[5] == '\0'));
1452 }
1453 rc = RTLatin1ToUtf16Ex(pszLat1, 3, &pwc2, RT_ELEMENTS(wc),
1454 &cwActual);
1455 RTTEST_CHECK_RC_OK(hTest, rc);
1456 if (RT_SUCCESS(rc))
1457 {
1458 RTTEST_CHECK(hTest, (cwActual == 3));
1459 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1460 && (wc[2] == 0x40) && (wc[3] == '\0'));
1461 }
1462 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc) - 1,
1463 &cwActual);
1464 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1465 RTTEST_CHECK(hTest, (cwActual == 5));
1466 RTTestSubDone(hTest);
1467}
1468
1469
1470static void testNoTransation(RTTEST hTest)
1471{
1472 /*
1473 * Try trigger a VERR_NO_TRANSLATION error in convert to
1474 * current CP to latin-1.
1475 */
1476 const RTUTF16 s_swzTest1[] = { 0x2358, 0x2242, 0x2357, 0x2359, 0x22f9, 0x2c4e, 0x0030, 0x0060,
1477 0x0092, 0x00c1, 0x00f2, 0x1f80, 0x0088, 0x2c38, 0x2c30, 0x0000 };
1478 char *pszTest1;
1479 int rc = RTUtf16ToUtf8(s_swzTest1, &pszTest1);
1480 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
1481
1482 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP");
1483 char *pszOut;
1484 rc = RTStrUtf8ToCurrentCP(&pszOut, pszTest1);
1485 if (rc == VINF_SUCCESS)
1486 {
1487 RTTestIPrintf(RTTESTLVL_ALWAYS, "CurrentCP is UTF-8 or similar (LC_ALL=%s LANG=%s LC_CTYPE=%s)\n",
1488 RTEnvGet("LC_ALL"), RTEnvGet("LANG"), RTEnvGet("LC_CTYPE"));
1489 if (strcmp(pszOut, pszTest1))
1490 RTTestFailed(hTest, "mismatch\nutf8: %.*Rhxs\n got: %.*Rhxs\n", strlen(pszTest1), pszTest1, strlen(pszOut), pszOut);
1491 RTStrFree(pszOut);
1492 }
1493 else
1494 RTTESTI_CHECK_MSG(rc == VWRN_NO_TRANSLATION || rc == VERR_NO_TRANSLATION, ("rc=%Rrc\n", rc));
1495
1496 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
1497 rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
1498 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1499 if (RT_SUCCESS(rc))
1500 RTStrFree(pszOut);
1501
1502 RTStrFree(pszTest1);
1503 RTTestSubDone(hTest);
1504}
1505
1506static void testGetPut(RTTEST hTest)
1507{
1508 /*
1509 * Test RTStrPutCp, RTStrGetCp and RTStrGetCpEx.
1510 */
1511 RTTestSub(hTest, "RTStrPutCp, RTStrGetCp and RTStrGetCpEx");
1512
1513 RTUNICP uc = 0;
1514 while (uc <= 0x10fffd)
1515 {
1516 /* Figure the range - skip illegal ranges. */
1517 RTUNICP ucFirst = uc;
1518 if (ucFirst - UINT32_C(0xd800) <= 0x7ff)
1519 ucFirst = 0xe000;
1520 else if (ucFirst == UINT32_C(0xfffe) || ucFirst == UINT32_C(0xffff))
1521 ucFirst = 0x10000;
1522
1523 RTUNICP ucLast = ucFirst + 1023;
1524 if (ucLast - UINT32_C(0xd800) <= 0x7ff)
1525 ucLast = 0xd7ff;
1526 else if (ucLast == UINT32_C(0xfffe) || ucLast == UINT32_C(0xffff))
1527 ucLast = 0xfffd;
1528
1529 /* Encode the range into a string, decode each code point as we go along. */
1530 char sz1[8192];
1531 char *pszDst = sz1;
1532 for (uc = ucFirst; uc <= ucLast; uc++)
1533 {
1534 char *pszBefore = pszDst;
1535 pszDst = RTStrPutCp(pszDst, uc);
1536 RTTESTI_CHECK(pszBefore - pszDst < 6);
1537
1538 RTUNICP uc2 = RTStrGetCp(pszBefore);
1539 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1540
1541 const char *pszSrc = pszBefore;
1542 RTUNICP uc3 = 42;
1543 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1544 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1545 RTTESTI_CHECK_MSG(pszSrc == pszDst, ("pszSrc=%p pszDst=%p\n", pszSrc, pszDst));
1546 }
1547
1548 /* Decode and re-encode it. */
1549 const char *pszSrc = pszDst = sz1;
1550 for (uc = ucFirst; uc <= ucLast; uc++)
1551 {
1552 RTUNICP uc2 = RTStrGetCp(pszSrc);
1553 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1554
1555 RTUNICP uc3 = 42;
1556 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1557 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1558
1559 pszDst = RTStrPutCp(pszDst, uc);
1560 RTTESTI_CHECK_MSG(pszSrc == pszDst, ("pszSrc=%p pszDst=%p\n", pszSrc, pszDst));
1561 pszSrc = pszDst;
1562 }
1563
1564 /* Decode and wipe it (checking compiler optimizations). */
1565 pszSrc = pszDst = sz1;
1566 for (uc = ucFirst; uc <= ucLast; uc++)
1567 {
1568 RTUNICP uc2 = RTStrGetCp(pszSrc);
1569 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1570
1571 RTUNICP uc3 = 42;
1572 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1573 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1574
1575 pszDst = RTStrPutCp(pszDst, 0);
1576 }
1577
1578 /* advance */
1579 uc = ucLast + 1;
1580 }
1581
1582}
1583
1584
1585int main()
1586{
1587 /*
1588 * Init the runtime, test and say hello.
1589 */
1590 RTTEST hTest;
1591 RTEXITCODE rcExit = RTTestInitAndCreate("tstUtf8", &hTest);
1592 if (rcExit != RTEXITCODE_SUCCESS)
1593 return rcExit;
1594 RTTestBanner(hTest);
1595
1596 /*
1597 * Run the tests.
1598 */
1599 InitStrings();
1600 test1(hTest);
1601 test2(hTest);
1602 test3(hTest);
1603 TstRTStrXCmp(hTest);
1604 TstRTStrPurgeEncoding(hTest);
1605 /* TstRT*PurgeComplementSet test conditions which assert. */
1606 TstRTStrPurgeComplementSet(hTest);
1607 TstRTUtf16PurgeComplementSet(hTest);
1608 testStrEnd(hTest);
1609 testStrStr(hTest);
1610 testUtf8Latin1(hTest);
1611 testUtf16Latin1(hTest);
1612 testNoTransation(hTest);
1613 testGetPut(hTest);
1614
1615 Benchmarks(hTest);
1616
1617 /*
1618 * Summary
1619 */
1620 return RTTestSummaryAndDestroy(hTest);
1621}
1622
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