VirtualBox

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

Last change on this file since 46072 was 45262, checked in by vboxsync, 12 years ago

tstUtf8: Another VERR_NO_TRANSLATE/VWRN_NO_TRANSLATE hack.

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