Move and reshuffle reactos/regtetsts into rostests. 1/2
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 /* check % followed by any char */
56 strcpy(buffer,"\"%12@");
57 strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
58 ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
59 ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
60
61 /* Check float */
62 ret = sprintf(buffer,"%f %f",res1, res2);
63 ret = sscanf(buffer,"%f%f",&res11, &res12);
64 ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
65
66 /* check strings */
67 ret = sprintf(buffer," %s", pname);
68 ret = sscanf(buffer,"%*c%[^\n]",buffer1);
69 ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
70 ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
71
72 ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
73 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
74 ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
75
76 ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
77 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
78 ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
79
80 /* check digits */
81 ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
82 ret = sscanf(buffer,"%d%n",&number,&number_so_far);
83 ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
84 ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
85 ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
86
87 ret = sscanf(buffer+2,"%*c%n",&number_so_far);
88 ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
89 ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
90
91 /* Check %i according to bug 1878 */
92 strcpy(buffer,"123");
93 ret = sscanf(buffer, "%i", &result);
94 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
95 ok(result == 123, "Wrong number read\n");
96 ret = sscanf(buffer, "%d", &result);
97 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
98 ok(result == 123, "Wrong number read\n");
99
100 /* Check %c */
101 strcpy(buffer,"a");
102 c = 0x55;
103 ret = sscanf(buffer, "%c", &c);
104 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
105 ok(c == 'a', "Field incorrect: '%c'\n", c);
106
107 strcpy(buffer," a");
108 c = 0x55;
109 ret = sscanf(buffer, "%c", &c);
110 ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
111 ok(c == ' ', "Field incorrect: '%c'\n", c);
112
113 strcpy(buffer,"18:59");
114 c = 0x55;
115 ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
116 ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
117 ok(hour == 18, "Field 1 incorrect: %d\n", hour);
118 ok(min == 59, "Field 2 incorrect: %d\n", min);
119 ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
120
121 /* Check %n (also whitespace in format strings and %s) */
122 buffer[0]=0; buffer1[0]=0;
123 ret = sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
124 ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
125 ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
126 ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
127 ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
128 }
129
130 START_TEST(scanf)
131 {
132 test_sscanf();
133 }