diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/readv-writev.c | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/readv-writev.c')
-rw-r--r-- | lib/mlibc/tests/posix/readv-writev.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/readv-writev.c b/lib/mlibc/tests/posix/readv-writev.c new file mode 100644 index 0000000..5025ce2 --- /dev/null +++ b/lib/mlibc/tests/posix/readv-writev.c @@ -0,0 +1,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); +} |