1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/* Copyright (c) 2001-2003, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2008, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/* $Id$ */
#ifndef __TEST_H
#define __TEST_H
#define TEST_H_ID "$Id$"
/**
* \file test.h
* \brief Macros used by unit tests.
*/
#include "compat.h"
#ifdef __GNUC__
#define PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define PRETTY_FUNCTION ""
#endif
extern int have_failed;
#define test_fail_msg(msg) \
STMT_BEGIN \
have_failed = 1; \
printf("\nFile %s: line %d (%s): %s", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
msg); \
return; \
STMT_END
#define test_fail() test_fail_msg("Assertion failed.")
#define test_assert(expr) \
STMT_BEGIN \
if (expr) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr); \
return; \
} STMT_END
#define test_eq_type(tp, fmt, expr1, expr2) \
STMT_BEGIN \
tp _test_v1=(tp)(expr1); \
tp _test_v2=(tp)(expr2); \
if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
" "fmt "!="fmt"\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
#define test_eq(expr1, expr2) \
test_eq_type(long, "%ld", expr1, expr2)
#define test_eq_ptr(expr1, expr2) \
test_eq_type(void*, "%p", expr1, expr2)
#define test_neq_type(tp, fmt, expr1, expr2) \
STMT_BEGIN \
tp _test_v1=(tp)(expr1); \
tp _test_v2=(tp)(expr2); \
if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
" ("fmt" == "fmt")\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
#define test_neq(expr1, expr2) \
test_neq_type(long, "%ld", expr1, expr2)
#define test_neq_ptr(expr1, expr2) \
test_neq_type(void *, "%p", expr1, expr2)
#define test_streq(expr1, expr2) \
STMT_BEGIN \
const char *_test_v1=(expr1), *_test_v2=(expr2); \
if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
" (\"%s\" != \"%s\")\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
#define test_strneq(expr1, expr2) \
STMT_BEGIN \
const char *_test_v1=(expr1), *_test_v2=(expr2); \
if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
" (\"%s\" == \"%s\")\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
#define test_memeq(expr1, expr2, len) \
STMT_BEGIN \
const void *_test_v1=(expr1), *_test_v2=(expr2); \
char *mem1, *mem2; \
if (!memcmp(_test_v1,_test_v2,(len))) { \
printf("."); fflush(stdout); } else { \
have_failed = 1; \
mem1 = tor_malloc(len*2+1); \
mem2 = tor_malloc(len*2+1); \
base16_encode(mem1, len*2+1, _test_v1, len); \
base16_encode(mem2, len*2+1, _test_v2, len); \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
" %s != %s\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, mem1, mem2); \
return; \
} STMT_END
#define test_memeq_hex(expr1, hex) \
STMT_BEGIN \
const void *_test_v1 = (expr1); \
const char *_test_v2 = (hex); \
size_t _len_v2 = strlen(_test_v2); \
char *mem2 = tor_malloc(_len_v2/2); \
tor_assert((_len_v2 & 1) == 0); \
base16_decode(mem2, _len_v2/2, _test_v2, _len_v2); \
if (!memcmp(mem2, _test_v1, _len_v2/2)) { \
printf("."); fflush(stdout); } else { \
char *mem1 = tor_malloc(_len_v2)+1; \
base16_encode(mem1, _len_v2+1, _test_v1, _len_v2/2); \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
" %s != %s\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, _test_v2, mem1, _test_v2); \
return; \
} \
tor_free(mem2); \
STMT_END
#define test_memneq(expr1, expr2, len) \
STMT_BEGIN \
void *_test_v1=(expr1), *_test_v2=(expr2); \
if (memcmp(_test_v1,_test_v2,(len))) { \
printf("."); fflush(stdout); \
} else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2); \
return; \
} STMT_END
#endif
|