VirtualBox

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

Last change on this file was 102985, checked in by vboxsync, 4 months ago

tstRTStrFormat: Added an overflow test.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 41.8 KB
Line 
1/* $Id: tstRTStrFormat.cpp 102985 2024-01-22 09:59:41Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String formatting.
4 */
5
6/*
7 * Copyright (C) 2006-2023 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
243static void testBasics(RTTEST hTest, char *pszBuf)
244{
245 RTTestSub(hTest, "Basics");
246
247 uint32_t u32 = 0x010;
248 uint64_t u64 = 0x100;
249
250 /* simple */
251 static const char s_szSimpleExpect[] = "u32=16 u64=256 u64=0x100";
252 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
253 if (strcmp(pszBuf, s_szSimpleExpect))
254 RTTestIFailed("error: '%s'\n"
255 "wanted '%s'\n", pszBuf, s_szSimpleExpect);
256 else if (cch != sizeof(s_szSimpleExpect) - 1)
257 RTTestIFailed("error: got %zd, expected %zd (#1)\n", cch, sizeof(s_szSimpleExpect) - 1);
258
259 ssize_t cch2 = RTStrPrintf2(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
260 if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100"))
261 RTTestIFailed("error: '%s' (#2)\n"
262 "wanted '%s' (#2)\n", pszBuf, s_szSimpleExpect);
263 else if (cch2 != sizeof(s_szSimpleExpect) - 1)
264 RTTestIFailed("error: got %zd, expected %zd (#2)\n", cch2, sizeof(s_szSimpleExpect) - 1);
265
266 /* just big. */
267 u64 = UINT64_C(0x7070605040302010);
268 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
269 if (strcmp(pszBuf, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
270 {
271 RTTestIFailed("error: '%s'\n"
272 "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf);
273 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
274 }
275
276 /* huge and negative. */
277 u64 = UINT64_C(0x8070605040302010);
278 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
279 /* Not sure if this is the correct decimal representation... But both */
280 if (strcmp(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
281 {
282 RTTestIFailed("error: '%s'\n"
283 "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf);
284 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
285 }
286
287 /* 64-bit value bug. */
288 u64 = 0xa0000000;
289 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
290 if (strcmp(pszBuf, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
291 RTTestIFailed("error: '%s'\n"
292 "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf);
293
294 /* uuid */
295 RTUUID Uuid;
296 RTUuidCreate(&Uuid);
297 char szCorrect[RTUUID_STR_LENGTH];
298 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
299 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
300 if (strcmp(pszBuf, szCorrect))
301 RTTestIFailed("error: '%s'\n"
302 "expected: '%s'\n",
303 pszBuf, szCorrect);
304
305 /* overflow */
306 size_t const cbOverflowBuf = 40;
307 static char const s_szOverflowExpect[] = "i=8 ";
308 char *pszBuf3 = (char *)RTTestGuardedAllocTail(hTest, cbOverflowBuf);
309 cch = RTStrPrintf(pszBuf3, cbOverflowBuf, "i=%d%*s\n", (int)8, cbOverflowBuf, "");
310 if (strcmp(pszBuf3, s_szOverflowExpect))
311 RTTestIFailed("error: '%s' (#2)\n"
312 "wanted '%s' (#2)\n", pszBuf, s_szOverflowExpect);
313 else if (cch != sizeof(s_szOverflowExpect) - 1)
314 RTTestIFailed("error: got %zd, expected %zd (#2)\n", cch2, sizeof(s_szOverflowExpect) - 1);
315 RTTestGuardedFree(hTest, pszBuf3);
316}
317
318
319static void testRuntimeExtensions(RTTEST hTest, char *pszBuf)
320{
321 RTTestSub(hTest, "Runtime format types (%R*)");
322 CHECK42("%RGi", (RTGCINT)127, "127");
323 CHECK42("%RGi", (RTGCINT)-586589, "-586589");
324
325 CHECK42("%RGp", (RTGCPHYS)0x0000000044505045, "0000000044505045");
326 CHECK42("%RGp", ~(RTGCPHYS)0, "ffffffffffffffff");
327
328 CHECK42("%RGu", (RTGCUINT)586589, "586589");
329 CHECK42("%RGu", (RTGCUINT)1, "1");
330 CHECK42("%RGu", (RTGCUINT)3000000000U, "3000000000");
331
332#if GC_ARCH_BITS == 32
333 CHECK42("%RGv", (RTGCUINTPTR)0, "00000000");
334 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffff");
335 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "84342134");
336#else
337 CHECK42("%RGv", (RTGCUINTPTR)0, "0000000000000000");
338 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffffffffffff");
339 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "0000000084342134");
340#endif
341
342 CHECK42("%RGx", (RTGCUINT)0x234, "234");
343 CHECK42("%RGx", (RTGCUINT)0xffffffff, "ffffffff");
344
345 CHECK42("%RRv", (RTRCUINTPTR)0, "00000000");
346 CHECK42("%RRv", ~(RTRCUINTPTR)0, "ffffffff");
347 CHECK42("%RRv", (RTRCUINTPTR)0x84342134, "84342134");
348
349 CHECK42("%RHi", (RTHCINT)127, "127");
350 CHECK42("%RHi", (RTHCINT)-586589, "-586589");
351
352 CHECK42("%RHp", (RTHCPHYS)0x0000000044505045, "0000000044505045");
353 CHECK42("%RHp", ~(RTHCPHYS)0, "ffffffffffffffff");
354
355 CHECK42("%RHu", (RTHCUINT)586589, "586589");
356 CHECK42("%RHu", (RTHCUINT)1, "1");
357 CHECK42("%RHu", (RTHCUINT)3000000000U, "3000000000");
358
359 if (sizeof(void*) == 8)
360 {
361 CHECK42("%RHv", (RTHCUINTPTR)0, "0000000000000000");
362 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffffffffffff");
363 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "0000000084342134");
364 }
365 else
366 {
367 CHECK42("%RHv", (RTHCUINTPTR)0, "00000000");
368 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffff");
369 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "84342134");
370 }
371
372 CHECK42("%RHx", (RTHCUINT)0x234, "234");
373 CHECK42("%RHx", (RTHCUINT)0xffffffff, "ffffffff");
374
375 CHECK42("%RI16", (int16_t)1, "1");
376 CHECK42("%RI16", (int16_t)-16384, "-16384");
377 CHECK42("%RI16", INT16_MAX, "32767");
378 CHECK42("%RI16", INT16_MIN, "-32768");
379
380 CHECK42("%RI32", (int32_t)1123, "1123");
381 CHECK42("%RI32", (int32_t)-86596, "-86596");
382 CHECK42("%RI32", INT32_MAX, "2147483647");
383 CHECK42("%RI32", INT32_MIN, "-2147483648");
384 CHECK42("%RI32", INT32_MIN+1, "-2147483647");
385 CHECK42("%RI32", INT32_MIN+2, "-2147483646");
386
387 CHECK42("%RI64", (int64_t)112345987345LL, "112345987345");
388 CHECK42("%RI64", (int64_t)-8659643985723459LL, "-8659643985723459");
389 CHECK42("%RI64", INT64_MAX, "9223372036854775807");
390 CHECK42("%RI64", INT64_MIN, "-9223372036854775808");
391 CHECK42("%RI64", INT64_MIN+1, "-9223372036854775807");
392 CHECK42("%RI64", INT64_MIN+2, "-9223372036854775806");
393
394 CHECK42("%RI8", (int8_t)1, "1");
395 CHECK42("%RI8", (int8_t)-128, "-128");
396
397 CHECK42("%Rbn", "file.c", "file.c");
398 CHECK42("%Rbn", "foo/file.c", "file.c");
399 CHECK42("%Rbn", "/foo/file.c", "file.c");
400 CHECK42("%Rbn", "/dir/subdir/", "subdir/");
401
402 CHECK42("%Rfn", "function", "function");
403 CHECK42("%Rfn", "void function(void)", "function");
404
405 CHECK42("%RTfile", (RTFILE)127, "127");
406 CHECK42("%RTfile", (RTFILE)12341234, "12341234");
407
408 CHECK42("%RTfmode", (RTFMODE)0x123403, "00123403");
409
410 CHECK42("%RTfoff", (RTFOFF)12342312, "12342312");
411 CHECK42("%RTfoff", (RTFOFF)-123123123, "-123123123");
412 CHECK42("%RTfoff", (RTFOFF)858694596874568LL, "858694596874568");
413
414 RTFAR16 fp16;
415 fp16.off = 0x34ff;
416 fp16.sel = 0x0160;
417 CHECK42("%RTfp16", fp16, "0160:34ff");
418
419 RTFAR32 fp32;
420 fp32.off = 0xff094030;
421 fp32.sel = 0x0168;
422 CHECK42("%RTfp32", fp32, "0168:ff094030");
423
424 RTFAR64 fp64;
425 fp64.off = 0xffff003401293487ULL;
426 fp64.sel = 0x0ff8;
427 CHECK42("%RTfp64", fp64, "0ff8:ffff003401293487");
428 fp64.off = 0x0;
429 fp64.sel = 0x0;
430 CHECK42("%RTfp64", fp64, "0000:0000000000000000");
431
432 CHECK42("%RTgid", (RTGID)-1, "-1");
433 CHECK42("%RTgid", (RTGID)1004, "1004");
434
435 CHECK42("%RTino", (RTINODE)0, "0000000000000000");
436 CHECK42("%RTino", (RTINODE)0x123412341324ULL, "0000123412341324");
437
438 CHECK42("%RTint", (RTINT)127, "127");
439 CHECK42("%RTint", (RTINT)-586589, "-586589");
440 CHECK42("%RTint", (RTINT)-23498723, "-23498723");
441
442 CHECK42("%RTiop", (RTIOPORT)0x3c4, "03c4");
443 CHECK42("%RTiop", (RTIOPORT)0xffff, "ffff");
444
445 RTMAC Mac;
446 Mac.au8[0] = 0;
447 Mac.au8[1] = 0x1b;
448 Mac.au8[2] = 0x21;
449 Mac.au8[3] = 0x0a;
450 Mac.au8[4] = 0x1d;
451 Mac.au8[5] = 0xd9;
452 CHECK42("%RTmac", &Mac, "00:1b:21:0a:1d:d9");
453 Mac.au16[0] = 0xffff;
454 Mac.au16[1] = 0xffff;
455 Mac.au16[2] = 0xffff;
456 CHECK42("%RTmac", &Mac, "ff:ff:ff:ff:ff:ff");
457
458 RTNETADDRIPV4 Ipv4Addr;
459 Ipv4Addr.u = RT_H2N_U32_C(0xf040d003);
460 CHECK42("%RTnaipv4", Ipv4Addr.u, "240.64.208.3");
461 Ipv4Addr.u = RT_H2N_U32_C(0xffffffff);
462 CHECK42("%RTnaipv4", Ipv4Addr.u, "255.255.255.255");
463
464 RTNETADDRIPV6 Ipv6Addr;
465
466 /* any */
467 memset(&Ipv6Addr, 0, sizeof(Ipv6Addr));
468 CHECK42("%RTnaipv6", &Ipv6Addr, "::");
469
470 /* loopback */
471 Ipv6Addr.au8[15] = 1;
472 CHECK42("%RTnaipv6", &Ipv6Addr, "::1");
473
474 /* IPv4-compatible */
475 Ipv6Addr.au8[12] = 1;
476 Ipv6Addr.au8[13] = 1;
477 Ipv6Addr.au8[14] = 1;
478 Ipv6Addr.au8[15] = 1;
479 CHECK42("%RTnaipv6", &Ipv6Addr, "::1.1.1.1");
480
481 /* IPv4-mapped */
482 Ipv6Addr.au16[5] = RT_H2N_U16_C(0xffff);
483 CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:1.1.1.1");
484
485 /* IPv4-translated */
486 Ipv6Addr.au16[4] = RT_H2N_U16_C(0xffff);
487 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
488 CHECK42("%RTnaipv6", &Ipv6Addr, "::ffff:0:1.1.1.1");
489
490 /* single zero word is not abbreviated, leading zeroes are not printed */
491 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000);
492 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0001);
493 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
494 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0001);
495 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
496 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0001);
497 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
498 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
499 CHECK42("%RTnaipv6", &Ipv6Addr, "0:1:0:1:0:1:0:1");
500
501 /* longest run is abbreviated (here: at the beginning) */
502 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0000);
503 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0000);
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(0x0000);
508 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0001);
509 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0000);
510 CHECK42("%RTnaipv6", &Ipv6Addr, "::1:0:0:1:0");
511
512 /* longest run is abbreviated (here: first) */
513 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x0001);
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(0x0000);
517 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001);
518 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x0000);
519 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0000);
520 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x0001);
521 CHECK42("%RTnaipv6", &Ipv6Addr, "1::1:0:0:1");
522
523 /* longest run is abbreviated (here: second) */
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(0x0001);
528 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
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:0:0:1::1");
533
534 /* longest run is abbreviated (here: at the end) */
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(0x0000);
543 CHECK42("%RTnaipv6", &Ipv6Addr, "1:0:0:1::");
544
545 /* first of the two runs of equal length is abbreviated */
546 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001);
547 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8);
548 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x0000);
549 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
550 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0001);
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(0x0001);
554 CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8::1:0:0:1");
555
556 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001);
557 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8);
558 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x85a3);
559 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
560 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
561 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x8a2e);
562 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0370);
563 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x7334);
564 CHECK42("%RTnaipv6", &Ipv6Addr, "2001:db8:85a3::8a2e:370:7334");
565
566 Ipv6Addr.au64[0] = UINT64_MAX;
567 Ipv6Addr.au64[1] = UINT64_MAX;
568 CHECK42("%RTnaipv6", &Ipv6Addr, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
569
570 RTNETADDR NetAddr;
571 memset(&NetAddr, 0, sizeof(NetAddr));
572
573 /* plain IPv6 address if port is not specified */
574 NetAddr.enmType = RTNETADDRTYPE_IPV6;
575 NetAddr.uAddr.au16[0] = RT_H2N_U16_C(0x0001);
576 NetAddr.uAddr.au16[7] = RT_H2N_U16_C(0x0001);
577 NetAddr.uPort = RTNETADDR_PORT_NA;
578 CHECK42("%RTnaddr", &NetAddr, "1::1");
579
580 /* square brackets around IPv6 address if port is specified */
581 NetAddr.uPort = 1;
582 CHECK42("%RTnaddr", &NetAddr, "[1::1]:1");
583
584 CHECK42("%RTproc", (RTPROCESS)0xffffff, "00ffffff");
585 CHECK42("%RTproc", (RTPROCESS)0x43455443, "43455443");
586
587#if (HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64)
588 CHECK42("%RTptr", (RTUINTPTR)0, "0000000000000000");
589 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffffffffffff");
590 CHECK42("%RTptr", (RTUINTPTR)(uintptr_t)0x84342134, "0000000084342134");
591#else
592 CHECK42("%RTptr", (RTUINTPTR)0, "00000000");
593 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffff");
594 CHECK42("%RTptr", (RTUINTPTR)(uintptr_t)0x84342134, "84342134");
595#endif
596
597#if ARCH_BITS == 64
598 AssertCompileSize(RTCCUINTREG, 8);
599 CHECK42("%RTreg", (RTCCUINTREG)0, "0000000000000000");
600 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffffffffffff");
601 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "0000000084342134");
602 CHECK42("%RTreg", (RTCCUINTREG)0x23484342134ULL, "0000023484342134");
603#elif ARCH_BITS == 32
604 AssertCompileSize(RTCCUINTREG, 4);
605 CHECK42("%RTreg", (RTCCUINTREG)0, "00000000");
606 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffff");
607 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "84342134");
608#else
609# error ARCH_BITS
610#endif
611
612 CHECK42("%RTsel", (RTSEL)0x543, "0543");
613 CHECK42("%RTsel", (RTSEL)0xf8f8, "f8f8");
614
615#if ARCH_BITS == 64
616 CHECK42("%RTsem", (RTSEMEVENT)0, "0000000000000000");
617 CHECK42("%RTsem", (RTSEMEVENT)(uintptr_t)0x23484342134ULL, "0000023484342134");
618#else
619 CHECK42("%RTsem", (RTSEMEVENT)0, "00000000");
620 CHECK42("%RTsem", (RTSEMEVENT)(uintptr_t)0x84342134, "84342134");
621#endif
622
623 CHECK42("%RTsock", (RTSOCKET)(uintptr_t)12234, "12234");
624 CHECK42("%RTsock", (RTSOCKET)(uintptr_t)584854543, "584854543");
625
626#if ARCH_BITS == 64
627 CHECK42("%RTthrd", (RTTHREAD)0, "0000000000000000");
628 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffffffffffff");
629 CHECK42("%RTthrd", (RTTHREAD)(uintptr_t)0x63484342134ULL, "0000063484342134");
630#else
631 CHECK42("%RTthrd", (RTTHREAD)0, "00000000");
632 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffff");
633 CHECK42("%RTthrd", (RTTHREAD)(uintptr_t)0x54342134, "54342134");
634#endif
635
636 CHECK42("%RTuid", (RTUID)-2, "-2");
637 CHECK42("%RTuid", (RTUID)90344, "90344");
638
639 CHECK42("%RTuint", (RTUINT)584589, "584589");
640 CHECK42("%RTuint", (RTUINT)3, "3");
641 CHECK42("%RTuint", (RTUINT)2400000000U, "2400000000");
642
643 RTUUID Uuid;
644 char szCorrect[RTUUID_STR_LENGTH];
645 RTUuidCreate(&Uuid);
646 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
647 RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
648 if (strcmp(pszBuf, szCorrect))
649 RTTestIFailed("error: '%s'\n"
650 "expected: '%s'\n",
651 pszBuf, szCorrect);
652
653 CHECK42("%RTxint", (RTUINT)0x2345, "2345");
654 CHECK42("%RTxint", (RTUINT)0xffff8fff, "ffff8fff");
655
656 CHECK42("%RU16", (uint16_t)7, "7");
657 CHECK42("%RU16", (uint16_t)46384, "46384");
658
659 CHECK42("%RU32", (uint32_t)1123, "1123");
660 CHECK42("%RU32", (uint32_t)86596, "86596");
661 CHECK42("%4RU32", (uint32_t)42, " 42");
662 CHECK42("%04RU32", (uint32_t)42, "0042");
663 CHECK42("%.4RU32", (uint32_t)42, "0042");
664
665 CHECK42("%RU64", (uint64_t)112345987345ULL, "112345987345");
666 CHECK42("%RU64", (uint64_t)8659643985723459ULL, "8659643985723459");
667 CHECK42("%14RU64", (uint64_t)4, " 4");
668 CHECK42("%014RU64", (uint64_t)4, "00000000000004");
669 CHECK42("%.14RU64", (uint64_t)4, "00000000000004");
670
671 CHECK42("%RU8", (uint8_t)1, "1");
672 CHECK42("%RU8", (uint8_t)254, "254");
673 CHECK42("%RU8", 256, "0");
674
675 CHECK42("%RX16", (uint16_t)0x7, "7");
676 CHECK42("%RX16", 0x46384, "6384");
677 CHECK42("%RX16", UINT16_MAX, "ffff");
678
679 CHECK42("%RX32", (uint32_t)0x1123, "1123");
680 CHECK42("%RX32", (uint32_t)0x49939493, "49939493");
681 CHECK42("%RX32", UINT32_MAX, "ffffffff");
682
683 CHECK42("%RX64", UINT64_C(0x348734), "348734");
684 CHECK42("%RX64", UINT64_C(0x12312312312343f), "12312312312343f");
685 CHECK42("%RX64", UINT64_MAX, "ffffffffffffffff");
686 CHECK42("%5RX64", UINT64_C(0x42), " 42");
687 CHECK42("%05RX64", UINT64_C(0x42), "00042");
688 CHECK42("%.5RX64", UINT64_C(0x42), "00042");
689 CHECK42("%.05RX64", UINT64_C(0x42), "00042"); /* '0' is ignored */
690
691 CHECK42("%RX8", (uint8_t)1, "1");
692 CHECK42("%RX8", (uint8_t)0xff, "ff");
693 CHECK42("%RX8", UINT8_MAX, "ff");
694 CHECK42("%RX8", 0x100, "0");
695}
696
697static void testThousandSeparators(RTTEST hTest, char *pszBuf)
698{
699 RTTestSub(hTest, "Thousand Separators (%'*)");
700
701 RTStrFormatNumber(pszBuf, 1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1"); memset(pszBuf, '!', BUF_SIZE);
702 RTStrFormatNumber(pszBuf, 10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10"); memset(pszBuf, '!', BUF_SIZE);
703 RTStrFormatNumber(pszBuf, 100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100"); memset(pszBuf, '!', BUF_SIZE);
704 RTStrFormatNumber(pszBuf, 1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000"); memset(pszBuf, '!', BUF_SIZE);
705 RTStrFormatNumber(pszBuf, 10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000"); memset(pszBuf, '!', BUF_SIZE);
706 RTStrFormatNumber(pszBuf, 100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000"); memset(pszBuf, '!', BUF_SIZE);
707 RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000"); memset(pszBuf, '!', BUF_SIZE);
708
709 CHECK42("%'u", 1, "1");
710 CHECK42("%'u", 10, "10");
711 CHECK42("%'u", 100, "100");
712 CHECK42("%'u", 1000, "1 000");
713 CHECK42("%'u", 10000, "10 000");
714 CHECK42("%'u", 100000, "100 000");
715 CHECK42("%'u", 1000000, "1 000 000");
716 CHECK42("%'RU64", _1T, "1 099 511 627 776");
717 CHECK42("%'RU64", _1E, "1 152 921 504 606 846 976");
718}
719
720static void testStringFormatter(RTTEST hTest, char *pszBuf)
721{
722 RTTestSub(hTest, "String formatting (%s)");
723
724// 0 1 2 3 4 5 6 7
725// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
726 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "args", "description");
727 CHECKSTR("cmd args description");
728
729 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "", "description");
730 CHECKSTR("cmd description");
731
732
733 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%*s", 0, "");
734 CHECKSTR("");
735
736 /* automatic conversions. */
737 static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
738 static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
739
740 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz1);
741 CHECKSTR("hello world");
742 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz1);
743 CHECKSTR("hello world");
744
745 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5ls", s_wsz1);
746 CHECKSTR("hello");
747 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5Ls", s_usz1);
748 CHECKSTR("hello");
749}
750
751static void testUnicodeStringFormatter(RTTEST hTest, char *pszBuf)
752{
753 RTTestSub(hTest, "Unicode string formatting (%ls)");
754 static RTUTF16 s_wszEmpty[] = { 0 }; //assumes ascii.
755 static RTUTF16 s_wszCmd[] = { 'c', 'm', 'd', 0 }; //assumes ascii.
756 static RTUTF16 s_wszArgs[] = { 'a', 'r', 'g', 's', 0 }; //assumes ascii.
757 static RTUTF16 s_wszDesc[] = { 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 0 }; //assumes ascii.
758
759// 0 1 2 3 4 5 6 7
760// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
761 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc);
762 CHECKSTR("cmd args description");
763
764 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc);
765 CHECKSTR("cmd description");
766
767
768#if 0
769 static RTUNICP s_usz2[] = { 0xc5, 0xc6, 0xf8, 0 };
770 static RTUTF16 s_wsz2[] = { 0xc5, 0xc6, 0xf8, 0 };
771 static char s_sz2[] = { 0xc5, 0xc6, 0xf8, 0 };/// @todo multibyte tests.
772
773 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz2);
774 CHECKSTR(s_sz2);
775 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz2);
776 CHECKSTR(s_sz2);
777#endif
778}
779
780static void testHexFormatter(RTTEST hTest, char *pszBuf, char *pszBuf2)
781{
782 RTTestSub(hTest, "Hex dump formatting (%Rhx*)");
783 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 };
784 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.1Rhxs", s_abHex1);
785 CHECKSTR("00");
786 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.2Rhxs", s_abHex1);
787 CHECKSTR("00 01");
788 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhxs", s_abHex1);
789 CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f");
790 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxs", sizeof(s_abHex1), s_abHex1);
791 CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
792 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.*Rhxs", sizeof(s_abHex1), s_abHex1);
793 CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
794 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%1.*Rhxs", sizeof(s_abHex1), s_abHex1);
795 CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
796 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*Rhxs", sizeof(s_abHex1), s_abHex1);
797 CHECKSTR("00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
798 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*RhXs", sizeof(s_abHex1), s_abHex1, (uint64_t)0x1234);
799 CHECKSTR("00001234: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
800 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%256.*RhXs", sizeof(s_abHex1), s_abHex1, (uint64_t)UINT64_C(0x987654321abcdef));
801 CHECKSTR("0987654321abcdef: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14");
802
803 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.8Rhxd", s_abHex1);
804 RTStrPrintf(pszBuf2, BUF_SIZE,
805 "%p/0000: 00 01 02 03 ....\n"
806 "%p/0004: 04 05 06 07 ....",
807 &s_abHex1[0], &s_abHex1[4]);
808 CHECKSTR(pszBuf2);
809
810 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%4.6Rhxd", s_abHex1);
811 RTStrPrintf(pszBuf2, BUF_SIZE,
812 "%p/0000: 00 01 02 03 ....\n"
813 "%p/0004: 04 05 ..",
814 &s_abHex1[0], &s_abHex1[4]);
815 CHECKSTR(pszBuf2);
816
817 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*Rhxd", sizeof(s_abHex1), s_abHex1);
818 RTStrPrintf(pszBuf2, BUF_SIZE,
819 "%p/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
820 "%p/0010: 10 11 12 13 14 ....."
821 ,
822 &s_abHex1[0], &s_abHex1[0x10]);
823 CHECKSTR(pszBuf2);
824
825 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*RhXd", sizeof(s_abHex1), s_abHex1, (uint64_t)0xf304);
826 RTStrPrintf(pszBuf2, BUF_SIZE,
827 "0000f304/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
828 "0000f314/0010: 10 11 12 13 14 .....");
829 CHECKSTR(pszBuf2);
830
831 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.*RhXd", sizeof(s_abHex1), s_abHex1, (uint64_t)UINT64_C(0x123456789abcdef));
832 RTStrPrintf(pszBuf2, BUF_SIZE,
833 "0123456789abcdef/0000: 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
834 "0123456789abcdff/0010: 10 11 12 13 14 .....");
835 CHECKSTR(pszBuf2);
836}
837
838static void testHumanReadableNumbers(RTTEST hTest, char *pszBuf)
839{
840 RTTestSub(hTest, "Human readable (%Rhc?, %Rhn?)");
841 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(1235467), 42);
842 CHECKSTR("1.1MiB42");
843 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(999), 42);
844 CHECKSTR("999B42");
845 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(8), 42);
846 CHECKSTR("8B42");
847 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Rhcb%u", UINT64_C(0), 42);
848 CHECKSTR("0B42");
849 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.2Rhcb%u", UINT64_C(129957349834756374), 42);
850 CHECKSTR("115.42PiB42");
851 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.3Rhcb%u", UINT64_C(1957349834756374), 42);
852 CHECKSTR("1.738PiB42");
853 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.0Rhcb%u", UINT64_C(1957349834756374), 42);
854 CHECKSTR("1780TiB42");
855 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6678345), 42);
856 CHECKSTR(" 6.3MiB42");
857 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710886), 42);
858 CHECKSTR(" 6.3MiB42");
859 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710887), 42);
860 CHECKSTR(" 6.4MiB42");
861 cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10Rhcb%u", UINT64_C(6710887), 42);
862 CHECKSTR(" 6.4 MiB42");
863 cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10RhcB%u", UINT64_C(6710887), 42);
864 CHECKSTR(" 6.4 MB42");
865
866 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhub%u", UINT64_C(6678345), 42);
867 CHECKSTR(" 6.3Mi42");
868 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10RhuB%u", UINT64_C(6678345), 42);
869 CHECKSTR(" 6.3M42");
870
871 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhci%u", UINT64_C(6678345), 42);
872 CHECKSTR(" 6.7MB42"); /* rounded, unlike the binary variant.*/
873}
874
875static void testX86RegisterFormatter(RTTEST hTest, char *pszBuf)
876{
877
878 RTTestSub(hTest, "x86 register format types (%RAx86[*])");
879 CHECK42("%RAx86[cr0]", UINT64_C(0x80000011), "80000011{PE,ET,PG}");
880 CHECK42("%RAx86[cr0]", UINT64_C(0x80000001), "80000001{PE,PG}");
881 CHECK42("%RAx86[cr0]", UINT64_C(0x00000001), "00000001{PE}");
882 CHECK42("%RAx86[cr0]", UINT64_C(0x80000000), "80000000{PG}");
883 CHECK42("%RAx86[cr4]", UINT64_C(0x80000001), "80000001{VME,unkn=80000000}");
884 CHECK42("%#RAx86[cr4]", UINT64_C(0x80000001), "0x80000001{VME,unkn=0x80000000}");
885}
886
887static void testCustomTypes(RTTEST hTest, char *pszBuf)
888{
889 RTTestSub(hTest, "Custom format types (%R[*])");
890 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
891 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
892 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)1);
893 CHECKSTR("type3=1");
894
895 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
896 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
897 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)1, (void *)2);
898 CHECKSTR("type3=1 type1=2");
899
900 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
901 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
902 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
903 CHECKSTR("type3=1 type1=2 type4=3");
904
905 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
906 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
907 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
908 CHECKSTR("type3=1 type1=2 type4=3 type2=4");
909
910 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
911 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
912 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);
913 CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5");
914
915 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
916 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
917 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
918 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
919 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
920
921 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);
922 CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50");
923
924 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS);
925 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
926 CHECKSTR("type3=10 type1=20 type4=30 type5=40");
927
928 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS);
929 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
930 CHECKSTR("type3=10 type1=20 type4=30");
931
932 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS);
933 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)10, (void *)20);
934 CHECKSTR("type3=10 type1=20");
935
936 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS);
937 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)10);
938 CHECKSTR("type3=10");
939
940 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
941}
942
943
944int main()
945{
946 RTTEST hTest;
947 int rc = RTTestInitAndCreate("tstRTStrFormat", &hTest);
948 if (rc)
949 return rc;
950 RTTestBanner(hTest);
951
952 char *pszBuf = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
953 char *pszBuf2 = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
954
955 /*
956 * Do the basics.
957 */
958 testBasics(hTest, pszBuf);
959
960 /*
961 * Nested
962 */
963 RTTestSub(hTest, "Nested (%N)");
964 testNested(__LINE__, "42 2684354560 42 asdf 42", "42 %u 42 %s 42", 2684354560U, "asdf");
965 testNested(__LINE__, "", "");
966
967 /*
968 * allocation
969 */
970 testAllocPrintf(hTest);
971
972 /*
973 * Test the waters.
974 */
975 CHECK42("%d", 127, "127");
976 CHECK42("%s", "721", "721");
977
978 /*
979 * Runtime extensions.
980 */
981 testRuntimeExtensions(hTest, pszBuf);
982
983 /*
984 * Thousand separators.
985 */
986 testThousandSeparators(hTest, pszBuf);
987
988 /*
989 * String formatting.
990 */
991 testStringFormatter(hTest, pszBuf);
992
993 /*
994 * Unicode string formatting.
995 */
996 testUnicodeStringFormatter(hTest, pszBuf);
997
998 /*
999 * Hex formatting.
1000 */
1001 testHexFormatter(hTest, pszBuf, pszBuf2);
1002
1003 /*
1004 * human readable sizes and numbers.
1005 */
1006 testHumanReadableNumbers(hTest, pszBuf);
1007
1008 /*
1009 * x86 register formatting.
1010 */
1011 testX86RegisterFormatter(hTest, pszBuf);
1012
1013 /*
1014 * Custom types.
1015 */
1016 testCustomTypes(hTest, pszBuf);
1017
1018 testUtf16Printf(hTest);
1019
1020 /*
1021 * Summarize and exit.
1022 */
1023 return RTTestSummaryAndDestroy(hTest);
1024}
1025
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use