X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffread.c;h=9f7d3fa0e9135f57166e656469c6e09f2100dbe2;hb=b41576197133c1211d6ec353faf93f505f573b8a;hp=53e22a65961333e51ec8cd7d3ef42f0640bef82a;hpb=0c316ee3b4dc0712797877f5dc0f4a789646154d;p=pdclib diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index 53e22a6..9f7d3fa 100644 --- a/functions/stdio/fread.c +++ b/functions/stdio/fread.c @@ -7,13 +7,14 @@ */ #include + #ifndef REGTEST +#include <_PDCLIB_glue.h> + #include #include -#include <_PDCLIB_glue.h> - size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) { if ( _PDCLIB_prepread( stream ) == EOF ) @@ -48,12 +49,34 @@ size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PD int main( void ) { FILE * fh; - remove( "testfile" ); - TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL ); - TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 ); + char const * message = "Testing fwrite()...\n"; + char buffer[21]; + buffer[20] = 'x'; + TESTCASE( ( fh = tmpfile() ) != NULL ); + /* 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 ); - /* TODO: Add readback test. */ - TESTCASE( remove( "testfile" ) == 0 ); return TEST_RESULTS; }