]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4util/include/assert.h
57b654d638f2b9c20bf8b71cff333ab5e3219754
[l4.git] / l4 / pkg / l4util / include / assert.h
1 /*****************************************************************************/
2 /**
3  * \file
4  * \brief   Some useful assert-style macros.
5  *
6  * \date    09/2009
7  * \author  Bjoern Doebel <doebel@tudos.org>
8  *
9  * (c) 2009 Technische Universität Dresden
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU Lesser General Public License 2.1.
12  * Please see the COPYING-LGPL-2.1 file for details.
13  */
14
15 /*****************************************************************************/
16 #pragma once
17
18 #if !DEBUG
19
20 #define DO_NOTHING              do {} while (0)
21 #define ASSERT_VALID(c)         DO_NOTHING
22 #define ASSERT_EQUAL(a,b)       DO_NOTHING
23 #define ASSERT_NOT_EQUAL(a,b)   DO_NOTHING
24 #define ASSERT_LOWER_EQ(a,b)    DO_NOTHING
25 #define ASSERT_GREATER_EQ(a,b)  DO_NOTHING
26 #define ASSERT_BETWEEN(a,b,c)   DO_NOTHING
27 #define ASSERT_IPC_OK(i)        DO_NOTHING
28 #define ASSERT_OK(e)            DO_NOTHING
29 #define ASSERT_NOT_NULL(p)      DO_NOTHING
30 #ifndef assert
31 #define assert(cond)            DO_NOTHING
32 #endif
33
34 #else // DEBUG 
35
36 #include <stdio.h>
37 #include <assert.h>
38 #include <l4/sys/kdebug.h>
39
40 #define ASSERT_VALID(cap) \
41         do { \
42                 if (l4_is_invalid_cap(cap)) { \
43                         printf("%s: Cap invalid.\n", __func__); \
44                         assert(l4_is_invalid_cap(cap)); \
45                 } \
46         } while (0)
47
48
49 #define ASSERT_EQUAL(a, b) \
50         do { \
51                 if ((a) != (b)) { \
52                         printf("%s: "#a" (%lx) != "#b" (%lx)\n", __func__, (unsigned long)(a), (unsigned long)(b)); \
53                         assert((a)==(b)); \
54                 } \
55         } while (0)
56
57
58 #define ASSERT_NOT_EQUAL(a, b) \
59         do { \
60                 if ((a) == (b)) { \
61                         printf("%s: "#a" (%lx) == "#b" (%lx)\n", __func__, (unsigned long)(a), (unsigned long)(b)); \
62                         assert((a)!=(b)); \
63                 } \
64         } while (0)
65
66
67 #define ASSERT_LOWER_EQ(val, max) \
68         do { \
69                 if ((val) > (max)) { \
70                         printf("%s: "#val" (%lx) > "#max" (%lx)\n", __func__, (unsigned long)(val), (unsigned long)(max)); \
71                         assert((val)<=(max)); \
72                 } \
73         } while (0)
74
75
76 #define ASSERT_GREATER_EQ(val, min) \
77         do { \
78                 if ((val) < (min)) { \
79                         printf("%s: "#val" (%lx) < "#min" (%lx)\n", __func__, (unsigned long)(val), (unsigned long)(min)); \
80                         assert((val)>=(min)); \
81                 } \
82         } while (0)
83
84
85 #define ASSERT_BETWEEN(val, min, max) \
86         ASSERT_LOWER_EQ((val), (max)); \
87     ASSERT_GREATER_EQ((val), (min));
88
89
90 #define ASSERT_IPC_OK(msgtag) \
91         do { \
92                 if (l4_ipc_error((msgtag), l4_utcb())) { \
93                         printf("%s: IPC Error: %lx\n", __func__, l4_ipc_error(msgtag)); \
94                         assert(!l4_ipc_error(msgtag)); \
95                 } \
96         } while (0)
97
98 #define ASSERT_OK(val)         ASSERT_EQUAL((val), 0)
99 #define ASSERT_NOT_NULL(ptr)   ASSERT_NOT_EQUAL((ptr), NULL)
100
101 #endif // DEBUG