X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffread.c;h=5b093ff6fb2507ced8640c00f6fbcf0345cb78e8;hb=d865c4403fc91d1f1ac95ba76febcee9f429bb97;hp=51e6a7b37a5d8766134dc581e395808d4a2b57de;hpb=6c8c4f80e32177f27f89b4aff3b7568a7afd4041;p=pdclib diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index 51e6a7b..5b093ff 100644 --- a/functions/stdio/fread.c +++ b/functions/stdio/fread.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* fwrite( void *, size_t, size_t, FILE * ) This file is part of the Public Domain C Library (PDCLib). @@ -7,12 +5,12 @@ */ #include -#include <_PDCLIB_glue.h> +#include +#include #ifndef REGTEST -#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 ) { @@ -43,17 +41,41 @@ size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PD #endif #ifdef TEST -#include <_PDCLIB_test.h> + +#include "_PDCLIB_test.h" 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; } #endif -