VirtualBox

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

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

tstUtf8: More details on VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP on testboxsh1.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 54.8 KB
 
1/* $Id: tstUtf8.cpp 45257 2013-03-30 20:34:52Z vboxsync $ */
2/** @file
3 * IPRT Testcase - UTF-8 and UTF-16 string conversions.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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* Header Files *
29*******************************************************************************/
30#include <iprt/string.h>
31
32#include <iprt/alloc.h>
33#include <iprt/assert.h>
34#include <iprt/env.h>
35#include <iprt/err.h>
36#include <iprt/rand.h>
37#include <iprt/stream.h>
38#include <iprt/test.h>
39#include <iprt/time.h>
40#include <iprt/uni.h>
41#include <iprt/uuid.h>
42
43
44
45/**
46 * Generate a random codepoint for simple UTF-16 encoding.
47 */
48static RTUTF16 GetRandUtf16(void)
49{
50 RTUTF16 wc;
51 do
52 {
53 wc = (RTUTF16)RTRandU32Ex(1, 0xfffd);
54 } while (wc >= 0xd800 && wc <= 0xdfff);
55 return wc;
56}
57
58
59/**
60 *
61 */
62static void test1(RTTEST hTest)
63{
64 static const char s_szBadString1[] = "Bad \xe0\x13\x0";
65 static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
66 int rc;
67 char *pszUtf8;
68 char *pszCurrent;
69 PRTUTF16 pwsz;
70 PRTUTF16 pwszRand;
71
72 /*
73 * Invalid UTF-8 to UCS-2 test.
74 */
75 RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16");
76 rc = RTStrToUtf16(s_szBadString1, &pwsz);
77 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
78 (hTest, "Conversion of first bad UTF-8 string to UTF-16 apparently succeeded. It shouldn't. rc=%Rrc\n", rc));
79 rc = RTStrToUtf16(s_szBadString2, &pwsz);
80 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
81 (hTest, "Conversion of second bad UTF-8 strings to UTF-16 apparently succeeded. It shouldn't. rc=%Rrc\n", rc));
82
83 /*
84 * Test current CP conversion.
85 */
86 RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
87 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
88 for (int i = 0; i < 30; i++)
89 pwszRand[i] = GetRandUtf16();
90 pwszRand[30] = 0;
91
92 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
93 if (rc == VINF_SUCCESS)
94 {
95 rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
96 if (rc == VINF_SUCCESS)
97 {
98 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
99 if (rc == VINF_SUCCESS)
100 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
101 else
102 RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
103 __LINE__, rc);
104 }
105 else if (rc == VERR_NO_TRANSLATION)
106 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");
107 else
108 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
109 __LINE__, rc);
110 }
111 else
112 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
113 __LINE__, rc);
114
115 /*
116 * Generate a new random string.
117 */
118 RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16");
119 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
120 for (int i = 0; i < 30; i++)
121 pwszRand[i] = GetRandUtf16();
122 pwszRand[30] = 0;
123 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
124 if (rc == VINF_SUCCESS)
125 {
126 rc = RTStrToUtf16(pszUtf8, &pwsz);
127 if (rc == VINF_SUCCESS)
128 {
129 int i;
130 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
131 /* nothing */;
132 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
133 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
134 else
135 {
136 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__);
137 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
138 }
139 }
140 else
141 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
142 __LINE__, rc);
143 }
144 else
145 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
146 __LINE__, rc);
147
148 /*
149 * Generate yet another random string and convert it to a buffer.
150 */
151 RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16");
152 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
153 for (int i = 0; i < 30; i++)
154 pwszRand[i] = GetRandUtf16();
155 pwszRand[30] = 0;
156
157 char szUtf8Array[120];
158 char *pszUtf8Array = szUtf8Array;
159 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
160 if (rc == 0)
161 {
162 rc = RTStrToUtf16(pszUtf8Array, &pwsz);
163 if (rc == 0)
164 {
165 int i;
166 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
167 ;
168 if (pwsz[i] == 0 && i >= 8)
169 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
170 else
171 {
172 RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
173 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
174 }
175 }
176 else
177 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
178 }
179 else
180 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
181
182 /*
183 * And again.
184 */
185 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
186 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
187 for (int i = 0; i < 30; i++)
188 pwszRand[i] = GetRandUtf16();
189 pwszRand[30] = 0;
190
191 RTUTF16 wszBuf[70];
192 PRTUTF16 pwsz2Buf = wszBuf;
193 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
194 if (rc == 0)
195 {
196 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
197 if (rc == 0)
198 {
199 int i;
200 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
201 ;
202 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
203 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
204 else
205 {
206 RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
207 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
208 }
209 }
210 else
211 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
212 }
213 else
214 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
215 __LINE__, rc);
216 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
217 for (int i = 0; i < 30; i++)
218 pwszRand[i] = GetRandUtf16();
219 pwszRand[30] = 0;
220
221 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
222 if (rc == VERR_BUFFER_OVERFLOW)
223 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
224 else
225 RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
226 __LINE__, rc);
227
228 /*
229 * last time...
230 */
231 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
232 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
233 for (int i = 0; i < 30; i++)
234 pwszRand[i] = GetRandUtf16();
235 pwszRand[30] = 0;
236
237 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
238 if (rc == VINF_SUCCESS)
239 {
240 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
241 if (rc == VERR_BUFFER_OVERFLOW)
242 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
243 else
244 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",
245 __LINE__, rc);
246 }
247 else
248 RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
249 __LINE__, rc);
250
251
252 RTTestSubDone(hTest);
253}
254
255
256static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
257static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
258static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
259
260static void whereami(int cBits, size_t off)
261{
262 if (cBits == 8)
263 {
264 if (off < 0x7f)
265 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
266 else if (off < 0xf7f)
267 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
268 else if (off < 0x27f7f)
269 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
270 else if (off < 0x2df79)
271 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
272 else if (off < 0x42df79)
273 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
274 else
275 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
276 }
277 else if (cBits == 16)
278 {
279 if (off < 0xd7ff*2)
280 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
281 else if (off < 0xf7fd*2)
282 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
283 else if (off < 0x20f7fd)
284 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
285 else
286 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
287 }
288 else
289 {
290 if (off < (0xd800 - 1) * sizeof(RTUNICP))
291 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
292 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
293 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
294 else
295 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
296 }
297}
298
299int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
300{
301 const uint8_t *pb1 = (const uint8_t *)pv1;
302 const uint8_t *pb2 = (const uint8_t *)pv2;
303 for (size_t off = 0; off < cb; off++)
304 {
305 if (pb1[off] != pb2[off])
306 {
307 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
308 whereami(cBits, off);
309 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
310 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
311 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
312 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
313 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
314 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
315 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
316 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
317 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
318 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
319 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
320 return 1;
321 }
322 }
323 return 0;
324}
325
326
327void InitStrings()
328{
329 /*
330 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
331 */
332 /* the simple code point array first */
333 unsigned i = 0;
334 RTUNICP uc = 1;
335 while (uc < 0xd800)
336 g_uszAll[i++] = uc++;
337 uc = 0xe000;
338 while (uc < 0xfffe)
339 g_uszAll[i++] = uc++;
340 uc = 0x10000;
341 while (uc < 0x110000)
342 g_uszAll[i++] = uc++;
343 g_uszAll[i++] = 0;
344 Assert(RT_ELEMENTS(g_uszAll) == i);
345
346 /* the utf-16 one */
347 i = 0;
348 uc = 1;
349 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
350 while (uc < 0xd800)
351 g_wszAll[i++] = uc++;
352 uc = 0xe000;
353 //RTPrintf(" %#x=%#x", i, uc);
354 while (uc < 0xfffe)
355 g_wszAll[i++] = uc++;
356 uc = 0x10000;
357 //RTPrintf(" %#x=%#x", i, uc);
358 while (uc < 0x110000)
359 {
360 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
361 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
362 uc++;
363 }
364 //RTPrintf(" %#x=%#x\n", i, uc);
365 g_wszAll[i++] = '\0';
366 Assert(RT_ELEMENTS(g_wszAll) == i);
367
368 /*
369 * The utf-8 one
370 */
371 i = 0;
372 uc = 1;
373 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
374 while (uc < 0x80)
375 g_szAll[i++] = uc++;
376 //RTPrintf(" %#x=%#x", i, uc);
377 while (uc < 0x800)
378 {
379 g_szAll[i++] = 0xc0 | (uc >> 6);
380 g_szAll[i++] = 0x80 | (uc & 0x3f);
381 Assert(!((uc >> 6) & ~0x1f));
382 uc++;
383 }
384 //RTPrintf(" %#x=%#x", i, uc);
385 while (uc < 0xd800)
386 {
387 g_szAll[i++] = 0xe0 | (uc >> 12);
388 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
389 g_szAll[i++] = 0x80 | (uc & 0x3f);
390 Assert(!((uc >> 12) & ~0xf));
391 uc++;
392 }
393 uc = 0xe000;
394 //RTPrintf(" %#x=%#x", i, uc);
395 while (uc < 0xfffe)
396 {
397 g_szAll[i++] = 0xe0 | (uc >> 12);
398 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
399 g_szAll[i++] = 0x80 | (uc & 0x3f);
400 Assert(!((uc >> 12) & ~0xf));
401 uc++;
402 }
403 uc = 0x10000;
404 //RTPrintf(" %#x=%#x", i, uc);
405 while (uc < 0x110000)
406 {
407 g_szAll[i++] = 0xf0 | (uc >> 18);
408 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
409 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
410 g_szAll[i++] = 0x80 | (uc & 0x3f);
411 Assert(!((uc >> 18) & ~0x7));
412 uc++;
413 }
414 //RTPrintf(" %#x=%#x\n", i, uc);
415 g_szAll[i++] = '\0';
416 Assert(RT_ELEMENTS(g_szAll) == i);
417}
418
419
420void test2(RTTEST hTest)
421{
422 /*
423 * Convert to UTF-8 and back.
424 */
425 RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16");
426 char *pszUtf8;
427 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
428 if (rc == VINF_SUCCESS)
429 {
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
746 RTTestSub(hTest, "RTStrNICmp");
747 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
748 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
749 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
750 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
751 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
752 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
753 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
754 CHECK_DIFF(RTStrNICmp("", "", 0), == );
755 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
756 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
757 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
758 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
759 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
760
761 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
762 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
763 CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
764 CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
765 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
766 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
767 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
768
769 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
770 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
771 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
772 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
773 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
774 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
775 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
776 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
777 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
778 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
779
780 RTTestSubDone(hTest);
781}
782
783
784
785/**
786 * Check UTF-8 encoding purging.
787 */
788void TstRTStrPurgeEncoding(RTTEST hTest)
789{
790 RTTestSub(hTest, "RTStrPurgeEncoding");
791
792 /*
793 * Test some good strings.
794 */
795 char sz1[] = "1234567890wertyuiopsdfghjklzxcvbnm";
796 char sz1Copy[sizeof(sz1)];
797 memcpy(sz1Copy, sz1, sizeof(sz1));
798
799 RTTESTI_CHECK_RETV(RTStrPurgeEncoding(sz1) == 0);
800 RTTESTI_CHECK_RETV(!memcmp(sz1, sz1Copy, sizeof(sz1)));
801
802 char *pszAll = RTStrDup(g_szAll);
803 if (pszAll)
804 {
805 RTTESTI_CHECK(RTStrPurgeEncoding(pszAll) == 0);
806 RTTESTI_CHECK(!memcmp(pszAll, g_szAll, sizeof(g_szAll)));
807 RTStrFree(pszAll);
808 }
809
810 /*
811 * Test some bad stuff.
812 */
813 struct
814 {
815 size_t cErrors;
816 unsigned char szIn[5];
817 const char *pszExpect;
818 } aTests[] =
819 {
820 { 0, { '1', '2', '3', '4', '\0' }, "1234" },
821 { 1, { 0x80, '2', '3', '4', '\0' }, "?234" },
822 { 1, { '1', 0x80, '3', '4', '\0' }, "1?34" },
823 { 1, { '1', '2', 0x80, '4', '\0' }, "12?4" },
824 { 1, { '1', '2', '3', 0x80, '\0' }, "123?" },
825 { 2, { 0x80, 0x81, '3', '4', '\0' }, "??34" },
826 { 2, { '1', 0x80, 0x81, '4', '\0' }, "1??4" },
827 { 2, { '1', '2', 0x80, 0x81, '\0' }, "12??" },
828 };
829 for (size_t i = 0; i < RT_ELEMENTS(aTests); i++)
830 {
831 size_t cErrors = RTStrPurgeEncoding((char *)aTests[i].szIn);
832 if (cErrors != aTests[i].cErrors)
833 RTTestFailed(hTest, "#%u: cErrors=%u expected %u\n", i, cErrors, aTests[i].cErrors);
834 else if (strcmp((char *)aTests[i].szIn, aTests[i].pszExpect))
835 RTTestFailed(hTest, "#%u: %.5Rhxs expected %.5Rhxs (%s)\n", i, aTests[i].szIn, aTests[i].pszExpect, aTests[i].pszExpect);
836 }
837
838 RTTestSubDone(hTest);
839}
840
841
842/**
843 * Check string sanitising.
844 */
845void TstRTStrPurgeComplementSet(RTTEST hTest)
846{
847 RTTestSub(hTest, "RTStrPurgeComplementSet");
848 RTUNICP aCpSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
849 '\0' };
850 RTUNICP aCpBadSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
851 '7', '\0' }; /* Contains an incomplete pair. */
852 struct
853 {
854 const char *pcszIn;
855 const char *pcszOut;
856 PCRTUNICP pcCpSet;
857 char chReplacement;
858 ssize_t cExpected;
859 }
860 aTests[] =
861 {
862 { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
863 { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
864 "123_54wert__trew___4321", aCpSet, '_', 3 },
865 { "hjhj8766", "????????", aCpSet, '?', 8 },
866 { "123\xf0\xa4\xad\xa2""4", "123____4", aCpSet, '_', 1 },
867 { "\xff", "\xff", aCpSet, '_', -1 },
868 { "____", "____", aCpBadSet, '_', -1 }
869 };
870 enum { MAX_IN_STRING = 256 };
871
872 for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
873 {
874 char szCopy[MAX_IN_STRING];
875 ssize_t cReplacements;
876 AssertRC(RTStrCopy(szCopy, RT_ELEMENTS(szCopy), aTests[i].pcszIn));
877 cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet,
878 aTests[i].chReplacement);
879 if (cReplacements != aTests[i].cExpected)
880 RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
881 (long long) aTests[i].cExpected,
882 (long long) cReplacements);
883 if (strcmp(aTests[i].pcszOut, szCopy))
884 RTTestFailed(hTest, "#%u: expected %s, actual %s\n", i,
885 aTests[i].pcszOut, szCopy);
886 }
887}
888
889
890/**
891 * Check string sanitising.
892 */
893void TstRTUtf16PurgeComplementSet(RTTEST hTest)
894{
895 RTTestSub(hTest, "RTUtf16PurgeComplementSet");
896 RTUNICP aCpSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
897 '\0' };
898 RTUNICP aCpBadSet[] = { '1', '5', 'w', 'w', 'r', 'r', 'e', 'f', 't', 't',
899 '7', '\0' }; /* Contains an incomplete pair. */
900 struct
901 {
902 const char *pcszIn;
903 const char *pcszOut;
904 size_t cwc; /* Zero means the strings are Utf-8. */
905 PCRTUNICP pcCpSet;
906 char chReplacement;
907 ssize_t cExpected;
908 }
909 aTests[] =
910 {
911 { "1234werttrew4321", "1234werttrew4321", 0, aCpSet, '_', 0 },
912 { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
913 "123_54wert_trew_4321", 0, aCpSet, '_', 3 },
914 { "hjhj8766", "????????", 0, aCpSet, '?', 8 },
915 { "123\xf0\xa4\xad\xa2""4", "123__4", 0, aCpSet, '_', 1 },
916 { "\xff\xff\0", "\xff\xff\0", 2, aCpSet, '_', -1 },
917 { "\xff\xff\0", "\xff\xff\0", 2, aCpSet, '_', -1 },
918 { "____", "____", 0, aCpBadSet, '_', -1 }
919 };
920 enum { MAX_IN_STRING = 256 };
921
922 for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
923 {
924 RTUTF16 wszInCopy[MAX_IN_STRING], *pwszInCopy = wszInCopy;
925 RTUTF16 wszOutCopy[MAX_IN_STRING], *pwszOutCopy = wszOutCopy;
926 ssize_t cReplacements;
927 if (!aTests[i].cwc)
928 {
929 AssertRC(RTStrToUtf16Ex(aTests[i].pcszIn, RTSTR_MAX, &pwszInCopy,
930 RT_ELEMENTS(wszInCopy), NULL));
931 AssertRC(RTStrToUtf16Ex(aTests[i].pcszOut, RTSTR_MAX, &pwszOutCopy,
932 RT_ELEMENTS(wszOutCopy), NULL));
933 }
934 else
935 {
936 Assert(aTests[i].cwc <= RT_ELEMENTS(wszInCopy));
937 memcpy(wszInCopy, aTests[i].pcszIn, aTests[i].cwc * 2);
938 memcpy(wszOutCopy, aTests[i].pcszOut, aTests[i].cwc * 2);
939 }
940 cReplacements = RTUtf16PurgeComplementSet(wszInCopy, aTests[i].pcCpSet,
941 aTests[i].chReplacement);
942 if (cReplacements != aTests[i].cExpected)
943 RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
944 (long long) aTests[i].cExpected,
945 (long long) cReplacements);
946 if (RTUtf16Cmp(wszInCopy, wszOutCopy))
947 RTTestFailed(hTest, "#%u: expected %ls, actual %ls\n", i,
948 wszOutCopy, wszInCopy);
949 }
950}
951
952
953/**
954 * Benchmark stuff.
955 */
956void Benchmarks(RTTEST hTest)
957{
958 static union
959 {
960 RTUTF16 wszBuf[sizeof(g_wszAll)];
961 char szBuf[sizeof(g_szAll)];
962 } s_Buf;
963
964 RTTestSub(hTest, "Benchmarks");
965/** @todo add RTTest* methods for reporting benchmark results. */
966 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
967 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
968 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
969 if (RT_SUCCESS(rc))
970 {
971 int i;
972 uint64_t u64Start = RTTimeNanoTS();
973 for (i = 0; i < 100; i++)
974 {
975 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
976 if (RT_FAILURE(rc))
977 {
978 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
979 break;
980 }
981 }
982 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
983 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
984 }
985
986 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
987 char *psz = &s_Buf.szBuf[0];
988 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
989 if (RT_SUCCESS(rc))
990 {
991 int i;
992 uint64_t u64Start = RTTimeNanoTS();
993 for (i = 0; i < 100; i++)
994 {
995 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
996 if (RT_FAILURE(rc))
997 {
998 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
999 break;
1000 }
1001 }
1002 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
1003 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
1004 }
1005
1006 RTTestSubDone(hTest);
1007}
1008
1009
1010/**
1011 * Tests RTStrEnd
1012 */
1013static void testStrEnd(RTTEST hTest)
1014{
1015 RTTestSub(hTest, "RTStrEnd");
1016
1017 static char const s_szEmpty[1] = "";
1018 RTTESTI_CHECK(RTStrEnd(s_szEmpty, 0) == NULL);
1019 RTTESTI_CHECK(RTStrEnd(s_szEmpty, 1) == &s_szEmpty[0]);
1020 for (size_t i = 0; i < _1M; i++)
1021 RTTESTI_CHECK(RTStrEnd(s_szEmpty, ~i) == &s_szEmpty[0]);
1022
1023}
1024
1025
1026/**
1027 * Tests RTStrStr and RTStrIStr.
1028 */
1029static void testStrStr(RTTEST hTest)
1030{
1031#define CHECK_NULL(expr) \
1032 do { \
1033 const char *pszRet = expr; \
1034 if (pszRet != NULL) \
1035 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
1036 } while (0)
1037
1038#define CHECK(expr, expect) \
1039 do { \
1040 const char *pszRet = expr; \
1041 if ( (pszRet != NULL && (expect) == NULL) \
1042 || (pszRet == NULL && (expect) != NULL) \
1043 || strcmp(pszRet, (expect)) \
1044 ) \
1045 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
1046 } while (0)
1047
1048
1049 RTTestSub(hTest, "RTStrStr");
1050 CHECK(RTStrStr("abcdef", ""), "abcdef");
1051 CHECK_NULL(RTStrStr("abcdef", NULL));
1052 CHECK_NULL(RTStrStr(NULL, ""));
1053 CHECK_NULL(RTStrStr(NULL, NULL));
1054 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
1055 CHECK(RTStrStr("abcdef", "b"), "bcdef");
1056 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
1057 CHECK(RTStrStr("abcdef", "cdef"), "cdef");
1058 CHECK(RTStrStr("abcdef", "cde"), "cdef");
1059 CHECK(RTStrStr("abcdef", "cd"), "cdef");
1060 CHECK(RTStrStr("abcdef", "c"), "cdef");
1061 CHECK(RTStrStr("abcdef", "f"), "f");
1062 CHECK(RTStrStr("abcdef", "ef"), "ef");
1063 CHECK(RTStrStr("abcdef", "e"), "ef");
1064 CHECK_NULL(RTStrStr("abcdef", "z"));
1065 CHECK_NULL(RTStrStr("abcdef", "A"));
1066 CHECK_NULL(RTStrStr("abcdef", "F"));
1067
1068 RTTestSub(hTest, "RTStrIStr");
1069 CHECK(RTStrIStr("abcdef", ""), "abcdef");
1070 CHECK_NULL(RTStrIStr("abcdef", NULL));
1071 CHECK_NULL(RTStrIStr(NULL, ""));
1072 CHECK_NULL(RTStrIStr(NULL, NULL));
1073 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
1074 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
1075 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
1076 CHECK(RTStrIStr("abcdef", "b"), "bcdef");
1077 CHECK(RTStrIStr("abcdef", "B"), "bcdef");
1078 CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef");
1079 CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef");
1080 CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef");
1081 CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef");
1082 CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef");
1083 CHECK(RTStrIStr("abcdef", "cdef"), "cdef");
1084 CHECK(RTStrIStr("abcdef", "cde"), "cdef");
1085 CHECK(RTStrIStr("abcdef", "cd"), "cdef");
1086 CHECK(RTStrIStr("abcdef", "c"), "cdef");
1087 CHECK(RTStrIStr("abcdef", "f"), "f");
1088 CHECK(RTStrIStr("abcdeF", "F"), "F");
1089 CHECK(RTStrIStr("abcdef", "F"), "f");
1090 CHECK(RTStrIStr("abcdef", "ef"), "ef");
1091 CHECK(RTStrIStr("EeEef", "e"), "EeEef");
1092 CHECK(RTStrIStr("EeEef", "E"), "EeEef");
1093 CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
1094 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
1095 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
1096 CHECK_NULL(RTStrIStr("EeEef", "z"));
1097
1098#undef CHECK
1099#undef CHECK_NULL
1100 RTTestSubDone(hTest);
1101}
1102
1103
1104void testUtf8Latin1(RTTEST hTest)
1105{
1106 RTTestSub(hTest, "Latin-1 <-> Utf-8 conversion functions");
1107
1108 /* Test Utf8 -> Latin1 */
1109 size_t cch_szAll = 0;
1110 size_t cbShort = RTStrCalcLatin1Len(g_szAll);
1111 RTTEST_CHECK(hTest, cbShort == 0);
1112 int rc = RTStrCalcLatin1LenEx(g_szAll, 383, &cch_szAll);
1113 RTTEST_CHECK(hTest, (cch_szAll == 255));
1114 rc = RTStrCalcLatin1LenEx(g_szAll, RTSTR_MAX, &cch_szAll);
1115 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1116 char *psz = NULL;
1117 char szShort[256] = { 0 };
1118 memcpy(szShort, g_szAll, 255);
1119 cbShort = RTStrCalcLatin1Len(szShort);
1120 RTTEST_CHECK(hTest, cbShort == 191);
1121 rc = RTStrToLatin1(szShort, &psz);
1122 RTTEST_CHECK_RC_OK(hTest, rc);
1123 if (RT_SUCCESS(rc))
1124 {
1125 RTTEST_CHECK(hTest, (strlen(psz) == 191));
1126 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1127 if (psz[i] != (char) j)
1128 {
1129 RTTestFailed(hTest, "conversion of g_szAll to Latin1 failed at position %u\n", i);
1130 break;
1131 }
1132 }
1133 RTStrFree(psz);
1134 rc = RTStrToLatin1(g_szAll, &psz);
1135 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1136 char sz[512];
1137 char *psz2 = &sz[0];
1138 size_t cchActual = 0;
1139 rc = RTStrToLatin1Ex(g_szAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1140 &cchActual);
1141 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1142 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1143 (hTest, "cchActual=%lu\n", cchActual));
1144 rc = RTStrToLatin1Ex(g_szAll, 383, &psz2, sizeof(sz),
1145 &cchActual);
1146 RTTEST_CHECK_RC_OK(hTest, rc);
1147 if (RT_SUCCESS(rc))
1148 {
1149 RTTEST_CHECK(hTest, (cchActual == 255));
1150 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1151 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1152 if (psz2[i] != (char) j)
1153 {
1154 RTTestFailed(hTest, "second conversion of g_szAll to Latin1 failed at position %u\n", i);
1155 break;
1156 }
1157 }
1158 rc = RTStrToLatin1Ex(g_szAll, 129, &psz2, 128, &cchActual);
1159 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1160 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1161 (hTest, "cchActual=%lu\n", cchActual));
1162 rc = RTStrToLatin1Ex(g_szAll, 383, &psz, 0, &cchActual);
1163 RTTEST_CHECK_RC_OK(hTest, rc);
1164 if (RT_SUCCESS(rc))
1165 {
1166 RTTEST_CHECK(hTest, (cchActual == 255));
1167 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1168 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1169 if ( ((j < 0x100) && (psz[i] != (char) j))
1170 || ((j > 0xff) && psz[i] != '?'))
1171 {
1172 RTTestFailed(hTest, "third conversion of g_szAll to Latin1 failed at position %u\n", i);
1173 break;
1174 }
1175 }
1176 const char *pszBad = "Hello\xDC\xD8";
1177 rc = RTStrToLatin1Ex(pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1178 &cchActual);
1179 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF8_ENCODING);
1180 RTStrFree(psz);
1181
1182 /* Test Latin1 -> Utf8 */
1183 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1184 RTTEST_CHECK(hTest, RTLatin1CalcUtf8Len(pszLat1) == 7);
1185 rc = RTLatin1CalcUtf8LenEx(pszLat1, 3, &cchActual);
1186 RTTEST_CHECK_RC_OK(hTest, rc);
1187 if (RT_SUCCESS(rc))
1188 RTTEST_CHECK(hTest, cchActual == 3);
1189 rc = RTLatin1CalcUtf8LenEx(pszLat1, RTSTR_MAX, &cchActual);
1190 RTTEST_CHECK_RC_OK(hTest, rc);
1191 if (RT_SUCCESS(rc))
1192 RTTEST_CHECK(hTest, cchActual == 7);
1193 char *pch = NULL;
1194 char ch[8];
1195 char *pch2 = &ch[0];
1196 cchActual = 0;
1197 rc = RTLatin1ToUtf8(pszLat1, &pch);
1198 RTTEST_CHECK_RC_OK(hTest, rc);
1199 if (RT_SUCCESS(rc))
1200 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1201 RTStrFree(pch);
1202 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, &cchActual);
1203 RTTEST_CHECK_RC_OK(hTest, rc);
1204 if (RT_SUCCESS(rc))
1205 {
1206 RTTEST_CHECK(hTest, (cchActual == 7));
1207 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1208 }
1209 RTStrFree(pch);
1210 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch, 0, NULL);
1211 RTTEST_CHECK_RC_OK(hTest, rc);
1212 if (RT_SUCCESS(rc))
1213 RTTEST_CHECK(hTest, !strcmp(pch, "\x01\x20\x40\xC2\x80\xC2\x81"));
1214 RTStrFree(pch);
1215 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch),
1216 &cchActual);
1217 RTTEST_CHECK_RC_OK(hTest, rc);
1218 if (RT_SUCCESS(rc))
1219 {
1220 RTTEST_CHECK(hTest, (cchActual == 7));
1221 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40\xC2\x80\xC2\x81"));
1222 }
1223 rc = RTLatin1ToUtf8Ex(pszLat1, 3, &pch2, RT_ELEMENTS(ch),
1224 &cchActual);
1225 RTTEST_CHECK_RC_OK(hTest, rc);
1226 if (RT_SUCCESS(rc))
1227 {
1228 RTTEST_CHECK(hTest, (cchActual == 3));
1229 RTTEST_CHECK(hTest, !strcmp(pch2, "\x01\x20\x40"));
1230 }
1231 rc = RTLatin1ToUtf8Ex(pszLat1, RTSTR_MAX, &pch2, RT_ELEMENTS(ch) - 1,
1232 &cchActual);
1233 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1234 RTTEST_CHECK(hTest, (cchActual == 7));
1235 RTTestSubDone(hTest);
1236}
1237
1238
1239void testUtf16Latin1(RTTEST hTest)
1240{
1241 RTTestSub(hTest, "Latin-1 <-> Utf-16 conversion functions");
1242
1243 /* Test Utf16 -> Latin1 */
1244 size_t cch_szAll = 0;
1245 size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
1246 RTTEST_CHECK(hTest, cbShort == 0);
1247 int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
1248 RTTEST_CHECK(hTest, (cch_szAll == 255));
1249 rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
1250 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1251 char *psz = NULL;
1252 RTUTF16 wszShort[256] = { 0 };
1253 for (unsigned i = 0; i < 255; ++i)
1254 wszShort[i] = i + 1;
1255 cbShort = RTUtf16CalcLatin1Len(wszShort);
1256 RTTEST_CHECK(hTest, cbShort == 255);
1257 rc = RTUtf16ToLatin1(wszShort, &psz);
1258 RTTEST_CHECK_RC_OK(hTest, rc);
1259 if (RT_SUCCESS(rc))
1260 {
1261 RTTEST_CHECK(hTest, (strlen(psz) == 255));
1262 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1263 if (psz[i] != (char) j)
1264 {
1265 RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
1266 break;
1267 }
1268 }
1269 RTStrFree(psz);
1270 rc = RTUtf16ToLatin1(g_wszAll, &psz);
1271 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1272 char sz[512];
1273 char *psz2 = &sz[0];
1274 size_t cchActual = 0;
1275 rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
1276 &cchActual);
1277 RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
1278 RTTEST_CHECK_MSG(hTest, cchActual == 0,
1279 (hTest, "cchActual=%lu\n", cchActual));
1280 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
1281 &cchActual);
1282 RTTEST_CHECK_RC_OK(hTest, rc);
1283 if (RT_SUCCESS(rc))
1284 {
1285 RTTEST_CHECK(hTest, (cchActual == 255));
1286 RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
1287 for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
1288 if (psz2[i] != (char) j)
1289 {
1290 RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
1291 break;
1292 }
1293 }
1294 rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
1295 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1296 RTTEST_CHECK_MSG(hTest, cchActual == 128,
1297 (hTest, "cchActual=%lu\n", cchActual));
1298 rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
1299 RTTEST_CHECK_RC_OK(hTest, rc);
1300 if (RT_SUCCESS(rc))
1301 {
1302 RTTEST_CHECK(hTest, (cchActual == 255));
1303 RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
1304 for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
1305 if ( ((j < 0x100) && (psz[i] != (char) j))
1306 || ((j > 0xff) && psz[i] != '?'))
1307 {
1308 RTTestFailed(hTest, "third conversion of g_wszAll to Latin1 failed at position %u\n", i);
1309 break;
1310 }
1311 }
1312 const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
1313 rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
1314 &cchActual);
1315 RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
1316 RTStrFree(psz);
1317
1318 /* Test Latin1 -> Utf16 */
1319 const char *pszLat1 = "\x01\x20\x40\x80\x81";
1320 RTTEST_CHECK(hTest, RTLatin1CalcUtf16Len(pszLat1) == 5);
1321 rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
1322 RTTEST_CHECK_RC_OK(hTest, rc);
1323 if (RT_SUCCESS(rc))
1324 RTTEST_CHECK(hTest, cchActual == 3);
1325 rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
1326 RTTEST_CHECK_RC_OK(hTest, rc);
1327 if (RT_SUCCESS(rc))
1328 RTTEST_CHECK(hTest, cchActual == 5);
1329 RTUTF16 *pwc = NULL;
1330 RTUTF16 wc[6];
1331 RTUTF16 *pwc2 = &wc[0];
1332 size_t cwActual = 0;
1333 rc = RTLatin1ToUtf16(pszLat1, &pwc);
1334 RTTEST_CHECK_RC_OK(hTest, rc);
1335 if (RT_SUCCESS(rc))
1336 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1337 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1338 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1339 RTUtf16Free(pwc);
1340 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, &cwActual);
1341 RTTEST_CHECK_RC_OK(hTest, rc);
1342 if (RT_SUCCESS(rc))
1343 {
1344 RTTEST_CHECK(hTest, (cwActual == 5));
1345 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1346 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1347 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1348 }
1349 RTUtf16Free(pwc);
1350 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, NULL);
1351 RTTEST_CHECK_RC_OK(hTest, rc);
1352 if (RT_SUCCESS(rc))
1353 RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
1354 && (pwc[2] == 0x40) && (pwc[3] == 0x80)
1355 && (pwc[4] == 0x81) && (pwc[5] == '\0'));
1356 RTUtf16Free(pwc);
1357 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc),
1358 &cwActual);
1359 RTTEST_CHECK_RC_OK(hTest, rc);
1360 if (RT_SUCCESS(rc))
1361 {
1362 RTTEST_CHECK(hTest, (cwActual == 5));
1363 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1364 && (wc[2] == 0x40) && (wc[3] == 0x80)
1365 && (wc[4] == 0x81) && (wc[5] == '\0'));
1366 }
1367 rc = RTLatin1ToUtf16Ex(pszLat1, 3, &pwc2, RT_ELEMENTS(wc),
1368 &cwActual);
1369 RTTEST_CHECK_RC_OK(hTest, rc);
1370 if (RT_SUCCESS(rc))
1371 {
1372 RTTEST_CHECK(hTest, (cwActual == 3));
1373 RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
1374 && (wc[2] == 0x40) && (wc[3] == '\0'));
1375 }
1376 rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc) - 1,
1377 &cwActual);
1378 RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
1379 RTTEST_CHECK(hTest, (cwActual == 5));
1380 RTTestSubDone(hTest);
1381}
1382
1383
1384static void testNoTransation(RTTEST hTest)
1385{
1386 /*
1387 * Try trigger a VERR_NO_TRANSLATION error in convert to
1388 * current CP to latin-1.
1389 */
1390 const RTUTF16 s_swzTest1[] = { 0x2358, 0x2242, 0x2357, 0x2359, 0x22f9, 0x2c4e, 0x0030, 0x0060,
1391 0x0092, 0x00c1, 0x00f2, 0x1f80, 0x0088, 0x2c38, 0x2c30, 0x0000 };
1392 char *pszTest1;
1393 int rc = RTUtf16ToUtf8(s_swzTest1, &pszTest1);
1394 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
1395
1396 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP");
1397 char *pszOut;
1398 rc = RTStrUtf8ToCurrentCP(&pszOut, pszTest1);
1399 if (RT_SUCCESS(rc))
1400 {
1401 RTTESTI_CHECK(!strcmp(pszOut, pszTest1));
1402 RTTestIPrintf(RTTESTLVL_ALWAYS, "CurrentCP is UTF-8 or similar (LC_ALL=%s LANG=%s LC_CTYPE=%s)\n",
1403 RTEnvGet("LC_ALL"), RTEnvGet("LANG"), RTEnvGet("LC_CTYPE"));
1404 RTStrFree(pszOut);
1405 }
1406 else
1407 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1408
1409 RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
1410 rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
1411 RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
1412 if (RT_SUCCESS(rc))
1413 RTStrFree(pszOut);
1414
1415 RTStrFree(pszTest1);
1416 RTTestSubDone(hTest);
1417}
1418
1419static void testGetPut(RTTEST hTest)
1420{
1421 /*
1422 * Test RTStrPutCp, RTStrGetCp and RTStrGetCpEx.
1423 */
1424 RTTestSub(hTest, "RTStrPutCp, RTStrGetCp and RTStrGetCpEx");
1425
1426 RTUNICP uc = 0;
1427 while (uc <= 0x10fffd)
1428 {
1429 /* Figure the range - skip illegal ranges. */
1430 RTUNICP ucFirst = uc;
1431 if (ucFirst - UINT32_C(0xd800) <= 0x7ff)
1432 ucFirst = 0xe000;
1433 else if (ucFirst == UINT32_C(0xfffe) || ucFirst == UINT32_C(0xffff))
1434 ucFirst = 0x10000;
1435
1436 RTUNICP ucLast = ucFirst + 1023;
1437 if (ucLast - UINT32_C(0xd800) <= 0x7ff)
1438 ucLast = 0xd7ff;
1439 else if (ucLast == UINT32_C(0xfffe) || ucLast == UINT32_C(0xffff))
1440 ucLast = 0xfffd;
1441
1442 /* Encode the range into a string, decode each code point as we go along. */
1443 char sz1[8192];
1444 char *pszDst = sz1;
1445 for (uc = ucFirst; uc <= ucLast; uc++)
1446 {
1447 char *pszBefore = pszDst;
1448 pszDst = RTStrPutCp(pszDst, uc);
1449 RTTESTI_CHECK(pszBefore - pszDst < 6);
1450
1451 RTUNICP uc2 = RTStrGetCp(pszBefore);
1452 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1453
1454 const char *pszSrc = pszBefore;
1455 RTUNICP uc3 = 42;
1456 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1457 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1458 RTTESTI_CHECK_MSG(pszSrc == pszDst, ("pszSrc=%p pszDst=%p\n", pszSrc, pszDst));
1459 }
1460
1461 /* Decode and re-encode it. */
1462 const char *pszSrc = pszDst = sz1;
1463 for (uc = ucFirst; uc <= ucLast; uc++)
1464 {
1465 RTUNICP uc2 = RTStrGetCp(pszSrc);
1466 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1467
1468 RTUNICP uc3 = 42;
1469 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1470 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1471
1472 pszDst = RTStrPutCp(pszDst, uc);
1473 RTTESTI_CHECK_MSG(pszSrc == pszDst, ("pszSrc=%p pszDst=%p\n", pszSrc, pszDst));
1474 pszSrc = pszDst;
1475 }
1476
1477 /* Decode and wipe it (checking compiler optimizations). */
1478 pszSrc = pszDst = sz1;
1479 for (uc = ucFirst; uc <= ucLast; uc++)
1480 {
1481 RTUNICP uc2 = RTStrGetCp(pszSrc);
1482 RTTESTI_CHECK_MSG(uc2 == uc, ("uc2=%#x uc=%#x\n", uc2, uc));
1483
1484 RTUNICP uc3 = 42;
1485 RTTESTI_CHECK_RC(RTStrGetCpEx(&pszSrc, &uc3), VINF_SUCCESS);
1486 RTTESTI_CHECK_MSG(uc3 == uc, ("uc3=%#x uc=%#x\n", uc3, uc));
1487
1488 pszDst = RTStrPutCp(pszDst, 0);
1489 }
1490
1491 /* advance */
1492 uc = ucLast + 1;
1493 }
1494
1495}
1496
1497
1498int main()
1499{
1500 /*
1501 * Init the runtime, test and say hello.
1502 */
1503 RTTEST hTest;
1504 RTEXITCODE rcExit = RTTestInitAndCreate("tstUtf8", &hTest);
1505 if (rcExit != RTEXITCODE_SUCCESS)
1506 return rcExit;
1507 RTTestBanner(hTest);
1508
1509 /*
1510 * Run the tests.
1511 */
1512 InitStrings();
1513 test1(hTest);
1514 test2(hTest);
1515 test3(hTest);
1516 TstRTStrXCmp(hTest);
1517 TstRTStrPurgeEncoding(hTest);
1518 /* TstRT*PurgeComplementSet test conditions which assert. */
1519 bool fAreQuiet = RTAssertAreQuiet(), fMayPanic = RTAssertMayPanic();
1520 RTAssertSetQuiet(true);
1521 RTAssertSetMayPanic(false);
1522 TstRTStrPurgeComplementSet(hTest);
1523 TstRTUtf16PurgeComplementSet(hTest);
1524 RTAssertSetQuiet(fAreQuiet);
1525 RTAssertSetMayPanic(fMayPanic);
1526 testStrEnd(hTest);
1527 testStrStr(hTest);
1528 testUtf8Latin1(hTest);
1529 testUtf16Latin1(hTest);
1530 testNoTransation(hTest);
1531 testGetPut(hTest);
1532
1533 Benchmarks(hTest);
1534
1535 /*
1536 * Summary
1537 */
1538 return RTTestSummaryAndDestroy(hTest);
1539}
1540
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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