From 806fe1c26c7b11439bd7645e4e014c72c33c5b10 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 20 Sep 2011 23:04:11 +0200 Subject: [PATCH] Tests for function fputc(). * modules/fputc-tests: New file. * tests/test-fputc.c: New file. * modules/stdio-tests (Depends-on): Add fputc-tests. --- ChangeLog | 5 ++++ modules/fputc-tests | 12 +++++++++ tests/test-fputc.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 modules/fputc-tests create mode 100644 tests/test-fputc.c diff --git a/ChangeLog b/ChangeLog index 9e7308154..2af3b8ebf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2011-09-20 Bruno Haible + Tests for function fputc(). + * modules/fputc-tests: New file. + * tests/test-fputc.c: New file. + * modules/stdio-tests (Depends-on): Add fputc-tests. + Tests for function fgetc(). * modules/fgetc-tests: New file. * tests/test-fgetc.c: New file. diff --git a/modules/fputc-tests b/modules/fputc-tests new file mode 100644 index 000000000..6aa348eca --- /dev/null +++ b/modules/fputc-tests @@ -0,0 +1,12 @@ +Files: +tests/test-fputc.c +tests/signature.h +tests/macros.h + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-fputc +check_PROGRAMS += test-fputc diff --git a/tests/test-fputc.c b/tests/test-fputc.c new file mode 100644 index 000000000..a8b097090 --- /dev/null +++ b/tests/test-fputc.c @@ -0,0 +1,78 @@ +/* Test of fputc() 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 + +#include + +#include "signature.h" +SIGNATURE_CHECK (fputc, int, (int, FILE *)); + +#include +#include +#include + +#include "macros.h" + +int +main (int argc, char **argv) +{ + const char *filename = "test-fputc.txt"; + + /* Test that fputc() on an unbuffered stream sets errno if someone else + closes the stream fd behind the back of stdio. */ + { + FILE *fp = fopen (filename, "w"); + ASSERT (fp != NULL); + setvbuf (fp, NULL, _IONBF, 0); + ASSERT (close (fileno (fp)) == 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + fclose (fp); + } + + /* Test that fputc() on an unbuffered stream sets errno if the stream + was constructed with an invalid file descriptor. */ + { + FILE *fp = fdopen (-1, "w"); + if (fp != NULL) + { + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + fclose (fp); + } + } + { + FILE *fp = fdopen (99, "w"); + if (fp != NULL) + { + setvbuf (fp, NULL, _IONBF, 0); + errno = 0; + ASSERT (fputc ('x', fp) == EOF); + ASSERT (errno == EBADF); + fclose (fp); + } + } + + /* Clean up. */ + unlink (filename); + + return 0; +} -- 2.11.0