77 lines
3 KiB
C
77 lines
3 KiB
C
#include <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include <vessel/stream.h>
|
|
|
|
#define HW "Hello, World!"
|
|
#define HWL 13
|
|
|
|
#define Err(...) \
|
|
{ \
|
|
fprintf(stderr, __VA_ARGS__); \
|
|
return 1; \
|
|
}
|
|
|
|
#define Test(cond, ...) \
|
|
puts("Test false: " #cond); \
|
|
if (cond) \
|
|
Err(__VA_ARGS__)
|
|
|
|
#define Refresh(fp, fd) \
|
|
printf("Refreshing stream %d...\n", fd); \
|
|
Test(!Stream_stop(&(fp)), "Failed to stop stream %d\n", (fd)); \
|
|
Test((fp).used, "The stream for file descriptor %d is not used (?).\n", (fd)); \
|
|
Test(lseek((fd), 0, SEEK_SET) == -1, "Failed to seek to the beginning in fd %d\n", (fd)); \
|
|
Test(!Stream_use(&(fp), (fd)), \
|
|
"Unable to use a buffered stream for file descriptor %d\n", \
|
|
(fd)); \
|
|
Test(!(fp).used, "The stream for file descriptor %d is not being used (?).\n", (fd)); \
|
|
printf("Stream %d refreshed.\n", fd);
|
|
|
|
int main(void) {
|
|
off_t sz;
|
|
char buf[128];
|
|
Stream fp = { 0 };
|
|
int fd = open("stream.test", O_RDWR | O_APPEND | O_CREAT, 0644);
|
|
|
|
Test(fd < 0, "Failed to open file `stream.test`.\n");
|
|
|
|
Test(!Stream_init(&fp), "Unable to Initialize the stream.\n");
|
|
Test(fp.used, "Unable to Initialize the stream.\n");
|
|
|
|
Test(!Stream_use(&fp, fd), "Unable to use a buffered stream for file descriptor %d\n", fd);
|
|
Test(!fp.used, "The stream for file descriptor %d is not being used (?).\n", fd);
|
|
|
|
Test(Stream_write(&fp, HW, HWL) != HWL, "Unable to write %d bytes to file.\n", HWL);
|
|
Test(!Stream_flush(&fp), "Failed to flush stream %d\n", fd);
|
|
|
|
Refresh(fp, fd);
|
|
|
|
Test(Stream_read(&fp, buf, HWL) != HWL, "Unable to read %d bytes from fd %d.\n", HWL, fd);
|
|
Test(strncmp(buf, HW, HWL) != 0, "Failed to read \"%s\" from file.\n", HW);
|
|
|
|
Test(Stream_writef(&fp, "Test: %s", HW) != (HWL + 6),
|
|
"Unable to write %d bytes to file.\n",
|
|
HWL + 6);
|
|
Test(!Stream_flush(&fp), "Failed to flush stream %d\n", fd);
|
|
|
|
Refresh(fp, fd);
|
|
|
|
Test((sz = lseek(fd, 0, SEEK_END)) == -1, "Failed to get the file size of stream %d\n", fd);
|
|
Test(lseek(fd, sz - 19, SEEK_SET) == -1, "Failed to move back 19 bytes in stream %d\n", fd);
|
|
|
|
Test(Stream_read(&fp, buf, HWL + 6) != (HWL + 6),
|
|
"Unable to read %d bytes from fd %d.\n",
|
|
HWL,
|
|
fd);
|
|
Test(strncmp(buf, "Test: " HW, HWL) != 0, "Failed to read \"Test: %s\" from file.\n", HW);
|
|
|
|
Test(!Stream_destroy(&fp), "Failed to destroy stream %d\n", fd);
|
|
Test(close(fd) < 0, "Failed to close fd %d\n", fd);
|
|
|
|
puts("All stream tests successfully passed.");
|
|
|
|
return 0;
|
|
}
|