]> pd.if.org Git - pdclib.old/commitdiff
Yet closer to functional output.
authorsolar <>
Fri, 30 Jun 2006 09:56:02 +0000 (09:56 +0000)
committersolar <>
Fri, 30 Jun 2006 09:56:02 +0000 (09:56 +0000)
14 files changed:
functions/stdio/clearerr.c
functions/stdio/feof.c
functions/stdio/ferror.c
functions/stdio/fputc.c
functions/stdio/fputs.c
functions/stdio/fread.c [new file with mode: 0644]
functions/stdio/fwrite.c [new file with mode: 0644]
functions/stdio/printf.c
functions/stdio/vfprintf.c
functions/stdlib/_Exit.c
functions/stdlib/malloc.c
platform/example/functions/_PDCLIB/read.c [new file with mode: 0644]
platform/example/functions/_PDCLIB/stdinit.c
platform/example/functions/_PDCLIB/write.c [new file with mode: 0644]

index 6d80c8aeb381639c62b9fbf0a4194ff68b083bc7..4c51d714f54aefb4c019a6b8c216bd6820d19634 100644 (file)
@@ -22,7 +22,36 @@ void clearerr( struct _PDCLIB_file_t * stream )
 
 int main( void )
 {
+#ifndef REGTEST
+    FILE file = { 0, { 0 }, NULL, 0, 0, 0, NULL };
+    FILE * fh = &file;
+    TESTCASE( ! ferror( fh ) );
+    TESTCASE( ! feof( fh ) );
+    fh->status |= _PDCLIB_ERRORFLAG;
+    TESTCASE( ferror( fh ) );
+    TESTCASE( ! feof( fh ) );
+    clearerr( fh );
+    TESTCASE( ! ferror( fh ) );
+    TESTCASE( ! feof( fh ) );
+    fh->status |= _PDCLIB_EOFFLAG;
+    TESTCASE( ! ferror( fh ) );
+    TESTCASE( feof( fh ) );
+    clearerr( fh );
+    TESTCASE( ! ferror( fh ) );
+    TESTCASE( ! feof( fh ) );
+    fh->status |= _PDCLIB_EOFFLAG | _PDCLIB_ERRORFLAG;
+    TESTCASE( ferror( fh ) );
+    TESTCASE( feof( fh ) );
+    clearerr( fh );
+    TESTCASE( ! ferror( fh ) );
+    TESTCASE( ! feof( fh ) );
+#else
+    /* TODO: The above is ad-hoc and PDCLib specific. A better test driver
+       should be internals-agnostic (provoking the error / eof flag by
+       "regular" operations).
+    */
     TESTCASE( NO_TESTDRIVER );
+#endif
     return TEST_RESULTS;
 }
 
index 13c4b64b7e47fd54d84f4b220dee7be76e2db702..784ea4ed517e3a4dd3458de89f79ff7c56b16e72 100644 (file)
@@ -22,7 +22,7 @@ int feof( struct _PDCLIB_file_t * stream )
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    /* Testing covered by clearerr(). */
     return TEST_RESULTS;
 }
 
index b6f51ee6092ffe077edb769cca3dc0cb8f418492..6f24080697ba63e435fe41c1b6583857ef64603e 100644 (file)
@@ -22,7 +22,7 @@ int ferror( struct _PDCLIB_file_t * stream )
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    /* Testing covered by clearerr(). */
     return TEST_RESULTS;
 }
 
index 8f9737884b43f972beaa2cc56d7d1fe0d11b979c..e9036112297875ab33a8d4ef6dd26503cea30fc1 100644 (file)
@@ -21,13 +21,17 @@ int fputc( int c, struct _PDCLIB_file_t * stream )
     */
     stream->buffer[stream->bufidx++] = (char)c;
     if ( ( stream->bufidx == stream->bufsize )                   /* _IOFBF */
-           || ( ( stream->status & _IOLBF ) && (char)c == '\n' ) /* _IOLBF */
+           || ( ( stream->status & _IOLBF ) && ( (char)c == '\n' ) ) /* _IOLBF */
            || ( stream->status & _IONBF )                        /* _IONBF */
     )
     {
         /* buffer filled, unbuffered stream, or end-of-line. */
         fflush( stream );
     }
+    else
+    {
+        stream->status |= _PDCLIB_WROTELAST;
+    }
     return c;
 }
 
@@ -38,7 +42,15 @@ int fputc( int c, struct _PDCLIB_file_t * stream )
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    FILE * fh;
+    char buffer[100];
+    TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
+    TESTCASE( fputc( '!', fh ) == '!' );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL );
+    TESTCASE( fread( buffer, 1, 1, fh ) == 1 );
+    TESTCASE( buffer[0] == '!' );
+    TESTCASE( fclose( fh ) == 0 );
     return TEST_RESULTS;
 }
 
index 716fef838b5ebddaba629c760c52e777f268eadb..748fa5aff414ed8436951c48afed57e52f9cb36b 100644 (file)
@@ -39,9 +39,21 @@ int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_rest
 #ifdef TEST
 #include <_PDCLIB_test.h>
 
+#include <string.h>
+
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    FILE * fh;
+    char buffer[100];
+    char text[] = "SUCCESS testing fputs().";
+    TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
+    TESTCASE( fputs( text, fh ) != EOF );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( ( fh = fopen( "testfile", "r" ) ) != NULL );
+    TESTCASE( fread( buffer, 1, strlen( text ), fh ) == strlen( text ) );
+    TESTCASE( memcmp( buffer, text, strlen( text ) ) == 0 );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( "testfile" ) == 0 );
     return TEST_RESULTS;
 }
 
diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c
new file mode 100644 (file)
index 0000000..83bc3b7
--- /dev/null
@@ -0,0 +1,30 @@
+/* $Id$ */
+
+/* fread( void *, size_t, size_t, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <_PDCLIB_glue.h>
+
+#ifndef REGTEST
+
+size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    return _PDCLIB_read( stream->handle, ptr, size * nmemb );
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing handled by fwrite(). */
+    return TEST_RESULTS;
+}
+
+#endif
diff --git a/functions/stdio/fwrite.c b/functions/stdio/fwrite.c
new file mode 100644 (file)
index 0000000..8765d0b
--- /dev/null
@@ -0,0 +1,35 @@
+/* $Id$ */
+
+/* fwrite( const void *, size_t, size_t, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+#include <_PDCLIB_glue.h>
+
+#ifndef REGTEST
+
+size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    return _PDCLIB_write( stream->handle, ptr, size * nmemb );
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    FILE * fh;
+    TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
+    TESTCASE( fwrite( "SUCCESS testing fwrite()\n", 1, 25, fh ) == 25 );
+    TESTCASE( fclose( fh ) == 0 );
+    /* TODO: Add readback test. */
+    TESTCASE( remove( "testfile" ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
index ac3dfc355108de46aca18d44a2b787b9ed1d7cd3..c3c46cf2c344868114c7bc3f624261a9d4c2c19a 100644 (file)
@@ -28,7 +28,7 @@ int printf( const char * _PDCLIB_restrict format, ... )
 
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    TESTCASE( printf( "SUCCESS testing printf().\n" ) == 26 );
     return TEST_RESULTS;
 }
 
index 0288f6d74b90b9c17fa38ee43a6a68acc01e634a..9a9a9503ce099eb5a5ac6f4ef000c4d7effc54b6 100644 (file)
@@ -24,6 +24,7 @@ int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDC
         {
             /* No conversion specifier, print verbatim */
             putc( *(format++), stream );
+            status.i++;
         }
         else
         {
@@ -37,6 +38,7 @@ int vfprintf( struct _PDCLIB_file_t * _PDCLIB_restrict stream, const char * _PDC
 #endif
 
 #ifdef TEST
+#include <stdlib.h>
 #include <_PDCLIB_test.h>
 
 static int testprintf( FILE * stream, const char * format, ... )
@@ -52,13 +54,10 @@ static int testprintf( FILE * stream, const char * format, ... )
 int main( void )
 {
     FILE * fh;
-    TESTCASE( testprintf( stdout, "Hallo\n" ) == 6 );
-#if 0
     TESTCASE( ( fh = fopen( "testfile", "w" ) ) != NULL );
-    TESTCASE( testprintf( fh, "Hallo\n" ) );
+    TESTCASE( testprintf( fh, "Hallo\n" ) == 6 );
     TESTCASE( fclose( fh ) == 0 );
-    TESTCASE( remove( "testfile" ) == 0 );
-#endif
+    /* FIXME: Testfile doesn't exist... */
     return TEST_RESULTS;
 }
 
index 4a24b29a8311b8c18960226176a80f76d20865a2..7871385d8a025775ad995e641b2fc0be882e27fa 100644 (file)
@@ -7,13 +7,16 @@
 */
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <_PDCLIB_glue.h>
 
 #ifndef REGTEST
 
 void _Exit( int status )
 {
-    /* TODO: Flush and close open streams. Remove tmpfile() files. */
+    /* TODO: Flush and close open streams. Remove tmpfile() files. Make this
+       called on process termination automatically.
+    */
     _PDCLIB_Exit( status );
 }
 
index 769c447fbf19c044d3fba2eaeb8cdb3728b7d557..ab0920b8bb7f959336f8f63aa6eb56ec3806d554 100644 (file)
@@ -121,12 +121,12 @@ void * malloc( size_t size )
             if ( _PDCLIB_memlist.last == NULL )
             {
                 _PDCLIB_memlist.first = splitnode;
-                splitnode->next = NULL;
             }
             else
             {
                 _PDCLIB_memlist.last->next = splitnode;
             }
+            splitnode->next = NULL; /* TODO: This is bug #7, uncovered by testdriver yet. */
             _PDCLIB_memlist.last = splitnode;
         }
         return (char *)newnode + sizeof( struct _PDCLIB_memnode_t );
diff --git a/platform/example/functions/_PDCLIB/read.c b/platform/example/functions/_PDCLIB/read.c
new file mode 100644 (file)
index 0000000..db6418d
--- /dev/null
@@ -0,0 +1,31 @@
+/* $Id$ */
+
+/* _PDCLIB_read( _PDCLIB_fd_t, char *, _PDCLIB_size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <_PDCLIB_glue.h>
+
+#ifndef REGTEST
+
+int read(int, void *, unsigned int);
+
+_PDCLIB_size_t _PDCLIB_read( _PDCLIB_fd_t fd, char * buffer, _PDCLIB_size_t n )
+{
+    return read( fd, buffer, n );
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    TESTCASE( NO_TESTDRIVER );
+    return TEST_RESULTS;
+}
+
+#endif
index baf6985f4eaad7ee1568babed56693f8870ffdc9..160dac3440412b9d84a31b1b89c5280ec7ff05c4 100644 (file)
 /* TODO: This is proof-of-concept, requires finetuning. */
 static char _PDCLIB_sin_buffer[BUFSIZ];
 static char _PDCLIB_sout_buffer[BUFSIZ];
+static char _PDCLIB_serr_buffer[BUFSIZ];
 
-static struct _PDCLIB_file_t _PDCLIB_serr = { 2, { 0 }, NULL, 0, 0, 0u, /* mbstate, */ NULL };
-static struct _PDCLIB_file_t _PDCLIB_sout = { 1, { 0 }, _PDCLIB_sout_buffer, BUFSIZ, 0, 0u, /* mbstate, */ &_PDCLIB_serr };
-static struct _PDCLIB_file_t _PDCLIB_sin  = { 0, { 0 }, _PDCLIB_sin_buffer, BUFSIZ, 0, 0u, /* mbstate, */ &_PDCLIB_sout };
+/* FIXME: serr should handle one character. Buffering on out / in? */
+static struct _PDCLIB_file_t _PDCLIB_serr = { 2, { 0 }, _PDCLIB_serr_buffer, BUFSIZ, 0, _IONBF, /* mbstate, */ NULL };
+static struct _PDCLIB_file_t _PDCLIB_sout = { 1, { 0 }, _PDCLIB_sout_buffer, BUFSIZ, 0, _IOLBF, /* mbstate, */ &_PDCLIB_serr };
+static struct _PDCLIB_file_t _PDCLIB_sin  = { 0, { 0 }, _PDCLIB_sin_buffer, BUFSIZ, 0, _IOLBF, /* mbstate, */ &_PDCLIB_sout };
 
 struct _PDCLIB_file_t * stdin  = &_PDCLIB_sin;
 struct _PDCLIB_file_t * stdout = &_PDCLIB_sout;
diff --git a/platform/example/functions/_PDCLIB/write.c b/platform/example/functions/_PDCLIB/write.c
new file mode 100644 (file)
index 0000000..2489234
--- /dev/null
@@ -0,0 +1,31 @@
+/* $Id$ */
+
+/* _PDCLIB_write( _PDCLIB_fd_t, char const *, _PDCLIB_size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <_PDCLIB_glue.h>
+
+#ifndef REGTEST
+
+int write(int, const void *, unsigned int);
+
+_PDCLIB_size_t _PDCLIB_write( _PDCLIB_fd_t fd, char const * buffer, _PDCLIB_size_t n )
+{
+    return write( fd, buffer, n );
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    TESTCASE( NO_TESTDRIVER );
+    return TEST_RESULTS;
+}
+
+#endif