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