/
main.c
309 lines (247 loc) · 8.65 KB
/
main.c
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
/**
* @file main.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
static ssize_t test_vprintf_wrapper(FAR const char *fmt_p, ...)
{
ssize_t res;
va_list ap;
va_start(ap, fmt_p);
res = std_vprintf(fmt_p, &ap);
va_end(ap);
return (res);
}
static ssize_t test_vfprintf_wrapper(chan_t *chan_p,
FAR const char *fmt_p,
...)
{
ssize_t res;
va_list ap;
va_start(ap, fmt_p);
res = std_vfprintf(chan_p, fmt_p, &ap);
va_end(ap);
std_printf(FSTR("res = %d\r\n"), res);
return (res);
}
static int test_sprintf(struct harness_t *harness_p)
{
char buf[128];
ssize_t size;
BTASSERT((size = std_sprintf(buf,
FSTR("Normal: '%c' '%d' '%lu' '%s'"),
'b', -43, 0xffffffffUL, "foo")) == 51);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Left justification: '%-10c' '%-10d' '%-10lu' '%-10s'"),
'b', -43, 0xffffffffUL, "foo")) == 74);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Preceding with blanks: '%10c' '%10d' '%10lu' '%10s'"),
'b', -43, 0xffffffffUL, "foo")) == 74);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Preceding with zeros: '%010c' '%010d' '%010lu' '%010s'"),
'b', -43, 0xffffffffUL, "foo")) == 74);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Bad format: %g %"))) == 14);
std_printf(FSTR("%s\r\n"), buf);
#ifdef ARCH_LINUX
BTASSERT((size = std_sprintf(buf,
FSTR("Big unsigned: '%u'"),
0xffffffffU)) == 35);
#else
BTASSERT((size = std_sprintf(buf,
FSTR("Big unsigned: '%u'"),
0xffffU)) == 30);
#endif
std_printf(FSTR("%s\r\n"), buf);
return (0);
}
static int test_snprintf(struct harness_t *harness_p)
{
char buf[128];
BTASSERT(std_snprintf(buf, sizeof(buf), FSTR("foo")) == 3);
BTASSERT(strcmp(buf, "foo") == 0);
/* Destination buffer too small. */
memset(buf, -1, sizeof(buf));
BTASSERT(std_snprintf(buf, 2, FSTR("foo")) == 3);
BTASSERT(memcmp(buf, "fo\xff", 3) == 0);
return (0);
}
static int test_vprintf(struct harness_t *harness_p)
{
BTASSERT(test_vprintf_wrapper(FSTR("vprintf: %d\r\n"), 1) == 12);
return (0);
}
static int test_vfprintf(struct harness_t *harness_p)
{
BTASSERT(test_vfprintf_wrapper(sys_get_stdout(),
FSTR("vprintf: %d\r\n"),
1) == 12);
return (0);
}
static int test_sprintf_double(struct harness_t *harness_p)
{
char buf[128];
ssize_t size;
BTASSERT((size = std_sprintf(buf,
FSTR("Normal: '%f' '%f'"),
10.5f, -37.731)) == 47);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Left justification: '%-12f' '%-12f'"),
10.5f, -37.731)) == 52);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Preceding with blanks: '%12f' '%12f'"),
10.5f, -37.731)) == 52);
std_printf(FSTR("%s\r\n"), buf);
BTASSERT((size = std_sprintf(buf,
FSTR("Preceding with zeros: '%012f' '%012f'"),
10.5f, -37.731)) == 52);
std_printf(FSTR("%s\r\n"), buf);
return (0);
}
static int test_strtol(struct harness_t *harness_p)
{
long value;
const char *next_p;
/* Positive integers.*/
BTASSERT(std_strtol("0x1011", &value) != NULL);
BTASSERT(value == 0x1011);
BTASSERT(std_strtol("0xf011", &value) != NULL);
BTASSERT(value == 0xf011);
BTASSERT(std_strtol("0xB011", &value) != NULL);
BTASSERT(value == 0xB011);
/* Base 10. */
BTASSERT(std_strtol("1011", &value) != NULL);
BTASSERT(value == 1011);
/* Base 8. */
BTASSERT(std_strtol("01011", &value) != NULL);
BTASSERT(value == 01011);
/* Base 2. */
BTASSERT(std_strtol("0b1011", &value) != NULL);
BTASSERT(value == 0b1011);
/* Negative integers.*/
BTASSERT(std_strtol("-0x1011", &value) != NULL);
BTASSERT(value == -0x1011);
BTASSERT(std_strtol("-1011", &value) != NULL);
BTASSERT(value == -1011);
BTASSERT(std_strtol("-0b1011", &value) != NULL);
BTASSERT(value == -0b1011);
/* Non-null termination. */
BTASSERT(std_strtol("0x1r11", &value) != NULL);
BTASSERT(value == 0x1);
BTASSERT(std_strtol("0b1012", &value) != NULL);
BTASSERT(value == 0b101);
/* Bad input. */
BTASSERT(std_strtol("a011", &value) == NULL);
/* Next byte. */
next_p = std_strtol("0x1011", &value);
BTASSERT(next_p != NULL);
BTASSERT(value == 0x1011);
BTASSERT(*next_p == '\0');
next_p = std_strtol("0x101.", &value);
BTASSERT(next_p != NULL);
BTASSERT(value == 0x101);
BTASSERT(*next_p == '.');
return (0);
}
static int test_strcpy(struct harness_t *harness_p)
{
char buf[16];
BTASSERT(std_strcpy(buf, FSTR("foo")) == 3);
BTASSERT(buf[0] == 'f');
BTASSERT(buf[1] == 'o');
BTASSERT(buf[2] == 'o');
BTASSERT(buf[3] == '\0');
BTASSERT(std_strcpy(buf, FSTR("")) == 0);
BTASSERT(buf[0] == '\0');
return (0);
}
static int test_strcmp(struct harness_t *harness_p)
{
/* Far + local. */
BTASSERT(std_strncmp(FSTR("foo"), "foo", 3) == 0);
BTASSERT(std_strncmp(FSTR("foo"), "foo", 0) == 0);
BTASSERT(std_strncmp(FSTR("foo"), "foobar", 3) == 0);
BTASSERT(std_strncmp(FSTR("foo"), "foobar", 4) == -'b');
/* Far + far. */
BTASSERT(std_strncmp_f(FSTR("foo"), FSTR("foo"), 3) == 0);
BTASSERT(std_strncmp_f(FSTR("foo"), FSTR("foo"), 0) == 0);
BTASSERT(std_strncmp_f(FSTR("foo"), FSTR("foobar"), 3) == 0);
BTASSERT(std_strncmp_f(FSTR("foo"), FSTR("foobar"), 4) == -'b');
return (0);
}
static int test_strlen(struct harness_t *harness_p)
{
BTASSERT(std_strlen(FSTR("foo")) == 3);
BTASSERT(std_strlen(FSTR("")) == 0);
return (0);
}
static int test_strip(struct harness_t *harness_p)
{
char leading_whitespace[] = "\t hej";
char trailing_whitespace[] = "hej \r\n";
char empty_string[] = "";
char leading_and_trailing_abc_string[] = "abc123abc";
char buf[32];
char *expected_p;
/* Leading whitespace characters. */
strcpy(buf, leading_whitespace);
expected_p = &buf[2];
BTASSERT(std_strip(buf, NULL) == expected_p);
/* Trailing whitespace characters. */
strcpy(buf, trailing_whitespace);
expected_p = buf;
BTASSERT(std_strip(buf, NULL) == expected_p);
BTASSERT(buf[3] == '\0');
/* Empty string. */
strcpy(buf, empty_string);
expected_p = buf;
BTASSERT(std_strip(buf, NULL) == expected_p);
BTASSERT(buf[0] == '\0');
/* Strip chosen character set. */
strcpy(buf, leading_and_trailing_abc_string);
expected_p = &buf[3];
BTASSERT(std_strip(buf, "xyzabc") == expected_p);
BTASSERT(buf[6] == '\0');
return (0);
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_sprintf, "test_sprintf" },
{ test_snprintf, "test_snprintf" },
{ test_vprintf, "test_vprintf" },
{ test_vfprintf, "test_vfprintf" },
{ test_strtol, "test_strtol" },
{ test_strcpy, "test_strcpy" },
{ test_strcmp, "test_strcmp" },
{ test_strlen, "test_strlen" },
{ test_sprintf_double, "test_sprintf_double" },
{ test_strip, "test_strip" },
{ NULL, NULL }
};
sys_start();
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}