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