From: solar Date: Fri, 30 Jun 2006 09:56:02 +0000 (+0000) Subject: Yet closer to functional output. X-Git-Tag: v0.5~153 X-Git-Url: https://pd.if.org/git/?p=pdclib;a=commitdiff_plain;h=f603f88b4456cf9b7ab1328bf657ede22a0c9940 Yet closer to functional output. --- diff --git a/functions/stdio/clearerr.c b/functions/stdio/clearerr.c index 6d80c8a..4c51d71 100644 --- a/functions/stdio/clearerr.c +++ b/functions/stdio/clearerr.c @@ -22,7 +22,36 @@ void clearerr( struct _PDCLIB_file_t * stream ) int main( void ) { +#ifndef REGTEST + FILE file = { 0, { 0 }, NULL, 0, 0, 0, NULL }; + FILE * fh = &file; + TESTCASE( ! ferror( fh ) ); + TESTCASE( ! feof( fh ) ); + fh->status |= _PDCLIB_ERRORFLAG; + TESTCASE( ferror( fh ) ); + TESTCASE( ! feof( fh ) ); + clearerr( fh ); + TESTCASE( ! ferror( fh ) ); + TESTCASE( ! feof( fh ) ); + fh->status |= _PDCLIB_EOFFLAG; + TESTCASE( ! ferror( fh ) ); + TESTCASE( feof( fh ) ); + clearerr( fh ); + TESTCASE( ! ferror( fh ) ); + TESTCASE( ! feof( fh ) ); + fh->status |= _PDCLIB_EOFFLAG | _PDCLIB_ERRORFLAG; + TESTCASE( ferror( fh ) ); + TESTCASE( feof( fh ) ); + clearerr( fh ); + TESTCASE( ! ferror( fh ) ); + TESTCASE( ! feof( fh ) ); +#else + /* TODO: The above is ad-hoc and PDCLib specific. A better test driver + should be internals-agnostic (provoking the error / eof flag by + "regular" operations). + */ TESTCASE( NO_TESTDRIVER ); +#endif return TEST_RESULTS; } diff --git a/functions/stdio/feof.c b/functions/stdio/feof.c index 13c4b64..784ea4e 100644 --- a/functions/stdio/feof.c +++ b/functions/stdio/feof.c @@ -22,7 +22,7 @@ int feof( struct _PDCLIB_file_t * stream ) int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* Testing covered by clearerr(). */ return TEST_RESULTS; } diff --git a/functions/stdio/ferror.c b/functions/stdio/ferror.c index b6f51ee..6f24080 100644 --- a/functions/stdio/ferror.c +++ b/functions/stdio/ferror.c @@ -22,7 +22,7 @@ int ferror( struct _PDCLIB_file_t * stream ) int main( void ) { - TESTCASE( NO_TESTDRIVER ); + /* Testing covered by clearerr(). */ return TEST_RESULTS; } diff --git a/functions/stdio/fputc.c b/functions/stdio/fputc.c index 8f97378..e903611 100644 --- a/functions/stdio/fputc.c +++ b/functions/stdio/fputc.c @@ -21,13 +21,17 @@ int fputc( int c, struct _PDCLIB_file_t * stream ) */ stream->buffer[stream->bufidx++] = (char)c; if ( ( stream->bufidx == stream->bufsize ) /* _IOFBF */ - || ( ( stream->status & _IOLBF ) && (char)c == '\n' ) /* _IOLBF */ + || ( ( stream->status & _IOLBF ) && ( (char)c == '\n' ) ) /* _IOLBF */ || ( stream->status & _IONBF ) /* _IONBF */ ) { /* buffer filled, unbuffered stream, or end-of-line. */ fflush( stream ); } + else + { + stream->status |= _PDCLIB_WROTELAST; + } return c; } @@ -38,7 +42,15 @@ int fputc( int c, struct _PDCLIB_file_t * stream ) int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * fh; + char buffer[100]; + TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); + TESTCASE( fputc( '!', fh ) == '!' ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL ); + TESTCASE( fread( buffer, 1, 1, fh ) == 1 ); + TESTCASE( buffer[0] == '!' ); + TESTCASE( fclose( fh ) == 0 ); return TEST_RESULTS; } diff --git a/functions/stdio/fputs.c b/functions/stdio/fputs.c index 716fef8..748fa5a 100644 --- a/functions/stdio/fputs.c +++ b/functions/stdio/fputs.c @@ -39,9 +39,21 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest #ifdef TEST #include <_PDCLIB_test.h> +#include + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * fh; + char buffer[100]; + char text[] = "SUCCESS testing fputs()."; + TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); + TESTCASE( fputs( text, fh ) != EOF ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL ); + TESTCASE( fread( buffer, 1, strlen( text ), fh ) == strlen( text ) ); + TESTCASE( memcmp( buffer, text, strlen( text ) ) == 0 ); + TESTCASE( fclose( fh ) == 0 ); + TESTCASE( remove( "testfile" ) == 0 ); return TEST_RESULTS; } diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c new file mode 100644 index 0000000..83bc3b7 --- /dev/null +++ b/functions/stdio/fread.c @@ -0,0 +1,30 @@ +/* $Id$ */ + +/* fread( void *, size_t, size_t, FILE * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include <_PDCLIB_glue.h> + +#ifndef REGTEST + +size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + return _PDCLIB_read( stream->handle, ptr, size * nmemb ); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + /* Testing handled by fwrite(). */ + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdio/fwrite.c b/functions/stdio/fwrite.c new file mode 100644 index 0000000..8765d0b --- /dev/null +++ b/functions/stdio/fwrite.c @@ -0,0 +1,35 @@ +/* $Id$ */ + +/* fwrite( const void *, size_t, size_t, FILE * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include <_PDCLIB_glue.h> + +#ifndef REGTEST + +size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +{ + return _PDCLIB_write( stream->handle, ptr, size * nmemb ); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + FILE * fh; + TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); + TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 ); + TESTCASE( fclose( fh ) == 0 ); + /* TODO: Add readback test. */ + TESTCASE( remove( "testfile" ) == 0 ); + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdio/printf.c b/functions/stdio/printf.c index ac3dfc3..c3c46cf 100644 --- a/functions/stdio/printf.c +++ b/functions/stdio/printf.c @@ -28,7 +28,7 @@ int printf( const char * _PDCLIB_restrict format, ... ) int main( void ) { - TESTCASE( NO_TESTDRIVER ); + TESTCASE( printf( "SUCCESS testing printf().\n" ) == 26 ); return TEST_RESULTS; } diff --git a/functions/stdio/vfprintf.c b/functions/stdio/vfprintf.c index 0288f6d..9a9a950 100644 --- a/functions/stdio/vfprintf.c +++ b/functions/stdio/vfprintf.c @@ -24,6 +24,7 @@ int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDC { /* No conversion specifier, print verbatim */ putc( *(format++), stream ); + status.i++; } else { @@ -37,6 +38,7 @@ int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDC #endif #ifdef TEST +#include #include <_PDCLIB_test.h> static int testprintf( FILE * stream, const char * format, ... ) @@ -52,13 +54,10 @@ static int testprintf( FILE * stream, const char * format, ... ) int main( void ) { FILE * fh; - TESTCASE( testprintf( stdout, "Hallo\n" ) == 6 ); -#if 0 TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); - TESTCASE( testprintf( fh, "Hallo\n" ) ); + TESTCASE( testprintf( fh, "Hallo\n" ) == 6 ); TESTCASE( fclose( fh ) == 0 ); - TESTCASE( remove( "testfile" ) == 0 ); -#endif + /* FIXME: Testfile doesn't exist... */ return TEST_RESULTS; } diff --git a/functions/stdlib/_Exit.c b/functions/stdlib/_Exit.c index 4a24b29..7871385 100644 --- a/functions/stdlib/_Exit.c +++ b/functions/stdlib/_Exit.c @@ -7,13 +7,16 @@ */ #include +#include #include <_PDCLIB_glue.h> #ifndef REGTEST void _Exit( int status ) { - /* TODO: Flush and close open streams. Remove tmpfile() files. */ + /* TODO: Flush and close open streams. Remove tmpfile() files. Make this + called on process termination automatically. + */ _PDCLIB_Exit( status ); } diff --git a/functions/stdlib/malloc.c b/functions/stdlib/malloc.c index 769c447..ab0920b 100644 --- a/functions/stdlib/malloc.c +++ b/functions/stdlib/malloc.c @@ -121,12 +121,12 @@ void * malloc( size_t size ) if ( _PDCLIB_memlist.last == NULL ) { _PDCLIB_memlist.first = splitnode; - splitnode->next = NULL; } else { _PDCLIB_memlist.last->next = splitnode; } + splitnode->next = NULL; /* TODO: This is bug #7, uncovered by testdriver yet. */ _PDCLIB_memlist.last = splitnode; } return (char *)newnode + sizeof( struct _PDCLIB_memnode_t ); diff --git a/platform/example/functions/_PDCLIB/read.c b/platform/example/functions/_PDCLIB/read.c new file mode 100644 index 0000000..db6418d --- /dev/null +++ b/platform/example/functions/_PDCLIB/read.c @@ -0,0 +1,31 @@ +/* $Id$ */ + +/* _PDCLIB_read( _PDCLIB_fd_t, char *, _PDCLIB_size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_glue.h> + +#ifndef REGTEST + +int read(int, void *, unsigned int); + +_PDCLIB_size_t _PDCLIB_read( _PDCLIB_fd_t fd, char * buffer, _PDCLIB_size_t n ) +{ + return read( fd, buffer, n ); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} + +#endif diff --git a/platform/example/functions/_PDCLIB/stdinit.c b/platform/example/functions/_PDCLIB/stdinit.c index baf6985..160dac3 100644 --- a/platform/example/functions/_PDCLIB/stdinit.c +++ b/platform/example/functions/_PDCLIB/stdinit.c @@ -21,10 +21,12 @@ /* TODO: This is proof-of-concept, requires finetuning. */ static char _PDCLIB_sin_buffer[BUFSIZ]; static char _PDCLIB_sout_buffer[BUFSIZ]; +static char _PDCLIB_serr_buffer[BUFSIZ]; -static struct _PDCLIB_file_t _PDCLIB_serr = { 2, { 0 }, NULL, 0, 0, 0u, /* mbstate, */ NULL }; -static struct _PDCLIB_file_t _PDCLIB_sout = { 1, { 0 }, _PDCLIB_sout_buffer, BUFSIZ, 0, 0u, /* mbstate, */ &_PDCLIB_serr }; -static struct _PDCLIB_file_t _PDCLIB_sin = { 0, { 0 }, _PDCLIB_sin_buffer, BUFSIZ, 0, 0u, /* mbstate, */ &_PDCLIB_sout }; +/* FIXME: serr should handle one character. Buffering on out / in? */ +static struct _PDCLIB_file_t _PDCLIB_serr = { 2, { 0 }, _PDCLIB_serr_buffer, BUFSIZ, 0, _IONBF, /* mbstate, */ NULL }; +static struct _PDCLIB_file_t _PDCLIB_sout = { 1, { 0 }, _PDCLIB_sout_buffer, BUFSIZ, 0, _IOLBF, /* mbstate, */ &_PDCLIB_serr }; +static struct _PDCLIB_file_t _PDCLIB_sin = { 0, { 0 }, _PDCLIB_sin_buffer, BUFSIZ, 0, _IOLBF, /* mbstate, */ &_PDCLIB_sout }; struct _PDCLIB_file_t * stdin = &_PDCLIB_sin; struct _PDCLIB_file_t * stdout = &_PDCLIB_sout; diff --git a/platform/example/functions/_PDCLIB/write.c b/platform/example/functions/_PDCLIB/write.c new file mode 100644 index 0000000..2489234 --- /dev/null +++ b/platform/example/functions/_PDCLIB/write.c @@ -0,0 +1,31 @@ +/* $Id$ */ + +/* _PDCLIB_write( _PDCLIB_fd_t, char const *, _PDCLIB_size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_glue.h> + +#ifndef REGTEST + +int write(int, const void *, unsigned int); + +_PDCLIB_size_t _PDCLIB_write( _PDCLIB_fd_t fd, char const * buffer, _PDCLIB_size_t n ) +{ + return write( fd, buffer, n ); +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + TESTCASE( NO_TESTDRIVER ); + return TEST_RESULTS; +} + +#endif