Mercurial > hg > octave-kai > gnulib-hg
changeset 10771:1ac304db6933
More tests for 'select' module.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 08 Nov 2008 14:32:20 +0100 |
parents | 6f79b0e4356c |
children | 7bba9893a5aa |
files | ChangeLog modules/select-tests tests/test-select-fd.c tests/test-select-in.sh tests/test-select-out.sh tests/test-select-stdin.c |
diffstat | 6 files changed, 227 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-11-07 Bruno Haible <bruno@clisp.org> + + * tests/test-select-fd.c: New file. + * tests/test-select-in.sh: New file. + * tests/test-select-out.sh: New file. + * tests/test-select-stdin.c: New file. + * modules/select-tests (Files): Add the new files. + (Depends-on): Add gettimeofday. + (Makefile.am): Add test-select-in.sh, test-select-out.sh to TESTS. + Set TESTS_ENVIRONMENT. Add test-select-fd, test-select-stdin to + check_PROGRAMS. Define test_select_fd_LDADD, test_select_stdin_LDADD. + 2008-11-06 Alexander V. Lukyanov <lav@netis.ru> Bruno Haible <bruno@clisp.org>
--- a/modules/select-tests +++ b/modules/select-tests @@ -1,5 +1,9 @@ Files: tests/test-select.c +tests/test-select-fd.c +tests/test-select-in.sh +tests/test-select-out.sh +tests/test-select-stdin.c Depends-on: stdbool @@ -19,13 +23,18 @@ accept ioctl close +gettimeofday configure.ac: Makefile.am: -TESTS += test-select -check_PROGRAMS += test-select +TESTS += test-select test-select-in.sh test-select-out.sh +# test-select-stdin has to be run by hand. +TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' +check_PROGRAMS += test-select test-select-fd test-select-stdin test_select_LDADD = $(LDADD) @LIBSOCKET@ +test_select_fd_LDADD = $(LDADD) @LIBSOCKET@ +test_select_stdin_LDADD = $(LDADD) @LIBSOCKET@ License: LGPL
new file mode 100644 --- /dev/null +++ b/tests/test-select-fd.c @@ -0,0 +1,66 @@ +/* Test of select() substitute, reading or writing from a given file descriptor. + Copyright (C) 2008 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 of the License, 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, see <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2008. */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <sys/select.h> + +int +main (int argc, char *argv[]) +{ + if (argc == 3) + { + char mode = argv[1][0]; + + if (mode == 'r' || mode == 'w') + { + int fd = atoi (argv[2]); + + if (fd >= 0) + { + fd_set fds; + struct timeval timeout; + int ret; + + FD_ZERO (&fds); + FD_SET (fd, &fds); + timeout.tv_sec = 0; + timeout.tv_usec = 10000; + ret = (mode == 'r' + ? select (fd + 1, &fds, NULL, NULL, &timeout) + : select (fd + 1, NULL, &fds, NULL, &timeout)); + if (ret < 0) + { + perror ("select failed"); + exit (1); + } + if ((ret == 0) != ! FD_ISSET (fd, &fds)) + { + fprintf (stderr, "incorrect return value\n"); + exit (1); + } + fprintf (stderr, "%d\n", ret); + exit (0); + } + } + } + fprintf (stderr, "Usage: test-select-fd mode fd\n"); + exit (1); +}
new file mode 100755 --- /dev/null +++ b/tests/test-select-in.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Test select() on file descriptors opened for reading. + +tmpfiles="" +trap 'rm -fr $tmpfiles' 1 2 3 15 + +tmpfiles="$tmpfiles t-select-in.tmp" + +# Regular files. + +./test-select-fd${EXEEXT} r 0 < ./test-select-fd${EXEEXT} 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +# Pipes. + +{ sleep 1; echo abc; } | ./test-select-fd${EXEEXT} r 0 2> t-select-in.tmp +test `cat t-select-in.tmp` = "0" || exit 1 + +echo abc | { sleep 1; ./test-select-fd${EXEEXT} r 0; } 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +# Special files. + +./test-select-fd${EXEEXT} r 0 < /dev/null 2> t-select-in.tmp +test `cat t-select-in.tmp` = "1" || exit 1 + +rm -fr $tmpfiles + +exit 0
new file mode 100755 --- /dev/null +++ b/tests/test-select-out.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Test select() on file descriptors opened for writing. + +tmpfiles="" +trap 'rm -fr $tmpfiles' 1 2 3 15 + +tmpfiles="$tmpfiles t-select-out.out t-select-out.tmp" + +# Regular files. + +./test-select-fd${EXEEXT} w 1 > t-select-out.out 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +# Pipes. + +( { echo abc; ./test-select-fd${EXEEXT} w 1; } | { sleep 1; cat; } ) > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "0" || exit 1 + +( { sleep 1; echo abc; ./test-select-fd${EXEEXT} w 1; } | cat) > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +# Special files. + +./test-select-fd${EXEEXT} w 1 > /dev/null 2> t-select-out.tmp +test `cat t-select-out.tmp` = "1" || exit 1 + +rm -fr $tmpfiles + +exit 0
new file mode 100644 --- /dev/null +++ b/tests/test-select-stdin.c @@ -0,0 +1,80 @@ +/* Test of select() substitute, reading from stdin. + Copyright (C) 2008 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 of the License, 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, see <http://www.gnu.org/licenses/>. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2008. */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <sys/select.h> +#include <sys/time.h> +#include <unistd.h> + +int +main () +{ + printf ("Applying select() from standard input. Press Ctrl-C to abort.\n"); + for (;;) + { + struct timeval before; + struct timeval after; + unsigned long spent_usec; + fd_set readfds; + struct timeval timeout; + int ret; + + gettimeofday (&before, NULL); + + FD_ZERO (&readfds); + FD_SET (0, &readfds); + timeout.tv_sec = 0; + timeout.tv_usec = 500000; + ret = select (1, &readfds, NULL, NULL, &timeout); + + gettimeofday (&after, NULL); + spent_usec = (after.tv_sec - before.tv_sec) * 1000000 + + after.tv_usec - before.tv_usec; + + if (ret < 0) + { + perror ("select failed"); + exit (1); + } + if ((ret == 0) != ! FD_ISSET (0, &readfds)) + { + fprintf (stderr, "incorrect return value\n"); + exit (1); + } + if (ret == 0) + { + if (spent_usec < 250000) + { + fprintf (stderr, "returned too early\n"); + exit (1); + } + /* Timeout */ + printf ("."); fflush (stdout); + } + else + { + char c; + + printf ("Input available! Trying to read 1 byte...\n"); + read (0, &c, 1); + } + } +}