aboutsummaryrefslogtreecommitdiff
path: root/02-usart/test_harness/test_harness.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2020-11-16 18:20:32 -0700
committerJosh Rahm <joshuarahm@gmail.com>2020-11-16 18:20:32 -0700
commit9f17335c19a6ae91a450e267b5313148644a7a14 (patch)
treef1ae946ada62148913a8cc10bc985f8de926a4a0 /02-usart/test_harness/test_harness.c
parent4767c73fb2e1f96469fe24a83b443c1774b01d86 (diff)
downloadstm32l4-9f17335c19a6ae91a450e267b5313148644a7a14.tar.gz
stm32l4-9f17335c19a6ae91a450e267b5313148644a7a14.tar.bz2
stm32l4-9f17335c19a6ae91a450e267b5313148644a7a14.zip
Add DMA header file which defines the DMA registers and add
testing_harness with fake environment to allow testing on x86 development machines.
Diffstat (limited to '02-usart/test_harness/test_harness.c')
-rw-r--r--02-usart/test_harness/test_harness.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/02-usart/test_harness/test_harness.c b/02-usart/test_harness/test_harness.c
new file mode 100644
index 0000000..9f7dd79
--- /dev/null
+++ b/02-usart/test_harness/test_harness.c
@@ -0,0 +1,82 @@
+#include "test_harness.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <setjmp.h>
+
+
+static jmp_buf jmpbuf;
+
+volatile test_t dummy
+ __attribute((__section__("tests")))
+ __attribute((__used__)) = {
+ "dummy",
+ "dummy",
+ NULL
+};
+
+extern test_t __start_tests;
+extern test_t __stop_tests;
+
+test_t* iter = &__start_tests;
+
+static int execute_test(test_t* test);
+
+int main() {
+ for( ; iter < &__stop_tests; ++ iter) {
+ if (iter->fn_ptr != NULL) {
+ execute_test(iter);
+ }
+ }
+}
+
+void test_harness_abort(int ec)
+{
+ longjmp(jmpbuf, ec);
+ assert("Long jump failed.\n");
+}
+
+static int execute_test(test_t* test)
+{
+ char fullname[512];
+ int status;
+ int ec;
+ pid_t pid;
+ const char* note = "";
+
+ snprintf(
+ fullname, sizeof(fullname), "%s::%s", iter->test_suite, iter->test_name);
+
+ if (!(pid = fork())) {
+ // child
+
+ if ((ec = setjmp(jmpbuf))) {
+ exit(ec);
+ } else {
+ test->fn_ptr();
+ exit(0);
+ }
+ } else {
+ if (waitpid(pid, &status, 0) == -1) {
+ fprintf(stderr, "waitpid() failed\n");
+ return 1;
+ }
+
+ ec = WEXITSTATUS(status);
+ switch (ec) {
+ case 0:
+ printf("%s " GREEN "[PASS]" RESET "\n", fullname);
+ return 0;
+ case 11:
+ note = " (SIGSEGV)";
+ break;
+ }
+
+ printf("%s " RED "[FAIL] %d%s" RESET "\n", fullname, ec, note);
+ return ec;
+ }
+}