Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1#ifndef TEST_H
2#define TEST_H
3
4#include <stdbool.h>
5
6#ifndef TEST_EQUAL_EPSILON
7#define TEST_EQUAL_EPSILON 0.1
8#endif
9
10#ifndef TEST_ERROR_DELTA_PERCENT
11#define TEST_ERROR_DELTA_PERCENT 5
12#endif
13
14// TODO Find a way to do
15// TEST(NAME) {
16// // test body
17// }
18// TODO Also find a way for the name to have spaces / nicer formatting
19#define TEST(NAME, BODY) \
20 { \
21 test_t *T = test_start(#NAME); \
22 BODY test_end(T); \
23 }
24
25// TODO Add versions of the macros where labels are the variable names
26
27#define ASSERT(BOOL, OK, ERR) test_assert(T, OK, ERR, BOOL)
28
29#define ASSERT_OK(BOOL, LABEL) \
30 ASSERT(BOOL, LABEL " is okay", LABEL " is not okay")
31
32#define ASSERT_EQ(GOT, WANT, GOT_LABEL, WANT_LABEL) \
33 test_assert_equal(T, GOT_LABEL, WANT_LABEL, GOT, WANT)
34
35#define ASSERT_IN_ERROR(GOT, WANT, GOT_LABEL, WANT_LABEL) \
36 test_assert_within_error(T, GOT_LABEL, WANT_LABEL, GOT, WANT)
37
38// Records status for a set of assertions.
39typedef struct {
40 const char *name;
41 bool passes;
42} test_t;
43
44// Creates a test and prints a test start message.
45test_t *test_start(const char *name);
46
47// Destroys a test and prints a test end message.
48void test_end(test_t *t);
49
50// Prints a message depending on if the test passes or fails
51void test_assert(test_t *t, const char *pass_message, const char *fail_message,
52 bool passes);
53
54// Tests if the values are approximately equal (pass) or
55// not (fail); approximately equal depends on the value of ϵ
56// (TEST_EQUAL_EPSILON)
57void test_assert_equal(test_t *t, const char *a_label, const char *b_label,
58 float a, float b);
59
60// Tests if the values are within a percent error threshold (pass) or not
61// (fail); percent error threshold depends on the value of δ
62// (TEST_ERROR_DELTA_PERCENT)
63void test_assert_within_error(test_t *t, const char *actual_label,
64 const char *expected_label, float actual,
65 float expected);
66
67#endif // TEST_H
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