Skip to content

Commit

Permalink
test/reader: import legacy reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Aug 16, 2023
1 parent a4a3bd6 commit 31a6519
Showing 1 changed file with 224 additions and 3 deletions.
227 changes: 224 additions & 3 deletions test/reader/reader2.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ test_iter_next(void *data, size_t desired_size) {
(void)desired_size;
if (iter->remaining == 0) {
iter->data = "";
}
if (iter->remaining == 0) {
return -1;
return 0;
}
iter->remaining--;
iter->size = strlen(iter->data);
Expand Down Expand Up @@ -304,6 +302,219 @@ test_reader2_advance_with_zero_size() {
sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_advance_once(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {
.data = "THIS IS A TEST STRING", .remaining = 1};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 0, 6);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(data == (void *)iter.data);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_advance_once_with_offset(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {
.data = "THIS IS A TEST STRING", .remaining = 1};
rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 4, 6);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(data == (uint8_t *)&iter.data[4]);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_advance_twice_with_offset(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {
.data = "THIS IS A TEST STRING", .remaining = 1};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 4, 6);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(data == (uint8_t *)&iter.data[4]);

rv = sqsh__reader2_advance(&reader, 6, 2);
assert(rv == 0);

const uint8_t *data2 = sqsh__reader2_data(&reader);
assert(data2 == (uint8_t *)&iter.data[10]);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_initial_advance(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {
.data = "THIS IS A TEST STRING", .remaining = 1};
rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 5, 3);
assert(rv == 0);
const uint8_t *data = sqsh__reader2_data(&reader);
assert(3 == sqsh__reader2_size(&reader));
assert(memcmp(data, "IS ", 3) == 0);
assert(data == (uint8_t *)&iter.data[5]);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_advance_to_out_of_bounds(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {
.data = "THIS IS A TEST STRING", .remaining = 1};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, strlen(iter.data), 1);
assert(rv < 0);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_advance_over_boundary(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {.data = "0123456789", .remaining = 2};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 9, 2);
assert(rv == 0);
const uint8_t *data = sqsh__reader2_data(&reader);
assert(2 == sqsh__reader2_size(&reader));
assert(memcmp(data, "90", 2) == 0);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_initial_advance_2(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {.data = "ABCD", .remaining = 10};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 7, 5);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 5);
assert(memcmp(data, "DABCD", 5) == 0);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_error_1(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {.data = "AB", .remaining = 10};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 0, 4);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 4);
assert(memcmp(data, "ABAB", 4) == 0);

iter.data = "12";
rv = sqsh__reader2_advance(&reader, 4, 4);
assert(rv == 0);

data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 4);
assert(memcmp(data, "1212", 4) == 0);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_map_into_buffer(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {.data = "0123456789", .remaining = 2};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 8, 4);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 4);
assert(memcmp(data, "8901", 4) == 0);

rv = sqsh__reader2_advance(&reader, 1, 4);
assert(rv == 0);

data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 4);
assert(memcmp(data, "9012", 4) == 0);

sqsh__reader2_cleanup(&reader);
}

static void
test_reader2_map_into_buffer_twice(void) {
int rv;
struct SqshReader2 reader = {0};
struct TestIterator iter = {.data = "0123456789", .remaining = 3};

rv = sqsh__reader2_init(&reader, &test_iter, &iter);
assert(rv == 0);

rv = sqsh__reader2_advance(&reader, 8, 4);
assert(rv == 0);

const uint8_t *data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 4);
assert(memcmp(data, "8901", 4) == 0);

rv = sqsh__reader2_advance(&reader, 1, 14);
assert(rv == 0);

data = sqsh__reader2_data(&reader);
assert(sqsh__reader2_size(&reader) == 14);
assert(memcmp(data, "90123456789012", 14) == 0);

sqsh__reader2_cleanup(&reader);
}

DECLARE_TESTS
TEST(test_reader2_init)
TEST(test_reader2_advance_to_block)
Expand All @@ -315,4 +526,14 @@ TEST(test_reader2_from_buffered_to_buffered)
TEST(test_reader2_from_buffered_to_buffered)
TEST(test_reader2_advance_inside_buffered)
TEST(test_reader2_advance_with_zero_size)
TEST(test_reader2_advance_once)
TEST(test_reader2_advance_once_with_offset)
TEST(test_reader2_advance_twice_with_offset)
TEST(test_reader2_initial_advance)
TEST(test_reader2_advance_to_out_of_bounds)
TEST(test_reader2_advance_over_boundary)
TEST(test_reader2_initial_advance_2)
TEST(test_reader2_error_1)
TEST(test_reader2_map_into_buffer)
TEST(test_reader2_map_into_buffer_twice)
END_TESTS

0 comments on commit 31a6519

Please sign in to comment.