add test for pread, pwrite
This commit is contained in:
parent
d5e8d43e7b
commit
0a8e84f4de
@ -12,6 +12,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
const char* write_msg = "Hello World\n";
|
const char* write_msg = "Hello World\n";
|
||||||
char read_buf[128] = {0};
|
char read_buf[128] = {0};
|
||||||
|
|
||||||
|
// write
|
||||||
flags = O_WRONLY | O_CREAT| O_TRUNC;
|
flags = O_WRONLY | O_CREAT| O_TRUNC;
|
||||||
mode = 00666;
|
mode = 00666;
|
||||||
if ((fd = open(file_name, flags, mode)) < 0) {
|
if ((fd = open(file_name, flags, mode)) < 0) {
|
||||||
@ -24,6 +25,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
// read
|
||||||
flags = O_RDONLY;
|
flags = O_RDONLY;
|
||||||
if ((fd = open(file_name, flags)) < 0) {
|
if ((fd = open(file_name, flags)) < 0) {
|
||||||
printf("ERROR: failed to open a file for read\n");
|
printf("ERROR: failed to open a file for read\n");
|
||||||
@ -40,14 +42,14 @@ int main(int argc, const char* argv[]) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writev
|
||||||
flags = O_RDWR;
|
flags = O_WRONLY | O_CREAT| O_TRUNC;
|
||||||
if ((fd = open(file_name, flags)) < 0) {
|
if ((fd = open(file_name, flags)) < 0) {
|
||||||
printf("ERROR: failed to open a file for read and write\n");
|
printf("ERROR: failed to open a file for write\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* iov_msg[2] = {"hello ", "world!"};
|
const char* iov_msg[2] = {"hello_", "world!"};
|
||||||
struct iovec iov[2];
|
struct iovec iov[2];
|
||||||
for(int i=0; i<2; ++i) {
|
for(int i=0; i<2; ++i) {
|
||||||
iov[i].iov_base = (void*)iov_msg[i];
|
iov[i].iov_base = (void*)iov_msg[i];
|
||||||
@ -58,22 +60,46 @@ int main(int argc, const char* argv[]) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((offset = lseek(fd, 0, SEEK_SET)) != 0) {
|
// pwrite
|
||||||
|
if ((len = pwrite(fd, " ", 1, 5)) != 1) {
|
||||||
|
printf("ERROR: failed to pwrite to the file\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
flags = O_RDONLY;
|
||||||
|
if ((fd = open(file_name, flags)) < 0) {
|
||||||
|
printf("ERROR: failed to open a file for read\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// lseek
|
||||||
|
if ((offset = lseek(fd, 2, SEEK_SET)) != 2) {
|
||||||
printf("ERROR: failed to lseek the file\n");
|
printf("ERROR: failed to lseek the file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readv
|
||||||
iov[0].iov_base = read_buf;
|
iov[0].iov_base = read_buf;
|
||||||
iov[0].iov_len = 3;
|
iov[0].iov_len = 3;
|
||||||
iov[1].iov_base = read_buf + 5;
|
iov[1].iov_base = read_buf + 5;
|
||||||
iov[1].iov_len = 20;
|
iov[1].iov_len = 20;
|
||||||
if ((len = readv(fd, iov, 2)) != 12) {
|
if ((len = readv(fd, iov, 2)) != 10) {
|
||||||
printf("ERROR: failed to read vectors from the file\n");
|
printf("ERROR: failed to read vectors from the file\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (memcmp(read_buf, "hel", 3) != 0
|
if (memcmp(read_buf, "llo", 3) != 0
|
||||||
|| memcmp(read_buf + 5, "lo world!", 9) != 0) {
|
|| memcmp(read_buf + 5, " world!", 7) != 0) {
|
||||||
|
printf("ERROR: the message read from the file is not as it was written\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pread
|
||||||
|
if ((len = pread(fd, read_buf, sizeof(read_buf) - 1, 4)) != 8) {
|
||||||
|
printf("ERROR: failed to pread from the file\n");
|
||||||
|
}
|
||||||
|
if (memcmp(read_buf, "o world!", 8) != 0) {
|
||||||
printf("ERROR: the message read from the file is not as it was written\n");
|
printf("ERROR: the message read from the file is not as it was written\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user