Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9a963664f14aba30ad2631991ed812a1c9c0c75 (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
/*******************************************************************************
 * 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.h"
#include <string.h>
#include <time.h>
#include "etLogger.h"


/*** member variables */

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

/* names */
static char* etUnit_TestFileName = NULL;
static char* etUnit_TestResultPath = NULL;

static char* etUnit_TestSuiteName = NULL;
static char* etUnit_TestCaseName = NULL;

/* counters */
static etInt32 etUnit_passCount = 0;
static etInt32 etUnit_failCount = 0;
static etInt32 etUnit_passCountTotal = 0;
static etInt32 etUnit_failCountTotal = 0;

static etBool etUnit_testcaseSuccess = TRUE;

#define ETUNIT_FAILURE_TEXT_LEN 256

static char etUnit_failureText[ETUNIT_FAILURE_TEXT_LEN];

/* time measuring */
static clock_t etUnit_startTime = 0;
static clock_t etUnit_currentTime = 0;

/* forward declarations of private functions */
void expect_equal_int(const char* message, etInt32 expected, etInt32 actual);
void expect_equal_uint(const char* message, etUInt32 expected, etUInt32 actual);
void expect_equal_float(const char* message, etFloat64 expected, etFloat64 actual, etFloat64 precision);
void etUnit_writeTestLog(const char *testcase, etBool result, const char *resulttext);
void etUnit_handleExpect(etBool result, const char *resulttext);

/* public functions */

void etUnit_open(char* testResultPath, char* testFileName) {
	etUnit_passCount = 0;
	etUnit_failCount = 0;
	strcpy(etUnit_failureText,"");

	etUnit_TestFileName = testFileName;
	etUnit_TestResultPath = testResultPath;

	etLogger_logInfoF("************* TEST START (%s) **************", etUnit_TestFileName);

	char filename[ETUNIT_FAILURE_TEXT_LEN];
	sprintf(filename, "%s/%s.xml", etUnit_TestResultPath, etUnit_TestFileName);

	if (etUnit_reportfile == NULL) {
		etUnit_reportfile = etLogger_fopen(filename, "w+");
		if (etUnit_reportfile != NULL) {
			etLogger_fprintf(etUnit_reportfile, "<testsuites name=\"%s\" tests=\"0\" failures=\"0\" errors=\"0\" time=\"0\">\n", etUnit_TestFileName);
		} else {
			etLogger_logErrorF("Unable to open file %s/%s.xml", etUnit_TestResultPath, etUnit_TestFileName);
		}
	}
	/* prepare time measurement */
	etUnit_startTime = clock();
	etUnit_currentTime = clock();
	etLogger_logInfoF("Start Time: %ld", etUnit_startTime);

}

void etUnit_close(void) {
	etLogger_logInfoF("");
	if (etUnit_failCount > 0) {
		etLogger_logInfoF("************* TEST FAILED *************");
	} else {
		etLogger_logInfoF("************* TEST PASSED *************");
	}
	etLogger_logInfoF("Number of Tests: %ld", etUnit_failCount + etUnit_passCount);
	etLogger_logInfoF("Failed: %ld", etUnit_failCount);
	etLogger_logInfoF("Passed: %ld", etUnit_passCount);
	etLogger_logInfoF("Time: %ld", clock() - etUnit_startTime);
	etLogger_logInfoF("End Time: %ld[ms]", (clock() / 1000) * CLOCKS_PER_SEC );
	etLogger_logInfoF("***************************************");
	if (etUnit_failCountTotal > 0) {
		etLogger_logInfoF("   ******* %d TEST(S) FAILED *****", etUnit_failCountTotal);
	} else {
		etLogger_logInfoF("   ******* ALL TESTS PASSED ******");
	}
	etLogger_logInfoF("***************************************");

	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "</testsuites>\n");
		etLogger_fclose(etUnit_reportfile);
		etUnit_reportfile = NULL;
	}
}

void etUnit_openTestSuite(char* testSuiteName) {
	etUnit_TestSuiteName = testSuiteName;
	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "\t<testsuite name=\"%s\" tests=\"0\" failures=\"0\" errors=\"0\" time=\"0\">\n",
				etUnit_TestSuiteName);
	}
}

void etUnit_closeTestSuite(void) {
	if (etUnit_reportfile != NULL) {
		etLogger_fprintf(etUnit_reportfile, "\t</testsuite>\n");
	}
}

void etUnit_openTestCase(char* testCaseName) {
	etUnit_TestCaseName = testCaseName;
	etUnit_testcaseSuccess = TRUE;
	strcpy(etUnit_failureText,"");
}

void etUnit_closeTestCase(void) {
	if (etUnit_reportfile != NULL && etUnit_TestSuiteName != NULL) {
		etUnit_writeTestLog(etUnit_TestCaseName, etUnit_testcaseSuccess, etUnit_failureText);
	}
}

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

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

void EXPECT_TRUE(const char* message, etBool condition) {
	if (condition == FALSE) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: *** EXPECT_TRUE == FALSE", message);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}

void EXPECT_FALSE(const char* message, etBool condition) {
	if (condition == TRUE) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: EXPECT_FALSE == TRUE", message);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}

void EXPECT_EQUAL_INT8(const char* message, etInt8 expected, etInt8 actual) {
	expect_equal_int(message, (etInt32) expected, (etInt32) actual);
}

void EXPECT_EQUAL_INT16(const char* message, etInt16 expected, etInt16 actual) {
	expect_equal_int(message, (etInt32) expected, (etInt32) actual);
}

void EXPECT_EQUAL_INT32(const char* message, etInt32 expected, etInt32 actual) {
	expect_equal_int(message, (etInt32) expected, (etInt32) actual);
}

void EXPECT_EQUAL_UINT8(const char* message, etUInt8 expected, etUInt8 actual) {
	expect_equal_uint(message, (etUInt32) expected, (etUInt32) actual);
}

void EXPECT_EQUAL_UINT16(const char* message, etUInt16 expected, etUInt16 actual) {
	expect_equal_uint(message, (etUInt32) expected, (etUInt32) actual);
}

void EXPECT_EQUAL_UINT32(const char* message, etUInt32 expected, etUInt32 actual) {
	expect_equal_uint(message, (etUInt32) expected, (etUInt32) actual);
}


void EXPECT_EQUAL_FLOAT32(const char* message, etFloat32 expected, etFloat32 actual, etFloat32 precision) {
	expect_equal_float(message, (etFloat64) expected, (etFloat64) actual, (etFloat64) precision);
}

void EXPECT_EQUAL_FLOAT64(const char* message, etFloat64 expected, etFloat64 actual, etFloat64 precision) {
	expect_equal_float(message, (etFloat64) expected, (etFloat64) actual, (etFloat64) precision);
}

/* order */
static etInt16 etUnit_orderCurrentIndex = 0;
static etInt16 etUnit_orderSize = 0;
static etInt16* etUnit_orderList = NULL;

void EXPECT_ORDER_START(etInt16* list, etInt16 size){
	etUnit_orderCurrentIndex = 0;
	etUnit_orderSize = size;
	etUnit_orderList = list;
}

void EXPECT_ORDER(const char* message, etInt16 identifier){
	if (etUnit_orderCurrentIndex < etUnit_orderSize) {
		if (etUnit_orderList[etUnit_orderCurrentIndex] != identifier){
			char testresult[ETUNIT_FAILURE_TEXT_LEN];
			sprintf(testresult, "EXPECT_ORDER %s: index=%d, expected=%d, actual=%d", message, etUnit_orderCurrentIndex, identifier, etUnit_orderList[etUnit_orderCurrentIndex]);
			etUnit_handleExpect(FALSE, testresult);
		}
		else {
			etUnit_handleExpect(TRUE, "");
			etUnit_orderCurrentIndex++;
		}
	}
	else {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "EXPECT_ORDER: index(%d) is too big in %s", etUnit_orderCurrentIndex, message);
		etUnit_handleExpect(FALSE, testresult);
		etLogger_logInfoF("EXPECT_ORDER: index too big in %s", message);
	}
}

void EXPECT_ORDER_END(const char* message, etInt16 identifier){
	EXPECT_ORDER(message, identifier);
	if (etUnit_orderCurrentIndex != etUnit_orderSize){
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "EXPECT_ORDER_END %s: wrong index at the end: expected=%d, actual=%d", message, etUnit_orderSize, etUnit_orderCurrentIndex);
		etUnit_handleExpect(FALSE, testresult);
	}
}

/* private functions */

void expect_equal_int(const char* message, etInt32 expected, etInt32 actual) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: expected=%ld, actual=%ld", message, expected, actual);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}

void expect_equal_uint(const char* message, etUInt32 expected, etUInt32 actual) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: expected=%lu, actual=%lu", message, expected, actual);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}


void expect_equal_float(const char* message, etFloat64 expected, etFloat64 actual, etFloat64 precision) {
	if (expected - actual < -precision || expected - actual > precision) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: expected=%lf, actual=%lf", message, expected, actual);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}

void expect_equal_void_ptr(const char* message, const void* expected, const void* actual) {
	if (expected != actual) {
		char testresult[ETUNIT_FAILURE_TEXT_LEN];
		sprintf(testresult, "%s: expected=%ld, actual=%ld", message, (etUInt32) expected, (etUInt32) actual);
		etUnit_handleExpect(FALSE, testresult);
	} else {
		etUnit_handleExpect(TRUE, "");
	}
}


//_________

void etUnit_handleExpect(etBool result, const char *resulttext){
	if (result == TRUE) {
		/* nothing to do because no failure */
	}
	else {
		if (etUnit_testcaseSuccess == TRUE){
			/* first failure will be remembered */
			etUnit_testcaseSuccess = FALSE;
			strcpy(etUnit_failureText, resulttext);
		}
		else{
			/* more than one error will be ignored */
		}
	}
}

void etUnit_buildTestLogXML(char* xml, const char *testcase, etBool result, const char *resulttext, clock_t time) {
	if (result == TRUE) {
		sprintf(xml, "\t\t<testcase name=\"%s\" time=\"%ld\"/>\n", testcase, time);
	} else {
		sprintf(
				xml,
				"\t\t<testcase name=\"%s\" classname=\"none\" time=\"%ld\">\n\t\t\t<failure>%s</failure>\n\t\t</testcase>\n",
				testcase, time, resulttext);
	}
}

void etUnit_writeTestLog(const char *testcase, etBool result, const char *resulttext) {
	char writeBuffer[ETUNIT_FAILURE_TEXT_LEN];

	// counting
	if (result == TRUE) {
		etUnit_passCount++;
		etUnit_passCountTotal++;
		etLogger_logInfoF("PASS: %s %s", testcase, resulttext);
	} else {
		etUnit_failCount++;
		etUnit_failCountTotal++;
		etLogger_logInfoF("FAIL: %s : %s", testcase, resulttext);
	}

	clock_t time = clock() - etUnit_currentTime;
	etUnit_currentTime = clock();

	// writing to file
	if (etUnit_reportfile != NULL) {
		etUnit_buildTestLogXML(writeBuffer, testcase, result, resulttext, time);
		etLogger_fprintf(etUnit_reportfile, writeBuffer);
	}
}

Back to the top