bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH 2/2] test pread (using the new init.sh framework)


From: Jim Meyering
Subject: [PATCH 2/2] test pread (using the new init.sh framework)
Date: Wed, 25 Nov 2009 16:15:28 +0100

Notice how this tiny shell script drives
the compiled program.  There is no need to
choose unique names from within the C code, nor
even to clean up when done, since it's run from
within a just-created directory that's removed upon completion
of this single test.  Also, there's no more need for the $EXE suffix,
since we're using PATH.

    #!/bin/sh
    : ${srcdir=.} ${builddir=.}
    . $srcdir/init.sh --set-path=$builddir

    fail=0;
    test-pread || fail=1

    Exit $fail


>From ebbfe98516776233fa7f40f2e44c8f23b54969de Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Wed, 25 Nov 2009 15:50:56 +0100
Subject: [PATCH 2/2] 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(+), 0 deletions(-)
 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 c255af0..cbfafb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2009-11-25  Jim Meyering  <address@hidden>

+       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 0000000..7a4e7a7
--- /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 0000000..cf4179f
--- /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;
+}
diff --git a/tests/test-pread.sh b/tests/test-pread.sh
new file mode 100644
index 0000000..0e0754d
--- /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
--
1.6.6.rc0.236.ge0b94




reply via email to

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