VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrFormat.cpp@ 107817

Last change on this file since 107817 was 107805, checked in by vboxsync, 4 weeks ago

Runtime/testcase/tstRTStrFormat.cpp: Improve testcase, check that the return value of RTStrPrintf() matches the expectations, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 42.4 KB
Line 
1/* $Id: tstRTStrFormat.cpp 107805 2025-01-16 09:46:15Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String formatting.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/string.h>
42#include <iprt/utf16.h>
43
44#include <iprt/initterm.h>
45#include <iprt/net.h>
46#include <iprt/stream.h>
47#include <iprt/test.h>
48#include <iprt/uuid.h>
49
50
51/** See FNRTSTRFORMATTYPE. */
52static DECLCALLBACK(size_t) TstType(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
53 const char *pszType, void const *pvValue,
54 int cchWidth, int cchPrecision, unsigned fFlags,
55 void *pvUser)
56{
57 /* validate */
58 if (strncmp(pszType, "type", 4))
59 RTTestIFailed("pszType=%s expected 'typeN'\n", pszType);
60
61 int iType = pszType[4] - '0';
62 if ((uintptr_t)pvUser != (uintptr_t)TstType + iType)
63 RTTestIFailed("pvValue=%p expected %p\n", pvUser, (void *)((uintptr_t)TstType + iType));
64
65 /* format */
66 size_t cch = pfnOutput(pvArgOutput, pszType, 5);
67 cch += pfnOutput(pvArgOutput, "=", 1);
68 char szNum[64];
69 size_t cchNum = RTStrFormatNumber(szNum, (uintptr_t)pvValue, 10, cchWidth, cchPrecision, fFlags);
70 cch += pfnOutput(pvArgOutput, szNum, cchNum);
71 return cch;
72}
73
74
75static void testNested(int iLine, const char *pszExpect, const char *pszFormat, ...)
76{
77 size_t cchExpect = strlen(pszExpect);
78 char szBuf[512];
79
80 va_list va;
81 va_start(va, pszFormat);
82 size_t cch = RTStrPrintf(szBuf, sizeof(szBuf), "%N", pszFormat, &va);
83 va_end(va);
84 if (strcmp(szBuf, pszExpect))
85 RTTestIFailed("at line %d: nested format '%s'\n"
86 " output: '%s'\n"
87 " wanted: '%s'\n",
88 iLine, pszFormat, szBuf, pszExpect);
89 else if (cch != cchExpect)
90 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
91 iLine, cch, cchExpect);
92
93 va_start(va, pszFormat);
94 cch = RTStrPrintf(szBuf, sizeof(szBuf), "%uxxx%Nyyy%u", 43, pszFormat, &va, 43);
95 va_end(va);
96 if ( strncmp(szBuf, "43xxx", 5)
97 || strncmp(szBuf + 5, pszExpect, cchExpect)
98 || strcmp( szBuf + 5 + cchExpect, "yyy43") )
99 RTTestIFailed("at line %d: nested format '%s'\n"
100 " output: '%s'\n"
101 " wanted: '43xxx%syyy43'\n",
102 iLine, pszFormat, szBuf, pszExpect);
103 else if (cch != 5 + cchExpect + 5)
104 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
105 iLine, cch, 5 + cchExpect + 5);
106}
107
108
109static void testUtf16Printf(RTTEST hTest)
110{
111 RTTestSub(hTest, "RTUtf16Printf");
112 size_t const cwcBuf = 120;
113 PRTUTF16 const pwszBuf = (PRTUTF16)RTTestGuardedAllocTail(hTest, cwcBuf * sizeof(RTUTF16));
114
115 static const char s_szSimpleExpect[] = "Hello world!";
116 static const ssize_t s_cwcSimpleExpect = sizeof(s_szSimpleExpect) - 1;
117 ssize_t cwc = RTUtf16Printf(pwszBuf, cwcBuf, "Hello%c%s!", ' ', "world");
118 if (RTUtf16CmpAscii(pwszBuf, s_szSimpleExpect))
119 RTTestIFailed("error: '%ls'\n"
120 "wanted '%s'\n", pwszBuf, s_szSimpleExpect);
121 if (cwc != s_cwcSimpleExpect)
122 RTTestIFailed("error: got %zd, expected %zd (#1)\n", cwc, s_cwcSimpleExpect);
123
124 RTTestDisableAssertions(hTest);
125 for (size_t cwcThisBuf = 0; cwcThisBuf < sizeof(s_szSimpleExpect) + 8; cwcThisBuf++)
126 {
127 memset(pwszBuf, 0x88, cwcBuf * sizeof(*pwszBuf));
128
129 PRTUTF16 pwszThisBuf = &pwszBuf[cwcBuf - cwcThisBuf];
130 cwc = RTUtf16Printf(pwszThisBuf, cwcThisBuf, "Hello%c%s!", ' ', "world");
131
132 if (cwcThisBuf <= (size_t)s_cwcSimpleExpect)
133 {
134 if (cwcThisBuf > 1)
135 {
136 if (RTUtf16NCmpAscii(pwszThisBuf, s_szSimpleExpect, cwcThisBuf - 1))
137 RTTestIFailed("error: '%.*ls'\n"
138 "wanted '%.*s'\n", cwcThisBuf - 1, pwszThisBuf, cwcThisBuf - 1, s_szSimpleExpect);
139 }
140 if (cwcThisBuf > 1 && pwszThisBuf[cwcThisBuf - 1] != '\0')
141 RTTestIFailed("error: cwcThisBuf=%zu not null terminated! %#x\n", cwcThisBuf, pwszThisBuf[cwcThisBuf - 1]);
142 if (cwc != -s_cwcSimpleExpect - 1)
143 RTTestIFailed("error: cwcThisBuf=%zu got %zd, expected %zd (#1)\n", cwcThisBuf, cwc, -s_cwcSimpleExpect - 1);
144 }
145 else
146 {
147 if (RTUtf16CmpAscii(pwszThisBuf, s_szSimpleExpect))
148 RTTestIFailed("error: '%ls'\n"
149 "wanted '%s'\n", pwszThisBuf, s_szSimpleExpect);
150 if (cwc != s_cwcSimpleExpect)
151 RTTestIFailed("error: cwcThisBuf=%zu got %zd, expected %zd (#1)\n", cwcThisBuf, cwc, s_cwcSimpleExpect);
152 }
153 }
154 RTTestRestoreAssertions(hTest);
155}
156
157
158
159static void testAllocPrintf(RTTEST hTest)
160{
161 RTTestSub(hTest, "RTStrAPrintf");
162 char *psz = (char *)~0;
163 int cch3 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!");
164 if (cch3 < 0)
165 RTTestIFailed("RTStrAPrintf failed, cch3=%d\n", cch3);
166 else if (strcmp(psz, "Hey there! This is a test!"))
167 RTTestIFailed("RTStrAPrintf failed\n"
168 "got : '%s'\n"
169 "wanted: 'Hey there! This is a test!'\n",
170 psz);
171 else if ((int)strlen(psz) != cch3)
172 RTTestIFailed("RTStrAPrintf failed, cch3 == %d expected %u\n", cch3, strlen(psz));
173 RTStrFree(psz);
174}
175
176
177/*
178 * This next portion used to all be in main() but gcc cannot handle
179 * that in asan + -O2 mode.
180 */
181
182
183
184#define BUF_SIZE 120
185
186/* This used to be very simple, but is not doing overflow handling checks and two APIs. */
187#define CHECK42(fmt, arg, out) \
188 do { \
189 static const char g_szCheck42Fmt[] = fmt " 42=%d " fmt " 42=%d" ; \
190 static const char g_szCheck42Expect[] = out " 42=42 " out " 42=42" ; \
191 \
192 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, g_szCheck42Fmt, arg, 42, arg, 42); \
193 if (memcmp(pszBuf, g_szCheck42Expect, sizeof(g_szCheck42Expect)) != 0) \
194 RTTestIFailed("at line %d: format '%s'\n" \
195 " output: '%s'\n" \
196 " wanted: '%s'\n", \
197 __LINE__, fmt, pszBuf, g_szCheck42Expect); \
198 else if (cch != sizeof(g_szCheck42Expect) - 1) \
199 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \
200 __LINE__, cch, sizeof(g_szCheck42Expect) - 1); \
201 \
202 RTTestIDisableAssertions(); \
203 for (size_t cbBuf = 0; cbBuf <= BUF_SIZE; cbBuf++) \
204 { \
205 memset(pszBuf, 0xcc, BUF_SIZE); \
206 const char chAfter = cbBuf != 0 ? '\0' : 0xcc; \
207 const size_t cchCompare = cbBuf >= sizeof(g_szCheck42Expect) ? sizeof(g_szCheck42Expect) - 1 \
208 : cbBuf > 0 ? cbBuf - 1 : 0; \
209 size_t cch1Expect = cchCompare; \
210 ssize_t cch2Expect = cbBuf >= sizeof(g_szCheck42Expect) \
211 ? sizeof(g_szCheck42Expect) - 1 : -(ssize_t)sizeof(g_szCheck42Expect); \
212 \
213 cch = RTStrPrintf(pszBuf, cbBuf, g_szCheck42Fmt, arg, 42, arg, 42);\
214 if ( memcmp(pszBuf, g_szCheck42Expect, cchCompare) != 0 \
215 || pszBuf[cchCompare] != chAfter) \
216 RTTestIFailed("at line %d: format '%s' (#1, cbBuf=%zu)\n" \
217 " output: '%s'\n" \
218 " wanted: '%s'\n", \
219 __LINE__, fmt, cbBuf, cbBuf ? pszBuf : "", g_szCheck42Expect); \
220 if (cch != cch1Expect) \
221 RTTestIFailed("at line %d: Invalid length %d returned for cbBuf=%zu, expected %zd! (#1)\n", \
222 __LINE__, cch, cbBuf, cch1Expect); \
223 \
224 ssize_t cch2 = RTStrPrintf2(pszBuf, cbBuf, g_szCheck42Fmt, arg, 42, arg, 42);\
225 if ( memcmp(pszBuf, g_szCheck42Expect, cchCompare) != 0 \
226 || pszBuf[cchCompare] != chAfter) \
227 RTTestIFailed("at line %d: format '%s' (#2, cbBuf=%zu)\n" \
228 " output: '%s'\n" \
229 " wanted: '%s'\n", \
230 __LINE__, fmt, cbBuf, cbBuf ? pszBuf : "", g_szCheck42Expect); \
231 if (cch2 != cch2Expect) \
232 RTTestIFailed("at line %d: Invalid length %d returned for cbBuf=%zu, expected %zd! (#2)\n", \
233 __LINE__, cch2, cbBuf, cch2Expect); \
234 } \
235 RTTestIRestoreAssertions(); \
236 } while (0)
237
238#define CHECKSTR(Correct) \
239 if (strcmp(pszBuf, Correct)) \
240 RTTestIFailed("error: '%s'\n" \
241 "expected: '%s'\n", pszBuf, Correct);
242
243#define CHECKSTRCCH(a_pszCorrect) \
244 do { \
245 size_t const cchCorrect = strlen(a_pszCorrect); \
246 if (cch != cchCorrect) \
247 RTTestIFailed("cch error: %zu\n" \
248 "cch expected: %zu\n", cch, cchCorrect); \
249 if (strcmp(pszBuf, a_pszCorrect)) \
250 RTTestIFailed("error: '%s'\n" \
251 "expected: '%s'\n", pszBuf, a_pszCorrect); \
252 } while (0)
253
254static void testBasics(RTTEST hTest, char *pszBuf)
255{
256 RTTestSub(hTest, "Basics");
257
258 uint32_t u32 = 0x010;
259 uint64_t u64 = 0x100;
260
261 /* simple */
262 static const char s_szSimpleExpect[] = "u32=16 u64=256 u64=0x100";
263 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
264 if (strcmp(pszBuf, s_szSimpleExpect))
265 RTTestIFailed("error: '%s'\n"
266 "wanted '%s'\n", pszBuf, s_szSimpleExpect);
267 else if (cch != sizeof(s_szSimpleExpect) - 1)
268 RTTestIFailed("error: got %zd, expected %zd (#1)\n", cch, sizeof(s_szSimpleExpect) - 1);
269
270 ssize_t cch2 = RTStrPrintf2(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
271 if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100"))
272 RTTestIFailed("error: '%s' (#2)\n"
273 "wanted '%s' (#2)\n", pszBuf, s_szSimpleExpect);
274 else if (cch2 != sizeof(s_szSimpleExpect) - 1)
275 RTTestIFailed("error: got %zd, expected %zd (#2)\n", cch2, sizeof(s_szSimpleExpect) - 1);
276
277 /* just big. */
278 u64 = UINT64_C(0x7070605040302010);
279 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
280 if (strcmp(pszBuf, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
281 {
282 RTTestIFailed("error: '%s'\n"
283 "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf);
284 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
285 }
286
287 /* huge and negative. */
288 u64 = UINT64_C(0x8070605040302010);
289 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
290 /* Not sure if this is the correct decimal representation... But both */
291 if (strcmp(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
292 {
293 RTTestIFailed("error: '%s'\n"
294 "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf);
295 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
296 }
297
298 /* 64-bit value bug. */
299 u64 = 0xa0000000;
300 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
301 if (strcmp(pszBuf, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
302 RTTestIFailed("error: '%s'\n"
303 "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf);
304
305 /* uuid */
306 RTUUID Uuid;
307 RTUuidCreate(&Uuid);
308 char szCorrect[RTUUID_STR_LENGTH];
309 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
310 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
311 if (strcmp(pszBuf, szCorrect))
312 RTTestIFailed("error: '%s'\n"
313 "expected: '%s'\n",
314 pszBuf, szCorrect);
315
316 /* overflow */
317 size_t const cbOverflowBuf = 40;
318 static char const s_szOverflowExpect[] = "i=8 ";
319 char *pszBuf3 = (char *)RTTestGuardedAllocTail(hTest, cbOverflowBuf);
320 cch = RTStrPrintf(pszBuf3, cbOverflowBuf, "i=%d%*s\n", (int)8, cbOverflowBuf, "");
321 if (strcmp(pszBuf3, s_szOverflowExpect))
322 RTTestIFailed("error: '%s' (#2)\n"
323 "wanted '%s' (#2)\n", pszBuf, s_szOverflowExpect);
324 else if (cch != sizeof(s_szOverflowExpect) - 1)
325 RTTestIFailed("error: got %zd, expected %zd (#2)\n", cch2, sizeof(s_szOverflowExpect) - 1);
326 RTTestGuardedFree(hTest, pszBuf3);
327}
328
329
330static void testRuntimeExtensions(RTTEST hTest, char *pszBuf)
331{
332 RTTestSub(hTest, "Runtime format types (%R*)");
333 CHECK42("%RGi", (RTGCINT)127, "127");
334 CHECK42("%RGi", (RTGCINT)-586589, "-586589");
335
336 CHECK42("%RGp", (RTGCPHYS)0x0000000044505045, "0000000044505045");
337 CHECK42("%RGp", ~(RTGCPHYS)0, "ffffffffffffffff");
338
339 CHECK42("%RGu", (RTGCUINT)586589, "586589");
340 CHECK42("%RGu", (RTGCUINT)1, "1");
341 CHECK42("%RGu", (RTGCUINT)3000000000U, "3000000000");
342
343#if GC_ARCH_BITS == 32
344 CHECK42("%RGv", (RTGCUINTPTR)0, "00000000");
345 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffff");
346 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "84342134");
347#else
348 CHECK42("%RGv", (RTGCUINTPTR)0, "0000000000000000");
349 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffffffffffff");
350 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "0000000084342134");
351#endif
352
353 CHECK42("%RGx", (RTGCUINT)0x234, "234");
354 CHECK42("%RGx", (RTGCUINT)0xffffffff, "ffffffff");
355
356 CHECK42("%RRv", (RTRCUINTPTR)0, "00000000");
357 CHECK42("%RRv", ~(RTRCUINTPTR)0, "ffffffff");
358 CHECK42("%RRv", (RTRCUINTPTR)0x84342134, "84342134");
359
360 CHECK42("%RHi", (RTHCINT)127, "127");
361 CHECK42("%RHi", (RTHCINT)-586589, "-586589");
362
363 CHECK42("%RHp", (RTHCPHYS)0x0000000044505045, "0000000044505045");
364 CHECK42("%RHp", ~(RTHCPHYS)0, "ffffffffffffffff");
365
366 CHECK42("%RHu", (RTHCUINT)586589, "586589");
367 CHECK42("%RHu", (RTHCUINT)1, "1");
368 CHECK42("%RHu", (RTHCUINT)3000000000U, "3000000000");
369
370 if (sizeof(void*) == 8)
371 {
372 CHECK42("%RHv", (RTHCUINTPTR)0, "0000000000000000");
373 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffffffffffff");
374 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "0000000084342134");
375 }
376 else
377 {
378 CHECK42("%RHv", (RTHCUINTPTR)0, "00000000");
379 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffff");
380 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "84342134");
381 }
382
383 CHECK42("%RHx", (RTHCUINT)0x234, "234");
384 CHECK42("%RHx", (RTHCUINT)0xffffffff, "ffffffff");
385
386 CHECK42("%RI16", (int16_t)1, "1");
387 CHECK42("%RI16", (int16_t)-16384, "-16384");
388 CHECK42("%RI16", INT16_MAX, "32767");
389 CHECK42("%RI16", INT16_MIN, "-32768");
390
391 CHECK42("%RI32", (int32_t)1123, "1123");
392 CHECK42("%RI32", (int32_t)-86596, "-86596");
393 CHECK42("%RI32", INT32_MAX, "2147483647");
394 CHECK42("%RI32", INT32_MIN, "-2147483648");
395 CHECK42("%RI32", INT32_MIN+1, "-2147483647");
396 CHECK42("%RI32", INT32_MIN+2, "-2147483646");
397
398 CHECK42("%RI64", (int64_t)112345987345LL, "112345987345");
399 CHECK42("%RI64", (int64_t)-8659643985723459LL, "-8659643985723459");
400 CHECK42("%RI64", INT64_MAX, "9223372036854775807");
401 CHECK42("%RI64", INT64_MIN, "-9223372036854775808");
402 CHECK42("%RI64", INT64_MIN+1, "-9223372036854775807");
403 CHECK42("%RI64", INT64_MIN+2, "-9223372036854775806");
404
405 CHECK42("%RI8", (int8_t)1, "1");
406 CHECK42("%RI8", (int8_t)-128, "-128");
407
408 CHECK42("%Rbn", "file.c", "file.c");
409 CHECK42("%Rbn", "foo/file.c", "file.c");
410 CHECK42("%Rbn", "/foo/file.c", "file.c");
411 CHECK42("%Rbn", "/dir/subdir/", "subdir/");
412
413 CHECK42("%Rfn", "function", "function");
414 CHECK42("%Rfn", "void function(void)", "function");
415
416 CHECK42("%RTfile", (RTFILE)127, "127");
417 CHECK42("%RTfile", (RTFILE)12341234, "12341234");
418
419 CHECK42("%RTfmode", (RTFMODE)0x123403, "00123403");
420
421 CHECK42("%RTfoff", (RTFOFF)12342312, "12342312");
422 CHECK42("%RTfoff", (RTFOFF)-123123123, "-123123123");
423 CHECK42("%RTfoff", (RTFOFF)858694596874568LL, "858694596874568");
424
425 RTFAR16 fp16;
426 fp16.off = 0x34ff;
427 fp16.sel = 0x0160;
428 CHECK42("%RTfp16", fp16, "0160:34ff");
429
430 RTFAR32 fp32;
431 fp32.off = 0xff094030;
432 fp32.sel = 0x0168;
433 CHECK42("%RTfp32", fp32, "0168:ff094030");
434
435 RTFAR64 fp64;
436 fp64.off = 0xffff003401293487ULL;
437 fp64.sel = 0x0ff8;
438 CHECK42("%RTfp64", fp64, "0ff8:ffff003401293487");
439 fp64.off = 0x0;
440 fp64.sel = 0x0;
441 CHECK42("%RTfp64", fp64, "0000:0000000000000000");
442
443 CHECK42("%RTgid", (RTGID)-1, "-1");
444 CHECK42("%RTgid", (RTGID)1004, "1004");
445
446 CHECK42("%RTino", (RTINODE)0, "0000000000000000");
447 CHECK42("%RTino", (RTINODE)0x123412341324ULL, "0000123412341324");
448
449 CHECK42("%RTint", (RTINT)127, "127");
450 CHECK42("%RTint", (RTINT)-586589, "-586589");
451 CHECK42("%RTint", (RTINT)-23498723, "-23498723");
452
453 CHECK42("%RTiop", (RTIOPORT)0x3c4, "03c4");
454 CHECK42("%RTiop", (RTIOPORT)0xffff, "ffff");
455
456 RTMAC Mac;
457 Mac.au8[0] = 0;
458 Mac.au8[1] = 0x1b;
459 Mac.au8[2] = 0x21;
460 Mac.au8[3] = 0x0a;
461 Mac.au8[4] = 0x1d;
462 Mac.au8[5] = 0xd9;
463 CHECK42("%RTmac", &Mac, "00:1b:21:0a:1d:d9");
464 Mac.au16[0] = 0xffff;
465 Mac.au16[1] = 0xffff;
466 Mac.au16[2] = 0xffff;
467 CHECK42("%RTmac", &Mac, "ff:ff:ff:ff:ff:ff");
468
469 RTNETADDRIPV4 Ipv4Addr;
470 Ipv4Addr.u = RT_H2N_U32_C(0xf040d003);
471 CHECK42("%RTnaipv4", Ipv4Addr.u, "240.64.208.3");
472 Ipv4Addr.u = RT_H2N_U32_C(0xffffffff);
473 CHECK42("%RTnaipv4", Ipv4Addr.u, "255.255.255.255");
474
475 RTNETADDRIPV6 Ipv6Addr;
476
477 /* any */
478 memset(&Ipv6Addr, 0, sizeof(Ipv6Addr));
479 CHECK42("%RTnaipv6", &Ipv6Addr, "::");
480
481 /* loopback */
482 Ipv6Addr.au8[15] = 1;
483 CHECK42("%RTnaipv6", &Ipv6Addr, "::1");
484
485 /* IPv4-compatible */
486 Ipv6Addr.au8[12] = 1;
487 Ipv6Addr.au8[13] = 1;
488 Ipv6Addr.au8[14] = 1;
489 Ipv6Addr.au8[15] = 1;
490 CHECK42("%RTnaipv6", &Ipv6Addr, "::1.1.1.1");
491
492 /* IPv4-mapped */
493 Ipv6Addr.au16[5] = RT_H2N_U16_C(0xffff);
494 CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:1.1.1.1");
495
496 /* IPv4-translated */
497 Ipv6Addr.au16[4] = RT_H2N_U16_C(0xffff);
498 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
499 CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:0:1.1.1.1");
500
501 /* single zero word is not abbreviated, leading zeroes are not printed */
502 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000);
503 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0001);
504 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
505 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001);
506 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
507 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0001);
508 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
509 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
510 CHECK42("%RTnaipv6", &Ipv6Addr, "0:1:0:1:0:1:0:1");
511
512 /* longest run is abbreviated (here: at the beginning) */
513 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000);
514 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000);
515 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
516 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001);
517 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
518 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
519 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0001);
520 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0000);
521 CHECK42("%RTnaipv6", &Ipv6Addr, "::1:0:0:1:0");
522
523 /* longest run is abbreviated (here: first) */
524 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001);
525 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000);
526 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
527 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
528 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001);
529 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
530 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
531 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
532 CHECK42("%RTnaipv6", &Ipv6Addr, "1::1:0:0:1");
533
534 /* longest run is abbreviated (here: second) */
535 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001);
536 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000);
537 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
538 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001);
539 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
540 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
541 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
542 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
543 CHECK42("%RTnaipv6", &Ipv6Addr, "1:0:0:1::1");
544
545 /* longest run is abbreviated (here: at the end) */
546 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001);
547 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000);
548 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
549 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001);
550 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
551 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
552 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
553 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0000);
554 CHECK42("%RTnaipv6", &Ipv6Addr, "1:0:0:1::");
555
556 /* first of the two runs of equal length is abbreviated */
557 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001);
558 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8);
559 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
560 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
561 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001);
562 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
563 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
564 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
565 CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8::1:0:0:1");
566
567 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001);
568 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8);
569 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x85a3);
570 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
571 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
572 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x8a2e);
573 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0370);
574 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x7334);
575 CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8:85a3::8a2e:370:7334");
576
577 Ipv6Addr.au64[0] = UINT64_MAX;
578 Ipv6Addr.au64[1] = UINT64_MAX;
579 CHECK42("%RTnaipv6", &Ipv6Addr, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
580
581 RTNETADDR NetAddr;
582 memset(&NetAddr, 0, sizeof(NetAddr));
583
584 /* plain IPv6 address if port is not specified */
585 NetAddr.enmType = RTNETADDRTYPE_IPV6;
586 NetAddr.uAddr.au16[0] = RT_H2N_U16_C(0x0001);
587 NetAddr.uAddr.au16[7] = RT_H2N_U16_C(0x0001);
588 NetAddr.uPort = RTNETADDR_PORT_NA;
589 CHECK42("%RTnaddr", &NetAddr, "1::1");
590
591 /* square brackets around IPv6 address if port is specified */
592 NetAddr.uPort = 1;
593 CHECK42("%RTnaddr", &NetAddr, "[1::1]:1");
594
595 CHECK42("%RTproc", (RTPROCESS)0xffffff, "00ffffff");
596 CHECK42("%RTproc", (RTPROCESS)0x43455443, "43455443");
597
598#if (HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64)
599 CHECK42("%RTptr", (RTUINTPTR)0, "0000000000000000");
600 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffffffffffff");
601 CHECK42("%RTptr", (RTUINTPTR)(uintptr_t)0x84342134, "0000000084342134");
602#else
603 CHECK42("%RTptr", (RTUINTPTR)0, "00000000");
604 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffff");
605 CHECK42("%RTptr", (RTUINTPTR)(uintptr_t)0x84342134, "84342134");
606#endif
607
608#if ARCH_BITS == 64
609 AssertCompileSize(RTCCUINTREG, 8);
610 CHECK42("%RTreg", (RTCCUINTREG)0, "0000000000000000");
611 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffffffffffff");
612 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "0000000084342134");
613 CHECK42("%RTreg", (RTCCUINTREG)0x23484342134ULL, "0000023484342134");
614#elif ARCH_BITS == 32
615 AssertCompileSize(RTCCUINTREG, 4);
616 CHECK42("%RTreg", (RTCCUINTREG)0, "00000000");
617 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffff");
618 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "84342134");
619#else
620# error ARCH_BITS
621#endif
622
623 CHECK42("%RTsel", (RTSEL)0x543, "0543");
624 CHECK42("%RTsel", (RTSEL)0xf8f8, "f8f8");
625
626#if ARCH_BITS == 64
627 CHECK42("%RTsem", (RTSEMEVENT)0, "0000000000000000");
628 CHECK42("%RTsem", (RTSEMEVENT)(uintptr_t)0x23484342134ULL, "0000023484342134");
629#else
630 CHECK42("%RTsem", (RTSEMEVENT)0, "00000000");
631 CHECK42("%RTsem", (RTSEMEVENT)(uintptr_t)0x84342134, "84342134");
632#endif
633
634 CHECK42("%RTsock", (RTSOCKET)(uintptr_t)12234, "12234");
635 CHECK42("%RTsock", (RTSOCKET)(uintptr_t)584854543, "584854543");
636
637#if ARCH_BITS == 64
638 CHECK42("%RTthrd", (RTTHREAD)0, "0000000000000000");
639 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffffffffffff");
640 CHECK42("%RTthrd", (RTTHREAD)(uintptr_t)0x63484342134ULL, "0000063484342134");
641#else
642 CHECK42("%RTthrd", (RTTHREAD)0, "00000000");
643 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffff");
644 CHECK42("%RTthrd", (RTTHREAD)(uintptr_t)0x54342134, "54342134");
645#endif
646
647 CHECK42("%RTuid", (RTUID)-2, "-2");
648 CHECK42("%RTuid", (RTUID)90344, "90344");
649
650 CHECK42("%RTuint", (RTUINT)584589, "584589");
651 CHECK42("%RTuint", (RTUINT)3, "3");
652 CHECK42("%RTuint", (RTUINT)2400000000U, "2400000000");
653
654 RTUUID Uuid;
655 char szCorrect[RTUUID_STR_LENGTH];
656 RTUuidCreate(&Uuid);
657 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
658 RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
659 if (strcmp(pszBuf, szCorrect))
660 RTTestIFailed("error: '%s'\n"
661 "expected: '%s'\n",
662 pszBuf, szCorrect);
663
664 CHECK42("%RTxint", (RTUINT)0x2345, "2345");
665 CHECK42("%RTxint", (RTUINT)0xffff8fff, "ffff8fff");
666
667 CHECK42("%RU16", (uint16_t)7, "7");
668 CHECK42("%RU16", (uint16_t)46384, "46384");
669
670 CHECK42("%RU32", (uint32_t)1123, "1123");
671 CHECK42("%RU32", (uint32_t)86596, "86596");
672 CHECK42("%4RU32", (uint32_t)42, " 42");
673 CHECK42("%04RU32", (uint32_t)42, "0042");
674 CHECK42("%.4RU32", (uint32_t)42, "0042");
675
676 CHECK42("%RU64", (uint64_t)112345987345ULL, "112345987345");
677 CHECK42("%RU64", (uint64_t)8659643985723459ULL, "8659643985723459");
678 CHECK42("%14RU64", (uint64_t)4, " 4");
679 CHECK42("%014RU64", (uint64_t)4, "00000000000004");
680 CHECK42("%.14RU64", (uint64_t)4, "00000000000004");
681
682 CHECK42("%RU8", (uint8_t)1, "1");
683 CHECK42("%RU8", (uint8_t)254, "254");
684 CHECK42("%RU8", 256, "0");
685
686 CHECK42("%RX16", (uint16_t)0x7, "7");
687 CHECK42("%RX16", 0x46384, "6384");
688 CHECK42("%RX16", UINT16_MAX, "ffff");
689
690 CHECK42("%RX32", (uint32_t)0x1123, "1123");
691 CHECK42("%RX32", (uint32_t)0x49939493, "49939493");
692 CHECK42("%RX32", UINT32_MAX, "ffffffff");
693
694 CHECK42("%RX64", UINT64_C(0x348734), "348734");
695 CHECK42("%RX64", UINT64_C(0x12312312312343f), "12312312312343f");
696 CHECK42("%RX64", UINT64_MAX, "ffffffffffffffff");
697 CHECK42("%5RX64", UINT64_C(0x42), " 42");
698 CHECK42("%05RX64", UINT64_C(0x42), "00042");
699 CHECK42("%.5RX64", UINT64_C(0x42), "00042");
700 CHECK42("%.05RX64", UINT64_C(0x42), "00042"); /* '0' is ignored */
701
702 CHECK42("%RX8", (uint8_t)1, "1");
703 CHECK42("%RX8", (uint8_t)0xff, "ff");
704 CHECK42("%RX8", UINT8_MAX, "ff");
705 CHECK42("%RX8", 0x100, "0");
706}
707
708static void testThousandSeparators(RTTEST hTest, char *pszBuf)
709{
710 RTTestSub(hTest, "Thousand Separators (%'*)");
711
712 RTStrFormatNumber(pszBuf, 1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1"); memset(pszBuf, '!', BUF_SIZE);
713 RTStrFormatNumber(pszBuf, 10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10"); memset(pszBuf, '!', BUF_SIZE);
714 RTStrFormatNumber(pszBuf, 100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100"); memset(pszBuf, '!', BUF_SIZE);
715 RTStrFormatNumber(pszBuf, 1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000"); memset(pszBuf, '!', BUF_SIZE);
716 RTStrFormatNumber(pszBuf, 10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000"); memset(pszBuf, '!', BUF_SIZE);
717 RTStrFormatNumber(pszBuf, 100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000"); memset(pszBuf, '!', BUF_SIZE);
718 RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000"); memset(pszBuf, '!', BUF_SIZE);
719
720 CHECK42("%'u", 1, "1");
721 CHECK42("%'u", 10, "10");
722 CHECK42("%'u", 100, "100");
723 CHECK42("%'u", 1000, "1 000");
724 CHECK42("%'u", 10000, "10 000");
725 CHECK42("%'u", 100000, "100 000");
726 CHECK42("%'u", 1000000, "1 000 000");
727 CHECK42("%'RU64", _1T, "1 099 511 627 776");
728 CHECK42("%'RU64", _1E, "1 152 921 504 606 846 976");
729}
730
731static void testStringFormatter(RTTEST hTest, char *pszBuf)
732{
733 RTTestSub(hTest, "String formatting (%s)");
734
735// 0 1 2 3 4 5 6 7
736// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
737 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "args", "description");
738 CHECKSTRCCH("cmd args description");
739
740 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "", "description");
741 CHECKSTRCCH("cmd description");
742
743
744 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%*s", 0, "");
745 CHECKSTRCCH("");
746
747 /* automatic conversions. */
748 static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
749 static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
750
751 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz1);
752 CHECKSTRCCH("hello world");
753 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz1);
754 CHECKSTRCCH("hello world");
755
756 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5ls", s_wsz1);
757 CHECKSTRCCH("hello");
758 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5Ls", s_usz1);
759 CHECKSTRCCH("hello");
760}
761
762static void testUnicodeStringFormatter(RTTEST hTest, char *pszBuf)
763{
764 RTTestSub(hTest, "Unicode string formatting (%ls)");
765 static RTUTF16 s_wszEmpty[] = { 0 }; //assumes ascii.
766 static RTUTF16 s_wszCmd[] = { 'c', 'm', 'd', 0 }; //assumes ascii.
767 static RTUTF16 s_wszArgs[] = { 'a', 'r', 'g', 's', 0 }; //assumes ascii.
768 static RTUTF16 s_wszDesc[] = { 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 0 }; //assumes ascii.
769
770// 0 1 2 3 4 5 6 7
771// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
772 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc);
773 CHECKSTRCCH("cmd args description");
774
775 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc);
776 CHECKSTRCCH("cmd description");
777
778
779#if 0
780 static RTUNICP s_usz2[] = { 0xc5, 0xc6, 0xf8, 0 };
781 static RTUTF16 s_wsz2[] = { 0xc5, 0xc6, 0xf8, 0 };
782 static char s_sz2[] = { 0xc5, 0xc6, 0xf8, 0 };/// @todo multibyte tests.
783
784 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz2);
785 CHECKSTRCCH(s_sz2);
786 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz2);
787 CHECKSTRCCH(s_sz2);
788#endif
789}
790
791static void testHexFormatter(RTTEST hTest, char *pszBuf, char *pszBuf2)
792{
793 RTTestSub(hTest, "Hex dump formatting (%Rhx*)");
794 static uint8_t const s_abHex1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
795 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.1Rhxs", s_abHex1);
796 CHECKSTRCCH("00");
797 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.2Rhxs", s_abHex1);
798 CHECKSTRCCH("00 01");
799 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhxs", s_abHex1);
800 CHECKSTRCCH("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f");
801 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxs", sizeof(s_abHex1), s_abHex1);
802 CHECKSTRCCH("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
803 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.*Rhxs", sizeof(s_abHex1), s_abHex1);
804 CHECKSTRCCH("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
805 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%1.*Rhxs", sizeof(s_abHex1), s_abHex1);
806 CHECKSTRCCH("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
807 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*Rhxs", sizeof(s_abHex1), s_abHex1);
808 CHECKSTRCCH("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
809 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*RhXs", sizeof(s_abHex1), s_abHex1, (uint64_t)0x1234);
810 CHECKSTRCCH("00001234: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
811 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*RhXs", sizeof(s_abHex1), s_abHex1, (uint64_t)UINT64_C(0x987654321abcdef));
812 CHECKSTRCCH("0987654321abcdef: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
813
814 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.8Rhxd", s_abHex1);
815 RTStrPrintf(pszBuf2, BUF_SIZE,
816 "%p/0000: 00 01 02 03 ....\n"
817 "%p/0004: 04 05 06 07 ....",
818 &s_abHex1[0], &s_abHex1[4]);
819 CHECKSTRCCH(pszBuf2);
820
821 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.6Rhxd", s_abHex1);
822 RTStrPrintf(pszBuf2, BUF_SIZE,
823 "%p/0000: 00 01 02 03 ....\n"
824 "%p/0004: 04 05 ..",
825 &s_abHex1[0], &s_abHex1[4]);
826 CHECKSTRCCH(pszBuf2);
827
828 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxd", sizeof(s_abHex1), s_abHex1);
829 RTStrPrintf(pszBuf2, BUF_SIZE,
830 "%p/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
831 "%p/0010: 10 11 12 13 14 ....."
832 ,
833 &s_abHex1[0], &s_abHex1[0x10]);
834 CHECKSTRCCH(pszBuf2);
835
836 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*RhXd", sizeof(s_abHex1), s_abHex1, (uint64_t)0xf304);
837 RTStrPrintf(pszBuf2, BUF_SIZE,
838 "0000f304/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
839 "0000f314/0010: 10 11 12 13 14 .....");
840 CHECKSTRCCH(pszBuf2);
841
842 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*RhXd", sizeof(s_abHex1), s_abHex1, (uint64_t)UINT64_C(0x123456789abcdef));
843 RTStrPrintf(pszBuf2, BUF_SIZE,
844 "0123456789abcdef/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
845 "0123456789abcdff/0010: 10 11 12 13 14 .....");
846 CHECKSTRCCH(pszBuf2);
847}
848
849static void testHumanReadableNumbers(RTTEST hTest, char *pszBuf)
850{
851 RTTestSub(hTest, "Human readable (%Rhc?, %Rhn?)");
852 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(1235467), 42);
853 CHECKSTRCCH("1.1MiB42");
854 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(999), 42);
855 CHECKSTRCCH("999B42");
856 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(8), 42);
857 CHECKSTRCCH("8B42");
858 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(0), 42);
859 CHECKSTRCCH("0B42");
860 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.2Rhcb%u", UINT64_C(129957349834756374), 42);
861 CHECKSTRCCH("115.42PiB42");
862 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.3Rhcb%u", UINT64_C(1957349834756374), 42);
863 CHECKSTRCCH("1.738PiB42");
864 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.0Rhcb%u", UINT64_C(1957349834756374), 42);
865 CHECKSTRCCH("1780TiB42");
866 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6678345), 42);
867 CHECKSTRCCH(" 6.3MiB42");
868 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710886), 42);
869 CHECKSTRCCH(" 6.3MiB42");
870 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710887), 42);
871 CHECKSTRCCH(" 6.4MiB42");
872 cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10Rhcb%u", UINT64_C(6710887), 42);
873 CHECKSTRCCH(" 6.4 MiB42");
874 cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10RhcB%u", UINT64_C(6710887), 42);
875 CHECKSTRCCH(" 6.4 MB42");
876
877 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhub%u", UINT64_C(6678345), 42);
878 CHECKSTRCCH(" 6.3Mi42");
879 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10RhuB%u", UINT64_C(6678345), 42);
880 CHECKSTRCCH(" 6.3M42");
881
882 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhci%u", UINT64_C(6678345), 42);
883 CHECKSTRCCH(" 6.7MB42"); /* rounded, unlike the binary variant.*/
884}
885
886static void testX86RegisterFormatter(RTTEST hTest, char *pszBuf)
887{
888
889 RTTestSub(hTest, "x86 register format types (%RAx86[*])");
890 CHECK42("%RAx86[cr0]", UINT64_C(0x80000011), "80000011{PE,ET,PG}");
891 CHECK42("%RAx86[cr0]", UINT64_C(0x80000001), "80000001{PE,PG}");
892 CHECK42("%RAx86[cr0]", UINT64_C(0x00000001), "00000001{PE}");
893 CHECK42("%RAx86[cr0]", UINT64_C(0x80000000), "80000000{PG}");
894 CHECK42("%RAx86[cr4]", UINT64_C(0x80000001), "80000001{VME,unkn=80000000}");
895 CHECK42("%#RAx86[cr4]", UINT64_C(0x80000001), "0x80000001{VME,unkn=0x80000000}");
896}
897
898static void testCustomTypes(RTTEST hTest, char *pszBuf)
899{
900 RTTestSub(hTest, "Custom format types (%R[*])");
901 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
902 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
903 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)1);
904 CHECKSTRCCH("type3=1");
905
906 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
907 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
908 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)1, (void *)2);
909 CHECKSTRCCH("type3=1 type1=2");
910
911 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
912 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
913 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
914 CHECKSTRCCH("type3=1 type1=2 type4=3");
915
916 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
917 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
918 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
919 CHECKSTRCCH("type3=1 type1=2 type4=3 type2=4");
920
921 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
922 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
923 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5);
924 CHECKSTRCCH("type3=1 type1=2 type4=3 type2=4 type5=5");
925
926 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
927 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
928 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
929 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
930 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
931
932 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50);
933 CHECKSTRCCH("type3=10 type1=20 type4=30 type2=40 type5=50");
934
935 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS);
936 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
937 CHECKSTRCCH("type3=10 type1=20 type4=30 type5=40");
938
939 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS);
940 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
941 CHECKSTRCCH("type3=10 type1=20 type4=30");
942
943 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS);
944 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)10, (void *)20);
945 CHECKSTRCCH("type3=10 type1=20");
946
947 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS);
948 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)10);
949 CHECKSTRCCH("type3=10");
950
951 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
952}
953
954
955int main()
956{
957 RTTEST hTest;
958 int rc = RTTestInitAndCreate("tstRTStrFormat", &hTest);
959 if (rc)
960 return rc;
961 RTTestBanner(hTest);
962
963 char *pszBuf = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
964 char *pszBuf2 = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
965
966 /*
967 * Do the basics.
968 */
969 testBasics(hTest, pszBuf);
970
971 /*
972 * Nested
973 */
974 RTTestSub(hTest, "Nested (%N)");
975 testNested(__LINE__, "42 2684354560 42 asdf 42", "42 %u 42 %s 42", 2684354560U, "asdf");
976 testNested(__LINE__, "", "");
977
978 /*
979 * allocation
980 */
981 testAllocPrintf(hTest);
982
983 /*
984 * Test the waters.
985 */
986 CHECK42("%d", 127, "127");
987 CHECK42("%s", "721", "721");
988
989 /*
990 * Runtime extensions.
991 */
992 testRuntimeExtensions(hTest, pszBuf);
993
994 /*
995 * Thousand separators.
996 */
997 testThousandSeparators(hTest, pszBuf);
998
999 /*
1000 * String formatting.
1001 */
1002 testStringFormatter(hTest, pszBuf);
1003
1004 /*
1005 * Unicode string formatting.
1006 */
1007 testUnicodeStringFormatter(hTest, pszBuf);
1008
1009 /*
1010 * Hex formatting.
1011 */
1012 testHexFormatter(hTest, pszBuf, pszBuf2);
1013
1014 /*
1015 * human readable sizes and numbers.
1016 */
1017 testHumanReadableNumbers(hTest, pszBuf);
1018
1019 /*
1020 * x86 register formatting.
1021 */
1022 testX86RegisterFormatter(hTest, pszBuf);
1023
1024 /*
1025 * Custom types.
1026 */
1027 testCustomTypes(hTest, pszBuf);
1028
1029 testUtf16Printf(hTest);
1030
1031 /*
1032 * Summarize and exit.
1033 */
1034 return RTTestSummaryAndDestroy(hTest);
1035}
1036
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