Reworked scanf() testing. General cleanups.
[fw/pdclib] / functions / stdio / fseek.c
1 /* $Id$ */
2
3 /* fseek( FILE *, long offset, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12
13 #include <_PDCLIB_glue.h>
14
15 int fseek( struct _PDCLIB_file_t * stream, long offset, int whence )
16 {
17     if ( stream->status & _PDCLIB_FWRITE )
18     {
19         if ( _PDCLIB_flushbuffer( stream ) == EOF )
20         {
21             return EOF;
22         }
23     }
24     stream->status &= ~ _PDCLIB_EOFFLAG;
25     if ( stream->status & _PDCLIB_FRW )
26     {
27         stream->status &= ~ ( _PDCLIB_FREAD | _PDCLIB_FWRITE );
28     }
29     return ( _PDCLIB_seek( stream, offset, whence ) != EOF ) ? 0 : EOF;
30 }
31
32 #endif
33
34 #ifdef TEST
35 #include <_PDCLIB_test.h>
36 #include <string.h>
37
38 int main( void )
39 {
40     FILE * fh;
41     TESTCASE( ( fh = tmpfile() ) != NULL );
42     TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
43     /* General functionality */
44     TESTCASE( fseek( fh, -1, SEEK_END ) == 0  );
45     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) - 1 );
46     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
47     TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
48     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
49     TESTCASE( ftell( fh ) == 0 );
50     TESTCASE( fseek( fh, 5, SEEK_CUR ) == 0 );
51     TESTCASE( ftell( fh ) == 5 );
52     TESTCASE( fseek( fh, -3, SEEK_CUR ) == 0 );
53     TESTCASE( ftell( fh ) == 2 );
54     /* Checking behaviour around EOF */
55     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
56     TESTCASE( ! feof( fh ) );
57     TESTCASE( fgetc( fh ) == EOF );
58     TESTCASE( feof( fh ) );
59     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
60     TESTCASE( ! feof( fh ) );
61     /* Checking undo of ungetc() */
62     TESTCASE( fseek( fh, 0, SEEK_SET ) == 0 );
63     TESTCASE( fgetc( fh ) == teststring[0] );
64     TESTCASE( fgetc( fh ) == teststring[1] );
65     TESTCASE( fgetc( fh ) == teststring[2] );
66     TESTCASE( ftell( fh ) == 3 );
67     TESTCASE( ungetc( teststring[2], fh ) == teststring[2] );
68     TESTCASE( ftell( fh ) == 2 );
69     TESTCASE( fgetc( fh ) == teststring[2] );
70     TESTCASE( ftell( fh ) == 3 );
71     TESTCASE( ungetc( 'x', fh ) == 'x' );
72     TESTCASE( ftell( fh ) == 2 );
73     TESTCASE( fgetc( fh ) == 'x' );
74     TESTCASE( ungetc( 'x', fh ) == 'x' );
75     TESTCASE( ftell( fh ) == 2 );
76     TESTCASE( fseek( fh, 2, SEEK_SET ) == 0 );
77     TESTCASE( fgetc( fh ) == teststring[2] );
78     /* Checking error handling */
79     TESTCASE( fseek( fh, -5, SEEK_SET ) == -1 );
80     TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
81     TESTCASE( fclose( fh ) == 0 );
82     return TEST_RESULTS;
83 }
84
85 #endif
86