ReactOS 0.4.15-dev-7660-g0086d05
scanf.c
Go to the documentation of this file.
1/*
2 * Unit test suite for *scanf functions.
3 *
4 * Copyright 2002 Uwe Bonnes
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdio.h>
22
23#include "wine/test.h"
24
25static void test_sscanf( void )
26{
27 /* use function pointers to bypass gcc builtin */
28 int (WINAPIV *p_sprintf)(char *buf, const char *fmt, ...);
29 int (WINAPIV *p_sscanf)(const char *buf, const char *fmt, ...);
30 char buffer[100], buffer1[100];
31 char format[20];
32 int result, ret;
33 LONGLONG result64;
34 char c;
35 void *ptr;
36 float res1= -82.6267f, res2= 27.76f, res11, res12;
37 double double_res;
38 static const char pname[]=" St. Petersburg, Florida\n";
39 int hour=21,min=59,sec=20;
40 int number,number_so_far;
41 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
42
43 p_sprintf = (void *)GetProcAddress( hmod, "sprintf" );
44 p_sscanf = (void *)GetProcAddress( hmod, "sscanf" );
45
46 /* check EOF */
47 strcpy(buffer,"");
48 ret = p_sscanf(buffer, "%d", &result);
49 ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
50
51 /* check %p */
52 ok( p_sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n" );
53 ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 );
54
55 ok( p_sscanf("0046F171", "%p", &ptr) == 1, "sscanf failed\n" );
56 ok( ptr == (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr, 0x46F171 );
57
58 ok( p_sscanf("46F172", "%p", &ptr) == 1, "sscanf failed\n" );
59 ok( ptr == (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr, 0x46F172 );
60
61 ok( p_sscanf("0x46F173", "%p", &ptr) == 1, "sscanf failed\n" );
62 ok( ptr == NULL,"sscanf reads %p instead of %x\n", ptr, 0 );
63
64 ok( p_sscanf("-46F174", "%p", &ptr) == 1, "sscanf failed\n" );
65 ok( ptr == (void *)(ULONG_PTR)-0x46f174,"sscanf reads %p instead of %p\n",
66 ptr, (void *)(ULONG_PTR)-0x46f174 );
67
68 ok( p_sscanf("+46F175", "%p", &ptr) == 1, "sscanf failed\n" );
69 ok( ptr == (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr, 0x46F175 );
70
71 /* check %p with no hex digits */
72 ok( p_sscanf("1233", "%p", &ptr) == 1, "sscanf failed\n" );
73 ok( ptr == (void *)0x1233,"sscanf reads %p instead of %x\n", ptr, 0x1233 );
74
75 ok( p_sscanf("1234", "%P", &ptr) == 1, "sscanf failed\n" );
76 ok( ptr == (void *)0x1234,"sscanf reads %p instead of %x\n", ptr, 0x1234 );
77
78 /* check %x */
79 strcpy(buffer,"0x519");
80 ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
81 ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
82
83 strcpy(buffer,"0x51a");
84 ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
85 ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
86
87 strcpy(buffer,"0x51g");
88 ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
89 ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
90
91 result = 0;
92 ret = p_sscanf("-1", "%x", &result);
93 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
94 ok(result == -1, "Read %d, expected -1\n", result);
95
96 /* check % followed by any char */
97 strcpy(buffer,"\"%12@");
98 strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
99 ok( p_sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
100 ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
101
102 /* Check float */
103 ret = p_sprintf(buffer,"%f %f",res1, res2);
104 ok( ret == 20, "expected 20, got %u\n", ret);
105 ret = p_sscanf(buffer,"%f%f",&res11, &res12);
106 ok( ret == 2, "expected 2, got %u\n", ret);
107 ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
108
109 /* Check double */
110 ret = p_sprintf(buffer, "%lf", 32.715);
111 ok(ret == 9, "expected 9, got %u\n", ret);
112 ret = p_sscanf(buffer, "%lf", &double_res);
113 ok(ret == 1, "expected 1, got %u\n", ret);
114 ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
115 ret = p_sscanf(buffer, "%Lf", &double_res);
116 ok(ret == 1, "expected 1, got %u\n", ret);
117 ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
118
119 strcpy(buffer, "1.1e-30");
120 ret = p_sscanf(buffer, "%lf", &double_res);
121 ok(ret == 1, "expected 1, got %u\n", ret);
122 ok(double_res >= 1.1e-30-1e-45 && double_res <= 1.1e-30+1e-45,
123 "Got %.18le, expected %.18le\n", double_res, 1.1e-30);
124
125 /* check strings */
126 ret = p_sprintf(buffer," %s", pname);
127 ok( ret == 26, "expected 26, got %u\n", ret);
128 ret = p_sscanf(buffer,"%*c%[^\n]",buffer1);
129 ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
130 ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
131
132 ret = p_sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
133 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
134 ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
135
136 ret = p_sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
137 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
138 ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
139
140 buffer1[0] = 'b';
141 ret = p_sscanf("a","%s%s", buffer, buffer1);
142 ok( ret == 1, "expected 1, got %u\n", ret);
143 ok( buffer[0] == 'a', "buffer[0] = '%c'\n", buffer[0]);
144 ok( buffer[1] == '\0', "buffer[1] = '%c'\n", buffer[1]);
145 ok( buffer1[0] == 'b', "buffer1[0] = '%c'\n", buffer1[0]);
146
147 /* check digits */
148 ret = p_sprintf(buffer,"%d:%d:%d",hour,min,sec);
149 ok( ret == 8, "expected 8, got %u\n", ret);
150 ret = p_sscanf(buffer,"%d%n",&number,&number_so_far);
151 ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
152 ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
153 ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
154
155 ret = p_sscanf(buffer+2,"%*c%n",&number_so_far);
156 ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
157 ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
158
159 result = 0xdeadbeef;
160 strcpy(buffer,"12345678");
161 ret = p_sscanf(buffer, "%hd", &result);
162 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
163 ok(result == 0xdead614e, "Wrong number read (%x)\n", result);
164
165 result = 0xdeadbeef;
166 ret = p_sscanf(buffer, "%hhd", &result);
167 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
168 ok(result == 0xbc614e, "Wrong number read (%x)\n", result);
169
170 strcpy(buffer,"12345678901234");
171 ret = p_sscanf(buffer, "%lld", &result64);
172 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
173 ret = p_sprintf(buffer1, "%lld", result64);
174 ok(ret==14 || broken(ret==10), "sprintf returned %d\n", ret);
175 if(ret == 14)
176 ok(!strcmp(buffer, buffer1), "got %s, expected %s\n", buffer1, buffer);
177
178 /* Check %i according to bug 1878 */
179 strcpy(buffer,"123");
180 ret = p_sscanf(buffer, "%i", &result);
181 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
182 ok(result == 123, "Wrong number read\n");
183 result = 0;
184 ret = p_sscanf("-1", "%i", &result);
185 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
186 ok(result == -1, "Read %d, expected -1\n", result);
187 ret = p_sscanf(buffer, "%d", &result);
188 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
189 ok(result == 123, "Wrong number read\n");
190 result = 0;
191 ret = p_sscanf("-1", "%d", &result);
192 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
193 ok(result == -1, "Read %d, expected -1\n", result);
194
195 /* Check %i for octal and hexadecimal input */
196 result = 0;
197 strcpy(buffer,"017");
198 ret = p_sscanf(buffer, "%i", &result);
199 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
200 ok(result == 15, "Wrong number read\n");
201 result = 0;
202 strcpy(buffer,"0x17");
203 ret = p_sscanf(buffer, "%i", &result);
204 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
205 ok(result == 23, "Wrong number read\n");
206
207 /* %o */
208 result = 0;
209 ret = p_sscanf("-1", "%o", &result);
210 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
211 ok(result == -1, "Read %d, expected -1\n", result);
212
213 /* %u */
214 result = 0;
215 ret = p_sscanf("-1", "%u", &result);
216 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
217 ok(result == -1, "Read %d, expected -1\n", result);
218
219 /* Check %c */
220 strcpy(buffer,"a");
221 c = 0x55;
222 ret = p_sscanf(buffer, "%c", &c);
223 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
224 ok(c == 'a', "Field incorrect: '%c'\n", c);
225
226 strcpy(buffer," a");
227 c = 0x55;
228 ret = p_sscanf(buffer, "%c", &c);
229 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
230 ok(c == ' ', "Field incorrect: '%c'\n", c);
231
232 strcpy(buffer,"18:59");
233 c = 0x55;
234 ret = p_sscanf(buffer, "%d:%d%c", &hour, &min, &c);
235 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
236 ok(hour == 18, "Field 1 incorrect: %d\n", hour);
237 ok(min == 59, "Field 2 incorrect: %d\n", min);
238 ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
239
240 /* Check %n (also whitespace in format strings and %s) */
241 buffer[0]=0; buffer1[0]=0;
242 ret = p_sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
243 ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
244 ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
245 ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
246 ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
247
248 /* Check where %n matches to EOF in buffer */
249 strcpy(buffer, "3:45");
250 ret = p_sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
251 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
252 ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);
253
254 buffer[0] = 0;
255 buffer1[0] = 0;
256 ret = p_sscanf("test=value\xda", "%[^=] = %[^;]", buffer, buffer1);
257 ok(ret == 2, "got %d\n", ret);
258 ok(!strcmp(buffer, "test"), "buf %s\n", buffer);
259 ok(!strcmp(buffer1, "value\xda"), "buf %s\n", buffer1);
260
261 ret = p_sscanf("\x81\x82test", "\x81%\x82%s", buffer);
262 ok(ret == 1, "got %d\n", ret);
263 ok(!strcmp(buffer, "test"), "buf = %s\n", buffer);
264}
265
266static void test_sscanf_s(void)
267{
268 int (WINAPIV *psscanf_s)(const char*,const char*,...);
269 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
270 int i, ret;
271 char buf[100];
272
273 psscanf_s = (void*)GetProcAddress(hmod, "sscanf_s");
274 if(!psscanf_s) {
275 win_skip("sscanf_s not available\n");
276 return;
277 }
278
279 ret = psscanf_s("123", "%d", &i);
280 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
281 ok(i == 123, "i = %d\n", i);
282
283 ret = psscanf_s("123", "%s", buf, 100);
284 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
285 ok(!strcmp("123", buf), "buf = %s\n", buf);
286
287 ret = psscanf_s("123", "%s", buf, 3);
288 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
289 ok(buf[0]=='\0', "buf = %s\n", buf);
290
291 memset(buf, 'a', sizeof(buf));
292 ret = psscanf_s("123", "%3c", buf, 2);
293 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
294 ok(buf[0]=='\0', "buf = %s\n", buf);
295 ok(buf[1]=='2', "buf[1] = %d\n", buf[1]);
296 ok(buf[2]=='a', "buf[2] = %d\n", buf[2]);
297
298 buf[3] = 'a';
299 buf[4] = 0;
300 ret = psscanf_s("123", "%3c", buf, 3);
301 ok(!strcmp("123a", buf), "buf = %s\n", buf);
302
303 i = 1;
304 ret = psscanf_s("123 123", "%s %d", buf, 2, &i);
305 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
306 ok(i==1, "i = %d\n", i);
307
308 i = 1;
309 ret = psscanf_s("123 123", "%d %s", &i, buf, 2);
310 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
311 ok(i==123, "i = %d\n", i);
312}
313
314static void test_swscanf( void )
315{
316 wchar_t buffer[100];
317 int result, ret;
318 static const WCHAR formatd[] = {'%','d',0};
319 const WCHAR format2[] = {'a',0x1234,'%',0x1234,'%','c',0};
320 WCHAR c;
321
322 /* check WEOF */
323 /* WEOF is an unsigned short -1 but swscanf returns int
324 so it should be sign-extended */
325 buffer[0] = 0;
326 ret = swscanf(buffer, formatd, &result);
327 /* msvcrt returns 0 but should return -1 (later versions do) */
328 ok( ret == (short)WEOF || broken(ret == 0),
329 "swscanf returns %x instead of %x\n", ret, WEOF );
330
331 buffer[0] = 'a';
332 buffer[1] = 0x1234;
333 buffer[2] = 0x1234;
334 buffer[3] = 'b';
335 ret = swscanf(buffer, format2, &c);
336 ok(ret == 1, "swscanf returned %d\n", ret);
337 ok(c == 'b', "c = %x\n", c);
338}
339
340static void test_swscanf_s(void)
341{
342 static const wchar_t fmt1[] = {'%','c',0};
343 static const wchar_t fmt2[] = {'%','[','a','-','z',']',0};
344
345 int (WINAPIV *pswscanf_s)(const wchar_t*,const wchar_t*,...);
346 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
347 wchar_t buf[2], out[2];
348 int ret;
349
350 pswscanf_s = (void*)GetProcAddress(hmod, "swscanf_s");
351 if(!pswscanf_s) {
352 win_skip("swscanf_s not available\n");
353 return;
354 }
355
356 buf[0] = 'a';
357 buf[1] = '1';
358 out[1] = 'b';
359 ret = pswscanf_s(buf, fmt1, out, 1);
360 ok(ret == 1, "swscanf_s returned %d\n", ret);
361 ok(out[0] == 'a', "out[0] = %x\n", out[0]);
362 ok(out[1] == 'b', "out[1] = %x\n", out[1]);
363
364 ret = pswscanf_s(buf, fmt2, out, 1);
365 ok(!ret, "swscanf_s returned %d\n", ret);
366
367 ret = pswscanf_s(buf, fmt2, out, 2);
368 ok(ret == 1, "swscanf_s returned %d\n", ret);
369 ok(out[0] == 'a', "out[0] = %x\n", out[0]);
370 ok(!out[1], "out[1] = %x\n", out[1]);
371}
372
374{
375 test_sscanf();
377 test_swscanf();
379}
#define broken(x)
Definition: _sntprintf.h:21
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
GLuint buffer
Definition: glext.h:5915
const GLubyte * c
Definition: glext.h:8905
GLenum pname
Definition: glext.h:5645
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ _CRTIMP int __cdecl swscanf(_In_z_ const wchar_t *_Src, _In_z_ _Scanf_format_string_ const wchar_t *_Format,...)
_Check_return_ _CRTIMP int __cdecl scanf(_In_z_ _Scanf_format_string_ const char *_Format,...)
#define EOF
Definition: stdio.h:24
#define e
Definition: ke_i.h:82
#define c
Definition: ke_i.h:80
static PVOID ptr
Definition: dispmode.c:27
static PEXPLICIT_ACCESSW *static HMODULE hmod
Definition: security.c:143
static unsigned int number
Definition: dsound.c:1479
static void test_swscanf_s(void)
Definition: scanf.c:340
static void test_swscanf(void)
Definition: scanf.c:314
static void test_sscanf(void)
Definition: scanf.c:25
static void test_sscanf_s(void)
Definition: scanf.c:266
#define min(a, b)
Definition: monoChain.cc:55
static FILE * out
Definition: regtests2xml.c:44
#define WINAPIV
Definition: sdbpapi.h:64
#define WEOF
Definition: conio.h:185
#define win_skip
Definition: test.h:160
#define memset(x, y, z)
Definition: compat.h:39
Definition: dsound.c:943
int64_t LONGLONG
Definition: typedefs.h:68
uint32_t ULONG_PTR
Definition: typedefs.h:65
int ret
__wchar_t WCHAR
Definition: xmlstorage.h:180