VirtualBox

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

Last change on this file since 584 was 1, checked in by vboxsync, 55 years ago

import

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