]> pd.if.org Git - pdclib/blob - functions/stdio/ftell.c
Cleaned up the testing a bit.
[pdclib] / functions / stdio / ftell.c
1 /* $Id$ */
2
3 /* ftell( FILE * )
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 #include <limits.h>
11
12 #ifndef REGTEST
13
14 long int ftell( struct _PDCLIB_file_t * stream )
15 {
16     /* TODO: A bit too fuzzy in the head now. stream->ungetidx should be in here
17              somewhere.
18     */
19     if ( stream->pos.offset > ( LONG_MAX - stream->bufidx ) )
20     {
21         /* integer overflow */
22         _PDCLIB_errno = _PDCLIB_EINVAL;
23         return -1;
24     }
25     /* Position of start-of-buffer, plus:
26        - buffered, unwritten content (for output streams), or
27        - already-parsed content from buffer (for input streams)
28     */
29     return (long int)( stream->pos.offset + stream->bufidx - stream->ungetidx );
30 }
31
32 #endif
33
34 #ifdef TEST
35 #include <_PDCLIB_test.h>
36
37 #include <stdlib.h>
38
39 int main( void )
40 {
41     /* Testing all the basic I/O functions individually would result in lots
42        of duplicated code, so I took the liberty of lumping it all together
43        here.
44     */
45     /* The following functions delegate their tests to here:
46        fgetc fflush rewind fputc ungetc fseek
47        flushbuffer seek fillbuffer prepread prepwrite
48     */
49     char * buffer = (char*)malloc( 4 );
50     FILE * fh;
51     remove( testfile );
52     TESTCASE( ( fh = fopen( testfile, "w+" ) ) != NULL );
53     TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
54     TESTCASE( fputc( '1', fh ) == '1' );
55     TESTCASE( fputc( '2', fh ) == '2' );
56     TESTCASE( fputc( '3', fh ) == '3' );
57     /* Positions incrementing as expected? */
58     TESTCASE( ftell( fh ) == 3l );
59     TESTCASE_NOREG( fh->pos.offset == 0l );
60     TESTCASE_NOREG( fh->bufidx == 3l );
61     /* Buffer properly flushed when full? */
62     TESTCASE( fputc( '4', fh ) == '4' );
63     TESTCASE_NOREG( fh->pos.offset == 4l );
64     TESTCASE_NOREG( fh->bufidx == 0 );
65     /* fflush() resetting positions as expected? */
66     TESTCASE( fputc( '5', fh ) == '5' );
67     TESTCASE( fflush( fh ) == 0 );
68     TESTCASE( ftell( fh ) == 5l );
69     TESTCASE_NOREG( fh->pos.offset == 5l );
70     TESTCASE_NOREG( fh->bufidx == 0l );
71     /* rewind() resetting positions as expected? */
72     rewind( fh );
73     TESTCASE( ftell( fh ) == 0l );
74     TESTCASE_NOREG( fh->pos.offset == 0 );
75     TESTCASE_NOREG( fh->bufidx == 0 );
76     /* Reading back first character after rewind for basic read check */
77     TESTCASE( fgetc( fh ) == '1' );
78     TESTCASE( fclose( fh ) == 0 );
79     /* TODO: t.b.c. */
80     remove( testfile );
81     return TEST_RESULTS;
82 }
83
84 #endif
85