From dd71cbbfbd68cd8c653277a0c1f70ab2b179421c Mon Sep 17 00:00:00 2001 From: solar Date: Thu, 16 Dec 2010 06:00:24 +0000 Subject: [PATCH] Cleaning up TODOs. --- Makefile | 1 - functions/inttypes/strtoumax.c | 18 +++++-- functions/stdio/fread.c | 28 ++++++++++- functions/stdio/ftell.c | 7 +++ functions/stdio/fwrite.c | 6 +-- functions/stdio/setvbuf.c | 1 - functions/stdio/vfscanf.c | 6 +-- platform/example/functions/signal/raise.c | 4 +- .../example_cygwin/functions/signal/raise.c | 49 +++++++++++++++++-- 9 files changed, 99 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 4531725..2993c25 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,6 @@ pdclib.a: $(OBJFILES) @echo test: functions/$(FILE) - echo $(TSTDEPFILES) functions/$(FILE) tests: testdrivers diff --git a/functions/inttypes/strtoumax.c b/functions/inttypes/strtoumax.c index 9c9833e..c54cfce 100644 --- a/functions/inttypes/strtoumax.c +++ b/functions/inttypes/strtoumax.c @@ -78,20 +78,30 @@ int main( void ) errno = 0; /* uintmax_t -> long long -> 64 bit */ #if UINTMAX_MAX >> 63 == 1 - /* testing "even" overflow, i.e. base is power of two */ + /* testing "odd" overflow, i.e. base is not 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 */ + /* testing "even" overflow, i.e. base is power of two */ + errno = 0; + TESTCASE( strtoumax( "0xFFFFFFFFFFFFFFFF", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoumax( "0x10000000000000000", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == ERANGE ); /* uintmax_t -> long long -> 128 bit */ #elif UINTMAX_MAX >> 127 == 1 - /* testing "even" overflow, i.e. base is power of two */ + /* testing "odd" overflow, i.e. base is not 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 */ + /* testing "even" everflow, i.e. base is power of two */ + errno = 0; + TESTCASE( strtoumax( "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == 0 ); + TESTCASE( strtoumax( "0x100000000000000000000000000000000", NULL, 0 ) == UINTMAX_MAX ); + TESTCASE( errno == ERANGE ); #else #error Unsupported width of 'uintmax_t' (neither 64 nor 128 bit). #endif diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index 52abb73..9f7d3fa 100644 --- a/functions/stdio/fread.c +++ b/functions/stdio/fread.c @@ -49,9 +49,33 @@ size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PD int main( void ) { FILE * fh; + char const * message = "Testing fwrite()...\n"; + char buffer[21]; + buffer[20] = 'x'; TESTCASE( ( fh = tmpfile() ) != NULL ); - TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 ); - /* TODO: Add readback test. */ + /* fwrite() / readback */ + TESTCASE( fwrite( message, 1, 20, fh ) == 20 ); + rewind( fh ); + TESTCASE( fread( buffer, 1, 20, fh ) == 20 ); + TESTCASE( memcmp( buffer, message, 20 ) == 0 ); + TESTCASE( buffer[20] == 'x' ); + /* same, different nmemb / size settings */ + rewind( fh ); + TESTCASE( memset( buffer, '\0', 20 ) == buffer ); + TESTCASE( fwrite( message, 5, 4, fh ) == 4 ); + rewind( fh ); + TESTCASE( fread( buffer, 5, 4, fh ) == 4 ); + TESTCASE( memcmp( buffer, message, 20 ) == 0 ); + TESTCASE( buffer[20] == 'x' ); + /* same... */ + rewind( fh ); + TESTCASE( memset( buffer, '\0', 20 ) == buffer ); + TESTCASE( fwrite( message, 20, 1, fh ) == 1 ); + rewind( fh ); + TESTCASE( fread( buffer, 20, 1, fh ) == 1 ); + TESTCASE( memcmp( buffer, message, 20 ) == 0 ); + TESTCASE( buffer[20] == 'x' ); + /* Done. */ TESTCASE( fclose( fh ) == 0 ); return TEST_RESULTS; } diff --git a/functions/stdio/ftell.c b/functions/stdio/ftell.c index 29c360a..27e25ee 100644 --- a/functions/stdio/ftell.c +++ b/functions/stdio/ftell.c @@ -62,6 +62,13 @@ int main( void ) FILE * fh; TESTCASE( ( fh = tmpfile() ) != NULL ); TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 ); + /* Testing ungetc() at offset 0 */ + rewind( fh ); + TESTCASE( ungetc( 'x', fh ) == 'x' ); + TESTCASE( ftell( fh ) == -1l ); + rewind( fh ); + TESTCASE( ftell( fh ) == 0l ); + /* Commence "normal" tests */ TESTCASE( fputc( '1', fh ) == '1' ); TESTCASE( fputc( '2', fh ) == '2' ); TESTCASE( fputc( '3', fh ) == '3' ); diff --git a/functions/stdio/fwrite.c b/functions/stdio/fwrite.c index 8b40551..0ea1b02 100644 --- a/functions/stdio/fwrite.c +++ b/functions/stdio/fwrite.c @@ -87,11 +87,7 @@ size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, str int main( void ) { - FILE * fh; - TESTCASE( ( fh = tmpfile() ) != NULL ); - TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 ); - /* TODO: Add readback test. */ - TESTCASE( fclose( fh ) == 0 ); + /* Testing covered by fread(). */ return TEST_RESULTS; } diff --git a/functions/stdio/setvbuf.c b/functions/stdio/setvbuf.c index ecb0be5..e3f9278 100644 --- a/functions/stdio/setvbuf.c +++ b/functions/stdio/setvbuf.c @@ -21,7 +21,6 @@ int setvbuf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, char * _PDCLIB_res we don't want to e.g. flush the stream for every character of a stream being printed. */ - /* TODO: Check this */ break; case _IOFBF: case _IOLBF: diff --git a/functions/stdio/vfscanf.c b/functions/stdio/vfscanf.c index fbf5356..b9a6896 100644 --- a/functions/stdio/vfscanf.c +++ b/functions/stdio/vfscanf.c @@ -42,7 +42,7 @@ int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict forma { ++status.i; } - if ( ! feof( stream ) ) /* TODO: Check EOF status directly */ + if ( ! feof( stream ) ) { ungetc( c, stream ); } @@ -50,10 +50,10 @@ int vfscanf( FILE * _PDCLIB_restrict stream, const char * _PDCLIB_restrict forma else { /* Non-whitespace char in format string: Match verbatim */ - if ( ( ( c = getc( stream ) ) != *format ) || feof( stream ) ) /* TODO: Check EOF status directly */ + if ( ( ( c = getc( stream ) ) != *format ) || feof( stream ) ) { /* Matching error */ - if ( ! feof( stream ) && ! ferror( stream ) ) /* TODO: Check EOF status directly */ + if ( ! feof( stream ) && ! ferror( stream ) ) { ungetc( c, stream ); } diff --git a/platform/example/functions/signal/raise.c b/platform/example/functions/signal/raise.c index 307c876..8d7080b 100644 --- a/platform/example/functions/signal/raise.c +++ b/platform/example/functions/signal/raise.c @@ -51,8 +51,8 @@ int raise( int sig ) message = "Termination request (SIGTERM)"; break; default: - /* TODO: Implement. */ - break; + fprintf( stderr, "Unknown signal #%d\n", sig ); + _Exit( EXIT_FAILURE ); } if ( sighandler == SIG_DFL ) { diff --git a/platform/example_cygwin/functions/signal/raise.c b/platform/example_cygwin/functions/signal/raise.c index b16249c..8d7080b 100644 --- a/platform/example_cygwin/functions/signal/raise.c +++ b/platform/example_cygwin/functions/signal/raise.c @@ -10,6 +10,7 @@ #ifndef REGTEST +#include #include extern void (*_PDCLIB_sigabrt)( int ); @@ -22,32 +23,40 @@ extern void (*_PDCLIB_sigterm)( int ); int raise( int sig ) { void (*sighandler)( int ); + char const * message; switch ( sig ) { case SIGABRT: sighandler = _PDCLIB_sigabrt; + message = "Abnormal termination (SIGABRT)"; break; case SIGFPE: sighandler = _PDCLIB_sigfpe; + message = "Arithmetic exception (SIGFPE)"; break; case SIGILL: sighandler = _PDCLIB_sigill; + message = "Illegal instruction (SIGILL)"; break; case SIGINT: sighandler = _PDCLIB_sigint; + message = "Interactive attention signal (SIGINT)"; break; case SIGSEGV: sighandler = _PDCLIB_sigsegv; + message = "Invalid memory access (SIGSEGV)"; break; case SIGTERM: sighandler = _PDCLIB_sigterm; + message = "Termination request (SIGTERM)"; break; default: - /* TODO: Implement. */ - break; + fprintf( stderr, "Unknown signal #%d\n", sig ); + _Exit( EXIT_FAILURE ); } if ( sighandler == SIG_DFL ) { + fputs( message, stderr ); _Exit( EXIT_FAILURE ); } else if ( sighandler != SIG_IGN ) @@ -63,9 +72,43 @@ int raise( int sig ) #ifdef TEST #include <_PDCLIB_test.h> +#include + +static volatile sig_atomic_t flag = 0; + +static int expected_signal = 0; + +static void test_handler( int sig ) +{ + TESTCASE( sig == expected_signal ); + flag = 1; +} + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* Could be other than SIG_DFL if you changed the implementation. */ + TESTCASE( signal( SIGABRT, SIG_IGN ) == SIG_DFL ); + /* Should be ignored. */ + TESTCASE( raise( SIGABRT ) == 0 ); + /* Installing test handler, old handler should be returned */ + TESTCASE( signal( SIGABRT, test_handler ) == SIG_IGN ); + /* Raising and checking SIGABRT */ + expected_signal = SIGABRT; + TESTCASE( raise( SIGABRT ) == 0 ); + TESTCASE( flag == 1 ); + /* Re-installing test handler, should have been reset to default */ + /* Could be other than SIG_DFL if you changed the implementation. */ + TESTCASE( signal( SIGABRT, test_handler ) == SIG_DFL ); + /* Raising and checking SIGABRT */ + flag = 0; + TESTCASE( raise( SIGABRT ) == 0 ); + TESTCASE( flag == 1 ); + /* Installing test handler for different signal... */ + TESTCASE( signal( SIGTERM, test_handler ) == SIG_DFL ); + /* Raising and checking SIGTERM */ + expected_signal = SIGTERM; + TESTCASE( raise( SIGTERM ) == 0 ); + TESTCASE( flag == 1 ); return TEST_RESULTS; } #endif -- 2.40.0