summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-09-20 23:02:57 +0200
committerBruno Haible <bruno@clisp.org>2011-09-20 23:28:03 +0200
commitb7a31293817c8d30e63dea34ee33365637cd7a7a (patch)
tree566770a1fbb32abdfec6b7198038edb46088e8dc
parentca39b1aeb0317680a7344fc9b9a1951323c90fab (diff)
downloadgnulib-b7a31293817c8d30e63dea34ee33365637cd7a7a.tar.gz
Tests for function fgetc().
* modules/fgetc-tests: New file. * tests/test-fgetc.c: New file. * modules/stdio-tests (Depends-on): Add fgetc-tests.
-rw-r--r--ChangeLog5
-rw-r--r--modules/fgetc-tests12
-rw-r--r--modules/stdio-tests1
-rw-r--r--tests/test-fgetc.c84
4 files changed, 102 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 117c162a04..9e73081544 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2011-09-20 Bruno Haible <bruno@clisp.org>
+ Tests for function fgetc().
+ * modules/fgetc-tests: New file.
+ * tests/test-fgetc.c: New file.
+ * modules/stdio-tests (Depends-on): Add fgetc-tests.
+
Tests for function fdopen().
* modules/fdopen-tests: New file.
* tests/test-fdopen.c: New file.
diff --git a/modules/fgetc-tests b/modules/fgetc-tests
new file mode 100644
index 0000000000..9bbb166e50
--- /dev/null
+++ b/modules/fgetc-tests
@@ -0,0 +1,12 @@
+Files:
+tests/test-fgetc.c
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-fgetc
+check_PROGRAMS += test-fgetc
diff --git a/modules/stdio-tests b/modules/stdio-tests
index 76f8dc1ab4..fff23030a4 100644
--- a/modules/stdio-tests
+++ b/modules/stdio-tests
@@ -5,6 +5,7 @@ Depends-on:
verify
stdio-c++-tests
fdopen-tests
+fgetc-tests
configure.ac:
diff --git a/tests/test-fgetc.c b/tests/test-fgetc.c
new file mode 100644
index 0000000000..a4e1953eec
--- /dev/null
+++ b/tests/test-fgetc.c
@@ -0,0 +1,84 @@
+/* Test of fgetc() function.
+ Copyright (C) 2011 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
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (fgetc, int, (FILE *));
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "macros.h"
+
+int
+main (int argc, char **argv)
+{
+ const char *filename = "test-fgetc.txt";
+
+ /* Prepare a file. */
+ {
+ const char text[] = "hello world";
+ int fd = open (filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
+ ASSERT (fd >= 0);
+ ASSERT (write (fd, text, sizeof (text)) == sizeof (text));
+ ASSERT (close (fd) == 0);
+ }
+
+ /* Test that fgetc() sets errno if someone else closes the stream
+ fd behind the back of stdio. */
+ {
+ FILE *fp = fopen (filename, "r");
+ ASSERT (fp != NULL);
+ ASSERT (close (fileno (fp)) == 0);
+ errno = 0;
+ ASSERT (fgetc (fp) == EOF);
+ ASSERT (errno == EBADF);
+ fclose (fp);
+ }
+
+ /* Test that fgetc() sets errno if the stream was constructed with
+ an invalid file descriptor. */
+ {
+ FILE *fp = fdopen (-1, "r");
+ if (fp != NULL)
+ {
+ errno = 0;
+ ASSERT (fgetc (fp) == EOF);
+ ASSERT (errno == EBADF);
+ fclose (fp);
+ }
+ }
+ {
+ FILE *fp = fdopen (99, "r");
+ if (fp != NULL)
+ {
+ errno = 0;
+ ASSERT (fgetc (fp) == EOF);
+ ASSERT (errno == EBADF);
+ fclose (fp);
+ }
+ }
+
+ /* Clean up. */
+ unlink (filename);
+
+ return 0;
+}