Skip to content
Snippets Groups Projects
Commit a0181ded authored by Peter Thedens's avatar Peter Thedens
Browse files

fix quad app test

parent 5d7037c6
No related branches found
No related tags found
No related merge requests found
...@@ -4,8 +4,6 @@ ...@@ -4,8 +4,6 @@
#include "computation_graph.h" #include "computation_graph.h"
#include "graph_blocks.h" #include "graph_blocks.h"
#define UNITY_INCLUDE_CONFIG_H
#define GRAPH_TEST_EPS 0.00001 #define GRAPH_TEST_EPS 0.00001
......
#include "communication.h" #include "communication.h"
#include <string.h> #include <string.h>
#include "queue.h" #include "queue.h"
#include "test.h" #include "unity.h"
struct Queue *queue; struct Queue *queue;
struct UARTDriver *uart; struct UARTDriver *uart;
...@@ -67,8 +67,11 @@ extern u8 packet[MAX_PACKET_SIZE]; ...@@ -67,8 +67,11 @@ extern u8 packet[MAX_PACKET_SIZE];
extern int bytes_recv; extern int bytes_recv;
extern unsigned char packet_checksum; extern unsigned char packet_checksum;
/*--------- TESTS ---------*/
// Test fails when no BE and run out // Test fails when no BE and run out
int test_try_receive_packet_fails_when_no_BE_and_run_out() { void test_try_receive_packet_fails_when_no_BE_and_run_out() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(5); queue = queue_malloc(5);
...@@ -76,35 +79,32 @@ int test_try_receive_packet_fails_when_no_BE_and_run_out() { ...@@ -76,35 +79,32 @@ int test_try_receive_packet_fails_when_no_BE_and_run_out() {
queue_add(queue, 0); queue_add(queue, 0);
queue_add(queue, 1); queue_add(queue, 1);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 0); TEST_ASSERT_EQUAL(0, bytes_recv);
// Try again to verify that we actually ran out
test_assert(try_receive_packet(uart));
test_assert(bytes_recv == 0);
return 0; // Try again to verify that we actually ran out
TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
TEST_ASSERT_EQUAL(0, bytes_recv);
} }
// Test fails when no BE and too much (check resume) // Test fails when no BE and too much (check resume)
int test_try_receive_packet_fails_when_no_BE_and_too_much() { void test_try_receive_packet_fails_when_no_BE_and_too_much() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
int size = 255; int size = 255;
queue = queue_malloc(size); queue = queue_malloc(size);
queue_add_corruption(queue, size); queue_add_corruption(queue, size);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 0); TEST_ASSERT_EQUAL(0, bytes_recv);
// Ensure that we quit trying // Ensure that we quit trying
test_assert(size - queue_size(queue) <= MAX_PACKET_SIZE); TEST_ASSERT_TRUE(size - queue_size(queue) <= MAX_PACKET_SIZE);
return 0;
} }
// Test fails when BE and run out (check resume) // Test fails when BE and run out (check resume)
int test_try_receive_packet_fails_when_BE_and_run_out() { void test_try_receive_packet_fails_when_BE_and_run_out() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(100); queue = queue_malloc(100);
...@@ -115,23 +115,18 @@ int test_try_receive_packet_fails_when_BE_and_run_out() { ...@@ -115,23 +115,18 @@ int test_try_receive_packet_fails_when_BE_and_run_out() {
queue_add(queue, 2); queue_add(queue, 2);
queue_add(queue, 3); queue_add(queue, 3);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 4); TEST_ASSERT_EQUAL(4, bytes_recv);
test_assert(packet[0] == 0xBE); TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 1, 2, 3}, packet, 4);
test_assert(packet[1] == 1);
test_assert(packet[2] == 2);
test_assert(packet[3] == 3);
// Try again to verify that we actually ran out // Try again to verify that we actually ran out
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 4); TEST_ASSERT_EQUAL(4, bytes_recv);
return 0;
} }
// Test fails when BE and nonsensical length (check resume) // Test fails when BE and nonsensical length (check resume)
int test_try_receive_packet_fails_when_BE_and_big_length() { void test_try_receive_packet_fails_when_BE_and_big_length() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(500); queue = queue_malloc(500);
...@@ -143,18 +138,16 @@ int test_try_receive_packet_fails_when_BE_and_big_length() { ...@@ -143,18 +138,16 @@ int test_try_receive_packet_fails_when_BE_and_big_length() {
queue_add_short(queue, 500); queue_add_short(queue, 500);
for (i = 0; i < 10; i += 1) queue_add_corruption(queue, i); for (i = 0; i < 10; i += 1) queue_add_corruption(queue, i);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 0); TEST_ASSERT_EQUAL(0, bytes_recv);
// Try again to verify that we actually ran out // Try again to verify that we actually ran out
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 0); TEST_ASSERT_EQUAL(0, bytes_recv);
return 0;
} }
// Test fails when BE and length and run out (check resume) // Test fails when BE and length and run out (check resume)
int test_try_receive_packet_fails_when_BE_length_and_run_out() { void test_try_receive_packet_fails_when_BE_length_and_run_out() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(500); queue = queue_malloc(500);
...@@ -167,27 +160,17 @@ int test_try_receive_packet_fails_when_BE_length_and_run_out() { ...@@ -167,27 +160,17 @@ int test_try_receive_packet_fails_when_BE_length_and_run_out() {
unsigned char data[4] = {1, 2, 3, 4}; unsigned char data[4] = {1, 2, 3, 4};
for (i = 0; i < 2; i += 1) queue_add(queue, data[i]); for (i = 0; i < 2; i += 1) queue_add(queue, data[i]);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 9); TEST_ASSERT_EQUAL(9, bytes_recv);
test_assert(packet[0] == 0xBE); TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2}, packet, 9);
test_assert(packet[1] == 0);
test_assert(packet[2] == 0);
test_assert(packet[3] == 0);
test_assert(packet[4] == 0);
test_assert(packet[5] == 4);
test_assert(packet[6] == 0);
test_assert(packet[7] == 1);
test_assert(packet[8] == 2);
// Try again to verify that we actually ran out // Try again to verify that we actually ran out
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 9); TEST_ASSERT_EQUAL(9, bytes_recv);
return 0;
} }
// Test fails when BE and length and data and run out (check resume) // Test fails when BE and length and data and run out (check resume)
int test_try_receive_packet_fails_when_BE_length_data_and_run_out() { void test_try_receive_packet_fails_when_BE_length_data_and_run_out() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(500); queue = queue_malloc(500);
...@@ -200,29 +183,17 @@ int test_try_receive_packet_fails_when_BE_length_data_and_run_out() { ...@@ -200,29 +183,17 @@ int test_try_receive_packet_fails_when_BE_length_data_and_run_out() {
unsigned char data[4] = {1, 2, 3, 4}; unsigned char data[4] = {1, 2, 3, 4};
for (i = 0; i < 4; i += 1) queue_add(queue, data[i]); for (i = 0; i < 4; i += 1) queue_add(queue, data[i]);
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 11); TEST_ASSERT_EQUAL(11, bytes_recv);
test_assert(packet[0] == 0xBE); TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4}, packet, 11);
test_assert(packet[1] == 0);
test_assert(packet[2] == 0);
test_assert(packet[3] == 0);
test_assert(packet[4] == 0);
test_assert(packet[5] == 4);
test_assert(packet[6] == 0);
test_assert(packet[7] == 1);
test_assert(packet[8] == 2);
test_assert(packet[9] == 3);
test_assert(packet[10] == 4);
// Try again to verify that we actually ran out // Try again to verify that we actually ran out
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 11); TEST_ASSERT_EQUAL(11, bytes_recv);
return 0;
} }
// Test fails when BE, length, data, and checksum fails // Test fails when BE, length, data, and checksum fails
int test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum() { void test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(500); queue = queue_malloc(500);
...@@ -237,58 +208,43 @@ int test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum() { ...@@ -237,58 +208,43 @@ int test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum() {
queue_add(queue, 0xFF); // bad checksum queue_add(queue, 0xFF); // bad checksum
queue_add(queue, 0xBE); // next start queue_add(queue, 0xBE); // next start
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 1); TEST_ASSERT_EQUAL(1, bytes_recv);
test_assert(packet[0] == 0xBE); TEST_ASSERT_EQUAL(0xBE, packet[0]);
// Try again to verify that we actually ran out // Try again to verify that we actually ran out
test_assert(try_receive_packet(uart)); TEST_ASSERT_EQUAL(-1, try_receive_packet(uart));
test_assert(bytes_recv == 1); TEST_ASSERT_EQUAL(1, bytes_recv);
return 0;
} }
// Test succeeds when BE, length, data, checksum // Test succeeds when BE, length, data, checksum
int test_try_receive_packet_succeeds() { void test_try_receive_packet_succeeds() {
bytes_recv = 0; bytes_recv = 0;
uart = mock_uart_malloc(); uart = mock_uart_malloc();
queue = queue_malloc(500); queue = queue_malloc(500);
int i; int i;
queue_add_corruption(queue, 10);; queue_add_corruption(queue, 10);
unsigned char data[4] = {1, 2, 3, 4}; unsigned char data[4] = {1, 2, 3, 4};
unsigned char checksum = queue_add_packet(queue, 0, 0, 4, data); unsigned char checksum = queue_add_packet(queue, 0, 0, 4, data);
int failure = try_receive_packet(uart); TEST_ASSERT_EQUAL(0, try_receive_packet(uart));
test_assert(!failure); TEST_ASSERT_EQUAL(12, bytes_recv);
test_assert(bytes_recv == 12); TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4, checksum}, packet, 12);
test_assert(packet[0] == 0xBE);
test_assert(packet[1] == 0);
test_assert(packet[2] == 0);
test_assert(packet[3] == 0);
test_assert(packet[4] == 0);
test_assert(packet[5] == 4);
test_assert(packet[6] == 0);
test_assert(packet[7] == 1);
test_assert(packet[8] == 2);
test_assert(packet[9] == 3);
test_assert(packet[10] == 4);
test_assert(packet[11] == checksum);
// Try again to verify that we don't lose the ready packet // Try again to verify that we don't lose the ready packet
test_assert(!try_receive_packet(uart)); TEST_ASSERT_EQUAL(0, try_receive_packet(uart));
test_assert(bytes_recv == 12); TEST_ASSERT_EQUAL(12, bytes_recv);
return 0;
} }
int main() { int main() {
TEST(test_try_receive_packet_fails_when_no_BE_and_run_out); UNITY_BEGIN();
TEST(test_try_receive_packet_fails_when_no_BE_and_too_much); RUN_TEST(test_try_receive_packet_fails_when_no_BE_and_run_out);
TEST(test_try_receive_packet_fails_when_BE_and_run_out); RUN_TEST(test_try_receive_packet_fails_when_no_BE_and_too_much);
TEST(test_try_receive_packet_fails_when_BE_and_big_length); RUN_TEST(test_try_receive_packet_fails_when_BE_and_run_out);
TEST(test_try_receive_packet_fails_when_BE_length_and_run_out); RUN_TEST(test_try_receive_packet_fails_when_BE_and_big_length);
TEST(test_try_receive_packet_fails_when_BE_length_data_and_run_out); RUN_TEST(test_try_receive_packet_fails_when_BE_length_and_run_out);
TEST(test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum); RUN_TEST(test_try_receive_packet_fails_when_BE_length_data_and_run_out);
TEST(test_try_receive_packet_succeeds); RUN_TEST(test_try_receive_packet_fails_when_BE_length_data_and_bad_checksum);
return test_summary(); RUN_TEST(test_try_receive_packet_succeeds);
return UNITY_END();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment