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

/*** member variables */

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

/* names */
static char* RUnit_TestFileName = NULL;
static char* RUnit_TestResultPath = NULL;

static char* RUnit_TestSuiteName = NULL;
static char* RUnit_TestCaseName = NULL;

/* counters */
static etInt32 RUnit_passCount = 0;
static etInt32 RUnit_failCount = 0;

static boool etUnit_testcaseSuccess = TRUE;

#define ETUNIT_FAILURE_TEXT_LEN 256

static char etUnit_failureText[ETUNIT_FAILURE_TEXT_LEN];

/* time measuring */
static clock_t RUnit_startTime = 0;
static clock_t RUnit_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, float64 expected, float64 actual, float64 precision);
void RUnit_writeTestLog(const char *testcase, boool result, const char *resulttext);
void etUnit_handleExpect(boool result, const char *resulttext);

/* public functions */

void RUnit_open(char* testResultPath, char* testFileName) {
	RUnit_passCount = 0;
	RUnit_failCount = 0;
	strcpy(etUnit_failureText,"");

	RUnit_TestFileName = testFileName;
	RUnit_TestResultPath = testResultPath;

	printf("************* TEST START (%s) **************\n", RUnit_TestFileName);

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

	if (RUnit_reportfile == NULL) {
		RUnit_reportfile = fopen(filename, "w+");
		if (RUnit_reportfile != NULL) {
			fprintf(RUnit_reportfile, "<testsuites name=\"%s\" tests=\"0\" failures=\"0\" errors=\"0\" time=\"0\">\n",
					RUnit_TestFileName);
		} else {
			printf("Unable to open file %s/%s.xml\n", RUnit_TestResultPath, RUnit_TestFileName);
		}
	}
	// prepare time measurement
	RUnit_startTime = clock();
	RUnit_currentTime = clock();
	printf("Start Time: %ld\n", RUnit_startTime);

}

void RUnit_close(void) {
	printf("\n");
	if (RUnit_failCount > 0) {
		printf("************* TEST FAILED *************\n");
	} else {
		printf("************* TEST PASSED *************\n");
	}
	printf("Number of Tests: %ld\n", RUnit_failCount + RUnit_passCount);
	printf("Failed: %ld\n", RUnit_failCount);
	printf("Passed: %ld\n", RUnit_passCount);
	printf("Total Time: %ld\n", clock() - RUnit_startTime);
	printf("End Time: %ld, CLOCKS_PER_SEC: %ld\n", clock(), CLOCKS_PER_SEC);
	printf("***************************************\n");

	if (RUnit_reportfile != NULL) {
		fprintf(RUnit_reportfile, "</testsuites>\n");
		fclose(RUnit_reportfile);
		RUnit_reportfile = NULL;
	}
}

void RUnit_openTestSuite(char* testSuiteName) {
	RUnit_TestSuiteName = testSuiteName;
	if (RUnit_reportfile != NULL) {
		fprintf(RUnit_reportfile, "\t<testsuite name=\"%s\" tests=\"0\" failures=\"0\" errors=\"0\" time=\"0\">\n",
				RUnit_TestSuiteName);
	}
}

void RUnit_closeTestSuite(void) {
	if (RUnit_reportfile != NULL) {
		fprintf(RUnit_reportfile, "\t</testsuite>\n");
	}
}

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

void RUnit_closeTestCase(void) {
	if (RUnit_reportfile != NULL && RUnit_TestSuiteName != NULL) {
		RUnit_writeTestLog(RUnit_TestCaseName, etUnit_testcaseSuccess, etUnit_failureText);
	}
}

void EXPECT_TRUE(const char* message, boool 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, boool 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, float32 expected, float32 actual, float32 precision) {
	expect_equal_float(message, (float64) expected, (float64) actual, (float64) precision);
}

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


/* 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, float64 expected, float64 actual, float64 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(boool 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 RUnit_buildTestLogXML(char* xml, const char *testcase, boool 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<failure>%s</failure>\n\t</testcase>\n",
				testcase, time, resulttext);
	}
}

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

	// counting
	if (result == TRUE) {
		RUnit_passCount++;
		printf("PASS: %s: %s\n", testcase, resulttext);
	} else {
		RUnit_failCount++;
		printf("FAIL: %s: %s\n", testcase, resulttext);
	}

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

	// writing to file
	if (RUnit_reportfile != NULL) {
		RUnit_buildTestLogXML(writeBuffer, testcase, result, resulttext, time);
		fprintf(RUnit_reportfile, writeBuffer);
	}
}

Back to the top