Listing 1: Embedded systems-friendly assertions
1: #ifndef qassert_h
2: #define qassert_h
3:
4: /** NASSERT macro disables all contract validations
5: * (assertions, preconditions, postconditions, and invariants).
6: */
7: #ifdef NASSERT /* NASSERT defined--DbC disabled */
8:
9: #define DEFINE_THIS_FILE
10: #define ASSERT(ignore_) ((void)0)
11: #define ALLEGE(test_) ((void)(test_))
12:
13: #else /* NASSERT not defined--DbC enabled */
14:
15: #ifdef __cplusplus
16: extern "C" {
17: #endif
18: /* callback invoked in case of assertion failure */
19: void onAssert__(char const *file, unsigned line);
20:
21: #ifdef __cplusplus
22: }
23: #endif
24:
25: #define DEFINE_THIS_FILE \
26: static char const THIS_FILE__[] = __FILE__
27:
28: #define ASSERT(test_) \
29: ((test_) ? (void)0 : onAssert__(THIS_FILE__, __LINE__))
30:
31: #define ALLEGE(test_) ASSERT(test_)
32:
33: #endif /* NASSERT */
34:
35: #define REQUIRE(test_) ASSERT(test_)
36: #define ENSURE(test_) ASSERT(test_)
37: #define INVARIANT(test_) ASSERT(test_)
38:
39: #endif /* qassert_h */