Mercurial > hg > octave-lojdl > gnulib-hg
changeset 12356:26486a11640c
test pread
* tests/test-pread.c: New file.
* tests/test-pread.sh: Likewise.
* modules/pread-tests: Likewise.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Wed, 25 Nov 2009 15:50:56 +0100 |
parents | 9961377da68e |
children | 8224b244d6c2 |
files | ChangeLog modules/pread-tests tests/test-pread.c tests/test-pread.sh |
diffstat | 4 files changed, 104 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2009-11-25 Jim Meyering <meyering@redhat.com> + 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.
new file mode 100644 --- /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)'
new file mode 100644 --- /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 <http://www.gnu.org/licenses/>. */ + +/* Written by Jim Meyering. */ + +#include <config.h> + +#include <unistd.h> + +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <fcntl.h> + +#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; +}