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/allocator.h>
32 #include <utils/linked_list.h>
33 #include <queues/job_queue.h>
36 typedef struct private_tester_t private_tester_t
;
39 * @brief Private Variables and Functions of tester class.
42 struct private_tester_t
{
50 /* Private functions */
52 * Runs a specific test.
54 * @param tester associated tester object
55 * @param test_function test function to perform
56 * @param test_name name for the given test
58 void (*run_test
) (tester_t
*tester
, void (*test_function
) (tester_t
* tester
), char * test_name
);
63 * Output is written into this file.
68 * Number of runned tests.
73 * Number of failed tests.
75 int failed_tests_count
;
78 * Number of failed asserts in curret test.
80 int failed_asserts_count
;
83 * TRUE if succeeded asserts should also be written to output.
85 bool display_succeeded_asserts
;
88 * Mutex to make this object thread-save.
90 pthread_mutex_t mutex
;
94 * Implementation of tester_t.perform_tests.
96 static status_t
perform_tests(tester_t
*tester
,test_t
**tests
)
98 private_tester_t
*this =(private_tester_t
*) tester
;
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");
105 while (tests
[current_test
] != NULL
)
107 this->run_test(tester
,tests
[current_test
]->test_function
,tests
[current_test
]->test_name
);
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");
117 * Implementation of tester_t.perform_test.
119 static status_t
perform_test(tester_t
*tester
, test_t
*test
)
121 test_t
*tests
[] = {test
, NULL
};
122 return (perform_tests(tester
,tests
));
126 * Returns the difference of to timeval structs in microseconds.
128 * @warning this function is also defined in the event queue
129 * in later improvements, this function can be added to a general
132 * @param end_time end time
133 * @param start_time start time
135 * @TODO make object function or move to utils!
137 * @return difference in microseconds
139 static long time_difference(struct timeval
*end_time
, struct timeval
*start_time
)
141 long seconds
, microseconds
;
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
);
150 * Implementation of private_tester_t.run_test.
152 static void run_test(tester_t
*tester
, void (*test_function
) (tester_t
* tester
), char * test_name
)
154 struct timeval start_time
, end_time
;
156 private_tester_t
*this = (private_tester_t
*) tester
;
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
);
165 if (this->failed_asserts_count
> 0)
167 fprintf(this->output
," => Test failed: %-37s|%10ld us\n",test_name
,timediff
);
170 fprintf(this->output
,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff
);
172 if (this->failed_asserts_count
> 0)
174 this->failed_tests_count
++;
180 * Implementation of tester_t.assert_true.
182 static void assert_true(tester_t
*tester
, bool to_be_true
,char * assert_name
)
184 private_tester_t
*this = (private_tester_t
*) tester
;
186 if (assert_name
== NULL
)
188 assert_name
= "unknown";
191 pthread_mutex_lock(&(this->mutex
));
194 this->failed_asserts_count
++;
195 fprintf(this->output
," check '%s' failed!\n", assert_name
);
198 if (this->display_succeeded_asserts
)
200 fprintf(this->output
," check '%s' succeeded\n", assert_name
);
203 pthread_mutex_unlock(&(this->mutex
));
207 * Implementation of tester_t.assert_false.
209 static void assert_false(tester_t
*tester
, bool to_be_false
,char * assert_name
)
211 tester
->assert_true(tester
,(!to_be_false
),assert_name
);
215 * Implementation of tester_t.destroy.
217 static status_t
destroy(tester_t
*tester
)
219 private_tester_t
*this = (private_tester_t
*) tester
;
220 pthread_mutex_destroy(&(this->mutex
));
221 allocator_free(this);
226 * Described in header.
228 tester_t
*tester_create(FILE *output
, bool display_succeeded_asserts
)
230 private_tester_t
*this = allocator_alloc_thing(private_tester_t
);
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
;
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
);
246 return &(this->public);