VirtualBox

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

Last change on this file since 101472 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

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