Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
Functions
test.c File Reference
#include "test.h"
#include "Utils/Common.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for test.c:

Go to the source code of this file.

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)
 

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
Definition: test.h:39