diff --git a/Unity/src/.gitignore b/Unity/src/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2416a678e7d3f25ea206b6982136c2f49dcbb1f1 --- /dev/null +++ b/Unity/src/.gitignore @@ -0,0 +1 @@ +obj/ diff --git a/Unity/src/Makefile b/Unity/src/Makefile index 94b62309d4cc2768103dec82852aeb4d6c9fa67e..9bb394140d25d2d49c443487debe6710624f94d0 100644 --- a/Unity/src/Makefile +++ b/Unity/src/Makefile @@ -1,5 +1,6 @@ TOP=../../quad NAME = unity +DEFINES = $(UNITYDEFINES) include $(TOP)/library.mk diff --git a/Unity/src/unity_config.h b/Unity/src/unity_config.h index f9a7d1a424394cb6c21cdcab20ea7199df8399aa..a2f161a7a7220b13064aac865cc3856e5c2d788c 100755 --- a/Unity/src/unity_config.h +++ b/Unity/src/unity_config.h @@ -173,7 +173,7 @@ * Example: */ /* #define UNITY_FLOAT_PRECISION 0.001f */ -/* #define UNITY_DOUBLE_PRECISION 0.001f +/* #define UNITY_DOUBLE_PRECISION 0.001f */ /* *************************** TOOLSET CUSTOMIZATION *************************** diff --git a/quad/Makefile b/quad/Makefile index 637111f59ecbdd2d28f81804d9f573dfe55508b1..46eecc4cfaf0e087cee2e3a1d82c89909b877078 100644 --- a/quad/Makefile +++ b/quad/Makefile @@ -11,7 +11,6 @@ BOOT = $(OUTDIR)/BOOT.bin all: libs bins libs: - $(MAKE) -C src/test $(MAKE) -C ../Unity/src $(MAKE) -C src/queue $(MAKE) -C src/computation_graph @@ -44,7 +43,7 @@ test: all clean: rm -rf $(INCDIR) $(LIBDIR) $(OUTDIR) $(EXEDIR) - $(MAKE) -C src/test clean + $(MAKE) -C ../Unity/src clean $(MAKE) -C src/queue clean $(MAKE) -C src/computation_graph clean $(MAKE) -C src/graph_blocks clean diff --git a/quad/library.mk b/quad/library.mk index b02b460a3d01d3e874b718cd65ef5fae3578fc66..1a4aa5dd4490cbc4196eaaf22de2fc6efb633e6f 100644 --- a/quad/library.mk +++ b/quad/library.mk @@ -14,6 +14,7 @@ TESTOBJECTS = $(patsubst $.c, %.o, $(TESTSOURCES)) TARGET = $(LIBDIR)/lib$(NAME).a TESTBIN = run_tests +UNITYDEFINES = -DUNITY_INCLUDE_CONFIG_H .PHONY: default test clean @@ -37,7 +38,7 @@ $(TARGET): $(OBJECTS) | $(LIBDIR) $(AR) rcs $@ $^ $(OBJDIR)/%.o : %.c | $(OBJDIR) $(INCDIR) - $(GCC) -c -g -o $@ $< -I$(INCDIR) -Wall + $(GCC) -c -g -o $@ $< -I$(INCDIR) -Wall $(DEFINES) $(INCDIR)/%.h : %.h | $(INCDIR) cp $^ $(INCDIR) @@ -52,4 +53,4 @@ $(LIBDIR): mkdir $(LIBDIR) $(TESTBIN): $(TESTOBJECTS) $(OBJECTS) | default - $(GCC) -g -o $(TESTBIN) $^ -I$(INCDIR) -L$(LIBDIR) $(REQLIBS) + $(GCC) -g -o $(TESTBIN) $^ -I$(INCDIR) -L$(LIBDIR) $(REQLIBS) $(UNITYDEFINES) diff --git a/quad/src/computation_graph/test/test_computation_graph.c b/quad/src/computation_graph/test/test_computation_graph.c index fdd187756cdbe6a49b5649bea2d55b3cc30b6ae2..d5b64e669c5a553842b14eb5b9d9adc5a62c9892 100644 --- a/quad/src/computation_graph/test/test_computation_graph.c +++ b/quad/src/computation_graph/test/test_computation_graph.c @@ -1,6 +1,5 @@ #include "unity.h" - #include "computation_graph.h" #include "graph_blocks.h" diff --git a/quad/src/quad_app/Makefile b/quad/src/quad_app/Makefile index 02cc8d007386acd8b03e61623248cd1e75059829..c9e3ace9eab34682fe9a386a23e30ab1fbb8b237 100644 --- a/quad/src/quad_app/Makefile +++ b/quad/src/quad_app/Makefile @@ -1,6 +1,6 @@ TOP=../.. NAME = quad_app -REQLIBS = -ltest -lcomputation_graph -lm -lqueue -lgraph_blocks -lcommands +REQLIBS = -lunity -lcomputation_graph -lm -lqueue -lgraph_blocks -lcommands include $(TOP)/library.mk diff --git a/quad/src/quad_app/test/test_quad_app.c b/quad/src/quad_app/test/test_quad_app.c index d464d5594a204c1494ec7ebe1b98016b1e033993..12c869c9d12cddc7ee1bbdcaf62591db0666db4a 100644 --- a/quad/src/quad_app/test/test_quad_app.c +++ b/quad/src/quad_app/test/test_quad_app.c @@ -3,6 +3,7 @@ #include "queue.h" #include "unity.h" + struct Queue *queue; struct UARTDriver *uart; @@ -117,8 +118,8 @@ void test_try_receive_packet_fails_when_BE_and_run_out() { TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); TEST_ASSERT_EQUAL(4, bytes_recv); - - TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 1, 2, 3}, packet, 4); + u8 expected[] = {0xBE, 1, 2, 3}; + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, packet, 4); // Try again to verify that we actually ran out TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); @@ -162,7 +163,8 @@ void test_try_receive_packet_fails_when_BE_length_and_run_out() { TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); TEST_ASSERT_EQUAL(9, bytes_recv); - TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2}, packet, 9); + u8 expected[] = {0xBE, 0, 0, 0, 0, 4, 0, 1, 2}; + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, packet, 9); // Try again to verify that we actually ran out TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); @@ -185,7 +187,8 @@ void test_try_receive_packet_fails_when_BE_length_data_and_run_out() { TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); TEST_ASSERT_EQUAL(11, bytes_recv); - TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4}, packet, 11); + u8 expected[] = {0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4}; + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, packet, 11); // Try again to verify that we actually ran out TEST_ASSERT_EQUAL(-1, try_receive_packet(uart)); @@ -229,7 +232,8 @@ void test_try_receive_packet_succeeds() { TEST_ASSERT_EQUAL(0, try_receive_packet(uart)); TEST_ASSERT_EQUAL(12, bytes_recv); - TEST_ASSERT_EQUAL_INT_ARRAY({0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4, checksum}, packet, 12); + u8 expected[] = {0xBE, 0, 0, 0, 0, 4, 0, 1, 2, 3, 4, checksum}; + TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, packet, 12); // Try again to verify that we don't lose the ready packet TEST_ASSERT_EQUAL(0, try_receive_packet(uart)); diff --git a/quad/src/queue/test/test_queue.c b/quad/src/queue/test/test_queue.c index 81e43562e9014b5a30dc39f7543e14a8ef8cabba..9230e39faed165cd2a4c1723ce0bc889c51dda11 100644 --- a/quad/src/queue/test/test_queue.c +++ b/quad/src/queue/test/test_queue.c @@ -1,4 +1,3 @@ -#include "test.h" #include "queue.h" #include "unity.h" diff --git a/quad/src/test/.gitignore b/quad/src/test/.gitignore deleted file mode 100644 index da2dc67a4f0e61a89b67664a64a475efc2adca6d..0000000000000000000000000000000000000000 --- a/quad/src/test/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -run_tests -obj/ -obj-zybo/ diff --git a/quad/src/test/Makefile b/quad/src/test/Makefile deleted file mode 100644 index 2156f7994add62a9f65f41b3b33ffc852097de5d..0000000000000000000000000000000000000000 --- a/quad/src/test/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -TOP=../.. - -NAME = test -REQLIBS = -ltest - -include $(TOP)/library.mk diff --git a/quad/src/test/README.md b/quad/src/test/README.md deleted file mode 100644 index a484745cc317fe93bb0acf11953eb5f4043409fd..0000000000000000000000000000000000000000 --- a/quad/src/test/README.md +++ /dev/null @@ -1,42 +0,0 @@ -Basic Testing Suite in C ----- -THIS IS NOT A DIRECTORY OF TESTS. Rather, it is a library of test utility -functions that you can use to write unit tests. - -The functions in this library help handles the result of every test -whether it be a success, failure, or segfault, and keeps running until -all tests have been executed. It then gives a summary of the test results. - -To use, just write your tests using functions that return `int`s to indicate -failure. 1 means failure. 0 means success. - -Then in your main function for your tests, pass these functions to the `test()` -function along with a name you want included in the test report. - -```c -int main() { - TEST(test_func); - TEST(another_func); - ... -``` - -When writing your `test_func`, you can use the `test_assert` method to perform -basic assertions. If any assertion fails, then `test_func` will fail right -away. If you have multiple assertions in a single `test_func`, the output will -tell you specifically which assertion failed. - -Then at the end of your main function, call the `test_summary()` function, and -return its return value from your main function. - -```c -int main() { - ... - return test_summary(); -} -``` - -An example of creating a `test/` directory in your project is done in this -folder. To run the unit tests in your project, run -``` -make test -``` \ No newline at end of file diff --git a/quad/src/test/test.c b/quad/src/test/test.c deleted file mode 100644 index a5573bf45b29ef67c3850e560e7c5932c13b1bb8..0000000000000000000000000000000000000000 --- a/quad/src/test/test.c +++ /dev/null @@ -1,73 +0,0 @@ -#include "test.h" -#include <unistd.h> - -static int num_tests = 0; -static struct Test tests[255]; -static int longest_test_name = 0; -static int num_assertions = 0; - -void test(int (*function)(), char *test_name) { - int test_name_length = strlen(test_name); - if (test_name_length > longest_test_name) { - longest_test_name = test_name_length; - } - - pid_t pid = fork(); - if (pid == 0) { - // test process - puts("----------------------------------------"); - printf("#%3d: %s\n", num_tests + 1, test_name); - int exit_status = function(); - exit(exit_status); - } else { - int status; - waitpid(pid, &status, 0); - - struct Test test; - strcpy(test.name, test_name); - - if (WIFEXITED(status)) { - int exit_status = WEXITSTATUS(status); - if (exit_status == 0) { - test.failed = 0; - strcpy(test.result_msg, "passed"); - } else { - test.failed = 1; - strcpy(test.result_msg, "FAILED"); - } - } else if (WIFSIGNALED(status)) { - test.failed = 1; - strcpy(test.result_msg, "ERROR!"); - } - - tests[num_tests++] = test; - } -} - -void test_assert(int success) { - num_assertions += 1; - if (!success) { - printf("FAILED assertion #%d\n", num_assertions); - exit(1); - } -} - -int test_summary() { - unsigned char at_least_one_test_failed = 0; - int num_failed = 0; - puts("--------------------------------------------------------------------------------"); - puts("Test results:"); - puts(""); - int i = 0; - for (i = 0; i < num_tests; i += 1) { - printf("#%3d: %-*s (%s)\n", i + 1, longest_test_name, tests[i].name, tests[i].result_msg); - num_failed += tests[i].failed; - at_least_one_test_failed |= tests[i].failed; - } - puts(""); - printf("Total: %3d of %-3d tests passed\n", num_tests - num_failed, num_tests); - puts("--------------------------------------------------------------------------------"); - num_tests = 0; - longest_test_name = 0; - return at_least_one_test_failed; -} diff --git a/quad/src/test/test.h b/quad/src/test/test.h deleted file mode 100644 index ac50248ece65f20b0060e5ae7f5509a38a8fb821..0000000000000000000000000000000000000000 --- a/quad/src/test/test.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef TEST_H -#define TEST_H - -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include <sys/types.h> -#include <sys/wait.h> - -#define TEST(name) test(name, #name) - -struct Test { - char name[255]; - char result_msg[255]; - unsigned char failed; -}; - -void test(int (*)(), char *); -void test_assert(int success); -int test_summary(); - -#endif diff --git a/quad/src/test/test/example.c b/quad/src/test/test/example.c deleted file mode 100644 index 7a114d81d3154cababc6eb7c621c11c5dc5e0c33..0000000000000000000000000000000000000000 --- a/quad/src/test/test/example.c +++ /dev/null @@ -1,32 +0,0 @@ -#include <stdio.h> -#include "test.h" - -int test_always_passes() { - puts("hi world."); - return 0; -} - -int test_always_fails() { - return 1; -} - -int test_assertions() { - test_assert(1 == 1); - test_assert(2 == 3); - test_assert(3 == 3); - return 0; -} - -int test_always_segfaults() { - int *bad = NULL; - int x = *bad; - return 0; -} - -int main() { - TEST(test_always_passes); - TEST(test_always_fails); - TEST(test_assertions); - TEST(test_always_segfaults); - return test_summary(); -}