code documentation cleaned
[strongswan.git] / Source / charon / utils / tester.c
1 /**
2 * @file tester.c
3 *
4 * @brief Implementation of tester_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <sys/time.h>
28
29 #include "tester.h"
30
31 #include <utils/allocator.h>
32 #include <utils/linked_list.h>
33 #include <queues/job_queue.h>
34
35
36 typedef struct private_tester_t private_tester_t;
37
38 /**
39 * @brief Private Variables and Functions of tester class.
40 *
41 */
42 struct private_tester_t {
43
44 /**
45 * Public interface.
46 */
47 tester_t public;
48
49
50 /* Private functions */
51 /**
52 * Runs a specific test.
53 *
54 * @param tester associated tester object
55 * @param test_function test function to perform
56 * @param test_name name for the given test
57 */
58 void (*run_test) (tester_t *tester, void (*test_function) (tester_t * tester), char * test_name);
59
60
61 /* Private values */
62 /**
63 * Output is written into this file.
64 */
65 FILE* output;
66
67 /**
68 * Number of runned tests.
69 */
70 int tests_count;
71
72 /**
73 * Number of failed tests.
74 */
75 int failed_tests_count;
76
77 /**
78 * Number of failed asserts in curret test.
79 */
80 int failed_asserts_count;
81
82 /**
83 * TRUE if succeeded asserts should also be written to output.
84 */
85 bool display_succeeded_asserts;
86
87 /**
88 * Mutex to make this object thread-save.
89 */
90 pthread_mutex_t mutex;
91 };
92
93 /**
94 * Implementation of tester_t.perform_tests.
95 */
96 static status_t perform_tests(tester_t *tester,test_t **tests)
97 {
98 private_tester_t *this =(private_tester_t*) tester;
99 int current_test = 0;
100 fprintf(this->output,"\nStart testing...\n\n");
101 fprintf(this->output,"_____________________________________________________________________\n");
102 fprintf(this->output,"Testname | running time\n");
103 fprintf(this->output,"_______________________________________________________|_____________\n");
104
105 while (tests[current_test] != NULL)
106 {
107 this->run_test(tester,tests[current_test]->test_function,tests[current_test]->test_name);
108 current_test++;
109 }
110 fprintf(this->output,"=====================================================================\n");
111 fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
112 fprintf(this->output,"=====================================================================\n");
113 return SUCCESS;
114 }
115
116 /**
117 * Implementation of tester_t.perform_test.
118 */
119 static status_t perform_test(tester_t *tester, test_t *test)
120 {
121 test_t *tests[] = {test, NULL};
122 return (perform_tests(tester,tests));
123 }
124
125 /**
126 * Returns the difference of to timeval structs in microseconds.
127 *
128 * @warning this function is also defined in the event queue
129 * in later improvements, this function can be added to a general
130 * class type!
131 *
132 * @param end_time end time
133 * @param start_time start time
134 *
135 * @TODO make object function or move to utils!
136 *
137 * @return difference in microseconds
138 */
139 static long time_difference(struct timeval *end_time, struct timeval *start_time)
140 {
141 long seconds, microseconds;
142
143 seconds = (end_time->tv_sec - start_time->tv_sec);
144 microseconds = (end_time->tv_usec - start_time->tv_usec);
145 return ((seconds * 1000000) + microseconds);
146 }
147
148
149 /**
150 * Implementation of private_tester_t.run_test.
151 */
152 static void run_test(tester_t *tester, void (*test_function) (tester_t * tester), char * test_name)
153 {
154 struct timeval start_time, end_time;
155 long timediff;
156 private_tester_t *this = (private_tester_t *) tester;
157 this->tests_count++;
158 this->failed_asserts_count = 0;
159 fprintf(this->output,"%-55s\n", test_name);
160 gettimeofday(&start_time,NULL);
161 test_function(tester);
162 gettimeofday(&end_time,NULL);
163 timediff = time_difference(&end_time, &start_time);
164
165 if (this->failed_asserts_count > 0)
166 {
167 fprintf(this->output," => Test failed: %-37s|%10ld us\n",test_name,timediff);
168 }else
169 {
170 fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
171 }
172 if (this->failed_asserts_count > 0)
173 {
174 this->failed_tests_count++;
175 }
176 }
177
178
179 /**
180 * Implementation of tester_t.assert_true.
181 */
182 static void assert_true(tester_t *tester, bool to_be_true,char * assert_name)
183 {
184 private_tester_t *this = (private_tester_t *) tester;
185
186 if (assert_name == NULL)
187 {
188 assert_name = "unknown";
189 }
190
191 pthread_mutex_lock(&(this->mutex));
192 if (!to_be_true)
193 {
194 this->failed_asserts_count++;
195 fprintf(this->output," check '%s' failed!\n", assert_name);
196 }else
197 {
198 if (this->display_succeeded_asserts)
199 {
200 fprintf(this->output," check '%s' succeeded\n", assert_name);
201 }
202 }
203 pthread_mutex_unlock(&(this->mutex));
204 }
205
206 /**
207 * Implementation of tester_t.assert_false.
208 */
209 static void assert_false(tester_t *tester, bool to_be_false,char * assert_name)
210 {
211 tester->assert_true(tester,(!to_be_false),assert_name);
212 }
213
214 /**
215 * Implementation of tester_t.destroy.
216 */
217 static status_t destroy(tester_t *tester)
218 {
219 private_tester_t *this = (private_tester_t*) tester;
220 pthread_mutex_destroy(&(this->mutex));
221 allocator_free(this);
222 return SUCCESS;
223 }
224
225 /*
226 * Described in header.
227 */
228 tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
229 {
230 private_tester_t *this = allocator_alloc_thing(private_tester_t);
231
232 this->public.destroy = destroy;
233 this->public.perform_tests = perform_tests;
234 this->public.perform_test = perform_test;
235 this->public.assert_true = assert_true;
236 this->public.assert_false = assert_false;
237
238
239 this->run_test = run_test;
240 this->display_succeeded_asserts = display_succeeded_asserts;
241 this->failed_tests_count = 0;
242 this->tests_count = 0;
243 this->output = output;
244 pthread_mutex_init(&(this->mutex),NULL);
245
246 return &(this->public);
247 }