5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
15 #include <_PDCLIB_io.h>
17 long int _PDCLIB_ftell_unlocked( FILE * stream )
19 uint_fast64_t off64 = _PDCLIB_ftell64_unlocked( stream );
21 if ( off64 > LONG_MAX )
23 /* integer overflow */
30 long int ftell( FILE * stream )
32 _PDCLIB_flockfile( stream );
33 long int off = _PDCLIB_ftell_unlocked( stream );
34 _PDCLIB_funlockfile( stream );
41 #include <_PDCLIB_test.h>
44 #include <_PDCLIB_io.h>
49 /* Testing all the basic I/O functions individually would result in lots
50 of duplicated code, so I took the liberty of lumping it all together
53 /* The following functions delegate their tests to here:
54 fgetc fflush rewind fputc ungetc fseek
55 flushbuffer seek fillbuffer prepread prepwrite
57 char * buffer = (char*)malloc( 4 );
59 TESTCASE( ( fh = tmpfile() ) != NULL );
60 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
61 /* Testing ungetc() at offset 0 */
63 TESTCASE( ungetc( 'x', fh ) == 'x' );
64 TESTCASE( ftell( fh ) == -1l );
66 TESTCASE( ftell( fh ) == 0l );
67 /* Commence "normal" tests */
68 TESTCASE( fputc( '1', fh ) == '1' );
69 TESTCASE( fputc( '2', fh ) == '2' );
70 TESTCASE( fputc( '3', fh ) == '3' );
71 /* Positions incrementing as expected? */
72 TESTCASE( ftell( fh ) == 3l );
73 TESTCASE_NOREG( fh->pos.offset == 0l );
74 TESTCASE_NOREG( fh->bufidx == 3l );
75 /* Buffer properly flushed when full? */
76 TESTCASE( fputc( '4', fh ) == '4' );
77 TESTCASE_NOREG( fh->pos.offset == 4l );
78 TESTCASE_NOREG( fh->bufidx == 0 );
79 /* fflush() resetting positions as expected? */
80 TESTCASE( fputc( '5', fh ) == '5' );
81 TESTCASE( fflush( fh ) == 0 );
82 TESTCASE( ftell( fh ) == 5l );
83 TESTCASE_NOREG( fh->pos.offset == 5l );
84 TESTCASE_NOREG( fh->bufidx == 0l );
85 /* rewind() resetting positions as expected? */
87 TESTCASE( ftell( fh ) == 0l );
88 TESTCASE_NOREG( fh->pos.offset == 0 );
89 TESTCASE_NOREG( fh->bufidx == 0 );
90 /* Reading back first character after rewind for basic read check */
91 TESTCASE( fgetc( fh ) == '1' );
93 TESTCASE( fclose( fh ) == 0 );