doc: use ASCII in .texi files where UTF-8 isn't needed
[gnulib.git] / tests / test-fflush.c
index 3c36ed9..6af2363 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of POSIX compatible fflush() function.
-   Copyright (C) 2007, 2009-2011 Free Software Foundation, Inc.
+   Copyright (C) 2007, 2009-2014 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "signature.h"
 SIGNATURE_CHECK (fflush, int, (FILE *));
 
+#include <errno.h>
 #include <unistd.h>
 
+#include "macros.h"
+
 int
 main (void)
 {
@@ -46,11 +49,13 @@ main (void)
 
   /* Test fflush.  */
   f = fopen ("test-fflush.txt", "r");
+  ASSERT (f != NULL);
   fd = fileno (f);
   if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
     {
       fputs ("Failed initial read of sample file.\n", stderr);
-      fclose (f);
+      if (f)
+        fclose (f);
       unlink ("test-fflush.txt");
       return 1;
     }
@@ -140,6 +145,47 @@ main (void)
       return 1;
     }
   fclose (f);
+
+  /* Test that fflush() sets errno if someone else closes the stream
+     fd behind the back of stdio.  */
+  {
+    FILE *fp = fopen ("test-fflush.txt", "w");
+    ASSERT (fp != NULL);
+    fputc ('x', fp);
+    ASSERT (close (fileno (fp)) == 0);
+    errno = 0;
+    ASSERT (fflush (fp) == EOF);
+    ASSERT (errno == EBADF);
+    fclose (fp);
+  }
+
+  /* Test that fflush() sets errno if the stream was constructed with
+     an invalid file descriptor.  */
+  {
+    FILE *fp = fdopen (-1, "w");
+    if (fp != NULL)
+      {
+        fputc ('x', fp);
+        errno = 0;
+        ASSERT (fflush (fp) == EOF);
+        ASSERT (errno == EBADF);
+      }
+  }
+  {
+    FILE *fp;
+    close (99);
+    fp = fdopen (99, "w");
+    if (fp != NULL)
+      {
+        fputc ('x', fp);
+        errno = 0;
+        ASSERT (fflush (fp) == EOF);
+        ASSERT (errno == EBADF);
+      }
+  }
+
+  /* Clean up.  */
   unlink ("test-fflush.txt");
+
   return 0;
 }