-
Notifications
You must be signed in to change notification settings - Fork 3
/
Test.cc
310 lines (269 loc) · 5.81 KB
/
Test.cc
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// Copyright 2009-2017 M. Shulhan (ms@kilabit.info). All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "Test.hh"
namespace vos {
Test::Test(const char* header)
: Dlogger()
, _header(header)
, _prefix((const char*) 0)
{
out(">>> Testing %s\n", header);
}
Test::~Test()
{}
/**
* Method start(prefix,msg) will print a test message `header.prefix - msg`
* that indicate the test unit that will be running.
*/
void Test::start(const char* prefix, const char* msg)
{
if (prefix) {
_prefix.copy_raw(prefix);
}
out(" %s.%s", _header.v(), _prefix.v());
if (msg) {
out(" - %s", msg);
}
out(" : ");
}
/**
* Method ok() will print string "OK" with new line if test run succesfully.
*/
void Test::ok()
{
out("OK\n");
}
/**
* Method `expect_mem(exp, got, len, e)` will compare content of memory
* pointed by `exp` with `got` with length no more than `len`.
*
* It will return 0 if
*
* - e is IS_LESS_THAN and `exp` greater than `got`,
* - e is IS_EQUAL and `exp` is equal with `got`,
* - e is IS_GREATER_THAN and `exp` less than `got`.
*
* Otherwise it will terminate the program with exit code `1`.
*/
int Test::expect_mem(const void *exp, const void *got, size_t len
, enum EQUALITY e)
{
int res = memcmp(exp, got, len);
if (res == e) {
return 0;
}
append_fmt("\n");
append_fmt(" Expecting: %p\n", exp);
append_fmt(" got : %p\n", got);
exit(1);
}
/**
* Method `expect_ptr(exp, got, e)` will compare pointer of `exp` and
* `got`.
*
* It will return `0` if,
*
* - `exp` and `got` has the same pointer and e is IS_EQUAL.
* - `exp` and `got` is not pointing to the same memory location and e is
* IS_NOT_EQUAL.
*
* Otherwise it will terminate the program with exit status `1`.
*/
int Test::expect_ptr(const void *exp, const void *got
, enum EQUALITY e)
{
const char *str_e = NULL;
switch (e) {
case IS_EQUAL:
if (exp == got) {
return 0;
}
str_e = "equal";
break;
case IS_LESS_THAN:
case IS_NOT_EQUAL:
if (exp != got) {
return 0;
}
str_e = "not equal";
break;
}
append_fmt("\n");
append_fmt(" Expecting pointer '%p' %s '%p'\n", exp, str_e, got);
exit(1);
}
/**
* Method `expect_string(exp, got, to)` will compare expected string value
* `exp` with `got` value.
*
* Value of e IS_LESS_THAN means the function expect the `exp` value to be
* less than `got` value.
* Value of e IS_EQUAL means its `exp` equal with `got`.
* Value of e IS_GREATER_THAN means the function expect the `exp` value
* greater than `got` value.
*
* On success it will return `0`, if expected and got value true to their `e`
* value.
*
* On fail it will exit with status `1`.
*/
int Test::expect_string(const char* exp, const char* got
, enum EQUALITY e)
{
int s = strncmp(exp, got, strlen(exp));
if (s == e) {
return 0;
}
append_fmt("\n");
append_fmt(" Expecting: %s\n", exp);
append_fmt(" got : %s\n", got);
exit(1);
}
/**
* Method `expect_signed(exp, got, e)` will check if `exp` value is
* either less, equal, or greater than `got` value.
*
* On success it will return `0`, if expected and got value true to their `e`
* value.
*
* On fail it will exit with status `1`.
*/
int Test::expect_signed(const ssize_t exp, const ssize_t got
, enum EQUALITY e)
{
const char *str_e = NULL;
switch (e) {
case IS_LESS_THAN:
if (exp < got) {
return 0;
}
str_e = "less than";
break;
case IS_EQUAL:
if (exp == got) {
return 0;
}
str_e = "equal";
break;
case IS_GREATER_THAN:
if (exp > got) {
return 0;
}
str_e = "greater than";
break;
}
append_fmt("\n");
append_fmt(" Expecting '%d' %s '%d'\n", exp, str_e, got);
exit(1);
}
/**
* Method `expect_unsigned(exp, got, e)` will check if `exp` value is
* either less, equal, or greater than `got` value.
*
* On success it will return `0`, if expected and got value true to their `e`
* value.
*
* On fail it will exit with status `1`.
*/
int Test::expect_unsigned(const size_t exp, const size_t got
, enum EQUALITY e)
{
const char *str_e = NULL;
switch (e) {
case IS_LESS_THAN:
if (exp < got) {
return 0;
}
str_e = "less than";
break;
case IS_EQUAL:
if (exp == got) {
return 0;
}
str_e = "equal";
break;
case IS_GREATER_THAN:
if (exp > got) {
return 0;
}
str_e = "greater than";
break;
}
append_fmt("\n");
append_fmt(" Expecting '%u' %s '%u'\n", exp, str_e, got);
exit(1);
}
/**
* Method expect_double(exp, got, e) will check if `exp` value is
* either less, equal, or greater than `got` value.
*
* On success it will return `0`, if expected and got value true to their e
* value.
*
* On fail, it will exit with status `1`.
*/
int Test::expect_double(const double exp, const double got
, enum EQUALITY e)
{
const char *str_e = NULL;
switch (e) {
case IS_LESS_THAN:
if (exp < got) {
return 0;
}
str_e = "less than";
break;
case IS_EQUAL:
if (exp == got) {
return 0;
}
str_e = "equal";
break;
case IS_GREATER_THAN:
if (exp > got) {
return 0;
}
str_e = "greater than";
break;
}
append_fmt("\n");
append_fmt(" Expecting '%f' %s '%f'\n", exp, str_e, got);
exit(1);
}
/**
* Method expect_error(exp,got,e) will return 0 if,
*
* - e is IS_EQUAL and exp is same error with got
* - e is NOT_EQUAL and exp is not the same error with got
*
* Otherwise it will exit with status `1`.
*/
int Test::expect_error(Error exp, Error got, enum EQUALITY e)
{
const char *str_e;
switch (e) {
case IS_EQUAL:
if (exp == got) {
return 0;
}
str_e = "equal";
break;
case IS_LESS_THAN:
case IS_NOT_EQUAL:
default:
if (exp != got) {
return 0;
}
str_e = "not equal";
break;
}
append_fmt("\n");
append_fmt(" Expecting '%s' %s '%s'\n", exp.chars(), str_e
, got.chars());
exit(1);
}
} // namespace::vos
// vi: ts=8 sw=8 tw=80: