VirtualBox

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

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

iprt/cpp/ministring: Added printf and printfV methods.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1/* $Id: tstIprtMiniString.cpp 33605 2010-10-29 13:13:58Z 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
39
40static void test1(RTTEST hTest)
41{
42 RTTestSub(hTest, "Basics");
43
44#define CHECK(expr) RTTESTI_CHECK(expr)
45#define CHECK_DUMP(expr, value) \
46 do { \
47 if (!(expr)) \
48 RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
49 } while (0)
50
51#define CHECK_DUMP_I(expr) \
52 do { \
53 if (!(expr)) \
54 RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
55 } while (0)
56
57 iprt::MiniString empty;
58 CHECK(empty.length() == 0);
59 CHECK(empty.capacity() == 0);
60
61 iprt::MiniString sixbytes("12345");
62 CHECK(sixbytes.length() == 5);
63 CHECK(sixbytes.capacity() == 6);
64
65 sixbytes.append(iprt::MiniString("678"));
66 CHECK(sixbytes.length() == 8);
67 CHECK(sixbytes.capacity() >= 9);
68
69 sixbytes.append("9a");
70 CHECK(sixbytes.length() == 10);
71 CHECK(sixbytes.capacity() >= 11);
72
73 char *psz = sixbytes.mutableRaw();
74 // 123456789a
75 // ^
76 // 0123456
77 psz[6] = '\0';
78 sixbytes.jolt();
79 CHECK(sixbytes.length() == 6);
80 CHECK(sixbytes.capacity() == 7);
81
82 iprt::MiniString morebytes("tobereplaced");
83 morebytes = "newstring ";
84 morebytes.append(sixbytes);
85
86 CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
87
88 iprt::MiniString third(morebytes);
89 third.reserve(100 * 1024); // 100 KB
90 CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
91 CHECK(third.capacity() == 100 * 1024);
92 CHECK(third.length() == morebytes.length()); // must not have changed
93
94 iprt::MiniString copy1(morebytes);
95 iprt::MiniString copy2 = morebytes;
96 CHECK(copy1 == copy2);
97
98 copy1 = NULL;
99 CHECK(copy1.length() == 0);
100
101 copy1 = "";
102 CHECK(copy1.length() == 0);
103
104 CHECK(iprt::MiniString("abc") < iprt::MiniString("def"));
105 CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
106 CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
107 CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
108
109 CHECK(iprt::MiniString("abc") < "def");
110 CHECK(iprt::MiniString("abc") != "def");
111 CHECK_DUMP_I(iprt::MiniString("def") > "abc");
112 CHECK(iprt::MiniString("abc") == "abc");
113
114 CHECK(iprt::MiniString("abc").equals("abc"));
115 CHECK(!iprt::MiniString("abc").equals("def"));
116 CHECK(iprt::MiniString("abc").equalsIgnoreCase("Abc"));
117 CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABc"));
118 CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABC"));
119 CHECK(!iprt::MiniString("abc").equalsIgnoreCase("dBC"));
120
121 copy2.setNull();
122 for (int i = 0; i < 100; ++i)
123 {
124 copy2.reserve(50); // should be ignored after 50 loops
125 copy2.append("1");
126 }
127 CHECK(copy2.length() == 100);
128
129 copy2.setNull();
130 for (int i = 0; i < 100; ++i)
131 {
132 copy2.reserve(50); // should be ignored after 50 loops
133 copy2.append('1');
134 }
135 CHECK(copy2.length() == 100);
136
137 /* printf */
138 iprt::MiniString StrFmt;
139 CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
140
141#undef CHECK
142#undef CHECK_DUMP
143#undef CHECK_DUMP_I
144}
145
146
147static int mymemcmp(const char *psz1, const char *psz2, size_t cch)
148{
149 for (size_t off = 0; off < cch; off++)
150 if (psz1[off] != psz2[off])
151 {
152 RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
153 RT_MIN(cch - off, 8), &psz1[off],
154 RT_MIN(cch - off, 8), &psz2[off]);
155 return psz1[off] > psz2[off] ? 1 : -1;
156 }
157 return 0;
158}
159
160static void test2(RTTEST hTest)
161{
162 RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
163
164#define CHECK_EQUAL(str1, str2) \
165 do \
166 { \
167 RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
168 RTTESTI_CHECK((str1).length() == (str2).length()); \
169 RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
170 } while (0)
171
172 iprt::MiniString strTmp;
173 char szDst[16];
174
175 /* Collect all upper and lower case code points. */
176 iprt::MiniString strLower("");
177 strLower.reserve(_4M);
178
179 iprt::MiniString strUpper("");
180 strUpper.reserve(_4M);
181
182 for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
183 {
184 if (RTUniCpIsLower(uc))
185 {
186 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
187 strLower.appendCodePoint(uc);
188 }
189 if (RTUniCpIsUpper(uc))
190 {
191 RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
192 strUpper.appendCodePoint(uc);
193 }
194 }
195 RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
196 RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
197
198 /* Fold each code point in the lower case string and check that it encodes
199 into the same or less number of bytes. */
200 size_t cch = 0;
201 const char *pszCur = strLower.c_str();
202 iprt::MiniString strUpper2("");
203 strUpper2.reserve(strLower.length() + 64);
204 for (;;)
205 {
206 RTUNICP ucLower;
207 const char * const pszPrev = pszCur;
208 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
209 size_t const cchSrc = pszCur - pszPrev;
210 if (!ucLower)
211 break;
212
213 RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
214 const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
215 size_t const cchDst = pszDstEnd - &szDst[0];
216 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
217 ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
218 ucLower, cchSrc, ucUpper, cchDst));
219 cch += cchDst;
220 strUpper2.appendCodePoint(ucUpper);
221
222 /* roundtrip stability */
223 RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
224 RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
225
226 RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
227 RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
228 RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
229
230 pszDstEnd = RTStrPutCp(szDst, ucLower2);
231 size_t const cchLower2 = pszDstEnd - &szDst[0];
232 RTTESTI_CHECK_MSG(cchDst == cchLower2,
233 ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
234 ucLower2, cchLower2, ucUpper, cchDst));
235 }
236 RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
237 RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
238
239 /* the toUpper method shall do the same thing. */
240 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
241 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
242
243 /* Ditto for the upper case string. */
244 cch = 0;
245 pszCur = strUpper.c_str();
246 iprt::MiniString strLower2("");
247 strLower2.reserve(strUpper.length() + 64);
248 for (;;)
249 {
250 RTUNICP ucUpper;
251 const char * const pszPrev = pszCur;
252 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
253 size_t const cchSrc = pszCur - pszPrev;
254 if (!ucUpper)
255 break;
256
257 RTUNICP const ucLower = RTUniCpToLower(ucUpper);
258 const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
259 size_t const cchDst = pszDstEnd - &szDst[0];
260 RTTESTI_CHECK_MSG(cchSrc >= cchDst,
261 ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
262 ucUpper, cchSrc, ucLower, cchDst));
263
264 cch += cchDst;
265 strLower2.appendCodePoint(ucLower);
266
267 /* roundtrip stability */
268 RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
269 RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
270
271 RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
272 RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
273 RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
274
275 pszDstEnd = RTStrPutCp(szDst, ucUpper2);
276 size_t const cchUpper2 = pszDstEnd - &szDst[0];
277 RTTESTI_CHECK_MSG(cchDst == cchUpper2,
278 ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
279 ucUpper2, cchUpper2, ucLower, cchDst));
280 }
281 RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
282 RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
283
284 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
285 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
286
287 /* Checks of folding stability when nothing shall change. */
288 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
289 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
290 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
291 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
292
293 strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
294 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
295 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
296 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
297
298 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
299 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
300 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
301 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
302
303 strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
304 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
305 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
306 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
307
308 /* Check folding stability for roundtrips. */
309 strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
310 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
311 strTmp.toUpper();
312 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
313 strTmp.toUpper();
314 strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
315
316 strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
317 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
318 strTmp.toLower();
319 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
320 strTmp.toLower();
321 strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
322}
323
324
325int main()
326{
327 RTTEST hTest;
328 RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
329 if (rcExit == RTEXITCODE_SUCCESS)
330 {
331 RTTestBanner(hTest);
332
333 test1(hTest);
334 test2(hTest);
335
336 rcExit = RTTestSummaryAndDestroy(hTest);
337 }
338 return rcExit;
339}
340
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