VirtualBox

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

Last change on this file since 80565 was 80565, checked in by vboxsync, 5 years ago

IPRT/string.h: Added hexformatting variants that takes an additional 64-bit offset/address argument to be used instead of the memory pointer: %RhXd, %RhXD, %RhXs Also changed space between the pointer and offset to a slash.

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