X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-freading.c;h=1a38b9e44e7b48e1b68bceaefdb7a3302c6ba951;hb=9faed5de047d34fddbd4cddbfee73d1fbcea5e32;hp=02414e5d03591c0fadc0a1a28cecd3b890914b3d;hpb=a8f956440539cf954fb92e2396cc58e2a111455f;p=gnulib.git diff --git a/tests/test-freading.c b/tests/test-freading.c index 02414e5d0..1a38b9e44 100644 --- a/tests/test-freading.c +++ b/tests/test-freading.c @@ -21,9 +21,19 @@ #include "freading.h" +#include #include -#define ASSERT(expr) if (!(expr)) abort (); +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + abort (); \ + } \ + } \ + while (0) #define TESTFILE "t-freading.tmp" @@ -57,6 +67,11 @@ main () if (fgetc (fp) != 'b') goto skip; ASSERT (freading (fp)); + fflush (fp); + ASSERT (freading (fp)); + if (fgetc (fp) != 'a') + goto skip; + ASSERT (freading (fp)); if (fseek (fp, 0, SEEK_END)) goto skip; ASSERT (freading (fp)); @@ -68,6 +83,7 @@ main () freading is only deterministic after input or output, but this test case should be portable even on open, after reposition, and at EOF. */ + /* First a scenario with only fgetc, fseek, fputc. */ fp = fopen (TESTFILE, "r+"); if (fp == NULL) goto skip; @@ -77,18 +93,63 @@ main () ASSERT (freading (fp)); if (fseek (fp, 2, SEEK_CUR)) goto skip; - /* freading (fp)) is undefined here, but fwriting is false. */ + /* freading (fp) is undefined here, but fwriting (fp) is false. */ if (fgetc (fp) != 'b') goto skip; ASSERT (freading (fp)); + /* This fseek call is necessary when switching from reading to writing. + See the description of fopen(), ISO C 99 7.19.5.3.(6). */ if (fseek (fp, 0, SEEK_CUR) != 0) goto skip; - if (fputc ('z', fp) != 'z') + /* freading (fp) is undefined here, but fwriting (fp) is false. */ + if (fputc ('x', fp) != 'x') goto skip; ASSERT (!freading (fp)); if (fseek (fp, 0, SEEK_END)) goto skip; + /* freading (fp) is undefined here, because on some implementations (e.g. + glibc) fseek causes a buffer to be read. + fwriting (fp) is undefined as well. */ + if (fclose (fp)) + goto skip; + + /* Open it in read-write mode. POSIX requires a reposition (fseek, + fsetpos, rewind) or EOF when transitioning from read to write; + freading is only deterministic after input or output, but this + test case should be portable even on open, after reposition, and + at EOF. */ + /* Here a scenario that includes fflush. */ + fp = fopen (TESTFILE, "r+"); + if (fp == NULL) + goto skip; + ASSERT (!freading (fp)); + if (fgetc (fp) != 'f') + goto skip; + ASSERT (freading (fp)); + if (fseek (fp, 2, SEEK_CUR)) + goto skip; + /* freading (fp) is undefined here, but fwriting (fp) is false. */ + if (fgetc (fp) != 'b') + goto skip; + ASSERT (freading (fp)); + fflush (fp); + /* freading (fp) is undefined here, but fwriting (fp) is false. */ + if (fgetc (fp) != 'x') + goto skip; + ASSERT (freading (fp)); + /* This fseek call is necessary when switching from reading to writing. + See the description of fopen(), ISO C 99 7.19.5.3.(6). */ + if (fseek (fp, 0, SEEK_CUR) != 0) + goto skip; + /* freading (fp) is undefined here, but fwriting (fp) is false. */ + if (fputc ('z', fp) != 'z') + goto skip; ASSERT (!freading (fp)); + if (fseek (fp, 0, SEEK_END)) + goto skip; + /* freading (fp) is undefined here, because on some implementations (e.g. + glibc) fseek causes a buffer to be read. + fwriting (fp) is undefined as well. */ if (fclose (fp)) goto skip;