-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
272 lines (217 loc) · 8.29 KB
/
tests.cpp
File metadata and controls
272 lines (217 loc) · 8.29 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
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
#include <cstdio>
#include <cstring>
#include <cinttypes>
#include "codec.h"
#include "protocol.h"
#include "visualizer.h"
static int tests_run = 0;
static int tests_passed = 0;
#define TEST(name) \
void name(); \
static struct name##_register \
{ \
name##_register() \
{ \
printf("\n[TEST] %s\n", #name); \
++tests_run; \
name(); \
} \
} name##_instance; \
void name()
#define ASSERT(cond) \
do \
{ \
if (!(cond)) \
{ \
printf(" FAIL: %s (line %d)\n", #cond, __LINE__); \
return; \
} \
} while (0)
#define PASS() \
do \
{ \
++tests_passed; \
printf(" PASS\n"); \
} while (0)
// ===========================================================================
// Basic Encode/Decode Test
// ===========================================================================
TEST(test_encode_decode_roundtrip)
{
uint8_t buffer[32] = {0};
Order input = {
.order_id = 123456789,
.price = 50000,
.quantity = 100,
.side = protocol::SIDE_BUY};
size_t written = encode_order(buffer, sizeof(buffer), input);
ASSERT(written == protocol::ORDER_MSG_SIZE);
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::OK);
ASSERT(output.order_id == input.order_id);
ASSERT(output.price == input.price);
ASSERT(output.quantity == input.quantity);
ASSERT(output.side == input.side);
PASS();
}
// ===========================================================================
// Buffer Too Small Test
// ===========================================================================
TEST(test_buffer_too_small_encode)
{
uint8_t buffer[10]; // Too small
Order input = {1, 100, 10, protocol::SIDE_SELL};
size_t written = encode_order(buffer, sizeof(buffer), input);
ASSERT(written == 0);
PASS();
}
TEST(test_buffer_too_small_decode)
{
uint8_t buffer[10] = {0};
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::BUFFER_TOO_SMALL);
PASS();
}
// ===========================================================================
// Invalid Message Type Test
// ===========================================================================
TEST(test_invalid_message_type)
{
uint8_t buffer[32] = {0};
Order input = {1, 100, 10, protocol::SIDE_BUY};
encode_order(buffer, sizeof(buffer), input);
// Corrupt message type
buffer[0] = 0xFF;
buffer[1] = 0xFF;
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::INVALID_MSG_TYPE);
PASS();
}
// ===========================================================================
// Checksum Validation Test
// ===========================================================================
TEST(test_checksum_mismatch)
{
uint8_t buffer[32] = {0};
Order input = {42, 1000, 5, protocol::SIDE_SELL};
encode_order(buffer, sizeof(buffer), input);
// Corrupt a payload byte
buffer[10] ^= 0xFF;
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::CHECKSUM_MISMATCH);
PASS();
}
// ===========================================================================
// Edge Cases
// ===========================================================================
TEST(test_max_values)
{
uint8_t buffer[32] = {0};
Order input = {
.order_id = UINT64_MAX,
.price = UINT32_MAX,
.quantity = UINT32_MAX,
.side = protocol::SIDE_SELL};
size_t written = encode_order(buffer, sizeof(buffer), input);
ASSERT(written == protocol::ORDER_MSG_SIZE);
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::OK);
ASSERT(output.order_id == UINT64_MAX);
ASSERT(output.price == UINT32_MAX);
ASSERT(output.quantity == UINT32_MAX);
PASS();
}
TEST(test_zero_values)
{
uint8_t buffer[32] = {0};
Order input = {0, 0, 0, protocol::SIDE_BUY};
size_t written = encode_order(buffer, sizeof(buffer), input);
ASSERT(written == protocol::ORDER_MSG_SIZE);
Order output = {};
CodecResult result = decode_order(buffer, sizeof(buffer), output);
ASSERT(result == CodecResult::OK);
ASSERT(output.order_id == 0);
ASSERT(output.price == 0);
ASSERT(output.quantity == 0);
PASS();
}
// ===========================================================================
// Visual Demonstration
// ===========================================================================
void run_visual_demo()
{
printf("\n");
printf("##############################################################\n");
printf("# BINARY PROTOCOL VISUALIZER #\n");
printf("##############################################################\n");
uint8_t buffer[32] = {0};
Order order = {
.order_id = 42,
.price = 15000, // $150.00
.quantity = 100,
.side = protocol::SIDE_BUY};
printf("\nCreating order: id=%" PRIu64 ", price=%u, qty=%u, side=%s\n",
order.order_id, order.price, order.quantity,
order.side == protocol::SIDE_BUY ? "BUY" : "SELL");
size_t written = encode_order(buffer, sizeof(buffer), order);
printf("Encoded %zu bytes\n", written);
// Show raw hex dump
viz::hex_dump(buffer, written);
// Show annotated format
viz::annotate_order_message(buffer, written);
// Show encoding process
viz::show_encoding_process(buffer, written,
order.order_id, order.price,
order.quantity, order.side);
// Show decoding process
viz::show_decoding_process(buffer, written);
// Decode and verify
Order decoded = {};
CodecResult result = decode_order(buffer, written, decoded);
printf("\n");
printf("=== FINAL VERIFICATION ");
for (int i = 0; i < 38; ++i)
printf("=");
printf("\n\n");
printf("Decode result: %s\n\n", codec_result_str(result));
printf(" Original -> Decoded\n");
printf(" -------- --------\n");
printf(" order_id: %" PRIu64 " -> %" PRIu64 " %s\n",
order.order_id, decoded.order_id,
order.order_id == decoded.order_id ? "[OK]" : "[MISMATCH]");
printf(" price: %u -> %u %s\n",
order.price, decoded.price,
order.price == decoded.price ? "[OK]" : "[MISMATCH]");
printf(" quantity: %u -> %u %s\n",
order.quantity, decoded.quantity,
order.quantity == decoded.quantity ? "[OK]" : "[MISMATCH]");
printf(" side: %s -> %s %s\n",
order.side == protocol::SIDE_BUY ? "BUY" : "SELL",
decoded.side == protocol::SIDE_BUY ? "BUY" : "SELL",
order.side == decoded.side ? "[OK]" : "[MISMATCH]");
}
// ===========================================================================
// Main
// ===========================================================================
int main(int argc, char *argv[])
{
printf("Binary Protocol Test Suite\n");
printf("==========================\n");
// Tests run automatically via static initialization
printf("\n---------------------------\n");
printf("Results: %d/%d tests passed\n", tests_passed, tests_run);
if (argc > 1 && strcmp(argv[1], "--visual") == 0)
{
run_visual_demo();
}
else
{
printf("\nRun with --visual flag to see byte-level visualization\n");
}
return (tests_passed == tests_run) ? 0 : 1;
}