[go: up one dir, main page]

Menu

[f338fb]: / world / test_Controls.cc  Maximize  Restore  History

Download this file

230 lines (194 with data), 5.6 kB

  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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE Controls
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include "Controls.h"
#include "../geometry/Constants.h"
using namespace Vamos_World;
using namespace Vamos_Geometry;
const int RAW_MOTION_RANGE = 32767;
struct Control_Fixture : public Control_Handler
{
virtual Control& joystick () { return m_joystick; }
virtual Control& keyboard () { return m_keyboard; }
virtual Control& mouse () { return m_mouse; }
Control m_joystick;
Control m_keyboard;
Control m_mouse;
};
struct Keyboard_Fixture : public Control_Fixture
{
Keyboard_Fixture ();
bool on_down_1 (double, double arg)
{
down_1_called = true;
down_1_arg = arg;
return false;
}
bool on_down_2 (double, double arg)
{
down_2_called = true;
down_2_arg = arg;
return true;
}
bool on_down_3 (double, double arg)
{
down_3_called = true;
return false;
}
bool on_up (double, double arg)
{
up_called = true;
up_arg = arg;
return false;
}
Control c;
bool down_1_called;
double down_1_arg;
bool down_2_called;
double down_2_arg;
bool down_3_called;
double down_3_arg;
bool up_called;
double up_arg;
};
Keyboard_Fixture::Keyboard_Fixture () :
down_1_called (false),
down_1_arg (0.0),
down_2_called (false),
down_2_arg (0.0),
down_3_called (false),
down_3_arg (0.0),
up_called (false),
up_arg (0.0)
{
c.bind_action (12, DOWN, (Control_Handler*)this,
(Callback_Function)&Keyboard_Fixture::on_down_1, 1.0);
c.bind_action (12, NONE, (Control_Handler*)this,
(Callback_Function)&Keyboard_Fixture::on_down_2, 2.0);
c.bind_action (12, DOWN, (Control_Handler*)this,
(Callback_Function)&Keyboard_Fixture::on_down_3, 3.0);
c.bind_action (13, UP, (Control_Handler*)this,
(Callback_Function)&Keyboard_Fixture::on_up, 4.0);
}
BOOST_AUTO_TEST_CASE (press_12)
{
Keyboard_Fixture f;
f.c.press (12);
BOOST_CHECK (f.down_1_called);
BOOST_CHECK (f.down_2_called);
BOOST_CHECK (!f.down_3_called);
BOOST_CHECK (!f.up_called);
BOOST_CHECK_EQUAL (f.down_1_arg, 1.0);
BOOST_CHECK_EQUAL (f.down_2_arg, 2.0);
BOOST_CHECK_EQUAL (f.down_3_arg, 0.0);
BOOST_CHECK_EQUAL (f.up_arg, 0.0);
}
BOOST_AUTO_TEST_CASE (release_13)
{
Keyboard_Fixture f;
f.c.press (13);
BOOST_CHECK (!f.up_called);
BOOST_CHECK_EQUAL (f.up_arg, 0.0);
f.c.release (13);
BOOST_CHECK (f.up_called);
BOOST_CHECK_EQUAL (f.up_arg, 4.0);
}
//-----------------------------------------------------------------------------
struct Motion_Fixture : public Control_Fixture
{
Motion_Fixture ();
bool on_move_1_forward (double arg, double)
{
move_1_forward_arg = arg;
return false;
}
bool on_move_1_backward (double arg, double)
{
move_1_backward_arg = arg;
return false;
}
bool on_move_2 (double arg, double)
{
move_2_arg = arg;
return false;
}
Control c;
double move_1_forward_arg;
double move_1_backward_arg;
double move_2_arg;
};
Motion_Fixture::Motion_Fixture ()
{
c.bind_motion (4, FORWARD, (Control_Handler*)this,
(Callback_Function)&Motion_Fixture::on_move_1_forward,
1.0, 0.0, 0.0, 0.0);
c.bind_motion (4, BACKWARD, (Control_Handler*)this,
(Callback_Function)&Motion_Fixture::on_move_1_backward,
-1.0, 0.0, 0.0, 0.0);
c.bind_motion (7, NONE, (Control_Handler*)this,
(Callback_Function)&Motion_Fixture::on_move_2,
0.5, 0.5, 0.0, 0.0);
}
BOOST_AUTO_TEST_CASE (half_range)
{
Motion_Fixture f;
f.c.move (4, -1000);
BOOST_CHECK_CLOSE (f.move_1_forward_arg, 1000.0 / RAW_MOTION_RANGE, 1e-3);
BOOST_CHECK_EQUAL (f.move_1_backward_arg, 0.0);
f.c.move (4, 1000);
BOOST_CHECK_EQUAL (f.move_1_forward_arg, 0.0);
BOOST_CHECK_CLOSE (f.move_1_backward_arg, 1000.0 / RAW_MOTION_RANGE, 1e-3);
}
BOOST_AUTO_TEST_CASE (full_range)
{
Motion_Fixture f;
f.c.move (7, 32767);
BOOST_CHECK_CLOSE (f.move_2_arg, 0.0, 1e-3);
f.c.move (7, -32767);
BOOST_CHECK_CLOSE (f.move_2_arg, 1.0, 1e-3);
}
//-----------------------------------------------------------------------------
struct Deadband_Fixture : public Control_Fixture
{
Deadband_Fixture ();
bool on_move (double arg, double)
{
move_arg = arg;
return false;
}
Control c;
double move_arg;
};
Deadband_Fixture::Deadband_Fixture ()
{
c.bind_motion (5, NONE, (Control_Handler*)this,
(Callback_Function)&Deadband_Fixture::on_move,
2.0, 1.0, 0.1, 0.2);
}
BOOST_AUTO_TEST_CASE (deadband)
{
Deadband_Fixture f;
// lower deadband
f.c.move (5, 0);
BOOST_CHECK_CLOSE (f.move_arg, 1.0, 1e-3);
f.c.move (5, RAW_MOTION_RANGE / 10);
BOOST_CHECK_CLOSE (f.move_arg, 1.0, 1e-3);
f.c.move (5, -RAW_MOTION_RANGE / 10);
BOOST_CHECK_CLOSE (f.move_arg, 1.0, 1e-3);
// upper deadband
f.c.move (5, RAW_MOTION_RANGE);
BOOST_CHECK_CLOSE (f.move_arg, -1.0, 1e-3);
f.c.move (5, RAW_MOTION_RANGE - RAW_MOTION_RANGE / 5 + 1);
BOOST_CHECK_CLOSE (f.move_arg, -1.0, 1e-3);
f.c.move (5, -RAW_MOTION_RANGE + RAW_MOTION_RANGE / 5 - 1);
BOOST_CHECK_CLOSE (f.move_arg, 3.0, 1e-3);
f.c.move (5, -RAW_MOTION_RANGE);
BOOST_CHECK_CLOSE (f.move_arg, 3.0, 1e-3);
// negative active band
f.c.move (5, RAW_MOTION_RANGE * 9 / 20);
BOOST_CHECK_SMALL (f.move_arg, 1e-3);
// positive active band
f.c.move (5, -RAW_MOTION_RANGE * 9 / 20);
BOOST_CHECK_CLOSE (f.move_arg, 2.0, 1e-3);
}