From 931ee3f8a1b1f25086f86b33988cb755f4f6c854 Mon Sep 17 00:00:00 2001 From: Matthew Kelly <mkelly2@iastate.edu> Date: Wed, 8 Nov 2017 15:28:43 -0600 Subject: [PATCH] Fix tests and remove old testing library --- Unity/src/.gitignore | 1 + Unity/src/Makefile | 1 + Unity/src/unity_config.h | 2 +- quad/Makefile | 3 +- quad/library.mk | 5 +- .../test/test_computation_graph.c | 1 - quad/src/quad_app/Makefile | 2 +- quad/src/quad_app/test/test_quad_app.c | 14 ++-- quad/src/queue/test/test_queue.c | 1 - quad/src/test/.gitignore | 3 - quad/src/test/Makefile | 6 -- quad/src/test/README.md | 42 ----------- quad/src/test/test.c | 73 ------------------- quad/src/test/test.h | 22 ------ quad/src/test/test/example.c | 32 -------- 15 files changed, 17 insertions(+), 191 deletions(-) create mode 100644 Unity/src/.gitignore delete mode 100644 quad/src/test/.gitignore delete mode 100644 quad/src/test/Makefile delete mode 100644 quad/src/test/README.md delete mode 100644 quad/src/test/test.c delete mode 100644 quad/src/test/test.h delete mode 100644 quad/src/test/test/example.c diff --git a/Unity/src/.gitignore b/Unity/src/.gitignore new file mode 100644 index 000000000..2416a678e --- /dev/null +++ b/Unity/src/.gitignore @@ -0,0 +1 @@ +obj/ diff --git a/Unity/src/Makefile b/Unity/src/Makefile index 94b62309d..9bb394140 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 f9a7d1a42..a2f161a7a 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 637111f59..46eecc4cf 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 b02b460a3..1a4aa5dd4 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 fdd187756..d5b64e669 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 02cc8d007..c9e3ace9e 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 d464d5594..12c869c9d 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 81e43562e..9230e39fa 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 da2dc67a4..000000000 --- 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 2156f7994..000000000 --- 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 a484745cc..000000000 --- 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 a5573bf45..000000000 --- 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 ac50248ec..000000000 --- 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 7a114d81d..000000000 --- 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(); -} -- GitLab