77e9c772861196b286cdb859d47034ca63779e8d
[strongswan.git] / Source / charon / utils / tester.c
1 /**
2 * @file tester.c
3 *
4 * @brief Test module for automatic testing
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 tester_t public;
44
45
46 /* Private functions */
47 void (*run_test) (tester_t *tester, void (*test_function) (tester_t * tester), char * test_name);
48
49
50 /* Private values */
51 FILE* output;
52 int tests_count;
53 int failed_tests_count;
54 int failed_asserts_count;
55 bool display_succeeded_asserts;
56 pthread_mutex_t mutex;
57 };
58
59 /*
60 * Implementation of function perform_tests
61 */
62 static status_t perform_tests(tester_t *tester,test_t **tests)
63 {
64 private_tester_t *this =(private_tester_t*) tester;
65 int current_test = 0;
66 fprintf(this->output,"\nStart testing...\n\n");
67 fprintf(this->output,"_____________________________________________________________________\n");
68 fprintf(this->output,"Testname | running time\n");
69 fprintf(this->output,"_______________________________________________________|_____________\n");
70
71 while (tests[current_test] != NULL)
72 {
73 this->run_test(tester,tests[current_test]->test_function,tests[current_test]->test_name);
74 current_test++;
75 }
76 fprintf(this->output,"=====================================================================\n");
77 fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
78 fprintf(this->output,"=====================================================================\n");
79 return SUCCESS;
80 }
81
82 /*
83 * Implementation of function perform_test
84 */
85 static status_t perform_test(tester_t *tester, test_t *test)
86 {
87 test_t *tests[] = {test, NULL};
88 return (perform_tests(tester,tests));
89 }
90
91 /**
92 * Returns the difference of to timeval structs in microseconds
93 *
94 * @param end_time end time
95 * @param start_time start time
96 *
97 * @warning this function is also defined in the event queue
98 * in later improvements, this function can be added to a general
99 * class type!
100 *
101 * @return difference in microseconds
102 */
103 static long time_difference(struct timeval *end_time, struct timeval *start_time)
104 {
105 long seconds, microseconds;
106
107 seconds = (end_time->tv_sec - start_time->tv_sec);
108 microseconds = (end_time->tv_usec - start_time->tv_usec);
109 return ((seconds * 1000000) + microseconds);
110 }
111
112
113 /**
114 * Implementation of function run_test
115 */
116 static void run_test(tester_t *tester, void (*test_function) (tester_t * tester), char * test_name)
117 {
118 struct timeval start_time, end_time;
119 long timediff;
120 private_tester_t *this = (private_tester_t *) tester;
121 this->tests_count++;
122 this->failed_asserts_count = 0;
123 fprintf(this->output,"%-55s\n", test_name);
124 gettimeofday(&start_time,NULL);
125 test_function(tester);
126 gettimeofday(&end_time,NULL);
127 timediff = time_difference(&end_time, &start_time);
128
129 if (this->failed_asserts_count > 0)
130 {
131 fprintf(this->output," => Test failed: %-37s|%10ld us\n",test_name,timediff);
132 }else
133 {
134 fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
135 }
136 if (this->failed_asserts_count > 0)
137 {
138 this->failed_tests_count++;
139 }
140 }
141
142
143 /**
144 * Implementation of function assert_true
145 */
146 static void assert_true(tester_t *tester, bool to_be_true,char * assert_name)
147 {
148 private_tester_t *this = (private_tester_t *) tester;
149
150 if (assert_name == NULL)
151 {
152 assert_name = "unknown";
153 }
154
155 pthread_mutex_lock(&(this->mutex));
156 if (!to_be_true)
157 {
158 this->failed_asserts_count++;
159 fprintf(this->output," check '%s' failed!\n", assert_name);
160 }else
161 {
162 if (this->display_succeeded_asserts)
163 {
164 fprintf(this->output," check '%s' succeeded\n", assert_name);
165 }
166 }
167 pthread_mutex_unlock(&(this->mutex));
168 }
169
170 /**
171 * Implementation of function assert_false
172 */
173 static void assert_false(tester_t *tester, bool to_be_false,char * assert_name)
174 {
175 tester->assert_true(tester,(!to_be_false),assert_name);
176 }
177
178 /**
179 * Implements the destroy function
180 */
181 static status_t destroy(tester_t *tester)
182 {
183 private_tester_t *this = (private_tester_t*) tester;
184 pthread_mutex_destroy(&(this->mutex));
185 allocator_free(this);
186 return SUCCESS;
187 }
188
189 tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
190 {
191 private_tester_t *this = allocator_alloc_thing(private_tester_t);
192
193 this->public.destroy = destroy;
194 this->public.perform_tests = perform_tests;
195 this->public.perform_test = perform_test;
196 this->public.assert_true = assert_true;
197 this->public.assert_false = assert_false;
198
199
200 this->run_test = run_test;
201 this->display_succeeded_asserts = display_succeeded_asserts;
202 this->failed_tests_count = 0;
203 this->tests_count = 0;
204 this->output = output;
205 pthread_mutex_init(&(this->mutex),NULL);
206
207 return &(this->public);
208 }