VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstStrFormat.cpp@ 6677

Last change on this file since 6677 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.5 KB
Line 
1/* $Id: tstStrFormat.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - String formatting.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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#include <iprt/runtime.h>
32#include <iprt/uuid.h>
33#include <iprt/string.h>
34#include <iprt/stream.h>
35
36int main()
37{
38 RTR3Init();
39
40 int cErrors = 0;
41 uint32_t u32 = 0x010;
42 uint64_t u64 = 0x100;
43 char szStr[120];
44
45 /* simple */
46 size_t cch = RTStrPrintf(szStr, sizeof(szStr), "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
47 if (strcmp(szStr, "u32=16 u64=256 u64=0x100"))
48 {
49 RTPrintf("error: '%s'\n"
50 "wanted 'u32=16 u64=256 u64=0x100'\n", szStr);
51 cErrors++;
52 }
53
54 /* just big. */
55 u64 = UINT64_C(0x7070605040302010);
56 cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
57 if (strcmp(szStr, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
58 {
59 RTPrintf("error: '%s'\n"
60 "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", szStr);
61 RTPrintf("%d\n", (int)(u64 % 10));
62 cErrors++;
63 }
64
65 /* huge and negative. */
66 u64 = UINT64_C(0x8070605040302010);
67 cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
68 /* Not sure if this is the correct decimal representation... But both */
69 if (strcmp(szStr, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
70 {
71 RTPrintf("error: '%s'\n"
72 "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", szStr);
73 RTPrintf("%d\n", (int)(u64 % 10));
74 cErrors++;
75 }
76
77 /* 64-bit value bug. */
78 u64 = 0xa0000000;
79 cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
80 if (strcmp(szStr, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
81 {
82 RTPrintf("error: '%s'\n"
83 "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", szStr);
84 cErrors++;
85 }
86
87 /* uuid */
88 RTUUID Uuid;
89 RTUuidCreate(&Uuid);
90 char szCorrect[RTUUID_STR_LENGTH];
91 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
92 cch = RTStrPrintf(szStr, sizeof(szStr), "%Vuuid", &Uuid);
93 if (strcmp(szStr, szCorrect))
94 {
95 RTPrintf("error: '%s'\n"
96 "expected: '%s'\n",
97 szStr, szCorrect);
98 cErrors++;
99 }
100
101 /* allocation */
102 char *psz = (char *)~0;
103 int cch2 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!");
104 if (cch2 < 0)
105 {
106 RTPrintf("error: RTStrAPrintf failed, cch2=%d\n", cch2);
107 cErrors++;
108 }
109 else if (strcmp(psz, "Hey there! This is a test!"))
110 {
111 RTPrintf("error: RTStrAPrintf failed\n"
112 "got : '%s'\n"
113 "wanted: 'Hey there! This is a test!'\n",
114 psz);
115 cErrors++;
116 }
117 else if ((int)strlen(psz) != cch2)
118 {
119 RTPrintf("error: RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz));
120 cErrors++;
121 }
122 RTStrFree(psz);
123
124#define CHECK42(fmt, arg, out) \
125 do { \
126 cch = RTStrPrintf(szStr, sizeof(szStr), fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \
127 if (strcmp(szStr, out " 42=42 " out " 42=42")) \
128 { \
129 RTPrintf("error(%d): format '%s'\n" \
130 " output: '%s'\n" \
131 " wanted: '%s'\n", \
132 __LINE__, fmt, szStr, out " 42=42 " out " 42=42"); \
133 cErrors++; \
134 } \
135 else if (cch != sizeof(out " 42=42 " out " 42=42") - 1) \
136 { \
137 RTPrintf("error(%d): Invalid length %d returned, expected %u!\n", \
138 __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \
139 cErrors++; \
140 } \
141 } while (0)
142
143 /*
144 * Runtime extensions.
145 */
146 CHECK42("%RGi", (RTGCINT)127, "127");
147 CHECK42("%RGi", (RTGCINT)-586589, "-586589");
148
149 CHECK42("%RGp", (RTGCPHYS)0x44505045, "44505045");
150 CHECK42("%RGp", ~(RTGCPHYS)0, "ffffffff");
151
152 CHECK42("%RGu", (RTGCUINT)586589, "586589");
153 CHECK42("%RGu", (RTGCUINT)1, "1");
154 CHECK42("%RGu", (RTGCUINT)3000000000U, "3000000000");
155
156 CHECK42("%RGv", (RTGCUINTPTR)0, "00000000");
157 CHECK42("%RGv", ~(RTGCUINTPTR)0, "ffffffff");
158 CHECK42("%RGv", (RTGCUINTPTR)0x84342134, "84342134");
159
160 CHECK42("%RGx", (RTGCUINT)0x234, "234");
161 CHECK42("%RGx", (RTGCUINT)0xffffffff, "ffffffff");
162
163 CHECK42("%RHi", (RTHCINT)127, "127");
164 CHECK42("%RHi", (RTHCINT)-586589, "-586589");
165
166 CHECK42("%RHp", (RTHCPHYS)0x0000000044505045, "0000000044505045");
167 CHECK42("%RHp", ~(RTHCPHYS)0, "ffffffffffffffff");
168
169 CHECK42("%RHu", (RTHCUINT)586589, "586589");
170 CHECK42("%RHu", (RTHCUINT)1, "1");
171 CHECK42("%RHu", (RTHCUINT)3000000000U, "3000000000");
172
173 if (sizeof(void*) == 8)
174 {
175 CHECK42("%RHv", (RTHCUINTPTR)0, "0000000000000000");
176 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffffffffffff");
177 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "0000000084342134");
178 }
179 else
180 {
181 CHECK42("%RHv", (RTHCUINTPTR)0, "00000000");
182 CHECK42("%RHv", ~(RTHCUINTPTR)0, "ffffffff");
183 CHECK42("%RHv", (RTHCUINTPTR)0x84342134, "84342134");
184 }
185
186 CHECK42("%RHx", (RTHCUINT)0x234, "234");
187 CHECK42("%RHx", (RTHCUINT)0xffffffff, "ffffffff");
188
189 CHECK42("%RI16", (int16_t)1, "1");
190 CHECK42("%RI16", (int16_t)-16384, "-16384");
191
192 CHECK42("%RI32", (int32_t)1123, "1123");
193 CHECK42("%RI32", (int32_t)-86596, "-86596");
194
195 CHECK42("%RI64", (int64_t)112345987345LL, "112345987345");
196 CHECK42("%RI64", (int64_t)-8659643985723459LL, "-8659643985723459");
197
198 CHECK42("%RI8", (int8_t)1, "1");
199 CHECK42("%RI8", (int8_t)-128, "-128");
200
201 CHECK42("%RTfile", (RTFILE)127, "127");
202 CHECK42("%RTfile", (RTFILE)12341234, "12341234");
203
204 CHECK42("%RTfmode", (RTFMODE)0x123403, "00123403");
205
206 CHECK42("%RTfoff", (RTFOFF)12342312, "12342312");
207 CHECK42("%RTfoff", (RTFOFF)-123123123, "-123123123");
208 CHECK42("%RTfoff", (RTFOFF)858694596874568LL, "858694596874568");
209
210 RTFAR16 fp16;
211 fp16.off = 0x34ff;
212 fp16.sel = 0x0160;
213 CHECK42("%RTfp16", fp16, "0160:34ff");
214
215 RTFAR32 fp32;
216 fp32.off = 0xff094030;
217 fp32.sel = 0x0168;
218 CHECK42("%RTfp32", fp32, "0168:ff094030");
219
220 RTFAR64 fp64;
221 fp64.off = 0xffff003401293487ULL;
222 fp64.sel = 0x0ff8;
223 CHECK42("%RTfp64", fp64, "0ff8:ffff003401293487");
224 fp64.off = 0x0;
225 fp64.sel = 0x0;
226 CHECK42("%RTfp64", fp64, "0000:0000000000000000");
227
228 CHECK42("%RTgid", (RTGID)-1, "-1");
229 CHECK42("%RTgid", (RTGID)1004, "1004");
230
231 CHECK42("%RTino", (RTINODE)0, "0000000000000000");
232 CHECK42("%RTino", (RTINODE)0x123412341324ULL, "0000123412341324");
233
234 CHECK42("%RTint", (RTINT)127, "127");
235 CHECK42("%RTint", (RTINT)-586589, "-586589");
236 CHECK42("%RTint", (RTINT)-23498723, "-23498723");
237
238 CHECK42("%RTiop", (RTIOPORT)0x3c4, "03c4");
239 CHECK42("%RTiop", (RTIOPORT)0xffff, "ffff");
240
241 CHECK42("%RTproc", (RTPROCESS)0xffffff, "00ffffff");
242 CHECK42("%RTproc", (RTPROCESS)0x43455443, "43455443");
243
244 if (sizeof(RTUINTPTR) == 8)
245 {
246 CHECK42("%RTptr", (RTUINTPTR)0, "0000000000000000");
247 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffffffffffff");
248 CHECK42("%RTptr", (RTUINTPTR)0x84342134, "0000000084342134");
249 }
250 else
251 {
252 CHECK42("%RTptr", (RTUINTPTR)0, "00000000");
253 CHECK42("%RTptr", ~(RTUINTPTR)0, "ffffffff");
254 CHECK42("%RTptr", (RTUINTPTR)0x84342134, "84342134");
255 }
256
257 if (sizeof(RTUINTREG) == 8)
258 {
259 CHECK42("%RTreg", (RTUINTREG)0, "0000000000000000");
260 CHECK42("%RTreg", ~(RTUINTREG)0, "ffffffffffffffff");
261 CHECK42("%RTreg", (RTUINTREG)0x84342134, "0000000084342134");
262 CHECK42("%RTreg", (RTUINTREG)0x23484342134ULL, "0000023484342134");
263 }
264 else
265 {
266 CHECK42("%RTreg", (RTUINTREG)0, "00000000");
267 CHECK42("%RTreg", ~(RTUINTREG)0, "ffffffff");
268 CHECK42("%RTreg", (RTUINTREG)0x84342134, "84342134");
269 }
270
271 CHECK42("%RTsel", (RTSEL)0x543, "0543");
272 CHECK42("%RTsel", (RTSEL)0xf8f8, "f8f8");
273
274 if (sizeof(RTSEMEVENT) == 8)
275 {
276 CHECK42("%RTsem", (RTSEMEVENT)0, "0000000000000000");
277 CHECK42("%RTsem", (RTSEMEVENT)0x23484342134ULL, "0000023484342134");
278 }
279 else
280 {
281 CHECK42("%RTsem", (RTSEMEVENT)0, "00000000");
282 CHECK42("%RTsem", (RTSEMEVENT)0x84342134, "84342134");
283 }
284
285 CHECK42("%RTsock", (RTSOCKET)12234, "12234");
286 CHECK42("%RTsock", (RTSOCKET)584854543, "584854543");
287
288 if (sizeof(RTTHREAD) == 8)
289 {
290 CHECK42("%RTthrd", (RTTHREAD)0, "0000000000000000");
291 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffffffffffff");
292 CHECK42("%RTthrd", (RTTHREAD)0x63484342134ULL, "0000063484342134");
293 }
294 else
295 {
296 CHECK42("%RTthrd", (RTTHREAD)0, "00000000");
297 CHECK42("%RTthrd", (RTTHREAD)~(uintptr_t)0, "ffffffff");
298 CHECK42("%RTthrd", (RTTHREAD)0x54342134, "54342134");
299 }
300
301 CHECK42("%RTuid", (RTUID)-2, "-2");
302 CHECK42("%RTuid", (RTUID)90344, "90344");
303
304 CHECK42("%RTuint", (RTGCUINT)584589, "584589");
305 CHECK42("%RTuint", (RTGCUINT)3, "3");
306 CHECK42("%RTuint", (RTGCUINT)2400000000U, "2400000000");
307
308 RTUuidCreate(&Uuid);
309 RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
310 cch = RTStrPrintf(szStr, sizeof(szStr), "%RTuuid", &Uuid);
311 if (strcmp(szStr, szCorrect))
312 {
313 RTPrintf("error: '%s'\n"
314 "expected: '%s'\n",
315 szStr, szCorrect);
316 cErrors++;
317 }
318
319 CHECK42("%RTxint", (RTGCUINT)0x2345, "2345");
320 CHECK42("%RTxint", (RTGCUINT)0xffff8fff, "ffff8fff");
321
322 CHECK42("%RU16", (uint16_t)7, "7");
323 CHECK42("%RU16", (uint16_t)46384, "46384");
324
325 CHECK42("%RU32", (uint32_t)1123, "1123");
326 CHECK42("%RU32", (uint32_t)86596, "86596");
327
328 CHECK42("%RU64", (uint64_t)112345987345ULL, "112345987345");
329 CHECK42("%RU64", (uint64_t)8659643985723459ULL, "8659643985723459");
330
331 CHECK42("%RU8", (uint8_t)1, "1");
332 CHECK42("%RU8", (uint8_t)254, "254");
333 CHECK42("%RU8", 256, "0");
334
335 CHECK42("%RX16", (uint16_t)0x7, "7");
336 CHECK42("%RX16", 0x46384, "6384");
337
338 CHECK42("%RX32", (uint32_t)0x1123, "1123");
339 CHECK42("%RX32", (uint32_t)0x49939493, "49939493");
340
341 CHECK42("%RX64", (uint64_t)0x348734, "348734");
342 CHECK42("%RX64", (uint64_t)0x12312312312343fULL, "12312312312343f");
343
344 CHECK42("%RX8", (uint8_t)1, "1");
345 CHECK42("%RX8", (uint8_t)0xff, "ff");
346 CHECK42("%RX8", 0x100, "0");
347
348#define CHECKSTR(Correct) \
349 if (strcmp(szStr, Correct)) \
350 { \
351 RTPrintf("error: '%s'\n" \
352 "expected: '%s'\n", szStr, Correct); \
353 cErrors++; \
354 }
355
356 /*
357 * String formatting.
358 */
359// 0 1 2 3 4 5 6 7
360// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
361 cch = RTStrPrintf(szStr, sizeof(szStr), "%-10s %-30s %s", "cmd", "args", "description");
362 CHECKSTR("cmd args description");
363
364 cch = RTStrPrintf(szStr, sizeof(szStr), "%-10s %-30s %s", "cmd", "", "description");
365 CHECKSTR("cmd description");
366
367
368 cch = RTStrPrintf(szStr, sizeof(szStr), "%*s", 0, "");
369 CHECKSTR("");
370
371 /* automatic conversions. */
372 static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
373 static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
374
375 cch = RTStrPrintf(szStr, sizeof(szStr), "%ls", s_wsz1);
376 CHECKSTR("hello world");
377 cch = RTStrPrintf(szStr, sizeof(szStr), "%Ls", s_usz1);
378 CHECKSTR("hello world");
379
380 cch = RTStrPrintf(szStr, sizeof(szStr), "%.5ls", s_wsz1);
381 CHECKSTR("hello");
382 cch = RTStrPrintf(szStr, sizeof(szStr), "%.5Ls", s_usz1);
383 CHECKSTR("hello");
384
385#if 0
386 static RTUNICP s_usz2[] = { 0xc5, 0xc6, 0xf8, 0 };
387 static RTUTF16 s_wsz2[] = { 0xc5, 0xc6, 0xf8, 0 };
388 static char s_sz2[] = { 0xc5, 0xc6, 0xf8, 0 };///@todo multibyte tests.
389
390 cch = RTStrPrintf(szStr, sizeof(szStr), "%ls", s_wsz2);
391 CHECKSTR(s_sz2);
392 cch = RTStrPrintf(szStr, sizeof(szStr), "%Ls", s_usz2);
393 CHECKSTR(s_sz2);
394#endif
395
396
397 /*
398 * Summarize and exit.
399 */
400 if (!cErrors)
401 RTPrintf("tstStrFormat: SUCCESS\n");
402 else
403 RTPrintf("tstStrFormat: FAILED - %d errors\n", cErrors);
404 return !!cErrors;
405}
406
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