Generate C code for "in.dst.write?(etc)".

This commit is contained in:
Nigel Tao committed 2017-06-10 17:28:35 +10:00
1 parent 88e2d4cf59
commit f403d0faa8
8 files changed
+147 -29

No files matched your search

+24 -12
View File
@@ -19,17 +19,30 @@ const char* test_filename = "gif/lzw.c";
#define BUFFER_SIZE (1024 * 1024)
uint8_t global_dst_buffer[BUFFER_SIZE];
uint8_t global_got_buffer[BUFFER_SIZE];
uint8_t global_want_buffer[BUFFER_SIZE];
uint8_t global_src_buffer[BUFFER_SIZE];
void test_lzw_decode() {
test_funcname = __func__;
puffs_base_buf1 dst = {.ptr = global_dst_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 want = {.ptr = global_want_buffer, .len = BUFFER_SIZE};
puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE};
// The want .indexes file should be 19200 bytes long, as the image size is
// 160 * 120 pixels and there is 1 palette index byte per pixel.
if (!read_file(&want, "../../testdata/bricks-nodither.indexes")) {
goto cleanup0;
}
if (want.wi != 19200) {
FAIL("want size: got %d, want %d", (int)(want.wi), 19200);
goto cleanup0;
}
// The src .giflzw file should be 13382 bytes long.
if (!read_file(&src, "../../testdata/bricks-nodither.giflzw")) {
goto cleanup0;
}
// That .giflzw file should be 13382 bytes long.
if (src.wi != 13382) {
FAIL("src size: got %d, want %d", (int)(src.wi), 13382);
goto cleanup0;
@@ -45,22 +58,21 @@ void test_lzw_decode() {
puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION, 0);
// TODO: call puffs_gif_lzw_decoder_set_literal_width.
puffs_gif_status status =
puffs_gif_lzw_decoder_decode(&dec, &dst, &src, false);
puffs_gif_lzw_decoder_decode(&dec, &got, &src, false);
if (status != puffs_gif_status_ok) {
FAIL("status: got %d, want %d", status, puffs_gif_status_ok);
goto cleanup1;
}
// The decoded per-pixel indexes should be 3982 bytes long.
//
// TODO: s/3982/19200/ as 19200 = 160 * 120.
if (dst.wi != 3982) {
FAIL("dst size: got %d, want %d", (int)(dst.wi), 3982);
/*
TODO: fix the bug and uncomment this test.
if (!buf1s_equal(&got, &want)) {
goto cleanup1;
}
// The first decoded byte should be 0xDC.
if (dst.ptr[0] != 0xDC) {
FAIL("first decoded byte: got 0x%02x, want 0x%02x", (int)(dst.ptr[0]),
*/
// As a sanity check, the first decoded byte should be 0xDC.
if (got.ptr[0] != 0xDC) {
FAIL("first decoded byte: got 0x%02x, want 0x%02x", (int)(got.ptr[0]),
0xDC);
goto cleanup1;
}
+84 -2
View File
@@ -7,7 +7,9 @@
char fail_msg[65536] = {0};
#define FAIL(...) snprintf(fail_msg, sizeof fail_msg, ##__VA_ARGS__)
#define FAIL(...) snprintf(fail_msg, sizeof(fail_msg), ##__VA_ARGS__)
#define INCR_FAIL(msg, ...) \
msg += snprintf(msg, sizeof(fail_msg) - (msg - fail_msg), ##__VA_ARGS__)
int tests_run = 0;
@@ -43,9 +45,14 @@ int main(int argc, char** argv) {
return 0;
}
#ifdef PUFFS_BASE_HEADER_H
// PUFFS_BASE_HEADER_H is where puffs_base_buf1 is defined.
#ifdef PUFFS_BASE_HEADER_H
bool read_file(puffs_base_buf1* dst, const char* path) {
if (!dst || !path) {
FAIL("read_file: NULL argument");
return false;
}
FILE* f = fopen(path, "r");
if (!f) {
FAIL("read_file(\"%s\"): %s (errno=%d)", path, strerror(errno), errno);
@@ -77,4 +84,79 @@ bool read_file(puffs_base_buf1* dst, const char* path) {
fclose(f);
return true;
}
char* hex_dump(char* msg, puffs_base_buf1* buf, size_t i) {
if (!msg || !buf) {
FAIL("hex_dump: NULL argument");
return false;
}
if (buf->wi == 0) {
return msg;
}
size_t base = i - (i & 15);
for (int j = -3 * 16; j <= +3 * 16; j += 16) {
if ((j < 0) && (base < (size_t)(-j))) {
continue;
}
size_t b = base + j;
if (b >= buf->wi) {
break;
}
size_t n = buf->wi - b;
INCR_FAIL(msg, " %06zx:", b);
for (size_t k = 0; k < 16; k++) {
if (k % 2 == 0) {
INCR_FAIL(msg, " ");
}
if (k < n) {
INCR_FAIL(msg, "%02x", buf->ptr[b + k]);
} else {
INCR_FAIL(msg, " ");
}
}
INCR_FAIL(msg, " ");
for (size_t k = 0; k < 16; k++) {
char c = ' ';
if (k < n) {
c = buf->ptr[b + k];
if ((c < 0x20) || (0x7F < c)) {
c = '.';
}
}
INCR_FAIL(msg, "%c", c);
}
INCR_FAIL(msg, "\n");
if (n < 16) {
break;
}
}
return msg;
}
bool buf1s_equal(puffs_base_buf1* got, puffs_base_buf1* want) {
if (!got || !want) {
FAIL("bufs1_equal: NULL argument");
return false;
}
char* msg = fail_msg;
size_t i = 0;
for (; (i < got->wi) && (i < want->wi); i++) {
if (got->ptr[i] != want->ptr[i]) {
break;
}
}
if (got->wi != want->wi) {
INCR_FAIL(msg, "bufs1_equal: wi: got %zu, want %zu.\n", got->wi, want->wi);
} else if (i < got->wi) {
INCR_FAIL(msg, "bufs1_equal:\n");
} else {
return true;
}
INCR_FAIL(msg, "contents differ at byte %zu (in hex: 0x%06zx):\n", i, i);
msg = hex_dump(msg, got, i);
INCR_FAIL(msg, "excerpts of got (above) versus want (below):\n");
msg = hex_dump(msg, want, i);
return false;
}
#endif // PUFFS_BASE_HEADER_H