2 * Unit test suite for *scanf functions.
4 * Copyright 2002 Uwe Bonnes
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/test.h"
25 static void test_sscanf( void )
27 char buffer
[100], buffer1
[100];
33 float res1
= -82.6267f
, res2
= 27.76f
, res11
, res12
;
34 static const char pname
[]=" St. Petersburg, Florida\n";
35 int hour
=21,min
=59,sec
=20;
36 int number
,number_so_far
;
41 ret
= sscanf(buffer
, "%d", &result
);
42 ok( ret
== EOF
,"sscanf returns %x instead of %x\n", ret
, EOF
);
45 ok( sscanf("000000000046F170", "%p", &ptr
) == 1, "sscanf failed\n" );
46 ok( ptr
== (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr
, 0x46F170 );
48 ok( sscanf("0046F171", "%p", &ptr
) == 1, "sscanf failed\n" );
49 ok( ptr
== (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr
, 0x46F171 );
51 ok( sscanf("46F172", "%p", &ptr
) == 1, "sscanf failed\n" );
52 ok( ptr
== (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr
, 0x46F172 );
54 ok( sscanf("0x46F173", "%p", &ptr
) == 1, "sscanf failed\n" );
55 ok( ptr
== NULL
,"sscanf reads %p instead of %x\n", ptr
, 0 );
57 ok( sscanf("-46F174", "%p", &ptr
) == 1, "sscanf failed\n" );
58 ok( ptr
== (void *)(ULONG_PTR
)-0x46f174,"sscanf reads %p instead of %p\n",
59 ptr
, (void *)(ULONG_PTR
)-0x46f174 );
61 ok( sscanf("+46F175", "%p", &ptr
) == 1, "sscanf failed\n" );
62 ok( ptr
== (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr
, 0x46F175 );
64 /* check %p with no hex digits */
65 ok( sscanf("1233", "%p", &ptr
) == 1, "sscanf failed\n" );
66 ok( ptr
== (void *)0x1233,"sscanf reads %p instead of %x\n", ptr
, 0x1233 );
68 ok( sscanf("1234", "%P", &ptr
) == 1, "sscanf failed\n" );
69 ok( ptr
== (void *)0x1234,"sscanf reads %p instead of %x\n", ptr
, 0x1234 );
72 strcpy(buffer
,"0x519");
73 ok( sscanf(buffer
, "%x", &result
) == 1, "sscanf failed\n" );
74 ok( result
== 0x519,"sscanf reads %x instead of %x\n", result
, 0x519 );
76 strcpy(buffer
,"0x51a");
77 ok( sscanf(buffer
, "%x", &result
) == 1, "sscanf failed\n" );
78 ok( result
== 0x51a ,"sscanf reads %x instead of %x\n", result
, 0x51a );
80 strcpy(buffer
,"0x51g");
81 ok( sscanf(buffer
, "%x", &result
) == 1, "sscanf failed\n" );
82 ok( result
== 0x51, "sscanf reads %x instead of %x\n", result
, 0x51 );
85 ret
= sscanf("-1", "%x", &result
);
86 ok(ret
== 1, "Wrong number of arguments read: %d (expected 1)\n", ret
);
87 ok(result
== -1, "Read %d, expected -1\n", result
);
89 /* check % followed by any char */
90 strcpy(buffer
,"\"%12@");
91 strcpy(format
,"%\"%%%d%@"); /* work around gcc format check */
92 ok( sscanf(buffer
, format
, &result
) == 1, "sscanf failed\n" );
93 ok( result
== 12, "sscanf reads %x instead of %x\n", result
, 12 );
96 ret
= sprintf(buffer
,"%f %f",res1
, res2
);
97 ok( ret
== 20, "expected 20, got %u\n", ret
);
98 ret
= sscanf(buffer
,"%f%f",&res11
, &res12
);
99 ok( ret
== 2, "expected 2, got %u\n", ret
);
100 ok( (res11
== res1
) && (res12
== res2
), "Error reading floats\n");
103 ret
= sprintf(buffer
," %s", pname
);
104 ok( ret
== 26, "expected 26, got %u\n", ret
);
105 ret
= sscanf(buffer
,"%*c%[^\n]",buffer1
);
106 ok( ret
== 1, "Error with format \"%s\"\n","%*c%[^\n]");
107 ok( strncmp(pname
,buffer1
,strlen(buffer1
)) == 0, "Error with \"%s\" \"%s\"\n",pname
, buffer1
);
109 ret
= sscanf("abcefgdh","%*[a-cg-e]%c",&buffer
[0]);
110 ok( ret
== 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
111 ok( buffer
[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer
[0]);
113 ret
= sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer
[0]);
114 ok( ret
== 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
115 ok( buffer
[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer
[0]);
118 ret
= sprintf(buffer
,"%d:%d:%d",hour
,min
,sec
);
119 ok( ret
== 8, "expected 8, got %u\n", ret
);
120 ret
= sscanf(buffer
,"%d%n",&number
,&number_so_far
);
121 ok(ret
== 1 , "problem with format arg \"%%d%%n\"\n");
122 ok(number
== hour
,"Read wrong arg %d instead of %d\n",number
, hour
);
123 ok(number_so_far
== 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far
);
125 ret
= sscanf(buffer
+2,"%*c%n",&number_so_far
);
126 ok(ret
== 0 , "problem with format arg \"%%*c%%n\"\n");
127 ok(number_so_far
== 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far
);
130 strcpy(buffer
,"12345678");
131 ret
= sscanf(buffer
, "%hd", &result
);
132 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
133 ok(result
== 0xdead614e, "Wrong number read (%x)\n", result
);
136 ret
= sscanf(buffer
, "%hhd", &result
);
137 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
138 ok(result
== 0xbc614e, "Wrong number read (%x)\n", result
);
140 strcpy(buffer
,"12345678901234");
141 ret
= sscanf(buffer
, "%lld", &result64
);
142 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
143 ret
= sprintf(buffer1
, "%lld", result64
);
144 ok(ret
==14 || broken(ret
==10), "sprintf retuned %d\n", ret
);
146 ok(!strcmp(buffer
, buffer1
), "got %s, expected %s\n", buffer1
, buffer
);
148 /* Check %i according to bug 1878 */
149 strcpy(buffer
,"123");
150 ret
= sscanf(buffer
, "%i", &result
);
151 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
152 ok(result
== 123, "Wrong number read\n");
154 ret
= sscanf("-1", "%i", &result
);
155 ok(ret
== 1, "Wrong number of arguments read: %d (expected 1)\n", ret
);
156 ok(result
== -1, "Read %d, expected -1\n", result
);
157 ret
= sscanf(buffer
, "%d", &result
);
158 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
159 ok(result
== 123, "Wrong number read\n");
161 ret
= sscanf("-1", "%d", &result
);
162 ok(ret
== 1, "Wrong number of arguments read: %d (expected 1)\n", ret
);
163 ok(result
== -1, "Read %d, expected -1\n", result
);
165 /* Check %i for octal and hexadecimal input */
167 strcpy(buffer
,"017");
168 ret
= sscanf(buffer
, "%i", &result
);
169 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
170 ok(result
== 15, "Wrong number read\n");
172 strcpy(buffer
,"0x17");
173 ret
= sscanf(buffer
, "%i", &result
);
174 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
175 ok(result
== 23, "Wrong number read\n");
179 ret
= sscanf("-1", "%o", &result
);
180 ok(ret
== 1, "Wrong number of arguments read: %d (expected 1)\n", ret
);
181 ok(result
== -1, "Read %d, expected -1\n", result
);
185 ret
= sscanf("-1", "%u", &result
);
186 ok(ret
== 1, "Wrong number of arguments read: %d (expected 1)\n", ret
);
187 ok(result
== -1, "Read %d, expected -1\n", result
);
192 ret
= sscanf(buffer
, "%c", &c
);
193 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
194 ok(c
== 'a', "Field incorrect: '%c'\n", c
);
198 ret
= sscanf(buffer
, "%c", &c
);
199 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
200 ok(c
== ' ', "Field incorrect: '%c'\n", c
);
202 strcpy(buffer
,"18:59");
204 ret
= sscanf(buffer
, "%d:%d%c", &hour
, &min
, &c
);
205 ok(ret
== 2, "Wrong number of arguments read: %d\n", ret
);
206 ok(hour
== 18, "Field 1 incorrect: %d\n", hour
);
207 ok(min
== 59, "Field 2 incorrect: %d\n", min
);
208 ok(c
== 0x55, "Field 3 incorrect: 0x%02x\n", c
);
210 /* Check %n (also whitespace in format strings and %s) */
211 buffer
[0]=0; buffer1
[0]=0;
212 ret
= sscanf("abc def", "%s %n%s", buffer
, &number_so_far
, buffer1
);
213 ok(strcmp(buffer
, "abc")==0, "First %%s read incorrectly: %s\n", buffer
);
214 ok(strcmp(buffer1
,"def")==0, "Second %%s read incorrectly: %s\n", buffer1
);
215 ok(number_so_far
==6, "%%n yielded wrong result: %d\n", number_so_far
);
216 ok(ret
== 2, "%%n shouldn't count as a conversion: %d\n", ret
);
218 /* Check where %n matches to EOF in buffer */
219 strcpy(buffer
, "3:45");
220 ret
= sscanf(buffer
, "%d:%d%n", &hour
, &min
, &number_so_far
);
221 ok(ret
== 2, "Wrong number of arguments read: %d\n", ret
);
222 ok(number_so_far
== 4, "%%n yielded wrong result: %d\n", number_so_far
);
225 static void test_sscanf_s(void)
227 int (__cdecl
*psscanf_s
)(const char*,const char*,...);
228 HMODULE hmod
= GetModuleHandleA("msvcrt.dll");
232 psscanf_s
= (void*)GetProcAddress(hmod
, "sscanf_s");
234 win_skip("sscanf_s not available\n");
238 ret
= psscanf_s("123", "%d", &i
);
239 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
240 ok(i
== 123, "i = %d\n", i
);
242 ret
= psscanf_s("123", "%s", buf
, 100);
243 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
244 ok(!strcmp("123", buf
), "buf = %s\n", buf
);
246 ret
= psscanf_s("123", "%s", buf
, 3);
247 ok(ret
== 0, "Wrong number of arguments read: %d\n", ret
);
248 ok(buf
[0]=='\0', "buf = %s\n", buf
);
251 ret
= psscanf_s("123", "%3c", buf
, 2);
252 ok(ret
== 0, "Wrong number of arguments read: %d\n", ret
);
253 ok(buf
[0]=='\0', "buf = %s\n", buf
);
256 ret
= psscanf_s("123 123", "%s %d", buf
, 2, &i
);
257 ok(ret
== 0, "Wrong number of arguments read: %d\n", ret
);
258 ok(i
==1, "i = %d\n", i
);
261 ret
= psscanf_s("123 123", "%d %s", &i
, buf
, 2);
262 ok(ret
== 1, "Wrong number of arguments read: %d\n", ret
);
263 ok(i
==123, "i = %d\n", i
);
266 static void test_swscanf( void )
270 static const WCHAR formatd
[] = {'%','d',0};
273 /* WEOF is an unsigned short -1 but swscanf returns int
274 so it should be sign-extended */
276 ret
= swscanf(buffer
, formatd
, &result
);
277 /* msvcrt returns 0 but should return -1 (later versions do) */
278 ok( ret
== (short)WEOF
|| broken(ret
== 0),
279 "swscanf returns %x instead of %x\n", ret
, WEOF
);