Debian Codegraph @

check/stf/fcml_stf.c

raw
1 /*
2 * FCML - Free Code Manipulation Library.
3 * Copyright (C) 2010-2021 Slawomir Wojtasiak
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "fcml_stf.h"
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 /* Failed asserts and counters are stored here.*/
27 fcml_stf_test_result fcml_gl_stf_test_result = { 0 };
28
29 /* Assert's counters for running test.*/
30 fcml_stf_test_counter current_test_counters = { 0 };
31
32 /* Tested suite.*/
33 fcml_stf_test_suite *current_suite;
34
35 /* tested test case.*/
36 fcml_stf_test_case *current_test;
37
38 fcml_bool LIB_CALL_STF fcml_str_run_assert( fcml_bool condition,
39 const fcml_string file, fcml_int line, const fcml_string message ) {
40
41 if( !condition ) {
42
43 /* Assert failed, so allocate and save some information
44 * about this event.
45 */
46 fcml_stf_result_element *element = (fcml_stf_result_element*)
47 malloc( sizeof( fcml_stf_result_element ) );
48 if( !element ) {
49 printf("Can not allocate memory for assert result.");
50 return condition;
51 }
52
53 memset( element, 0, sizeof( fcml_stf_result_element ) );
54
55 element->file = file;
56 element->line = line;
57 element->message = message;
58 element->test_case = current_test;
59 element->suite = current_suite;
60
61 if( !fcml_gl_stf_test_result.first ) {
62 fcml_gl_stf_test_result.first = element;
63 } else {
64 fcml_gl_stf_test_result.last->next = element;
65 }
66
67 fcml_gl_stf_test_result.last = element;
68
69 current_test_counters.failed++;
70
71 } else {
72 current_test_counters.passed++;
73 }
74
75 return condition;
76
77 }
78
79 fcml_bool LIB_CALL_STF fcml_stf_run_tests( const fcml_string test_name,
80 fcml_stf_test_suite **suite ) {
81
82 /* Execute suites one by one.*/
83
84 fcml_stf_test_suite **current = suite;
85 while( *current ) {
86
87 fcml_stf_test_suite *suite = *current;
88
89 fcml_stf_test_case *test_case = suite->test_cases;
90
91 current_suite = suite;
92
93 /* Check if there are any test cases for current suite.*/
94 if( test_case ) {
95
96 /* True if at least one test failed.*/
97 fcml_bool test_failed = FCML_FALSE;
98
99 /* Initialize test suit.*/
100 if( suite->init_function ) {
101 if( !suite->init_function() ) {
102 test_failed = FCML_TRUE;
103 }
104 }
105
106 /* Last test which is also an array guard has NULL name.*/
107 while( test_case->name ) {
108
109 current_test = test_case;
110
111 /* Clear current counters.*/
112 current_test_counters.failed = 0;
113 current_test_counters.passed = 0;
114
115 /* Run test function.*/
116 if( test_case->test_function ) {
117 test_case->test_function();
118 }
119
120 fcml_gl_stf_test_result.asserts.failed +=
121 current_test_counters.failed;
122 fcml_gl_stf_test_result.asserts.passed +=
123 current_test_counters.passed;
124
125 if( current_test_counters.failed ) {
126 /* There are assert failures, so test has failed.*/
127 fcml_gl_stf_test_result.tests.failed++;
128 test_failed = FCML_TRUE;
129 } else {
130 fcml_gl_stf_test_result.tests.passed++;
131 }
132
133 test_case++;
134 }
135
136 /* Cleanup test suite.*/
137 if( suite->cleanup_function ) {
138 if( !suite->cleanup_function() ) {
139 test_failed = FCML_TRUE;
140 }
141 }
142
143 /* Count suites.*/
144 if( test_failed ) {
145 fcml_gl_stf_test_result.suites.failed++;
146 } else {
147 fcml_gl_stf_test_result.suites.passed++;
148 }
149
150 } else {
151 /* Guard.*/
152 break;
153 }
154
155 current++;
156 }
157
158 /* Report first 100 asserts errors.*/
159
160 printf("\nTest [%s]:", test_name );
161
162 current_suite = NULL;
163 current_test = NULL;
164
165 int counter = 0;
166 int asserts_counter = 0;
167
168 fcml_stf_result_element *assert = fcml_gl_stf_test_result.first;
169 while( assert && counter <= 100 ) {
170
171 fcml_stf_test_suite *suite = assert->suite;
172 fcml_stf_test_case *test_case = assert->test_case;
173
174 if( current_suite != suite || current_test != test_case ) {
175 current_suite = suite;
176 current_test = test_case;
177 asserts_counter = 0;
178 }
179
180 if( asserts_counter == 0 ) {
181 printf( "\nSuite %s, Test %s had failures:\n", current_suite->name,
182 current_test->name );
183 }
184
185 printf( " %4d. %s:%-6d \"%s\"\n", asserts_counter+++1, assert->file,
186 assert->line, assert->message );
187
188 assert = assert->next;
189
190 counter++;
191 }
192
193 /* Print counters report.*/
194
195 fcml_stf_test_counter suites = fcml_gl_stf_test_result.suites;
196 fcml_stf_test_counter tests = fcml_gl_stf_test_result.tests;
197 fcml_stf_test_counter asserts = fcml_gl_stf_test_result.asserts;
198
199 printf("\nRun Summary: Type Ran Passed Failed\n");
200 printf(" suites %10d %10d %10d\n", suites.failed +
201 suites.passed, suites.passed, suites.failed );
202 printf(" tests %10d %10d %10d\n", tests.failed +
203 tests.passed, tests.passed, tests.failed );
204 printf(" asserts %10d %10d %10d\n", asserts.failed +
205 asserts.passed, asserts.passed, asserts.failed );
206
207 /* Free all reported asserts.*/
208 fcml_stf_result_element *element = fcml_gl_stf_test_result.first;
209 while( element ) {
210 fcml_stf_result_element *tmp = element;
211 element = tmp->next;
212 free( tmp );
213 }
214
215 return fcml_gl_stf_test_result.suites.failed == 0;
216 }