2 * Conformance tests for *printf functions.
4 * Copyright 2002 Uwe Bonnes
5 * Copyright 2004 Aneurin Price
6 * Copyright 2005 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/test.h"
27 static void test_sprintf( void )
31 double pnumber
=789456123;
33 WCHAR wide
[] = { 'w','i','d','e',0};
36 r
= sprintf(buffer
,format
,pnumber
);
38 ok(!strcmp(buffer
,"+7.894561230000000e+008"),"exponent format incorrect\n");
40 ok( r
==23, "return count wrong\n");
44 r
= sprintf(buffer
,format
,((ULONGLONG
)0xffffffff)*0xffffffff);
45 ok(!strcmp(buffer
,"-8589934591"),"Problem with long long\n");
46 ok( r
==11, "return count wrong\n");
50 r
= sprintf(buffer
,format
,((ULONGLONG
)0xffffffff)*0xffffffff);
51 ok(!strcmp(buffer
, "1"), "Problem with \"ll\" interpretation\n");
52 ok( r
==1, "return count wrong\n");
55 r
= sprintf(buffer
,format
,wide
);
56 ok(!strcmp(buffer
,"wide"),"Problem with wide string format\n");
57 ok( r
==4, "return count wrong\n");
60 r
= sprintf(buffer
,format
,'1');
61 ok(!strcmp(buffer
,"0001"),"Character not zero-prefixed \"%s\"\n",buffer
);
62 ok( r
==4, "return count wrong\n");
65 r
= sprintf(buffer
,format
,(void *)57);
66 ok(!strcmp(buffer
,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
67 ok( r
==8, "return count wrong\n");
70 r
= sprintf(buffer
,format
,(void *)57);
71 ok(!strcmp(buffer
," 0X00000039"),"Pointer formatted incorrectly\n");
72 ok( r
==12, "return count wrong\n");
75 r
= sprintf(buffer
,format
,(void *)57);
76 ok(!strcmp(buffer
,"00000039"),"Pointer formatted incorrectly \"%s\"\n",buffer
);
77 ok( r
==8, "return count wrong\n");
80 r
= sprintf(buffer
,format
,"foo");
81 ok(!strcmp(buffer
,"0foo"),"String not zero-prefixed \"%s\"\n",buffer
);
82 ok( r
==4, "return count wrong\n");
85 r
= sprintf(buffer
,format
,"foo");
86 ok(!strcmp(buffer
,"f"),"Precision ignored \"%s\"\n",buffer
);
87 ok( r
==1, "return count wrong\n");
90 r
= sprintf(buffer
,format
,1,"foo");
91 ok(!strcmp(buffer
,"f"),"Precision ignored \"%s\"\n",buffer
);
92 ok( r
==1, "return count wrong\n");
95 r
= sprintf(buffer
,format
,(void *)57);
96 ok(!strcmp(buffer
,"0X00000039 "),"Pointer formatted incorrectly\n");
97 ok( r
==12, "return count wrong\n");
100 r
= sprintf(buffer
, format
);
101 ok(!strcmp(buffer
,"hello"), "failed\n");
102 ok( r
==5, "return count wrong\n");
105 r
= sprintf(buffer
, format
, wide
);
106 ok(!strcmp(buffer
,"wide"), "failed\n");
107 ok( r
==4, "return count wrong\n");
110 r
= sprintf(buffer
, format
, wide
);
111 ok(!strcmp(buffer
,"wide "), "failed\n");
112 ok( r
==10, "return count wrong\n");
115 r
= sprintf(buffer
, format
, wide
);
116 ok(!strcmp(buffer
," wide"), "failed\n");
117 ok( r
==10, "return count wrong\n");
119 format
= "%#+ -03whlls";
120 r
= sprintf(buffer
, format
, wide
);
121 ok(!strcmp(buffer
,"wide"), "failed\n");
122 ok( r
==4, "return count wrong\n");
125 r
= sprintf(buffer
, format
, wide
);
126 ok(!strcmp(buffer
,"0s"), "failed\n");
127 ok( r
==2, "return count wrong\n");
130 r
= sprintf(buffer
, format
, wide
);
131 ok(!strcmp(buffer
,"-s"), "failed\n");
132 ok( r
==2, "return count wrong\n");
135 r
= sprintf(buffer
, format
, wide
);
136 ok(!strcmp(buffer
,"wide"), "failed\n");
137 ok( r
==4, "return count wrong\n");
140 r
= sprintf(buffer
, format
, "not wide" );
141 ok(!strcmp(buffer
,"not wide"), "failed\n");
142 ok( r
==8, "return count wrong\n");
145 r
= sprintf(buffer
, format
);
146 ok(!strcmp(buffer
,"b"), "failed\n");
147 ok( r
==1, "return count wrong\n");
150 r
= sprintf(buffer
, format
,'a');
151 ok(!strcmp(buffer
," a"), "failed\n");
152 ok( r
==3, "return count wrong\n");
155 r
= sprintf(buffer
, format
,1234);
156 ok(!strcmp(buffer
,"1234"), "failed\n");
157 ok( r
==4, "return count wrong\n");
160 r
= sprintf(buffer
, format
);
161 ok(!strcmp(buffer
,""), "failed\n");
162 ok( r
==0, "return count wrong\n");
164 format
= "%j%k%m%q%r%t%v%y%z";
165 r
= sprintf(buffer
, format
);
166 ok(!strcmp(buffer
,"jkmqrtvyz"), "failed\n");
167 ok( r
==9, "return count wrong\n");
171 r
= sprintf(buffer
, format
, &x
);
172 ok(x
== 4, "should write to x\n");
173 ok(!strcmp(buffer
,"asdf"), "failed\n");
174 ok( r
==4, "return count wrong\n");
177 r
= sprintf(buffer
, format
,2);
178 ok(!strcmp(buffer
,"2"), "failed\n");
179 ok( r
==1, "return count wrong\n");
182 r
= sprintf(buffer
, format
,8.6);
183 ok(!strcmp(buffer
,"8.6000"), "failed\n");
184 ok( r
==6, "return count wrong\n");
187 r
= sprintf(buffer
, format
,0.6);
188 ok(!strcmp(buffer
,"0.600000"), "failed\n");
189 ok( r
==8, "return count wrong\n");
192 r
= sprintf(buffer
, format
,0.6);
193 ok(!strcmp(buffer
,"1"), "failed\n");
194 ok( r
==1, "return count wrong\n");
198 r
= sprintf(buffer
, format
,8.6);
199 ok(!strcmp(buffer
,"8.6000e+000"), "failed\n");
200 ok( r
==11, "return count wrong\n");
204 r
= sprintf(buffer
, format
,8.6);
205 ok(!strcmp(buffer
,"8.6"), "failed\n");
206 ok( r
==3, "return count wrong\n");
209 r
= sprintf(buffer
, format
,-1);
210 ok(!strcmp(buffer
,"-1"), "failed\n");
211 ok( r
==2, "return count wrong\n");
214 r
= sprintf(buffer
, format
,1);
215 ok(!strcmp(buffer
,"1"), "failed\n");
216 ok( r
==1, "return count wrong\n");
219 r
= sprintf(buffer
, format
,1);
220 ok(!strcmp(buffer
,"+1"), "failed\n");
221 ok( r
==2, "return count wrong\n");
224 r
= sprintf(buffer
, format
,10);
225 ok(!strcmp(buffer
,"12"), "failed\n");
226 ok( r
==2, "return count wrong\n");
229 r
= sprintf(buffer
, format
,0);
230 ok(!strcmp(buffer
,"00000000"), "failed\n");
231 ok( r
==8, "return count wrong\n");
234 r
= sprintf(buffer
, format
,0);
235 ok(!strcmp(buffer
,"(null)"), "failed\n");
236 ok( r
==6, "return count wrong\n");
239 r
= sprintf(buffer
, format
,"%%%%");
240 ok(!strcmp(buffer
,"%%%%"), "failed\n");
241 ok( r
==4, "return count wrong\n");
244 r
= sprintf(buffer
, format
,-1);
245 ok(!strcmp(buffer
,"4294967295"), "failed\n");
246 ok( r
==10, "return count wrong\n");
249 r
= sprintf(buffer
, format
,-1);
250 ok(!strcmp(buffer
,""), "failed\n");
251 ok( r
==0, "return count wrong\n");
254 r
= sprintf(buffer
, format
,-1);
255 ok(!strcmp(buffer
,""), "failed\n");
256 ok( r
==0, "return count wrong\n");
259 r
= sprintf(buffer
, format
,-1);
260 ok(!strcmp(buffer
,"z"), "failed\n");
261 ok( r
==1, "return count wrong\n");
264 r
= sprintf(buffer
, format
,-1);
265 ok(!strcmp(buffer
,"j"), "failed\n");
266 ok( r
==1, "return count wrong\n");
269 r
= sprintf(buffer
, format
,-1);
270 ok(!strcmp(buffer
,""), "failed\n");
271 ok( r
==0, "return count wrong\n");
274 r
= sprintf(buffer
, format
,-1);
275 ok(!strcmp(buffer
,"H"), "failed\n");
276 ok( r
==1, "return count wrong\n");
279 r
= sprintf(buffer
, format
, 0x100+'X');
280 ok(!strcmp(buffer
,"xXx"), "failed\n");
281 ok( r
==3, "return count wrong\n");
284 r
= sprintf(buffer
, format
);
285 ok(!strcmp(buffer
,"%0"), "failed: \"%s\"\n", buffer
);
286 ok( r
==2, "return count wrong\n");
289 static void test_swprintf( void )
292 const wchar_t I64d
[] = {'%','I','6','4','d',0};
293 double pnumber
=789456123;
294 const wchar_t TwentyThreePoint15e
[]= {'%','+','#','2','3','.','1','5','e',0};
295 const wchar_t e008
[] = {'e','+','0','0','8',0};
296 const char string
[]={'s','t','r','i','n','g',0};
297 const wchar_t S
[]={'%','S',0};
298 swprintf(buffer
,TwentyThreePoint15e
,pnumber
);
301 ok(wcsstr(buffer
,e008
) != 0,"Sprintf different\n");
303 swprintf(buffer
,I64d
,((ULONGLONG
)0xffffffff)*0xffffffff);
306 ok(wcslen(buffer
) == 11,"Problem with long long\n");
308 swprintf(buffer
,S
,string
);
309 ok(wcslen(buffer
) == 6,"Problem with \"%%S\" interpretation\n");
312 static void test_fwprintf( void )
314 const char *string
="not a wide string";
317 ok(fwprintf(fopen("nul","r+"),(wchar_t *)string
) == -1,"Non-wide string should not be printed by fwprintf\n");
321 static void test_snprintf (void)
323 struct snprintf_test
{
331 /* Pre-2.1 libc behaviour, not C99 compliant. */
332 const struct snprintf_test tests
[] = {{"short", 5, {0, 0}},
333 {"justfit", 7, {0, 0}},
334 {"justfits", 8, {0, 1}},
335 {"muchlonger", -1, {1, 1}}};
337 const int bufsiz
= sizeof buffer
;
340 for (i
= 0; i
< sizeof tests
/ sizeof tests
[0]; i
++) {
341 const char *fmt
= tests
[i
].format
;
342 const int expect
= tests
[i
].expected
;
343 const int n
= _snprintf (buffer
, bufsiz
, fmt
);
344 const int valid
= n
< 0 ? bufsiz
: (n
== bufsiz
? n
: n
+1);
346 todo (tests
[i
].todo
.retval
? "wine" : "none")
347 ok (n
== expect
, "\"%s\": expected %d, returned %d\n",
349 todo (tests
[i
].todo
.render
? "wine" : "none")
350 ok (!memcmp (fmt
, buffer
, valid
),
351 "\"%s\": rendered \"%.*s\"\n", fmt
, valid
, buffer
);