1 | /* $Id: tstUtf8.cpp 30320 2010-06-21 08:35:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - UTF-8 and UTF-16 string conversions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | #include <iprt/uni.h>
|
---|
32 | #include <iprt/initterm.h>
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/alloc.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/test.h>
|
---|
40 | #include <iprt/cpp/ministring.h>
|
---|
41 |
|
---|
42 | #include <stdlib.h> /** @todo use our random. */
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Generate a random codepoint for simple UTF-16 encoding.
|
---|
48 | */
|
---|
49 | static RTUTF16 GetRandUtf16(void)
|
---|
50 | {
|
---|
51 | RTUTF16 wc;
|
---|
52 | do
|
---|
53 | {
|
---|
54 | wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
|
---|
55 | } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
|
---|
56 | return wc;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | *
|
---|
62 | */
|
---|
63 | static 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 apparantly 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 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
|
---|
83 |
|
---|
84 | /*
|
---|
85 | * Test current CP convertion.
|
---|
86 | */
|
---|
87 | RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
|
---|
88 | pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
|
---|
89 | srand((unsigned)RTTimeNanoTS());
|
---|
90 | for (int i = 0; i < 30; i++)
|
---|
91 | pwszRand[i] = GetRandUtf16();
|
---|
92 | pwszRand[30] = 0;
|
---|
93 |
|
---|
94 | rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
|
---|
95 | if (rc == VINF_SUCCESS)
|
---|
96 | {
|
---|
97 | rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
|
---|
98 | if (rc == VINF_SUCCESS)
|
---|
99 | {
|
---|
100 | rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
|
---|
101 | if (rc == VINF_SUCCESS)
|
---|
102 | RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
|
---|
103 | else
|
---|
104 | RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
|
---|
105 | __LINE__, rc);
|
---|
106 | }
|
---|
107 | else if (rc == VERR_NO_TRANSLATION)
|
---|
108 | RTTestPassed(hTest, "The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n");
|
---|
109 | else
|
---|
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 | srand((unsigned)RTTimeNanoTS());
|
---|
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 | srand((unsigned)RTTimeNanoTS());
|
---|
157 | for (int i = 0; i < 30; i++)
|
---|
158 | pwszRand[i] = GetRandUtf16();
|
---|
159 | pwszRand[30] = 0;
|
---|
160 |
|
---|
161 | char szUtf8Array[120];
|
---|
162 | char *pszUtf8Array = szUtf8Array;
|
---|
163 | rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
|
---|
164 | if (rc == 0)
|
---|
165 | {
|
---|
166 | rc = RTStrToUtf16(pszUtf8Array, &pwsz);
|
---|
167 | if (rc == 0)
|
---|
168 | {
|
---|
169 | int i;
|
---|
170 | for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
|
---|
171 | ;
|
---|
172 | if (pwsz[i] == 0 && i >= 8)
|
---|
173 | RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
|
---|
174 | else
|
---|
175 | {
|
---|
176 | RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
|
---|
177 | RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | else
|
---|
181 | RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
|
---|
182 | }
|
---|
183 | else
|
---|
184 | RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * And again.
|
---|
188 | */
|
---|
189 | RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
|
---|
190 | pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
|
---|
191 | srand((unsigned)RTTimeNanoTS());
|
---|
192 | for (int i = 0; i < 30; i++)
|
---|
193 | pwszRand[i] = GetRandUtf16();
|
---|
194 | pwszRand[30] = 0;
|
---|
195 |
|
---|
196 | RTUTF16 wszBuf[70];
|
---|
197 | PRTUTF16 pwsz2Buf = wszBuf;
|
---|
198 | rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
|
---|
199 | if (rc == 0)
|
---|
200 | {
|
---|
201 | rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
|
---|
202 | if (rc == 0)
|
---|
203 | {
|
---|
204 | int i;
|
---|
205 | for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
|
---|
206 | ;
|
---|
207 | if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
|
---|
208 | RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
|
---|
209 | else
|
---|
210 | {
|
---|
211 | RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
|
---|
212 | RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else
|
---|
216 | RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
|
---|
217 | }
|
---|
218 | else
|
---|
219 | RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
|
---|
220 | __LINE__, rc);
|
---|
221 | pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
|
---|
222 | srand((unsigned)RTTimeNanoTS());
|
---|
223 | for (int i = 0; i < 30; i++)
|
---|
224 | pwszRand[i] = GetRandUtf16();
|
---|
225 | pwszRand[30] = 0;
|
---|
226 |
|
---|
227 | rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
|
---|
228 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
229 | RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
|
---|
230 | else
|
---|
231 | RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
|
---|
232 | __LINE__, rc);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * last time...
|
---|
236 | */
|
---|
237 | RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
|
---|
238 | pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
|
---|
239 | srand((unsigned)RTTimeNanoTS());
|
---|
240 | for (int i = 0; i < 30; i++)
|
---|
241 | pwszRand[i] = GetRandUtf16();
|
---|
242 | pwszRand[30] = 0;
|
---|
243 |
|
---|
244 | rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
|
---|
245 | if (rc == VINF_SUCCESS)
|
---|
246 | {
|
---|
247 | rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
|
---|
248 | if (rc == VERR_BUFFER_OVERFLOW)
|
---|
249 | RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
|
---|
250 | else
|
---|
251 | 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",
|
---|
252 | __LINE__, rc);
|
---|
253 | }
|
---|
254 | else
|
---|
255 | RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
|
---|
256 | __LINE__, rc);
|
---|
257 |
|
---|
258 |
|
---|
259 | RTTestSubDone(hTest);
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
|
---|
264 | static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
|
---|
265 | static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
|
---|
266 |
|
---|
267 | static void whereami(int cBits, size_t off)
|
---|
268 | {
|
---|
269 | if (cBits == 8)
|
---|
270 | {
|
---|
271 | if (off < 0x7f)
|
---|
272 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
|
---|
273 | else if (off < 0xf7f)
|
---|
274 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
|
---|
275 | else if (off < 0x27f7f)
|
---|
276 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
|
---|
277 | else if (off < 0x2df79)
|
---|
278 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
|
---|
279 | else if (off < 0x42df79)
|
---|
280 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
|
---|
281 | else
|
---|
282 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
|
---|
283 | }
|
---|
284 | else if (cBits == 16)
|
---|
285 | {
|
---|
286 | if (off < 0xd7ff*2)
|
---|
287 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
|
---|
288 | else if (off < 0xf7fd*2)
|
---|
289 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
|
---|
290 | else if (off < 0x20f7fd)
|
---|
291 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
|
---|
292 | else
|
---|
293 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
|
---|
294 | }
|
---|
295 | else
|
---|
296 | {
|
---|
297 | if (off < (0xd800 - 1) * sizeof(RTUNICP))
|
---|
298 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
|
---|
299 | else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
|
---|
300 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
|
---|
301 | else
|
---|
302 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
|
---|
307 | {
|
---|
308 | const uint8_t *pb1 = (const uint8_t *)pv1;
|
---|
309 | const uint8_t *pb2 = (const uint8_t *)pv2;
|
---|
310 | for (size_t off = 0; off < cb; off++)
|
---|
311 | {
|
---|
312 | if (pb1[off] != pb2[off])
|
---|
313 | {
|
---|
314 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
|
---|
315 | whereami(cBits, off);
|
---|
316 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
|
---|
317 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
|
---|
318 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
|
---|
319 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
|
---|
320 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
|
---|
321 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
|
---|
322 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
|
---|
323 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
|
---|
324 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
|
---|
325 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
|
---|
326 | RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
|
---|
327 | return 1;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | return 0;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | void InitStrings()
|
---|
335 | {
|
---|
336 | /*
|
---|
337 | * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
|
---|
338 | */
|
---|
339 | /* the simple code point array first */
|
---|
340 | unsigned i = 0;
|
---|
341 | RTUNICP uc = 1;
|
---|
342 | while (uc < 0xd800)
|
---|
343 | g_uszAll[i++] = uc++;
|
---|
344 | uc = 0xe000;
|
---|
345 | while (uc < 0xfffe)
|
---|
346 | g_uszAll[i++] = uc++;
|
---|
347 | uc = 0x10000;
|
---|
348 | while (uc < 0x110000)
|
---|
349 | g_uszAll[i++] = uc++;
|
---|
350 | g_uszAll[i++] = 0;
|
---|
351 | Assert(RT_ELEMENTS(g_uszAll) == i);
|
---|
352 |
|
---|
353 | /* the utf-16 one */
|
---|
354 | i = 0;
|
---|
355 | uc = 1;
|
---|
356 | //RTPrintf("tstUtf8: %#x=%#x", i, uc);
|
---|
357 | while (uc < 0xd800)
|
---|
358 | g_wszAll[i++] = uc++;
|
---|
359 | uc = 0xe000;
|
---|
360 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
361 | while (uc < 0xfffe)
|
---|
362 | g_wszAll[i++] = uc++;
|
---|
363 | uc = 0x10000;
|
---|
364 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
365 | while (uc < 0x110000)
|
---|
366 | {
|
---|
367 | g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
|
---|
368 | g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
|
---|
369 | uc++;
|
---|
370 | }
|
---|
371 | //RTPrintf(" %#x=%#x\n", i, uc);
|
---|
372 | g_wszAll[i++] = '\0';
|
---|
373 | Assert(RT_ELEMENTS(g_wszAll) == i);
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * The utf-8 one
|
---|
377 | */
|
---|
378 | i = 0;
|
---|
379 | uc = 1;
|
---|
380 | //RTPrintf("tstUtf8: %#x=%#x", i, uc);
|
---|
381 | while (uc < 0x80)
|
---|
382 | g_szAll[i++] = uc++;
|
---|
383 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
384 | while (uc < 0x800)
|
---|
385 | {
|
---|
386 | g_szAll[i++] = 0xc0 | (uc >> 6);
|
---|
387 | g_szAll[i++] = 0x80 | (uc & 0x3f);
|
---|
388 | Assert(!((uc >> 6) & ~0x1f));
|
---|
389 | uc++;
|
---|
390 | }
|
---|
391 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
392 | while (uc < 0xd800)
|
---|
393 | {
|
---|
394 | g_szAll[i++] = 0xe0 | (uc >> 12);
|
---|
395 | g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
|
---|
396 | g_szAll[i++] = 0x80 | (uc & 0x3f);
|
---|
397 | Assert(!((uc >> 12) & ~0xf));
|
---|
398 | uc++;
|
---|
399 | }
|
---|
400 | uc = 0xe000;
|
---|
401 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
402 | while (uc < 0xfffe)
|
---|
403 | {
|
---|
404 | g_szAll[i++] = 0xe0 | (uc >> 12);
|
---|
405 | g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
|
---|
406 | g_szAll[i++] = 0x80 | (uc & 0x3f);
|
---|
407 | Assert(!((uc >> 12) & ~0xf));
|
---|
408 | uc++;
|
---|
409 | }
|
---|
410 | uc = 0x10000;
|
---|
411 | //RTPrintf(" %#x=%#x", i, uc);
|
---|
412 | while (uc < 0x110000)
|
---|
413 | {
|
---|
414 | g_szAll[i++] = 0xf0 | (uc >> 18);
|
---|
415 | g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
|
---|
416 | g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
|
---|
417 | g_szAll[i++] = 0x80 | (uc & 0x3f);
|
---|
418 | Assert(!((uc >> 18) & ~0x7));
|
---|
419 | uc++;
|
---|
420 | }
|
---|
421 | //RTPrintf(" %#x=%#x\n", i, uc);
|
---|
422 | g_szAll[i++] = '\0';
|
---|
423 | Assert(RT_ELEMENTS(g_szAll) == i);
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | void test2(RTTEST hTest)
|
---|
428 | {
|
---|
429 | /*
|
---|
430 | * Convert to UTF-8 and back.
|
---|
431 | */
|
---|
432 | RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16");
|
---|
433 | char *pszUtf8;
|
---|
434 | int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
|
---|
435 | if (rc == VINF_SUCCESS)
|
---|
436 | {
|
---|
437 | if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
|
---|
438 | RTTestFailed(hTest, "UTF-16 -> UTF-8 mismatch!");
|
---|
439 |
|
---|
440 | PRTUTF16 pwszUtf16;
|
---|
441 | rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
|
---|
442 | if (rc == VINF_SUCCESS)
|
---|
443 | {
|
---|
444 | if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
|
---|
445 | RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
|
---|
446 | RTUtf16Free(pwszUtf16);
|
---|
447 | }
|
---|
448 | else
|
---|
449 | RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
|
---|
450 | RTStrFree(pszUtf8);
|
---|
451 | }
|
---|
452 | else
|
---|
453 | RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
|
---|
454 |
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Convert to UTF-16 and back. (just in case the above test fails)
|
---|
458 | */
|
---|
459 | RTTestSub(hTest, "UTF-8 -> UTF-16 -> UTF-8");
|
---|
460 | PRTUTF16 pwszUtf16;
|
---|
461 | rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
|
---|
462 | if (rc == VINF_SUCCESS)
|
---|
463 | {
|
---|
464 | if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
|
---|
465 | RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
|
---|
466 |
|
---|
467 | rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
|
---|
468 | if (rc == VINF_SUCCESS)
|
---|
469 | {
|
---|
470 | if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
|
---|
471 | RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
|
---|
472 | RTStrFree(pszUtf8);
|
---|
473 | }
|
---|
474 | else
|
---|
475 | RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
|
---|
476 | RTUtf16Free(pwszUtf16);
|
---|
477 | }
|
---|
478 | else
|
---|
479 | RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * Convert UTF-8 to CPs.
|
---|
483 | */
|
---|
484 | RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8");
|
---|
485 | PRTUNICP paCps;
|
---|
486 | rc = RTStrToUni(g_szAll, &paCps);
|
---|
487 | if (rc == VINF_SUCCESS)
|
---|
488 | {
|
---|
489 | if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
|
---|
490 | RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
|
---|
491 |
|
---|
492 | size_t cCps;
|
---|
493 | rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
|
---|
494 | if (rc == VINF_SUCCESS)
|
---|
495 | {
|
---|
496 | if (cCps != RT_ELEMENTS(g_uszAll) - 1)
|
---|
497 | RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
|
---|
498 | }
|
---|
499 | else
|
---|
500 | RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
|
---|
501 |
|
---|
502 | /** @todo RTCpsToUtf8 or something. */
|
---|
503 | }
|
---|
504 | else
|
---|
505 | RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
|
---|
506 |
|
---|
507 | /*
|
---|
508 | * Check the various string lengths.
|
---|
509 | */
|
---|
510 | RTTestSub(hTest, "Lengths");
|
---|
511 | size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
|
---|
512 | size_t cuc2 = RTUtf16Len(g_wszAll);
|
---|
513 | if (cuc1 != cuc2)
|
---|
514 | RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
|
---|
515 | //size_t cuc3 = RTUniLen(g_uszAll);
|
---|
516 |
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * Enumerate the strings.
|
---|
520 | */
|
---|
521 | RTTestSub(hTest, "Code Point Getters and Putters");
|
---|
522 | char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
|
---|
523 | AssertRelease(pszPut1Base);
|
---|
524 | char *pszPut1 = pszPut1Base;
|
---|
525 | PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
|
---|
526 | AssertRelease(pwszPut2Base);
|
---|
527 | PRTUTF16 pwszPut2 = pwszPut2Base;
|
---|
528 | const char *psz1 = g_szAll;
|
---|
529 | const char *psz2 = g_szAll;
|
---|
530 | PCRTUTF16 pwsz3 = g_wszAll;
|
---|
531 | PCRTUTF16 pwsz4 = g_wszAll;
|
---|
532 | for (;;)
|
---|
533 | {
|
---|
534 | /*
|
---|
535 | * getters
|
---|
536 | */
|
---|
537 | RTUNICP uc1;
|
---|
538 | rc = RTStrGetCpEx(&psz1, &uc1);
|
---|
539 | if (RT_FAILURE(rc))
|
---|
540 | {
|
---|
541 | RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
|
---|
542 | whereami(8, psz2 - &g_szAll[0]);
|
---|
543 | break;
|
---|
544 | }
|
---|
545 | char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
|
---|
546 | if (pszPrev1 != psz2)
|
---|
547 | {
|
---|
548 | RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
|
---|
549 | whereami(8, psz2 - &g_szAll[0]);
|
---|
550 | break;
|
---|
551 | }
|
---|
552 | RTUNICP uc2 = RTStrGetCp(psz2);
|
---|
553 | if (uc2 != uc1)
|
---|
554 | {
|
---|
555 | RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
|
---|
556 | whereami(8, psz2 - &g_szAll[0]);
|
---|
557 | break;
|
---|
558 | }
|
---|
559 | psz2 = RTStrNextCp(psz2);
|
---|
560 | if (psz2 != psz1)
|
---|
561 | {
|
---|
562 | RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
|
---|
563 | whereami(8, psz2 - &g_szAll[0]);
|
---|
564 | break;
|
---|
565 | }
|
---|
566 |
|
---|
567 | RTUNICP uc3;
|
---|
568 | rc = RTUtf16GetCpEx(&pwsz3, &uc3);
|
---|
569 | if (RT_FAILURE(rc))
|
---|
570 | {
|
---|
571 | RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
|
---|
572 | whereami(16, pwsz4 - &g_wszAll[0]);
|
---|
573 | break;
|
---|
574 | }
|
---|
575 | if (uc3 != uc2)
|
---|
576 | {
|
---|
577 | RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
|
---|
578 | whereami(16, pwsz4 - &g_wszAll[0]);
|
---|
579 | break;
|
---|
580 | }
|
---|
581 | RTUNICP uc4 = RTUtf16GetCp(pwsz4);
|
---|
582 | if (uc3 != uc4)
|
---|
583 | {
|
---|
584 | RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
|
---|
585 | whereami(16, pwsz4 - &g_wszAll[0]);
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | pwsz4 = RTUtf16NextCp(pwsz4);
|
---|
589 | if (pwsz4 != pwsz3)
|
---|
590 | {
|
---|
591 | RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
|
---|
592 | whereami(8, pwsz4 - &g_wszAll[0]);
|
---|
593 | break;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | /*
|
---|
598 | * putters
|
---|
599 | */
|
---|
600 | pszPut1 = RTStrPutCp(pszPut1, uc1);
|
---|
601 | if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
|
---|
602 | {
|
---|
603 | RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
|
---|
604 | pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
|
---|
605 | whereami(8, psz2 - &g_szAll[0]);
|
---|
606 | break;
|
---|
607 | }
|
---|
608 |
|
---|
609 | pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
|
---|
610 | if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
|
---|
611 | {
|
---|
612 | RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
|
---|
613 | pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
|
---|
614 | whereami(8, pwsz4 - &g_wszAll[0]);
|
---|
615 | break;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | /* the end? */
|
---|
620 | if (!uc1)
|
---|
621 | break;
|
---|
622 | }
|
---|
623 |
|
---|
624 | /* check output if we seems to have made it thru it all. */
|
---|
625 | if (psz2 == &g_szAll[sizeof(g_szAll)])
|
---|
626 | {
|
---|
627 | if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
|
---|
628 | RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
|
---|
629 | if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
|
---|
630 | RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
|
---|
631 | }
|
---|
632 |
|
---|
633 | RTMemFree(pszPut1Base);
|
---|
634 | RTMemFree(pwszPut2Base);
|
---|
635 |
|
---|
636 | RTTestSubDone(hTest);
|
---|
637 | }
|
---|
638 |
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Check case insensitivity.
|
---|
642 | */
|
---|
643 | void test3(RTTEST hTest)
|
---|
644 | {
|
---|
645 | RTTestSub(hTest, "Case Sensitivitity");
|
---|
646 |
|
---|
647 | if ( RTUniCpToLower('a') != 'a'
|
---|
648 | || RTUniCpToLower('A') != 'a'
|
---|
649 | || RTUniCpToLower('b') != 'b'
|
---|
650 | || RTUniCpToLower('B') != 'b'
|
---|
651 | || RTUniCpToLower('Z') != 'z'
|
---|
652 | || RTUniCpToLower('z') != 'z'
|
---|
653 | || RTUniCpToUpper('c') != 'C'
|
---|
654 | || RTUniCpToUpper('C') != 'C'
|
---|
655 | || RTUniCpToUpper('z') != 'Z'
|
---|
656 | || RTUniCpToUpper('Z') != 'Z')
|
---|
657 | RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
|
---|
658 |
|
---|
659 | if (RTUtf16ICmp(g_wszAll, g_wszAll))
|
---|
660 | RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
|
---|
661 |
|
---|
662 | if (RTUtf16Cmp(g_wszAll, g_wszAll))
|
---|
663 | RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
|
---|
664 |
|
---|
665 | 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 };
|
---|
666 | 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 };
|
---|
667 | if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
|
---|
668 | || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
|
---|
669 | || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
|
---|
670 | || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
|
---|
671 | )
|
---|
672 | RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
|
---|
673 |
|
---|
674 | if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
|
---|
675 | || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
|
---|
676 | || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
|
---|
677 | || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
|
---|
678 | )
|
---|
679 | RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
|
---|
680 |
|
---|
681 | RTTestSubDone(hTest);
|
---|
682 | }
|
---|
683 |
|
---|
684 |
|
---|
685 | /**
|
---|
686 | * Test the RTStr*Cmp functions.
|
---|
687 | */
|
---|
688 | void TstRTStrXCmp(RTTEST hTest)
|
---|
689 | {
|
---|
690 | #define CHECK_DIFF(expr, op) \
|
---|
691 | do \
|
---|
692 | { \
|
---|
693 | int iDiff = expr; \
|
---|
694 | if (!(iDiff op 0)) \
|
---|
695 | RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
|
---|
696 | } while (0)
|
---|
697 |
|
---|
698 | /** @todo test the non-ascii bits. */
|
---|
699 |
|
---|
700 | RTTestSub(hTest, "RTStrCmp");
|
---|
701 | CHECK_DIFF(RTStrCmp(NULL, NULL), == );
|
---|
702 | CHECK_DIFF(RTStrCmp(NULL, ""), < );
|
---|
703 | CHECK_DIFF(RTStrCmp("", NULL), > );
|
---|
704 | CHECK_DIFF(RTStrCmp("", ""), == );
|
---|
705 | CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
|
---|
706 | CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
|
---|
707 | CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
|
---|
708 | CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
|
---|
709 | CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
|
---|
710 | CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
|
---|
711 | CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
|
---|
712 |
|
---|
713 |
|
---|
714 | RTTestSub(hTest, "RTStrNCmp");
|
---|
715 | CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
|
---|
716 | CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
|
---|
717 | CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
|
---|
718 | CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
|
---|
719 | CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
|
---|
720 | CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
|
---|
721 | CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
|
---|
722 | CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
|
---|
723 | CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
|
---|
724 | CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
|
---|
725 | CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
|
---|
726 |
|
---|
727 | CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
|
---|
728 | CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
|
---|
729 | CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
|
---|
730 |
|
---|
731 |
|
---|
732 | RTTestSub(hTest, "RTStrICmp");
|
---|
733 | CHECK_DIFF(RTStrICmp(NULL, NULL), == );
|
---|
734 | CHECK_DIFF(RTStrICmp(NULL, ""), < );
|
---|
735 | CHECK_DIFF(RTStrICmp("", NULL), > );
|
---|
736 | CHECK_DIFF(RTStrICmp("", ""), == );
|
---|
737 | CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
|
---|
738 | CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
|
---|
739 | CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
|
---|
740 | CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
|
---|
741 | CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
|
---|
742 |
|
---|
743 | CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
|
---|
744 | CHECK_DIFF(RTStrICmp("abcdef", "abcdeF"), ==);
|
---|
745 | CHECK_DIFF(RTStrICmp("ABCDEF", "abcdef"), ==);
|
---|
746 | CHECK_DIFF(RTStrICmp("abcdef", "ABCDEF"), ==);
|
---|
747 | CHECK_DIFF(RTStrICmp("AbCdEf", "aBcDeF"), ==);
|
---|
748 | CHECK_DIFF(RTStrICmp("AbCdEg", "aBcDeF"), > );
|
---|
749 | CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
|
---|
750 |
|
---|
751 |
|
---|
752 |
|
---|
753 | RTTestSub(hTest, "RTStrNICmp");
|
---|
754 | CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
|
---|
755 | CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
|
---|
756 | CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
|
---|
757 | CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
|
---|
758 | CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
|
---|
759 | CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
|
---|
760 | CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
|
---|
761 | CHECK_DIFF(RTStrNICmp("", "", 0), == );
|
---|
762 | CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
|
---|
763 | CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
|
---|
764 | CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
|
---|
765 | CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
|
---|
766 | CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
|
---|
767 |
|
---|
768 | CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
|
---|
769 | CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
|
---|
770 | CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
|
---|
771 | CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
|
---|
772 | CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
|
---|
773 | CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
|
---|
774 | CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
|
---|
775 |
|
---|
776 | CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
|
---|
777 | CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
|
---|
778 | CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
|
---|
779 | CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
|
---|
780 | CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
|
---|
781 | CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
|
---|
782 | CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
|
---|
783 | CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
|
---|
784 | /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
|
---|
785 | // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
|
---|
786 |
|
---|
787 | RTTestSubDone(hTest);
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Benchmark stuff.
|
---|
794 | */
|
---|
795 | void Benchmarks(RTTEST hTest)
|
---|
796 | {
|
---|
797 | static union
|
---|
798 | {
|
---|
799 | RTUTF16 wszBuf[sizeof(g_wszAll)];
|
---|
800 | char szBuf[sizeof(g_szAll)];
|
---|
801 | } s_Buf;
|
---|
802 |
|
---|
803 | RTTestSub(hTest, "Benchmarks");
|
---|
804 | /** @todo add RTTest* methods for reporting benchmark results. */
|
---|
805 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
|
---|
806 | PRTUTF16 pwsz = &s_Buf.wszBuf[0];
|
---|
807 | int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
|
---|
808 | if (RT_SUCCESS(rc))
|
---|
809 | {
|
---|
810 | int i;
|
---|
811 | uint64_t u64Start = RTTimeNanoTS();
|
---|
812 | for (i = 0; i < 100; i++)
|
---|
813 | {
|
---|
814 | rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
|
---|
815 | if (RT_FAILURE(rc))
|
---|
816 | {
|
---|
817 | RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
|
---|
818 | break;
|
---|
819 | }
|
---|
820 | }
|
---|
821 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
822 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
|
---|
823 | }
|
---|
824 |
|
---|
825 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
|
---|
826 | char *psz = &s_Buf.szBuf[0];
|
---|
827 | rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
|
---|
828 | if (RT_SUCCESS(rc))
|
---|
829 | {
|
---|
830 | int i;
|
---|
831 | uint64_t u64Start = RTTimeNanoTS();
|
---|
832 | for (i = 0; i < 100; i++)
|
---|
833 | {
|
---|
834 | rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
|
---|
835 | if (RT_FAILURE(rc))
|
---|
836 | {
|
---|
837 | RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
|
---|
838 | break;
|
---|
839 | }
|
---|
840 | }
|
---|
841 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
842 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %'RI64 ns\n", i, u64Elapsed);
|
---|
843 | }
|
---|
844 |
|
---|
845 | RTTestSubDone(hTest);
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Tests RTStrEnd
|
---|
851 | */
|
---|
852 | static void testStrEnd(RTTEST hTest)
|
---|
853 | {
|
---|
854 | RTTestSub(hTest, "RTStrEnd");
|
---|
855 |
|
---|
856 | static char const s_szEmpty[1] = "";
|
---|
857 | RTTESTI_CHECK(RTStrEnd(s_szEmpty, 0) == NULL);
|
---|
858 | RTTESTI_CHECK(RTStrEnd(s_szEmpty, 1) == &s_szEmpty[0]);
|
---|
859 | for (size_t i = 0; i < _1M; i++)
|
---|
860 | RTTESTI_CHECK(RTStrEnd(s_szEmpty, ~i) == &s_szEmpty[0]);
|
---|
861 |
|
---|
862 | }
|
---|
863 |
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Tests RTStrStr and RTStrIStr.
|
---|
867 | */
|
---|
868 | static void testStrStr(RTTEST hTest)
|
---|
869 | {
|
---|
870 | #define CHECK_NULL(expr) \
|
---|
871 | do { \
|
---|
872 | const char *pszRet = expr; \
|
---|
873 | if (pszRet != NULL) \
|
---|
874 | RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
|
---|
875 | } while (0)
|
---|
876 |
|
---|
877 | #define CHECK(expr, expect) \
|
---|
878 | do { \
|
---|
879 | const char *pszRet = expr; \
|
---|
880 | if ( (pszRet != NULL && (expect) == NULL) \
|
---|
881 | || (pszRet == NULL && (expect) != NULL) \
|
---|
882 | || strcmp(pszRet, (expect)) \
|
---|
883 | ) \
|
---|
884 | RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
|
---|
885 | } while (0)
|
---|
886 |
|
---|
887 |
|
---|
888 | RTTestSub(hTest, "RTStrStr");
|
---|
889 | CHECK(RTStrStr("abcdef", ""), "abcdef");
|
---|
890 | CHECK_NULL(RTStrStr("abcdef", NULL));
|
---|
891 | CHECK_NULL(RTStrStr(NULL, ""));
|
---|
892 | CHECK_NULL(RTStrStr(NULL, NULL));
|
---|
893 | CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
|
---|
894 | CHECK(RTStrStr("abcdef", "b"), "bcdef");
|
---|
895 | CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
|
---|
896 | CHECK(RTStrStr("abcdef", "cdef"), "cdef");
|
---|
897 | CHECK(RTStrStr("abcdef", "cde"), "cdef");
|
---|
898 | CHECK(RTStrStr("abcdef", "cd"), "cdef");
|
---|
899 | CHECK(RTStrStr("abcdef", "c"), "cdef");
|
---|
900 | CHECK(RTStrStr("abcdef", "f"), "f");
|
---|
901 | CHECK(RTStrStr("abcdef", "ef"), "ef");
|
---|
902 | CHECK(RTStrStr("abcdef", "e"), "ef");
|
---|
903 | CHECK_NULL(RTStrStr("abcdef", "z"));
|
---|
904 | CHECK_NULL(RTStrStr("abcdef", "A"));
|
---|
905 | CHECK_NULL(RTStrStr("abcdef", "F"));
|
---|
906 |
|
---|
907 | RTTestSub(hTest, "RTStrIStr");
|
---|
908 | CHECK(RTStrIStr("abcdef", ""), "abcdef");
|
---|
909 | CHECK_NULL(RTStrIStr("abcdef", NULL));
|
---|
910 | CHECK_NULL(RTStrIStr(NULL, ""));
|
---|
911 | CHECK_NULL(RTStrIStr(NULL, NULL));
|
---|
912 | CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
|
---|
913 | CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
|
---|
914 | CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
|
---|
915 | CHECK(RTStrIStr("abcdef", "b"), "bcdef");
|
---|
916 | CHECK(RTStrIStr("abcdef", "B"), "bcdef");
|
---|
917 | CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef");
|
---|
918 | CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef");
|
---|
919 | CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef");
|
---|
920 | CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef");
|
---|
921 | CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef");
|
---|
922 | CHECK(RTStrIStr("abcdef", "cdef"), "cdef");
|
---|
923 | CHECK(RTStrIStr("abcdef", "cde"), "cdef");
|
---|
924 | CHECK(RTStrIStr("abcdef", "cd"), "cdef");
|
---|
925 | CHECK(RTStrIStr("abcdef", "c"), "cdef");
|
---|
926 | CHECK(RTStrIStr("abcdef", "f"), "f");
|
---|
927 | CHECK(RTStrIStr("abcdeF", "F"), "F");
|
---|
928 | CHECK(RTStrIStr("abcdef", "F"), "f");
|
---|
929 | CHECK(RTStrIStr("abcdef", "ef"), "ef");
|
---|
930 | CHECK(RTStrIStr("EeEef", "e"), "EeEef");
|
---|
931 | CHECK(RTStrIStr("EeEef", "E"), "EeEef");
|
---|
932 | CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
|
---|
933 | CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
|
---|
934 | CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
|
---|
935 | CHECK_NULL(RTStrIStr("EeEef", "z"));
|
---|
936 |
|
---|
937 | #undef CHECK
|
---|
938 | #undef CHECK_NULL
|
---|
939 | RTTestSubDone(hTest);
|
---|
940 | }
|
---|
941 |
|
---|
942 |
|
---|
943 | void testMinistring(RTTEST hTest)
|
---|
944 | {
|
---|
945 | RTTestSub(hTest, "class iprt::MiniString");
|
---|
946 |
|
---|
947 | #define CHECK(expr) \
|
---|
948 | do { \
|
---|
949 | if (!(expr)) \
|
---|
950 | RTTestFailed(hTest, "%d: FAILED %s", __LINE__, #expr); \
|
---|
951 | } while (0)
|
---|
952 |
|
---|
953 | #define CHECK_DUMP(expr, value) \
|
---|
954 | do { \
|
---|
955 | if (!(expr)) \
|
---|
956 | RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
|
---|
957 | } while (0)
|
---|
958 |
|
---|
959 | #define CHECK_DUMP_I(expr) \
|
---|
960 | do { \
|
---|
961 | if (!(expr)) \
|
---|
962 | RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
|
---|
963 | } while (0)
|
---|
964 |
|
---|
965 | iprt::MiniString empty;
|
---|
966 | CHECK(empty.length() == 0);
|
---|
967 | CHECK(empty.capacity() == 0);
|
---|
968 |
|
---|
969 | iprt::MiniString sixbytes("12345");
|
---|
970 | CHECK(sixbytes.length() == 5);
|
---|
971 | CHECK(sixbytes.capacity() == 6);
|
---|
972 |
|
---|
973 | sixbytes.append(iprt::MiniString("678"));
|
---|
974 | CHECK(sixbytes.length() == 8);
|
---|
975 | CHECK(sixbytes.capacity() == 9);
|
---|
976 |
|
---|
977 | sixbytes.append("9a");
|
---|
978 | CHECK(sixbytes.length() == 10);
|
---|
979 | CHECK(sixbytes.capacity() == 11);
|
---|
980 |
|
---|
981 | char *psz = sixbytes.mutableRaw();
|
---|
982 | // 123456789a
|
---|
983 | // ^
|
---|
984 | // 0123456
|
---|
985 | psz[6] = '\0';
|
---|
986 | sixbytes.jolt();
|
---|
987 | CHECK(sixbytes.length() == 6);
|
---|
988 | CHECK(sixbytes.capacity() == 7);
|
---|
989 |
|
---|
990 | iprt::MiniString morebytes("tobereplaced");
|
---|
991 | morebytes = "newstring ";
|
---|
992 | morebytes.append(sixbytes);
|
---|
993 |
|
---|
994 | CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
|
---|
995 |
|
---|
996 | iprt::MiniString third(morebytes);
|
---|
997 | third.reserve(100 * 1024); // 100 KB
|
---|
998 | CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
|
---|
999 | CHECK(third.capacity() == 100 * 1024);
|
---|
1000 | CHECK(third.length() == morebytes.length()); // must not have changed
|
---|
1001 |
|
---|
1002 | iprt::MiniString copy1(morebytes);
|
---|
1003 | iprt::MiniString copy2 = morebytes;
|
---|
1004 | CHECK(copy1 == copy2);
|
---|
1005 |
|
---|
1006 | copy1 = NULL;
|
---|
1007 | CHECK(copy1.length() == 0);
|
---|
1008 |
|
---|
1009 | copy1 = "";
|
---|
1010 | CHECK(copy1.length() == 0);
|
---|
1011 |
|
---|
1012 | CHECK(iprt::MiniString("abc") < iprt::MiniString("def"));
|
---|
1013 | CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
|
---|
1014 | CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
|
---|
1015 | CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
|
---|
1016 |
|
---|
1017 | copy2.setNull();
|
---|
1018 | for (int i = 0; i < 100; ++i)
|
---|
1019 | {
|
---|
1020 | copy2.reserve(50); // should be ignored after 50 loops
|
---|
1021 | copy2.append("1");
|
---|
1022 | }
|
---|
1023 | CHECK(copy2.length() == 100);
|
---|
1024 |
|
---|
1025 | copy2.setNull();
|
---|
1026 | for (int i = 0; i < 100; ++i)
|
---|
1027 | {
|
---|
1028 | copy2.reserve(50); // should be ignored after 50 loops
|
---|
1029 | copy2.append('1');
|
---|
1030 | }
|
---|
1031 | CHECK(copy2.length() == 100);
|
---|
1032 |
|
---|
1033 | #undef CHECK
|
---|
1034 | #undef CHECK_DUMP
|
---|
1035 | #undef CHECK_DUMP_I
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | void testLatin1(RTTEST hTest)
|
---|
1040 | {
|
---|
1041 | RTTestSub(hTest, "Latin1 conversion functions");
|
---|
1042 |
|
---|
1043 | /* Test Utf16 -> Latin1 */
|
---|
1044 | size_t cch_szAll = 0;
|
---|
1045 | size_t cbShort = RTUtf16CalcLatin1Len(g_wszAll);
|
---|
1046 | RTTEST_CHECK(hTest, cbShort == 0);
|
---|
1047 | int rc = RTUtf16CalcLatin1LenEx(g_wszAll, 255, &cch_szAll);
|
---|
1048 | RTTEST_CHECK(hTest, (cch_szAll == 255));
|
---|
1049 | rc = RTUtf16CalcLatin1LenEx(g_wszAll, RTSTR_MAX, &cch_szAll);
|
---|
1050 | RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
|
---|
1051 | char *psz = NULL;
|
---|
1052 | RTUTF16 wszShort[256] = { 0 };
|
---|
1053 | for (unsigned i = 0; i < 255; ++i)
|
---|
1054 | wszShort[i] = i + 1;
|
---|
1055 | cbShort = RTUtf16CalcLatin1Len(wszShort);
|
---|
1056 | RTTEST_CHECK(hTest, cbShort == 255);
|
---|
1057 | rc = RTUtf16ToLatin1(wszShort, &psz);
|
---|
1058 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1059 | if (RT_SUCCESS(rc))
|
---|
1060 | {
|
---|
1061 | RTTEST_CHECK(hTest, (strlen(psz) == 255));
|
---|
1062 | for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
|
---|
1063 | if (psz[i] != (char) j)
|
---|
1064 | {
|
---|
1065 | RTTestFailed(hTest, "conversion of g_wszAll to Latin1 failed at position %u\n", i);
|
---|
1066 | break;
|
---|
1067 | }
|
---|
1068 | }
|
---|
1069 | RTStrFree(psz);
|
---|
1070 | rc = RTUtf16ToLatin1(g_wszAll, &psz);
|
---|
1071 | RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
|
---|
1072 | char sz[512];
|
---|
1073 | char *psz2 = &sz[0];
|
---|
1074 | size_t cchActual = 0;
|
---|
1075 | rc = RTUtf16ToLatin1Ex(g_wszAll, sizeof(sz) - 1, &psz2, sizeof(sz),
|
---|
1076 | &cchActual);
|
---|
1077 | RTTEST_CHECK_RC(hTest, rc, VERR_NO_TRANSLATION);
|
---|
1078 | RTTEST_CHECK_MSG(hTest, cchActual == 0,
|
---|
1079 | (hTest, "cchActual=%lu\n", cchActual));
|
---|
1080 | rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz2, sizeof(sz),
|
---|
1081 | &cchActual);
|
---|
1082 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1083 | if (RT_SUCCESS(rc))
|
---|
1084 | {
|
---|
1085 | RTTEST_CHECK(hTest, (cchActual == 255));
|
---|
1086 | RTTEST_CHECK(hTest, (cchActual == strlen(sz)));
|
---|
1087 | for (unsigned i = 0, j = 1; psz2[i] != '\0'; ++i, ++j)
|
---|
1088 | if (psz2[i] != (char) j)
|
---|
1089 | {
|
---|
1090 | RTTestFailed(hTest, "second conversion of g_wszAll to Latin1 failed at position %u\n", i);
|
---|
1091 | break;
|
---|
1092 | }
|
---|
1093 | }
|
---|
1094 | rc = RTUtf16ToLatin1Ex(g_wszAll, 128, &psz2, 128, &cchActual);
|
---|
1095 | RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
|
---|
1096 | RTTEST_CHECK_MSG(hTest, cchActual == 128,
|
---|
1097 | (hTest, "cchActual=%lu\n", cchActual));
|
---|
1098 | rc = RTUtf16ToLatin1Ex(g_wszAll, 255, &psz, 0, &cchActual);
|
---|
1099 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1100 | if (RT_SUCCESS(rc))
|
---|
1101 | {
|
---|
1102 | RTTEST_CHECK(hTest, (cchActual == 255));
|
---|
1103 | RTTEST_CHECK(hTest, (cchActual == strlen(psz)));
|
---|
1104 | for (unsigned i = 0, j = 1; psz[i] != '\0'; ++i, ++j)
|
---|
1105 | if ( ((j < 0x100) && (psz[i] != (char) j))
|
---|
1106 | || ((j > 0xff) && psz[i] != '?'))
|
---|
1107 | {
|
---|
1108 | RTTestFailed(hTest, "third conversion of g_wszAll to Latin1 failed at position %u\n", i);
|
---|
1109 | break;
|
---|
1110 | }
|
---|
1111 | }
|
---|
1112 | const char *pszBad = "H\0e\0l\0l\0o\0\0\xDC\0\xD8\0";
|
---|
1113 | rc = RTUtf16ToLatin1Ex((RTUTF16 *) pszBad, RTSTR_MAX, &psz2, sizeof(sz),
|
---|
1114 | &cchActual);
|
---|
1115 | RTTEST_CHECK_RC(hTest, rc, VERR_INVALID_UTF16_ENCODING);
|
---|
1116 | RTStrFree(psz);
|
---|
1117 |
|
---|
1118 | /* Test Latin1 -> Utf16 */
|
---|
1119 | const char *pszLat1 = "\x01\x20\x40\x80\x81";
|
---|
1120 | RTTEST_CHECK(hTest, RTLatin1CalcUtf16Len(pszLat1) == 5);
|
---|
1121 | rc = RTLatin1CalcUtf16LenEx(pszLat1, 3, &cchActual);
|
---|
1122 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1123 | if (RT_SUCCESS(rc))
|
---|
1124 | RTTEST_CHECK(hTest, cchActual == 3);
|
---|
1125 | rc = RTLatin1CalcUtf16LenEx(pszLat1, RTSTR_MAX, &cchActual);
|
---|
1126 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1127 | if (RT_SUCCESS(rc))
|
---|
1128 | RTTEST_CHECK(hTest, cchActual == 5);
|
---|
1129 | RTUTF16 *pwc = NULL;
|
---|
1130 | RTUTF16 wc[6];
|
---|
1131 | RTUTF16 *pwc2 = &wc[0];
|
---|
1132 | size_t cwActual = 0;
|
---|
1133 | rc = RTLatin1ToUtf16(pszLat1, &pwc);
|
---|
1134 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1135 | if (RT_SUCCESS(rc))
|
---|
1136 | RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
|
---|
1137 | && (pwc[2] == 0x40) && (pwc[3] == 0x80)
|
---|
1138 | && (pwc[4] == 0x81) && (pwc[5] == '\0'));
|
---|
1139 | RTUtf16Free(pwc);
|
---|
1140 | rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, &cwActual);
|
---|
1141 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1142 | if (RT_SUCCESS(rc))
|
---|
1143 | {
|
---|
1144 | RTTEST_CHECK(hTest, (cwActual == 5));
|
---|
1145 | RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
|
---|
1146 | && (pwc[2] == 0x40) && (pwc[3] == 0x80)
|
---|
1147 | && (pwc[4] == 0x81) && (pwc[5] == '\0'));
|
---|
1148 | }
|
---|
1149 | RTUtf16Free(pwc);
|
---|
1150 | rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc, 0, NULL);
|
---|
1151 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1152 | if (RT_SUCCESS(rc))
|
---|
1153 | RTTEST_CHECK(hTest, (pwc[0] == 1) && (pwc[1] == 0x20)
|
---|
1154 | && (pwc[2] == 0x40) && (pwc[3] == 0x80)
|
---|
1155 | && (pwc[4] == 0x81) && (pwc[5] == '\0'));
|
---|
1156 | rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc),
|
---|
1157 | &cwActual);
|
---|
1158 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1159 | if (RT_SUCCESS(rc))
|
---|
1160 | {
|
---|
1161 | RTTEST_CHECK(hTest, (cwActual == 5));
|
---|
1162 | RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
|
---|
1163 | && (wc[2] == 0x40) && (wc[3] == 0x80)
|
---|
1164 | && (wc[4] == 0x81) && (wc[5] == '\0'));
|
---|
1165 | }
|
---|
1166 | rc = RTLatin1ToUtf16Ex(pszLat1, 3, &pwc2, RT_ELEMENTS(wc),
|
---|
1167 | &cwActual);
|
---|
1168 | RTTEST_CHECK_RC_OK(hTest, rc);
|
---|
1169 | if (RT_SUCCESS(rc))
|
---|
1170 | {
|
---|
1171 | RTTEST_CHECK(hTest, (cwActual == 3));
|
---|
1172 | RTTEST_CHECK(hTest, (wc[0] == 1) && (wc[1] == 0x20)
|
---|
1173 | && (wc[2] == 0x40) && (wc[3] == '\0'));
|
---|
1174 | }
|
---|
1175 | rc = RTLatin1ToUtf16Ex(pszLat1, RTSTR_MAX, &pwc2, RT_ELEMENTS(wc) - 1,
|
---|
1176 | &cwActual);
|
---|
1177 | RTTEST_CHECK_RC(hTest, rc, VERR_BUFFER_OVERFLOW);
|
---|
1178 | /** @todo Either fix the documentation or fix the code - cchActual is
|
---|
1179 | * set to the number of bytes actually encoded. */
|
---|
1180 | RTTEST_CHECK(hTest, (cwActual == 5));
|
---|
1181 | RTTestSubDone(hTest);
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | static void testNoTransation(RTTEST hTest)
|
---|
1186 | {
|
---|
1187 |
|
---|
1188 | /*
|
---|
1189 | * Try trigger a VERR_NO_TRANSLATION error in convert to
|
---|
1190 | * current CP to latin-1.
|
---|
1191 | */
|
---|
1192 | const RTUTF16 s_swzTest1[] = { 0x2358, 0x2242, 0x2357, 0x2359, 0x22f9, 0x2c4e, 0x0030, 0x0060,
|
---|
1193 | 0x0092, 0x00c1, 0x00f2, 0x1f80, 0x0088, 0x2c38, 0x2c30, 0x0000 };
|
---|
1194 | char *pszTest1;
|
---|
1195 | int rc = RTUtf16ToUtf8(s_swzTest1, &pszTest1);
|
---|
1196 | RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
|
---|
1197 |
|
---|
1198 | RTTestSub(hTest, "VERR_NO_TRANSLATION/RTStrUtf8ToCurrentCP");
|
---|
1199 | char *pszOut;
|
---|
1200 | rc = RTStrUtf8ToCurrentCP(&pszOut, pszTest1);
|
---|
1201 | if (RT_SUCCESS(rc))
|
---|
1202 | {
|
---|
1203 | RTTESTI_CHECK(!strcmp(pszOut, pszTest1));
|
---|
1204 | RTTestIPrintf(RTTESTLVL_ALWAYS, "CurrentCP is UTF-8 or similar\n");
|
---|
1205 | RTStrFree(pszOut);
|
---|
1206 | }
|
---|
1207 | else
|
---|
1208 | RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
|
---|
1209 |
|
---|
1210 | RTTestSub(hTest, "VERR_NO_TRANSLATION/RTUtf16ToLatin1");
|
---|
1211 | rc = RTUtf16ToLatin1(s_swzTest1, &pszOut);
|
---|
1212 | RTTESTI_CHECK_RC(rc, VERR_NO_TRANSLATION);
|
---|
1213 | if (RT_SUCCESS(rc))
|
---|
1214 | RTStrFree(pszOut);
|
---|
1215 |
|
---|
1216 | RTStrFree(pszTest1);
|
---|
1217 | RTTestSubDone(hTest);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 |
|
---|
1221 | int main()
|
---|
1222 | {
|
---|
1223 | /*
|
---|
1224 | * Init the runtime, test and say hello.
|
---|
1225 | */
|
---|
1226 | RTTEST hTest;
|
---|
1227 | int rc = RTTestInitAndCreate("tstUtf8", &hTest);
|
---|
1228 | if (rc)
|
---|
1229 | return rc;
|
---|
1230 | RTTestBanner(hTest);
|
---|
1231 |
|
---|
1232 | /*
|
---|
1233 | * Run the test.
|
---|
1234 | */
|
---|
1235 | InitStrings();
|
---|
1236 | test1(hTest);
|
---|
1237 | test2(hTest);
|
---|
1238 | test3(hTest);
|
---|
1239 | TstRTStrXCmp(hTest);
|
---|
1240 | testStrEnd(hTest);
|
---|
1241 | testStrStr(hTest);
|
---|
1242 | testMinistring(hTest);
|
---|
1243 | testLatin1(hTest);
|
---|
1244 | testNoTransation(hTest);
|
---|
1245 |
|
---|
1246 | Benchmarks(hTest);
|
---|
1247 |
|
---|
1248 | /*
|
---|
1249 | * Summary
|
---|
1250 | */
|
---|
1251 | return RTTestSummaryAndDestroy(hTest);
|
---|
1252 | }
|
---|
1253 |
|
---|