5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
16 long int ftell( struct _PDCLIB_file_t * stream )
18 uint_fast64_t off64 = _PDCLIB_ftell64( stream );
20 if ( off64 > LONG_MAX )
22 /* integer overflow */
32 #include <_PDCLIB_test.h>
38 /* Testing all the basic I/O functions individually would result in lots
39 of duplicated code, so I took the liberty of lumping it all together
42 /* The following functions delegate their tests to here:
43 fgetc fflush rewind fputc ungetc fseek
44 flushbuffer seek fillbuffer prepread prepwrite
46 char * buffer = (char*)malloc( 4 );
48 TESTCASE( ( fh = tmpfile() ) != NULL );
49 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
50 /* Testing ungetc() at offset 0 */
52 TESTCASE( ungetc( 'x', fh ) == 'x' );
53 TESTCASE( ftell( fh ) == -1l );
55 TESTCASE( ftell( fh ) == 0l );
56 /* Commence "normal" tests */
57 TESTCASE( fputc( '1', fh ) == '1' );
58 TESTCASE( fputc( '2', fh ) == '2' );
59 TESTCASE( fputc( '3', fh ) == '3' );
60 /* Positions incrementing as expected? */
61 TESTCASE( ftell( fh ) == 3l );
62 TESTCASE_NOREG( fh->pos.offset == 0l );
63 TESTCASE_NOREG( fh->bufidx == 3l );
64 /* Buffer properly flushed when full? */
65 TESTCASE( fputc( '4', fh ) == '4' );
66 TESTCASE_NOREG( fh->pos.offset == 4l );
67 TESTCASE_NOREG( fh->bufidx == 0 );
68 /* fflush() resetting positions as expected? */
69 TESTCASE( fputc( '5', fh ) == '5' );
70 TESTCASE( fflush( fh ) == 0 );
71 TESTCASE( ftell( fh ) == 5l );
72 TESTCASE_NOREG( fh->pos.offset == 5l );
73 TESTCASE_NOREG( fh->bufidx == 0l );
74 /* rewind() resetting positions as expected? */
76 TESTCASE( ftell( fh ) == 0l );
77 TESTCASE_NOREG( fh->pos.offset == 0 );
78 TESTCASE_NOREG( fh->bufidx == 0 );
79 /* Reading back first character after rewind for basic read check */
80 TESTCASE( fgetc( fh ) == '1' );
82 TESTCASE( fclose( fh ) == 0 );