suricata
util-unittest.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Breno Silva <breno.silva@gmail.com>
23  *
24  * Unit test framework
25  */
26 
27 /**
28  * \addtogroup Testing
29  *
30  * @{
31  */
32 
33 #ifndef __UTIL_UNITTEST_H__
34 #define __UTIL_UNITTEST_H__
35 
36 #ifdef UNITTESTS
37 
38 typedef struct UtTest_
39 {
40  const char *name;
41  int(*TestFn)(void);
42 
43  struct UtTest_ *next;
44 
45 } UtTest;
46 
47 void UtRegisterTest(const char *name, int(*TestFn)(void));
48 uint32_t UtRunTests(const char *regex_arg);
49 void UtInitialize(void);
50 void UtCleanup(void);
51 int UtRunSelftest (const char *regex_arg);
52 void UtListTests(const char *regex_arg);
53 void UtRunModeRegister(void);
54 
55 extern int unittests_fatal;
56 
57 /**
58  * \breif Fail a test.
59  */
60 #define FAIL do { \
61  if (unittests_fatal) { \
62  BUG_ON(1); \
63  } else { \
64  return 0; \
65  } \
66  } while (0)
67 
68 /**
69  * \brief Fail a test if expression evaluates to false.
70  */
71 #define FAIL_IF(expr) do { \
72  if (unittests_fatal) { \
73  BUG_ON(expr); \
74  } else if (expr) { \
75  return 0; \
76  } \
77  } while (0)
78 
79 /**
80  * \brief Fail a test if expression to true.
81  */
82 #define FAIL_IF_NOT(expr) do { \
83  FAIL_IF(!(expr)); \
84  } while (0)
85 
86 /**
87  * \brief Fail a test if expression evaluates to NULL.
88  */
89 #define FAIL_IF_NULL(expr) do { \
90  FAIL_IF(NULL == expr); \
91  } while (0)
92 
93 /**
94  * \brief Fail a test if expression evaluates to non-NULL.
95  */
96 #define FAIL_IF_NOT_NULL(expr) do { \
97  FAIL_IF(NULL != expr); \
98  } while (0)
99 
100 /**
101  * \brief Pass the test.
102  *
103  * Only to be used at the end of a function instead instead of "return 1."
104  */
105 #define PASS do { \
106  return 1; \
107  } while (0)
108 
109 #endif
110 
111 /**
112  * \brief Pass the test if expression evaluates to true.
113  *
114  * Only to be used at the end of a function instead of returning the
115  * result of an expression.
116  */
117 #define PASS_IF(expr) do { \
118  FAIL_IF(!(expr)); \
119  PASS; \
120  } while (0)
121 
122 #endif /* __UTIL_UNITTEST_H__ */
123 
124 /**
125  * @}
126  */
struct UtTest_ * next
Definition: util-unittest.h:43
int unittests_fatal
Definition: util-unittest.c:52
void UtListTests(const char *regex_arg)
List all registered unit tests.
const char * name
Definition: util-unittest.h:40
int UtRunSelftest(const char *regex_arg)
Run self tests.
void UtInitialize(void)
Initialize unit test list.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void UtRunModeRegister(void)
const char * regex_arg
void UtCleanup(void)
Cleanup unit test list.
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
int(* TestFn)(void)
Definition: util-unittest.h:41
struct UtTest_ UtTest