Forward port changelog
[tor.git] / src / common / test.h
blob12731a53bc38e19c4fef76885fc0263c4dad1137
1 /* Copyright 2001,2002,2003 Roger Dingledine. */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __TEST_H
6 #define __TEST_H
7 #define TEST_H_ID "$Id$"
9 /**
10 * \file test.h
11 * \brief Headers for test.c
14 #include <string.h>
15 #include <stdio.h>
16 #include "compat.h"
18 #define STMT_BEGIN do {
19 #define STMT_END } while (0)
21 #ifdef __GNUC__
22 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
23 #else
24 #define PRETTY_FUNCTION ""
25 #endif
27 extern int have_failed;
29 #define test_fail() \
30 STMT_BEGIN \
31 have_failed = 1; \
32 printf("\nFile %s: line %d (%s): assertion failed.", \
33 _SHORT_FILE_, \
34 __LINE__, \
35 PRETTY_FUNCTION); \
36 return; \
37 STMT_END
39 #define test_assert(expr) \
40 STMT_BEGIN \
41 if (expr) { printf("."); fflush(stdout); } else { \
42 have_failed = 1; \
43 printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
44 _SHORT_FILE_, \
45 __LINE__, \
46 PRETTY_FUNCTION, \
47 #expr); \
48 return; \
49 } STMT_END
51 #define test_eq(expr1, expr2) \
52 STMT_BEGIN \
53 long v1=(long)(expr1), v2=(long)(expr2); \
54 if (v1==v2) { printf("."); fflush(stdout); } else { \
55 have_failed = 1; \
56 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
57 " (%ld != %ld)\n", \
58 _SHORT_FILE_, \
59 __LINE__, \
60 PRETTY_FUNCTION, \
61 #expr1, #expr2, \
62 v1, v2); \
63 return; \
64 } STMT_END
66 #define test_neq(expr1, expr2) \
67 STMT_BEGIN \
68 long v1=(long)(expr1), v2=(long)(expr2); \
69 if (v1!=v2) { printf("."); fflush(stdout); } else { \
70 have_failed = 1; \
71 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
72 " (%ld == %ld)\n", \
73 _SHORT_FILE_, \
74 __LINE__, \
75 PRETTY_FUNCTION, \
76 #expr1, #expr2, \
77 v1, v2); \
78 return; \
79 } STMT_END
81 #define test_streq(expr1, expr2) \
82 STMT_BEGIN \
83 const char *v1=(expr1), *v2=(expr2); \
84 if (!strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
85 have_failed = 1; \
86 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
87 " (\"%s\" != \"%s\")\n", \
88 _SHORT_FILE_, \
89 __LINE__, \
90 PRETTY_FUNCTION, \
91 #expr1, #expr2, \
92 v1, v2); \
93 return; \
94 } STMT_END
96 #define test_strneq(expr1, expr2) \
97 STMT_BEGIN \
98 const char *v1=(expr1), *v2=(expr2); \
99 if (strcmp(v1,v2)) { printf("."); fflush(stdout); } else { \
100 have_failed = 1; \
101 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
102 " (\"%s\" == \"%s\")\n", \
103 _SHORT_FILE_, \
104 __LINE__, \
105 PRETTY_FUNCTION, \
106 #expr1, #expr2, \
107 v1, v2); \
108 return; \
109 } STMT_END
111 #define test_memeq(expr1, expr2, len) \
112 STMT_BEGIN \
113 const void *v1=(expr1), *v2=(expr2); \
114 if (!memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
115 have_failed = 1; \
116 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
117 _SHORT_FILE_, \
118 __LINE__, \
119 PRETTY_FUNCTION, \
120 #expr1, #expr2); \
121 return; \
122 } STMT_END
124 #define test_memneq(expr1, expr2, len) \
125 STMT_BEGIN \
126 void *v1=(expr1), *v2=(expr2); \
127 if (memcmp(v1,v2,(len))) { printf("."); fflush(stdout); } else {\
128 have_failed = 1; \
129 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
130 _SHORT_FILE_, \
131 __LINE__, \
132 PRETTY_FUNCTION, \
133 #expr1, #expr2); \
134 return; \
135 } STMT_END
137 #endif