2002-10-31 00:49:03 +01: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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-10-31 00:49:03 +01:00
|
|
|
*/
|
|
|
|
|
2002-11-06 20:57:49 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2002-10-31 00:49:03 +01:00
|
|
|
#include "wine/test.h"
|
|
|
|
|
2019-10-17 00:09:16 +02:00
|
|
|
static void test_fscanf( void )
|
|
|
|
{
|
|
|
|
static const char file_name[] = "fscanf.tst";
|
|
|
|
static const char contents[] =
|
|
|
|
"line1\n"
|
|
|
|
"line2 "
|
|
|
|
;
|
|
|
|
char buf[1024];
|
|
|
|
FILE *fp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
fp = fopen(file_name, "wb");
|
|
|
|
ok(fp != NULL, "fp = %p\n", fp);
|
|
|
|
if(!fp) {
|
|
|
|
skip("failed to create temporary test file\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fprintf(fp, contents);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
fp = fopen(file_name, "rb");
|
|
|
|
ret = fscanf(fp, "%s", buf);
|
|
|
|
ok(ret == 1, "ret = %d\n", ret);
|
|
|
|
ok(strcmp(buf, "line1") == 0, "buf = %s\n", buf);
|
|
|
|
ret = fscanf(fp, "%s", buf);
|
|
|
|
ok(ret == 1, "ret = %d\n", ret);
|
|
|
|
ok(strcmp(buf, "line2") == 0, "buf = %s\n", buf);
|
|
|
|
ret = fscanf(fp, "%s", buf);
|
|
|
|
ok(ret == EOF, "ret = %d\n", ret);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
unlink(file_name);
|
|
|
|
}
|
|
|
|
|
2002-10-31 00:49:03 +01:00
|
|
|
static void test_sscanf( void )
|
|
|
|
{
|
2019-04-10 19:11:18 +02:00
|
|
|
/* use function pointers to bypass gcc builtin */
|
|
|
|
int (WINAPIV *p_sprintf)(char *buf, const char *fmt, ...);
|
|
|
|
int (WINAPIV *p_sscanf)(const char *buf, const char *fmt, ...);
|
2003-05-14 01:38:46 +02:00
|
|
|
char buffer[100], buffer1[100];
|
2002-12-20 01:35:41 +01:00
|
|
|
char format[20];
|
2003-01-11 21:53:22 +01:00
|
|
|
int result, ret;
|
2012-04-16 16:32:57 +02:00
|
|
|
LONGLONG result64;
|
2005-01-28 12:27:26 +01:00
|
|
|
char c;
|
2008-07-01 21:14:51 +02:00
|
|
|
void *ptr;
|
2003-09-25 22:29:40 +02:00
|
|
|
float res1= -82.6267f, res2= 27.76f, res11, res12;
|
2012-11-01 15:27:01 +01:00
|
|
|
double double_res;
|
2004-05-04 06:13:05 +02:00
|
|
|
static const char pname[]=" St. Petersburg, Florida\n";
|
2003-10-10 02:00:35 +02:00
|
|
|
int hour=21,min=59,sec=20;
|
|
|
|
int number,number_so_far;
|
2019-04-10 19:11:18 +02:00
|
|
|
HMODULE hmod = GetModuleHandleA("msvcrt.dll");
|
2003-01-11 21:53:22 +01:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
p_sprintf = (void *)GetProcAddress( hmod, "sprintf" );
|
|
|
|
p_sscanf = (void *)GetProcAddress( hmod, "sscanf" );
|
2004-05-04 06:13:05 +02:00
|
|
|
|
2003-01-11 21:53:22 +01:00
|
|
|
/* check EOF */
|
|
|
|
strcpy(buffer,"");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%d", &result);
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF );
|
2004-05-04 06:13:05 +02:00
|
|
|
|
2019-10-17 00:09:16 +02:00
|
|
|
ret = p_sscanf(" \t\n\n", "%s", buffer);
|
|
|
|
ok( ret == EOF, "ret = %d\n", ret );
|
|
|
|
|
|
|
|
buffer1[0] = 'a';
|
|
|
|
ret = p_sscanf("test\n", "%s%c", buffer, buffer1);
|
|
|
|
ok( ret == 2, "ret = %d\n", ret );
|
|
|
|
ok( buffer1[0] == '\n', "buffer1[0] = %d\n", buffer1[0] );
|
|
|
|
|
2008-07-01 21:14:51 +02:00
|
|
|
/* check %p */
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 );
|
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("0046F171", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x46F171,"sscanf reads %p instead of %x\n", ptr, 0x46F171 );
|
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("46F172", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x46F172,"sscanf reads %p instead of %x\n", ptr, 0x46F172 );
|
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("0x46F173", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-12-05 07:47:01 +01:00
|
|
|
ok( ptr == NULL,"sscanf reads %p instead of %x\n", ptr, 0 );
|
2008-07-01 21:14:51 +02:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("-46F174", "%p", &ptr) == 1, "sscanf failed\n" );
|
2009-01-04 14:11:08 +01:00
|
|
|
ok( ptr == (void *)(ULONG_PTR)-0x46f174,"sscanf reads %p instead of %p\n",
|
|
|
|
ptr, (void *)(ULONG_PTR)-0x46f174 );
|
2008-07-01 21:14:51 +02:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("+46F175", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x46F175,"sscanf reads %p instead of %x\n", ptr, 0x46F175 );
|
|
|
|
|
|
|
|
/* check %p with no hex digits */
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("1233", "%p", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x1233,"sscanf reads %p instead of %x\n", ptr, 0x1233 );
|
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf("1234", "%P", &ptr) == 1, "sscanf failed\n" );
|
2008-07-01 21:14:51 +02:00
|
|
|
ok( ptr == (void *)0x1234,"sscanf reads %p instead of %x\n", ptr, 0x1234 );
|
|
|
|
|
2003-01-11 21:53:22 +01:00
|
|
|
/* check %x */
|
2002-10-31 00:49:03 +01:00
|
|
|
strcpy(buffer,"0x519");
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( result == 0x519,"sscanf reads %x instead of %x\n", result, 0x519 );
|
2002-10-31 00:49:03 +01:00
|
|
|
|
|
|
|
strcpy(buffer,"0x51a");
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( result == 0x51a ,"sscanf reads %x instead of %x\n", result, 0x51a );
|
2002-10-31 00:49:03 +01:00
|
|
|
|
|
|
|
strcpy(buffer,"0x51g");
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf(buffer, "%x", &result) == 1, "sscanf failed\n" );
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( result == 0x51, "sscanf reads %x instead of %x\n", result, 0x51 );
|
2002-12-20 01:35:41 +01:00
|
|
|
|
2006-03-26 04:10:28 +02:00
|
|
|
result = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("-1", "%x", &result);
|
2006-03-26 04:10:28 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
|
|
|
|
ok(result == -1, "Read %d, expected -1\n", result);
|
|
|
|
|
2002-12-20 01:35:41 +01:00
|
|
|
/* check % followed by any char */
|
|
|
|
strcpy(buffer,"\"%12@");
|
|
|
|
strcpy(format,"%\"%%%d%@"); /* work around gcc format check */
|
2019-04-10 19:11:18 +02:00
|
|
|
ok( p_sscanf(buffer, format, &result) == 1, "sscanf failed\n" );
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( result == 12, "sscanf reads %x instead of %x\n", result, 12 );
|
2003-05-14 01:38:46 +02:00
|
|
|
|
2004-01-06 22:36:10 +01:00
|
|
|
/* Check float */
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sprintf(buffer,"%f %f",res1, res2);
|
2011-02-07 23:58:09 +01:00
|
|
|
ok( ret == 20, "expected 20, got %u\n", ret);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer,"%f%f",&res11, &res12);
|
2011-02-07 23:58:09 +01:00
|
|
|
ok( ret == 2, "expected 2, got %u\n", ret);
|
2004-01-27 05:01:10 +01:00
|
|
|
ok( (res11 == res1) && (res12 == res2), "Error reading floats\n");
|
2004-01-06 22:36:10 +01:00
|
|
|
|
2012-11-01 15:27:01 +01:00
|
|
|
/* Check double */
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sprintf(buffer, "%lf", 32.715);
|
2012-11-01 15:27:01 +01:00
|
|
|
ok(ret == 9, "expected 9, got %u\n", ret);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%lf", &double_res);
|
2012-11-01 15:27:01 +01:00
|
|
|
ok(ret == 1, "expected 1, got %u\n", ret);
|
|
|
|
ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%Lf", &double_res);
|
2012-11-01 15:27:01 +01:00
|
|
|
ok(ret == 1, "expected 1, got %u\n", ret);
|
|
|
|
ok(double_res == 32.715, "Got %lf, expected %lf\n", double_res, 32.715);
|
|
|
|
|
2012-11-01 15:27:29 +01:00
|
|
|
strcpy(buffer, "1.1e-30");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%lf", &double_res);
|
2012-11-01 15:27:29 +01:00
|
|
|
ok(ret == 1, "expected 1, got %u\n", ret);
|
|
|
|
ok(double_res >= 1.1e-30-1e-45 && double_res <= 1.1e-30+1e-45,
|
|
|
|
"Got %.18le, expected %.18le\n", double_res, 1.1e-30);
|
|
|
|
|
2020-01-13 20:17:17 +01:00
|
|
|
buffer[0] = 0;
|
|
|
|
double_res = 1;
|
|
|
|
ret = p_sscanf(buffer, "%lf", &double_res);
|
|
|
|
ok(ret == -1, "expected 0, got %u\n", ret);
|
|
|
|
ok(double_res == 1, "Got %lf, expected 1\n", double_res);
|
|
|
|
|
2004-01-06 22:36:10 +01:00
|
|
|
/* check strings */
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sprintf(buffer," %s", pname);
|
2011-02-07 23:58:09 +01:00
|
|
|
ok( ret == 26, "expected 26, got %u\n", ret);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer,"%*c%[^\n]",buffer1);
|
2004-01-27 05:01:10 +01: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 22:36:10 +01:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("abcefgdh","%*[a-cg-e]%c",&buffer[0]);
|
2004-01-27 05:01:10 +01: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 22:36:10 +01:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("abcefgdh","%*[a-cd-dg-e]%c",&buffer[0]);
|
2004-01-27 05:01:10 +01: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 22:36:10 +01:00
|
|
|
|
2020-11-02 16:00:16 +01:00
|
|
|
ret = p_sscanf("-123", "%[-0-9]", buffer);
|
|
|
|
ok( ret == 1, "Error with format \"%s\"\n", "%[-0-9]");
|
|
|
|
ok( strcmp("-123", buffer) == 0, "Error with \"-123\" \"%s\"\n", buffer);
|
|
|
|
|
|
|
|
ret = p_sscanf("-321", "%[0-9-]", buffer);
|
|
|
|
ok( ret == 1, "Error with format \"%s\"\n", "%[0-9-]");
|
|
|
|
ok( strcmp("-321", buffer) == 0, "Error with \"-321\" \"%s\"\n", buffer);
|
|
|
|
|
|
|
|
ret = p_sscanf("-4123", "%[1-2-4]", buffer);
|
|
|
|
ok( ret == 1, "Error with format \"%s\"\n", "%[1-2-4]");
|
|
|
|
ok( strcmp("-412", buffer) == 0, "Error with \"-412\" \"%s\"\n", buffer);
|
|
|
|
|
|
|
|
ret = p_sscanf("-456123", "%[1-2-45-6]", buffer);
|
|
|
|
ok( ret == 1, "Error with format \"%s\"\n", "%[1-2-45-6]");
|
|
|
|
ok( strcmp("-45612", buffer) == 0, "Error with \"-45612\" \"%s\"\n", buffer);
|
|
|
|
|
2014-01-16 11:39:44 +01:00
|
|
|
buffer1[0] = 'b';
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("a","%s%s", buffer, buffer1);
|
2014-01-16 11:39:44 +01:00
|
|
|
ok( ret == 1, "expected 1, got %u\n", ret);
|
|
|
|
ok( buffer[0] == 'a', "buffer[0] = '%c'\n", buffer[0]);
|
|
|
|
ok( buffer[1] == '\0', "buffer[1] = '%c'\n", buffer[1]);
|
|
|
|
ok( buffer1[0] == 'b', "buffer1[0] = '%c'\n", buffer1[0]);
|
|
|
|
|
2004-01-06 22:36:10 +01:00
|
|
|
/* check digits */
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sprintf(buffer,"%d:%d:%d",hour,min,sec);
|
2011-02-07 23:58:09 +01:00
|
|
|
ok( ret == 8, "expected 8, got %u\n", ret);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer,"%d%n",&number,&number_so_far);
|
2004-01-27 05:01:10 +01: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 22:36:10 +01:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer+2,"%*c%n",&number_so_far);
|
2004-01-27 05:01:10 +01: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 22:06:19 +01:00
|
|
|
|
2012-04-16 16:32:57 +02:00
|
|
|
result = 0xdeadbeef;
|
|
|
|
strcpy(buffer,"12345678");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%hd", &result);
|
2012-04-16 16:32:57 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xdead614e, "Wrong number read (%x)\n", result);
|
|
|
|
|
2019-12-06 15:57:09 +01:00
|
|
|
result = 0xdeadbeef;
|
|
|
|
strcpy(buffer,"12345678");
|
|
|
|
ret = p_sscanf(buffer, "%02hd", &result);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xdead000c, "Wrong number read (%x)\n", result);
|
|
|
|
|
|
|
|
result = 0xdeadbeef;
|
|
|
|
strcpy(buffer,"12345678");
|
|
|
|
ret = p_sscanf(buffer, "%h02d", &result);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xdead000c, "Wrong number read (%x)\n", result);
|
|
|
|
|
|
|
|
result = 0xdeadbeef;
|
|
|
|
strcpy(buffer,"12345678");
|
|
|
|
ret = p_sscanf(buffer, "%000h02d", &result);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xdead000c, "Wrong number read (%x)\n", result);
|
|
|
|
|
|
|
|
result = 0xdeadbeef;
|
|
|
|
strcpy(buffer,"12345678");
|
|
|
|
ret = p_sscanf(buffer, "%2h0d", &result);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xdead614e, "Wrong number read (%x)\n", result);
|
|
|
|
|
2012-04-16 16:32:57 +02:00
|
|
|
result = 0xdeadbeef;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%hhd", &result);
|
2012-04-16 16:32:57 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 0xbc614e, "Wrong number read (%x)\n", result);
|
|
|
|
|
|
|
|
strcpy(buffer,"12345678901234");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%lld", &result64);
|
2012-04-16 16:32:57 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sprintf(buffer1, "%lld", result64);
|
2015-11-26 23:23:09 +01:00
|
|
|
ok(ret==14 || broken(ret==10), "sprintf returned %d\n", ret);
|
2012-04-16 16:32:57 +02:00
|
|
|
if(ret == 14)
|
|
|
|
ok(!strcmp(buffer, buffer1), "got %s, expected %s\n", buffer1, buffer);
|
|
|
|
|
2003-12-15 22:06:19 +01:00
|
|
|
/* Check %i according to bug 1878 */
|
|
|
|
strcpy(buffer,"123");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%i", &result);
|
2005-01-28 12:27:26 +01:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
2004-01-27 05:01:10 +01:00
|
|
|
ok(result == 123, "Wrong number read\n");
|
2006-03-26 04:10:28 +02:00
|
|
|
result = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("-1", "%i", &result);
|
2006-03-26 04:10:28 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
|
|
|
|
ok(result == -1, "Read %d, expected -1\n", result);
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%d", &result);
|
2005-01-28 12:27:26 +01:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
2004-01-27 05:01:10 +01:00
|
|
|
ok(result == 123, "Wrong number read\n");
|
2006-03-26 04:10:28 +02:00
|
|
|
result = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("-1", "%d", &result);
|
2006-03-26 04:10:28 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
|
|
|
|
ok(result == -1, "Read %d, expected -1\n", result);
|
|
|
|
|
2006-08-22 19:38:56 +02:00
|
|
|
/* Check %i for octal and hexadecimal input */
|
|
|
|
result = 0;
|
|
|
|
strcpy(buffer,"017");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%i", &result);
|
2006-08-22 19:38:56 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 15, "Wrong number read\n");
|
|
|
|
result = 0;
|
|
|
|
strcpy(buffer,"0x17");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%i", &result);
|
2006-08-22 19:38:56 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(result == 23, "Wrong number read\n");
|
|
|
|
|
2006-03-26 04:10:28 +02:00
|
|
|
/* %o */
|
|
|
|
result = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("-1", "%o", &result);
|
2006-03-26 04:10:28 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
|
|
|
|
ok(result == -1, "Read %d, expected -1\n", result);
|
|
|
|
|
|
|
|
/* %u */
|
|
|
|
result = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("-1", "%u", &result);
|
2006-03-26 04:10:28 +02:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d (expected 1)\n", ret);
|
|
|
|
ok(result == -1, "Read %d, expected -1\n", result);
|
2005-01-28 12:27:26 +01:00
|
|
|
|
|
|
|
/* Check %c */
|
|
|
|
strcpy(buffer,"a");
|
|
|
|
c = 0x55;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%c", &c);
|
2005-01-28 12:27:26 +01:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(c == 'a', "Field incorrect: '%c'\n", c);
|
|
|
|
|
|
|
|
strcpy(buffer," a");
|
|
|
|
c = 0x55;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%c", &c);
|
2005-01-28 12:27:26 +01:00
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(c == ' ', "Field incorrect: '%c'\n", c);
|
|
|
|
|
|
|
|
strcpy(buffer,"18:59");
|
|
|
|
c = 0x55;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%d:%d%c", &hour, &min, &c);
|
2005-01-28 12:27:26 +01:00
|
|
|
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 12:44:19 +01:00
|
|
|
|
|
|
|
/* Check %n (also whitespace in format strings and %s) */
|
|
|
|
buffer[0]=0; buffer1[0]=0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("abc def", "%s %n%s", buffer, &number_so_far, buffer1);
|
2005-03-10 12:44:19 +01:00
|
|
|
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);
|
2007-01-17 05:14:27 +01:00
|
|
|
|
|
|
|
/* Check where %n matches to EOF in buffer */
|
|
|
|
strcpy(buffer, "3:45");
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf(buffer, "%d:%d%n", &hour, &min, &number_so_far);
|
2007-01-17 05:14:27 +01:00
|
|
|
ok(ret == 2, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(number_so_far == 4, "%%n yielded wrong result: %d\n", number_so_far);
|
2015-12-22 23:14:28 +01:00
|
|
|
|
|
|
|
buffer[0] = 0;
|
|
|
|
buffer1[0] = 0;
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("test=value\xda", "%[^=] = %[^;]", buffer, buffer1);
|
2015-12-22 23:14:28 +01:00
|
|
|
ok(ret == 2, "got %d\n", ret);
|
|
|
|
ok(!strcmp(buffer, "test"), "buf %s\n", buffer);
|
|
|
|
ok(!strcmp(buffer1, "value\xda"), "buf %s\n", buffer1);
|
2017-03-31 15:02:48 +02:00
|
|
|
|
2019-04-10 19:11:18 +02:00
|
|
|
ret = p_sscanf("\x81\x82test", "\x81%\x82%s", buffer);
|
2017-03-31 15:02:48 +02:00
|
|
|
ok(ret == 1, "got %d\n", ret);
|
|
|
|
ok(!strcmp(buffer, "test"), "buf = %s\n", buffer);
|
2002-10-31 00:49:03 +01:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:56:24 +02:00
|
|
|
static void test_sscanf_s(void)
|
|
|
|
{
|
2017-11-02 10:19:18 +01:00
|
|
|
int (WINAPIV *psscanf_s)(const char*,const char*,...);
|
2010-04-22 17:56:24 +02:00
|
|
|
HMODULE hmod = GetModuleHandleA("msvcrt.dll");
|
|
|
|
int i, ret;
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
psscanf_s = (void*)GetProcAddress(hmod, "sscanf_s");
|
|
|
|
if(!psscanf_s) {
|
|
|
|
win_skip("sscanf_s not available\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = psscanf_s("123", "%d", &i);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(i == 123, "i = %d\n", i);
|
|
|
|
|
|
|
|
ret = psscanf_s("123", "%s", buf, 100);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(!strcmp("123", buf), "buf = %s\n", buf);
|
|
|
|
|
|
|
|
ret = psscanf_s("123", "%s", buf, 3);
|
|
|
|
ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(buf[0]=='\0', "buf = %s\n", buf);
|
|
|
|
|
2018-07-09 14:45:28 +02:00
|
|
|
memset(buf, 'a', sizeof(buf));
|
2010-04-22 17:56:24 +02:00
|
|
|
ret = psscanf_s("123", "%3c", buf, 2);
|
|
|
|
ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(buf[0]=='\0', "buf = %s\n", buf);
|
2018-07-09 14:45:28 +02:00
|
|
|
ok(buf[1]=='2', "buf[1] = %d\n", buf[1]);
|
|
|
|
ok(buf[2]=='a', "buf[2] = %d\n", buf[2]);
|
|
|
|
|
|
|
|
buf[3] = 'a';
|
|
|
|
buf[4] = 0;
|
|
|
|
ret = psscanf_s("123", "%3c", buf, 3);
|
|
|
|
ok(!strcmp("123a", buf), "buf = %s\n", buf);
|
2010-04-22 17:56:24 +02:00
|
|
|
|
|
|
|
i = 1;
|
|
|
|
ret = psscanf_s("123 123", "%s %d", buf, 2, &i);
|
|
|
|
ok(ret == 0, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(i==1, "i = %d\n", i);
|
|
|
|
|
|
|
|
i = 1;
|
|
|
|
ret = psscanf_s("123 123", "%d %s", &i, buf, 2);
|
|
|
|
ok(ret == 1, "Wrong number of arguments read: %d\n", ret);
|
|
|
|
ok(i==123, "i = %d\n", i);
|
|
|
|
}
|
|
|
|
|
2012-01-04 22:58:23 +01:00
|
|
|
static void test_swscanf( void )
|
|
|
|
{
|
2019-10-17 00:09:16 +02:00
|
|
|
wchar_t buffer[100], results[100];
|
2012-01-04 22:58:23 +01:00
|
|
|
int result, ret;
|
2017-04-03 12:45:58 +02:00
|
|
|
WCHAR c;
|
2012-01-04 22:58:23 +01:00
|
|
|
|
|
|
|
/* check WEOF */
|
|
|
|
/* WEOF is an unsigned short -1 but swscanf returns int
|
|
|
|
so it should be sign-extended */
|
|
|
|
buffer[0] = 0;
|
2020-11-27 14:16:56 +01:00
|
|
|
ret = swscanf(buffer, L"%d", &result);
|
2012-01-04 22:58:23 +01:00
|
|
|
/* msvcrt returns 0 but should return -1 (later versions do) */
|
|
|
|
ok( ret == (short)WEOF || broken(ret == 0),
|
|
|
|
"swscanf returns %x instead of %x\n", ret, WEOF );
|
2017-04-03 12:45:58 +02:00
|
|
|
|
2019-10-17 00:09:16 +02:00
|
|
|
ret = swscanf(L" \t\n\n", L"%s", results);
|
|
|
|
/* sscanf returns EOF under this case, but swscanf does not return WEOF */
|
|
|
|
ok( ret == 0, "ret = %d\n", ret );
|
|
|
|
|
2017-04-03 12:45:58 +02:00
|
|
|
buffer[0] = 'a';
|
|
|
|
buffer[1] = 0x1234;
|
|
|
|
buffer[2] = 0x1234;
|
|
|
|
buffer[3] = 'b';
|
2020-11-27 14:16:56 +01:00
|
|
|
ret = swscanf(buffer, L"a\x1234%\x1234%c", &c);
|
2017-04-03 12:45:58 +02:00
|
|
|
ok(ret == 1, "swscanf returned %d\n", ret);
|
|
|
|
ok(c == 'b', "c = %x\n", c);
|
2012-01-04 22:58:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 12:29:08 +01:00
|
|
|
static void test_swscanf_s(void)
|
|
|
|
{
|
|
|
|
int (WINAPIV *pswscanf_s)(const wchar_t*,const wchar_t*,...);
|
|
|
|
HMODULE hmod = GetModuleHandleA("msvcrt.dll");
|
|
|
|
wchar_t buf[2], out[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pswscanf_s = (void*)GetProcAddress(hmod, "swscanf_s");
|
|
|
|
if(!pswscanf_s) {
|
|
|
|
win_skip("swscanf_s not available\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = 'a';
|
|
|
|
buf[1] = '1';
|
|
|
|
out[1] = 'b';
|
2020-11-27 14:16:56 +01:00
|
|
|
ret = pswscanf_s(buf, L"%c", out, 1);
|
2017-12-01 12:29:08 +01:00
|
|
|
ok(ret == 1, "swscanf_s returned %d\n", ret);
|
|
|
|
ok(out[0] == 'a', "out[0] = %x\n", out[0]);
|
|
|
|
ok(out[1] == 'b', "out[1] = %x\n", out[1]);
|
|
|
|
|
2020-11-27 14:16:56 +01:00
|
|
|
ret = pswscanf_s(buf, L"%[a-z]", out, 1);
|
2017-12-01 12:29:08 +01:00
|
|
|
ok(!ret, "swscanf_s returned %d\n", ret);
|
|
|
|
|
2020-11-27 14:16:56 +01:00
|
|
|
ret = pswscanf_s(buf, L"%[a-z]", out, 2);
|
2017-12-01 12:29:08 +01:00
|
|
|
ok(ret == 1, "swscanf_s returned %d\n", ret);
|
|
|
|
ok(out[0] == 'a', "out[0] = %x\n", out[0]);
|
|
|
|
ok(!out[1], "out[1] = %x\n", out[1]);
|
|
|
|
}
|
|
|
|
|
2002-10-31 00:49:03 +01:00
|
|
|
START_TEST(scanf)
|
|
|
|
{
|
2019-10-17 00:09:16 +02:00
|
|
|
test_fscanf();
|
2002-10-31 00:49:03 +01:00
|
|
|
test_sscanf();
|
2010-04-22 17:56:24 +02:00
|
|
|
test_sscanf_s();
|
2012-01-04 22:58:23 +01:00
|
|
|
test_swscanf();
|
2017-12-01 12:29:08 +01:00
|
|
|
test_swscanf_s();
|
2002-10-31 00:49:03 +01:00
|
|
|
}
|