Skip to content
Snippets Groups Projects
Commit 931ee3f8 authored by mkelly2's avatar mkelly2
Browse files

Fix tests and remove old testing library

parent a75dfeff
No related branches found
No related tags found
No related merge requests found
obj/
TOP=../../quad
NAME = unity
DEFINES = $(UNITYDEFINES)
include $(TOP)/library.mk
......@@ -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 ***************************
......
......@@ -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
......
......@@ -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)
#include "unity.h"
#include "computation_graph.h"
#include "graph_blocks.h"
......
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
......@@ -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));
......
#include "test.h"
#include "queue.h"
#include "unity.h"
......
run_tests
obj/
obj-zybo/
TOP=../..
NAME = test
REQLIBS = -ltest
include $(TOP)/library.mk
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
#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;
}
#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
#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();
}
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