bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new tests for 'select'


From: Bruno Haible
Subject: new tests for 'select'
Date: Sat, 8 Nov 2008 14:33:38 +0100
User-agent: KMail/1.5.4

Hi,

For improving select(), I'm adding a couple of new tests for select().

Status on mingw:
  - test-select-in.sh fails.
  - test-select-out.sh fails.
  - test-select-stdin, when run in CMD.EXE window, hangs after reading the
    first byte of a line, until Return gets pressed.
  - test-select-stdin, when run in a Cygwin window, from bash, notices the
    input of a line only after Return has been pressed a second time.

Feel free to port the tests to poll() instead of select().

Bruno


2008-11-07  Bruno Haible  <address@hidden>

        * 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.

======================= tests/test-select-fd.c ============================
/* 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 <address@hidden>, 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);
}
======================= tests/test-select-in.sh ===========================
#!/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
======================= tests/test-select-out.sh ==========================
#!/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
======================= tests/test-select-stdin.c =========================
/* 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 <address@hidden>, 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);
        }
    }
}
===========================================================================
*** modules/select-tests.orig   2008-11-08 14:25:38.000000000 +0100
--- modules/select-tests        2008-11-08 14:25:30.000000000 +0100
***************
*** 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,31 ****
  accept
  ioctl
  close
  
  configure.ac:
  
  Makefile.am:
! TESTS += test-select
! check_PROGRAMS += test-select
  test_select_LDADD = $(LDADD) @LIBSOCKET@
  
  License:
  LGPL
--- 23,40 ----
  accept
  ioctl
  close
+ gettimeofday
  
  configure.ac:
  
  Makefile.am:
! 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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]