FxLibcTest/src/unistd/files.c

88 lines
1.8 KiB
C

#include <ft/test.h>
#include <ft/all-tests.h>
#include <ft/util.h>
#include <gint/gint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
static void _ft_unistd_simple_write_switch(ft_test *t)
{
int fd, rc;
errno = 0;
DO_E(fd, open("ft_write.txt", O_WRONLY | O_CREAT), t, "%d");
ft_assert(t, fd >= 0);
if(fd >= 0) {
DO_E(rc, write(fd, "write\n", 6), t, "%d");
ft_assert(t, rc == 6);
DO_E(rc, write(fd, "line #2\n", 8), t, "%d");
ft_assert(t, rc == 8);
DO_E(rc, write(fd, "line #3\n", 8), t, "%d");
ft_assert(t, rc == 8);
}
close(fd);
}
static void _ft_unistd_simple_write(ft_test *t)
{
gint_world_switch(GINT_CALL(_ft_unistd_simple_write_switch,(void *)t));
}
ft_test ft_unistd_simple_write = {
.name = "Write simple file",
.function = _ft_unistd_simple_write,
};
static void _ft_unistd_write_odd_switch_1(ft_test *t)
{
int fd, rc;
errno = 0;
DO_E(fd, open("ft_odd1.txt", O_WRONLY | O_CREAT), t, "%d");
ft_assert(t, fd >= 0);
if(fd >= 0) {
DO_E(rc, write(fd, "write odd byte count\n", 21), t, "%d");
ft_assert(t, rc == 21);
DO_E(rc, write(fd, "again, cancelling it\n", 21), t, "%d");
ft_assert(t, rc == 21);
}
close(fd);
}
static void _ft_unistd_write_odd_switch_2(ft_test *t)
{
int fd, rc;
errno = 0;
DO_E(fd, open("ft_odd2.txt", O_WRONLY | O_CREAT), t, "%d");
ft_assert(t, fd >= 0);
if(fd >= 0) {
DO_E(rc, write(fd, "write odd byte count\n", 21), t, "%d");
ft_assert(t, rc == 21);
DO_E(rc, write(fd, "then even, keeping it\n", 22), t, "%d");
ft_assert(t, rc == 22);
}
close(fd);
}
static void _ft_unistd_write_odd(ft_test *t)
{
gint_world_switch(GINT_CALL(_ft_unistd_write_odd_switch_1, (void *)t));
gint_world_switch(GINT_CALL(_ft_unistd_write_odd_switch_2, (void *)t));
}
ft_test ft_unistd_write_odd = {
.name = "Odd-length writes",
.function = _ft_unistd_write_odd,
};