diff options
Diffstat (limited to 'test/lib')
-rw-r--r-- | test/lib/comparators.c | 5 | ||||
-rw-r--r-- | test/lib/comparators.h | 18 | ||||
-rw-r--r-- | test/lib/exceptions.h | 42 | ||||
-rw-r--r-- | test/lib/list.c | 154 | ||||
-rw-r--r-- | test/lib/list.h | 57 | ||||
-rw-r--r-- | test/lib/testassert.c | 64 | ||||
-rw-r--r-- | test/lib/testassert.h | 34 | ||||
-rw-r--r-- | test/lib/testobject.c | 233 | ||||
-rw-r--r-- | test/lib/testobject.h | 22 | ||||
-rw-r--r-- | test/lib/utils.c | 34 | ||||
-rw-r--r-- | test/lib/utils.h | 35 |
11 files changed, 0 insertions, 698 deletions
diff --git a/test/lib/comparators.c b/test/lib/comparators.c deleted file mode 100644 index c07236d..0000000 --- a/test/lib/comparators.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "comparators.h" - -int int_comparator(const int* a, const int* b) { - return (*a) - (*b); -} diff --git a/test/lib/comparators.h b/test/lib/comparators.h deleted file mode 100644 index 4cdeb97..0000000 --- a/test/lib/comparators.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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 <jshaw@netsurf-browser.org> - */ - -#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 int* a, const int* b); - -#endif diff --git a/test/lib/exceptions.h b/test/lib/exceptions.h deleted file mode 100644 index 8b98d5b..0000000 --- a/test/lib/exceptions.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 <jshaw@netsurf-browser.org> - */ - -#ifndef exceptions_h_ -#define exceptions_h_ - -#include <setjmp.h> - -#include <dom/core/exceptions.h> - -/* Adapted from http://www.math.princeton.edu/~asnowden/c-except.html - Usage: - TRY - THROW(DOM_NOT_FOUND_ERR); - THROW_IF_ERR(dom_document_get_doctype(...)); - CATCH(ex) - printf("exception: %d\n", ex); - ENDTRY -*/ -#define TRY __exvalue=setjmp(__exbuf); \ - if (__exvalue==0) { -#define CATCH(x) } else { \ - int x = __exvalue; -#define ENDTRY } -#define THROW(x) longjmp(__exbuf, x) - -#define THROW_IF_ERR(x) \ - do { \ - int err = x; \ - if (err != DOM_NO_ERR) \ - THROW(err); \ - } while (0) - -jmp_buf __exbuf; -int __exvalue; - -#endif - diff --git a/test/lib/list.c b/test/lib/list.c deleted file mode 100644 index e72b50c..0000000 --- a/test/lib/list.c +++ /dev/null @@ -1,154 +0,0 @@ -/* - * 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 <jshaw@netsurf-browser.org> - */ - - -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> - -#include "comparators.h" -#include "list.h" -#include "testassert.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(void) -{ - struct list* list = malloc(sizeof(struct list)); - assert(list != NULL); - list->size = 0; - list->head = NULL; - list->tail = NULL; - return list; -} - -void list_destroy(struct list* list) -{ - struct list_elt* elt = list->head; - while (elt != NULL) { - 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++; -} - -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(); - 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(subElt->data, superElt->data) == 0) { - found = true; - list_remove(superListClone, superElt->data); - } - superElt = superElt->next; - } - - subElt = subElt->next; - } - free(superListClone); - - return found; -} - diff --git a/test/lib/list.h b/test/lib/list.h deleted file mode 100644 index 7c27796..0000000 --- a/test/lib/list.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 <jshaw@netsurf-browser.org> - */ - -#ifndef list_h_ -#define list_h_ - -#include <stdbool.h> - -#include "comparators.h" - -struct list_elt { - void* data; - struct list_elt* next; -}; - -struct list { - unsigned int size; - struct list_elt* head; - struct list_elt* tail; -}; - -struct list* list_new(void); -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/lib/testassert.c b/test/lib/testassert.c deleted file mode 100644 index 191a94b..0000000 --- a/test/lib/testassert.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - * Copyright 2007 James Shaw <jshaw@netsurf-browser.org> - */ - -#include <stdio.h> -#include <stdlib.h> - -#include <dom/core/string.h> - -#include "testassert.h" -#include "comparators.h" -#include "utils.h" - -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); -} - -void assert_equals_collection(struct list* expected, struct list* actual, - comparator comparator) -{ - assert(expected != NULL); - assert(actual != NULL); - assert_equals(&(expected->size), &(actual->size), (int (*)(const void* a, const void* b)) int_comparator); - list_contains_all(actual, expected, comparator); -} - -void assert_equals(void* expected, void* actual, comparator comparator) -{ - assert(comparator(expected, actual) == 0); -} - -void assert_same(void* expected, void* actual, comparator comparator) -{ - if (!(expected == actual)) { - assert_equals(expected, actual, comparator); - } -} -void assert_uri_equals(char* scheme, char* path, char* host, char* file, - char* name, char* query, char* fragment, bool isAbsolute, - char* actual) -{ - UNUSED(scheme); - UNUSED(path); - UNUSED(host); - UNUSED(file); - UNUSED(name); - UNUSED(query); - UNUSED(fragment); - UNUSED(isAbsolute); - UNUSED(actual); - /* TODO: implement me. Look at netsurf/url.c */ -} - diff --git a/test/lib/testassert.h b/test/lib/testassert.h deleted file mode 100644 index 85472ce..0000000 --- a/test/lib/testassert.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - */ - -#ifndef testassert_h_ -#define testassert_h_ - -#include "comparators.h" -#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))) - -void assert_equals_collection(struct list* expected, struct list* actual, - comparator comparator); - -void assert_equals(void* expected, void* actual, comparator comparator); - -void assert_same(void* expected, void* actual, comparator comparator); - -void assert_uri_equals(char* scheme, char* path, char* host, char* file, - char* name, char* query, char* fragment, bool isAbsolute, - char* actual); - -#endif diff --git a/test/lib/testobject.c b/test/lib/testobject.c deleted file mode 100644 index 4c27e0b..0000000 --- a/test/lib/testobject.c +++ /dev/null @@ -1,233 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - */ - -#include <ctype.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <dom/bootstrap/init_fini.h> - -#include "bindings/hubbub/parser.h" - -#include "bindings/xml/xmlbinding.h" -#include "bindings/xml/xmlparser.h" - -#include "testassert.h" -#include "testobject.h" -#include "utils.h" - -static bool parser_initialised; - -struct TestObject { - enum { OBJECT_XML, OBJECT_HTML } type; - union { - dom_xml_parser *xml; - dom_hubbub_parser *html; - } parser; - struct dom_document *doc; -}; - -static void test_object_cleanup(void); - -TestObject *test_object_create(int argc, char **argv, - const char *uri, bool will_be_modified) -{ - char fnbuf[1024]; -#define CHUNK_SIZE 4096 - uint8_t buf[CHUNK_SIZE]; - FILE *fp; - char *dot; - size_t len; - TestObject *ret; - - UNUSED(will_be_modified); - - if (argc != 2) { - printf("Usage: %s <datapath>\n", argv[0]); - exit(EXIT_FAILURE); - } - - if (parser_initialised == false) { - assert(dom_initialise(myrealloc, NULL) == DOM_NO_ERR); - - assert(dom_xml_binding_initialise(myrealloc, NULL) == - DOM_XML_OK); - -// assert(dom_hubbub_binding_initialise(myrealloc, NULL) == -// DOM_HUBBUB_OK); - - atexit(test_object_cleanup); - - parser_initialised = true; - } - - snprintf(fnbuf, sizeof fnbuf, "%s/%s", argv[1], uri); - - ret = malloc(sizeof(TestObject)); - if (ret == NULL) - return NULL; - - /* Detect the parser type (this is mildly hacky) */ - dot = strrchr(uri, '.'); - len = strlen(uri); - - if (dot == NULL) { - printf("No file extension, assuming XML\n"); - - ret->type = OBJECT_XML; - } else if (len - ((dot + 1) - uri) == 3) { - if (tolower(dot[1]) == 'x' && tolower(dot[2]) == 'm' - && tolower(dot[3]) == 'l') { - ret->type = OBJECT_XML; - } else if (tolower(dot[1]) == 'h' && tolower(dot[2]) == 't' && - tolower(dot[3]) == 'm') { - ret->type = OBJECT_HTML; - } - } else if (len - ((dot + 1) - uri) == 4) { - if (tolower(dot[1]) == 'h' && tolower(dot[2]) == 't' && - tolower(dot[3]) == 'm' && - tolower(dot[4]) == 'l') { - ret->type = OBJECT_HTML; - } - } else { - /* Assume XML */ - ret->type = OBJECT_XML; - } - - switch (ret->type) { - case OBJECT_XML: - ret->parser.xml = dom_xml_parser_create(NULL, "UTF-8", - myrealloc, NULL, mymsg, NULL); - if (ret->parser.xml == NULL) { - free(ret); - return NULL; - } - break; - case OBJECT_HTML: - { - char abuf[1024]; - snprintf(abuf, sizeof abuf, "%s/Aliases", argv[1]); - - ret->parser.html = dom_hubbub_parser_create(abuf, - NULL, true, myrealloc, NULL, mymsg, NULL); - if (ret->parser.html == NULL) { - free(ret); - return NULL; - } - break; - } - } - - fp = fopen(fnbuf, "r"); - if (fp == NULL) { - switch (ret->type) { - case OBJECT_XML: - dom_xml_parser_destroy(ret->parser.xml); - break; - case OBJECT_HTML: - dom_hubbub_parser_destroy(ret->parser.html); - break; - } - free(ret); - return NULL; - } - - fseek(fp, 0, SEEK_END); - len = ftell(fp); - fseek(fp, 0, SEEK_SET); - - while (len > CHUNK_SIZE) { - fread(buf, 1, CHUNK_SIZE, fp); - - switch (ret->type) { - case OBJECT_XML: - assert(dom_xml_parser_parse_chunk(ret->parser.xml, - buf, CHUNK_SIZE) == DOM_XML_OK); - break; - case OBJECT_HTML: - assert(dom_hubbub_parser_parse_chunk(ret->parser.html, - buf, CHUNK_SIZE) == DOM_HUBBUB_OK); - break; - } - - len -= CHUNK_SIZE; - } - - if (len > 0) { - fread(buf, 1, len, fp); - - switch (ret->type) { - case OBJECT_XML: - assert(dom_xml_parser_parse_chunk(ret->parser.xml, - buf, len) == DOM_XML_OK); - break; - case OBJECT_HTML: - assert(dom_hubbub_parser_parse_chunk(ret->parser.html, - buf, len) == DOM_HUBBUB_OK); - break; - } - - len = 0; - } - - switch (ret->type) { - case OBJECT_XML: - assert(dom_xml_parser_completed(ret->parser.xml) == DOM_XML_OK); - break; - case OBJECT_HTML: - assert(dom_hubbub_parser_completed(ret->parser.html) == - DOM_HUBBUB_OK); - break; - } - - fclose(fp); - - switch (ret->type) { - case OBJECT_XML: - ret->doc = dom_xml_parser_get_document(ret->parser.xml); - break; - case OBJECT_HTML: - ret->doc = dom_hubbub_parser_get_document(ret->parser.html); - break; - } - - switch (ret->type) { - case OBJECT_XML: - dom_xml_parser_destroy(ret->parser.xml); - ret->parser.xml = NULL; - break; - case OBJECT_HTML: - dom_hubbub_parser_destroy(ret->parser.html); - ret->parser.html = NULL; - break; - } - - return ret; - -#undef CHUNK_SIZE -} - -struct dom_document *test_object_get_doc(TestObject *obj) -{ - return obj->doc; -} - -const char *test_object_get_mimetype(TestObject *obj) -{ - return (obj->type == OBJECT_XML ? "text/xml" : "text/html"); -} - -void test_object_cleanup(void) -{ - if (parser_initialised) { -// dom_hubbub_binding_finalise(); - dom_xml_binding_finalise(); - dom_finalise(); - } -} - diff --git a/test/lib/testobject.h b/test/lib/testobject.h deleted file mode 100644 index c72b39f..0000000 --- a/test/lib/testobject.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - */ - -#ifndef testobject_h_ -#define testobject_h_ - -#include <stdbool.h> - -struct TestObject; -typedef struct TestObject TestObject; - -TestObject *test_object_create(int argc, char **argv, - const char *uri, bool will_be_modified); -struct dom_document *test_object_get_doc(TestObject *obj); -const char *test_object_get_mimetype(TestObject *obj); - -#endif - diff --git a/test/lib/utils.c b/test/lib/utils.c deleted file mode 100644 index 739933f..0000000 --- a/test/lib/utils.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - */ - -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> - -#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/lib/utils.h b/test/lib/utils.h deleted file mode 100644 index b57db36..0000000 --- a/test/lib/utils.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 <jmb@netsurf-browser.org> - */ - -#ifndef utils_h_ -#define utils_h_ - -#include <stddef.h> -#include <inttypes.h> - -#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 - |