cbfb18441933d5de0d6b5ca9c63e1e0cc1f0ebb7
[reactos.git] / rostests / winetests / msvcrt / scanf.c
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
25 static void test_sscanf( void )
26 {
27 char buffer[100], buffer1[100];
28 char format[20];
29 int result, ret;
30 char c;
31 void *ptr;
32 float res1= -82.6267f, res2= 27.76f, res11, res12;
33 static const char pname[]=" St. Petersburg, Florida\n";
34 int hour=21,min=59,sec=20;
35 int number,number_so_far;
36
37
38 /* check EOF */
39 strcpy(buffer,"");
40 ret = sscanf(buffer, "%d", &result);
41 ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
42
43 /* check %p */
44 ok( sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n" );
45 ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 );
46
47 ok( sscanf("0046F171", "%p", &ptr) == 1, "sscanf failed\n" );
48 ok( ptr == (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr, 0x46F171 );
49
50 ok( sscanf("46F172", "%p", &ptr) == 1, "sscanf failed\n" );
51 ok( ptr == (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr, 0x46F172 );
52
53 ok( sscanf("0x46F173", "%p", &ptr) == 1, "sscanf failed\n" );
54 ok( ptr == NULL,"sscanf reads %p instead of %x\n", ptr, 0 );
55
56 ok( sscanf("-46F174", "%p", &ptr) == 1, "sscanf failed\n" );
57 ok( ptr == (void *)(ULONG_PTR)-0x46f174,"sscanf reads %p instead of %p\n",
58 ptr, (void *)(ULONG_PTR)-0x46f174 );
59
60 ok( sscanf("+46F175", "%p", &ptr) == 1, "sscanf failed\n" );
61 ok( ptr == (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr, 0x46F175 );
62
63 /* check %p with no hex digits */
64 ok( sscanf("1233", "%p", &ptr) == 1, "sscanf failed\n" );
65 ok( ptr == (void *)0x1233,"sscanf reads %p instead of %x\n", ptr, 0x1233 );
66
67 ok( sscanf("1234", "%P", &ptr) == 1, "sscanf failed\n" );
68 ok( ptr == (void *)0x1234,"sscanf reads %p instead of %x\n", ptr, 0x1234 );
69
70 /* check %x */
71 strcpy(buffer,"0x519");
72 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
73 ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
74
75 strcpy(buffer,"0x51a");
76 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
77 ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
78
79 strcpy(buffer,"0x51g");
80 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
81 ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
82
83 result = 0;
84 ret = sscanf("-1", "%x", &result);
85 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
86 ok(result == -1, "Read %d, expected -1\n", result);
87
88 /* check % followed by any char */
89 strcpy(buffer,"\"%12@");
90 strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
91 ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
92 ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
93
94 /* Check float */
95 ret = sprintf(buffer,"%f %f",res1, res2);
96 ok( ret == 20, "expected 20, got %u\n", ret);
97 ret = sscanf(buffer,"%f%f",&res11, &res12);
98 ok( ret == 2, "expected 2, got %u\n", ret);
99 ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
100
101 /* check strings */
102 ret = sprintf(buffer," %s", pname);
103 ok( ret == 26, "expected 26, got %u\n", ret);
104 ret = sscanf(buffer,"%*c%[^\n]",buffer1);
105 ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
106 ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
107
108 ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
109 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
110 ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
111
112 ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
113 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
114 ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
115
116 /* check digits */
117 ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
118 ok( ret == 8, "expected 8, got %u\n", ret);
119 ret = sscanf(buffer,"%d%n",&number,&number_so_far);
120 ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
121 ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
122 ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
123
124 ret = sscanf(buffer+2,"%*c%n",&number_so_far);
125 ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
126 ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
127
128 /* Check %i according to bug 1878 */
129 strcpy(buffer,"123");
130 ret = sscanf(buffer, "%i", &result);
131 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
132 ok(result == 123, "Wrong number read\n");
133 result = 0;
134 ret = sscanf("-1", "%i", &result);
135 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
136 ok(result == -1, "Read %d, expected -1\n", result);
137 ret = sscanf(buffer, "%d", &result);
138 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
139 ok(result == 123, "Wrong number read\n");
140 result = 0;
141 ret = sscanf("-1", "%d", &result);
142 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
143 ok(result == -1, "Read %d, expected -1\n", result);
144
145 /* Check %i for octal and hexadecimal input */
146 result = 0;
147 strcpy(buffer,"017");
148 ret = sscanf(buffer, "%i", &result);
149 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
150 ok(result == 15, "Wrong number read\n");
151 result = 0;
152 strcpy(buffer,"0x17");
153 ret = sscanf(buffer, "%i", &result);
154 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
155 ok(result == 23, "Wrong number read\n");
156
157 /* %o */
158 result = 0;
159 ret = sscanf("-1", "%o", &result);
160 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
161 ok(result == -1, "Read %d, expected -1\n", result);
162
163 /* %u */
164 result = 0;
165 ret = sscanf("-1", "%u", &result);
166 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
167 ok(result == -1, "Read %d, expected -1\n", result);
168
169 /* Check %c */
170 strcpy(buffer,"a");
171 c = 0x55;
172 ret = sscanf(buffer, "%c", &c);
173 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
174 ok(c == 'a', "Field incorrect: '%c'\n", c);
175
176 strcpy(buffer," a");
177 c = 0x55;
178 ret = sscanf(buffer, "%c", &c);
179 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
180 ok(c == ' ', "Field incorrect: '%c'\n", c);
181
182 strcpy(buffer,"18:59");
183 c = 0x55;
184 ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
185 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
186 ok(hour == 18, "Field 1 incorrect: %d\n", hour);
187 ok(min == 59, "Field 2 incorrect: %d\n", min);
188 ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
189
190 /* Check %n (also whitespace in format strings and %s) */
191 buffer[0]=0; buffer1[0]=0;
192 ret = sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
193 ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
194 ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
195 ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
196 ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
197
198 /* Check where %n matches to EOF in buffer */
199 strcpy(buffer, "3:45");
200 ret = sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
201 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
202 ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);
203 }
204
205 static void test_sscanf_s(void)
206 {
207 int (__cdecl *psscanf_s)(const char*,const char*,...);
208 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
209 int i, ret;
210 char buf[100];
211
212 psscanf_s = (void*)GetProcAddress(hmod, "sscanf_s");
213 if(!psscanf_s) {
214 win_skip("sscanf_s not available\n");
215 return;
216 }
217
218 ret = psscanf_s("123", "%d", &i);
219 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
220 ok(i == 123, "i = %d\n", i);
221
222 ret = psscanf_s("123", "%s", buf, 100);
223 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
224 ok(!strcmp("123", buf), "buf = %s\n", buf);
225
226 ret = psscanf_s("123", "%s", buf, 3);
227 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
228 ok(buf[0]=='\0', "buf = %s\n", buf);
229
230 buf[0] = 'a';
231 ret = psscanf_s("123", "%3c", buf, 2);
232 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
233 ok(buf[0]=='\0', "buf = %s\n", buf);
234
235 i = 1;
236 ret = psscanf_s("123 123", "%s %d", buf, 2, &i);
237 ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
238 ok(i==1, "i = %d\n", i);
239
240 i = 1;
241 ret = psscanf_s("123 123", "%d %s", &i, buf, 2);
242 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
243 ok(i==123, "i = %d\n", i);
244 }
245
246 START_TEST(scanf)
247 {
248 test_sscanf();
249 test_sscanf_s();
250 }