From 45f394553721285e7bc6187419613c33915cce21 Mon Sep 17 00:00:00 2001 From: solar Date: Wed, 23 Jun 2010 08:28:28 +0000 Subject: [PATCH] Extended test drivers to cover sign error. --- functions/inttypes/strtoimax.c | 25 ++++++++++++------------- functions/inttypes/strtoumax.c | 18 ++++++++++++++++++ functions/stdlib/strtol.c | 6 +++--- functions/stdlib/strtoll.c | 6 +++--- functions/stdlib/strtoul.c | 23 +++++++++++++++++++++++ functions/stdlib/strtoull.c | 18 ++++++++++++++++++ internals/_PDCLIB_int.h | 2 +- 7 files changed, 78 insertions(+), 20 deletions(-) diff --git a/functions/inttypes/strtoimax.c b/functions/inttypes/strtoimax.c index af29d3d..936389c 100755 --- a/functions/inttypes/strtoimax.c +++ b/functions/inttypes/strtoimax.c @@ -26,7 +26,6 @@ intmax_t strtoimax( const char * _PDCLIB_restrict nptr, char ** _PDCLIB_restrict else { /* FIXME: This breaks on some machines that round negatives wrongly */ - /* FIXME: Sign error not caught by testdriver */ rc = (intmax_t)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)INTMAX_MIN, (uintmax_t)( INTMAX_MIN / -base ), (int)( -( INTMAX_MIN % base ) ), &sign ); } if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) nptr; @@ -91,34 +90,34 @@ int main( void ) errno = 0; #if INTMAX_MAX == 0x7fffffffffffffffLL /* testing "even" overflow, i.e. base is power of two */ - TESTCASE( strtoimax( "0x7FFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffff ); + TESTCASE( strtoimax( "9223372036854775807", NULL, 0 ) == INTMAX_MAX ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "0x8000000000000000", NULL, 0 ) == INTMAX_MAX ); + TESTCASE( strtoimax( "9223372036854775808", NULL, 0 ) == INTMAX_MAX ); TESTCASE( errno == ERANGE ); errno = 0; - TESTCASE( strtoimax( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == (long long)0x8000000000000001 ); + TESTCASE( strtoimax( "-9223372036854775807", NULL, 0 ) == (INTMAX_MIN + 1) ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "-0x8000000000000000", NULL, 0 ) == INTMAX_MIN ); + TESTCASE( strtoimax( "-9223372036854775808", NULL, 0 ) == INTMAX_MIN ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "-0x8000000000000001", NULL, 0 ) == INTMAX_MIN ); + TESTCASE( strtoimax( "-9223372036854775809", NULL, 0 ) == INTMAX_MIN ); TESTCASE( errno == ERANGE ); /* TODO: test "odd" overflow, i.e. base is not power of two */ -#elif INTMAX_MAX == 0x7fffffffffffffffffffffffffffffffLL +#elif LLONG_MAX == 0x7fffffffffffffffffffffffffffffffLL /* testing "even" overflow, i.e. base is power of two */ - TESTCASE( strtoimax( "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == 0x7fffffffffffffffffffffffffffffff ); + TESTCASE( strtoimax( "170141183460469231731687303715884105728", NULL, 0 ) == INTMAX_MAX ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MAX ); + TESTCASE( strtoimax( "170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MAX ); TESTCASE( errno == ERANGE ); errno = 0; - TESTCASE( strtoimax( "-0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == -0x80000000000000000000000000000001 ); + TESTCASE( strtoimax( "-170141183460469231731687303715884105728", NULL, 0 ) == (INTMAX_MIN + 1) ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "-0x80000000000000000000000000000000", NULL, 0 ) == INTMAX_MIN ); + TESTCASE( strtoimax( "-170141183460469231731687303715884105729", NULL, 0 ) == INTMAX_MIN ); TESTCASE( errno == 0 ); - TESTCASE( strtoimax( "-0x80000000000000000000000000000001", NULL, 0 ) == INTMAX_MIN ); + TESTCASE( strtoimax( "-170141183460469231731687303715884105730", NULL, 0 ) == INTMAX_MIN ); TESTCASE( errno == ERANGE ); /* TODO: test "odd" overflow, i.e. base is not power of two */ #else -#error Unsupported width of 'long long' (neither 64 nor 128 bit). +#error Unsupported width of 'intmax_t' (neither 64 nor 128 bit). #endif return TEST_RESULTS; } diff --git a/functions/inttypes/strtoumax.c b/functions/inttypes/strtoumax.c index 063e7ab..0da7ec2 100755 --- a/functions/inttypes/strtoumax.c +++ b/functions/inttypes/strtoumax.c @@ -75,6 +75,24 @@ int main( void ) endptr = NULL; TESTCASE( strtoumax( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); + errno = 0; +#if UINTMAX_MAX == 0xffffffffffffffffLL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoumax( "18446744073709551615", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoumax( "18446744073709551616", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#elif UINTMAX_MAX == 0xffffffffffffffffffffffffffffffffLL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoumax( "340282366920938463463374607431768211455", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoumax( "340282366920938463463374607431768211456", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#else +#error Unsupported width of 'uintmax_t' (neither 64 nor 128 bit). +#endif return TEST_RESULTS; } diff --git a/functions/stdlib/strtol.c b/functions/stdlib/strtol.c index ff5aa8b..085abbb 100644 --- a/functions/stdlib/strtol.c +++ b/functions/stdlib/strtol.c @@ -84,9 +84,9 @@ int main( void ) endptr = NULL; TESTCASE( strtol( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); - /* These tests assume two-complement, but conversion should work for */ - /* one-complement and signed magnitude just as well. Anyone having a */ - /* platform to test this on? */ + /* TODO: These tests assume two-complement, but conversion should work */ + /* for one-complement and signed magnitude just as well. Anyone having */ + /* a platform to test this on? */ errno = 0; #if LONG_MAX == 0x7fffffffL /* testing "even" overflow, i.e. base is power of two */ diff --git a/functions/stdlib/strtoll.c b/functions/stdlib/strtoll.c index 13a1a1d..b447691 100644 --- a/functions/stdlib/strtoll.c +++ b/functions/stdlib/strtoll.c @@ -84,9 +84,9 @@ int main( void ) endptr = NULL; TESTCASE( strtoll( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); - /* These tests assume two-complement, but conversion should work for */ - /* one-complement and signed magnitude just as well. Anyone having a */ - /* platform to test this on? */ + /* TODO: These tests assume two-complement, but conversion should work */ + /* for one-complement and signed magnitude just as well. Anyone having */ + /* a platform to test this on? */ errno = 0; #if LLONG_MAX == 0x7fffffffffffffffLL /* testing "even" overflow, i.e. base is power of two */ diff --git a/functions/stdlib/strtoul.c b/functions/stdlib/strtoul.c index 4aefc4f..5680297 100644 --- a/functions/stdlib/strtoul.c +++ b/functions/stdlib/strtoul.c @@ -75,6 +75,29 @@ int main( void ) endptr = NULL; TESTCASE( strtoul( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); + /* TODO: These tests assume two-complement, but conversion should work */ + /* for one-complement and signed magnitude just as well. Anyone having */ + /* a platform to test this on? */ + errno = 0; +#if ULONG_MAX == 0xffffffffL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoul( "4294967295", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == 0 ); + errno = 0; + TESTCASE( strtoul( "4294967296", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#elif ULONG_MAX == 0xffffffffffffffffL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoul( "18446744073709551615", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == 0 ); + errno = 0; + TESTCASE( strtoul( "18446744073709551616", NULL, 0 ) == ULONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#else +#error Unsupported width of 'long' (neither 32 nor 64 bit). +#endif return TEST_RESULTS; } diff --git a/functions/stdlib/strtoull.c b/functions/stdlib/strtoull.c index a65779c..ca9dd19 100644 --- a/functions/stdlib/strtoull.c +++ b/functions/stdlib/strtoull.c @@ -75,6 +75,24 @@ int main( void ) endptr = NULL; TESTCASE( strtoull( overflow, &endptr, 0 ) == 0 ); TESTCASE( endptr == overflow ); + errno = 0; +#if ULLONG_MAX == 0xffffffffffffffffLL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoull( "18446744073709551615", NULL, 0 ) == ULLONG_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoull( "18446744073709551616", NULL, 0 ) == ULLONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#elif ULLONG_MAX == 0xffffffffffffffffffffffffffffffffLL + /* testing "even" overflow, i.e. base is power of two */ + TESTCASE( strtoull( "340282366920938463463374607431768211455", NULL, 0 ) == ULLONG_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoull( "340282366920938463463374607431768211456", NULL, 0 ) == ULLONG_MAX ); + TESTCASE( errno == ERANGE ); + /* TODO: test "odd" overflow, i.e. base is not power of two */ +#else +#error Unsupported width of 'long long' (neither 64 nor 128 bit). +#endif return TEST_RESULTS; } diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index f6fedd1..2420c79 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -101,7 +101,7 @@ #elif _PDCLIB_LLONG_BYTES == 16 #define _PDCLIB_LLONG_MAX 0x7fffffffffffffffffffffffffffffffLL #define _PDCLIB_LLONG_MIN (-0x7fffffffffffffffffffffffffffffffLL - 1LL) -#define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffLL +#define _PDCLIB_ULLONG_MAX 0xffffffffffffffffffffffffffffffffULL #else #error Unsupported width of 'long long' (neither 64 nor 128 bit). #endif -- 2.40.0