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];
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;
40 ret = sscanf(buffer, "%d", &result);
41 ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
44 ok( sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n" );
45 ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 );
47 ok( sscanf("0046F171", "%p", &ptr) == 1, "sscanf failed\n" );
48 ok( ptr == (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr, 0x46F171 );
50 ok( sscanf("46F172", "%p", &ptr) == 1, "sscanf failed\n" );
51 ok( ptr == (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr, 0x46F172 );
53 ok( sscanf("0x46F173", "%p", &ptr) == 1, "sscanf failed\n" );
54 ok( ptr == (void *)0,"sscanf reads %p instead of %x\n", ptr, 0 );
56 ok( sscanf("-46F174", "%p", &ptr) == 1, "sscanf failed\n" );
57 ok( ptr == (void *)0xFFB90E8C,"sscanf reads %p instead of %x\n", ptr, 0xFFB90E8C );
59 ok( sscanf("+46F175", "%p", &ptr) == 1, "sscanf failed\n" );
60 ok( ptr == (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr, 0x46F175 );
62 /* check %p with no hex digits */
63 ok( sscanf("1233", "%p", &ptr) == 1, "sscanf failed\n" );
64 ok( ptr == (void *)0x1233,"sscanf reads %p instead of %x\n", ptr, 0x1233 );
66 ok( sscanf("1234", "%P", &ptr) == 1, "sscanf failed\n" );
67 ok( ptr == (void *)0x1234,"sscanf reads %p instead of %x\n", ptr, 0x1234 );
70 strcpy(buffer,"0x519");
71 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
72 ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
74 strcpy(buffer,"0x51a");
75 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
76 ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
78 strcpy(buffer,"0x51g");
79 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
80 ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
83 ret = sscanf("-1", "%x", &result);
84 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
85 ok(result == -1, "Read %d, expected -1\n", result);
87 /* check % followed by any char */
88 strcpy(buffer,"\"%12@");
89 strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
90 ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
91 ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
94 ret = sprintf(buffer,"%f %f",res1, res2);
95 ret = sscanf(buffer,"%f%f",&res11, &res12);
96 ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
99 ret = sprintf(buffer," %s", pname);
100 ret = sscanf(buffer,"%*c%[^\n]",buffer1);
101 ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
102 ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
104 ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
105 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
106 ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
108 ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
109 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
110 ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
113 ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
114 ret = sscanf(buffer,"%d%n",&number,&number_so_far);
115 ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
116 ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
117 ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
119 ret = sscanf(buffer+2,"%*c%n",&number_so_far);
120 ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
121 ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
123 /* Check %i according to bug 1878 */
124 strcpy(buffer,"123");
125 ret = sscanf(buffer, "%i", &result);
126 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
127 ok(result == 123, "Wrong number read\n");
129 ret = sscanf("-1", "%i", &result);
130 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
131 ok(result == -1, "Read %d, expected -1\n", result);
132 ret = sscanf(buffer, "%d", &result);
133 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
134 ok(result == 123, "Wrong number read\n");
136 ret = sscanf("-1", "%d", &result);
137 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
138 ok(result == -1, "Read %d, expected -1\n", result);
140 /* Check %i for octal and hexadecimal input */
142 strcpy(buffer,"017");
143 ret = sscanf(buffer, "%i", &result);
144 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
145 ok(result == 15, "Wrong number read\n");
147 strcpy(buffer,"0x17");
148 ret = sscanf(buffer, "%i", &result);
149 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
150 ok(result == 23, "Wrong number read\n");
154 ret = sscanf("-1", "%o", &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);
160 ret = sscanf("-1", "%u", &result);
161 ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
162 ok(result == -1, "Read %d, expected -1\n", result);
167 ret = sscanf(buffer, "%c", &c);
168 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
169 ok(c == 'a', "Field incorrect: '%c'\n", c);
173 ret = sscanf(buffer, "%c", &c);
174 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
175 ok(c == ' ', "Field incorrect: '%c'\n", c);
177 strcpy(buffer,"18:59");
179 ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
180 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
181 ok(hour == 18, "Field 1 incorrect: %d\n", hour);
182 ok(min == 59, "Field 2 incorrect: %d\n", min);
183 ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
185 /* Check %n (also whitespace in format strings and %s) */
186 buffer[0]=0; buffer1[0]=0;
187 ret = sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
188 ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
189 ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
190 ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
191 ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
193 /* Check where %n matches to EOF in buffer */
194 strcpy(buffer, "3:45");
195 ret = sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
196 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
197 ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);