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