3#include "externals/unity.h"
10size_t output_str_size;
13char input_str[MAXMESSAGELEN + 1];
14char output_str[MAXMESSAGELEN + 1];
16void send_out(uint8_t
byte);
17uint8_t receive_in(
void);
18bool anything_to_receive(
void);
19void null_inout_strs(
void);
22const char *thisMessage;
25const char *messages[nMessages] = {
29 ">1234567890ABCDEF.9411\n",
30 ">FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.39DE\n",
31 ">00000000000000000000000000000000000000000.1DC1\n",
32 ">345666666666666666666666666666666666666666666666666666666.C7FB\n",
36 ">defxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.350F\n",
37 ">AFw0123456789.A86F\n"};
40const char *badMessages[nBadMessages] = {
41 ">",
"\n",
">\n",
">abc\n",
">abcC103\n",
45void send_out(uint8_t
byte) {
46 if (i_output_str < MAXMESSAGELEN) {
47 output_str[i_output_str++] = (char)
byte;
49 printf(
"output_str: %s\n", output_str);
51 "trying to send message larger than MAXMESSAGELEN using send_out");
55uint8_t receive_in(
void) {
56 if (anything_to_receive() && i_input_str < MAXMESSAGELEN) {
57 return (uint8_t)input_str[i_input_str++];
59 printf(
"input_str: %s\n", input_str);
61 "trying to receive_in when nothing to receive (problem in test code?)");
66bool anything_to_receive(
void) {
return i_input_str < input_str_size; }
68void setup_inout_strs(
const char *instr) {
73 for (
size_t i = 0; i < MAXMESSAGELEN + 1; i++) {
77 strncpy(input_str, instr, MAXMESSAGELEN + 1);
78 for (
size_t i = 0; i < MAXMESSAGELEN + 1; i++) {
79 if (input_str[i] ==
'\0')
94void test_ascii_serial_com_loopback(
void) {
96 char ascVersion, appVersion, command, dataBuffer[MAXDATALEN];
104 size_t iExtraIters = 0;
106 for (
size_t iMessage = 0; iMessage < nMessages; iMessage++) {
107 setup_inout_strs(messages[iMessage]);
108 while (anything_to_receive() ||
110 if (anything_to_receive()) {
115 &asc, &ascVersion, &appVersion, &command, dataBuffer, &dataLen);
116 if (command !=
'\0') {
118 &asc, ascVersion, appVersion, command, dataBuffer, dataLen);
124 if (!(anything_to_receive() ||
135 TEST_ASSERT_EQUAL_STRING(messages[iMessage], output_str);
139 printf(
"Uncaught exception: %u\n", e1);
140 TEST_FAIL_MESSAGE(
"Uncaught exception!");
144void test_ascii_serial_com_loopback_random_order(
void) {
146 char ascVersion, appVersion, command, dataBuffer[MAXDATALEN];
154 size_t iExtraIters = 0;
156 const size_t nTries = 200;
157 for (
size_t iTry = 0; iTry < nTries; iTry++) {
158 const size_t iMessage = random() % nMessages;
159 setup_inout_strs(messages[iMessage]);
160 while (anything_to_receive() ||
162 if (anything_to_receive()) {
167 &asc, &ascVersion, &appVersion, &command, dataBuffer, &dataLen);
168 if (command !=
'\0') {
170 &asc, ascVersion, appVersion, command, dataBuffer, dataLen);
176 if (!(anything_to_receive() ||
189 TEST_ASSERT_EQUAL_STRING(messages[iMessage], output_str);
193 printf(
"Uncaught exception: %u\n", e1);
194 TEST_FAIL_MESSAGE(
"Uncaught exception!");
198void test_ascii_serial_com_loopback_random_order_bad_messages(
void) {
200 char ascVersion, appVersion, command, dataBuffer[MAXDATALEN];
207 size_t iExtraIters = 0;
209 const size_t nTries = 200;
210 for (
size_t iTry = 0; iTry < nTries; iTry++) {
211 bool isBadMessage = random() % 5000 < 500;
212 if (iTry == nTries - 1)
213 isBadMessage =
false;
215 const size_t iBadMessage = random() % nBadMessages;
216 setup_inout_strs(badMessages[iBadMessage]);
217 thisMessage = badMessages[iBadMessage];
219 const size_t iMessage = random() % nMessages;
220 setup_inout_strs(messages[iMessage]);
221 thisMessage = messages[iMessage];
228 if (anything_to_receive()) {
233 &asc, &ascVersion, &appVersion, &command, dataBuffer, &dataLen);
234 if (command !=
'\0') {
236 &asc, ascVersion, appVersion, command, dataBuffer, dataLen);
242 if (!(anything_to_receive() ||
252 if (e1 == ASC_ERROR_INVALID_FRAME_PERIOD) {
253 printf(
"Uncaught exception: ASC_ERROR_INVALID_FRAME_PERIOD\n");
257 printf(
"Uncaught exception: %u\n", e1);
258 TEST_FAIL_MESSAGE(
"Uncaught exception!");
263 TEST_ASSERT_EQUAL_STRING(
"", output_str);
269 TEST_ASSERT_EQUAL_STRING(thisMessage, output_str);
276 RUN_TEST(test_ascii_serial_com_loopback);
277 RUN_TEST(test_ascii_serial_com_loopback_random_order);
278 RUN_TEST(test_ascii_serial_com_loopback_random_order_bad_messages);
circular_buffer_uint8 * ascii_serial_com_get_output_buffer(ascii_serial_com *asc)
ASCII Serial Com get output buffer.
void ascii_serial_com_init(ascii_serial_com *asc)
ASCII Serial Com Interface init method.
void ascii_serial_com_put_message_in_output_buffer(ascii_serial_com *asc, char ascVersion, char appVersion, char command, const char *data, size_t dataLen)
ASCII Serial Com Pack and put message in output buffer.
void ascii_serial_com_get_message_from_input_buffer(ascii_serial_com *asc, char *ascVersion, char *appVersion, char *command, char *data, size_t *dataLen)
ASCII Serial Com pop message from input buffer and unpack.
circular_buffer_uint8 * ascii_serial_com_get_input_buffer(ascii_serial_com *asc)
ASCII Serial Com get input buffer.
uint8_t circular_buffer_pop_front_uint8(circular_buffer_uint8 *circ_buf)
circular buffer pop front
bool circular_buffer_is_empty_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get if empty
void circular_buffer_push_back_uint8(circular_buffer_uint8 *circ_buf, const uint8_t element)
circular buffer push back
ASCII Serial Com Interface State struct.