created protected_tester_t for testcases
[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 Data of tester_t class.
40 *
41 */
42 struct private_tester_t {
43
44 /**
45 * Protected interface of tester_t.
46 */
47 protected_tester_t protected;
48
49 /**
50 * Runs a specific test.
51 *
52 * @param tester associated tester object
53 * @param test_function test function to perform
54 * @param test_name name for the given test
55 */
56 void (*run_test) (private_tester_t *tester, void (*test_function) (protected_tester_t * tester), char * test_name);
57
58 /**
59 * Returns the difference of to timeval structs in microseconds.
60 *
61 * @warning this function is also defined in the event queue
62 * in later improvements, this function can be added to a general
63 * class type!
64 *
65 * @param end_time end time
66 * @param start_time start time
67 *
68 * @TODO make object function or move to utils!
69 *
70 * @return difference in microseconds
71 */
72 long (*time_difference) (private_tester_t *tester,struct timeval *end_time, struct timeval *start_time);
73
74 /**
75 * Output is written into this file.
76 */
77 FILE* output;
78
79 /**
80 * Number of already performed tests.
81 */
82 int tests_count;
83
84 /**
85 * Number of failed tests.
86 */
87 int failed_tests_count;
88
89 /**
90 * Number of failed asserts in current test.
91 */
92 int failed_asserts_count;
93
94 /**
95 * TRUE if also succeeded asserts should be written to output.
96 */
97 bool display_succeeded_asserts;
98
99 /**
100 * Mutex to make this class thread-save.
101 */
102 pthread_mutex_t mutex;
103 };
104
105 /**
106 * Implementation of tester_t.perform_tests.
107 */
108 static void perform_tests(private_tester_t *this,test_t **tests)
109 {
110 int current_test = 0;
111 fprintf(this->output,"\nStart testing...\n\n");
112 fprintf(this->output,"_____________________________________________________________________\n");
113 fprintf(this->output,"Testname | running time\n");
114 fprintf(this->output,"_______________________________________________________|_____________\n");
115
116 while (tests[current_test] != NULL)
117 {
118 this->run_test(this,tests[current_test]->test_function,tests[current_test]->test_name);
119 current_test++;
120 }
121 fprintf(this->output,"=====================================================================\n");
122 fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
123 fprintf(this->output,"=====================================================================\n");
124 }
125
126 /**
127 * Implementation of tester_t.perform_test.
128 */
129 static void perform_test(private_tester_t *this, test_t *test)
130 {
131 test_t *tests[] = {test, NULL};
132 return (perform_tests(this,tests));
133 }
134
135 /**
136 * Returns the difference of to timeval structs in microseconds.
137 *
138 * @warning this function is also defined in the event queue
139 * in later improvements, this function can be added to a general
140 * class type!
141 *
142 * @param end_time end time
143 * @param start_time start time
144 *
145 * @TODO make object function or move to utils!
146 *
147 * @return difference in microseconds
148 */
149 static long time_difference(private_tester_t *this,struct timeval *end_time, struct timeval *start_time)
150 {
151 long seconds, microseconds;
152
153 seconds = (end_time->tv_sec - start_time->tv_sec);
154 microseconds = (end_time->tv_usec - start_time->tv_usec);
155 return ((seconds * 1000000) + microseconds);
156 }
157
158
159 /**
160 * Implementation of private_tester_t.run_test.
161 */
162 static void run_test(private_tester_t *this, void (*test_function) (protected_tester_t * tester), char * test_name)
163 {
164 struct timeval start_time, end_time;
165 long timediff;
166 this->tests_count++;
167 this->failed_asserts_count = 0;
168 fprintf(this->output,"%-55s\n", test_name);
169 gettimeofday(&start_time,NULL);
170 test_function(&(this->protected));
171 gettimeofday(&end_time,NULL);
172 timediff = this->time_difference(this,&end_time, &start_time);
173
174 if (this->failed_asserts_count > 0)
175 {
176 fprintf(this->output," => Test failed: %-37s|%10ld us\n",test_name,timediff);
177 }else
178 {
179 fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
180 }
181 if (this->failed_asserts_count > 0)
182 {
183 this->failed_tests_count++;
184 }
185 }
186
187
188 /**
189 * Implementation of tester_t.assert_true.
190 */
191 static void assert_true(private_tester_t *this, bool to_be_true,char * assert_name)
192 {
193 if (assert_name == NULL)
194 {
195 assert_name = "unknown";
196 }
197
198 pthread_mutex_lock(&(this->mutex));
199 if (!to_be_true)
200 {
201 this->failed_asserts_count++;
202 fprintf(this->output," check '%s' failed!\n", assert_name);
203 }else
204 {
205 if (this->display_succeeded_asserts)
206 {
207 fprintf(this->output," check '%s' succeeded\n", assert_name);
208 }
209 }
210 pthread_mutex_unlock(&(this->mutex));
211 }
212
213 /**
214 * Implementation of tester_t.assert_false.
215 */
216 static void assert_false(private_tester_t *this, bool to_be_false,char * assert_name)
217 {
218 this->protected.assert_true(&(this->protected),(!to_be_false),assert_name);
219 }
220
221 /**
222 * Implementation of tester_t.destroy.
223 */
224 static void destroy(private_tester_t *tester)
225 {
226 private_tester_t *this = (private_tester_t*) tester;
227 pthread_mutex_destroy(&(this->mutex));
228 allocator_free(this);
229 }
230
231 /*
232 * Described in header.
233 */
234 tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
235 {
236 private_tester_t *this = allocator_alloc_thing(private_tester_t);
237
238 /* public functions */
239 this->protected.public.destroy = (void (*) (tester_t *))destroy;
240 this->protected.public.perform_tests = (void (*) (tester_t *, test_t**)) perform_tests;
241 this->protected.public.perform_test = (void (*) (tester_t *, test_t*))perform_test;
242 this->protected.assert_true = (void (*) (protected_tester_t *, bool, char*)) assert_true;
243 this->protected.assert_false = (void (*) (protected_tester_t *, bool, char*)) assert_false;
244
245 /* private functions */
246 this->run_test = run_test;
247 this->time_difference = time_difference;
248
249 /* private data */
250 this->display_succeeded_asserts = display_succeeded_asserts;
251 this->failed_tests_count = 0;
252 this->tests_count = 0;
253 this->output = output;
254 pthread_mutex_init(&(this->mutex),NULL);
255
256 return &(this->protected.public);
257 }