Consolidate lseek/lseek64/llseek implementations
[glibc.git] / misc / tst-preadvwritev-common.c
blob2b1e36f312e021a1352ce53e7c6fb2f655c34d7a
1 /* Common definitions for preadv and pwritev.
2 Copyright (C) 2016 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 <sys/uio.h>
21 static void do_prepare (void);
22 #define PREPARE(argc, argv) do_prepare ()
23 static int do_test (void);
24 #define TEST_FUNCTION do_test ()
25 #include "test-skeleton.c"
27 static char *temp_filename;
28 static int temp_fd;
30 static void
31 do_prepare (void)
33 temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
34 if (temp_fd == -1)
36 printf ("cannot create temporary file: %m\n");
37 exit (1);
41 #define FAIL(str) \
42 do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
44 static int
45 do_test_with_offset (off_t offset)
47 struct iovec iov[2];
48 ssize_t ret;
50 char buf1[32];
51 char buf2[64];
53 memset (buf1, 0xf0, sizeof buf1);
54 memset (buf2, 0x0f, sizeof buf2);
56 /* Write two buffer with 32 and 64 bytes respectively. */
57 memset (iov, 0, sizeof iov);
58 iov[0].iov_base = buf1;
59 iov[0].iov_len = sizeof buf1;
60 iov[1].iov_base = buf2;
61 iov[1].iov_len = sizeof buf2;
63 ret = pwritev (temp_fd, iov, 2, offset);
64 if (ret == -1)
65 FAIL ("first pwritev returned -1");
66 if (ret != (sizeof buf1 + sizeof buf2))
67 FAIL ("first pwritev returned an unexpected value");
69 ret = pwritev (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
70 if (ret == -1)
71 FAIL ("second pwritev returned -1");
72 if (ret != (sizeof buf1 + sizeof buf2))
73 FAIL ("second pwritev returned an unexpected value");
75 char buf3[32];
76 char buf4[64];
78 memset (buf3, 0x0f, sizeof buf3);
79 memset (buf4, 0xf0, sizeof buf4);
81 iov[0].iov_base = buf3;
82 iov[0].iov_len = sizeof buf3;
83 iov[1].iov_base = buf4;
84 iov[1].iov_len = sizeof buf4;
86 /* Now read two buffer with 32 and 64 bytes respectively. */
87 ret = preadv (temp_fd, iov, 2, offset);
88 if (ret == -1)
89 FAIL ("first preadv returned -1");
90 if (ret != (sizeof buf3 + sizeof buf4))
91 FAIL ("first preadv returned an unexpected value");
93 if (memcmp (buf1, buf3, sizeof buf1) != 0)
94 FAIL ("first buffer from first preadv different than expected");
95 if (memcmp (buf2, buf4, sizeof buf2) != 0)
96 FAIL ("second buffer from first preadv different than expected");
98 ret = preadv (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
99 if (ret == -1)
100 FAIL ("second preadv returned -1");
101 if (ret != (sizeof buf3 + sizeof buf4))
102 FAIL ("second preadv returned an unexpected value");
104 /* And compare the buffers read and written to check if there are equal. */
105 if (memcmp (buf1, buf3, sizeof buf1) != 0)
106 FAIL ("first buffer from second preadv different than expected");
107 if (memcmp (buf2, buf4, sizeof buf2) != 0)
108 FAIL ("second buffer from second preadv different than expected");
110 return 0;