aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/lib/utils/tester.c
blob: a7599dd822bddf9c5f43c4fea33b35f0abf14e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * @file tester.c
 *
 * @brief Implementation of tester_t.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>

#include "tester.h"

#include <utils/linked_list.h>
#include <queues/job_queue.h>


typedef struct private_tester_t private_tester_t;

/**
 * @brief Private Data of tester_t class.
 *
 */
struct private_tester_t {
 	
 	/**
 	 * Protected interface of tester_t.
 	 */
 	protected_tester_t protected;

	/**
	 * Runs a specific test.
	 * 
	 * @param tester 			associated tester object
	 * @param test_function		test function to perform
	 * @param test_name			name for the given test
	 */
	void (*run_test) (private_tester_t *tester, void (*test_function) (protected_tester_t * tester), char * test_name);
	
	/**
	 * Returns the difference of to timeval structs in microseconds.
	 *
	 * @warning this function is also defined in the event queue
	 * 			in later improvements, this function can be added to a general
	 *          class type!
	 *
	 * @param end_time 		end time
	 * @param start_time 	start time
	 * 
	 * @TODO make object function or move to utils!
	 *
	 * @return difference in microseconds
	 */
	long (*time_difference) (private_tester_t *tester,struct timeval *end_time, struct timeval *start_time);

	/**
	 * Output is written into this file.
	 */
 	FILE* output;
 	
 	/**
 	 * Number of already performed tests.
 	 */
 	int tests_count;
 	
 	/**
 	 * Number of failed tests.
 	 */
 	int failed_tests_count;
 	
 	/**
 	 * Number of failed asserts in current test.
 	 */ 
 	int failed_asserts_count;
 	
 	/**
 	 * TRUE if also succeeded asserts should be written to output.
 	 */
 	bool display_succeeded_asserts;
 	
 	/**
 	 * Mutex to make this class thread-save.
 	 */
 	pthread_mutex_t mutex;
};

/**
 * Implementation of tester_t.perform_tests.
 */
static void perform_tests(private_tester_t *this,test_t **tests)
{
	int current_test = 0;
	fprintf(this->output,"\nStart testing...\n\n");
	fprintf(this->output,"_____________________________________________________________________\n");
	fprintf(this->output,"Testname                                               | running time\n");
	fprintf(this->output,"_______________________________________________________|_____________\n");

	while (tests[current_test] != NULL)
	{
		this->run_test(this,tests[current_test]->test_function,tests[current_test]->test_name);
		current_test++;
	}
	fprintf(this->output,"=====================================================================\n");
	fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
	fprintf(this->output,"=====================================================================\n");
}

/**
 * Implementation of tester_t.perform_test.
 */
static void perform_test(private_tester_t *this, test_t *test)
{
	test_t *tests[] = {test, NULL};
	return (perform_tests(this,tests));
}

/**
 * Returns the difference of to timeval structs in microseconds.
 *
 * @warning this function is also defined in the event queue
 * 			in later improvements, this function can be added to a general
 *          class type!
 *
 * @param end_time 		end time
 * @param start_time 	start time
 * 
 * @TODO make object function or move to utils!
 *
 * @return difference in microseconds
 */
static long time_difference(private_tester_t *this,struct timeval *end_time, struct timeval *start_time)
{
	long seconds, microseconds;

	seconds = (end_time->tv_sec - start_time->tv_sec);
	microseconds = (end_time->tv_usec - start_time->tv_usec);
	return ((seconds * 1000000) + microseconds);
}


/**
 * Implementation of private_tester_t.run_test.
 */
static void run_test(private_tester_t *this, void (*test_function) (protected_tester_t * tester), char * test_name)
{
	struct timeval start_time, end_time;
	long timediff;
	this->tests_count++;
	this->failed_asserts_count = 0;
	fprintf(this->output,"%-55s\n", test_name);
	gettimeofday(&start_time,NULL);
	test_function(&(this->protected));
	gettimeofday(&end_time,NULL);
	timediff = this->time_difference(this,&end_time, &start_time);

	if (this->failed_asserts_count > 0)
	{
		fprintf(this->output,"  => Test failed: %-37s|%10ld us\n",test_name,timediff);
	}else
	{
		fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
	}
	if (this->failed_asserts_count > 0)
	{
		this->failed_tests_count++;
	}
}
 

/**
 * Implementation of tester_t.assert_true.
 */
static void assert_true(private_tester_t *this, bool to_be_true,char * assert_name)
{
	if (assert_name == NULL)
	{
		assert_name = "unknown";
	}

	pthread_mutex_lock(&(this->mutex));
	if (!to_be_true)
	{
		this->failed_asserts_count++;
		fprintf(this->output,"  check '%s' failed!\n", assert_name);
	}else
	{
		if (this->display_succeeded_asserts)
		{
			fprintf(this->output,"  check '%s' succeeded\n", assert_name);
		}
	}
	pthread_mutex_unlock(&(this->mutex));
}

/**
 * Implementation of tester_t.assert_false.
 */
static void assert_false(private_tester_t *this, bool to_be_false,char * assert_name)
{
	this->protected.assert_true(&(this->protected),(!to_be_false),assert_name);
}

/**
 * Implementation of tester_t.destroy.
 */
static void destroy(private_tester_t *tester)
{
	private_tester_t *this = (private_tester_t*) tester;
	pthread_mutex_destroy(&(this->mutex));
	free(this);
}

/*
 * Described in header.
 */
tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
{
	private_tester_t *this = malloc_thing(private_tester_t);

	/* public functions */
	this->protected.public.destroy = (void (*) (tester_t *))destroy;
	this->protected.public.perform_tests = (void (*) (tester_t *, test_t**)) perform_tests;
	this->protected.public.perform_test = (void (*) (tester_t *, test_t*))perform_test;
	this->protected.assert_true =  (void (*) (protected_tester_t *, bool, char*)) assert_true;
	this->protected.assert_false = (void (*) (protected_tester_t *, bool, char*)) assert_false;
	
	/* private functions */
	this->run_test = run_test;
	this->time_difference = time_difference;
	
	/* private data */
	this->display_succeeded_asserts = display_succeeded_asserts;
	this->failed_tests_count = 0;
	this->tests_count = 0;
	this->output = output;
	pthread_mutex_init(&(this->mutex),NULL);

	return &(this->protected.public);
}