forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutest.h
261 lines (230 loc) · 13 KB
/
utest.h
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright (c) 2015 Douglas J. Bakkum
Copyright (c) 2022 bluezr
Copyright (c) 2022 The Dogecoin Foundation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/*
//
// Macros for light weight unit testing based on
// minunit.h from http://www.jera.com/techinfo/jtns/jtn002.html
//
// Example code:
//
#include "utest.h"
int U_TESTS_RUN = 0;
int U_TESTS_FAIL = 0;
static void test_str(void) {
char * str = "hello";
u_assert_str_eq(str, "hello");
u_assert_str_eq(str, "hellooo");
return;
}
static void test_int(void) {
int i = 7;
u_assert_int_eq(i, 7);
u_assert_int_eq(i, 8);
return;
}
static void test_mem(void) {
char * str = "hello";
u_assert_mem_eq(str, "hello", 5);
u_assert_mem_eq(str, "hellooo", 5);
u_assert_mem_eq(str, "helooo", 5);
return;
}
int main(void)
{
u_run_test(test_str);
u_run_test(test_mem);
u_run_test(test_int);
if (!U_TESTS_FAIL) {
printf("\nALL TESTS PASSED\n\n");
}
return U_TESTS_FAIL;
}
*/
#ifndef _UTEST_H_
#define _UTEST_H_
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#define u_run_test(TEST) \
do { \
int f_ = U_TESTS_FAIL; \
TEST(); \
U_TESTS_RUN++; \
if (f_ == U_TESTS_FAIL) { \
printf("PASSED - %s()\n", #TEST); \
}; \
} while (0)
#define u_assert_int_eq(R, E) \
{ \
int r_ = (R); \
int e_ = (E); \
do { \
if (r_ != e_) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%d\n", e_); \
printf("\tReceive:\t%d\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_uint32_eq(R, E) \
{ \
uint64_t r_ = (R); \
uint64_t e_ = (E); \
do { \
if (r_ != e_) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%" PRIu64 "\n", e_); \
printf("\tReceive:\t%" PRIu64 "\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_long_double_eq(R, E) \
{ \
long double r_ = (R); \
long double e_ = (E); \
do { \
if (r_ != e_ ) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%.8Lf\n", e_); \
printf("\tReceive:\t%.8Lf\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_double_eq(R, E) \
{ \
double r_ = (R); \
double e_ = (E); \
do { \
if (r_ != e_) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%.80lf\n", e_); \
printf("\tReceive:\t%.80lf\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_str_eq(R, E) \
{ \
const char* r_ = (R); \
const char* e_ = (E); \
do { \
if (strcmp(r_, e_)) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%s\n", e_); \
printf("\tReceive:\t%s\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_str_not_eq(R, E) \
{ \
const char* r_ = (R); \
const char* e_ = (E); \
do { \
if (!strcmp(r_, e_)) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tNot expect:\t%s\n", e_); \
printf("\tReceive:\t%s\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_str_has(R, E) \
{ \
const char* r_ = (R); \
const char* e_ = (E); \
do { \
if (!strstr(r_, e_)) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%s\n", e_); \
printf("\tReceive:\t%s\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_str_has_not(R, E) \
{ \
const char* r_ = (R); \
const char* e_ = (E); \
do { \
if (strstr(r_, e_)) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tNot expect:\t%s\n", e_); \
printf("\tReceive:\t%s\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_mem_eq(R, E, L) \
{ \
const void* r_ = (R); \
const void* e_ = (E); \
size_t l_ = (L); \
do { \
if (memcmp(r_, e_, l_)) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \t%s\n", utils_uint8_to_hex(e_, l_)); \
printf("\tReceive:\t%s\n", utils_uint8_to_hex(r_, l_)); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_is_null(R) \
{ \
const void* r_ = (R); \
do { \
if (r_) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \tNULL\n"); \
printf("\tReceive:\t%p\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
#define u_assert_not_null(R) \
{ \
const void* r_ = (R); \
do { \
if (!r_) { \
printf("FAILED - %s() - Line %d\n", __func__, __LINE__); \
printf("\tExpect: \tnot NULL\n"); \
printf("\tReceive:\tNULL\n", r_); \
U_TESTS_FAIL++; \
return; \
}; \
} while (0); \
}
extern int U_TESTS_RUN;
extern int U_TESTS_FAIL;
#endif