VirtualBox

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

Last change on this file since 28267 was 27066, checked in by vboxsync, 15 years ago

fixed tstRTStrFormat for 32-bit-gcc platforms -- do it like the other VBox code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 23.9 KB
Line 
1/* $Id: tstRTStrFormat.cpp 27066 2010-03-05 10:20:53Z vboxsync $ */
2/** @file
3 * IPRT Testcase - String formatting.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/string.h>
35
36#include <iprt/initterm.h>
37#include <iprt/net.h>
38#include <iprt/stream.h>
39#include <iprt/test.h>
40#include <iprt/uuid.h>
41
42
43/** See FNRTSTRFORMATTYPE. */
44static DECLCALLBACK(size_t) TstType(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
45 const char *pszType, void const *pvValue,
46 int cchWidth, int cchPrecision, unsigned fFlags,
47 void *pvUser)
48{
49 /* validate */
50 if (strncmp(pszType, "type", 4))
51 RTTestIFailed("pszType=%s expected 'typeN'\n", pszType);
52
53 int iType = pszType[4] - '0';
54 if ((uintptr_t)pvUser != (uintptr_t)TstType + iType)
55 RTTestIFailed("pvValue=%p expected %p\n", pvUser, (void *)((uintptr_t)TstType + iType));
56
57 /* format */
58 size_t cch = pfnOutput(pvArgOutput, pszType, 5);
59 cch += pfnOutput(pvArgOutput, "=", 1);
60 char szNum[64];
61 size_t cchNum = RTStrFormatNumber(szNum, (uintptr_t)pvValue, 10, cchWidth, cchPrecision, fFlags);
62 cch += pfnOutput(pvArgOutput, szNum, cchNum);
63 return cch;
64}
65
66
67static void testNested(int iLine, const char *pszExpect, const char *pszFormat, ...)
68{
69 size_t cchExpect = strlen(pszExpect);
70 char szBuf[512];
71
72 va_list va;
73 va_start(va, pszFormat);
74 size_t cch = RTStrPrintf(szBuf, sizeof(szBuf), "%N", pszFormat, &va);
75 va_end(va);
76 if (strcmp(szBuf, pszExpect))
77 RTTestIFailed("at line %d: nested format '%s'\n"
78 " output: '%s'\n"
79 " wanted: '%s'\n",
80 iLine, pszFormat, szBuf, pszExpect);
81 else if (cch != cchExpect)
82 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
83 iLine, cch, cchExpect);
84
85 va_start(va, pszFormat);
86 cch = RTStrPrintf(szBuf, sizeof(szBuf), "%uxxx%Nyyy%u", 43, pszFormat, &va, 43);
87 va_end(va);
88 if ( strncmp(szBuf, "43xxx", 5)
89 || strncmp(szBuf + 5, pszExpect, cchExpect)
90 || strcmp( szBuf + 5 + cchExpect, "yyy43") )
91 RTTestIFailed("at line %d: nested format '%s'\n"
92 " output: '%s'\n"
93 " wanted: '43xxx%syyy43'\n",
94 iLine, pszFormat, szBuf, pszExpect);
95 else if (cch != 5 + cchExpect + 5)
96 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
97 iLine, cch, 5 + cchExpect + 5);
98}
99
100
101int main()
102{
103 RTTEST hTest;
104 int rc = RTTestInitAndCreate("tstRTStrFormat", &hTest);
105 if (rc)
106 return rc;
107 RTTestBanner(hTest);
108
109 uint32_t u32 = 0x010;
110 uint64_t u64 = 0x100;
111#define BUF_SIZE 120
112 char *pszBuf = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
113
114 RTTestSub(hTest, "Basics");
115
116 /* simple */
117 size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
118 if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100"))
119 {
120 RTTestIFailed("error: '%s'\n"
121 "wanted 'u32=16 u64=256 u64=0x100'\n", pszBuf);
122 }
123
124 /* just big. */
125 u64 = UINT64_C(0x7070605040302010);
126 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
127 if (strcmp(pszBuf, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
128 {
129 RTTestIFailed("error: '%s'\n"
130 "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf);
131 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
132 }
133
134 /* huge and negative. */
135 u64 = UINT64_C(0x8070605040302010);
136 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
137 /* Not sure if this is the correct decimal representation... But both */
138 if (strcmp(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
139 {
140 RTTestIFailed("error: '%s'\n"
141 "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf);
142 RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
143 }
144
145 /* 64-bit value bug. */
146 u64 = 0xa0000000;
147 cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
148 if (strcmp(pszBuf, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
149 RTTestIFailed("error: '%s'\n"
150 "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf);
151
152 /* uuid */
153 RTUUID Uuid;
154 RTUuidCreate(&Uuid);
155 char szCorrect[RTUUID_STR_LENGTH];
156 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
157 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
158 if (strcmp(pszBuf, szCorrect))
159 RTTestIFailed("error: '%s'\n"
160 "expected: '%s'\n",
161 pszBuf, szCorrect);
162
163 /*
164 * Nested
165 */
166 RTTestSub(hTest, "Nested (%N)");
167 testNested(__LINE__, "42 2684354560 42 asdf 42", "42 %u 42 %s 42", 2684354560U, "asdf");
168 testNested(__LINE__, "", "");
169
170 /*
171 * allocation
172 */
173 RTTestSub(hTest, "RTStrAPrintf");
174 char *psz = (char *)~0;
175 int cch2 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!");
176 if (cch2 < 0)
177 RTTestIFailed("RTStrAPrintf failed, cch2=%d\n", cch2);
178 else if (strcmp(psz, "Hey there! This is a test!"))
179 RTTestIFailed("RTStrAPrintf failed\n"
180 "got : '%s'\n"
181 "wanted: 'Hey there! This is a test!'\n",
182 psz);
183 else if ((int)strlen(psz) != cch2)
184 RTTestIFailed("RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz));
185 RTStrFree(psz);
186
187#define CHECK42(fmt, arg, out) \
188 do { \
189 cch = RTStrPrintf(pszBuf, BUF_SIZE, fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \
190 if (strcmp(pszBuf, out " 42=42 " out " 42=42")) \
191 RTTestIFailed("at line %d: format '%s'\n" \
192 " output: '%s'\n" \
193 " wanted: '%s'\n", \
194 __LINE__, fmt, pszBuf, out " 42=42 " out " 42=42"); \
195 else if (cch != sizeof(out " 42=42 " out " 42=42") - 1) \
196 RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \
197 __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \
198 } while (0)
199
200#define CHECKSTR(Correct) \
201 if (strcmp(pszBuf, Correct)) \
202 RTTestIFailed("error: '%s'\n" \
203 "expected: '%s'\n", pszBuf, Correct); \
204
205 /*
206 * Runtime extensions.
207 */
208 RTTestSub(hTest, "Runtime format types (%R*)");
209 CHECK42("%RGi", (RTGCINT)127, "127");
210 CHECK42("%RGi", (RTGCINT)-586589, "-586589");
211
212 CHECK42("%RGp", (RTGCPHYS)0x0000000044505045, "0000000044505045");
213 CHECK42("%RGp", ~(RTGCPHYS)0, "ffffffffffffffff");
214
215 CHECK42("%RGu", (RTGCUINT)586589, "586589");
216 CHECK42("%RGu", (RTGCUINT)1, "1");
217 CHECK42("%RGu", (RTGCUINT)3000000000U, "3000000000");
218
219#if GC_ARCH_BITS == 32
220 CHECK42("%RGv", (RTGCUINTPTR)0, "00000000");
221 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffff");
222 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "84342134");
223#else
224 CHECK42("%RGv", (RTGCUINTPTR)0, "0000000000000000");
225 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffffffffffff");
226 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "0000000084342134");
227#endif
228
229 CHECK42("%RGx", (RTGCUINT)0x234, "234");
230 CHECK42("%RGx", (RTGCUINT)0xffffffff, "ffffffff");
231
232 CHECK42("%RRv", (RTRCUINTPTR)0, "00000000");
233 CHECK42("%RRv", ~(RTRCUINTPTR)0, "ffffffff");
234 CHECK42("%RRv", (RTRCUINTPTR)0x84342134, "84342134");
235
236 CHECK42("%RHi", (RTHCINT)127, "127");
237 CHECK42("%RHi", (RTHCINT)-586589, "-586589");
238
239 CHECK42("%RHp", (RTHCPHYS)0x0000000044505045, "0000000044505045");
240 CHECK42("%RHp", ~(RTHCPHYS)0, "ffffffffffffffff");
241
242 CHECK42("%RHu", (RTHCUINT)586589, "586589");
243 CHECK42("%RHu", (RTHCUINT)1, "1");
244 CHECK42("%RHu", (RTHCUINT)3000000000U, "3000000000");
245
246 if (sizeof(void*) == 8)
247 {
248 CHECK42("%RHv", (RTHCUINTPTR)0, "0000000000000000");
249 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffffffffffff");
250 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "0000000084342134");
251 }
252 else
253 {
254 CHECK42("%RHv", (RTHCUINTPTR)0, "00000000");
255 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffff");
256 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "84342134");
257 }
258
259 CHECK42("%RHx", (RTHCUINT)0x234, "234");
260 CHECK42("%RHx", (RTHCUINT)0xffffffff, "ffffffff");
261
262 CHECK42("%RI16", (int16_t)1, "1");
263 CHECK42("%RI16", (int16_t)-16384, "-16384");
264
265 CHECK42("%RI32", (int32_t)1123, "1123");
266 CHECK42("%RI32", (int32_t)-86596, "-86596");
267
268 CHECK42("%RI64", (int64_t)112345987345LL, "112345987345");
269 CHECK42("%RI64", (int64_t)-8659643985723459LL, "-8659643985723459");
270
271 CHECK42("%RI8", (int8_t)1, "1");
272 CHECK42("%RI8", (int8_t)-128, "-128");
273
274 CHECK42("%Rbn", "file.c", "file.c");
275 CHECK42("%Rbn", "foo/file.c", "file.c");
276 CHECK42("%Rbn", "/foo/file.c", "file.c");
277 CHECK42("%Rbn", "/dir/subdir/", "subdir/");
278
279 CHECK42("%Rfn", "function", "function");
280 CHECK42("%Rfn", "void function(void)", "function");
281
282 CHECK42("%RTfile", (RTFILE)127, "127");
283 CHECK42("%RTfile", (RTFILE)12341234, "12341234");
284
285 CHECK42("%RTfmode", (RTFMODE)0x123403, "00123403");
286
287 CHECK42("%RTfoff", (RTFOFF)12342312, "12342312");
288 CHECK42("%RTfoff", (RTFOFF)-123123123, "-123123123");
289 CHECK42("%RTfoff", (RTFOFF)858694596874568LL, "858694596874568");
290
291 RTFAR16 fp16;
292 fp16.off = 0x34ff;
293 fp16.sel = 0x0160;
294 CHECK42("%RTfp16", fp16, "0160:34ff");
295
296 RTFAR32 fp32;
297 fp32.off = 0xff094030;
298 fp32.sel = 0x0168;
299 CHECK42("%RTfp32", fp32, "0168:ff094030");
300
301 RTFAR64 fp64;
302 fp64.off = 0xffff003401293487ULL;
303 fp64.sel = 0x0ff8;
304 CHECK42("%RTfp64", fp64, "0ff8:ffff003401293487");
305 fp64.off = 0x0;
306 fp64.sel = 0x0;
307 CHECK42("%RTfp64", fp64, "0000:0000000000000000");
308
309 CHECK42("%RTgid", (RTGID)-1, "-1");
310 CHECK42("%RTgid", (RTGID)1004, "1004");
311
312 CHECK42("%RTino", (RTINODE)0, "0000000000000000");
313 CHECK42("%RTino", (RTINODE)0x123412341324ULL, "0000123412341324");
314
315 CHECK42("%RTint", (RTINT)127, "127");
316 CHECK42("%RTint", (RTINT)-586589, "-586589");
317 CHECK42("%RTint", (RTINT)-23498723, "-23498723");
318
319 CHECK42("%RTiop", (RTIOPORT)0x3c4, "03c4");
320 CHECK42("%RTiop", (RTIOPORT)0xffff, "ffff");
321
322 RTMAC Mac;
323 Mac.au8[0] = 0;
324 Mac.au8[1] = 0x1b;
325 Mac.au8[2] = 0x21;
326 Mac.au8[3] = 0x0a;
327 Mac.au8[4] = 0x1d;
328 Mac.au8[5] = 0xd9;
329 CHECK42("%RTmac", &Mac, "00:1b:21:0a:1d:d9");
330 Mac.au16[0] = 0xffff;
331 Mac.au16[1] = 0xffff;
332 Mac.au16[2] = 0xffff;
333 CHECK42("%RTmac", &Mac, "ff:ff:ff:ff:ff:ff");
334
335 RTNETADDRIPV4 Ipv4Addr;
336 Ipv4Addr.u = RT_H2N_U32_C(0xf040d003);
337 CHECK42("%RTnaipv4", Ipv4Addr.u, "240.64.208.3");
338 Ipv4Addr.u = RT_H2N_U32_C(0xffffffff);
339 CHECK42("%RTnaipv4", Ipv4Addr.u, "255.255.255.255");
340
341 RTNETADDRIPV6 Ipv6Addr;
342 Ipv6Addr.au16[0] = RT_H2N_U16_C(0x2001);
343 Ipv6Addr.au16[1] = RT_H2N_U16_C(0x0db8);
344 Ipv6Addr.au16[2] = RT_H2N_U16_C(0x85a3);
345 Ipv6Addr.au16[3] = RT_H2N_U16_C(0x0000);
346 Ipv6Addr.au16[4] = RT_H2N_U16_C(0x0000);
347 Ipv6Addr.au16[5] = RT_H2N_U16_C(0x8a2e);
348 Ipv6Addr.au16[6] = RT_H2N_U16_C(0x0370);
349 Ipv6Addr.au16[7] = RT_H2N_U16_C(0x7334);
350 CHECK42("%RTnaipv6", &Ipv6Addr, "2001:0db8:85a3:0000:0000:8a2e:0370:7334");
351 Ipv6Addr.au64[0] = UINT64_MAX;
352 Ipv6Addr.au64[1] = UINT64_MAX;
353 CHECK42("%RTnaipv6", &Ipv6Addr, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
354
355 CHECK42("%RTproc", (RTPROCESS)0xffffff, "00ffffff");
356 CHECK42("%RTproc", (RTPROCESS)0x43455443, "43455443");
357
358 if (sizeof(RTUINTPTR) == 8)
359 {
360 CHECK42("%RTptr", (RTUINTPTR)0, "0000000000000000");
361 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffffffffffff");
362 CHECK42("%RTptr", (RTUINTPTR)0x84342134, "0000000084342134");
363 }
364 else
365 {
366 CHECK42("%RTptr", (RTUINTPTR)0, "00000000");
367 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffff");
368 CHECK42("%RTptr", (RTUINTPTR)0x84342134, "84342134");
369 }
370
371 if (sizeof(RTCCUINTREG) == 8)
372 {
373 CHECK42("%RTreg", (RTCCUINTREG)0, "0000000000000000");
374 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffffffffffff");
375 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "0000000084342134");
376 CHECK42("%RTreg", (RTCCUINTREG)0x23484342134ULL, "0000023484342134");
377 }
378 else
379 {
380 CHECK42("%RTreg", (RTCCUINTREG)0, "00000000");
381 CHECK42("%RTreg", ~(RTCCUINTREG)0, "ffffffff");
382 CHECK42("%RTreg", (RTCCUINTREG)0x84342134, "84342134");
383 }
384
385 CHECK42("%RTsel", (RTSEL)0x543, "0543");
386 CHECK42("%RTsel", (RTSEL)0xf8f8, "f8f8");
387
388 if (sizeof(RTSEMEVENT) == 8)
389 {
390 CHECK42("%RTsem", (RTSEMEVENT)0, "0000000000000000");
391 CHECK42("%RTsem", (RTSEMEVENT)0x23484342134ULL, "0000023484342134");
392 }
393 else
394 {
395 CHECK42("%RTsem", (RTSEMEVENT)0, "00000000");
396 CHECK42("%RTsem", (RTSEMEVENT)0x84342134, "84342134");
397 }
398
399 CHECK42("%RTsock", (RTSOCKET)12234, "12234");
400 CHECK42("%RTsock", (RTSOCKET)584854543, "584854543");
401
402 if (sizeof(RTTHREAD) == 8)
403 {
404 CHECK42("%RTthrd", (RTTHREAD)0, "0000000000000000");
405 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffffffffffff");
406 CHECK42("%RTthrd", (RTTHREAD)0x63484342134ULL, "0000063484342134");
407 }
408 else
409 {
410 CHECK42("%RTthrd", (RTTHREAD)0, "00000000");
411 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffff");
412 CHECK42("%RTthrd", (RTTHREAD)0x54342134, "54342134");
413 }
414
415 CHECK42("%RTuid", (RTUID)-2, "-2");
416 CHECK42("%RTuid", (RTUID)90344, "90344");
417
418 CHECK42("%RTuint", (RTUINT)584589, "584589");
419 CHECK42("%RTuint", (RTUINT)3, "3");
420 CHECK42("%RTuint", (RTUINT)2400000000U, "2400000000");
421
422 RTUuidCreate(&Uuid);
423 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
424 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
425 if (strcmp(pszBuf, szCorrect))
426 RTTestIFailed("error: '%s'\n"
427 "expected: '%s'\n",
428 pszBuf, szCorrect);
429
430 CHECK42("%RTxint", (RTUINT)0x2345, "2345");
431 CHECK42("%RTxint", (RTUINT)0xffff8fff, "ffff8fff");
432
433 CHECK42("%RU16", (uint16_t)7, "7");
434 CHECK42("%RU16", (uint16_t)46384, "46384");
435
436 CHECK42("%RU32", (uint32_t)1123, "1123");
437 CHECK42("%RU32", (uint32_t)86596, "86596");
438
439 CHECK42("%RU64", (uint64_t)112345987345ULL, "112345987345");
440 CHECK42("%RU64", (uint64_t)8659643985723459ULL, "8659643985723459");
441
442 CHECK42("%RU8", (uint8_t)1, "1");
443 CHECK42("%RU8", (uint8_t)254, "254");
444 CHECK42("%RU8", 256, "0");
445
446 CHECK42("%RX16", (uint16_t)0x7, "7");
447 CHECK42("%RX16", 0x46384, "6384");
448
449 CHECK42("%RX32", (uint32_t)0x1123, "1123");
450 CHECK42("%RX32", (uint32_t)0x49939493, "49939493");
451
452 CHECK42("%RX64", (uint64_t)0x348734, "348734");
453 CHECK42("%RX64", (uint64_t)0x12312312312343fULL, "12312312312343f");
454
455 CHECK42("%RX8", (uint8_t)1, "1");
456 CHECK42("%RX8", (uint8_t)0xff, "ff");
457 CHECK42("%RX8", 0x100, "0");
458
459 /*
460 * Thousand separators.
461 */
462 RTTestSub(hTest, "Thousand Separators (%'*)");
463
464 RTStrFormatNumber(pszBuf, 1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1"); memset(pszBuf, '!', BUF_SIZE);
465 RTStrFormatNumber(pszBuf, 10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10"); memset(pszBuf, '!', BUF_SIZE);
466 RTStrFormatNumber(pszBuf, 100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100"); memset(pszBuf, '!', BUF_SIZE);
467 RTStrFormatNumber(pszBuf, 1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000"); memset(pszBuf, '!', BUF_SIZE);
468 RTStrFormatNumber(pszBuf, 10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000"); memset(pszBuf, '!', BUF_SIZE);
469 RTStrFormatNumber(pszBuf, 100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000"); memset(pszBuf, '!', BUF_SIZE);
470 RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000"); memset(pszBuf, '!', BUF_SIZE);
471
472 CHECK42("%'u", 1, "1");
473 CHECK42("%'u", 10, "10");
474 CHECK42("%'u", 100, "100");
475 CHECK42("%'u", 1000, "1 000");
476 CHECK42("%'u", 10000, "10 000");
477 CHECK42("%'u", 100000, "100 000");
478 CHECK42("%'u", 1000000, "1 000 000");
479 CHECK42("%'RU64", _1T, "1 099 511 627 776");
480 CHECK42("%'RU64", _1E, "1 152 921 504 606 846 976");
481
482 /*
483 * String formatting.
484 */
485 RTTestSub(hTest, "String formatting (%s)");
486
487// 0 1 2 3 4 5 6 7
488// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
489 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "args", "description");
490 CHECKSTR("cmd args description");
491
492 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "", "description");
493 CHECKSTR("cmd description");
494
495
496 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%*s", 0, "");
497 CHECKSTR("");
498
499 /* automatic conversions. */
500 static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
501 static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
502
503 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz1);
504 CHECKSTR("hello world");
505 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz1);
506 CHECKSTR("hello world");
507
508 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5ls", s_wsz1);
509 CHECKSTR("hello");
510 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5Ls", s_usz1);
511 CHECKSTR("hello");
512
513 /*
514 * Unicode string formatting.
515 */
516 RTTestSub(hTest, "Unicode string formatting (%ls)");
517 static RTUTF16 s_wszEmpty[] = { 0 }; //assumes ascii.
518 static RTUTF16 s_wszCmd[] = { 'c', 'm', 'd', 0 }; //assumes ascii.
519 static RTUTF16 s_wszArgs[] = { 'a', 'r', 'g', 's', 0 }; //assumes ascii.
520 static RTUTF16 s_wszDesc[] = { 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 0 }; //assumes ascii.
521
522// 0 1 2 3 4 5 6 7
523// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
524 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc);
525 CHECKSTR("cmd args description");
526
527 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc);
528 CHECKSTR("cmd description");
529
530
531#if 0
532 static RTUNICP s_usz2[] = { 0xc5, 0xc6, 0xf8, 0 };
533 static RTUTF16 s_wsz2[] = { 0xc5, 0xc6, 0xf8, 0 };
534 static char s_sz2[] = { 0xc5, 0xc6, 0xf8, 0 };///@todo multibyte tests.
535
536 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz2);
537 CHECKSTR(s_sz2);
538 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz2);
539 CHECKSTR(s_sz2);
540#endif
541
542 /*
543 * Custom types.
544 */
545 RTTestSub(hTest, "Custom format types (%R[*])");
546 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
547 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
548 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)1);
549 CHECKSTR("type3=1");
550
551 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
552 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
553 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)1, (void *)2);
554 CHECKSTR("type3=1 type1=2");
555
556 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
557 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
558 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
559 CHECKSTR("type3=1 type1=2 type4=3");
560
561 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
562 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
563 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
564 CHECKSTR("type3=1 type1=2 type4=3 type2=4");
565
566 RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
567 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
568 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);
569 CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5");
570
571 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1", (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
572 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2", (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
573 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3", (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
574 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4", (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
575 RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5", (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
576
577 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);
578 CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50");
579
580 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS);
581 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
582 CHECKSTR("type3=10 type1=20 type4=30 type5=40");
583
584 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS);
585 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
586 CHECKSTR("type3=10 type1=20 type4=30");
587
588 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS);
589 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)10, (void *)20);
590 CHECKSTR("type3=10 type1=20");
591
592 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS);
593 cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)10);
594 CHECKSTR("type3=10");
595
596 RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
597
598 /*
599 * Summarize and exit.
600 */
601 return RTTestSummaryAndDestroy(hTest);
602}
603
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