1 /* Common definitions for preadv and pwritev.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <array_length.h>
27 #include <support/check.h>
28 #include <support/temp_file.h>
29 #include <support/xunistd.h>
31 static char *temp_filename
;
34 static int do_test (void);
37 do_prepare (int argc
, char **argv
)
39 temp_fd
= create_temp_file ("tst-preadvwritev.", &temp_filename
);
41 FAIL_EXIT1 ("cannot create temporary file");
43 #define PREPARE do_prepare
46 # define PREADV(__fd, __iov, __iovcnt, __offset) \
47 preadv (__fd, __iov, __iovcnt, __offset)
51 # define PWRITEV(__fd, __iov, __iovcnt, __offset) \
52 pwritev (__fd, __iov, __iovcnt, __offset)
55 static __attribute__ ((unused
)) void
56 do_test_without_offset (void)
58 xftruncate (temp_fd
, 0);
60 xwrite (temp_fd
, "123", 3);
61 xlseek (temp_fd
, 2, SEEK_SET
);
65 { (void *) "abc", 3 },
66 { (void *) "xyzt", 4 },
68 TEST_COMPARE (PWRITEV (temp_fd
, iov
, array_length (iov
), -1), 7);
70 TEST_COMPARE (xlseek (temp_fd
, 0, SEEK_CUR
), 9);
72 xlseek (temp_fd
, 1, SEEK_SET
);
78 { buf1
, sizeof (buf1
) },
79 { buf2
, sizeof (buf2
) },
81 TEST_COMPARE (PREADV (temp_fd
, iov
, array_length (iov
), -1),
82 sizeof (buf1
) + sizeof (buf2
));
83 TEST_COMPARE (memcmp ("2ab", buf1
, sizeof (buf1
)), 0);
84 TEST_COMPARE (memcmp ("cx", buf2
, sizeof (buf2
)), 0);
85 TEST_COMPARE (xlseek (temp_fd
, 0, SEEK_CUR
), 6);
88 xftruncate (temp_fd
, 0);
92 do_test_with_offset (off_t offset
)
100 memset (buf1
, 0xf0, sizeof buf1
);
101 memset (buf2
, 0x0f, sizeof buf2
);
103 /* Write two buffer with 32 and 64 bytes respectively. */
104 memset (iov
, 0, sizeof iov
);
105 iov
[0].iov_base
= buf1
;
106 iov
[0].iov_len
= sizeof buf1
;
107 iov
[1].iov_base
= buf2
;
108 iov
[1].iov_len
= sizeof buf2
;
110 ret
= PWRITEV (temp_fd
, iov
, 2, offset
);
112 FAIL_RET ("first pwritev returned -1");
113 if (ret
!= (sizeof buf1
+ sizeof buf2
))
114 FAIL_RET ("first pwritev returned an unexpected value");
116 ret
= PWRITEV (temp_fd
, iov
, 2, sizeof buf1
+ sizeof buf2
+ offset
);
118 FAIL_RET ("second pwritev returned -1");
119 if (ret
!= (sizeof buf1
+ sizeof buf2
))
120 FAIL_RET ("second pwritev returned an unexpected value");
125 memset (buf3
, 0x0f, sizeof buf3
);
126 memset (buf4
, 0xf0, sizeof buf4
);
128 iov
[0].iov_base
= buf3
;
129 iov
[0].iov_len
= sizeof buf3
;
130 iov
[1].iov_base
= buf4
;
131 iov
[1].iov_len
= sizeof buf4
;
133 /* Now read two buffer with 32 and 64 bytes respectively. */
134 ret
= PREADV (temp_fd
, iov
, 2, offset
);
136 FAIL_RET ("first preadv returned -1");
137 if (ret
!= (sizeof buf3
+ sizeof buf4
))
138 FAIL_RET ("first preadv returned an unexpected value");
140 if (memcmp (buf1
, buf3
, sizeof buf1
) != 0)
141 FAIL_RET ("first buffer from first preadv different than expected");
142 if (memcmp (buf2
, buf4
, sizeof buf2
) != 0)
143 FAIL_RET ("second buffer from first preadv different than expected");
145 ret
= PREADV (temp_fd
, iov
, 2, sizeof buf3
+ sizeof buf4
+ offset
);
147 FAIL_RET ("second preadv returned -1");
148 if (ret
!= (sizeof buf3
+ sizeof buf4
))
149 FAIL_RET ("second preadv returned an unexpected value");
151 /* And compare the buffers read and written to check if there are equal. */
152 if (memcmp (buf1
, buf3
, sizeof buf1
) != 0)
153 FAIL_RET ("first buffer from second preadv different than expected");
154 if (memcmp (buf2
, buf4
, sizeof buf2
) != 0)
155 FAIL_RET ("second buffer from second preadv different than expected");
160 #include <support/test-driver.c>