Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Classes | Macros | Functions
test.h File Reference
#include <stdbool.h>
Include dependency graph for test.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  test_t
 

Macros

#define TEST_EQUAL_EPSILON   0.1
 
#define TEST_ERROR_DELTA_PERCENT   5
 
#define TEST(NAME, BODY)
 
#define ASSERT(BOOL, OK, ERR)   test_assert(T, OK, ERR, BOOL)
 
#define ASSERT_OK(BOOL, LABEL)    ASSERT(BOOL, LABEL " is okay", LABEL " is not okay")
 
#define ASSERT_EQ(GOT, WANT, GOT_LABEL, WANT_LABEL)    test_assert_equal(T, GOT_LABEL, WANT_LABEL, GOT, WANT)
 
#define ASSERT_IN_ERROR(GOT, WANT, GOT_LABEL, WANT_LABEL)    test_assert_within_error(T, GOT_LABEL, WANT_LABEL, GOT, WANT)
 

Functions

test_ttest_start (const char *name)
 
void test_end (test_t *t)
 
void test_assert (test_t *t, const char *pass_message, const char *fail_message, bool passes)
 
void test_assert_equal (test_t *t, const char *a_label, const char *b_label, float a, float b)
 
void test_assert_within_error (test_t *t, const char *actual_label, const char *expected_label, float actual, float expected)
 

Macro Definition Documentation

◆ ASSERT

#define ASSERT (   BOOL,
  OK,
  ERR 
)    test_assert(T, OK, ERR, BOOL)

Definition at line 27 of file test.h.

◆ ASSERT_EQ

#define ASSERT_EQ (   GOT,
  WANT,
  GOT_LABEL,
  WANT_LABEL 
)     test_assert_equal(T, GOT_LABEL, WANT_LABEL, GOT, WANT)

Definition at line 32 of file test.h.

◆ ASSERT_IN_ERROR

#define ASSERT_IN_ERROR (   GOT,
  WANT,
  GOT_LABEL,
  WANT_LABEL 
)     test_assert_within_error(T, GOT_LABEL, WANT_LABEL, GOT, WANT)

Definition at line 35 of file test.h.

◆ ASSERT_OK

#define ASSERT_OK (   BOOL,
  LABEL 
)     ASSERT(BOOL, LABEL " is okay", LABEL " is not okay")

Definition at line 29 of file test.h.

◆ TEST

#define TEST (   NAME,
  BODY 
)
Value:
{ \
test_t *T = test_start(#NAME); \
BODY test_end(T); \
}
Definition: test.h:39
test_t * test_start(const char *name)
Definition: test.c:9
void test_end(test_t *t)
Definition: test.c:22

Definition at line 19 of file test.h.

◆ TEST_EQUAL_EPSILON

#define TEST_EQUAL_EPSILON   0.1

Definition at line 7 of file test.h.

◆ TEST_ERROR_DELTA_PERCENT

#define TEST_ERROR_DELTA_PERCENT   5

Definition at line 11 of file test.h.

Function Documentation

◆ test_assert()

void test_assert ( test_t t,
const char *  pass_message,
const char *  fail_message,
bool  passes 
)

Definition at line 33 of file test.c.

34 {
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}
#define TEST_OK
Definition: Common.h:17
#define TEST_ERR
Definition: Common.h:18
const char * name
Definition: test.h:40
bool passes
Definition: test.h:41

◆ test_assert_equal()

void test_assert_equal ( test_t t,
const char *  a_label,
const char *  b_label,
float  a,
float  b 
)

Definition at line 44 of file test.c.

45 {
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}
#define TEST_EQUAL_EPSILON
Definition: test.h:7

◆ test_assert_within_error()

void test_assert_within_error ( test_t t,
const char *  actual_label,
const char *  expected_label,
float  actual,
float  expected 
)

Definition at line 56 of file test.c.

58 {
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_ERROR_DELTA_PERCENT
Definition: test.h:11

◆ test_end()

void test_end ( test_t t)

Definition at line 22 of file test.c.

22 {
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}
#define TEST_FAIL
Definition: Common.h:19
#define TEST_PASS
Definition: Common.h:16

◆ test_start()

test_t * test_start ( const char *  name)

Definition at line 9 of file test.c.

9 {
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}
#define TEST_START
Definition: Common.h:15