4 * @brief Implementation of tester_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
31 #include <utils/linked_list.h>
32 #include <queues/job_queue.h>
35 typedef struct private_tester_t private_tester_t
;
38 * @brief Private Data of tester_t class.
41 struct private_tester_t
{
44 * Protected interface of tester_t.
46 protected_tester_t
protected;
49 * Runs a specific test.
51 * @param tester associated tester object
52 * @param test_function test function to perform
53 * @param test_name name for the given test
55 void (*run_test
) (private_tester_t
*tester
, void (*test_function
) (protected_tester_t
* tester
), char * test_name
);
58 * Returns the difference of to timeval structs in microseconds.
60 * @warning this function is also defined in the event queue
61 * in later improvements, this function can be added to a general
64 * @param end_time end time
65 * @param start_time start time
67 * @TODO make object function or move to utils!
69 * @return difference in microseconds
71 long (*time_difference
) (private_tester_t
*tester
,struct timeval
*end_time
, struct timeval
*start_time
);
74 * Output is written into this file.
79 * Number of already performed tests.
84 * Number of failed tests.
86 int failed_tests_count
;
89 * Number of failed asserts in current test.
91 int failed_asserts_count
;
94 * TRUE if also succeeded asserts should be written to output.
96 bool display_succeeded_asserts
;
99 * Mutex to make this class thread-save.
101 pthread_mutex_t mutex
;
105 * Implementation of tester_t.perform_tests.
107 static void perform_tests(private_tester_t
*this,test_t
**tests
)
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");
115 while (tests
[current_test
] != NULL
)
117 this->run_test(this,tests
[current_test
]->test_function
,tests
[current_test
]->test_name
);
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");
126 * Implementation of tester_t.perform_test.
128 static void perform_test(private_tester_t
*this, test_t
*test
)
130 test_t
*tests
[] = {test
, NULL
};
131 return (perform_tests(this,tests
));
135 * Returns the difference of to timeval structs in microseconds.
137 * @warning this function is also defined in the event queue
138 * in later improvements, this function can be added to a general
141 * @param end_time end time
142 * @param start_time start time
144 * @TODO make object function or move to utils!
146 * @return difference in microseconds
148 static long time_difference(private_tester_t
*this,struct timeval
*end_time
, struct timeval
*start_time
)
150 long seconds
, microseconds
;
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
);
159 * Implementation of private_tester_t.run_test.
161 static void run_test(private_tester_t
*this, void (*test_function
) (protected_tester_t
* tester
), char * test_name
)
163 struct timeval start_time
, end_time
;
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
);
173 if (this->failed_asserts_count
> 0)
175 fprintf(this->output
," => Test failed: %-37s|%10ld us\n",test_name
,timediff
);
178 fprintf(this->output
,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff
);
180 if (this->failed_asserts_count
> 0)
182 this->failed_tests_count
++;
188 * Implementation of tester_t.assert_true.
190 static void assert_true(private_tester_t
*this, bool to_be_true
,char * assert_name
)
192 if (assert_name
== NULL
)
194 assert_name
= "unknown";
197 pthread_mutex_lock(&(this->mutex
));
200 this->failed_asserts_count
++;
201 fprintf(this->output
," check '%s' failed!\n", assert_name
);
204 if (this->display_succeeded_asserts
)
206 fprintf(this->output
," check '%s' succeeded\n", assert_name
);
209 pthread_mutex_unlock(&(this->mutex
));
213 * Implementation of tester_t.assert_false.
215 static void assert_false(private_tester_t
*this, bool to_be_false
,char * assert_name
)
217 this->protected.assert_true(&(this->protected),(!to_be_false
),assert_name
);
221 * Implementation of tester_t.destroy.
223 static void destroy(private_tester_t
*tester
)
225 private_tester_t
*this = (private_tester_t
*) tester
;
226 pthread_mutex_destroy(&(this->mutex
));
231 * Described in header.
233 tester_t
*tester_create(FILE *output
, bool display_succeeded_asserts
)
235 private_tester_t
*this = malloc_thing(private_tester_t
);
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
;
244 /* private functions */
245 this->run_test
= run_test
;
246 this->time_difference
= time_difference
;
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
);
255 return &(this->protected.public);