added test framework and foreach macro

Sat, 18 Feb 2012 15:50:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 18 Feb 2012 15:50:43 +0100
changeset 26
59f147baea31
parent 25
3192553c0df1
child 27
22644e2572bc

added test framework and foreach macro

test/main.c file | annotate | diff | comparison | revisions
ucx/Makefile file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
ucx/test.c file | annotate | diff | comparison | revisions
ucx/test.h file | annotate | diff | comparison | revisions
ucx/ucx.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/main.c	Thu Feb 09 10:40:19 2012 +0100
     1.2 +++ b/test/main.c	Sat Feb 18 15:50:43 2012 +0100
     1.3 @@ -29,30 +29,55 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  
     1.7 +#include "ucx/test.h"
     1.8 +
     1.9  #include "list_tests.h"
    1.10  #include "mpool_tests.h"
    1.11  #include "map_tests.h"
    1.12  
    1.13 +UCX_TEST_BEGIN(testTestSuitePositive) {
    1.14 +    UCX_TEST_ASSERT(2*2 == 4, "the test framework fails")
    1.15 +    UCX_TEST_END
    1.16 +}
    1.17 +
    1.18 +UCX_TEST_BEGIN(testTestSuiteNegative) {
    1.19 +    UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works")
    1.20 +    UCX_TEST_END
    1.21 +}
    1.22 +
    1.23  int main(int argc, char **argv) {
    1.24      printf("UCX Tests\n---------\n");
    1.25  
    1.26 -    printf("\nUcxDlist Tests\n");
    1.27 -    if(dlist_tests()) {
    1.28 -        fprintf(stderr, "dlist_tests failed\n");
    1.29 +    printf("\nUcxTestSuite Tests\n");
    1.30 +    UcxTestSuite* suite = ucx_test_suite_new();
    1.31 +    ucx_test_register(suite, testTestSuitePositive);
    1.32 +    ucx_test_register(suite, testTestSuiteNegative);
    1.33 +    ucx_test_run(suite, stdout);
    1.34 +    if (suite->failure == 1 && suite->success == 1) {
    1.35 +        ucx_test_suite_free(suite);
    1.36 +    
    1.37 +        /* TODO: replace these tests with "real" tests */
    1.38 +        printf("\nUcxDlist Tests\n");
    1.39 +        if(dlist_tests()) {
    1.40 +            fprintf(stderr, "dlist_tests failed\n");
    1.41 +        }
    1.42 +
    1.43 +        printf("\nUcxList Tests\n   Assumed to be correct\n");
    1.44 +
    1.45 +        printf("\nUcxMemPool Tests\n");
    1.46 +        if(mpool_tests()) {
    1.47 +            fprintf(stderr, "mpool_tests failed\n");
    1.48 +        }
    1.49 +
    1.50 +        printf("\nUcxMap Tests\n");
    1.51 +        if(map_tests()) {
    1.52 +            fprintf(stderr, "map_tests failed\n");
    1.53 +        }
    1.54 +        
    1.55 +        return EXIT_SUCCESS;
    1.56 +    } else {
    1.57 +        ucx_test_suite_free(suite);
    1.58 +        return EXIT_FAILURE;
    1.59      }
    1.60 -
    1.61 -    printf("\nUcxList Tests\n   Assumed to be correct\n");
    1.62 -
    1.63 -    printf("\nUcxMemPool Tests\n");
    1.64 -    if(mpool_tests()) {
    1.65 -        fprintf(stderr, "mpool_tests failed\n");
    1.66 -    }
    1.67 -
    1.68 -    printf("\nUcxMap Tests\n");
    1.69 -    if(map_tests()) {
    1.70 -        fprintf(stderr, "map_tests failed\n");
    1.71 -    }
    1.72 -    
    1.73 -    return EXIT_SUCCESS; 
    1.74  }
    1.75  
     2.1 --- a/ucx/Makefile	Thu Feb 09 10:40:19 2012 +0100
     2.2 +++ b/ucx/Makefile	Sat Feb 18 15:50:43 2012 +0100
     2.3 @@ -29,7 +29,7 @@
     2.4  include ../$(CONF).mk
     2.5  
     2.6  # list of source files
     2.7 -SRC = list.c dlist.c map.c mempool.c string.c
     2.8 +SRC = list.c dlist.c map.c mempool.c string.c test.c
     2.9  
    2.10  OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
    2.11  
     3.1 --- a/ucx/list.h	Thu Feb 09 10:40:19 2012 +0100
     3.2 +++ b/ucx/list.h	Sat Feb 18 15:50:43 2012 +0100
     3.3 @@ -11,7 +11,7 @@
     3.4  #ifdef	__cplusplus
     3.5  extern "C" {
     3.6  #endif
     3.7 -
     3.8 +    
     3.9  typedef struct UcxList UcxList;
    3.10  struct UcxList {
    3.11      void    *data;
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/ucx/test.c	Sat Feb 18 15:50:43 2012 +0100
     4.3 @@ -0,0 +1,39 @@
     4.4 +/* 
     4.5 + * File:   test.c
     4.6 + * Author: Mike
     4.7 + *
     4.8 + * Created on 18. Februar 2012, 14:15
     4.9 + */
    4.10 +
    4.11 +#include "test.h"
    4.12 +
    4.13 +UcxTestSuite* ucx_test_suite_new() {
    4.14 +    UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite));
    4.15 +    if (suite != NULL) {
    4.16 +        suite->success = 0;
    4.17 +        suite->failure = 0;
    4.18 +        suite->tests = NULL;
    4.19 +    }
    4.20 +    return suite;
    4.21 +}
    4.22 +
    4.23 +void ucx_test_suite_free(UcxTestSuite* suite) {
    4.24 +    ucx_list_free(suite->tests);
    4.25 +    free(suite);
    4.26 +}
    4.27 +
    4.28 +void ucx_test_register(UcxTestSuite* suite, UcxTest test) {
    4.29 +    suite->tests = ucx_list_append(suite->tests, (void*) test);
    4.30 +}
    4.31 +
    4.32 +void ucx_test_run(UcxTestSuite* suite, FILE* output) {
    4.33 +    suite->success = 0;
    4.34 +    suite->failure = 0;
    4.35 +    UCX_FOREACH (UcxList*, suite->tests, e) {
    4.36 +        UcxTest test = (UcxTest) (e->data);
    4.37 +        test(suite, output);
    4.38 +    }
    4.39 +    fwrite("\nAll test completed.\n", 1, 21, output);
    4.40 +    fprintf(output, "  Total:   %d\n  Success: %d\n  Failure: %d\n",
    4.41 +            suite->success+suite->failure, suite->success, suite->failure);
    4.42 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/ucx/test.h	Sat Feb 18 15:50:43 2012 +0100
     5.3 @@ -0,0 +1,49 @@
     5.4 +/* 
     5.5 + * File:   test.h
     5.6 + * Author: Mike
     5.7 + *
     5.8 + * Created on 18. Februar 2012, 14:15
     5.9 + */
    5.10 +
    5.11 +#ifndef TEST_H
    5.12 +#define	TEST_H
    5.13 +
    5.14 +#include <stdio.h>
    5.15 +#include <string.h>
    5.16 +#include "list.h"
    5.17 +
    5.18 +#ifdef	__cplusplus
    5.19 +extern "C" {
    5.20 +#endif
    5.21 +
    5.22 +typedef struct {
    5.23 +    unsigned int success;
    5.24 +    unsigned int failure;
    5.25 +    UcxList *tests;
    5.26 +} UcxTestSuite;
    5.27 +
    5.28 +typedef void(*UcxTest)(UcxTestSuite*,FILE*);
    5.29 +
    5.30 +UcxTestSuite* ucx_test_suite_new();
    5.31 +void ucx_test_suite_free(UcxTestSuite*);
    5.32 +
    5.33 +void ucx_test_register(UcxTestSuite*, UcxTest);
    5.34 +void ucx_test_run(UcxTestSuite*, FILE*);
    5.35 +
    5.36 +#define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\
    5.37 +    fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_);
    5.38 +
    5.39 +#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
    5.40 +        fwrite(message".\n", 1, 2+strlen(message), _output_); \
    5.41 +        _suite_->failure++; \
    5.42 +        return;\
    5.43 +    }
    5.44 +
    5.45 +#define UCX_TEST_END } fwrite("success.\n", 1, 9, _output_); _suite_->success++;
    5.46 +
    5.47 +#ifdef	__cplusplus
    5.48 +}
    5.49 +#endif
    5.50 +
    5.51 +#endif	/* TEST_H */
    5.52 +
     6.1 --- a/ucx/ucx.h	Thu Feb 09 10:40:19 2012 +0100
     6.2 +++ b/ucx/ucx.h	Sat Feb 18 15:50:43 2012 +0100
     6.3 @@ -14,6 +14,9 @@
     6.4  extern "C" {
     6.5  #endif
     6.6  
     6.7 +#define UCX_FOREACH(type,list,elem) \
     6.8 +        for (type elem = list ; elem != NULL ; elem = elem->next)
     6.9 +    
    6.10  /* source,data -> errno */
    6.11  typedef int(*ucx_callback)(void*,void*);
    6.12  

mercurial