-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
continuous_state_test.cc
109 lines (90 loc) · 3.81 KB
/
continuous_state_test.cc
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
#include "drake/systems/framework/continuous_state.h"
#include <memory>
#include <Eigen/Dense>
#include <gtest/gtest.h>
#include "drake/systems/framework/basic_vector.h"
#include "drake/systems/framework/vector_base.h"
namespace drake {
namespace systems {
namespace {
constexpr int kPositionLength = 2;
constexpr int kVelocityLength = 1;
constexpr int kMiscLength = 1;
constexpr int kLength = kPositionLength + kVelocityLength + kMiscLength;
std::unique_ptr<VectorBase<double>> MakeSomeVector() {
return BasicVector<double>::Make({1, 2, 3, 4});
}
class ContinuousStateTest : public ::testing::Test {
protected:
void SetUp() override {
continuous_state_.reset(new ContinuousState<double>(
MakeSomeVector(), kPositionLength, kVelocityLength, kMiscLength));
}
std::unique_ptr<ContinuousState<double>> continuous_state_;
};
TEST_F(ContinuousStateTest, Access) {
EXPECT_EQ(kLength, continuous_state_->size());
EXPECT_EQ(kPositionLength,
continuous_state_->get_generalized_position().size());
EXPECT_EQ(1, continuous_state_->get_generalized_position().GetAtIndex(0));
EXPECT_EQ(2, continuous_state_->get_generalized_position().GetAtIndex(1));
EXPECT_EQ(kVelocityLength,
continuous_state_->get_generalized_velocity().size());
EXPECT_EQ(3, continuous_state_->get_generalized_velocity().GetAtIndex(0));
EXPECT_EQ(kMiscLength, continuous_state_->get_misc_continuous_state().size());
EXPECT_EQ(4, continuous_state_->get_misc_continuous_state().GetAtIndex(0));
}
TEST_F(ContinuousStateTest, Mutation) {
continuous_state_->get_mutable_generalized_position().SetAtIndex(0, 5);
continuous_state_->get_mutable_generalized_position().SetAtIndex(1, 6);
continuous_state_->get_mutable_generalized_velocity().SetAtIndex(0, 7);
continuous_state_->get_mutable_misc_continuous_state().SetAtIndex(0, 8);
EXPECT_EQ(5, continuous_state_->get_vector()[0]);
EXPECT_EQ(6, continuous_state_->get_vector()[1]);
EXPECT_EQ(7, continuous_state_->get_vector()[2]);
EXPECT_EQ(8, continuous_state_->get_vector()[3]);
}
// Tests that the continuous state can be indexed as an array.
TEST_F(ContinuousStateTest, ArrayOperator) {
(*continuous_state_)[1] = 42;
EXPECT_EQ(42, continuous_state_->get_generalized_position()[1]);
EXPECT_EQ(4, (*continuous_state_)[3]);
}
TEST_F(ContinuousStateTest, OutOfBoundsAccess) {
EXPECT_THROW(continuous_state_->get_generalized_position().GetAtIndex(2),
std::runtime_error);
EXPECT_THROW(
continuous_state_->get_mutable_generalized_velocity().SetAtIndex(1, 42),
std::runtime_error);
}
// Tests that std::out_of_range is thrown if the component dimensions do not
// sum to the state vector dimension.
TEST_F(ContinuousStateTest, OutOfBoundsConstruction) {
EXPECT_THROW(
continuous_state_.reset(new ContinuousState<double>(
MakeSomeVector(), kPositionLength, kVelocityLength, kMiscLength + 1)),
std::out_of_range);
}
// Tests that std::logic_error is thrown if there are more velocity than
// position variables.
TEST_F(ContinuousStateTest, MoreVelocityThanPositionVariables) {
EXPECT_THROW(
continuous_state_.reset(new ContinuousState<double>(
MakeSomeVector(), 1 /* num_q */, 2 /* num_v */, kMiscLength + 1)),
std::out_of_range);
}
TEST_F(ContinuousStateTest, CopyFrom) {
// Create a zero-initialized continuous state, with the same dimensions as
// the continuous state in the fixture.
ContinuousState<double> next_state(BasicVector<double>::Make({0, 0, 0, 0}),
kPositionLength, kVelocityLength,
kMiscLength);
next_state.CopyFrom(*continuous_state_);
EXPECT_EQ(1, next_state[0]);
EXPECT_EQ(2, next_state[1]);
EXPECT_EQ(3, next_state[2]);
EXPECT_EQ(4, next_state[3]);
}
} // namespace
} // namespace systems
} // namespace drake