VirtualBox

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

Last change on this file since 52320 was 50392, checked in by vboxsync, 11 years ago

More test cases for %RTnaipv6 and IPv6 %RTnaddr.

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