aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/readv-writev.c
blob: 5025ce2ed622d370607d345d5ecde342824cde93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>

#ifdef USE_HOST_LIBC
#define TEST_FILE "readv-writev-host-libc.tmp"
#else
#define TEST_FILE "readv-writev.tmp"
#endif

int main() {
	// Make sure that it wasn't created by a previous test run
	unlink(TEST_FILE);

	int fd = open(TEST_FILE, O_RDWR | O_CREAT, 0644);
	assert(fd != -1);

	char str0[] = "hello ";
	char str1[] = "world!";

	struct iovec bufs[2] = {
		{
			.iov_base = &str0,
			.iov_len = strlen(str0),
		},
		{
			.iov_base = &str1,
			.iov_len = strlen(str1),
		},
	};

	ssize_t written = writev(fd, bufs, 2);
	assert(written == 12);

	memset(&str0, 0, strlen(str0));
	memset(&str1, 0, strlen(str1));

	assert(!lseek(fd, 0, SEEK_SET));

	ssize_t read = readv(fd, bufs, 2);
	assert(read == 12);

	assert(!strncmp(str0, "hello ", 7));
	assert(!strncmp(str1, "world!", 7));

	assert(!close(fd));

	unlink(TEST_FILE);
}