VirtualBox

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

最後變更 在這個檔案從76346是 76346,由 vboxsync 提交於 6 年 前

*: Preparing for iprt/string.h, iprt/json.h and iprt/serialport.h no longer including iprt/err.h and string.h no longer including latin1.h (it needs err.h). bugref:9344

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette