From: solar Date: Mon, 20 Dec 2010 05:17:35 +0000 (+0000) Subject: Some testcases for scanf(). Fixed bug with %*... . Fixed artifact in Makefile. X-Git-Tag: v0.5~2 X-Git-Url: https://pd.if.org/git/?a=commitdiff_plain;ds=sidebyside;h=31fdebd6d624ffce068548cb4d63f4ec583cec04;hp=d193d54478243fbd9e68649e0eb534c72d6aea0d;p=pdclib Some testcases for scanf(). Fixed bug with %*... . Fixed artifact in Makefile. --- diff --git a/Makefile b/Makefile index 2993c25..2d1fa6d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # $Id$ # This is where you chose which platform to compile for (see 'make links' / './platform') -PLATFORM := example_cygwin +PLATFORM := example # This is a list of all non-source files that are part of the distribution. AUXFILES := Makefile Readme.txt diff --git a/functions/_PDCLIB/scan.c b/functions/_PDCLIB/scan.c index 6bef32e..15b1d50 100644 --- a/functions/_PDCLIB/scan.c +++ b/functions/_PDCLIB/scan.c @@ -519,67 +519,70 @@ const char * _PDCLIB_scan( const char * spec, struct _PDCLIB_status_t * status ) return NULL; } /* convert value to target type and assign to parameter */ - switch ( status->flags & ( E_char | E_short | E_long | E_llong | - E_intmax | E_size | E_ptrdiff | - E_unsigned ) ) + if ( ! ( status->flags & E_suppressed ) ) { - case E_char: - *( va_arg( status->arg, char * ) ) = (char)( value * sign ); - break; - case E_char | E_unsigned: - *( va_arg( status->arg, unsigned char * ) ) = (unsigned char)( value * sign ); - break; + switch ( status->flags & ( E_char | E_short | E_long | E_llong | + E_intmax | E_size | E_ptrdiff | + E_unsigned ) ) + { + case E_char: + *( va_arg( status->arg, char * ) ) = (char)( value * sign ); + break; + case E_char | E_unsigned: + *( va_arg( status->arg, unsigned char * ) ) = (unsigned char)( value * sign ); + break; - case E_short: - *( va_arg( status->arg, short * ) ) = (short)( value * sign ); - break; - case E_short | E_unsigned: - *( va_arg( status->arg, unsigned short * ) ) = (unsigned short)( value * sign ); - break; + case E_short: + *( va_arg( status->arg, short * ) ) = (short)( value * sign ); + break; + case E_short | E_unsigned: + *( va_arg( status->arg, unsigned short * ) ) = (unsigned short)( value * sign ); + break; - case 0: - *( va_arg( status->arg, int * ) ) = (int)( value * sign ); - break; - case E_unsigned: - *( va_arg( status->arg, unsigned int * ) ) = (unsigned int)( value * sign ); - break; + case 0: + *( va_arg( status->arg, int * ) ) = (int)( value * sign ); + break; + case E_unsigned: + *( va_arg( status->arg, unsigned int * ) ) = (unsigned int)( value * sign ); + break; - case E_long: - *( va_arg( status->arg, long * ) ) = (long)( value * sign ); - break; - case E_long | E_unsigned: - *( va_arg( status->arg, unsigned long * ) ) = (unsigned long)( value * sign ); - break; + case E_long: + *( va_arg( status->arg, long * ) ) = (long)( value * sign ); + break; + case E_long | E_unsigned: + *( va_arg( status->arg, unsigned long * ) ) = (unsigned long)( value * sign ); + break; - case E_llong: - *( va_arg( status->arg, long long * ) ) = (long long)( value * sign ); - break; - case E_llong | E_unsigned: - *( va_arg( status->arg, unsigned long long * ) ) = (unsigned long long)( value * sign ); - break; + case E_llong: + *( va_arg( status->arg, long long * ) ) = (long long)( value * sign ); + break; + case E_llong | E_unsigned: + *( va_arg( status->arg, unsigned long long * ) ) = (unsigned long long)( value * sign ); + break; - case E_intmax: - *( va_arg( status->arg, intmax_t * ) ) = (intmax_t)( value * sign ); - break; - case E_intmax | E_unsigned: - *( va_arg( status->arg, uintmax_t * ) ) = (uintmax_t)( value * sign ); - break; + case E_intmax: + *( va_arg( status->arg, intmax_t * ) ) = (intmax_t)( value * sign ); + break; + case E_intmax | E_unsigned: + *( va_arg( status->arg, uintmax_t * ) ) = (uintmax_t)( value * sign ); + break; - case E_size: - /* E_size always implies unsigned */ - *( va_arg( status->arg, size_t * ) ) = (size_t)( value * sign ); - break; + case E_size: + /* E_size always implies unsigned */ + *( va_arg( status->arg, size_t * ) ) = (size_t)( value * sign ); + break; - case E_ptrdiff: - /* E_ptrdiff always implies signed */ - *( va_arg( status->arg, ptrdiff_t * ) ) = (ptrdiff_t)( value * sign ); - break; + case E_ptrdiff: + /* E_ptrdiff always implies signed */ + *( va_arg( status->arg, ptrdiff_t * ) ) = (ptrdiff_t)( value * sign ); + break; - default: - puts( "UNSUPPORTED SCANF FLAG COMBINATION" ); - return NULL; /* behaviour unspecified */ + default: + puts( "UNSUPPORTED SCANF FLAG COMBINATION" ); + return NULL; /* behaviour unspecified */ + } + ++(status->n); } - ++(status->n); return ++spec; } /* TODO: Floats. */ diff --git a/testing/scanf_testcases.h b/testing/scanf_testcases.h index f8d3b0f..a1521fe 100644 --- a/testing/scanf_testcases.h +++ b/testing/scanf_testcases.h @@ -1,5 +1,59 @@ { char buffer[100]; + int i; + unsigned int u; + /* basic: reading of three-char string */ SCANF_TEST( 1, "foo", "%3c", buffer ); TESTCASE( memcmp( buffer, "foo", 3 ) == 0 ); +#ifndef TEST_CONVERSION_ONLY + /* %% for single % */ + SCANF_TEST( 1, "%x", "%%%c", buffer ); + TESTCASE( buffer[0] == 'x' ); + /* * to skip assignment */ + SCANF_TEST( 1, "3xfoo", "%*dx%3c", buffer ); + TESTCASE( memcmp( buffer, "foo", 3 ) == 0 ); +#endif + /* domain testing on 'int' type */ + SCANF_TEST( 1, "-" INT_MIN_DEZ_STR, "%d", &i ); + TESTCASE( i == INT_MIN ); + SCANF_TEST( 1, INT_MAX_DEZ_STR, "%d", &i ); + TESTCASE( i == INT_MAX ); + SCANF_TEST( 1, "-1", "%d", &i ); + TESTCASE( i == -1 ); + SCANF_TEST( 1, "0", "%d", &i ); + TESTCASE( i == 0 ); + SCANF_TEST( 1, "1", "%d", &i ); + TESTCASE( i == 1 ); + SCANF_TEST( 1, "-" INT_MIN_DEZ_STR, "%i", &i ); + TESTCASE( i == INT_MIN ); + SCANF_TEST( 1, INT_MAX_DEZ_STR, "%i", &i ); + TESTCASE( i == INT_MAX ); + SCANF_TEST( 1, "-1", "%i", &i ); + TESTCASE( i == -1 ); + SCANF_TEST( 1, "0", "%i", &i ); + TESTCASE( i == 0 ); + SCANF_TEST( 1, "1", "%i", &i ); + TESTCASE( i == 1 ); + SCANF_TEST( 1, "0x7" INT_HEXDIG, "%i", &i ); + TESTCASE( i == INT_MAX ); + SCANF_TEST( 1, "0x0", "%i", &i ); + TESTCASE( i == 0 ); +#ifndef TEST_CONVERSION_ONLY + SCANF_TEST( 1, "00", "%i%n", &i, &u ); + TESTCASE( i == 0 ); + TESTCASE( u == 2 ); +#endif + /* domain testing on 'unsigned int' type */ + SCANF_TEST( 1, UINT_MAX_DEZ_STR, "%u", &u ); + TESTCASE( u == UINT_MAX ); + SCANF_TEST( 1, "0", "%u", &u ); + TESTCASE( u == 0 ); + SCANF_TEST( 1, "f" INT_HEXDIG, "%x", &u ); + TESTCASE( u == UINT_MAX ); + SCANF_TEST( 1, "7" INT_HEXDIG, "%x", &u ); + TESTCASE( u == INT_MAX ); + SCANF_TEST( 1, "0", "%o", &u ); + TESTCASE( u == 0 ); + SCANF_TEST( 1, INT_OCTDIG, "%o", &u ); + TESTCASE( u == UINT_MAX ); }