msvcrt: Fixed space in type with double indirection.
[wine] / dlls / msvcrt / tests / 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     float res1= -82.6267f, res2= 27.76f, res11, res12;
32     static const char pname[]=" St. Petersburg, Florida\n";
33     int hour=21,min=59,sec=20;
34     int  number,number_so_far;
35
36
37     /* check EOF */
38     strcpy(buffer,"");
39     ret = sscanf(buffer, "%d", &result);
40     ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
41
42     /* check %x */
43     strcpy(buffer,"0x519");
44     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n"  );
45     ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
46
47     strcpy(buffer,"0x51a");
48     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
49     ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
50
51     strcpy(buffer,"0x51g");
52     ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
53     ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
54
55     result = 0;
56     ret = sscanf("-1", "%x", &result);
57     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
58     ok(result == -1, "Read %d, expected -1\n", result);
59
60     /* check % followed by any char */
61     strcpy(buffer,"\"%12@");
62     strcpy(format,"%\"%%%d%@");  /* work around gcc format check */
63     ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
64     ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
65
66     /* Check float */
67     ret = sprintf(buffer,"%f %f",res1, res2);
68     ret = sscanf(buffer,"%f%f",&res11, &res12);
69     ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
70
71     /* check strings */
72     ret = sprintf(buffer," %s", pname);
73     ret = sscanf(buffer,"%*c%[^\n]",buffer1);
74     ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
75     ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
76
77     ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
78     ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
79     ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
80
81     ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
82     ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
83     ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
84
85     /* check digits */
86     ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
87     ret = sscanf(buffer,"%d%n",&number,&number_so_far);
88     ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
89     ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
90     ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
91
92     ret = sscanf(buffer+2,"%*c%n",&number_so_far);
93     ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
94     ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
95
96     /* Check %i according to bug 1878 */
97     strcpy(buffer,"123");
98     ret = sscanf(buffer, "%i", &result);
99     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
100     ok(result == 123, "Wrong number read\n");
101     result = 0;
102     ret = sscanf("-1", "%i", &result);
103     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
104     ok(result == -1, "Read %d, expected -1\n", result);
105     ret = sscanf(buffer, "%d", &result);
106     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
107     ok(result == 123, "Wrong number read\n");
108     result = 0;
109     ret = sscanf("-1", "%d", &result);
110     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
111     ok(result == -1, "Read %d, expected -1\n", result);
112
113     /* Check %i for octal and hexadecimal input */
114     result = 0;
115     strcpy(buffer,"017");
116     ret = sscanf(buffer, "%i", &result);
117     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
118     ok(result == 15, "Wrong number read\n");
119     result = 0;
120     strcpy(buffer,"0x17");
121     ret = sscanf(buffer, "%i", &result);
122     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
123     ok(result == 23, "Wrong number read\n");
124
125     /* %o */
126     result = 0;
127     ret = sscanf("-1", "%o", &result);
128     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
129     ok(result == -1, "Read %d, expected -1\n", result);
130
131     /* %u */
132     result = 0;
133     ret = sscanf("-1", "%u", &result);
134     ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
135     ok(result == -1, "Read %d, expected -1\n", result);
136
137     /* Check %c */
138     strcpy(buffer,"a");
139     c = 0x55;
140     ret = sscanf(buffer, "%c", &c);
141     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
142     ok(c == 'a', "Field incorrect: '%c'\n", c);
143
144     strcpy(buffer," a");
145     c = 0x55;
146     ret = sscanf(buffer, "%c", &c);
147     ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
148     ok(c == ' ', "Field incorrect: '%c'\n", c);
149
150     strcpy(buffer,"18:59");
151     c = 0x55;
152     ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
153     ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
154     ok(hour == 18, "Field 1 incorrect: %d\n", hour);
155     ok(min == 59, "Field 2 incorrect: %d\n", min);
156     ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
157
158     /* Check %n (also whitespace in format strings and %s) */
159     buffer[0]=0; buffer1[0]=0;
160     ret = sscanf("abc   def", "%s %n%s", buffer, &number_so_far, buffer1);
161     ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
162     ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
163     ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
164     ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
165
166     /* Check where %n matches to EOF in buffer */
167     strcpy(buffer, "3:45");
168     ret = sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
169     ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
170     ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);
171 }
172
173 START_TEST(scanf)
174 {
175     test_sscanf();
176 }