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
|
#include "kernel.h"
#include "stdarg.h"
#include "test/usertest.h"
#include "util/debug.h"
#include "util/printf.h"
typedef struct test_data
{
int td_passed;
int td_failed;
} test_data_t;
static void _default_test_fail(const char *file, int line, const char *name,
const char *fmt, va_list args);
static void _default_test_pass(int val, const char *file, int line,
const char *name, const char *fmt, va_list args);
static test_data_t _test_data;
static test_pass_func_t _pass_func = _default_test_pass;
static test_fail_func_t _fail_func = _default_test_fail;
void test_init(void)
{
_test_data.td_passed = 0;
_test_data.td_failed = 0;
}
void test_fini(void)
{
dbgq(DBG_TEST, "tests completed:\n");
dbgq(DBG_TEST, "\t\t%d passed\n", _test_data.td_passed);
dbgq(DBG_TEST, "\t\t%d failed\n", _test_data.td_failed);
}
const char *test_errstr(int err)
{
switch (err)
{
case 1:
return "EPERM";
case 2:
return "ENOENT";
case 3:
return "ESRCH";
case 4:
return "EINTR";
case 5:
return "EIO";
case 6:
return "ENXIO";
case 7:
return "E2BIG";
case 8:
return "ENOEXEC";
case 9:
return "EBADF";
case 10:
return "ECHILD";
case 11:
return "EAGAIN";
case 12:
return "ENOMEM";
case 13:
return "EACCES";
case 14:
return "EFAULT";
case 15:
return "ENOTBLK";
case 16:
return "EBUSY";
case 17:
return "EEXIST";
case 18:
return "EXDEV";
case 19:
return "ENODEV";
case 20:
return "ENOTDIR";
case 21:
return "EISDIR";
case 22:
return "EINVAL";
case 23:
return "ENFILE";
case 24:
return "EMFILE";
case 25:
return "ENOTTY";
case 26:
return "ETXTBSY";
case 27:
return "EFBIG";
case 28:
return "ENOSPC";
case 29:
return "ESPIPE";
case 30:
return "EROFS";
case 31:
return "EMLINK";
case 32:
return "EPIPE";
case 33:
return "EDOM";
case 34:
return "ERANGE";
case 35:
return "EDEADLK";
case 36:
return "ENAMETOOLONG";
case 37:
return "ENOLCK";
case 38:
return "ENOSYS";
case 39:
return "ENOTEMPTY";
case 40:
return "ELOOP";
default:
return "UNKNOWN";
}
}
static void _default_test_fail(const char *file, int line, const char *name,
const char *fmt, va_list args)
{
_test_data.td_failed++;
if (NULL == fmt)
{
dbgq(DBG_TEST, "FAILED: %s(%d): %s\n", file, line, name);
}
else
{
char buf[2048];
vsnprintf(buf, sizeof(buf), fmt, args);
buf[2047] = '\0';
dbgq(DBG_TEST, "FAILED: %s(%d): %s: %s\n", file, line, name, buf);
}
}
static void _default_test_pass(int val, const char *file, int line,
const char *name, const char *fmt,
va_list args)
{
_test_data.td_passed++;
}
int _test_assert(int val, const char *file, int line, const char *name,
const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (0 == val)
{
if (NULL != _fail_func)
{
_fail_func(file, line, name, fmt, args);
}
}
else
{
if (NULL != _pass_func)
{
_pass_func(val, file, line, name, fmt, args);
}
}
va_end(args);
return val;
}
|