VirtualBox

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

Last change on this file since 67808 was 66362, checked in by vboxsync, 8 years ago

IPRT: Adding RTStrICmpAscii (mirrors RTUtf16ICmpAscii).

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

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