From f1f80696ebe87520de0c857371e0ca109fce68cb Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sat, 22 Sep 2007 13:32:42 +0000 Subject: Create a library of utility functions for the testsuite to use Make test/binding.c include stdio.h itself rather than relying on other things to include it. svn path=/trunk/dom/; revision=3568 --- test/lib/Makefile | 69 ++++++++++++++++++++++++++++++ test/lib/exceptions.h | 34 +++++++++++++++ test/lib/list.c | 85 ++++++++++++++++++++++++++++++++++++ test/lib/list.h | 44 +++++++++++++++++++ test/lib/testassert.c | 25 +++++++++++ test/lib/testassert.h | 22 ++++++++++ test/lib/testobject.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/lib/testobject.h | 22 ++++++++++ test/lib/utils.c | 34 +++++++++++++++ test/lib/utils.h | 35 +++++++++++++++ 10 files changed, 486 insertions(+) create mode 100644 test/lib/Makefile create mode 100644 test/lib/exceptions.h create mode 100644 test/lib/list.c create mode 100644 test/lib/list.h create mode 100644 test/lib/testassert.c create mode 100644 test/lib/testassert.h create mode 100644 test/lib/testobject.c create mode 100644 test/lib/testobject.h create mode 100644 test/lib/utils.c create mode 100644 test/lib/utils.h (limited to 'test/lib') diff --git a/test/lib/Makefile b/test/lib/Makefile new file mode 100644 index 0000000..22a06e9 --- /dev/null +++ b/test/lib/Makefile @@ -0,0 +1,69 @@ +# Makefile for DOM testcase utility library +# +# Toolchain is exported by top-level makefile +# +# Top-level makefile also exports the following variables: +# +# COMPONENT Name of component +# EXPORT Absolute path of export directory +# TOP Absolute path of source tree root +# +# The top-level makefile requires the following targets to exist: +# +# clean Clean source tree +# debug Create a debug binary +# distclean Fully clean source tree, back to pristine condition +# export Export distributable components to ${EXPORT} +# release Create a release binary +# setup Perform any setup required prior to compilation +# test Execute any test cases + +# Manipulate include paths +CFLAGS += -I$(TOP) -I$(CURDIR) + +# Release output +RELEASE = libdebug.a + +# Debug output +DEBUG = libdebug-debug.a + +# Objects +OBJS = list testassert testobject utils + +.PHONY: clean debug export release setup test + +# Targets +release: $(addprefix Release/, $(addsuffix .o, $(OBJS))) + @${AR} ${ARFLAGS} $(RELEASE) Release/* + +debug: $(addprefix Debug/, $(addsuffix .o, $(OBJS))) + @${AR} ${ARFLAGS} $(DEBUG) Debug/* + +clean: +ifneq (${OBJS}, ) + -@${RM} ${RMFLAGS} $(addprefix Release/, $(addsuffix .o, ${OBJS})) + -@${RM} ${RMFLAGS} $(addprefix Debug/, $(addsuffix .o, ${OBJS})) +endif + -@${RM} ${RMFLAGS} $(RELEASE) $(DEBUG) + +distclean: + -@${RM} ${RMFLAGS} -r Release + -@${RM} ${RMFLAGS} -r Debug + +setup: + @${MKDIR} ${MKDIRFLAGS} Release + @${MKDIR} ${MKDIRFLAGS} Debug + +export: + +test: + +# Pattern rules +Release/%.o: %.c + @${ECHO} ${ECHOFLAGS} "==> $<" + @${CC} -c ${CFLAGS} -DNDEBUG -o $@ $< + +Debug/%.o: %.c + @${ECHO} ${ECHOFLAGS} "==> $<" + @${CC} -c -g ${CFLAGS} -o $@ $< + diff --git a/test/lib/exceptions.h b/test/lib/exceptions.h new file mode 100644 index 0000000..1c6b8e2 --- /dev/null +++ b/test/lib/exceptions.h @@ -0,0 +1,34 @@ +#ifndef exceptions_h_ +#define exceptions_h_ + +#include + +#include + +/* 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 new file mode 100644 index 0000000..0b2965f --- /dev/null +++ b/test/lib/list.c @@ -0,0 +1,85 @@ +/* + * 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 + */ + + +#include +#include +#include + +#include "list.h" +#include "testassert.h" + +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 = malloc(sizeof(struct list_elt)); + assert(elt != NULL); + elt->data = data; + elt->next = NULL; + 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_contains(struct list* list, void* data, list_compare_func 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, + list_compare_func comparator) +{ + struct list_elt* elt = subList->head; + while (elt != NULL) { + if (!list_contains(superList, elt->data, comparator)) { + return false; + } + elt = elt->next; + } + return true; +} + diff --git a/test/lib/list.h b/test/lib/list.h new file mode 100644 index 0000000..a0a6c6a --- /dev/null +++ b/test/lib/list.h @@ -0,0 +1,44 @@ +/* + * 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_ + +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); + +typedef int (*list_compare_func)(const void* a, const void* b); + +/** + * Add data to the tail of the list. + */ +void list_add(struct list* list, void* data); + +/** + * Tests if data is equal to any element in the list. + */ +bool list_contains(struct list* list, void* data, + int (*comparator)(const void* a, const void* b)); + +/** + * Tests if superlist contains all elements in sublist. Order is not important. + */ +bool list_contains_all(struct list* superList, struct list* subList, + list_compare_func comparator); + +#endif diff --git a/test/lib/testassert.c b/test/lib/testassert.c new file mode 100644 index 0000000..2a31ff3 --- /dev/null +++ b/test/lib/testassert.c @@ -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 2007 John-Mark Bell + */ + +#include +#include + +#include "testassert.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); +} + + diff --git a/test/lib/testassert.h b/test/lib/testassert.h new file mode 100644 index 0000000..329feff --- /dev/null +++ b/test/lib/testassert.h @@ -0,0 +1,22 @@ +/* + * 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 testassert_h_ +#define testassert_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))) + + +#endif + diff --git a/test/lib/testobject.c b/test/lib/testobject.c new file mode 100644 index 0000000..e9d3d06 --- /dev/null +++ b/test/lib/testobject.c @@ -0,0 +1,116 @@ +/* + * 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 "bindings/xml/xmlbinding.h" +#include "bindings/xml/xmlparser.h" + +#include "testassert.h" +#include "testobject.h" +#include "utils.h" + +struct TestObject { + xml_parser *parser; + struct dom_document *doc; +}; + +TestObject *test_object_create(int argc, char **argv, + const char *uri, bool will_be_modified) +{ + static bool xml_parser_initialised; + + char fnbuf[1024]; +#define CHUNK_SIZE 4096 + uint8_t buf[CHUNK_SIZE]; + FILE *fp; + size_t len; + TestObject *ret; + + UNUSED(will_be_modified); + + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + if (xml_parser_initialised == false) { + assert(xml_dom_binding_initialise(myrealloc, NULL) == XML_OK); + + xml_parser_initialised = true; + } + + snprintf(fnbuf, sizeof fnbuf, "%s/%s", argv[1], uri); + + ret = malloc(sizeof(TestObject)); + if (ret == NULL) + return NULL; + + ret->parser = xml_parser_create(NULL, "UTF-8", myrealloc, NULL, + mymsg, NULL); + if (ret->parser == NULL) { + free(ret); + return NULL; + } + + fp = fopen(fnbuf, "r"); + if (fp == NULL) { + xml_parser_destroy(ret->parser); + 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); + + assert(xml_parser_parse_chunk(ret->parser, buf, + CHUNK_SIZE) == XML_OK); + + len -= CHUNK_SIZE; + } + + if (len > 0) { + fread(buf, 1, len, fp); + + assert(xml_parser_parse_chunk(ret->parser, buf, + len) == XML_OK); + + len = 0; + } + + assert(xml_parser_completed(ret->parser) == XML_OK); + + fclose(fp); + + ret->doc = xml_parser_get_document(ret->parser); + + xml_parser_destroy(ret->parser); + ret->parser = NULL; + + 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) +{ + UNUSED(obj); + + return "text/xml"; +} + + diff --git a/test/lib/testobject.h b/test/lib/testobject.h new file mode 100644 index 0000000..c72b39f --- /dev/null +++ b/test/lib/testobject.h @@ -0,0 +1,22 @@ +/* + * 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 testobject_h_ +#define testobject_h_ + +#include + +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 new file mode 100644 index 0000000..739933f --- /dev/null +++ b/test/lib/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/lib/utils.h b/test/lib/utils.h new file mode 100644 index 0000000..b57db36 --- /dev/null +++ b/test/lib/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