aboutsummaryrefslogtreecommitdiff
path: root/02-usart/test_harness/test_harness.c
diff options
context:
space:
mode:
Diffstat (limited to '02-usart/test_harness/test_harness.c')
-rw-r--r--02-usart/test_harness/test_harness.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/02-usart/test_harness/test_harness.c b/02-usart/test_harness/test_harness.c
index a0a2a33..bf9249c 100644
--- a/02-usart/test_harness/test_harness.c
+++ b/02-usart/test_harness/test_harness.c
@@ -1,4 +1,5 @@
#include "test_harness.h"
+#include "fake_env.h"
#include <assert.h>
#include <stdio.h>
@@ -19,6 +20,8 @@ volatile test_t dummy
NULL
};
+extern unsigned char __data_start;
+extern unsigned char _end;
extern test_t __start_tests;
extern test_t __stop_tests;
@@ -72,8 +75,17 @@ void test_printuc(
fprintf(stderr, "%02x == %02x\n", (int) v1, (int) v2);
}
-int do_fork = 1;
+static int do_fork = 1;
+static size_t saved_data_size;
+static unsigned char* saved_data = NULL;
+
int main(int argc, char** argv) {
+ /* Save all initialized data. */
+ saved_data_size = &_end - &__data_start;
+ saved_data = malloc(saved_data_size);
+ memcpy(saved_data, &__data_start, saved_data_size);
+
+
if (argc > 1 && strcmp(argv[1], "--nofork") == 0) {
do_fork = 0;
}
@@ -91,6 +103,26 @@ void test_harness_abort(int ec)
assert("Long jump failed.\n");
}
+/*
+ * When nofork is true, this function will be called after each
+ * test to try and make each test hermetic.
+ *
+ * It does this by reseting the data segment to what it was when
+ * the program was first initialized.
+ *
+ * Of course, without forking, there's no way to guarantee hermetic
+ * testing in C. In fact a simple segementation fault will break
+ * the hermetic testing, but this does a pretty good job of at least
+ * reseting the environment so tests don't directly depend on eachother.
+ */
+static void nofork_reset()
+{
+ wipeout_fake_env();
+
+ /* Reset the data segment to what it was before. */
+ memcpy(&__data_start, saved_data, saved_data_size);
+}
+
static int execute_test(test_t* test)
{
char fullname[512];
@@ -105,9 +137,11 @@ static int execute_test(test_t* test)
if ((ec = setjmp(jmpbuf)) == 0) {
test->fn_ptr();
printf(GREEN "[PASS]" RESET " %s\n", fullname);
+ nofork_reset();
return 0;
} else {
printf(RED "[FAIL] (%d)" RESET " %s\n", ec, fullname);
+ nofork_reset();
return ec;
}
}