annotate tests/test-pwrite.c @ 40231:9b3c79fdfe0b

strtod: fix clash with strtold Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817). * lib/strtod.c (compute_minus_zero, minus_zero): Simplify by remving the macro / external variable, and having just a function. User changed. This avoids the need for an external variable that might clash.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 11 Mar 2019 16:40:29 -0700
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13315
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
1 /* Test the pwrite function.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
13315
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
3
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
7 (at your option) any later version.
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
8
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
12 GNU General Public License for more details.
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
13
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
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/>. */
13315
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
16
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
17 #include <config.h>
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
18
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
19 #include <unistd.h>
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
20
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
21 #include "signature.h"
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
22 SIGNATURE_CHECK (pwrite, ssize_t, (int, const void *, size_t, off_t));
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
23
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
24 #include <sys/types.h>
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
25 #include <fcntl.h>
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
26 #include <errno.h>
13373
f11f73d08233 Add missing include in test-pwrite.c.
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents: 13336
diff changeset
27 #include <string.h>
13315
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
28
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
29 #include "macros.h"
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
30
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
31 #define N (sizeof buf - 1)
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
32
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
33 int
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
34 main (void)
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
35 {
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
36 char const *file = "out";
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
37 int fd;
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
38 char buf[] = "0123456789";
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
39 off_t pos = 2;
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
40 size_t i;
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
41 off_t init_pos;
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
42
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
43 ASSERT (file);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
44
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
45 fd = open (file, O_CREAT | O_WRONLY, 0600);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
46 ASSERT (0 <= fd);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
47 ASSERT (write (fd, buf, N) == N);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
48 ASSERT (close (fd) == 0);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
49
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
50 fd = open (file, O_WRONLY, 0600);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
51 ASSERT (0 <= fd);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
52
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
53 init_pos = lseek (fd, pos, SEEK_SET);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
54 ASSERT (init_pos == pos);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
55
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
56 for (i = 0; i < N; i+=2)
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
57 {
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
58 const char byte = 'W';
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
59 ASSERT (pwrite (fd, &byte, 1, i) == 1);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
60 ASSERT (lseek (fd, 0, SEEK_CUR) == init_pos);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
61 }
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
62
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
63 {
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
64 /* Invalid offset must evoke failure with EINVAL. */
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
65 const char byte = 'b';
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
66 ASSERT (pwrite (fd, &byte, 1, (off_t) -1) == -1);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
67 ASSERT (errno == EINVAL);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
68 }
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
69
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
70 ASSERT (close (fd) == 0);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
71
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
72 {
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
73 fd = open (file, O_RDONLY);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
74 ASSERT (0 <= fd);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
75 ASSERT (read (fd, buf, N) == N);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
76 ASSERT (close (fd) == 0);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
77 ASSERT (strcmp ("W1W3W5W7W9",buf) == 0);
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
78 }
15693
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
79
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
80 /* Test behaviour for invalid file descriptors. */
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
81 {
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
82 char byte = 'x';
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
83 errno = 0;
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
84 ASSERT (pwrite (-1, &byte, 1, 0) == -1);
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
85 ASSERT (errno == EBADF);
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
86 }
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
87 {
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
88 char byte = 'x';
17292
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
89 close (99);
15693
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
90 errno = 0;
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
91 ASSERT (pwrite (99, &byte, 1, 0) == -1);
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
92 ASSERT (errno == EBADF);
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
93 }
bc4a7eb2598f pwrite tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
94
13315
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
95 return 0;
39dc842a035e Tests for module pwrite.
Peter O'Gorman <pogma@thewrittenword.com>
parents:
diff changeset
96 }