Fri, 09 Aug 2013 11:32:10 +0200
documented test.h and removed duplicated implement/declare macros for UCX_TEST
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "test.h"
32 struct UcxTestList{
33 UcxTest test;
34 UcxTestList *next;
35 };
37 UcxTestSuite* ucx_test_suite_new() {
38 UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite));
39 if (suite != NULL) {
40 suite->success = 0;
41 suite->failure = 0;
42 suite->tests = NULL;
43 }
45 return suite;
46 }
48 void ucx_test_suite_free(UcxTestSuite* suite) {
49 UcxTestList *l = suite->tests;
50 while (l != NULL) {
51 UcxTestList *e = l;
52 l = l->next;
53 free(e);
54 }
55 free(suite);
56 }
58 int ucx_test_register(UcxTestSuite* suite, UcxTest test) {
59 if (suite->tests) {
60 UcxTestList *newelem = (UcxTestList*) malloc(sizeof(UcxTestList));
61 if (newelem) {
62 newelem->test = test;
63 newelem->next = NULL;
65 UcxTestList *last = suite->tests;
66 while (last->next) {
67 last = last->next;
68 }
69 last->next = newelem;
71 return EXIT_SUCCESS;
72 } else {
73 return EXIT_FAILURE;
74 }
75 } else {
76 suite->tests = (UcxTestList*) malloc(sizeof(UcxTestList));
77 if (suite->tests) {
78 suite->tests->test = test;
79 suite->tests->next = NULL;
81 return EXIT_SUCCESS;
82 } else {
83 return EXIT_FAILURE;
84 }
85 }
86 }
88 void ucx_test_run(UcxTestSuite* suite, FILE* output) {
89 suite->success = 0;
90 suite->failure = 0;
91 for (UcxTestList* elem = suite->tests ; elem ; elem = elem->next) {
92 elem->test(suite, output);
93 }
94 fwrite("\nAll test completed.\n", 1, 21, output);
95 fprintf(output, " Total: %d\n Success: %d\n Failure: %d\n",
96 suite->success+suite->failure, suite->success, suite->failure);
97 }