From ebbfe98516776233fa7f40f2e44c8f23b54969de Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 25 Nov 2009 15:50:56 +0100 Subject: [PATCH] test pread * tests/test-pread.c: New file. * tests/test-pread.sh: Likewise. * modules/pread-tests: Likewise. --- ChangeLog | 5 ++++ modules/pread-tests | 14 ++++++++++ tests/test-pread.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-pread.sh | 7 +++++ 4 files changed, 104 insertions(+) create mode 100644 modules/pread-tests create mode 100644 tests/test-pread.c create mode 100644 tests/test-pread.sh diff --git a/ChangeLog b/ChangeLog index c255af034..cbfafb26a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2009-11-25 Jim Meyering + test pread + * tests/test-pread.c: New file. + * tests/test-pread.sh: Likewise. + * modules/pread-tests: Likewise. + pread: new module * modules/pread: New file. * lib/unistd.in.h (pread): Define/declare. diff --git a/modules/pread-tests b/modules/pread-tests new file mode 100644 index 000000000..7a4e7a70c --- /dev/null +++ b/modules/pread-tests @@ -0,0 +1,14 @@ +Files: +tests/test-pread.c +tests/init.sh + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-pread.sh +check_PROGRAMS += test-pread +TESTS_ENVIRONMENT += \ + srcdir='$(srcdir)' \ + builddir='$(builddir)' diff --git a/tests/test-pread.c b/tests/test-pread.c new file mode 100644 index 000000000..cf4179f32 --- /dev/null +++ b/tests/test-pread.c @@ -0,0 +1,78 @@ +/* Test the pread function. + Copyright (C) 2009 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 . */ + +/* Written by Jim Meyering. */ + +#include + +#include + +#include +#include +#include +#include + +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +#define N (sizeof buf - 1) + +int +main (void) +{ + char const *file = "in"; + int fd; + char buf[] = "0123456789"; + off_t pos; + + ASSERT (file); + + fd = open (file, O_CREAT | O_WRONLY, 0600); + ASSERT (0 <= fd); + ASSERT (write (fd, buf, N) == N); + ASSERT (close (fd) == 0); + + fd = open (file, O_RDONLY); + ASSERT (0 <= fd); + + for (pos = 0; pos < 3; pos++) + { + size_t i; + off_t init_pos = lseek (fd, pos, SEEK_SET); + ASSERT (init_pos == pos); + + for (i = 0; i < N; i++) + { + char byte_buf; + ASSERT (pread (fd, &byte_buf, 1, i) == 1); + ASSERT (byte_buf == buf[i]); + ASSERT (lseek (fd, 0, SEEK_CUR) == init_pos); + } + } + + ASSERT (close (fd) == 0); + + return 0; +} diff --git a/tests/test-pread.sh b/tests/test-pread.sh new file mode 100644 index 000000000..0e0754d84 --- /dev/null +++ b/tests/test-pread.sh @@ -0,0 +1,7 @@ +: ${srcdir=.} ${builddir=.} +. $srcdir/init.sh --set-path=$builddir + +fail=0; +test-pread || fail=1 + +Exit $fail -- 2.11.0