annotate tests/test-read.c @ 40196:e63f5d3edab5

relocatable-prog: Update documentation. * doc/relocatable-maint.texi (Supporting Relocation): Update to match the recent changes.
author Bruno Haible <bruno@clisp.org>
date Sun, 24 Feb 2019 01:49:15 +0100
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15694
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test the read() function.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
15694
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
15694
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <unistd.h>
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "signature.h"
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 SIGNATURE_CHECK (read, ssize_t, (int, void *, size_t));
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <errno.h>
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <fcntl.h>
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include "macros.h"
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 int
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 main (void)
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 {
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 const char *filename = "test-read.tmp";
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 int fd;
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* Create a file with a simple contents. */
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 fd = open (filename, O_CREAT | O_WRONLY, 0600);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 ASSERT (fd >= 0);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 ASSERT (write (fd, "Hello World", 11) == 11);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 ASSERT (close (fd) == 0);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* Read from the middle of the file. */
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 fd = open (filename, O_RDONLY);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 ASSERT (fd >= 0);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 ASSERT (lseek (fd, 6, SEEK_SET) == 6);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 {
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 char buf[10];
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 ssize_t ret = read (fd, buf, 10);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 ASSERT (ret == 5);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 ASSERT (memcmp (buf, "World", 5) == 0);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 }
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 ASSERT (close (fd) == 0);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 /* Test behaviour for invalid file descriptors. */
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 {
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 char byte;
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 errno = 0;
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 ASSERT (read (-1, &byte, 1) == -1);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 ASSERT (errno == EBADF);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 char byte;
17292
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
63 close (99);
15694
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 errno = 0;
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 ASSERT (read (99, &byte, 1) == -1);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 ASSERT (errno == EBADF);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 }
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 /* Clean up. */
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 unlink (filename);
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 return 0;
264f4282e7c1 Tests for module 'read'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 }