FxLibcTest/src/unistd/files.c

247 lines
6.1 KiB
C

#include <ft/test.h>
#include <ft/all-tests.h>
#include <ft/util.h>
#include <gint/gint.h>
#include <gint/bfile.h>
#include <gint/fs.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);
}
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
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);
}
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
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);
}
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
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,
};
static void _ft_unistd_simple_read_switch(ft_test *t)
{
/* First we need to generate some stuff to read... */
int fd, rc;
errno = 0;
DO_E(fd, open("ft_read.txt", O_WRONLY | O_CREAT | O_TRUNC), t, "%d");
ft_assert(t, fd >= 0);
if(fd >= 0) {
DO_E(rc, write(fd, "_ft_unistd_simple_read", 22), t, "%d");
ft_assert(t, rc == 22);
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
else return;
/* Then actually read it */
DO_E(fd, open("ft_read.txt", O_RDONLY), t, "%d");
ft_assert(t, fd >= 0);
if(fd < 0) return;
int fugue_fd = (int)fs_get_descriptor(fd)->data;
ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n",
lseek(fd, 0, SEEK_CUR));
DO_E(rc, BFile_Size(fugue_fd), t, "%d");
char buffer[29] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
DO_E(rc, read(fd, buffer, 11), t, "%d");
ft_assert(t, rc == 11);
ft_assert(t, !strcmp(buffer, "_ft_unistd_xxxxxxxxxxxxxxxxx"));
ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n",
lseek(fd, 0, SEEK_CUR));
DO_E(rc, BFile_Size(fugue_fd), t, "%d");
DO_E(rc, read(fd, buffer + 11, 11), t, "%d");
ft_assert(t, rc == 11);
ft_assert(t, !strcmp(buffer, "_ft_unistd_simple_readxxxxxx"));
ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n",
lseek(fd, 0, SEEK_CUR));
DO_E(rc, BFile_Size(fugue_fd), t, "%d");
ft_log(t, "[INFO] lseek(fd, 4, SEEK_SET) = %ld\n",
lseek(fd, 4, SEEK_SET));
ft_log(t, "[INFO] lseek(fd, 4, SEEK_END) = %ld\n",
lseek(fd, 4, SEEK_END));
ft_log(t, "[INFO] lseek(fd, 0, SEEK_CUR) = %ld\n",
lseek(fd, 4, SEEK_CUR));
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
static void _ft_unistd_simple_read(ft_test *t)
{
gint_world_switch(GINT_CALL(_ft_unistd_simple_read_switch, (void *)t));
}
ft_test ft_unistd_simple_read = {
.name = "Simple read",
.function = _ft_unistd_simple_read,
};
static void _ft_unistd_seek_patterns_switch(ft_test *t)
{
/* Generate a basic file */
int fd, rc;
errno = 0;
DO_E(fd, open("ft_seek.txt", O_WRONLY | O_CREAT | O_TRUNC), t, "%d");
ft_assert(t, fd >= 0);
if(fd >= 0) {
DO_E(rc, write(fd, "_ft_unistd_seek_patterns", 24), t, "%d");
ft_assert(t, rc == 24);
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
else return;
DO_E(fd, open("ft_seek.txt", O_RDWR), t, "%d");
ft_assert(t, fd >= 0);
if(fd < 0) return;
int fugue_fd = (int)fs_get_descriptor(fd)->data;
/* Read from the start */
char str[32] = { 0 };
DO_E(rc, read(fd, str, 8), t, "%d");
ft_assert(t, rc == 8 && !strncmp(str, "_ft_unis", 8));
DO_E(rc, read(fd, str, 4), t, "%d");
ft_assert(t, rc == 4 && !strncmp(str, "td_sunis", 8));
/* Rewind */
DO_E(rc, lseek(fd, 0, SEEK_SET), t, "%d");
ft_assert(t, rc == 0);
DO_E(rc, read(fd, str, 24), t, "%d");
ft_assert(t, rc == 24 && !strcmp(str, "_ft_unistd_seek_patterns"));
/* Read from the middle */
DO_E(rc, pread(fd, str, 8, (off_t)12), t, "%d");
ft_assert(t, rc == 8 && !strncmp(str, "eek_patt", 8));
/* Write to the end */
DO_E(rc, lseek(fd, -8, SEEK_END), t, "%d");
ft_assert(t, rc == 16);
DO_E(rc, write(fd, "methods!", 8), t, "%d");
ft_assert(t, rc == 8);
/* Read again from the end */
DO_E(rc, pread(fd, str, 8, (off_t)16), t, "%d");
ft_assert(t, rc == 8 && !strncmp(str, "methods!", 8));
/* Play around with absolute seeking */
DO_E(rc, lseek(fd, -4, SEEK_END), t, "%d");
ft_assert(t, rc == 20);
DO_E(rc, lseek(fd, 0, SEEK_END), t, "%d");
ft_assert(t, rc == BFile_Size(fugue_fd));
DO_E(rc, lseek(fd, 8, SEEK_SET), t, "%d");
ft_assert(t, rc == 8);
/* Relative seeking */
DO_E(rc, lseek(fd, -6, SEEK_CUR), t, "%d");
ft_assert(t, rc == 2);
/* Write in the middle */
DO_E(rc, pwrite(fd, "@", 1, (off_t)3), t, "%d");
ft_assert(t, rc == 1);
DO_E(rc, lseek(fd, 0, SEEK_SET), t, "%d");
ft_assert(t, rc == 0);
memset(str, 0, sizeof str);
DO_E(rc, read(fd, str, 24), t, "%d");
ft_assert(t, rc == 24 && !strcmp(str, "_ft@unistd_seek_methods!"));
DO_E(rc, close(fd), t, "%d");
ft_assert(t, rc == 0);
}
static void _ft_unistd_seek_patterns(ft_test *t)
{
gint_world_switch(GINT_CALL(_ft_unistd_seek_patterns_switch,
(void *)t));
}
ft_test ft_unistd_seek_patterns = {
.name = "Seek patterns",
.function = _ft_unistd_seek_patterns,
};