summaryrefslogtreecommitdiffstats
blob: 98acdf003ba1ed353bcc00457ed32884773edddc (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
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
/*******************************************************************************
 * 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)
 *
 *******************************************************************************/

/**
 * \file etUnit.h
 *
 * a collection of methods for unit testing. The component uses a very simple file output format
 * which is translated into JUnit xml format by a separate tool written in Java and also part of the
 * eTrice project
 *
 * \author Thomas Schuetz
 */

#ifndef _ETUNIT_H_
#define _ETUNIT_H_

#include "etDatatypes.h"


/**
 * opens a file to protocol the test results
 *
 * \param testResultPath the file path
 * \param testFileName the file name
 */
void etUnit_open(const char* testResultPath, const char* testFileName);
/**
 * closes the protocol file
 */
void etUnit_close(void);
/**
 * opens a test suite which is a collection of test cases
 *
 * \param testSuiteName the name of the suite
 */
void etUnit_openTestSuite(const char* testSuiteName);
/**
 * closes the currently open test suite
 */
void etUnit_closeTestSuite(void);
/**
 * opens a test case. Multiple test cases can be open at a time
 *
 * \param testCaseName the name of the test case
 *
 * \return an id for the test case which has to be used as an identifier
 * for the expect... and other methods
 */
etInt16 etUnit_openTestCase(const char* testCaseName);
/**
 * closes a test case
 *
 * \param id the test case id
 */
void etUnit_closeTestCase(etInt16 id);
/**
 * indicate success of a test case
 *
 * \param id the test case id
 */
etBool etUnit_isSuccess(etInt16 id);
/**
 * releases the \ref etRuntime_getTerminateSemaphore() and thus makes the program terminate
 *
 * \param id (unused)
 */
void etUnit_testFinished(etInt16 id);

/* functions for more convenience for model and generator tests */

/**
 * opens a file, test suite and test case
 *
 * \param testResultPath the file path
 * \param testFileName the file name
 * \param testSuiteName the name of the suite
 * \param testCaseName the name of the test case
 *
 * \return an id for the test case which has to be used as an identifier
 * for the expect... and other methods
 */
etInt16 etUnit_openAll(const char* testResultPath, const char* testFileName, const char* testSuiteName, const char* testCaseName);
/**
 * closes test case, suite and file
 *
 * \param id the test case id
 */
void etUnit_closeAll(etInt16 id);

/*
 * boolean values
 */

/**
 * calls \ref expectTrue()
 */
#define EXPECT_TRUE(id, msg, condition)		expectTrue(id, msg, condition, __FILE__, __LINE__)
/** calls \ref expectFalse() */
#define EXPECT_FALSE(id, msg, condition)	expectFalse(id, msg, condition, __FILE__, __LINE__)

/*
 * signed integer values
 */
/** calls \ref expectEqualInt8() */
#define EXPECT_EQUAL_INT8(id, msg, expected, actual)		expectEqualInt8(id, msg, expected, actual, __FILE__, __LINE__)
/** calls \ref expectEqualInt16() */
#define EXPECT_EQUAL_INT16(id, msg, expected, actual)		expectEqualInt16(id, msg, expected, actual, __FILE__, __LINE__)
/** calls \ref expectEqualInt32() */
#define EXPECT_EQUAL_INT32(id, msg, expected, actual)		expectEqualInt32(id, msg, expected, actual, __FILE__, __LINE__)

/*
 * unsigned integer values
 */
/** calls \ref expectEqualUInt8() */
#define EXPECT_EQUAL_UINT8(id, msg, expected, actual)		expectEqualUInt8(id, msg, expected, actual, __FILE__, __LINE__)
/** calls \ref expectEqualUInt16() */
#define EXPECT_EQUAL_UINT16(id, msg, expected, actual)		expectEqualUInt16(id, msg, expected, actual, __FILE__, __LINE__)
/** calls \ref expectEqualUInt32() */
#define EXPECT_EQUAL_UINT32(id, msg, expected, actual)		expectEqualUInt32(id, msg, expected, actual, __FILE__, __LINE__)

/*
 * float values
 */
/** calls \ref expectEqualFloat32() */
#define EXPECT_EQUAL_FLOAT32(id, msg, expected, actual, precision)		expectEqualFloat32(id, msg, expected, actual, precision, __FILE__, __LINE__)
/** calls \ref expectEqualFloat64() */
#define EXPECT_EQUAL_FLOAT64(id, msg, expected, actual, precision)		expectEqualFloat64(id, msg, expected, actual, precision, __FILE__, __LINE__)

/*
 * Pointers
 */
/** calls \ref expect_equal_void_ptr() */
#define EXPECT_EQUAL_PTR(id, msg, expected, actual) \
	expect_equal_void_ptr(id, msg, (const void*) expected, (const void*) actual, __FILE__, __LINE__)


/*
 * more specialized functions
 */
/** calls \ref expectOrderStart() */
#define EXPECT_ORDER_START(id, list, size)		expectOrderStart(id, list, size, __FILE__, __LINE__)
/** calls \ref expectOrder() */
#define EXPECT_ORDER(id, msg, val)				expectOrder(id, msg, val, __FILE__, __LINE__)
/** calls \ref expectOrderEnd() */
#define EXPECT_ORDER_END(id, msg, val)			expectOrderEnd(id, msg, val, __FILE__, __LINE__)


/*
 * Helpers for adding test cases
 */

/**
 * code block with calls \ref etUnit_openTestCase() followed by a call to the passed
 * test case followed by a call to \ref etUnit_closeTestCase
 *
 * \param testcase the name of a test case method
 */
#define ADD_TESTCASE(testcase) \
	{ etInt16 id = etUnit_openTestCase(#testcase); \
	testcase(id); \
	etUnit_closeTestCase(id);}

/*
 * function prototypes, use above macros to call them
 */

/**
 * reports an error if the condition is <code>false</code>
 *
 * \param id the test case id
 * \param msg the result message
 * \param condition the condition that is expected to be <code>true</code>
 * \param file the file name with the test case
 * \param line the line
 */
void expectTrue(etInt16 id, const char* msg, etBool condition, const char* file, int line);
/**
 * reports an error if the condition is <code>true</code>
 *
 * \param id the test case id
 * \param msg the result message
 * \param condition the condition that is expected to be <code>false</code>
 * \param file the file name with the test case
 * \param line the line
 */
void expectFalse(etInt16 id, const char* msg, etBool condition, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualInt8(etInt16 id, const char* msg, etInt8 expected, etInt8 actual, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualInt16(etInt16 id, const char* msg, etInt16 expected, etInt16 actual, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualInt32(etInt16 id, const char* msg, etInt32 expected, etInt32 actual, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualUInt8(etInt16 id, const char* msg, etUInt8 expected, etUInt8 actual, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualUInt16(etInt16 id, const char* msg, etUInt16 expected, etUInt16 actual, const char* file, int line);
/**
 * reports an error if two integers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualUInt32(etInt16 id, const char* msg, etUInt32 expected, etUInt32 actual, const char* file, int line);
/**
 * reports an error if two floats aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualFloat32(etInt16 id, const char* msg, etFloat32 expected, etFloat32 actual, etFloat32 precision, const char* file, int line);
/**
 * reports an error if two floats aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectEqualFloat64(etInt16 id, const char* msg, etFloat64 expected, etFloat64 actual, etFloat64 precision, const char* file, int line);
/**
 * reports an error if two pointers aren't equal
 *
 * \param id the test case id
 * \param msg the result message
 * \param expected the expected value
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expect_equal_void_ptr(etInt16 id, const char* msg, const void* expected, const void* actual, const char* file, int line);
/**
 * start of a comparison of an expected order. Initially with this method
 * a list of integers is passed. Later calls of \ref expectOrder(etInt16, const char* msg, etInt16, const char* int)
 * are compared against the next value in the list and the list pointer is incremented
 *
 * \param id the test case id
 * \param list the list of expected values
 * \param size the size of the list
 * \param file the file name with the test case
 * \param line the line
 */
void expectOrderStart(etInt16 id, etInt16* list, etInt16 size, const char* file, int line);
/**
 * reports an error if the identifier doesn't match the next value in the list
 *
 * \param id the test case id
 * \param msg the result message
 * \param identifier the expected next value
 * \param file the file name with the test case
 * \param line the line
 */
void expectOrder(etInt16 id, const char* msg, etInt16 identifier, const char* file, int line);
/**
 * reports an error if the identifier doesn't match the next value in the list which
 * is expected to be the last one
 *
 * \param id the test case id
 * \param msg the result message
 * \param identifier the expected next value
 * \param file the file name with the test case
 * \param line the line
 */
void expectOrderEnd(etInt16 id, const char* msg, etInt16 identifier, const char* file, int line);

#endif /* _ETUNIT_H_ */