Make the tests sharper: also test the interaction with fflush().
authorBruno Haible <bruno@clisp.org>
Sun, 29 Apr 2007 02:07:56 +0000 (02:07 +0000)
committerBruno Haible <bruno@clisp.org>
Sun, 29 Apr 2007 02:07:56 +0000 (02:07 +0000)
ChangeLog
modules/freading-tests
modules/fwriting-tests
tests/test-freading.c
tests/test-fwriting.c

index 3987866..3c75b00 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2007-04-28  Bruno Haible  <bruno@clisp.org>
 
+       * tests/test-fwriting.c (main): Also test the interaction between
+       fflush and fwriting.
+       * modules/fwriting-tests (Depends-on): Add fflush.
+
+       * tests/test-freading.c (main): Also test the interaction between
+       fflush and freading.
+       * modules/freading-tests (Depends-on): Add fflush.
+
+2007-04-28  Bruno Haible  <bruno@clisp.org>
+
        * lib/stdio_.h (fseek, ftell): Provide link warnings suggesting to use
        fseeko and ftello.
        Suggested by Eric Blake.
index fea9a69..78cb96a 100644 (file)
@@ -2,6 +2,7 @@ Files:
 tests/test-freading.c
 
 Depends-on:
+fflush
 
 configure.ac:
 
index f4258d4..5594223 100644 (file)
@@ -2,6 +2,7 @@ Files:
 tests/test-fwriting.c
 
 Depends-on:
+fflush
 
 configure.ac:
 
index 02414e5..e51d7ab 100644 (file)
@@ -57,6 +57,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));
@@ -77,12 +82,52 @@ 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;
+  /* 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;
+  ASSERT (!freading (fp));
+  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.  */
+  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) != 'a')
+    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));
index 24279f3..00b6870 100644 (file)
@@ -57,6 +57,11 @@ main ()
   if (fgetc (fp) != 'b')
     goto skip;
   ASSERT (!fwriting (fp));
+  fflush (fp);
+  ASSERT (!fwriting (fp));
+  if (fgetc (fp) != 'a')
+    goto skip;
+  ASSERT (!fwriting (fp));
   if (fseek (fp, 0, SEEK_END))
     goto skip;
   ASSERT (!fwriting (fp));
@@ -81,14 +86,54 @@ main ()
   if (fgetc (fp) != 'b')
     goto skip;
   ASSERT (!fwriting (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;
+  ASSERT (!fwriting (fp));
+  if (fputc ('z', fp) != 'z')
+    goto skip;
+  ASSERT (fwriting (fp));
+  if (fseek (fp, 0, SEEK_END))
+    goto skip;
+  /* fwriting (fp) is undefined here, but freading (fp) is false.  */
+  if (fclose (fp))
+    goto skip;
+
+  /* Open it in read-write mode.  POSIX requires a reposition (fseek,
+     fsetpos, rewind) or fflush when transitioning from write to read,
+     fwriting is only deterministic after input or output, but this
+     test case should be portable even on open, after reposition, and
+     after fflush.  */
+  fp = fopen (TESTFILE, "r+");
+  if (fp == NULL)
+    goto skip;
+  ASSERT (!fwriting (fp));
+  if (fgetc (fp) != 'f')
+    goto skip;
+  ASSERT (!fwriting (fp));
+  if (fseek (fp, 2, SEEK_CUR))
+    goto skip;
+  ASSERT (!fwriting (fp));
+  if (fgetc (fp) != 'b')
+    goto skip;
+  ASSERT (!fwriting (fp));
+  fflush (fp);
+  ASSERT (!fwriting (fp));
+  if (fgetc (fp) != 'a')
+    goto skip;
+  ASSERT (!fwriting (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;
+  ASSERT (!fwriting (fp));
   if (fputc ('z', fp) != 'z')
     goto skip;
   ASSERT (fwriting (fp));
   if (fseek (fp, 0, SEEK_END))
     goto skip;
-  /* fwriting (fp) is undefined here, but freading is false.  */
+  /* fwriting (fp) is undefined here, but freading (fp) is false.  */
   if (fclose (fp))
     goto skip;