diff --git a/quad/src/computation_graph/test/test_computation_graph.c b/quad/src/computation_graph/test/test_computation_graph.c index f44d713813d72502f7a8403c67ad8bfe6717a850..e51880b4ac92d82d7d861e27124ff0a1f6c4ca59 100644 --- a/quad/src/computation_graph/test/test_computation_graph.c +++ b/quad/src/computation_graph/test/test_computation_graph.c @@ -1,4 +1,4 @@ -#include "test.h" +#include "unity.h" #include "computation_graph.h" @@ -6,14 +6,8 @@ #define GRAPH_TEST_EPS 0.00001 -static int nequal(double val1, double val2) { - if (fabs(val1 - val2) < GRAPH_TEST_EPS) { - return 0; - } - return -1; -} -int test_adding_2_numbers() { +void test_adding_2_numbers() { struct computation_graph *graph = create_graph(); int block = graph_add_defined_block(graph, BLOCK_ADD, "Add"); int cblock3 = graph_add_defined_block(graph, BLOCK_CONSTANT, "3"); @@ -25,11 +19,11 @@ int test_adding_2_numbers() { int to_compute_for[1] = {block}; graph_compute_nodes(graph, to_compute_for, 1); double result = graph_get_output(graph, block, ADD_SUM); - return nequal(result, 7); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 7, result); } -int test_computing_cycles() { +void test_computing_cycles() { struct computation_graph *graph = create_graph(); int gain1 = graph_add_defined_block(graph, BLOCK_GAIN, "gain1"); int gain2 = graph_add_defined_block(graph, BLOCK_GAIN, "gain2"); @@ -38,20 +32,19 @@ int test_computing_cycles() { int to_compute_for[1] = {gain2}; graph_compute_nodes(graph, to_compute_for, 1); // If no infinite loop, then success. Value is undefined for circular graphs - return 0; } -int test_resetting_cycles() { +void test_resetting_cycles() { struct computation_graph *graph = create_graph(); int acum1 = graph_add_defined_block(graph, BLOCK_ACCUMULATE, "accumulator1"); int acum2 = graph_add_defined_block(graph, BLOCK_ACCUMULATE, "accumulator2"); graph_set_source(graph, acum2, ACCUM_IN, acum1, ACCUMULATED); graph_set_source(graph, acum1, ACCUM_IN, acum2, ACCUMULATED); - return 0; // Passes if no infinite loop + // Passes if no infinite loop } // Tests the accumulator block, thereby testing reset and state changes -int test_accumulator_state() { +void test_accumulator_state() { struct computation_graph *graph = create_graph(); int cblock = graph_add_defined_block(graph, BLOCK_CONSTANT, "const"); int acum_b = graph_add_defined_block(graph, BLOCK_ACCUMULATE, "accumulator"); @@ -66,10 +59,9 @@ int test_accumulator_state() { graph_compute_nodes(graph, to_compute_for, 1); double result = graph_get_output(graph, acum_b, ACCUMULATED); - if (nequal(result, 9)) { - printf("graph_test_accumulator failed on step 1, equals %f\n", result); - return -1; - } + + TEST_ASSERT_DOUBLE_WITHIN_MESSAGE(GRAPH_TEST_EPS, 9, result, "graph_test_accumulator failed on step 1"); + // Test reset on source set int gain_b = graph_add_defined_block(graph, BLOCK_GAIN, "Gain"); @@ -78,16 +70,12 @@ int test_accumulator_state() { to_compute_for[0] = gain_b; graph_compute_nodes(graph, to_compute_for, 1); result = graph_get_output(graph, gain_b, GAIN_RESULT); - if (nequal(result, -2)) { - printf("graph_test_accumulator failed on step 2\n"); - return -2; - } - return 0; + TEST_ASSERT_DOUBLE_WITHIN_MESSAGE(GRAPH_TEST_EPS, -2, result, "graph_test_accumulator failed on step 2"); } // Tests that a block will only execute once per compute, // even if its output is connected to multiple inputs -int test_that_blocks_only_get_executed_once() { +void test_that_blocks_only_get_executed_once() { struct computation_graph *graph = create_graph(); int acum_b = graph_add_defined_block(graph, BLOCK_ACCUMULATE, "accumulator"); int add_block = graph_add_defined_block(graph, BLOCK_ADD, "Add"); @@ -102,11 +90,11 @@ int test_that_blocks_only_get_executed_once() { int to_compute_for[1] = {add_block}; graph_compute_nodes(graph, to_compute_for, 1); double result = graph_get_output(graph, add_block, ADD_SUM); - return nequal(result, 4); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 4, result); } // Tests that upon connection of a second child, a block will not reset -int tests_that_already_connected_blocks_dont_get_reset() { +void tests_that_already_connected_blocks_dont_get_reset() { struct computation_graph *graph = create_graph(); int cblock = graph_add_defined_block(graph, BLOCK_CONSTANT, "5"); graph_set_param_val(graph, cblock, CONST_SET, 5); @@ -129,19 +117,18 @@ int tests_that_already_connected_blocks_dont_get_reset() { double result = graph_get_output(graph, gain2, GAIN_RESULT); // Equals 5 and not 10 because the inputs to the accumulator did not change, // so it didn't run again' - return nequal(result, 5); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 5, result); } -int test_that_a_self_loop_computation_terminates() { +void test_that_a_self_loop_computation_terminates() { struct computation_graph *graph = create_graph(); int gain1 = graph_add_defined_block(graph, BLOCK_GAIN, "gain1"); graph_set_source(graph, gain1, GAIN_INPUT, gain1, GAIN_RESULT); int to_compute_for[1] = {gain1}; graph_compute_nodes(graph, to_compute_for, 1); - return 0; } -int test_that_nodes_only_update_when_their_inputs_change() { +void test_that_nodes_only_update_when_their_inputs_change() { struct computation_graph *graph = create_graph(); int cblock = graph_add_defined_block(graph, BLOCK_CONSTANT, "const"); int acum_b = graph_add_defined_block(graph, BLOCK_ACCUMULATE, "accumulator"); @@ -153,7 +140,7 @@ int test_that_nodes_only_update_when_their_inputs_change() { graph_compute_nodes(graph, to_compute_for, 1); double result = graph_get_output(graph, acum_b, ACCUMULATED); - return nequal(result, 3); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 3, result); } /* @@ -161,7 +148,7 @@ C1 --->| accum_b1 --->| | Add ---> C2 --->| accum_b2 --->| */ -int test_that_updates_propagate_only_to_their_children() { +void test_that_updates_propagate_only_to_their_children() { struct computation_graph *graph = create_graph(); int cblock1 = graph_add_defined_block(graph, BLOCK_CONSTANT, "const1"); int cblock2 = graph_add_defined_block(graph, BLOCK_CONSTANT, "const2"); @@ -181,7 +168,8 @@ int test_that_updates_propagate_only_to_their_children() { graph_compute_nodes(graph, to_compute_for, 1); double result2 = graph_get_output(graph, add_b, ADD_SUM); - return nequal(result1, 7) || nequal(result2, 8); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 7, result1); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 8, result2); } /* @@ -192,7 +180,7 @@ This caused problems, because if a node had its output to two things, then it wo Since it didn't get marked as "updated" when it got connected to the computation path, and it had its original "updated" flag cleared, the node would never get set. */ -int test_that_nodes_get_executed_when_updated_even_if_disconnected() { +void test_that_nodes_get_executed_when_updated_even_if_disconnected() { printf("\n\n---------\n"); struct computation_graph *graph = create_graph(); int d_block = graph_add_defined_block(graph, BLOCK_CONSTANT, "const1"); @@ -210,38 +198,29 @@ int test_that_nodes_get_executed_when_updated_even_if_disconnected() { graph_compute_nodes(graph, to_compute_for, 1); double set_val = graph_get_output(graph, gain_block, GAIN_RESULT); - return nequal(set_val, 2*1.2345); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 2*1.2345, set_val); } -int test_that_the_get_source_call_works_normally() { +void test_that_the_get_source_call_works_normally() { struct computation_graph *graph = create_graph(); int add_block = graph_add_defined_block(graph, BLOCK_ADD, "Add"); int cblock3 = graph_add_defined_block(graph, BLOCK_CONSTANT, "3"); graph_set_source(graph, add_block, ADD_SUMMAND1, cblock3, CONST_VAL); struct node_src source = graph_get_source(graph, add_block, ADD_SUMMAND1); - if (source.controller_id != cblock3 || source.controller_output != CONST_VAL) { - return 1; - } - else { - return 0; - } + TEST_ASSERT_EQUAL_INT(cblock3, source.controller_id); + TEST_ASSERT_EQUAL_INT(CONST_VAL, source.controller_output); } -int test_that_the_get_source_call_returns_ID_neg_1_when_invalid_ID_is_passed() { +void test_that_the_get_source_call_returns_ID_neg_1_when_invalid_ID_is_passed() { struct computation_graph *graph = create_graph(); int add_block = graph_add_defined_block(graph, BLOCK_ADD, "Add"); struct node_src source = graph_get_source(graph, 123, ADD_SUMMAND1); - if (source.controller_id != -1) { - return 1; - } - else { - return 0; - } + TEST_ASSERT_EQUAL_INT(-1, source.controller_id); } -int test_that_new_nodes_can_be_created_by_ID() { +void test_that_new_nodes_can_be_created_by_ID() { struct computation_graph *graph = create_graph(); int desired_id = 87; int add_block = graph_add_node_id(graph, desired_id, "Add", &node_add_type); @@ -260,22 +239,23 @@ int test_that_new_nodes_can_be_created_by_ID() { double result = graph_get_output(graph, add_block, ADD_SUM); printf("n_nodes: %d, size: %d\n", graph->n_nodes, graph->size); printf("result: %f", result); - return nequal(result, 3.5 + 2.5); + TEST_ASSERT_DOUBLE_WITHIN(GRAPH_TEST_EPS, 3.5 + 2.5, result); } int main() { - TEST(test_adding_2_numbers); - TEST(test_computing_cycles); - TEST(test_resetting_cycles); - TEST(test_accumulator_state); - TEST(test_that_blocks_only_get_executed_once); - TEST(tests_that_already_connected_blocks_dont_get_reset); - TEST(test_that_a_self_loop_computation_terminates); - TEST(test_that_nodes_only_update_when_their_inputs_change); - TEST(test_that_updates_propagate_only_to_their_children); - TEST(test_that_nodes_get_executed_when_updated_even_if_disconnected); - TEST(test_that_the_get_source_call_works_normally); - TEST(test_that_the_get_source_call_returns_ID_neg_1_when_invalid_ID_is_passed); - TEST(test_that_new_nodes_can_be_created_by_ID); - return test_summary(); + UNITY_BEGIN(); + RUN_TEST(test_adding_2_numbers); + RUN_TEST(test_computing_cycles); + RUN_TEST(test_resetting_cycles); + RUN_TEST(test_accumulator_state); + RUN_TEST(test_that_blocks_only_get_executed_once); + RUN_TEST(tests_that_already_connected_blocks_dont_get_reset); + RUN_TEST(test_that_a_self_loop_computation_terminates); + RUN_TEST(test_that_nodes_only_update_when_their_inputs_change); + RUN_TEST(test_that_updates_propagate_only_to_their_children); + RUN_TEST(test_that_nodes_get_executed_when_updated_even_if_disconnected); + RUN_TEST(test_that_the_get_source_call_works_normally); + RUN_TEST(test_that_the_get_source_call_returns_ID_neg_1_when_invalid_ID_is_passed); + RUN_TEST(test_that_new_nodes_can_be_created_by_ID); + return UNITY_END(); }