VirtualBox

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

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

iprt/cpp/ministring.h: Changed the substring constructors to match std::string. As I feared, this caused some minor issues with the format+va_list constructor, fortunately it's only when passing a_cchSrc=0. While at it, I've added the one missing std::string constructor, the repeat character constructor.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.6 KB
Line 
1/* $Id: tstIprtMiniString.cpp 33862 2010-11-08 17:07:49Z 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
214 /* special constructor and assignment arguments */
215 iprt::MiniString StrCtor1("");
216 RTTESTI_CHECK(StrCtor1.isEmpty());
217 RTTESTI_CHECK(StrCtor1.length() == 0);
218
219 iprt::MiniString StrCtor2(NULL);
220 RTTESTI_CHECK(StrCtor2.isEmpty());
221 RTTESTI_CHECK(StrCtor2.length() == 0);
222
223 iprt::MiniString StrCtor1d(StrCtor1);
224 RTTESTI_CHECK(StrCtor1d.isEmpty());
225 RTTESTI_CHECK(StrCtor1d.length() == 0);
226
227 iprt::MiniString StrCtor2d(StrCtor2);
228 RTTESTI_CHECK(StrCtor2d.isEmpty());
229 RTTESTI_CHECK(StrCtor2d.length() == 0);
230
231 for (unsigned i = 0; i < 2; i++)
232 {
233 iprt::MiniString StrAssign;
234 if (i) StrAssign = "abcdef";
235 StrAssign = (char *)NULL;
236 RTTESTI_CHECK(StrAssign.isEmpty());
237 RTTESTI_CHECK(StrAssign.length() == 0);
238
239 if (i) StrAssign = "abcdef";
240 StrAssign = "";
241 RTTESTI_CHECK(StrAssign.isEmpty());
242 RTTESTI_CHECK(StrAssign.length() == 0);
243
244 if (i) StrAssign = "abcdef";
245 StrAssign = StrCtor1;
246 RTTESTI_CHECK(StrAssign.isEmpty());
247 RTTESTI_CHECK(StrAssign.length() == 0);
248
249 if (i) StrAssign = "abcdef";
250 StrAssign = StrCtor2;
251 RTTESTI_CHECK(StrAssign.isEmpty());
252 RTTESTI_CHECK(StrAssign.length() == 0);
253 }
254
255#undef CHECK
256#undef CHECK_DUMP
257#undef CHECK_DUMP_I
258#undef CHECK_EQUAL
259}
260
261
262static int mymemcmp(const char *psz1, const char *psz2, size_t cch)
263{
264 for (size_t off = 0; off < cch; off++)
265 if (psz1[off] != psz2[off])
266 {
267 RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
268 RT_MIN(cch - off, 8), &psz1[off],
269 RT_MIN(cch - off, 8), &psz2[off]);
270 return psz1[off] > psz2[off] ? 1 : -1;
271 }
272 return 0;
273}
274
275static void test2(RTTEST hTest)
276{
277 RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
278
279#define CHECK_EQUAL(str1, str2) \
280 do \
281 { \
282 RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
283 RTTESTI_CHECK((str1).length() == (str2).length()); \
284 RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
285 } while (0)
286
287 iprt::MiniString strTmp;
288 char szDst[16];
289
290 /* Collect all upper and lower case code points. */
291 iprt::MiniString strLower("");
292 strLower.reserve(_4M);
293
294 iprt::MiniString strUpper("");
295 strUpper.reserve(_4M);
296
297 for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
298 {
299 if (RTUniCpIsLower(uc))
300 {
301 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
302 strLower.appendCodePoint(uc);
303 }
304 if (RTUniCpIsUpper(uc))
305 {
306 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
307 strUpper.appendCodePoint(uc);
308 }
309 }
310 RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
311 RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
312
313 /* Fold each code point in the lower case string and check that it encodes
314 into the same or less number of bytes. */
315 size_t cch = 0;
316 const char *pszCur = strLower.c_str();
317 iprt::MiniString strUpper2("");
318 strUpper2.reserve(strLower.length() + 64);
319 for (;;)
320 {
321 RTUNICP ucLower;
322 const char * const pszPrev = pszCur;
323 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
324 size_t const cchSrc = pszCur - pszPrev;
325 if (!ucLower)
326 break;
327
328 RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
329 const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
330 size_t const cchDst = pszDstEnd - &szDst[0];
331 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
332 ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
333 ucLower, cchSrc, ucUpper, cchDst));
334 cch += cchDst;
335 strUpper2.appendCodePoint(ucUpper);
336
337 /* roundtrip stability */
338 RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
339 RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
340
341 RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
342 RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
343 RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
344
345 pszDstEnd = RTStrPutCp(szDst, ucLower2);
346 size_t const cchLower2 = pszDstEnd - &szDst[0];
347 RTTESTI_CHECK_MSG(cchDst == cchLower2,
348 ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
349 ucLower2, cchLower2, ucUpper, cchDst));
350 }
351 RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
352 RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
353
354 /* the toUpper method shall do the same thing. */
355 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
356 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
357
358 /* Ditto for the upper case string. */
359 cch = 0;
360 pszCur = strUpper.c_str();
361 iprt::MiniString strLower2("");
362 strLower2.reserve(strUpper.length() + 64);
363 for (;;)
364 {
365 RTUNICP ucUpper;
366 const char * const pszPrev = pszCur;
367 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
368 size_t const cchSrc = pszCur - pszPrev;
369 if (!ucUpper)
370 break;
371
372 RTUNICP const ucLower = RTUniCpToLower(ucUpper);
373 const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
374 size_t const cchDst = pszDstEnd - &szDst[0];
375 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
376 ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
377 ucUpper, cchSrc, ucLower, cchDst));
378
379 cch += cchDst;
380 strLower2.appendCodePoint(ucLower);
381
382 /* roundtrip stability */
383 RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
384 RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
385
386 RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
387 RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
388 RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
389
390 pszDstEnd = RTStrPutCp(szDst, ucUpper2);
391 size_t const cchUpper2 = pszDstEnd - &szDst[0];
392 RTTESTI_CHECK_MSG(cchDst == cchUpper2,
393 ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
394 ucUpper2, cchUpper2, ucLower, cchDst));
395 }
396 RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
397 RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
398
399 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
400 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
401
402 /* Checks of folding stability when nothing shall change. */
403 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
404 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
405 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
406 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
407
408 strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
409 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
410 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
411 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
412
413 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
414 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
415 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
416 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
417
418 strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
419 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
420 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
421 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
422
423 /* Check folding stability for roundtrips. */
424 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
425 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
426 strTmp.toUpper();
427 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
428 strTmp.toUpper();
429 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
430
431 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
432 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
433 strTmp.toLower();
434 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
435 strTmp.toLower();
436 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
437}
438
439
440int main()
441{
442 RTTEST hTest;
443 RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
444 if (rcExit == RTEXITCODE_SUCCESS)
445 {
446 RTTestBanner(hTest);
447
448 test1(hTest);
449 test2(hTest);
450
451 rcExit = RTTestSummaryAndDestroy(hTest);
452 }
453 return rcExit;
454}
455
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