From 185c9e2dc3a58f33f12d406049b36350c93dc00b Mon Sep 17 00:00:00 2001 From: solar Date: Thu, 2 Dec 2010 06:20:16 +0000 Subject: [PATCH] Using constants for testfile names git-svn-id: https://srv7.svn-repos.de/dev34/pdclib/trunk@474 546481bc-9713-0410-bf18-d3337bbf4a3e --- functions/stdio/fclose.c | 14 +++---- functions/stdio/fopen.c | 12 +++--- platform/example/functions/_PDCLIB/open.c | 18 ++++---- platform/example/functions/_PDCLIB/rename.c | 39 +++++++++--------- .../example_cygwin/functions/_PDCLIB/open.c | 18 ++++---- .../example_cygwin/functions/_PDCLIB/rename.c | 41 ++++++++++--------- testing/_PDCLIB_test.h | 19 ++++++--- 7 files changed, 86 insertions(+), 75 deletions(-) diff --git a/functions/stdio/fclose.c b/functions/stdio/fclose.c index 456acc1..cfea902 100644 --- a/functions/stdio/fclose.c +++ b/functions/stdio/fclose.c @@ -72,23 +72,23 @@ int main( void ) #ifndef REGTEST struct _PDCLIB_file_t * file1; struct _PDCLIB_file_t * file2; - remove( "testing/testfile1" ); - remove( "testing/testfile2" ); + remove( testfile1 ); + remove( testfile2 ); TESTCASE( _PDCLIB_filelist == stdin ); - TESTCASE( ( file1 = fopen( "testing/testfile1", "w" ) ) != NULL ); + TESTCASE( ( file1 = fopen( testfile1, "w" ) ) != NULL ); TESTCASE( _PDCLIB_filelist == file1 ); - TESTCASE( ( file2 = fopen( "testing/testfile2", "w" ) ) != NULL ); + TESTCASE( ( file2 = fopen( testfile2, "w" ) ) != NULL ); TESTCASE( _PDCLIB_filelist == file2 ); TESTCASE( fclose( file2 ) == 0 ); TESTCASE( _PDCLIB_filelist == file1 ); - TESTCASE( ( file2 = fopen( "testing/testfile1", "w" ) ) != NULL ); + TESTCASE( ( file2 = fopen( testfile1, "w" ) ) != NULL ); TESTCASE( _PDCLIB_filelist == file2 ); TESTCASE( fclose( file1 ) == 0 ); TESTCASE( _PDCLIB_filelist == file2 ); TESTCASE( fclose( file2 ) == 0 ); TESTCASE( _PDCLIB_filelist == stdin ); - remove( "testing/testfile1" ); - remove( "testing/testfile2" ); + remove( testfile1 ); + remove( testfile2 ); #else puts( " NOTEST fclose() test driver is PDCLib-specific." ); #endif diff --git a/functions/stdio/fopen.c b/functions/stdio/fopen.c index bc9fab4..32a66a1 100644 --- a/functions/stdio/fopen.c +++ b/functions/stdio/fopen.c @@ -83,16 +83,18 @@ int main( void ) my system is at once less forgiving (segfaults on mode NULL) and more forgiving (accepts undefined modes). */ - remove( "testing/testfile" ); + FILE * fh; + remove( testfile ); TESTCASE_NOREG( fopen( NULL, NULL ) == NULL ); TESTCASE( fopen( NULL, "w" ) == NULL ); TESTCASE_NOREG( fopen( "", NULL ) == NULL ); TESTCASE( fopen( "", "w" ) == NULL ); TESTCASE( fopen( "foo", "" ) == NULL ); - TESTCASE_NOREG( fopen( "testing/testfile", "wq" ) == NULL ); /* Undefined mode */ - TESTCASE_NOREG( fopen( "testing/testfile", "wr" ) == NULL ); /* Undefined mode */ - TESTCASE( fopen( "testing/testfile", "w" ) != NULL ); - remove( "testing/testfile" ); + TESTCASE_NOREG( fopen( testfile, "wq" ) == NULL ); /* Undefined mode */ + TESTCASE_NOREG( fopen( testfile, "wr" ) == NULL ); /* Undefined mode */ + TESTCASE( ( fh = fopen( testfile, "w" ) ) != NULL ); + TESTCASE( fclose( fh ) == 0 ); + remove( testfile ); return TEST_RESULTS; } diff --git a/platform/example/functions/_PDCLIB/open.c b/platform/example/functions/_PDCLIB/open.c index 133b729..0230558 100644 --- a/platform/example/functions/_PDCLIB/open.c +++ b/platform/example/functions/_PDCLIB/open.c @@ -103,28 +103,28 @@ int main( void ) */ int fh; char buffer[ 10 ]; - remove( "testfile" ); + remove( testfile ); /* Trying to read non-existent file. */ - TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); + TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); /* Writing to file, trying to read from it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading from file, trying to write to it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending to file, trying to read from it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "app", 3 ) == 3 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == -1 ); TESTCASE( write( fh, "end", 3 ) == 3 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading and writing from file ("r+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); @@ -134,7 +134,7 @@ int main( void ) TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Writing and reading from file ("w+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 ); TESTCASE( read( fh, buffer, 2 ) == 2 ); @@ -145,14 +145,14 @@ int main( void ) TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending and reading from file ("a+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "baby", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Cleaning up. */ - TESTCASE( remove( "testfile" ) == 0 ); + TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; } diff --git a/platform/example/functions/_PDCLIB/rename.c b/platform/example/functions/_PDCLIB/rename.c index 53cf84c..f314e4c 100644 --- a/platform/example/functions/_PDCLIB/rename.c +++ b/platform/example/functions/_PDCLIB/rename.c @@ -90,42 +90,41 @@ int _PDCLIB_rename( const char * old, const char * new ) int main( void ) { - char filename1[] = "touch testfile1"; - char filename2[] = "testfile2"; FILE * file; - remove( filename1 + 6 ); - remove( filename2 ); + remove( testfile1 ); + remove( testfile2 ); /* check that neither file exists */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) == NULL ); + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( fopen( testfile2, "r" ) == NULL ); /* rename file 1 to file 2 - expected to fail */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 ); /* create file 1 */ - system( filename1 ); + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputc( 'x', file ) == 'x' ); + TESTCASE( fclose( file ) == 0 ); /* check that file 1 exists */ - TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL ); TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 ); /* check that file 2 exists, file 1 does not */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL ); + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL ); TESTCASE( fclose( file ) == 0 ); /* create another file 1 */ - system( filename1 ); + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputc( 'x', file ) == 'x' ); + TESTCASE( fclose( file ) == 0 ); /* check that file 1 exists */ - TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL ); TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 - expected to fail, see comment in _PDCLIB_rename() itself. */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 ); /* remove both files */ - remove( filename1 + 6 ); - remove( filename2 ); - /* check that they're gone */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) == NULL ); + remove( testfile1 ); + remove( testfile2 ); return TEST_RESULTS; } diff --git a/platform/example_cygwin/functions/_PDCLIB/open.c b/platform/example_cygwin/functions/_PDCLIB/open.c index f0379ba..88af3c4 100644 --- a/platform/example_cygwin/functions/_PDCLIB/open.c +++ b/platform/example_cygwin/functions/_PDCLIB/open.c @@ -105,28 +105,28 @@ int main( void ) */ int fh; char buffer[ 10 ]; - remove( "testfile" ); + remove( testfile ); /* Trying to read non-existent file. */ - TESTCASE( _PDCLIB_open( "testfile", _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); + TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); /* Writing to file, trying to read from it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading from file, trying to write to it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending to file, trying to read from it. */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "app", 3 ) == 3 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == -1 ); TESTCASE( write( fh, "end", 3 ) == 3 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading and writing from file ("r+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); @@ -136,7 +136,7 @@ int main( void ) TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Writing and reading from file ("w+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 ); TESTCASE( read( fh, buffer, 2 ) == 2 ); @@ -147,14 +147,14 @@ int main( void ) TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending and reading from file ("a+"). */ - TESTCASE( ( fh = _PDCLIB_open( "testfile", _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); + TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "baby", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Cleaning up. */ - TESTCASE( remove( "testfile" ) == 0 ); + TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; } diff --git a/platform/example_cygwin/functions/_PDCLIB/rename.c b/platform/example_cygwin/functions/_PDCLIB/rename.c index 3f770c9..e8e7cb1 100644 --- a/platform/example_cygwin/functions/_PDCLIB/rename.c +++ b/platform/example_cygwin/functions/_PDCLIB/rename.c @@ -90,38 +90,39 @@ int _PDCLIB_rename( const char * old, const char * new ) int main( void ) { - char filename1[] = "touch testfile1"; - char filename2[] = "testfile2"; - remove( filename1 + 6 ); - remove( filename2 ); + FILE * file; + remove( testfile1 ); + remove( testfile2 ); /* check that neither file exists */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) == NULL ); + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( fopen( testfile2, "r" ) == NULL ); /* rename file 1 to file 2 - expected to fail */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 ); /* create file 1 */ - system( filename1 ); + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputc( 'x', file ) == 'x' ); + TESTCASE( fclose( file ) == 0 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( fopen( testfile1, "r" ) != NULL ); /* rename file 1 to file 2 */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 ); /* check that file 2 exists, file 1 does not */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) != NULL ); + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( fopen( testfile2, "r" ) != NULL ); /* create another file 1 */ - system( filename1 ); + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputc( 'x', file ) == 'x' ); + TESTCASE( fclose( file ) == 0 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 - expected to fail, see comment in _PDCLIB_rename() itself. */ - TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 ); + TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 ); /* remove both files */ - remove( filename1 + 6 ); - remove( filename2 ); - /* check that they're gone */ - TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) == NULL ); + remove( testfile1 ); + remove( testfile2 ); return TEST_RESULTS; } diff --git a/testing/_PDCLIB_test.h b/testing/_PDCLIB_test.h index f39ce8a..aacd704 100644 --- a/testing/_PDCLIB_test.h +++ b/testing/_PDCLIB_test.h @@ -12,9 +12,12 @@ #include +/* Some strings used for and testing. */ static char const abcde[] = "abcde"; static char const abcdx[] = "abcdx"; static char const teststring[] = "1234567890\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n"; + +/* Temporary file names */ static char const testfile[]="testing/testfile"; static char const testfile1[]="testing/testfile1"; static char const testfile2[]="testing/testfile2"; @@ -23,9 +26,18 @@ static char const testfile2[]="testing/testfile2"; static int TEST_RESULTS = 0; +/* TESTCASE() - generic test */ #define TESTCASE( x ) if ( x ) {} \ else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); } +/* TESTCASE_NOREG() - PDCLib-only test */ +#ifndef REGTEST +#define TESTCASE_NOREG( x ) TESTCASE( x ) +#else +#define TESTCASE_NOREG( x ) +#endif + +/* ...printf() tests */ #if defined( FPRINTF_FUNCTION ) static char result_buffer[ 1000 ]; #define RESULT_MISMATCH( act, exp ) \ @@ -49,8 +61,5 @@ static char result_buffer[ 1000 ]; } \ } while ( 0 ) -#ifndef REGTEST -#define TESTCASE_NOREG( x ) TESTCASE( x ) -#else -#define TESTCASE_NOREG( x ) -#endif +/* ...scanf() tests */ +/* TODO: t.b.d. */ -- 2.30.2