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