VirtualBox

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

Last change on this file since 24639 was 23961, checked in by vboxsync, 15 years ago

IPRT: Three new format types: %RTmac, %RTnaipv4 and %RTnapiv6.

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