VirtualBox

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

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

IPRT: another ministring testcase

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.2 KB
Line 
1/* $Id: tstIprtMiniString.cpp 35568 2011-01-14 14:24:53Z 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 /* and check cooperation with find() */
255 size_t pos = strTest.find("ß");
256 CHECK_EQUAL(strTest.substr(pos), "ßäbcdef");
257
258 /* special constructor and assignment arguments */
259 iprt::MiniString StrCtor1("");
260 RTTESTI_CHECK(StrCtor1.isEmpty());
261 RTTESTI_CHECK(StrCtor1.length() == 0);
262
263 iprt::MiniString StrCtor2(NULL);
264 RTTESTI_CHECK(StrCtor2.isEmpty());
265 RTTESTI_CHECK(StrCtor2.length() == 0);
266
267 iprt::MiniString StrCtor1d(StrCtor1);
268 RTTESTI_CHECK(StrCtor1d.isEmpty());
269 RTTESTI_CHECK(StrCtor1d.length() == 0);
270
271 iprt::MiniString StrCtor2d(StrCtor2);
272 RTTESTI_CHECK(StrCtor2d.isEmpty());
273 RTTESTI_CHECK(StrCtor2d.length() == 0);
274
275 for (unsigned i = 0; i < 2; i++)
276 {
277 iprt::MiniString StrAssign;
278 if (i) StrAssign = "abcdef";
279 StrAssign = (char *)NULL;
280 RTTESTI_CHECK(StrAssign.isEmpty());
281 RTTESTI_CHECK(StrAssign.length() == 0);
282
283 if (i) StrAssign = "abcdef";
284 StrAssign = "";
285 RTTESTI_CHECK(StrAssign.isEmpty());
286 RTTESTI_CHECK(StrAssign.length() == 0);
287
288 if (i) StrAssign = "abcdef";
289 StrAssign = StrCtor1;
290 RTTESTI_CHECK(StrAssign.isEmpty());
291 RTTESTI_CHECK(StrAssign.length() == 0);
292
293 if (i) StrAssign = "abcdef";
294 StrAssign = StrCtor2;
295 RTTESTI_CHECK(StrAssign.isEmpty());
296 RTTESTI_CHECK(StrAssign.length() == 0);
297 }
298
299#undef CHECK
300#undef CHECK_DUMP
301#undef CHECK_DUMP_I
302#undef CHECK_EQUAL
303}
304
305
306static int mymemcmp(const char *psz1, const char *psz2, size_t cch)
307{
308 for (size_t off = 0; off < cch; off++)
309 if (psz1[off] != psz2[off])
310 {
311 RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
312 RT_MIN(cch - off, 8), &psz1[off],
313 RT_MIN(cch - off, 8), &psz2[off]);
314 return psz1[off] > psz2[off] ? 1 : -1;
315 }
316 return 0;
317}
318
319static void test2(RTTEST hTest)
320{
321 RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
322
323#define CHECK_EQUAL(str1, str2) \
324 do \
325 { \
326 RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
327 RTTESTI_CHECK((str1).length() == (str2).length()); \
328 RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
329 } while (0)
330
331 iprt::MiniString strTmp;
332 char szDst[16];
333
334 /* Collect all upper and lower case code points. */
335 iprt::MiniString strLower("");
336 strLower.reserve(_4M);
337
338 iprt::MiniString strUpper("");
339 strUpper.reserve(_4M);
340
341 for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
342 {
343 if (RTUniCpIsLower(uc))
344 {
345 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
346 strLower.appendCodePoint(uc);
347 }
348 if (RTUniCpIsUpper(uc))
349 {
350 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
351 strUpper.appendCodePoint(uc);
352 }
353 }
354 RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
355 RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
356
357 /* Fold each code point in the lower case string and check that it encodes
358 into the same or less number of bytes. */
359 size_t cch = 0;
360 const char *pszCur = strLower.c_str();
361 iprt::MiniString strUpper2("");
362 strUpper2.reserve(strLower.length() + 64);
363 for (;;)
364 {
365 RTUNICP ucLower;
366 const char * const pszPrev = pszCur;
367 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
368 size_t const cchSrc = pszCur - pszPrev;
369 if (!ucLower)
370 break;
371
372 RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
373 const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
374 size_t const cchDst = pszDstEnd - &szDst[0];
375 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
376 ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
377 ucLower, cchSrc, ucUpper, cchDst));
378 cch += cchDst;
379 strUpper2.appendCodePoint(ucUpper);
380
381 /* roundtrip stability */
382 RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
383 RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
384
385 RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
386 RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
387 RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
388
389 pszDstEnd = RTStrPutCp(szDst, ucLower2);
390 size_t const cchLower2 = pszDstEnd - &szDst[0];
391 RTTESTI_CHECK_MSG(cchDst == cchLower2,
392 ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
393 ucLower2, cchLower2, ucUpper, cchDst));
394 }
395 RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
396 RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
397
398 /* the toUpper method shall do the same thing. */
399 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
400 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
401
402 /* Ditto for the upper case string. */
403 cch = 0;
404 pszCur = strUpper.c_str();
405 iprt::MiniString strLower2("");
406 strLower2.reserve(strUpper.length() + 64);
407 for (;;)
408 {
409 RTUNICP ucUpper;
410 const char * const pszPrev = pszCur;
411 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
412 size_t const cchSrc = pszCur - pszPrev;
413 if (!ucUpper)
414 break;
415
416 RTUNICP const ucLower = RTUniCpToLower(ucUpper);
417 const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
418 size_t const cchDst = pszDstEnd - &szDst[0];
419 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
420 ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
421 ucUpper, cchSrc, ucLower, cchDst));
422
423 cch += cchDst;
424 strLower2.appendCodePoint(ucLower);
425
426 /* roundtrip stability */
427 RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
428 RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
429
430 RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
431 RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
432 RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
433
434 pszDstEnd = RTStrPutCp(szDst, ucUpper2);
435 size_t const cchUpper2 = pszDstEnd - &szDst[0];
436 RTTESTI_CHECK_MSG(cchDst == cchUpper2,
437 ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
438 ucUpper2, cchUpper2, ucLower, cchDst));
439 }
440 RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
441 RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
442
443 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
444 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
445
446 /* Checks of folding stability when nothing shall change. */
447 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
448 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
449 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
450 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
451
452 strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
453 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
454 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
455 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
456
457 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
458 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
459 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
460 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
461
462 strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
463 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
464 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
465 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
466
467 /* Check folding stability for roundtrips. */
468 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
469 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
470 strTmp.toUpper();
471 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
472 strTmp.toUpper();
473 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
474
475 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
476 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
477 strTmp.toLower();
478 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
479 strTmp.toLower();
480 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
481}
482
483
484int main()
485{
486 RTTEST hTest;
487 RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
488 if (rcExit == RTEXITCODE_SUCCESS)
489 {
490 RTTestBanner(hTest);
491
492 test1(hTest);
493 test2(hTest);
494
495 rcExit = RTTestSummaryAndDestroy(hTest);
496 }
497 return rcExit;
498}
499
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