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_unlocked( struct _PDCLIB_file_t * stream )
18 uint_fast64_t off64 = _PDCLIB_ftell64_unlocked( stream );
20 if ( off64 > LONG_MAX )
22 /* integer overflow */
29 long int ftell( struct _PDCLIB_file_t * stream )
32 long int off = ftell_unlocked( stream );
33 funlockfile( stream );
40 #include <_PDCLIB_test.h>
46 /* Testing all the basic I/O functions individually would result in lots
47 of duplicated code, so I took the liberty of lumping it all together
50 /* The following functions delegate their tests to here:
51 fgetc fflush rewind fputc ungetc fseek
52 flushbuffer seek fillbuffer prepread prepwrite
54 char * buffer = (char*)malloc( 4 );
56 TESTCASE( ( fh = tmpfile() ) != NULL );
57 TESTCASE( setvbuf( fh, buffer, _IOLBF, 4 ) == 0 );
58 /* Testing ungetc() at offset 0 */
60 TESTCASE( ungetc( 'x', fh ) == 'x' );
61 TESTCASE( ftell( fh ) == -1l );
63 TESTCASE( ftell( fh ) == 0l );
64 /* Commence "normal" tests */
65 TESTCASE( fputc( '1', fh ) == '1' );
66 TESTCASE( fputc( '2', fh ) == '2' );
67 TESTCASE( fputc( '3', fh ) == '3' );
68 /* Positions incrementing as expected? */
69 TESTCASE( ftell( fh ) == 3l );
70 TESTCASE_NOREG( fh->pos.offset == 0l );
71 TESTCASE_NOREG( fh->bufidx == 3l );
72 /* Buffer properly flushed when full? */
73 TESTCASE( fputc( '4', fh ) == '4' );
74 TESTCASE_NOREG( fh->pos.offset == 4l );
75 TESTCASE_NOREG( fh->bufidx == 0 );
76 /* fflush() resetting positions as expected? */
77 TESTCASE( fputc( '5', fh ) == '5' );
78 TESTCASE( fflush( fh ) == 0 );
79 TESTCASE( ftell( fh ) == 5l );
80 TESTCASE_NOREG( fh->pos.offset == 5l );
81 TESTCASE_NOREG( fh->bufidx == 0l );
82 /* rewind() resetting positions as expected? */
84 TESTCASE( ftell( fh ) == 0l );
85 TESTCASE_NOREG( fh->pos.offset == 0 );
86 TESTCASE_NOREG( fh->bufidx == 0 );
87 /* Reading back first character after rewind for basic read check */
88 TESTCASE( fgetc( fh ) == '1' );
90 TESTCASE( fclose( fh ) == 0 );