Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
test.c
Go to the documentation of this file.
1#include "test.h"
2#include "Utils/Common.h"
3
4#include <assert.h>
5#include <math.h>
6#include <stdio.h>
7#include <stdlib.h>
8
9test_t *test_start(const char *name) {
10 test_t *t = malloc(sizeof(test_t));
11 assert(t != NULL);
12
13 // TODO strncpy?
14 t->name = name;
15 t->passes = true;
16
17 printf(TEST_START "%s\n", t->name);
18
19 return t;
20}
21
22void test_end(test_t *t) {
23 if (t->passes) {
24 printf(TEST_PASS "%s all passed\n\n", t->name);
25 } else {
26 printf(TEST_FAIL "%s failed\n\n", t->name);
27 }
28
29 // NOTE If t->name is copied in test_start, it needs to be freed
30 free(t);
31}
32
33void test_assert(test_t *t, const char *pass_message, const char *fail_message,
34 bool passes) {
35 if (passes) {
36 printf(TEST_OK "%s %s\n", t->name, pass_message);
37 } else {
38 printf(TEST_ERR "%s %s\n", t->name, fail_message);
39 }
40
41 t->passes &= passes;
42}
43
44void test_assert_equal(test_t *t, const char *a_label, const char *b_label,
45 float a, float b) {
46 bool passes = fabs(a - b) < TEST_EQUAL_EPSILON;
47 if (passes) {
48 printf(TEST_OK "%s %s = %s (%f = %f)\n", t->name, a_label, b_label, a, b);
49 } else {
50 printf(TEST_ERR "%s %s ≠ %s (%f ≠ %f)\n", t->name, a_label, b_label, a, b);
51 }
52
53 t->passes &= passes;
54}
55
56void test_assert_within_error(test_t *t, const char *actual_label,
57 const char *expected_label, float actual,
58 float expected) {
59 float percent_error = (actual - expected) / expected * 100;
60 bool passes = fabs(percent_error) < TEST_ERROR_DELTA_PERCENT;
61 if (passes) {
62 printf(TEST_OK "%s %s ≈ %s (%f ≈ %f)\n", t->name, actual_label,
63 expected_label, actual, expected);
64 } else {
65 printf(TEST_ERR "%s %s ≠ %s (%f ≠ %f)\n", t->name, actual_label,
66 expected_label, actual, expected);
67 }
68
69 t->passes &= passes;
70}
#define TEST_START
Definition: Common.h:15
#define TEST_FAIL
Definition: Common.h:19
#define TEST_PASS
Definition: Common.h:16
#define TEST_OK
Definition: Common.h:17
#define TEST_ERR
Definition: Common.h:18
Definition: test.h:39
const char * name
Definition: test.h:40
bool passes
Definition: test.h:41
void test_assert(test_t *t, const char *pass_message, const char *fail_message, bool passes)
Definition: test.c:33
void test_assert_equal(test_t *t, const char *a_label, const char *b_label, float a, float b)
Definition: test.c:44
test_t * test_start(const char *name)
Definition: test.c:9
void test_assert_within_error(test_t *t, const char *actual_label, const char *expected_label, float actual, float expected)
Definition: test.c:56
void test_end(test_t *t)
Definition: test.c:22
#define TEST_ERROR_DELTA_PERCENT
Definition: test.h:11
#define TEST_EQUAL_EPSILON
Definition: test.h:7