-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (87 loc) · 3.19 KB
/
main.cpp
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
#include <stdlib.h>
#include "dll.hpp"
#include "mem.hpp"
#include "config.hpp"
#ifdef DEBUG_MEM_ELABORATE
#define allocate(x, ...) put_str(#x); put_str(": "); allocate(x, ##__VA_ARGS__)
#define reallocate(x, ...) put_str(#x); put_str(": "); reallocate(x,##__VA_ARGS__)
#define deallocate(x, ...) put_str(#x); put_str(": "); deallocate(x, ##__VA_ARGS__)
#endif
#define NUM_TESTS 1
#define NUM_UPDATES NUM_TESTS
bool dll_test(DLL&);
int main() {
#ifndef WINDOWS
init_uart0(); // init uart
_delay_ms(100); // delay for uart to initialize properly
put_str("--------------------------------------------------------\r\n");
#endif
// Test DLL
DLL dll;
for (uint16_t i = 0; i < NUM_TESTS; i++) {
bool error = dll_test(dll);
if (error == true) {
put_str("Test "); put_uint16(i + 1); put_str(" failed\r\n");
return 1;
} else if ((i + 1) % (NUM_TESTS/NUM_UPDATES) == 0) {
if (i == NUM_TESTS - 1) {
put_str("All ");
}
put_uint16(i + 1); put_str(" tests passed\r\n");
#ifdef DEBUG_DLL_TEST
if (i < NUM_TESTS - 1) {
put_str("\r\n");
}
#endif
}
}
}
bool dll_test(DLL& dll) {
uint8_t packet_length = rand() % 24 + 1; // 1-24 bytes
// uint8_t packet_length = rand() % 255 + 1; // 1-255 bytes
uint8_t packet[packet_length];
// Initialise packet to send
for (uint16_t byte_num = 0; byte_num < packet_length; byte_num++) {
// packet[byte_num] = rand() % 0x100; // All possible values
// packet[byte_num] = rand() % 0x10 + 0x70; // 0x70 to 0x7E
packet[byte_num] = rand() % 2 + FLAG; // FLAG and ESC
// packet[byte_num] = byte_num; // Sequential numbering
}
#ifdef DEBUG_DLL_TEST
put_str("Sending packet: "); print(packet, packet_length);
#endif
// Send packet
dll.send(packet, packet_length, 0xFF);
#ifdef DEBUG_DLL_TEST
put_str("Received packet: "); print(dll.received_packet, dll.received_packet_length);
#ifdef DEBUG_DLL
put_str("Sent packet: "); print(packet, packet_length);
#endif
#endif
// Check received packet length matches
if (dll.received_packet_length != packet_length) {
put_str("Error: Packet lengths do not match\r\n");
put_str("Sent packet length = "); put_uint8(packet_length); put_str("\r\n");
put_str("Received packet length = "); put_uint8(dll.received_packet_length); put_str("\r\n");
return 1;
}
// Check received packet contents matches
for (uint8_t byte_num = 0; byte_num < packet_length; byte_num++) {
if (dll.received_packet[byte_num] != packet[byte_num]) {
put_str("Error: Packet contents do not match\r\n");
return 1;
}
}
#ifdef DEBUG_DLL
put_str("Sent and received packets match\r\n\r\n");
#endif
// Deallocate received packet
deallocate(dll.received_packet, dll.received_packet_length);
// Check for no memory leaks
if (mem_leak()) {
put_str("Error: Memory leak\r\n");
print_mem_use();
return 1;
}
return 0;
}