Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3c23e63ad132bc431dc3b38786778cb4a550bdd0 (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
/*******************************************************************************
 * 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
 */
#ifdef __cplusplus
extern "C" {
#endif

#ifndef _ETUNIT_H_
#define _ETUNIT_H_

#include "etDatatypes.h"
#include <string.h>

// compile time evaluated
#define ETUNIT_FILENAME (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__))

/**
 * 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);
/**
 * skips a test case
 *
 * \param id the test case id
 * \param msg the message or reason
 */
void etUnit_skipTestCase(etInt16 id, const char* msg);
/**
 * 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, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectFalse() */
#define EXPECT_FALSE(id, msg, condition)	expectFalse(id, msg, condition, ETUNIT_FILENAME, __LINE__)

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

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

/*
 * float values
 */
#ifdef ET_FLOAT32
/** calls \ref expectEqualFloat32() */
#define EXPECT_EQUAL_FLOAT32(id, msg, expected, actual, precision)		expectEqualFloat32(id, msg, expected, actual, precision, ETUNIT_FILENAME, __LINE__)
#define EXPECT_RANGE_FLOAT32(id, msg, min, max, actual)		expectRangeFloat32(id, msg, min, max, actual, ETUNIT_FILENAME, __LINE__)
#endif

#ifdef ET_FLOAT64
/** calls \ref expectEqualFloat64() */
#define EXPECT_EQUAL_FLOAT64(id, msg, expected, actual, precision)		expectEqualFloat64(id, msg, expected, actual, precision, ETUNIT_FILENAME, __LINE__)
#define EXPECT_RANGE_FLOAT64(id, msg, min, max, actual)		expectRangeFloat64(id, msg, min, max, actual, ETUNIT_FILENAME, __LINE__)
#endif


/*
 * 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, ETUNIT_FILENAME, __LINE__)

/*
 *  Strings
 */
#define EXPECT_EQUAL_STR(id, msg, expected, actual) expectEqualStr(id, msg, expected, actual, ETUNIT_FILENAME, __LINE__)

/*
 * more specialized functions
 */
/** calls \ref expectOrderStart() */
#ifdef __cplusplus
#define EXPECT_ORDER_START(id, list, size)		expectOrderStart(id, list.getData(), size, ETUNIT_FILENAME, __LINE__)
#else
#define EXPECT_ORDER_START(id, list, size)		expectOrderStart(id, list, size, ETUNIT_FILENAME, __LINE__)
#endif
/** calls \ref expectOrder() */
#define EXPECT_ORDER(id, msg, val)				expectOrder(id, msg, val, ETUNIT_FILENAME, __LINE__)
/** calls \ref expectOrderEnd() */
#define EXPECT_ORDER_END(id, msg, val)			expectOrderEnd(id, msg, val, ETUNIT_FILENAME, __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 a value is not inside a range e.g. [2, 123]
 *
 * \param id the test case id
 * \param msg the result message
 * \param min the lower limit (inclusive)
 * \param max the upper limit (inclusive)
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
void expectRangeInt8(etInt16 id, const char* msg, etInt8 min, etInt8 max, 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 precision expected +/- precision defines the window for acceptance of an actual value
 * \param file the file name with the test case
 * \param line the line
 */
#ifdef ET_FLOAT32
void expectEqualFloat32(etInt16 id, const char* msg, etFloat32 expected, etFloat32 actual, etFloat32 precision, const char* file, int line);
#endif
/**
 * 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 precision expected +/- precision defines the window for acceptance of an actual value
 * \param file the file name with the test case
 * \param line the line
 */
#ifdef ET_FLOAT64
void expectEqualFloat64(etInt16 id, const char* msg, etFloat64 expected, etFloat64 actual, etFloat64 precision, const char* file, int line);
#endif

/**
 * reports an error if a value is not inside a range e.g. [-5.1, +3.0]
 *
 * \param id the test case id
 * \param msg the result message
 * \param min the lower limit (inclusive)
 * \param max the upper limit (inclusive)
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
#ifdef ET_FLOAT32
void expectRangeFloat32(etInt16 id, const char* message, etFloat32 min, etFloat32 max, etFloat32 actual, const char* file, int line) ;
#endif

/**
 * reports an error if a value is not inside a range e.g. [-5.1, +3.0]
 *
 * \param id the test case id
 * \param msg the result message
 * \param min the lower limit (inclusive)
 * \param max the upper limit (inclusive)
 * \param actual the actual value
 * \param file the file name with the test case
 * \param line the line
 */
#ifdef ET_FLOAT64
void expectRangeFloat64(etInt16 id, const char* message, etFloat64 min, etFloat64 max, etFloat64 actual, const char* file, int line) ;
#endif

/**
 * 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);

/**
 * reports an error if two strings 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 expectEqualStr(etInt16 id, const char* msg, const char* expected, const char* 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_ */

#ifdef __cplusplus
}
#endif

Back to the top