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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/test.h"
25 static void test_sscanf( void )
27 char buffer[100], buffer1[100];
30 float res1= -82.6267f, res2= 27.76f, res11, res12;
31 static const char pname[]=" St. Petersburg, Florida\n";
32 int hour=21,min=59,sec=20;
33 int number,number_so_far;
38 ret = sscanf(buffer, "%d", &result);
39 ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
42 strcpy(buffer,"0x519");
43 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
44 ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
46 strcpy(buffer,"0x51a");
47 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
48 ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
50 strcpy(buffer,"0x51g");
51 ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
52 ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
54 /* check % followed by any char */
55 strcpy(buffer,"\"%12@");
56 strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
57 ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
58 ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
61 ret = sprintf(buffer,"%f %f",res1, res2);
62 ret = sscanf(buffer,"%f%f",&res11, &res12);
63 ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
66 ret = sprintf(buffer," %s", pname);
67 ret = sscanf(buffer,"%*c%[^\n]",buffer1);
68 ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
69 ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
71 ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
72 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
73 ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
75 ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
76 ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
77 ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
80 ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
81 ret = sscanf(buffer,"%d%n",&number,&number_so_far);
82 ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
83 ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
84 ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
86 ret = sscanf(buffer+2,"%*c%n",&number_so_far);
87 ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
88 ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
90 /* Check %i according to bug 1878 */
92 ret = sscanf(buffer, "%i", &result);
93 ok( ret == 1 , "Wrong number of arguments read\n");
94 ok(result == 123, "Wrong number read\n");
95 ret = sscanf(buffer, "%d", &result);
96 ok( ret == 1 , "Wrong number of arguments read\n");
97 ok(result == 123, "Wrong number read\n");
100 static void test_sprintf( void )
103 const char *I64d = "%I64d";
104 double pnumber=789456123;
105 sprintf(buffer,"%+#23.15e",pnumber);
108 ok(strstr(buffer,"e+008") != 0,"Sprintf different \"%s\"\n",buffer);
110 sprintf(buffer,I64d,((ULONGLONG)0xffffffff)*0xffffffff);
113 ok(strlen(buffer) == 19,"Problem with long long \"%s\"\n",buffer);
117 static void test_snprintf (void)
119 struct snprintf_test {
127 /* Pre-2.1 libc behaviour, not C99 compliant. */
128 const struct snprintf_test tests[] = {{"short", 5, {0, 0}},
129 {"justfit", 7, {0, 0}},
130 {"justfits", 8, {0, 1}},
131 {"muchlonger", -1, {1, 1}}};
133 const int bufsiz = sizeof buffer;
136 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
137 const char *fmt = tests[i].format;
138 const int expect = tests[i].expected;
139 const int n = _snprintf (buffer, bufsiz, fmt);
140 const int valid = n < 0 ? bufsiz : (n == bufsiz ? n : n+1);
142 todo (tests[i].todo.retval ? "wine" : "none")
143 ok (n == expect, "\"%s\": expected %d, returned %d\n",
145 todo (tests[i].todo.render ? "wine" : "none")
146 ok (!memcmp (fmt, buffer, valid),
147 "\"%s\": rendered \"%.*s\"\n", fmt, valid, buffer);