X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Ffread.c;h=9f7d3fa0e9135f57166e656469c6e09f2100dbe2;hp=52abb73478e05ea19597afb229695e3631164393;hb=dd71cbbfbd68cd8c653277a0c1f70ab2b179421c;hpb=bdad1f82d0c0aa0b83e8b195303d7957c578da2e 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; }