-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_test.c
More file actions
187 lines (143 loc) · 3.5 KB
/
_test.c
File metadata and controls
187 lines (143 loc) · 3.5 KB
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
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
#include "slcan.h"
#include "log.h"
#include "script.h"
#define QUOTE(str) #str
#define _P(str) _make_printable(str, strlen(str))
int _be_hex_to_uint32(uint8_t *buf, size_t len, uint32_t *ret);
void
_result(const char *val, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "%s: ", val);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
int
_test_hex(void)
{
#define _CHECK_POS(x, y) \
do { \
uint32_t v; \
if (_be_hex_to_uint32(x, strlen(x), &v) < 0) { LOG_ERROR("conversion failed"); return -1; } \
if (v != y) { _result("FAIL", "\"" x "\" != " QUOTE(y) " it's 0x%x", v); return -1; } \
_result("PASS", "\"" x "\" == " QUOTE(y)); \
} while (0)
#define _CHECK_NEG(x, y) \
do { \
uint32_t v; \
if (_be_hex_to_uint32(x, strlen(x), &v) >= 0) { \
_result("FAIL", "conversion succeeded when it shouldn't"); \
return -1; \
} \
_result("PASS", "\"" x "\" didn't convert"); \
} while (0)
_CHECK_POS("A", 0xa);
_CHECK_POS("1235", 0x1235);
_CHECK_POS("AeF1", 0xaef1);
_CHECK_NEG("S", 0xaef1);
_CHECK_NEG("123G", 0xaef1);
_CHECK_NEG("", 0xaef1);
#undef _CHECK_NEG
#undef _CHECK_POS
return 0;
}
int _open_hook(slcan_state_t *s) {
LOG_DEBUG("open hook.");
return 0;
}
int _close_hook(slcan_state_t *s) {
LOG_DEBUG("close hook.");
return 0;
}
int _xmit_hook(slcan_state_t *s, uint8_t ext, uint32_t id, uint8_t *buf, size_t len) {
LOG_DEBUG("xmit: %d ext 0x%0X id %d len", ext, id, len);
for (int i = 0; i < len; i++) {
LOG_DEBUG("\t%d: 0x%0X", i, buf[i]);
}
return 0;
}
int _rtr_hook(slcan_state_t *s, uint8_t ext, uint32_t id, size_t len) {
LOG_DEBUG("rtr: %d ext 0x%0X id %d len", ext, id, len);
return 0;
}
int _resp_hook(slcan_state_t *s, const char *resp) {
LOG_DEBUG("sending response: '%s'", _P(resp));
return strlen(resp);
}
int
_test_slcan(void)
{
#define _CMD_POS(cmd) do { if ((ret = slcan_handle_cmd(&s, cmd, strlen(cmd))) < 0) { _result("FAIL", "incorrect return code: %d at %d", ret, __LINE__); return -1 ; } _result("PASS", "positive case succeeded"); } while (0)
#define _CMD_NEG(cmd) do { if ((ret = slcan_handle_cmd(&s, cmd, strlen(cmd))) >= 0) { _result("FAIL", "incurrent return code : %d at %d", ret, __LINE__); return -1; } _result("PASS", "negative case failed"); } while (0)
{
int ret;
slcan_state_t s;
slcan_init(&s);
(&s)->open_hook = _open_hook;
(&s)->close_hook = _close_hook;
(&s)->xmit_hook = _xmit_hook;
(&s)->rtr_hook = _rtr_hook;
(&s)->resp_hook = _resp_hook;
LOG_INFO("testing hooks...");
_CMD_POS("O\r");
_CMD_POS("T0000010021133\r");
_CMD_NEG("T0000010021\r");
_CMD_POS("t4563112233\r");
_CMD_POS("T12ABCDEF2AA55\r");
_CMD_POS("r1230\r");
_CMD_POS("C\r");
_CMD_NEG("T0000010021133\r");
}
#undef _CMD_NEG
#undef _CMD_POS
return 0;
}
int
_test_script(void)
{
#define _W(x) if (script_word_ingest(state, #x) != 0) return -1
uint8_t heap[1024];
uint8_t *here = heap;
script_state_t *state = (script_state_t *)here;
script_state_init(state);
here += sizeof(script_state_t);
state->dp = here;
_W(16);
_W(base);
_W(!);
_W(1);
_W(dup);
_W(+);
_W(DEADBEEF);
_W(swap);
_W(.);
_W(dup);
_W(1);
_W(+);
_W(swap);
_W(.);
_W(drop);
script_eval_str(state, "3 4 + .\r");
return 0;
}
int
main(void)
{
if (_test_hex() != 0) {
return -1;
}
if (_test_slcan() != 0) {
return -1;
}
if (_test_script() != 0) {
return -1;
}
return 0;
}