Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e8ee2e1761f9ea97d3cbbf8b43063dd6dd8d99b3 (plain) (blame)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/

#include "etUnit/etUnit.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "debugging/etLogger.h"
#include "osal/etSema.h"
#include "runtime/etRuntime.h"
#include "helpers/etTimeHelpers.h"

/*** member variables */

/* file handling */
static FILE* etUnit_reportfile = NULL;

/* counters */
static etInt16 etUnit_nextCaseId;
static etInt32 etUnit_errorCounter;

#define ETUNIT_MAX_TEST_CASES		256
static etBool etUnit_testcaseSuccess[ETUNIT_MAX_TEST_CASES];

#define ETUNIT_FAILURE_TEXT_LEN 256

/* time measuring */
static etTime etUnit_startTime;
static etTime etUnit_lastTestCaseTime;

etInt16 getCurrentEtUnitId() {
	return etUnit_nextCaseId - 1;
}

/* order */
#define ETUNIT_ORDER_MAX	16
typedef struct OrderInfo {
	etInt16 id;
	etInt16 currentIndex;
	etInt16 size;
	etInt16* list;
} OrderInfo;
static OrderInfo etUnit_orderInfo[ETUNIT_ORDER_MAX];

static OrderInfo* getOrderInfo(etInt16 id) {
	int i;
	for (i = 0; i < ETUNIT_ORDER_MAX; ++i)
		if (etUnit_orderInfo[i].id == id)
			return etUnit_orderInfo + i;

	return NULL;
}

/* float measuring */
#if defined (ET_FLOAT32) || defined (ET_FLOAT64)
#define ETUNIT_FLOAT
#ifdef ET_FLOAT64
typedef etFloat64 etUnitFloat;
#else
typedef etFloat32 etUnitFloat;
#endif
#endif

/* forward declarations of private functions */
static void expect_equal_int(etInt16 id, const char* message, etInt32 expected, etInt32 actual, const char* file, int line);
/* currently not used
static void expect_range_int(etInt16 id, const char* message, etInt32 min, etInt32 max, etInt32 actual, const char* file, int line);
*/
static void expect_equal_uint(etInt16 id, const char* message, etUInt32 expected, etUInt32 actual, const char* file, int line);
/* currently not used
static void expect_range_uint(etInt16 id, const char* message, etUInt32 min, etUInt32 max, etUInt32 actual, const char* file, int line);
*/

#ifdef ETUNIT_FLOAT
static void expect_equal_float(etInt16 id, const char* message, etUnitFloat expected, etUnitFloat actual, etUnitFloat precision, const char* file, int line);
static void expect_range_float(etInt16 id, const char* message, etUnitFloat min, etUnitFloat max, etUnitFloat actual, const char* file, int line);
#endif

static void etUnit_handleExpect(etInt16 id, etBool result, const char *trace, const char* expected, const char* actual, const char* file, int line);

/* public functions */

void etUnit_open(const char* testResultPath, const char* testFileName) {
	etLogger_logInfoF("************* TEST START (%s) **************", testFileName);

	{
		char filename[ETUNIT_FAILURE_TEXT_LEN];
		int i;

		if (testResultPath != NULL)
			snprintf(filename, sizeof(filename), "%s/%s.etu", testResultPath, testFileName);
		else
			snprintf(filename, sizeof(filename),"%s.etu", testFileName);

		/* init global data */
		for (i = 0; i < ETUNIT_ORDER_MAX; ++i)
			etUnit_orderInfo[i].id = 0;
		for (i = 0; i < ETUNIT_MAX_TEST_CASES; ++i)
			etUnit_testcaseSuccess[i] = ET_TRUE;
		etUnit_errorCounter = 0;
		etUnit_nextCaseId = 1;

		if (etUnit_reportfile == NULL) {
			etUnit_reportfile = etLogger_fopen(filename, "w+");
			if (etUnit_reportfile != NULL) {
				etLogger_fprintf(etUnit_reportfile, "etUnit report\n");
			} else {
				etLogger_logErrorF("Unable to open file %s", filename);
			}
		}
	}

	/* prepare time measurement */
	getTimeFromTarget(&etUnit_startTime);
}

void etUnit_close(void) {
	etTime endTime;
	if (etUnit_reportfile != NULL) {
		etLogger_fclose(etUnit_reportfile);
		etUnit_reportfile = NULL;
	}
	getTimeFromTarget(&endTime);
	etTimeHelpers_subtract(&endTime, &etUnit_startTime);
	etLogger_logInfoF("Elapsed Time: %ld ms", etTimeHelpers_convertToMSec(&endTime));
	if (etUnit_errorCounter == 0)
		etLogger_logInfoF("Error Counter: %ld", etUnit_errorCounter);
	else
		etLogger_logErrorF("Error Counter: %ld", etUnit_errorCounter);
	etLogger_logInfoF("************* TEST END **************");
}

void etUnit_openTestSuite(const char* testSuiteName) {
	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "ts start: %s\n", testSuiteName);
	}
}

void etUnit_closeTestSuite(void) {
}

etInt16 etUnit_openTestCase(const char* testCaseName) {
	etInt16 caseId = etUnit_nextCaseId++;

	if (caseId >= ETUNIT_MAX_TEST_CASES) {
		etLogger_logErrorF("Too many test cases. Maximum number of test cases is %d\n",
		ETUNIT_MAX_TEST_CASES);
		etLogger_logErrorF("ETUNIT_MAX_TEST_CASES (etUnit_openTestCase, %s: %d)", __FILE__,
		__LINE__);
		exit(-1);
	}
	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "tc start %d: %s\n", caseId, testCaseName);
		getTimeFromTarget(&etUnit_lastTestCaseTime);
	}

	return caseId;
}

void etUnit_closeTestCase(etInt16 id) {
	OrderInfo* info = getOrderInfo(id);
	if(info != NULL){
		if (info->currentIndex != info->size) {
			etUnit_handleExpect(id, ET_FALSE, "EXPECT_ORDER was not completed", NULL, NULL, 0, 0);
		}
	}

	if (etUnit_reportfile != NULL) {
		etTime time;
		getTimeFromTarget(&time);
		etTimeHelpers_subtract(&time, &etUnit_lastTestCaseTime);
		etLogger_fprintf(etUnit_reportfile, "tc end %d: %d\n", id, etTimeHelpers_convertToMSec(&time));
	}
}

void etUnit_skipTestCase(etInt16 id, const char* msg) {
	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "tc skip %d: %s\n", id, msg);
	}
}

etInt16 etUnit_openAll(const char* testResultPath, const char* testFileName, const char* testSuiteName, const char* testCaseName) {
	etUnit_open(testResultPath, testFileName);
	etUnit_openTestSuite(testSuiteName);
	return etUnit_openTestCase(testCaseName);
}

void etUnit_closeAll(etInt16 id) {
	etUnit_closeTestCase(id);
	etUnit_closeTestSuite();
	etUnit_close();
}

void etUnit_testFinished(etInt16 id) {
	etSema_wakeup(etRuntime_getTerminateSemaphore());
}

void expectTrue(etInt16 id, const char* message, etBool condition, const char* file, int line) {
	if (condition == ET_FALSE) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		snprintf(testresult, sizeof(testresult), "%s: *** EXPECT_TRUE == FALSE", message);
		etUnit_handleExpect(id, ET_FALSE, testresult, "TRUE", "FALSE", file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

void expectFalse(etInt16 id, const char* message, etBool condition, const char* file, int line) {
	if (condition == ET_TRUE) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		snprintf(testresult, sizeof(testresult), "%s: EXPECT_FALSE == TRUE", message);
		etUnit_handleExpect(id, ET_FALSE, testresult, "FALSE", "TRUE", file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

void expectEqualInt8(etInt16 id, const char* message, etInt8 expected, etInt8 actual, const char* file, int line) {
	expect_equal_int(id, message, (etInt32) expected, (etInt32) actual, file, line);
}

void expectEqualInt16(etInt16 id, const char* message, etInt16 expected, etInt16 actual, const char* file, int line) {
	expect_equal_int(id, message, (etInt32) expected, (etInt32) actual, file, line);
}

void expectEqualInt32(etInt16 id, const char* message, etInt32 expected, etInt32 actual, const char* file, int line) {
	expect_equal_int(id, message, (etInt32) expected, (etInt32) actual, file, line);
}

void expectEqualUInt8(etInt16 id, const char* message, etUInt8 expected, etUInt8 actual, const char* file, int line) {
	expect_equal_uint(id, message, (etUInt32) expected, (etUInt32) actual, file, line);
}

void expectEqualUInt16(etInt16 id, const char* message, etUInt16 expected, etUInt16 actual, const char* file, int line) {
	expect_equal_uint(id, message, (etUInt32) expected, (etUInt32) actual, file, line);
}

void expectEqualUInt32(etInt16 id, const char* message, etUInt32 expected, etUInt32 actual, const char* file, int line) {
	expect_equal_uint(id, message, (etUInt32) expected, (etUInt32) actual, file, line);
}

void expect_equal_void_ptr(etInt16 id, const char* message, const void* expected, const void* actual, const char* file, int line) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[16], act[16];
		snprintf(testresult,  sizeof(testresult), "%s: expected=%ld, actual=%ld", message, (etUInt32) expected, (etUInt32) actual);
		sprintf(exp, "%ld", (etUInt32) expected);
		sprintf(act, "%ld", (etUInt32) actual);
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

void expectEqualStr(etInt16 id, const char* message, const char* expected, const char* actual, const char* file, int line) {
	if (!(expected || actual || strcmp(expected, actual) == 0)) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		snprintf(testresult,  sizeof(testresult), "%s: expected=%s, actual=%s", message, expected, actual);
		etUnit_handleExpect(id, ET_FALSE, testresult, expected, actual, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

#ifdef ET_FLOAT32
void expectEqualFloat32(etInt16 id, const char* message, etFloat32 expected, etFloat32 actual, etFloat32 precision, const char* file, int line) {
	expect_equal_float(id, message, expected, actual, precision, file, line);
}

void expectRangeFloat32(etInt16 id, const char* message, etFloat32 min, etFloat32 max, etFloat32 actual, const char* file, int line) {
	expect_range_float(id, message, min, max, actual, file, line);
}
#endif

#ifdef ET_FLOAT64
void expectEqualFloat64(etInt16 id, const char* message, etFloat64 expected, etFloat64 actual, etFloat64 precision, const char* file, int line) {
	expect_equal_float(id, message, expected, actual, precision, file, line);
}

void expectRangeFloat64(etInt16 id, const char* message, etFloat64 min, etFloat64 max, etFloat64 actual, const char* file, int line) {
	expect_range_float(id, message, min, max, actual, file, line);
}
#endif

void expectOrderStart(etInt16 id, etInt16* list, etInt16 size, const char* file, int line) {
	int i;
	for (i = 0; i < ETUNIT_ORDER_MAX; ++i)
		if (etUnit_orderInfo[i].id == 0) {
			etUnit_orderInfo[i].id = id;
			etUnit_orderInfo[i].currentIndex = 0;
			etUnit_orderInfo[i].size = size;
			etUnit_orderInfo[i].list = list;
			break;
		}
}

void expectOrder(etInt16 id, const char* message, etInt16 identifier, const char* file, int line) {
	OrderInfo* info = getOrderInfo(id);
	if (info != NULL) {
		if (info->currentIndex < info->size) {
			if (info->list[info->currentIndex] != identifier) {
				char testresult[ETUNIT_FAILURE_TEXT_LEN];
				char exp[16], act[16];
				snprintf(testresult,  sizeof(testresult), "EXPECT_ORDER %s: index=%d, expected=%d, actual=%d", message, info->currentIndex, identifier,
						info->list[info->currentIndex]);
				sprintf(exp, "%d", identifier);
				sprintf(act, "%d", info->list[info->currentIndex]);
				etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
			} else {
				etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
				info->currentIndex++;
			}
		} else {
			char testresult[ETUNIT_FAILURE_TEXT_LEN];
			snprintf(testresult, sizeof(testresult), "EXPECT_ORDER: index(%d) is too big in %s", info->currentIndex, message);
			etUnit_handleExpect(id, ET_FALSE, testresult, NULL, NULL, file, line);
			etLogger_logInfoF("EXPECT_ORDER: index too big in %s", message);
		}
	}
}

void expectOrderEnd(etInt16 id, const char* message, etInt16 identifier, const char* file, int line) {
	OrderInfo* info = getOrderInfo(id);
	expectOrder(id, message, identifier, file, line);
	if (info->currentIndex != info->size) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		snprintf(testresult,  sizeof(testresult), "EXPECT_ORDER_END %s: wrong index at the end: expected=%d, actual=%d", message, info->size, info->currentIndex);
		etUnit_handleExpect(id, ET_FALSE, testresult, NULL, NULL, file, line);
	}
}

etBool etUnit_isSuccess(etInt16 id) {
	return etUnit_testcaseSuccess[id];
}

/* private functions */

static void expect_equal_int(etInt16 id, const char* message, etInt32 expected, etInt32 actual, const char* file, int line) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[16], act[16];
		snprintf(testresult, sizeof(testresult), "%s: expected=%ld, actual=%ld", message, expected, actual);
		sprintf(exp, "%ld", expected);
		sprintf(act, "%ld", actual);
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

/* currently not used
static void expect_range_int(etInt16 id, const char* message, etInt32 min, etInt32 max, etInt32 actual, const char* file, int line) {
	if (actual < min || actual > max) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[64], act[16];
		sprintf(testresult, "%s: min=%ld, max=%ld, actual=%ld", message, min, max, actual);
		if (actual < min) {
			sprintf(exp, ">=%ld(min)", min);
			sprintf(act, "%ld", actual);
		} else {
			sprintf(exp, "<=%ld(max)", max);
			sprintf(act, "%ld", actual);
		}
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}
*/

static void expect_equal_uint(etInt16 id, const char* message, etUInt32 expected, etUInt32 actual, const char* file, int line) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[16], act[16];
		snprintf(testresult, sizeof(testresult), "%s: expected=%lu, actual=%lu", message, expected, actual);
		sprintf(exp, "%lu", expected);
		sprintf(act, "%lu", actual);
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

/* currently not used
static void expect_range_uint(etInt16 id, const char* message, etUInt32 min, etUInt32 max, etUInt32 actual, const char* file, int line) {
	if (actual < min || actual > max) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[64], act[16];
		sprintf(testresult, "%s: min=%lu, max=%lu, actual=%lu", message, min, max, actual);
		if (actual < min) {
			sprintf(exp, ">=%lu(min)", min);
			sprintf(act, "%lu", actual);
		} else {
			sprintf(exp, "<=%lu(max)", max);
			sprintf(act, "%lu", actual);
		}
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}
*/

#ifdef ETUNIT_FLOAT
static void expect_equal_float(etInt16 id, const char* message, etUnitFloat expected, etUnitFloat actual, etUnitFloat precision, const char* file, int line) {
	if (expected - actual < -precision || expected - actual > precision) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[16], act[16];
		snprintf(testresult, sizeof(testresult), "%s: expected=%f, actual=%f", message, expected, actual);
		sprintf(exp, "%f", expected);
		sprintf(act, "%f", actual);
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}

static void expect_range_float(etInt16 id, const char* message, etUnitFloat min, etUnitFloat max, etUnitFloat actual, const char* file, int line) {
	if (actual < min || actual > max) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		char exp[64], act[16];
		snprintf(testresult, sizeof(testresult), "%s: min=%f, max=%f, actual=%f", message, min, max, actual);
		if (actual < min) {
			sprintf(exp, ">=%f(min)", min);
			sprintf(act, "%f", actual);
		} else {
			sprintf(exp, "<=%f(max)", max);
			sprintf(act, "%f", actual);
		}
		etUnit_handleExpect(id, ET_FALSE, testresult, exp, act, file, line);
	} else {
		etUnit_handleExpect(id, ET_TRUE, "", NULL, NULL, file, line);
	}
}
#endif

static void etUnit_handleExpect(etInt16 id, etBool result, const char *resulttext, const char* exp, const char* act, const char* file, int line) {
	if (result == ET_TRUE) {
		/* nothing to do because no failure */
	} else {
		etUnit_errorCounter++;
		if (etUnit_testcaseSuccess[id] == ET_TRUE) {
			/* first failure will be remembered */
			etUnit_testcaseSuccess[id] = ET_FALSE;

			if (act != NULL && exp != NULL)
				etLogger_fprintf(etUnit_reportfile, "tc fail %d: #%s#%s#%s:%d#%s\n", id, exp, act, file, line, resulttext);
			else
				etLogger_fprintf(etUnit_reportfile, "tc fail %d: ###%s:%d#%s\n", id, file, line, resulttext);
		} else {
			/* more than one error will be ignored */
		}
	}
}

Back to the top