tests: add signature checks
[gnulib.git] / tests / test-pread.c
1 /* Test the pread function.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Jim Meyering.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (pread, ssize_t, (int, void *, size_t, off_t));
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 #define N (sizeof buf - 1)
45
46 int
47 main (void)
48 {
49   char const *file = "in";
50   int fd;
51   char buf[] = "0123456789";
52   off_t pos;
53
54   ASSERT (file);
55
56   fd = open (file, O_CREAT | O_WRONLY, 0600);
57   ASSERT (0 <= fd);
58   ASSERT (write (fd, buf, N) == N);
59   ASSERT (close (fd) == 0);
60
61   fd = open (file, O_RDONLY);
62   ASSERT (0 <= fd);
63
64   for (pos = 0; pos < 3; pos++)
65     {
66       size_t i;
67       off_t init_pos = lseek (fd, pos, SEEK_SET);
68       ASSERT (init_pos == pos);
69
70       for (i = 0; i < N; i++)
71         {
72           char byte_buf;
73           ASSERT (pread (fd, &byte_buf, 1, i) == 1);
74           ASSERT (byte_buf == buf[i]);
75           ASSERT (lseek (fd, 0, SEEK_CUR) == init_pos);
76         }
77     }
78
79   {
80     /* Invalid offset must evoke failure with EINVAL.  */
81     char byte;
82     ASSERT (pread (fd, &byte, 1, (off_t) -1) == -1);
83     ASSERT (errno == EINVAL);
84   }
85
86   ASSERT (close (fd) == 0);
87
88   {
89     char byte;
90     /* Trying to operate on a pipe must evoke failure with ESPIPE.
91        This assumes that stdin is a pipe, and hence not seekable.  */
92     ASSERT (pread (STDIN_FILENO, &byte, 1, 1) == -1);
93     ASSERT (errno == ESPIPE);
94   }
95
96   return 0;
97 }