X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Ffread.c;h=d1afc99be3016a720a7d2311e45f8315eaeaeaad;hb=b1fc26afebd4d557ff89a44bc21767a8704c3809;hp=b77b5dd4724d700dafdc2a242793bb8f047a705e;hpb=393020b6e48719d27699dea6b29e53025bbd5123;p=pdclib diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index b77b5dd..d1afc99 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,10 +5,11 @@ */ #include -#include <_PDCLIB_glue.h> #ifndef REGTEST +#include <_PDCLIB_glue.h> + #include #include @@ -48,12 +47,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; }