L4Re - L4 Runtime Environment
assert.h
Go to the documentation of this file.
1 /*****************************************************************************/
9 /*
10  * (c) 2009 Author(s)
11  * economic rights: Technische Universit├Ąt Dresden (Germany)
12  * This file is part of TUD:OS and distributed under the terms of the
13  * GNU Lesser General Public License 2.1.
14  * Please see the COPYING-LGPL-2.1 file for details.
15  */
16 
17 /*****************************************************************************/
18 #pragma once
19 
20 #ifdef NDEBUG
21 
22 #define DO_NOTHING do {} while (0)
23 #define ASSERT_VALID(c) DO_NOTHING
24 #define ASSERT_EQUAL(a,b) DO_NOTHING
25 #define ASSERT_NOT_EQUAL(a,b) DO_NOTHING
26 #define ASSERT_LOWER_EQ(a,b) DO_NOTHING
27 #define ASSERT_GREATER_EQ(a,b) DO_NOTHING
28 #define ASSERT_BETWEEN(a,b,c) DO_NOTHING
29 #define ASSERT_IPC_OK(i) DO_NOTHING
30 #define ASSERT_OK(e) do { (void)e; } while (0)
31 #define ASSERT_NOT_NULL(p) DO_NOTHING
32 #ifndef assert
33 #define assert(cond) DO_NOTHING
34 #endif
35 
36 #else // NDEBUG
37 
38 #ifndef ASSERT_PRINTF
39 #include <stdio.h>
40 #define ASSERT_PRINTF printf
41 #endif
42 #ifndef ASSERT_ASSERT
43 #include <assert.h>
44 #define ASSERT_ASSERT(x) assert(x)
45 #endif
46 
47 #define ASSERT_VALID(cap) \
48  do { \
49  typeof(cap) _cap = cap; \
50  if (l4_is_invalid_cap(_cap)) { \
51  ASSERT_PRINTF("%s: Cap invalid.\n", __func__); \
52  ASSERT_ASSERT(!l4_is_invalid_cap(_cap)); \
53  } \
54  } while (0)
55 
56 
57 #define ASSERT_EQUAL(a, b) \
58  do { \
59  typeof(a) _a = a; \
60  typeof(b) _b = b; \
61  if (_a != _b) { \
62  ASSERT_PRINTF("%s:\n", __func__); \
63  ASSERT_PRINTF(" "#a" (%lx) != "#b" (%lx)\n", (unsigned long)_a, (unsigned long)_b); \
64  ASSERT_ASSERT(_a == _b); \
65  } \
66  } while (0)
67 
68 
69 #define ASSERT_NOT_EQUAL(a, b) \
70  do { \
71  typeof(a) _a = a; \
72  typeof(b) _b = b; \
73  if (_a == _b) { \
74  ASSERT_PRINTF("%s:\n", __func__); \
75  ASSERT_PRINTF(" "#a" (%lx) == "#b" (%lx)\n", (unsigned long)_a, (unsigned long)_b); \
76  ASSERT_ASSERT(_a != _b); \
77  } \
78  } while (0)
79 
80 
81 #define ASSERT_LOWER_EQ(val, max) \
82  do { \
83  typeof(val) _val = val; \
84  typeof(max) _max = max; \
85  if (_val > _max) { \
86  ASSERT_PRINTF("%s:\n", __func__); \
87  ASSERT_PRINTF(" "#val" (%lx) > "#max" (%lx)\n", (unsigned long)_val, (unsigned long)_max); \
88  ASSERT_ASSERT(_val <= _max); \
89  } \
90  } while (0)
91 
92 
93 #define ASSERT_GREATER_EQ(val, min) \
94  do { \
95  typeof(val) _val = val; \
96  typeof(min) _min = min; \
97  if (_val < _min) { \
98  ASSERT_PRINTF("%s:\n", __func__); \
99  ASSERT_PRINTF(" "#val" (%lx) < "#min" (%lx)\n", (unsigned long)_val, (unsigned long)_min); \
100  ASSERT_ASSERT(_val >= _min); \
101  } \
102  } while (0)
103 
104 
105 #define ASSERT_BETWEEN(val, min, max) \
106  ASSERT_LOWER_EQ((val), (max)); \
107  ASSERT_GREATER_EQ((val), (min));
108 
109 
110 #define ASSERT_IPC_OK(msgtag) \
111  do { \
112  int _r = l4_ipc_error(msgtag, l4_utcb()); \
113  if (_r) { \
114  ASSERT_PRINTF("%s: IPC Error: %lx\n", __func__, _r); \
115  ASSERT_ASSERT(_r == 0); \
116  } \
117  } while (0)
118 
119 #define ASSERT_OK(val) ASSERT_EQUAL((val), 0)
120 #define ASSERT_NOT_NULL(ptr) ASSERT_NOT_EQUAL((ptr), (void *)0)
121 
122 #endif // NDEBUG
Some useful assert-style macros.