VirtualBox

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

Last change on this file since 21383 was 21079, checked in by vboxsync, 16 years ago

Main: move libxml2 to IPRT unconditionally (remove VBOX_WITH_LIBXML2_IN_VBOXRT); move xml classes to IPRT; introduce IPRT ministring class as base for both Utf8Str and xml.cpp, with better performance; introduce some Utf8Str helpers to avoid string buffer hacks in Main code; remove std::auto_ptr<> from some headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.0 KB
Line 
1/* $Id: tstUtf8.cpp 21079 2009-06-30 15:59:22Z vboxsync $ */
2/** @file
3 * IPRT Testcase - UTF-8 and UTF-16 string conversions.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/string.h>
35#include <iprt/uni.h>
36#include <iprt/initterm.h>
37#include <iprt/uuid.h>
38#include <iprt/time.h>
39#include <iprt/stream.h>
40#include <iprt/alloc.h>
41#include <iprt/assert.h>
42#include <iprt/err.h>
43#include <iprt/test.h>
44#include <iprt/ministring_cpp.h>
45
46#include <stdlib.h> /** @todo use our random. */
47
48
49
50/**
51 * Generate a random codepoint for simple UTF-16 encoding.
52 */
53static RTUTF16 GetRandUtf16(void)
54{
55 RTUTF16 wc;
56 do
57 {
58 wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
59 } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
60 return wc;
61}
62
63
64/**
65 *
66 */
67static void test1(RTTEST hTest)
68{
69 static const char s_szBadString1[] = "Bad \xe0\x13\x0";
70 static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
71 int rc;
72 char *pszUtf8;
73 char *pszCurrent;
74 PRTUTF16 pwsz;
75 PRTUTF16 pwszRand;
76
77 /*
78 * Invalid UTF-8 to UCS-2 test.
79 */
80 RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16");
81 rc = RTStrToUtf16(s_szBadString1, &pwsz);
82 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
83 (hTest, "Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
84 rc = RTStrToUtf16(s_szBadString2, &pwsz);
85 RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
86 (hTest, "Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
87
88 /*
89 * Test current CP convertion.
90 */
91 RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
92 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
93 srand((unsigned)RTTimeNanoTS());
94 for (int i = 0; i < 30; i++)
95 pwszRand[i] = GetRandUtf16();
96 pwszRand[30] = 0;
97
98 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
99 if (rc == VINF_SUCCESS)
100 {
101 rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
102 if (rc == VINF_SUCCESS)
103 {
104 rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
105 if (rc == VINF_SUCCESS)
106 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
107 else
108 RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
109 __LINE__, rc);
110 }
111 else if (rc == VERR_NO_TRANSLATION)
112 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");
113 else
114 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
115 __LINE__, rc);
116 }
117 else
118 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
119 __LINE__, rc);
120
121 /*
122 * Generate a new random string.
123 */
124 RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16");
125 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
126 srand((unsigned)RTTimeNanoTS());
127 for (int i = 0; i < 30; i++)
128 pwszRand[i] = GetRandUtf16();
129 pwszRand[30] = 0;
130 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
131 if (rc == VINF_SUCCESS)
132 {
133 rc = RTStrToUtf16(pszUtf8, &pwsz);
134 if (rc == VINF_SUCCESS)
135 {
136 int i;
137 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
138 /* nothing */;
139 if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
140 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
141 else
142 {
143 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__);
144 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
145 }
146 }
147 else
148 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
149 __LINE__, rc);
150 }
151 else
152 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
153 __LINE__, rc);
154
155 /*
156 * Generate yet another random string and convert it to a buffer.
157 */
158 RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16");
159 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
160 srand((unsigned)RTTimeNanoTS());
161 for (int i = 0; i < 30; i++)
162 pwszRand[i] = GetRandUtf16();
163 pwszRand[30] = 0;
164
165 char szUtf8Array[120];
166 char *pszUtf8Array = szUtf8Array;
167 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
168 if (rc == 0)
169 {
170 rc = RTStrToUtf16(pszUtf8Array, &pwsz);
171 if (rc == 0)
172 {
173 int i;
174 for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
175 ;
176 if (pwsz[i] == 0 && i >= 8)
177 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
178 else
179 {
180 RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
181 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
182 }
183 }
184 else
185 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
186 }
187 else
188 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
189
190 /*
191 * And again.
192 */
193 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
194 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
195 srand((unsigned)RTTimeNanoTS());
196 for (int i = 0; i < 30; i++)
197 pwszRand[i] = GetRandUtf16();
198 pwszRand[30] = 0;
199
200 RTUTF16 wszBuf[70];
201 PRTUTF16 pwsz2Buf = wszBuf;
202 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
203 if (rc == 0)
204 {
205 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
206 if (rc == 0)
207 {
208 int i;
209 for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
210 ;
211 if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
212 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
213 else
214 {
215 RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
216 RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
217 }
218 }
219 else
220 RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
221 }
222 else
223 RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
224 __LINE__, rc);
225 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
226 srand((unsigned)RTTimeNanoTS());
227 for (int i = 0; i < 30; i++)
228 pwszRand[i] = GetRandUtf16();
229 pwszRand[30] = 0;
230
231 rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
232 if (rc == VERR_BUFFER_OVERFLOW)
233 RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
234 else
235 RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
236 __LINE__, rc);
237
238 /*
239 * last time...
240 */
241 RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
242 pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
243 srand((unsigned)RTTimeNanoTS());
244 for (int i = 0; i < 30; i++)
245 pwszRand[i] = GetRandUtf16();
246 pwszRand[30] = 0;
247
248 rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
249 if (rc == VINF_SUCCESS)
250 {
251 rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
252 if (rc == VERR_BUFFER_OVERFLOW)
253 RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
254 else
255 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",
256 __LINE__, rc);
257 }
258 else
259 RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
260 __LINE__, rc);
261
262
263 RTTestSubDone(hTest);
264}
265
266
267static RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
268static RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
269static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
270
271static void whereami(int cBits, size_t off)
272{
273 if (cBits == 8)
274 {
275 if (off < 0x7f)
276 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
277 else if (off < 0xf7f)
278 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
279 else if (off < 0x27f7f)
280 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
281 else if (off < 0x2df79)
282 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
283 else if (off < 0x42df79)
284 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
285 else
286 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
287 }
288 else if (cBits == 16)
289 {
290 if (off < 0xd7ff*2)
291 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
292 else if (off < 0xf7fd*2)
293 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
294 else if (off < 0x20f7fd)
295 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
296 else
297 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
298 }
299 else
300 {
301 if (off < (0xd800 - 1) * sizeof(RTUNICP))
302 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
303 else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
304 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
305 else
306 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
307 }
308}
309
310int mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
311{
312 const uint8_t *pb1 = (const uint8_t *)pv1;
313 const uint8_t *pb2 = (const uint8_t *)pv2;
314 for (size_t off = 0; off < cb; off++)
315 {
316 if (pb1[off] != pb2[off])
317 {
318 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
319 whereami(cBits, off);
320 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
321 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
322 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
323 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
324 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
325 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
326 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
327 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
328 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
329 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
330 RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
331 return 1;
332 }
333 }
334 return 0;
335}
336
337
338void InitStrings()
339{
340 /*
341 * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
342 */
343 /* the simple code point array first */
344 unsigned i = 0;
345 RTUNICP uc = 1;
346 while (uc < 0xd800)
347 g_uszAll[i++] = uc++;
348 uc = 0xe000;
349 while (uc < 0xfffe)
350 g_uszAll[i++] = uc++;
351 uc = 0x10000;
352 while (uc < 0x110000)
353 g_uszAll[i++] = uc++;
354 g_uszAll[i++] = 0;
355 Assert(RT_ELEMENTS(g_uszAll) == i);
356
357 /* the utf-16 one */
358 i = 0;
359 uc = 1;
360 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
361 while (uc < 0xd800)
362 g_wszAll[i++] = uc++;
363 uc = 0xe000;
364 //RTPrintf(" %#x=%#x", i, uc);
365 while (uc < 0xfffe)
366 g_wszAll[i++] = uc++;
367 uc = 0x10000;
368 //RTPrintf(" %#x=%#x", i, uc);
369 while (uc < 0x110000)
370 {
371 g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
372 g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
373 uc++;
374 }
375 //RTPrintf(" %#x=%#x\n", i, uc);
376 g_wszAll[i++] = '\0';
377 Assert(RT_ELEMENTS(g_wszAll) == i);
378
379 /*
380 * The utf-8 one
381 */
382 i = 0;
383 uc = 1;
384 //RTPrintf("tstUtf8: %#x=%#x", i, uc);
385 while (uc < 0x80)
386 g_szAll[i++] = uc++;
387 //RTPrintf(" %#x=%#x", i, uc);
388 while (uc < 0x800)
389 {
390 g_szAll[i++] = 0xc0 | (uc >> 6);
391 g_szAll[i++] = 0x80 | (uc & 0x3f);
392 Assert(!((uc >> 6) & ~0x1f));
393 uc++;
394 }
395 //RTPrintf(" %#x=%#x", i, uc);
396 while (uc < 0xd800)
397 {
398 g_szAll[i++] = 0xe0 | (uc >> 12);
399 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
400 g_szAll[i++] = 0x80 | (uc & 0x3f);
401 Assert(!((uc >> 12) & ~0xf));
402 uc++;
403 }
404 uc = 0xe000;
405 //RTPrintf(" %#x=%#x", i, uc);
406 while (uc < 0xfffe)
407 {
408 g_szAll[i++] = 0xe0 | (uc >> 12);
409 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
410 g_szAll[i++] = 0x80 | (uc & 0x3f);
411 Assert(!((uc >> 12) & ~0xf));
412 uc++;
413 }
414 uc = 0x10000;
415 //RTPrintf(" %#x=%#x", i, uc);
416 while (uc < 0x110000)
417 {
418 g_szAll[i++] = 0xf0 | (uc >> 18);
419 g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
420 g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
421 g_szAll[i++] = 0x80 | (uc & 0x3f);
422 Assert(!((uc >> 18) & ~0x7));
423 uc++;
424 }
425 //RTPrintf(" %#x=%#x\n", i, uc);
426 g_szAll[i++] = '\0';
427 Assert(RT_ELEMENTS(g_szAll) == i);
428}
429
430
431void test2(RTTEST hTest)
432{
433 /*
434 * Convert to UTF-8 and back.
435 */
436 RTTestSub(hTest, "UTF-16 -> UTF-8 -> UTF-16");
437 char *pszUtf8;
438 int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
439 if (rc == VINF_SUCCESS)
440 {
441 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
442 RTTestFailed(hTest, "UTF-16 -> UTF-8 mismatch!");
443
444 PRTUTF16 pwszUtf16;
445 rc = RTStrToUtf16(pszUtf8, &pwszUtf16);
446 if (rc == VINF_SUCCESS)
447 {
448 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
449 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
450 RTUtf16Free(pwszUtf16);
451 }
452 else
453 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
454 RTStrFree(pszUtf8);
455 }
456 else
457 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
458
459
460 /*
461 * Convert to UTF-16 and back. (just in case the above test fails)
462 */
463 RTTestSub(hTest, "UTF-8 -> UTF-16 -> UTF-8");
464 PRTUTF16 pwszUtf16;
465 rc = RTStrToUtf16(&g_szAll[0], &pwszUtf16);
466 if (rc == VINF_SUCCESS)
467 {
468 if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
469 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
470
471 char *pszUtf8;
472 rc = RTUtf16ToUtf8(pwszUtf16, &pszUtf8);
473 if (rc == VINF_SUCCESS)
474 {
475 if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
476 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
477 RTStrFree(pszUtf8);
478 }
479 else
480 RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
481 RTUtf16Free(pwszUtf16);
482 }
483 else
484 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
485
486 /*
487 * Convert UTF-8 to CPs.
488 */
489 RTTestSub(hTest, "UTF-8 -> UNI -> UTF-8");
490 PRTUNICP paCps;
491 rc = RTStrToUni(g_szAll, &paCps);
492 if (rc == VINF_SUCCESS)
493 {
494 if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
495 RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
496
497 size_t cCps;
498 rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
499 if (rc == VINF_SUCCESS)
500 {
501 if (cCps != RT_ELEMENTS(g_uszAll) - 1)
502 RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
503 }
504 else
505 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
506
507 /** @todo RTCpsToUtf8 or something. */
508 }
509 else
510 RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
511
512 /*
513 * Check the various string lengths.
514 */
515 RTTestSub(hTest, "Lengths");
516 size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
517 size_t cuc2 = RTUtf16Len(g_wszAll);
518 if (cuc1 != cuc2)
519 RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
520 //size_t cuc3 = RTUniLen(g_uszAll);
521
522
523 /*
524 * Enumerate the strings.
525 */
526 RTTestSub(hTest, "Code Point Getters and Putters");
527 char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
528 AssertRelease(pszPut1Base);
529 char *pszPut1 = pszPut1Base;
530 PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
531 AssertRelease(pwszPut2Base);
532 PRTUTF16 pwszPut2 = pwszPut2Base;
533 const char *psz1 = g_szAll;
534 const char *psz2 = g_szAll;
535 PCRTUTF16 pwsz3 = g_wszAll;
536 PCRTUTF16 pwsz4 = g_wszAll;
537 for (;;)
538 {
539 /*
540 * getters
541 */
542 RTUNICP uc1;
543 rc = RTStrGetCpEx(&psz1, &uc1);
544 if (RT_FAILURE(rc))
545 {
546 RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
547 whereami(8, psz2 - &g_szAll[0]);
548 break;
549 }
550 char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
551 if (pszPrev1 != psz2)
552 {
553 RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
554 whereami(8, psz2 - &g_szAll[0]);
555 break;
556 }
557 RTUNICP uc2 = RTStrGetCp(psz2);
558 if (uc2 != uc1)
559 {
560 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
561 whereami(8, psz2 - &g_szAll[0]);
562 break;
563 }
564 psz2 = RTStrNextCp(psz2);
565 if (psz2 != psz1)
566 {
567 RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
568 whereami(8, psz2 - &g_szAll[0]);
569 break;
570 }
571
572 RTUNICP uc3;
573 rc = RTUtf16GetCpEx(&pwsz3, &uc3);
574 if (RT_FAILURE(rc))
575 {
576 RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
577 whereami(16, pwsz4 - &g_wszAll[0]);
578 break;
579 }
580 if (uc3 != uc2)
581 {
582 RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
583 whereami(16, pwsz4 - &g_wszAll[0]);
584 break;
585 }
586 RTUNICP uc4 = RTUtf16GetCp(pwsz4);
587 if (uc3 != uc4)
588 {
589 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
590 whereami(16, pwsz4 - &g_wszAll[0]);
591 break;
592 }
593 pwsz4 = RTUtf16NextCp(pwsz4);
594 if (pwsz4 != pwsz3)
595 {
596 RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
597 whereami(8, pwsz4 - &g_wszAll[0]);
598 break;
599 }
600
601
602 /*
603 * putters
604 */
605 pszPut1 = RTStrPutCp(pszPut1, uc1);
606 if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
607 {
608 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
609 pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
610 whereami(8, psz2 - &g_szAll[0]);
611 break;
612 }
613
614 pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
615 if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
616 {
617 RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
618 pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
619 whereami(8, pwsz4 - &g_wszAll[0]);
620 break;
621 }
622
623
624 /* the end? */
625 if (!uc1)
626 break;
627 }
628
629 /* check output if we seems to have made it thru it all. */
630 if (psz2 == &g_szAll[sizeof(g_szAll)])
631 {
632 if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
633 RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
634 if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
635 RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
636 }
637
638 RTMemFree(pszPut1Base);
639 RTMemFree(pwszPut2Base);
640
641 RTTestSubDone(hTest);
642}
643
644
645/**
646 * Check case insensitivity.
647 */
648void test3(RTTEST hTest)
649{
650 RTTestSub(hTest, "Case Sensitivitity");
651
652 if ( RTUniCpToLower('a') != 'a'
653 || RTUniCpToLower('A') != 'a'
654 || RTUniCpToLower('b') != 'b'
655 || RTUniCpToLower('B') != 'b'
656 || RTUniCpToLower('Z') != 'z'
657 || RTUniCpToLower('z') != 'z'
658 || RTUniCpToUpper('c') != 'C'
659 || RTUniCpToUpper('C') != 'C'
660 || RTUniCpToUpper('z') != 'Z'
661 || RTUniCpToUpper('Z') != 'Z')
662 RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
663
664 if (RTUtf16ICmp(g_wszAll, g_wszAll))
665 RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
666
667 if (RTUtf16Cmp(g_wszAll, g_wszAll))
668 RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
669
670 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 };
671 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 };
672 if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
673 || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
674 || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
675 || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
676 )
677 RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
678
679 if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
680 || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
681 || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
682 || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
683 )
684 RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
685
686 RTTestSubDone(hTest);
687}
688
689
690/**
691 * Test the RTStr*Cmp functions.
692 */
693void TstRTStrXCmp(RTTEST hTest)
694{
695#define CHECK_DIFF(expr, op) \
696 do \
697 { \
698 int iDiff = expr; \
699 if (!(iDiff op 0)) \
700 RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
701 } while (0)
702
703/** @todo test the non-ascii bits. */
704
705 RTTestSub(hTest, "RTStrCmp");
706 CHECK_DIFF(RTStrCmp(NULL, NULL), == );
707 CHECK_DIFF(RTStrCmp(NULL, ""), < );
708 CHECK_DIFF(RTStrCmp("", NULL), > );
709 CHECK_DIFF(RTStrCmp("", ""), == );
710 CHECK_DIFF(RTStrCmp("abcdef", "abcdef"), == );
711 CHECK_DIFF(RTStrCmp("abcdef", "abcde"), > );
712 CHECK_DIFF(RTStrCmp("abcde", "abcdef"), < );
713 CHECK_DIFF(RTStrCmp("abcdeg", "abcdef"), > );
714 CHECK_DIFF(RTStrCmp("abcdef", "abcdeg"), < );
715 CHECK_DIFF(RTStrCmp("abcdeF", "abcdef"), < );
716 CHECK_DIFF(RTStrCmp("abcdef", "abcdeF"), > );
717
718
719 RTTestSub(hTest, "RTStrNCmp");
720 CHECK_DIFF(RTStrNCmp(NULL, NULL, RTSTR_MAX), == );
721 CHECK_DIFF(RTStrNCmp(NULL, "", RTSTR_MAX), < );
722 CHECK_DIFF(RTStrNCmp("", NULL, RTSTR_MAX), > );
723 CHECK_DIFF(RTStrNCmp("", "", RTSTR_MAX), == );
724 CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
725 CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
726 CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
727 CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
728 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
729 CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
730 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
731
732 CHECK_DIFF(RTStrNCmp("abcdef", "fedcba", 0), ==);
733 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 5), ==);
734 CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", 6), > );
735
736
737 RTTestSub(hTest, "RTStrICmp");
738 CHECK_DIFF(RTStrICmp(NULL, NULL), == );
739 CHECK_DIFF(RTStrICmp(NULL, ""), < );
740 CHECK_DIFF(RTStrICmp("", NULL), > );
741 CHECK_DIFF(RTStrICmp("", ""), == );
742 CHECK_DIFF(RTStrICmp("abcdef", "abcdef"), == );
743 CHECK_DIFF(RTStrICmp("abcdef", "abcde"), > );
744 CHECK_DIFF(RTStrICmp("abcde", "abcdef"), < );
745 CHECK_DIFF(RTStrICmp("abcdeg", "abcdef"), > );
746 CHECK_DIFF(RTStrICmp("abcdef", "abcdeg"), < );
747
748 CHECK_DIFF(RTStrICmp("abcdeF", "abcdef"), ==);
749 CHECK_DIFF(RTStrICmp("abcdef", "abcdeF"), ==);
750 CHECK_DIFF(RTStrICmp("ABCDEF", "abcdef"), ==);
751 CHECK_DIFF(RTStrICmp("abcdef", "ABCDEF"), ==);
752 CHECK_DIFF(RTStrICmp("AbCdEf", "aBcDeF"), ==);
753 CHECK_DIFF(RTStrICmp("AbCdEg", "aBcDeF"), > );
754 CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
755
756
757
758 RTTestSub(hTest, "RTStrNICmp");
759 CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
760 CHECK_DIFF(RTStrNICmp(NULL, "", RTSTR_MAX), < );
761 CHECK_DIFF(RTStrNICmp("", NULL, RTSTR_MAX), > );
762 CHECK_DIFF(RTStrNICmp("", "", RTSTR_MAX), == );
763 CHECK_DIFF(RTStrNICmp(NULL, NULL, 0), == );
764 CHECK_DIFF(RTStrNICmp(NULL, "", 0), == );
765 CHECK_DIFF(RTStrNICmp("", NULL, 0), == );
766 CHECK_DIFF(RTStrNICmp("", "", 0), == );
767 CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
768 CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
769 CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
770 CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
771 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
772
773 CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
774 CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
775 CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
776 CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
777 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
778 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
779 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
780
781 CHECK_DIFF(RTStrNICmp("ABCDEF", "fedcba", 0), ==);
782 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 5), ==);
783 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", 5), ==);
784 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDe", 5), ==);
785 CHECK_DIFF(RTStrNICmp("AbCdE", "aBcDeF", 5), ==);
786 CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDe", 5), ==);
787 CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", 6), > );
788 CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
789 /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
790 // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
791
792 RTTestSubDone(hTest);
793}
794
795
796
797/**
798 * Benchmark stuff.
799 */
800void Benchmarks(RTTEST hTest)
801{
802 static union
803 {
804 RTUTF16 wszBuf[sizeof(g_wszAll)];
805 char szBuf[sizeof(g_szAll)];
806 } s_Buf;
807
808 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
809 PRTUTF16 pwsz = &s_Buf.wszBuf[0];
810 int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
811 if (RT_SUCCESS(rc))
812 {
813 int i;
814 uint64_t u64Start = RTTimeNanoTS();
815 for (i = 0; i < 100; i++)
816 {
817 rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
818 if (RT_FAILURE(rc))
819 {
820 RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
821 break;
822 }
823 }
824 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
825 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed);
826 }
827
828 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
829 char *psz = &s_Buf.szBuf[0];
830 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
831 if (RT_SUCCESS(rc))
832 {
833 int i;
834 uint64_t u64Start = RTTimeNanoTS();
835 for (i = 0; i < 100; i++)
836 {
837 rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
838 if (RT_FAILURE(rc))
839 {
840 RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
841 break;
842 }
843 }
844 uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
845 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed);
846 }
847
848}
849
850
851/**
852 * Tests RTStrStr and RTStrIStr.
853 */
854static void testStrStr(RTTEST hTest)
855{
856#define CHECK_NULL(expr) \
857 do { \
858 const char *pszRet = expr; \
859 if (pszRet != NULL) \
860 RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
861 } while (0)
862
863#define CHECK(expr, expect) \
864 do { \
865 const char *pszRet = expr; \
866 if ( (pszRet != NULL && (expect) == NULL) \
867 || (pszRet == NULL && (expect) != NULL) \
868 || strcmp(pszRet, (expect)) \
869 ) \
870 RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
871 } while (0)
872
873
874 RTTestSub(hTest, "RTStrStr");
875 CHECK(RTStrStr("abcdef", ""), "abcdef");
876 CHECK_NULL(RTStrStr("abcdef", NULL));
877 CHECK_NULL(RTStrStr(NULL, ""));
878 CHECK_NULL(RTStrStr(NULL, NULL));
879 CHECK(RTStrStr("abcdef", "abcdef"), "abcdef");
880 CHECK(RTStrStr("abcdef", "b"), "bcdef");
881 CHECK(RTStrStr("abcdef", "bcdef"), "bcdef");
882 CHECK(RTStrStr("abcdef", "cdef"), "cdef");
883 CHECK(RTStrStr("abcdef", "cde"), "cdef");
884 CHECK(RTStrStr("abcdef", "cd"), "cdef");
885 CHECK(RTStrStr("abcdef", "c"), "cdef");
886 CHECK(RTStrStr("abcdef", "f"), "f");
887 CHECK(RTStrStr("abcdef", "ef"), "ef");
888 CHECK(RTStrStr("abcdef", "e"), "ef");
889 CHECK_NULL(RTStrStr("abcdef", "z"));
890 CHECK_NULL(RTStrStr("abcdef", "A"));
891 CHECK_NULL(RTStrStr("abcdef", "F"));
892
893 RTTestSub(hTest, "RTStrIStr");
894 CHECK(RTStrIStr("abcdef", ""), "abcdef");
895 CHECK_NULL(RTStrIStr("abcdef", NULL));
896 CHECK_NULL(RTStrIStr(NULL, ""));
897 CHECK_NULL(RTStrIStr(NULL, NULL));
898 CHECK(RTStrIStr("abcdef", "abcdef"), "abcdef");
899 CHECK(RTStrIStr("abcdef", "Abcdef"), "abcdef");
900 CHECK(RTStrIStr("abcdef", "ABcDeF"), "abcdef");
901 CHECK(RTStrIStr("abcdef", "b"), "bcdef");
902 CHECK(RTStrIStr("abcdef", "B"), "bcdef");
903 CHECK(RTStrIStr("abcdef", "bcdef"), "bcdef");
904 CHECK(RTStrIStr("abcdef", "BCdEf"), "bcdef");
905 CHECK(RTStrIStr("abcdef", "bCdEf"), "bcdef");
906 CHECK(RTStrIStr("abcdef", "bcdEf"), "bcdef");
907 CHECK(RTStrIStr("abcdef", "BcdEf"), "bcdef");
908 CHECK(RTStrIStr("abcdef", "cdef"), "cdef");
909 CHECK(RTStrIStr("abcdef", "cde"), "cdef");
910 CHECK(RTStrIStr("abcdef", "cd"), "cdef");
911 CHECK(RTStrIStr("abcdef", "c"), "cdef");
912 CHECK(RTStrIStr("abcdef", "f"), "f");
913 CHECK(RTStrIStr("abcdeF", "F"), "F");
914 CHECK(RTStrIStr("abcdef", "F"), "f");
915 CHECK(RTStrIStr("abcdef", "ef"), "ef");
916 CHECK(RTStrIStr("EeEef", "e"), "EeEef");
917 CHECK(RTStrIStr("EeEef", "E"), "EeEef");
918 CHECK(RTStrIStr("EeEef", "EE"), "EeEef");
919 CHECK(RTStrIStr("EeEef", "EEE"), "EeEef");
920 CHECK(RTStrIStr("EeEef", "EEEF"), "eEef");
921 CHECK_NULL(RTStrIStr("EeEef", "z"));
922
923#undef CHECK
924#undef CHECK_NULL
925 RTTestSubDone(hTest);
926}
927
928
929void testMinistring(RTTEST hTest)
930{
931 RTTestSub(hTest, "class ministring");
932
933#define CHECK(expr) \
934 do { \
935 if (!(expr)) \
936 RTTestFailed(hTest, "%d: FAILED %s", __LINE__, #expr); \
937 } while (0)
938
939#define CHECK_DUMP(expr, value) \
940 do { \
941 if (!(expr)) \
942 RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
943 } while (0)
944
945#define CHECK_DUMP_I(expr) \
946 do { \
947 if (!(expr)) \
948 RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
949 } while (0)
950
951 ministring empty;
952 CHECK( (empty.length() == 0) );
953 CHECK( (empty.capacity() == 0) );
954
955 ministring sixbytes("12345");
956 CHECK( (sixbytes.length() == 5) );
957 CHECK( (sixbytes.capacity() == 6) );
958
959 sixbytes.append("678");
960 CHECK( (sixbytes.length() == 8) );
961 CHECK( (sixbytes.capacity() == 9) );
962
963 char *psz = sixbytes.mutableRaw();
964 // 12345678
965 // ^
966 // 0123456
967 psz[6] = '\0';
968 sixbytes.jolt();
969 CHECK( (sixbytes.length() == 6) );
970 CHECK( (sixbytes.capacity() == 7) );
971
972 ministring morebytes("tobereplaced");
973 morebytes = "newstring ";
974 morebytes.append(sixbytes);
975
976 CHECK_DUMP( (morebytes == "newstring 123456"), morebytes.c_str() );
977
978 ministring third(morebytes);
979 third.reserve(100 * 1024); // 100 KB
980 CHECK_DUMP( (third == "newstring 123456"), morebytes.c_str() );
981 CHECK( (third.capacity() == 100 * 1024) );
982 CHECK( (third.length() == morebytes.length()) ); // must not have changed
983
984 ministring copy1(morebytes);
985 ministring copy2 = morebytes;
986 CHECK( (copy1 == copy2) );
987
988 copy1 = NULL;
989 CHECK( (copy1.isNull()) );
990
991 copy1 = "";
992 CHECK( (copy1.isEmpty()) );
993
994 CHECK( (ministring("abc") < ministring("def")) );
995 CHECK( (ministring("abc") != ministring("def")) );
996 CHECK_DUMP_I( (ministring("def") > ministring("abc")) );
997
998 copy2.setNull();
999 for (int i = 0;
1000 i < 100;
1001 ++i)
1002 {
1003 copy2.reserve(50); // should be ignored after 50 loops
1004 copy2.append("1");
1005 }
1006 CHECK( (copy2.length() == 100) );
1007
1008#undef CHECK
1009}
1010
1011int main()
1012{
1013 /*
1014 * Init the runtime, test and say hello.
1015 */
1016 RTTEST hTest;
1017 int rc = RTTestInitAndCreate("tstUtf8", &hTest);
1018 if (rc)
1019 return rc;
1020 RTTestBanner(hTest);
1021
1022 /*
1023 * Run the test.
1024 */
1025 InitStrings();
1026 test1(hTest);
1027 test2(hTest);
1028 test3(hTest);
1029 TstRTStrXCmp(hTest);
1030 testStrStr(hTest);
1031
1032 testMinistring(hTest);
1033
1034 Benchmarks(hTest);
1035
1036 /*
1037 * Summary
1038 */
1039 return RTTestSummaryAndDestroy(hTest);
1040}
1041
Note: See TracBrowser for help on using the repository browser.

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