2002-10-30 23:49:03 +00:00
|
|
|
/*
|
|
|
|
* Unit test suite for *scanf functions.
|
|
|
|
*
|
|
|
|
* Copyright 2002 Uwe Bonnes
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2002-11-06 19:57:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2002-10-30 23:49:03 +00:00
|
|
|
#include "wine/test.h"
|
|
|
|
|
|
|
|
static void test_sscanf( void )
|
|
|
|
{
|
2003-05-13 23:38:46 +00:00
|
|
|
char buffer[100], buffer1[100];
|
2002-12-20 00:35:41 +00:00
|
|
|
char format[20];
|
2003-01-11 20:53:22 +00:00
|
|
|
int result, ret;
|
2005-01-28 11:27:26 +00:00
|
|
|
char c;
|
2003-09-25 20:29:40 +00:00
|
|
|
float res1= -82.6267f, res2= 27.76f, res11, res12;
|
2004-05-04 04:13:05 +00:00
|
|
|
static const char pname[]=" St. Petersburg, Florida\n";
|
2003-10-10 00:00:35 +00:00
|
|
|
int hour=21,min=59,sec=20;
|
|
|
|
int number,number_so_far;
|
2003-01-11 20:53:22 +00:00
|
|
|
|
2004-05-04 04:13:05 +00:00
|
|
|
|
2003-01-11 20:53:22 +00:00
|
|
|
/* check EOF */
|
|
|
|
strcpy(buffer,"");
|
|
|
|
ret = sscanf(buffer, "%d", &result);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
|
2004-05-04 04:13:05 +00:00
|
|
|
|
2003-01-11 20:53:22 +00:00
|
|
|
/* check %x */
|
2002-10-30 23:49:03 +00:00
|
|
|
strcpy(buffer,"0x519");
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
|
|
|
ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
|
2002-10-30 23:49:03 +00:00
|
|
|
|
|
|
|
strcpy(buffer,"0x51a");
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
|
|
|
ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
|
2002-10-30 23:49:03 +00:00
|
|
|
|
|
|
|
strcpy(buffer,"0x51g");
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
|
|
|
ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
|
2002-12-20 00:35:41 +00:00
|
|
|
|
|
|
|
/* check % followed by any char */
|
|
|
|
strcpy(buffer,"\"%12@");
|
|
|
|
strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
|
|
|
|
ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
|
2003-05-13 23:38:46 +00:00
|
|
|
|
2004-01-06 21:36:10 +00:00
|
|
|
/* Check float */
|
2003-05-13 23:38:46 +00:00
|
|
|
ret = sprintf(buffer,"%f %f",res1, res2);
|
|
|
|
ret = sscanf(buffer,"%f%f",&res11, &res12);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
|
2004-01-06 21:36:10 +00:00
|
|
|
|
|
|
|
/* check strings */
|
2003-05-13 23:38:46 +00:00
|
|
|
ret = sprintf(buffer," %s", pname);
|
|
|
|
ret = sscanf(buffer,"%*c%[^\n]",buffer1);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( ret == 1, "Error with format \"%s\"\n","%*c%[^\n]");
|
|
|
|
ok( strncmp(pname,buffer1,strlen(buffer1)) == 0, "Error with \"%s\" \"%s\"\n",pname, buffer1);
|
2004-01-06 21:36:10 +00:00
|
|
|
|
|
|
|
ret = sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( ret == 1, "Error with format \"%s\"\n","%*[a-cg-e]%c");
|
|
|
|
ok( buffer[0] == 'd', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
|
2004-01-06 21:36:10 +00:00
|
|
|
|
|
|
|
ret = sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok( ret == 1, "Error with format \"%s\"\n","%*[a-cd-dg-e]%c");
|
|
|
|
ok( buffer[0] == 'h', "Error with \"abcefgdh\" \"%c\"\n", buffer[0]);
|
2004-01-06 21:36:10 +00:00
|
|
|
|
|
|
|
/* check digits */
|
2003-10-10 00:00:35 +00:00
|
|
|
ret = sprintf(buffer,"%d:%d:%d",hour,min,sec);
|
|
|
|
ret = sscanf(buffer,"%d%n",&number,&number_so_far);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok(ret == 1 , "problem with format arg \"%%d%%n\"\n");
|
|
|
|
ok(number == hour,"Read wrong arg %d instead of %d\n",number, hour);
|
|
|
|
ok(number_so_far == 2,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
|
2004-01-06 21:36:10 +00:00
|
|
|
|
2003-10-10 00:00:35 +00:00
|
|
|
ret = sscanf(buffer+2,"%*c%n",&number_so_far);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok(ret == 0 , "problem with format arg \"%%*c%%n\"\n");
|
|
|
|
ok(number_so_far == 1,"Read wrong arg for \"%%n\" %d instead of 2\n",number_so_far);
|
2003-12-15 21:06:19 +00:00
|
|
|
|
|
|
|
/* Check %i according to bug 1878 */
|
|
|
|
strcpy(buffer,"123");
|
|
|
|
ret = sscanf(buffer, "%i", &result);
|
2005-01-28 11:27:26 +00:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok(result == 123, "Wrong number read\n");
|
2003-12-15 21:06:19 +00:00
|
|
|
ret = sscanf(buffer, "%d", &result);
|
2005-01-28 11:27:26 +00:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
2004-01-27 04:01:10 +00:00
|
|
|
ok(result == 123, "Wrong number read\n");
|
2005-01-28 11:27:26 +00:00
|
|
|
|
|
|
|
/* Check %c */
|
|
|
|
strcpy(buffer,"a");
|
|
|
|
c = 0x55;
|
|
|
|
ret = sscanf(buffer, "%c", &c);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(c == 'a', "Field incorrect: '%c'\n", c);
|
|
|
|
|
|
|
|
strcpy(buffer," a");
|
|
|
|
c = 0x55;
|
|
|
|
ret = sscanf(buffer, "%c", &c);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(c == ' ', "Field incorrect: '%c'\n", c);
|
|
|
|
|
|
|
|
strcpy(buffer,"18:59");
|
|
|
|
c = 0x55;
|
|
|
|
ret = sscanf(buffer, "%d:%d%c", &hour, &min, &c);
|
|
|
|
ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(hour == 18, "Field 1 incorrect: %d\n", hour);
|
|
|
|
ok(min == 59, "Field 2 incorrect: %d\n", min);
|
|
|
|
ok(c == 0x55, "Field 3 incorrect: 0x%02x\n", c);
|
2005-03-10 11:44:19 +00:00
|
|
|
|
|
|
|
/* Check %n (also whitespace in format strings and %s) */
|
|
|
|
buffer[0]=0; buffer1[0]=0;
|
|
|
|
ret = sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
|
|
|
|
ok(strcmp(buffer, "abc")==0, "First %%s read incorrectly: %s\n", buffer);
|
|
|
|
ok(strcmp(buffer1,"def")==0, "Second %%s read incorrectly: %s\n", buffer1);
|
|
|
|
ok(number_so_far==6, "%%n yielded wrong result: %d\n", number_so_far);
|
|
|
|
ok(ret == 2, "%%n shouldn't count as a conversion: %d\n", ret);
|
2002-10-30 23:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(scanf)
|
|
|
|
{
|
|
|
|
test_sscanf();
|
|
|
|
}
|