From aba54ed61e31df318abdfa165f971a11ce084608 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Tue, 11 Aug 2009 11:17:23 +0000 Subject: Merge the branches/struggleyb/libdom-remain back to trunk. svn path=/trunk/dom/; revision=9191 --- test/testutils/comparators.c | 80 +++++++++++++ test/testutils/comparators.h | 23 ++++ test/testutils/domts.h | 25 ++++ test/testutils/domtsasserts.c | 253 ++++++++++++++++++++++++++++++++++++++++ test/testutils/domtsasserts.h | 61 ++++++++++ test/testutils/domtscondition.c | 32 +++++ test/testutils/domtscondition.h | 18 +++ test/testutils/foreach.c | 96 +++++++++++++++ test/testutils/foreach.h | 41 +++++++ test/testutils/list.c | 171 +++++++++++++++++++++++++++ test/testutils/list.h | 70 +++++++++++ test/testutils/load.c | 151 ++++++++++++++++++++++++ test/testutils/utils.c | 34 ++++++ test/testutils/utils.h | 35 ++++++ 14 files changed, 1090 insertions(+) create mode 100644 test/testutils/comparators.c create mode 100644 test/testutils/comparators.h create mode 100644 test/testutils/domts.h create mode 100644 test/testutils/domtsasserts.c create mode 100644 test/testutils/domtsasserts.h create mode 100644 test/testutils/domtscondition.c create mode 100644 test/testutils/domtscondition.h create mode 100644 test/testutils/foreach.c create mode 100644 test/testutils/foreach.h create mode 100644 test/testutils/list.c create mode 100644 test/testutils/list.h create mode 100644 test/testutils/load.c create mode 100644 test/testutils/utils.c create mode 100644 test/testutils/utils.h (limited to 'test/testutils') diff --git a/test/testutils/comparators.c b/test/testutils/comparators.c new file mode 100644 index 0000000..20630c6 --- /dev/null +++ b/test/testutils/comparators.c @@ -0,0 +1,80 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#include "comparators.h" +#include "domts.h" + +#include + +#include + +/* Compare to integer, return zero if equal */ +int int_comparator(const void* a, const void* b) { + return *((const int *)a) - *((const int *)b); +} + +/* Compare two string. The first one is a char * and the second + * one is a dom_string, return zero if equal */ +int str_cmp(const void *a, const void *b) +{ + const char *excepted = (const char *) a; + dom_string *actual = (dom_string *) b; + dom_string *exp; + dom_exception err; + bool ret; + + err = dom_string_create(myrealloc, NULL, excepted, strlen(excepted), + &exp); + if (err != DOM_NO_ERR) + return false; + + ret = dom_string_cmp(exp, actual) == 0; + + dom_string_unref(exp); + + if (ret == true) + return 0; + else + return 1; +} + +/* Similar with str_cmp but the first param is a dom_string the second + * param is a char * */ +int str_cmp_r(const void *a, const void *b) +{ + return str_cmp(b, a); +} + +/* Similar with str_cmp but ignore the case of letters */ +int str_icmp(const void *a, const void *b) +{ + const char *excepted = (const char *) a; + dom_string *actual = (dom_string *) b; + dom_string *exp; + dom_exception err; + bool ret; + + err = dom_string_create(myrealloc, NULL, excepted, strlen(excepted), + &exp); + if (err != DOM_NO_ERR) + return false; + + ret = dom_string_icmp(exp, actual) == 0; + + dom_string_unref(exp); + + if (ret == true) + return 0; + else + return 1; +} + +/* Similar with str_icmp, but the param order are reverse */ +int str_icmp_r(const void *a, const void *b) +{ + return str_icmp(b, a); +} diff --git a/test/testutils/comparators.h b/test/testutils/comparators.h new file mode 100644 index 0000000..cad79e1 --- /dev/null +++ b/test/testutils/comparators.h @@ -0,0 +1,23 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 James Shaw + * Copyright 2009 Bo Yang + */ + +#ifndef comparators_h_ +#define comparators_h_ + +/** + * A function pointer type for a comparator. + */ +typedef int (*comparator)(const void* a, const void* b); + +int int_comparator(const void* a, const void* b); + +int str_icmp(const void *a, const void *b); +int str_icmp_r(const void *a, const void *b); +int str_cmp(const void *a, const void *b); +int str_cmp_r(const void *a, const void *b); +#endif diff --git a/test/testutils/domts.h b/test/testutils/domts.h new file mode 100644 index 0000000..14f4e9e --- /dev/null +++ b/test/testutils/domts.h @@ -0,0 +1,25 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#ifndef domts_h_ +#define domts_h_ + +#include +#include +#include +#include +#include +#include + +struct lwc_context_s; + +extern struct lwc_context_s *ctx; + +dom_document *load_xml(char *file, bool willBeModified); +dom_document *load_html(char *file, bool willBeModified); + +#endif diff --git a/test/testutils/domtsasserts.c b/test/testutils/domtsasserts.c new file mode 100644 index 0000000..affa881 --- /dev/null +++ b/test/testutils/domtsasserts.c @@ -0,0 +1,253 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#include +#include +#include +#include + +#include + +#include "domts.h" + +extern dom_implementation *doc_impl; + +void __assert2(const char *expr, const char *function, + const char *file, int line) +{ + UNUSED(function); + UNUSED(file); + + printf("FAIL - %s at line %d\n", expr, line); + + exit(EXIT_FAILURE); +} + +/** + * Following are the test conditions which defined in the DOMTS, please refer + * the DOM Test Suite for details + */ + +bool is_true(bool arg) +{ + return arg == true; +} + +bool is_null(void *arg) +{ + return arg == NULL; +} + +bool is_same(void *excepted, void *actual) +{ + return excepted == actual; +} + +bool is_same_int(int excepted, int actual) +{ + return excepted == actual; +} + +bool is_same_unsigned_long(unsigned long excepted, unsigned long actual) +{ + return excepted == actual; +} + +bool is_equals_int(int excepted, int actual, bool dummy) +{ + UNUSED(dummy); + + return excepted == actual; +} + +bool is_equals_unsigned_long(unsigned long excepted, unsigned long actual, bool dummy) +{ + UNUSED(dummy); + + return excepted == actual; +} + +/** + * Test whether two string are equal + * + * \param excepted The excepted string + * \param actual The actual string + * \param ignoreCase Whether to ignore letter case + */ +bool is_equals_string(const char *excepted, dom_string *actual, + bool ignoreCase) +{ + dom_string *exp; + dom_exception err; + bool ret; + + err = dom_string_create(myrealloc, NULL, excepted, strlen(excepted), + &exp); + if (err != DOM_NO_ERR) + return false; + + if (ignoreCase == true) + ret = dom_string_icmp(exp, actual) == 0; + else + ret = dom_string_cmp(exp, actual) == 0; + + dom_string_unref(exp); + return ret; +} + +/* Compare whether two dom_string are equal */ +bool is_equals_domstring(dom_string *excepted, dom_string *actual, + bool ignoreCase) +{ + if (ignoreCase == true) + return dom_string_icmp(excepted, actual) == 0; + else + return dom_string_cmp(excepted, actual) == 0; +} + +/* The param actual should always contain dom_sting and expectd should + * contain char * */ +bool is_equals_list(list *expected, list *actual, bool ignoreCase) +{ + assert((expected->type && 0xff00) == (actual->type && 0xff00)); + + comparator cmp = NULL; + comparator rcmp = NULL; + + if (expected->type == INT) + cmp = int_comparator; + if (expected->type == STRING) { + if (actual->type == DOM_STRING) { + cmp = ignoreCase? str_icmp : str_cmp; + rcmp = ignoreCase? str_icmp_r : str_cmp_r; + } + } + if (expected->type == DOM_STRING) { + if (actual->type == STRING) { + cmp = ignoreCase? str_icmp_r : str_cmp_r; + rcmp = ignoreCase? str_icmp : str_cmp; + } + } + + assert(cmp != NULL); + + bool ret = list_contains_all(expected, actual, cmp); + if (ret) + return list_contains_all(actual, expected, rcmp); +} + + + +bool is_instanceof(const char *type, dom_node *node) +{ + assert("There is no instanceOf in the test-suite" == NULL); + + return false; +} + + +bool is_size_domnamednodemap(int size, dom_namednodemap *map) +{ + unsigned long len; + dom_exception err; + + err = dom_namednodemap_get_length(map, &len); + if (err != DOM_NO_ERR) { + assert("Exception occured" == NULL); + return false; + } + + return size == len; +} + +bool is_size_domnodelist(int size, dom_nodelist *list) +{ + unsigned long len; + dom_exception err; + + err = dom_nodelist_get_length(list, &len); + if (err != DOM_NO_ERR) { + assert("Exception occured" == NULL); + return false; + } + + return size == len; +} + +bool is_size_list(int size, list *list) +{ + return size == list->size; +} + + +bool is_uri_equals(char *scheme, char *path, char *host, + char *file, char *query, char *fragment, + bool isAbsolute, dom_string *actual) +{ + UNUSED(scheme); + UNUSED(path); + UNUSED(host); + UNUSED(file); + UNUSED(query); + UNUSED(fragment); + UNUSED(isAbsolute); + UNUSED(actual); + + return false; +} + + +bool is_contenttype(const char *type) +{ + /* Now, we use the libxml2 parser for DOM parsing, so the content type + * is always "text/xml" */ + if (strcmp(type, "text/xml") == 0) + return true; + else + return false; +} + +bool has_feature(char *feature, char *version) +{ + dom_exception err; + bool ret; + dom_string *df, *dv; + + err = dom_string_create(myrealloc, NULL, feature, + feature == NULL ? 0 : strlen(feature), &df); + if (err != DOM_NO_ERR) + return false; + + err = dom_string_create(myrealloc, NULL, version, + version == NULL ? 0 : strlen(version), &dv); + if (err != DOM_NO_ERR) { + dom_string_unref(df); + return false; + } + + err = dom_implementation_has_feature(doc_impl, df, dv, &ret); + /* Here, when we come with exception, we should return false, + * TODO: this need to be improved, but I can't figure out how */ + if (err != DOM_NO_ERR) { + dom_string_unref(df); + dom_string_unref(dv); + return false; + } + + dom_string_unref(df); + dom_string_unref(dv); + return ret; +} + +bool implementation_attribute(char *name, bool value) +{ + /* We didnot support DOMConfigure for implementation now */ + UNUSED(name); + UNUSED(value); + + return true; +} diff --git a/test/testutils/domtsasserts.h b/test/testutils/domtsasserts.h new file mode 100644 index 0000000..ee0fb72 --- /dev/null +++ b/test/testutils/domtsasserts.h @@ -0,0 +1,61 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#ifndef domtsasserts_h_ +#define domtsasserts_h_ + +#include + +#include + +#include "list.h" + + +/* Redefine assert, so we can simply use the standard assert mechanism + * within testcases and exit with the right output for the testrunner + * to do the right thing. */ +void __assert2(const char *expr, const char *function, + const char *file, int line); + +#define assert(expr) \ + ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0))) + +bool is_true(bool arg); + +bool is_null(void *arg); + +bool is_same(void *excepted, void *actual); +bool is_same_int(int excepted, int actual); +bool is_same_unsigned_long(unsigned long excepted, unsigned long actual); + +bool is_equals_int(int excepted, int actual, bool dummy); +bool is_equals_unsigned_long(unsigned long excepted, unsigned long actual, bool dummy); +bool is_equals_string(const char *excepted, dom_string *actual, + bool ignoreCase); +bool is_equals_domstring(dom_string *excepted, dom_string *actual, bool ignoreCase); + +/* We may use two different string types in libDOM, but the expected string type is + always "char *" */ +bool is_equals_list(list *expected, list *actual, bool ignoreCase); + +bool is_instanceof(const char *type, dom_node *node); + +bool is_size_domnamednodemap(int size, dom_namednodemap *map); +bool is_size_domnodelist(int size, dom_nodelist *list); +bool is_size_list(int size, list *list); + +bool is_uri_equals(char *scheme, char *path, char *host, + char *file, char *query, char *fragment, + bool isAbsolute, dom_string *actual); + +bool is_contenttype(const char *type); + +bool has_feature(char *feature, char *version); + +bool implementation_attribute(char *name, bool value); + +#endif diff --git a/test/testutils/domtscondition.c b/test/testutils/domtscondition.c new file mode 100644 index 0000000..39f6c68 --- /dev/null +++ b/test/testutils/domtscondition.c @@ -0,0 +1,32 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#include + +/** + * Just simple functions which meet the needs of DOMTS conditions + */ + +bool less(int excepted, int actual) +{ + return actual < excepted; +} + +bool less_or_equals(int excepted, int actual) +{ + return actual <= excepted; +} + +bool greater(int excepted, int actual) +{ + return actual > excepted; +} + +bool greater_or_equals(int excepted, int actual) +{ + return actual >= excepted; +} diff --git a/test/testutils/domtscondition.h b/test/testutils/domtscondition.h new file mode 100644 index 0000000..164977f --- /dev/null +++ b/test/testutils/domtscondition.h @@ -0,0 +1,18 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#ifndef domtscondition_h_ +#define domtscondition_h_ + +#include + +inline bool less(int excepted, int actual); +inline bool less_or_equals(int excepted, int actual); +inline bool greater(int excepted, int actual); +inline bool greater_or_equals(int excepted, int actual); + +#endif diff --git a/test/testutils/foreach.c b/test/testutils/foreach.c new file mode 100644 index 0000000..c3868df --- /dev/null +++ b/test/testutils/foreach.c @@ -0,0 +1,96 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#include + +#include "foreach.h" +#include "list.h" + +#include + +/** + * Please see foreach.h for the usage of the following functions + */ + +void foreach_initialise_domnodelist(dom_nodelist *list, unsigned int *iterator) +{ + *iterator = 0; +} + +void foreach_initialise_list(list *list, unsigned int *iterator) +{ + *iterator = 0; +} + +void foreach_initialise_domnamednodemap(dom_namednodemap *map, unsigned int *iterator) +{ + *iterator = 0; +} + + +bool _get_next_domnodelist(dom_nodelist *list, unsigned int *iterator, dom_node **ret) +{ + dom_exception err; + unsigned long len; + + err = dom_nodelist_get_length(list, &len); + if (err != DOM_NO_ERR) + return false; + + if (*iterator >= len) + return false; + + err = dom_nodelist_item(list, (*iterator), ret); + if (err != DOM_NO_ERR) + return false; + + (*iterator)++; + return true; +} + +bool get_next_list(list *list, unsigned int *iterator, void **ret) +{ + unsigned int len = *iterator; + unsigned int i = 0; + struct list_elt *elt = list->head; + + for (; i < len; i++) { + if (elt == NULL) + return false; + elt = elt->next; + } + + if (elt == NULL) + return false; + + *ret = elt->data; + + (*iterator)++; + + return true; +} + +bool _get_next_domnamednodemap(dom_namednodemap *map, unsigned int *iterator, dom_node **ret) +{ + dom_exception err; + unsigned long len; + + err = dom_namednodemap_get_length(map, &len); + if (err != DOM_NO_ERR) + return false; + + if (*iterator >= len) + return false; + + err = dom_namednodemap_item(map, (*iterator), ret); + if (err != DOM_NO_ERR) + return false; + + (*iterator)++; + + return true; +} diff --git a/test/testutils/foreach.h b/test/testutils/foreach.h new file mode 100644 index 0000000..75874bc --- /dev/null +++ b/test/testutils/foreach.h @@ -0,0 +1,41 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#ifndef domts_foreach_h_ +#define domts_foreach_h_ + +#include + +#include + +/* The following six functions are used for the XML testcase's + element. + And the element can be converted to : + + unsigned int iterator; + foreach_initialise_*(list, &iterator); + while(get_next_*(list, &iterator, ret)){ + do the loop work. + } +*/ + +void foreach_initialise_domnodelist(dom_nodelist *list, unsigned int *iterator); +void foreach_initialise_list(list *list, unsigned int *iterator); +void foreach_initialise_domnamednodemap(dom_namednodemap *map, unsigned int *iterator); + +bool _get_next_domnodelist(dom_nodelist *list, unsigned int *iterator, dom_node **ret); +#define get_next_domnodelist(l, i, r) _get_next_domnodelist( \ + (dom_nodelist *) (l), (unsigned int *) (i), (dom_node **) (r)) + +bool get_next_list(list *list, unsigned int *iterator, void **ret); + +bool _get_next_domnamednodemap(dom_namednodemap *map, unsigned int *iterator, dom_node **ret); +#define get_next_domnamednodemap(m, i, r) _get_next_domnamednodemap( \ + (dom_namednodemap *) (m), (unsigned int *) (i), (dom_node **) (r)) + + +#endif diff --git a/test/testutils/list.c b/test/testutils/list.c new file mode 100644 index 0000000..09a8cfc --- /dev/null +++ b/test/testutils/list.c @@ -0,0 +1,171 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 James Shaw + * Copyright 2009 Bo Yang + */ + + +#include +#include +#include + +#include +#include + +#include "comparators.h" +#include "list.h" +#include "domtsasserts.h" + +/** + * Private helper function. + * Create a new list_elt and initialise it. + */ +struct list_elt* list_new_elt(void* data); + +struct list_elt* list_new_elt(void* data) { + struct list_elt* elt = malloc(sizeof(struct list_elt)); + assert(elt != NULL); + elt->data = data; + elt->next = NULL; + return elt; +} + +struct list* list_new(TYPE type) +{ + struct list* list = malloc(sizeof(struct list)); + assert(list != NULL); + list->size = 0; + list->type = type; + list->head = NULL; + list->tail = NULL; + return list; +} + +void list_destroy(struct list* list) +{ + struct list_elt* elt = list->head; + while (elt != NULL) { + if (list->type == DOM_STRING) + dom_string_unref((dom_string *) elt->data); + if (list->type == NODE) + dom_node_unref(elt->data); + struct list_elt* nextElt = elt->next; + free(elt); + elt = nextElt; + } + free(list); +} + +void list_add(struct list* list, void* data) +{ + struct list_elt* elt = list_new_elt(data); + struct list_elt* tail = list->tail; + + /* if tail was set, make its 'next' ptr point to elt */ + if (tail != NULL) { + tail->next = elt; + } + + /* make elt the new tail */ + list->tail = elt; + + if (list->head == NULL) { + list->head = elt; + } + + /* inc the size of the list */ + list->size++; + if (list->type == DOM_STRING) + dom_string_ref((dom_string *) data); + if (list->type == NODE) + dom_node_ref(data); +} + +bool list_remove(struct list* list, void* data) +{ + struct list_elt* prevElt = NULL; + struct list_elt* elt = list->head; + + bool found = false; + + while (elt != NULL) { + struct list_elt* nextElt = elt->next; + + /* if data is identical, fix up pointers, and free the element */ + if (data == elt->data) { + if (prevElt == NULL) { + list->head = nextElt; + } else { + prevElt->next = nextElt; + } + free(elt); + list->size--; + found = true; + break; + } + + prevElt = elt; + elt = nextElt; + } + + return found; +} + +struct list* list_clone(struct list* list) +{ + struct list* newList = list_new(list->type); + struct list_elt* elt = list->head; + + while (elt != NULL) { + list_add(newList, elt->data); + elt = elt->next; + } + + return newList; +} + +bool list_contains(struct list* list, void* data, comparator comparator) +{ + struct list_elt* elt = list->head; + while (elt != NULL) { + if (comparator(elt->data, data) == 0) { + return true; + } + elt = elt->next; + } + return false; +} + +bool list_contains_all(struct list* superList, struct list* subList, + comparator comparator) +{ + struct list_elt* subElt = subList->head; + struct list* superListClone = list_clone(superList); + + bool found = true; + + while (subElt != NULL) { + struct list_elt* superElt = superListClone->head; + + found = false; + while (superElt != NULL && found == false) { + if (comparator(superElt->data, subElt->data) == 0) { + found = true; + list_remove(superListClone, superElt->data); + break; + } + superElt = superElt->next; + } + + if (found == false) + break; + subElt = subElt->next; + } + + free(superListClone); + + return found; +} + diff --git a/test/testutils/list.h b/test/testutils/list.h new file mode 100644 index 0000000..6b0f209 --- /dev/null +++ b/test/testutils/list.h @@ -0,0 +1,70 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 James Shaw + */ + +#ifndef list_h_ +#define list_h_ + +#include + +#include "comparators.h" + +/* The element type in the list + * + * The high byte is used for category type + * The low byte is used for concrete type + */ +typedef enum TYPE { + INT = 0x0001, + STRING = 0x0100, + DOM_STRING = 0x0101, + NODE = 0x0200 +} TYPE; + + +struct list_elt { + void* data; + struct list_elt* next; +}; + +typedef struct list { + unsigned int size; + TYPE type; + struct list_elt* head; + struct list_elt* tail; +} list; + +struct list* list_new(TYPE type); +void list_destroy(struct list* list); + +/** + * Add data to the tail of the list. + */ +void list_add(struct list* list, void* data); + +/** + * Remove element containing data from list. + * The list element is freed, but the caller must free the data itself + * if necessary. + * + * Returns true if data was found in the list. + */ +bool list_remove(struct list* list, void* data); + +struct list* list_clone(struct list* list); +/** + * Tests if data is equal to any element in the list. + */ +bool list_contains(struct list* list, void* data, + comparator comparator); + +/** + * Tests if superlist contains all elements in sublist. Order is not important. + */ +bool list_contains_all(struct list* superList, struct list* subList, + comparator comparator); + +#endif diff --git a/test/testutils/load.c b/test/testutils/load.c new file mode 100644 index 0000000..74cc009 --- /dev/null +++ b/test/testutils/load.c @@ -0,0 +1,151 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include + +// For parsers +#include +#include +#include +#include + +#include "utils.h" + +extern lwc_context *ctx; + +/** + * Load the file as it is a XML file + * + * \param file The file path + * \param willBeModified Whether this file will be modified, not used + */ +dom_document *load_xml(char *file, bool willBeModified) +{ + dom_xml_parser *parser = NULL; + int handle; + int readed; + dom_xml_error error; + dom_document *ret; + char buffer[1024]; + + UNUSED(willBeModified); + + parser = dom_xml_parser_create(NULL, NULL, + myrealloc, NULL, mymsg, NULL, ctx); + if (parser == NULL) { + fprintf(stderr, "Can't create XMLParser\n"); + return NULL; + } + + handle = open(file, O_RDONLY); + if (handle == -1) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Can't open test input file: %s\n", file); + return NULL; + } + + readed = read(handle, buffer, 1024); + error = dom_xml_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + + while(readed == 1024) { + readed = read(handle, buffer, 1024); + error = dom_xml_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + } + + error = dom_xml_parser_completed(parser); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing error when construct DOM\n"); + return NULL; + } + + ret = dom_xml_parser_get_document(parser); + dom_xml_parser_destroy(parser); + + return ret; +} + +/** + * Load the file as it is a HTML file + * + * \param file The file path + * \param willBeModified Whether this file will be modified, not used + */ +dom_document *load_html(char *file, bool willBeModified) +{ + dom_hubbub_parser *parser = NULL; + int handle; + int readed; + dom_hubbub_error error; + dom_document *ret; + char buffer[1024]; + + UNUSED(willBeModified); + + parser = dom_hubbub_parser_create("data/Aliases", NULL, true, + myrealloc, NULL, mymsg, NULL, ctx); + if (parser == NULL) { + fprintf(stderr, "Can't create XMLParser\n"); + return NULL; + } + + handle = open(file, O_RDONLY); + if (handle == -1) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Can't open test input file: %s\n", file); + return NULL; + } + + readed = read(handle, buffer, 1024); + error = dom_hubbub_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + + while(readed == 1024) { + readed = read(handle, buffer, 1024); + error = dom_hubbub_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + } + + error = dom_hubbub_parser_completed(parser); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing error when construct DOM\n"); + return NULL; + } + + ret = dom_hubbub_parser_get_document(parser); + dom_hubbub_parser_destroy(parser); + + return ret; +} diff --git a/test/testutils/utils.c b/test/testutils/utils.c new file mode 100644 index 0000000..739933f --- /dev/null +++ b/test/testutils/utils.c @@ -0,0 +1,34 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#include +#include +#include + +#include "utils.h" + +void *myrealloc(void *ptr, size_t len, void *pw) +{ + UNUSED(pw); + + return realloc(ptr, len); +} + +void mymsg(uint32_t severity, void *ctx, const char *msg, ...) +{ + va_list l; + + UNUSED(ctx); + + va_start(l, msg); + + fprintf(stderr, "%d: ", severity); + vfprintf(stderr, msg, l); + fprintf(stderr, "\n"); +} + + diff --git a/test/testutils/utils.h b/test/testutils/utils.h new file mode 100644 index 0000000..b57db36 --- /dev/null +++ b/test/testutils/utils.h @@ -0,0 +1,35 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#ifndef utils_h_ +#define utils_h_ + +#include +#include + +#ifndef max +#define max(a,b) ((a)>(b)?(a):(b)) +#endif + +#ifndef min +#define min(a,b) ((a)<(b)?(a):(b)) +#endif + +#ifndef SLEN +/* Calculate length of a string constant */ +#define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */ +#endif + +#ifndef UNUSED +#define UNUSED(x) ((x) = (x)) +#endif + +void *myrealloc(void *ptr, size_t len, void *pw); +void mymsg(uint32_t severity, void *ctx, const char *msg, ...); + +#endif + -- cgit v1.2.3