-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.c
executable file
·85 lines (73 loc) · 2.32 KB
/
unit_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <assert.h>
#include <stdio.h>
#include "unit_test.h"
#include "unit.h"
#include "pi.h"
Unit unit;
void test_unit_without_throttle_doesnt_move();
void test_unit_without_delta_doesnt_move();
void test_unit_with_throttle_and_delta_does_move();
void text_unit_with_throttle_and_angular_throttle_turns_half_a_circle();
void test_unit_moves_in_direction();
void assert_position(float x, float y);
void assert_position_within_delta(float x, float y, float delta);
void test_unit() {
test_unit_initialize();
test_unit_update();
}
void test_unit_initialize() {
unit_initialize(&unit);
assert_position(0, 0);
assert(unit.direction == 0);
assert(unit.throttle == 0);
}
void test_unit_update() {
test_unit_without_throttle_doesnt_move();
test_unit_without_delta_doesnt_move();
test_unit_with_throttle_and_delta_does_move();
text_unit_with_throttle_and_angular_throttle_turns_half_a_circle();
}
void test_unit_without_throttle_doesnt_move() {
unit_initialize(&unit);
unit.throttle = 0;
unit_update(&unit, 1);
assert_position(0, 0);
}
void test_unit_without_delta_doesnt_move() {
unit_initialize(&unit);
unit.throttle = 1;
unit_update(&unit, 0);
assert_position(0, 0);
}
void test_unit_with_throttle_and_delta_does_move() {
unit_initialize(&unit);
unit.throttle = 1;
unit_update(&unit, 1);
assert_position(0, UNIT_PIXELS_PER_SECOND);
}
void text_unit_with_throttle_and_angular_throttle_turns_half_a_circle() {
unit_initialize(&unit);
unit.throttle = 1;
unit.angular_throttle = -1;
unit_update(&unit, PI / UNIT_RADIANS_PER_SECOND);
assert_position_within_delta(0, UNIT_PIXELS_PER_SECOND / UNIT_RADIANS_PER_SECOND * 2, 0.05);
}
void test_unit_moves_in_direction() {
unit_initialize(&unit);
unit.throttle = 1;
unit.direction = HALF_PI;
unit_update(&unit, 1);
assert_position(0, UNIT_PIXELS_PER_SECOND);
}
void assert_position(float x, float y) {
assert(unit.position.x == x);
assert(unit.position.y == y);
}
void assert_position_within_delta(float x, float y, float delta) {
int assertion_result = unit.position.x > x - delta && unit.position.x < x + delta
&& unit.position.y > y - delta && unit.position.y < y + delta;
if(!assertion_result)
printf("assert %7.1f,%7.1f to equal %7.1fx%7.1f (delta %f)\n",
unit.position.x, unit.position.y, x, y, delta);
assert(assertion_result);
}