1 /* |
1 /* |
2 * File: test.c |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * Author: Mike |
|
4 * |
3 * |
5 * Created on 18. Februar 2012, 14:15 |
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. |
6 */ |
27 */ |
7 |
28 |
8 #include "test.h" |
29 #include "test.h" |
|
30 |
|
31 |
|
32 struct UcxTestList{ |
|
33 UcxTest test; |
|
34 UcxTestList *next; |
|
35 }; |
9 |
36 |
10 UcxTestSuite* ucx_test_suite_new() { |
37 UcxTestSuite* ucx_test_suite_new() { |
11 UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite)); |
38 UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite)); |
12 if (suite != NULL) { |
39 if (suite != NULL) { |
13 suite->success = 0; |
40 suite->success = 0; |
14 suite->failure = 0; |
41 suite->failure = 0; |
15 suite->tests = NULL; |
42 suite->tests = NULL; |
16 } |
43 } |
|
44 |
17 return suite; |
45 return suite; |
18 } |
46 } |
19 |
47 |
20 void ucx_test_suite_free(UcxTestSuite* suite) { |
48 void ucx_test_suite_free(UcxTestSuite* suite) { |
21 ucx_list_free(suite->tests); |
49 UcxTestList *l = suite->tests; |
|
50 while (l != NULL) { |
|
51 UcxTestList *e = l; |
|
52 l = l->next; |
|
53 free(e); |
|
54 } |
22 free(suite); |
55 free(suite); |
23 } |
56 } |
24 |
57 |
25 void ucx_test_register(UcxTestSuite* suite, UcxTest test) { |
58 int ucx_test_register(UcxTestSuite* suite, UcxTest test) { |
26 suite->tests = ucx_list_append(suite->tests, (void*) test); |
59 if (suite->tests) { |
|
60 UcxTestList *newelem = (UcxTestList*) malloc(sizeof(UcxTestList)); |
|
61 if (newelem) { |
|
62 newelem->test = test; |
|
63 newelem->next = NULL; |
|
64 |
|
65 UcxTestList *last = suite->tests; |
|
66 while (last->next) { |
|
67 last = last->next; |
|
68 } |
|
69 last->next = newelem; |
|
70 |
|
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; |
|
80 |
|
81 return EXIT_SUCCESS; |
|
82 } else { |
|
83 return EXIT_FAILURE; |
|
84 } |
|
85 } |
27 } |
86 } |
28 |
87 |
29 void ucx_test_run(UcxTestSuite* suite, FILE* output) { |
88 void ucx_test_run(UcxTestSuite* suite, FILE* output) { |
30 suite->success = 0; |
89 suite->success = 0; |
31 suite->failure = 0; |
90 suite->failure = 0; |
32 UCX_FOREACH (UcxList*, suite->tests, e) { |
91 for (UcxTestList* elem = suite->tests ; elem ; elem = elem->next) { |
33 UcxTest test = (UcxTest) (e->data); |
92 elem->test(suite, output); |
34 test(suite, output); |
|
35 } |
93 } |
36 fwrite("\nAll test completed.\n", 1, 21, output); |
94 fwrite("\nAll test completed.\n", 1, 21, output); |
37 fprintf(output, " Total: %d\n Success: %d\n Failure: %d\n", |
95 fprintf(output, " Total: %d\n Success: %d\n Failure: %d\n", |
38 suite->success+suite->failure, suite->success, suite->failure); |
96 suite->success+suite->failure, suite->success, suite->failure); |
39 } |
97 } |