VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp@ 35567

Last change on this file since 35567 was 35567, checked in by vboxsync, 14 years ago

IPRT: fix rare crash in MiniString::substr(); rename substr() to substrCP() and add a substr that operates on bytes, not codepoints; more to come

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.1 KB
Line 
1/* $Id: tstIprtMiniString.cpp 35567 2011-01-14 14:16:45Z vboxsync $ */
2/** @file
3 * IPRT Testcase - iprt::MiniString.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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/cpp/ministring.h>
31
32#include <iprt/err.h>
33#include <iprt/mem.h>
34#include <iprt/string.h>
35#include <iprt/test.h>
36#include <iprt/uni.h>
37
38
39static void test1Hlp1(const char *pszExpect, const char *pszFormat, ...)
40{
41#if 0
42 va_list va;
43 va_start(va, pszFormat);
44 iprt::MiniString strTst(pszFormat, va);
45 va_end(va);
46 RTTESTI_CHECK_MSG(strTst.equals(pszExpect), ("strTst='%s' expected='%s'\n", strTst.c_str(), pszExpect));
47#endif
48}
49
50static void test1(RTTEST hTest)
51{
52 RTTestSub(hTest, "Basics");
53
54#define CHECK(expr) RTTESTI_CHECK(expr)
55#define CHECK_DUMP(expr, value) \
56 do { \
57 if (!(expr)) \
58 RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
59 } while (0)
60
61#define CHECK_DUMP_I(expr) \
62 do { \
63 if (!(expr)) \
64 RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
65 } while (0)
66#define CHECK_EQUAL(Str, szExpect) \
67 do { \
68 if (!(Str).equals(szExpect)) \
69 RTTestIFailed("line %u: expected \"%s\" got \"%s\"", __LINE__, szExpect, (Str).c_str()); \
70 } while (0)
71
72 iprt::MiniString empty;
73 CHECK(empty.length() == 0);
74 CHECK(empty.capacity() == 0);
75
76 iprt::MiniString sixbytes("12345");
77 CHECK(sixbytes.length() == 5);
78 CHECK(sixbytes.capacity() == 6);
79
80 sixbytes.append(iprt::MiniString("678"));
81 CHECK(sixbytes.length() == 8);
82 CHECK(sixbytes.capacity() >= 9);
83
84 sixbytes.append("9a");
85 CHECK(sixbytes.length() == 10);
86 CHECK(sixbytes.capacity() >= 11);
87
88 char *psz = sixbytes.mutableRaw();
89 // 123456789a
90 // ^
91 // 0123456
92 psz[6] = '\0';
93 sixbytes.jolt();
94 CHECK(sixbytes.length() == 6);
95 CHECK(sixbytes.capacity() == 7);
96
97 iprt::MiniString morebytes("tobereplaced");
98 morebytes = "newstring ";
99 morebytes.append(sixbytes);
100
101 CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
102
103 iprt::MiniString third(morebytes);
104 third.reserve(100 * 1024); // 100 KB
105 CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
106 CHECK(third.capacity() == 100 * 1024);
107 CHECK(third.length() == morebytes.length()); // must not have changed
108
109 iprt::MiniString copy1(morebytes);
110 iprt::MiniString copy2 = morebytes;
111 CHECK(copy1 == copy2);
112
113 copy1 = NULL;
114 CHECK(copy1.length() == 0);
115
116 copy1 = "";
117 CHECK(copy1.length() == 0);
118
119 CHECK(iprt::MiniString("abc") < iprt::MiniString("def"));
120 CHECK(iprt::MiniString("") < iprt::MiniString("def"));
121 CHECK(iprt::MiniString("abc") > iprt::MiniString(""));
122 CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
123 CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
124 CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
125 CHECK(iprt::MiniString("").compare("") == 0);
126 CHECK(iprt::MiniString("").compare(NULL) == 0);
127 CHECK(iprt::MiniString("").compare("a") < 0);
128 CHECK(iprt::MiniString("a").compare("") > 0);
129 CHECK(iprt::MiniString("a").compare(NULL) > 0);
130
131 CHECK(iprt::MiniString("abc") < "def");
132 CHECK(iprt::MiniString("abc") != "def");
133 CHECK_DUMP_I(iprt::MiniString("def") > "abc");
134 CHECK(iprt::MiniString("abc") == "abc");
135
136 CHECK(iprt::MiniString("abc").equals("abc"));
137 CHECK(!iprt::MiniString("abc").equals("def"));
138 CHECK(iprt::MiniString("abc").equalsIgnoreCase("Abc"));
139 CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABc"));
140 CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABC"));
141 CHECK(!iprt::MiniString("abc").equalsIgnoreCase("dBC"));
142 CHECK(iprt::MiniString("").equals(""));
143 CHECK(iprt::MiniString("").equals(NULL));
144 CHECK(!iprt::MiniString("").equals("a"));
145 CHECK(!iprt::MiniString("a").equals(""));
146 CHECK(!iprt::MiniString("a").equals(NULL));
147 CHECK(iprt::MiniString("").equalsIgnoreCase(""));
148 CHECK(iprt::MiniString("").equalsIgnoreCase(NULL));
149 CHECK(!iprt::MiniString("").equalsIgnoreCase("a"));
150 CHECK(!iprt::MiniString("a").equalsIgnoreCase(""));
151
152 copy2.setNull();
153 for (int i = 0; i < 100; ++i)
154 {
155 copy2.reserve(50); // should be ignored after 50 loops
156 copy2.append("1");
157 }
158 CHECK(copy2.length() == 100);
159
160 copy2.setNull();
161 for (int i = 0; i < 100; ++i)
162 {
163 copy2.reserve(50); // should be ignored after 50 loops
164 copy2.append('1');
165 }
166 CHECK(copy2.length() == 100);
167
168 /* printf */
169 iprt::MiniString StrFmt;
170 CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
171 test1Hlp1("abc-42-def", "%s-%d-%s", "abc", 42, "def");
172 test1Hlp1("", "");
173 test1Hlp1("1", "1");
174 test1Hlp1("foobar", "%s", "foobar");
175
176 /* substring constructors */
177 iprt::MiniString SubStr1("", (size_t)0);
178 CHECK_EQUAL(SubStr1, "");
179
180 iprt::MiniString SubStr2("abcdef", 2);
181 CHECK_EQUAL(SubStr2, "ab");
182
183 iprt::MiniString SubStr3("abcdef", 1);
184 CHECK_EQUAL(SubStr3, "a");
185
186 iprt::MiniString SubStr4("abcdef", 6);
187 CHECK_EQUAL(SubStr4, "abcdef");
188
189 iprt::MiniString SubStr5("abcdef", 7);
190 CHECK_EQUAL(SubStr5, "abcdef");
191
192
193 iprt::MiniString SubStrBase("abcdef");
194
195 iprt::MiniString SubStr10(SubStrBase, 0);
196 CHECK_EQUAL(SubStr10, "abcdef");
197
198 iprt::MiniString SubStr11(SubStrBase, 1);
199 CHECK_EQUAL(SubStr11, "bcdef");
200
201 iprt::MiniString SubStr12(SubStrBase, 1, 1);
202 CHECK_EQUAL(SubStr12, "b");
203
204 iprt::MiniString SubStr13(SubStrBase, 2, 3);
205 CHECK_EQUAL(SubStr13, "cde");
206
207 iprt::MiniString SubStr14(SubStrBase, 2, 4);
208 CHECK_EQUAL(SubStr14, "cdef");
209
210 iprt::MiniString SubStr15(SubStrBase, 2, 5);
211 CHECK_EQUAL(SubStr15, "cdef");
212
213 /* substr() and substrCP() functions */
214 iprt::MiniString strTest("");
215 CHECK_EQUAL(strTest.substr(0), "");
216 CHECK_EQUAL(strTest.substrCP(0), "");
217 CHECK_EQUAL(strTest.substr(1), "");
218 CHECK_EQUAL(strTest.substrCP(1), "");
219
220 /* now let's have some non-ASCII to chew on */
221 strTest = "abcdefßäbcdef";
222 // 13 codepoints, but 15 bytes (excluding null terminator);
223 // "ß" and "ä" consume two bytes each
224 CHECK_EQUAL(strTest.substr(0), strTest.c_str());
225 CHECK_EQUAL(strTest.substrCP(0), strTest.c_str());
226
227 CHECK_EQUAL(strTest.substr(2), "cdefßäbcdef");
228 CHECK_EQUAL(strTest.substrCP(2), "cdefßäbcdef");
229
230 CHECK_EQUAL(strTest.substr(2, 2), "cd");
231 CHECK_EQUAL(strTest.substrCP(2, 2), "cd");
232
233 CHECK_EQUAL(strTest.substr(6), "ßäbcdef");
234 CHECK_EQUAL(strTest.substrCP(6), "ßäbcdef");
235
236 CHECK_EQUAL(strTest.substr(6, 2), "ß"); // UTF-8 "ß" consumes two bytes
237 CHECK_EQUAL(strTest.substrCP(6, 1), "ß");
238
239 CHECK_EQUAL(strTest.substr(8), "äbcdef"); // UTF-8 "ß" consumes two bytes
240 CHECK_EQUAL(strTest.substrCP(7), "äbcdef");
241
242 CHECK_EQUAL(strTest.substr(8, 3), "äb"); // UTF-8 "ä" consumes two bytes
243 CHECK_EQUAL(strTest.substrCP(7, 2), "äb");
244
245 CHECK_EQUAL(strTest.substr(14, 1), "f");
246 CHECK_EQUAL(strTest.substrCP(12, 1), "f");
247
248 CHECK_EQUAL(strTest.substr(15, 1), "");
249 CHECK_EQUAL(strTest.substrCP(13, 1), "");
250
251 CHECK_EQUAL(strTest.substr(16, 1), "");
252 CHECK_EQUAL(strTest.substrCP(15, 1), "");
253
254 /* special constructor and assignment arguments */
255 iprt::MiniString StrCtor1("");
256 RTTESTI_CHECK(StrCtor1.isEmpty());
257 RTTESTI_CHECK(StrCtor1.length() == 0);
258
259 iprt::MiniString StrCtor2(NULL);
260 RTTESTI_CHECK(StrCtor2.isEmpty());
261 RTTESTI_CHECK(StrCtor2.length() == 0);
262
263 iprt::MiniString StrCtor1d(StrCtor1);
264 RTTESTI_CHECK(StrCtor1d.isEmpty());
265 RTTESTI_CHECK(StrCtor1d.length() == 0);
266
267 iprt::MiniString StrCtor2d(StrCtor2);
268 RTTESTI_CHECK(StrCtor2d.isEmpty());
269 RTTESTI_CHECK(StrCtor2d.length() == 0);
270
271 for (unsigned i = 0; i < 2; i++)
272 {
273 iprt::MiniString StrAssign;
274 if (i) StrAssign = "abcdef";
275 StrAssign = (char *)NULL;
276 RTTESTI_CHECK(StrAssign.isEmpty());
277 RTTESTI_CHECK(StrAssign.length() == 0);
278
279 if (i) StrAssign = "abcdef";
280 StrAssign = "";
281 RTTESTI_CHECK(StrAssign.isEmpty());
282 RTTESTI_CHECK(StrAssign.length() == 0);
283
284 if (i) StrAssign = "abcdef";
285 StrAssign = StrCtor1;
286 RTTESTI_CHECK(StrAssign.isEmpty());
287 RTTESTI_CHECK(StrAssign.length() == 0);
288
289 if (i) StrAssign = "abcdef";
290 StrAssign = StrCtor2;
291 RTTESTI_CHECK(StrAssign.isEmpty());
292 RTTESTI_CHECK(StrAssign.length() == 0);
293 }
294
295#undef CHECK
296#undef CHECK_DUMP
297#undef CHECK_DUMP_I
298#undef CHECK_EQUAL
299}
300
301
302static int mymemcmp(const char *psz1, const char *psz2, size_t cch)
303{
304 for (size_t off = 0; off < cch; off++)
305 if (psz1[off] != psz2[off])
306 {
307 RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
308 RT_MIN(cch - off, 8), &psz1[off],
309 RT_MIN(cch - off, 8), &psz2[off]);
310 return psz1[off] > psz2[off] ? 1 : -1;
311 }
312 return 0;
313}
314
315static void test2(RTTEST hTest)
316{
317 RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
318
319#define CHECK_EQUAL(str1, str2) \
320 do \
321 { \
322 RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
323 RTTESTI_CHECK((str1).length() == (str2).length()); \
324 RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
325 } while (0)
326
327 iprt::MiniString strTmp;
328 char szDst[16];
329
330 /* Collect all upper and lower case code points. */
331 iprt::MiniString strLower("");
332 strLower.reserve(_4M);
333
334 iprt::MiniString strUpper("");
335 strUpper.reserve(_4M);
336
337 for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
338 {
339 if (RTUniCpIsLower(uc))
340 {
341 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
342 strLower.appendCodePoint(uc);
343 }
344 if (RTUniCpIsUpper(uc))
345 {
346 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
347 strUpper.appendCodePoint(uc);
348 }
349 }
350 RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
351 RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
352
353 /* Fold each code point in the lower case string and check that it encodes
354 into the same or less number of bytes. */
355 size_t cch = 0;
356 const char *pszCur = strLower.c_str();
357 iprt::MiniString strUpper2("");
358 strUpper2.reserve(strLower.length() + 64);
359 for (;;)
360 {
361 RTUNICP ucLower;
362 const char * const pszPrev = pszCur;
363 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
364 size_t const cchSrc = pszCur - pszPrev;
365 if (!ucLower)
366 break;
367
368 RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
369 const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
370 size_t const cchDst = pszDstEnd - &szDst[0];
371 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
372 ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
373 ucLower, cchSrc, ucUpper, cchDst));
374 cch += cchDst;
375 strUpper2.appendCodePoint(ucUpper);
376
377 /* roundtrip stability */
378 RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
379 RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
380
381 RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
382 RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
383 RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
384
385 pszDstEnd = RTStrPutCp(szDst, ucLower2);
386 size_t const cchLower2 = pszDstEnd - &szDst[0];
387 RTTESTI_CHECK_MSG(cchDst == cchLower2,
388 ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
389 ucLower2, cchLower2, ucUpper, cchDst));
390 }
391 RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
392 RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
393
394 /* the toUpper method shall do the same thing. */
395 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
396 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
397
398 /* Ditto for the upper case string. */
399 cch = 0;
400 pszCur = strUpper.c_str();
401 iprt::MiniString strLower2("");
402 strLower2.reserve(strUpper.length() + 64);
403 for (;;)
404 {
405 RTUNICP ucUpper;
406 const char * const pszPrev = pszCur;
407 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
408 size_t const cchSrc = pszCur - pszPrev;
409 if (!ucUpper)
410 break;
411
412 RTUNICP const ucLower = RTUniCpToLower(ucUpper);
413 const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
414 size_t const cchDst = pszDstEnd - &szDst[0];
415 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
416 ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
417 ucUpper, cchSrc, ucLower, cchDst));
418
419 cch += cchDst;
420 strLower2.appendCodePoint(ucLower);
421
422 /* roundtrip stability */
423 RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
424 RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
425
426 RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
427 RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
428 RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
429
430 pszDstEnd = RTStrPutCp(szDst, ucUpper2);
431 size_t const cchUpper2 = pszDstEnd - &szDst[0];
432 RTTESTI_CHECK_MSG(cchDst == cchUpper2,
433 ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
434 ucUpper2, cchUpper2, ucLower, cchDst));
435 }
436 RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
437 RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
438
439 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
440 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
441
442 /* Checks of folding stability when nothing shall change. */
443 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
444 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
445 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
446 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
447
448 strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
449 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
450 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
451 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
452
453 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
454 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
455 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
456 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
457
458 strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
459 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
460 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
461 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
462
463 /* Check folding stability for roundtrips. */
464 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
465 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
466 strTmp.toUpper();
467 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
468 strTmp.toUpper();
469 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
470
471 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
472 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
473 strTmp.toLower();
474 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
475 strTmp.toLower();
476 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
477}
478
479
480int main()
481{
482 RTTEST hTest;
483 RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
484 if (rcExit == RTEXITCODE_SUCCESS)
485 {
486 RTTestBanner(hTest);
487
488 test1(hTest);
489 test2(hTest);
490
491 rcExit = RTTestSummaryAndDestroy(hTest);
492 }
493 return rcExit;
494}
495
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