From 82b3eb757de42a379fa26f7a699899223dbcee4a Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sat, 8 Aug 2015 13:25:32 +0100 Subject: Abstract out test file loading. --- dev/main.c | 15 +++++- test/test-loader.c | 134 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 104 insertions(+), 45 deletions(-) diff --git a/dev/main.c b/dev/main.c index 7c71a44..77c408b 100644 --- a/dev/main.c +++ b/dev/main.c @@ -22,10 +22,23 @@ static void nsl_test_lwc_iterator(lwc_string *str, void *pw) int main(void) { + struct test_loader_buffer *buffer; + bool ok; + + ok = test_loader_load_path_to_buffer( + "test-writing-mode.html", + &buffer); + if (!ok) return EXIT_FAILURE; + nslayout_init(); - test_loader("test-writing-mode.html", CSS_MEDIA_ALL, 15); + + ok = test_loader(buffer, CSS_MEDIA_ALL, 15); + if (!ok) return EXIT_FAILURE; + nslayout_fini(); + test_loader_free_buffer(buffer); + printf("Remaining lwc strings:\n"); lwc_iterate_strings(nsl_test_lwc_iterator, NULL); diff --git a/test/test-loader.c b/test/test-loader.c index 9e813a0..9f56f87 100644 --- a/test/test-loader.c +++ b/test/test-loader.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -29,24 +30,27 @@ static nslayout_error test_loader_nslayout_test_callback( return NSLAYOUT_OK; } +struct test_loader_buffer { + unsigned char *buf; + size_t len; + size_t pos; +}; + struct test_loader_ctx { + struct test_loader_buffer *html; dom_hubbub_parser *parser; dom_document *doc; - unsigned char *buffer; - size_t buffer_size; - FILE *handle; css_select_ctx *css_ctx; css_stylesheet *css_sheet; }; static bool test_loader_doc_load_start( - const char *file, size_t buffer_size, + size_t chunk_length, struct test_loader_ctx *load_ctx) { dom_hubbub_parser_params params; dom_hubbub_error error; - size_t chunk_length; params.enc = NULL; params.fix_enc = true; @@ -56,35 +60,22 @@ static bool test_loader_doc_load_start( params.ctx = NULL; params.daf = NULL; - load_ctx->buffer = malloc(buffer_size); - if (load_ctx->buffer == NULL) { - return false; - } - - load_ctx->buffer_size = buffer_size; - /* Create Hubbub parser */ error = dom_hubbub_parser_create(¶ms, &load_ctx->parser, &load_ctx->doc); if (error != DOM_HUBBUB_OK) { - free(load_ctx->buffer); return false; } - /* Open input file */ - load_ctx->handle = fopen(file, "rb"); - if (load_ctx->handle == NULL) { - dom_hubbub_parser_destroy(load_ctx->parser); - free(load_ctx->buffer); - return false; - } + /* Find length of first chunk */ + if (chunk_length > (load_ctx->html->len - load_ctx->html->pos)) + chunk_length = load_ctx->html->len - load_ctx->html->pos; - /* Parse input file in chunks */ - chunk_length = buffer_size; - chunk_length = fread(load_ctx->buffer, 1, load_ctx->buffer_size, - load_ctx->handle); + /* Load first chunk */ error = dom_hubbub_parser_parse_chunk(load_ctx->parser, - load_ctx->buffer, chunk_length); + load_ctx->html->buf + load_ctx->html->pos, + chunk_length); + load_ctx->html->pos += chunk_length; if (error != DOM_HUBBUB_OK) { dom_hubbub_parser_destroy(load_ctx->parser); printf("Parsing errors occur\n"); @@ -95,24 +86,31 @@ static bool test_loader_doc_load_start( } -static bool test_loader_doc_load_next( +bool test_loader_doc_load_next( struct test_loader_ctx *load_ctx, + size_t chunk_length, bool *complete) { dom_hubbub_error error; - int chunk_length; - /* Parse input file in chunks */ - chunk_length = fread(load_ctx->buffer, 1, load_ctx->buffer_size, - load_ctx->handle); + /* Find length of chunk */ + if (chunk_length > (load_ctx->html->len - load_ctx->html->pos)) + chunk_length = load_ctx->html->len - load_ctx->html->pos; + if (chunk_length != 0) { + /* Parse the chunk */ error = dom_hubbub_parser_parse_chunk(load_ctx->parser, - load_ctx->buffer, chunk_length); + load_ctx->html->buf + load_ctx->html->pos, + chunk_length); + load_ctx->html->pos += chunk_length; if (error != DOM_HUBBUB_OK) { dom_hubbub_parser_destroy(load_ctx->parser); printf("Parsing errors occur\n"); return false; } + } + + if (load_ctx->html->len != load_ctx->html->pos) { *complete = false; return true; } @@ -130,12 +128,6 @@ static bool test_loader_doc_load_next( /* Finished with parser */ dom_hubbub_parser_destroy(load_ctx->parser); - /* Close input file */ - if (fclose(load_ctx->handle) != 0) { - printf("Can't close test input file\n"); - return false; - } - return true; } @@ -239,7 +231,63 @@ fail: } -static bool test_loader(const char *document_path, +bool test_loader_load_path_to_buffer( + const char *path, struct test_loader_buffer **buffer) +{ + unsigned char *buf; + long l; + size_t read; + FILE *f = NULL; + + f = fopen(path, "rb"); + if (f == NULL) { + printf("File could not be opened: %s\n", + strerror(errno)); + return false; + } + fseek(f, 0, SEEK_END); + l = ftell(f); + if (l < 0) { + printf("Could not find end of file: %s\n", + strerror(errno)); + fclose(f); + return false; + } + fseek(f, 0, SEEK_SET); + + *buffer = malloc(sizeof(struct test_loader_buffer) + l + 1); + if (*buffer == NULL) { + printf("Could allocate space for file\n"); + fclose(f); + return false; + } + + buf = (unsigned char *)(*buffer + 1); + read = fread(buf, 1, l, f); + fclose(f); + + if (((long)read) != l) { + printf("Read unexpected data length from file\n"); + return false; + } + + buf[read] = '\0'; + + (*buffer)->len = read; + (*buffer)->buf = buf; + (*buffer)->pos = 0; + return true; +} + + +void test_loader_free_buffer(struct test_loader_buffer *buffer) +{ + free(buffer); +} + + +static bool test_loader( + struct test_loader_buffer *buffer, css_media_type media, size_t chunk_size) { @@ -253,14 +301,12 @@ static bool test_loader(const char *document_path, load_ctx.parser = NULL; load_ctx.doc = NULL; - load_ctx.buffer = NULL; - load_ctx.buffer_size = 0; - load_ctx.handle = NULL; + load_ctx.html = buffer; load_ctx.css_sheet = NULL; load_ctx.css_ctx = NULL; printf("Starting load\n"); - if (!test_loader_doc_load_start(document_path, chunk_size, &load_ctx)) { + if (!test_loader_doc_load_start(chunk_size, &load_ctx)) { printf("ERROR: doc_load_start\n"); goto fail; } @@ -284,7 +330,8 @@ static bool test_loader(const char *document_path, while (!complete) { printf("Loading a chunk of the document\n"); - if (!test_loader_doc_load_next(&load_ctx, &complete)) { + if (!test_loader_doc_load_next(&load_ctx, chunk_size, + &complete)) { printf("ERROR: doc_load_next\n"); goto fail; } @@ -301,7 +348,6 @@ fail: } test_loader_css_fini(&load_ctx); dom_node_unref(load_ctx.doc); - free(load_ctx.buffer); return ret; } -- cgit v1.2.3