nbio_test.c - cleanup

This commit is contained in:
twinaphex 2015-02-15 23:21:35 +01:00
parent 3b539cd2f1
commit ddf24f90d8

View File

@ -2,48 +2,67 @@
#include <string.h>
#include <stdio.h>
int main()
static void nbio_write_test(void)
{
struct nbio_t* write = nbio_open("test.bin", NBIO_WRITE);
nbio_resize(write, 1024*1024);
size_t size, prog;
bool looped=false;
void* ptr = NULL;
struct nbio_t* write = nbio_open("test.bin", NBIO_WRITE);
size_t size;
void* ptr = nbio_get_ptr(write, &size);
if (size != 1024*1024) puts("ERROR: wrong size (1)");
nbio_resize(write, 1024*1024);
memset(ptr, 0x42, 1024*1024);
nbio_begin_write(write);
ptr = nbio_get_ptr(write, &size);
if (size != 1024*1024)
puts("ERROR: wrong size (1)");
size_t prog;
bool looped=false;
while (!nbio_iterate(write, &prog, &size))
{
printf("%u/%u\n", (unsigned)prog, (unsigned)size);
looped=true;
}
if (!looped) puts("Write finished immediately?");
nbio_free(write);
memset(ptr, 0x42, 1024*1024);
nbio_begin_write(write);
while (!nbio_iterate(write, &prog, &size))
{
printf("%u/%u\n", (unsigned)prog, (unsigned)size);
looped=true;
}
struct nbio_t* read = nbio_open("test.bin", NBIO_READ);
if (!looped)
puts("Write finished immediately?");
ptr = nbio_get_ptr(read, &size);
if (size != 1024*1024) puts("ERROR: wrong size (2)");
if (ptr) puts("Read pointer is available before iterating?");
nbio_begin_read(read);
looped=false;
while (!nbio_iterate(read, &prog, &size))
{
printf("%u/%u\n", (unsigned)prog, (unsigned)size);
looped=true;
}
if (!looped) puts("Read finished immediately?");
ptr = nbio_get_ptr(read, &size);
if (size != 1024*1024) puts("ERROR: wrong size (3)");
if (*(char*)ptr != 0x42 || memcmp(ptr, (char*)ptr+1, 1024*1024-1)) puts("ERROR: wrong data");
nbio_free(read);
nbio_free(write);
}
static void nbio_read_test(void)
{
size_t size, prog;
bool looped=false;
struct nbio_t* read = nbio_open("test.bin", NBIO_READ);
void *ptr = nbio_get_ptr(read, &size);
if (size != 1024*1024)
puts("ERROR: wrong size (2)");
if (ptr)
puts("Read pointer is available before iterating?");
nbio_begin_read(read);
looped=false;
while (!nbio_iterate(read, &prog, &size))
{
printf("%u/%u\n", (unsigned)prog, (unsigned)size);
looped=true;
}
if (!looped) puts("Read finished immediately?");
ptr = nbio_get_ptr(read, &size);
if (size != 1024*1024)
puts("ERROR: wrong size (3)");
if (*(char*)ptr != 0x42 || memcmp(ptr, (char*)ptr+1, 1024*1024-1))
puts("ERROR: wrong data");
nbio_free(read);
}
int main(void)
{
nbio_write_test();
nbio_read_test();
}