*/
#include <stdio.h>
-#include <_PDCLIB_glue.h>
#ifndef REGTEST
+#include <_PDCLIB_glue.h>
+
#include <stdbool.h>
#include <string.h>
-size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+size_t fread_unlocked( void * _PDCLIB_restrict ptr,
+ size_t size, size_t nmemb,
+ struct _PDCLIB_file_t * _PDCLIB_restrict stream )
{
if ( _PDCLIB_prepread( stream ) == EOF )
{
return nmemb_i;
}
+size_t fread( void * _PDCLIB_restrict ptr,
+ size_t size, size_t nmemb,
+ struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+ flockfile( stream );
+ size_t r = fread_unlocked( ptr, size, nmemb, stream );
+ funlockfile( stream );
+ return r;
+}
+
#endif
#ifdef TEST
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;
}