forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiffxd_min_test.cc
44 lines (38 loc) · 1.76 KB
/
autodiffxd_min_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
/* clang-format off to disable clang-format-includes */
#include "drake/common/autodiff.h"
#include "drake/common/ad/test/standard_operations_test.h"
/* clang-format on */
#include "drake/common/test_utilities/eigen_matrix_compare.h"
namespace drake {
namespace test {
namespace {
TEST_F(AutoDiffXdTest, Min) {
CHECK_BINARY_FUNCTION_ADS_ADS(min, x, y, 0.3);
CHECK_BINARY_FUNCTION_ADS_ADS(min, x, y, -0.3);
CHECK_BINARY_FUNCTION_ADS_ADS(min, y, x, 0.4);
CHECK_BINARY_FUNCTION_ADS_ADS(min, y, x, -0.4);
}
TEST_F(AutoDiffXdTest, TieBreakingCheckMinBothNonEmpty) {
// Given `min(v1, v2)`, our overload returns the first argument `v1` when
// `v1 == v2` holds if both `v1` and `v2` have non-empty derivatives. In
// Drake, we rely on this implementation-detail. This test checks if the
// property holds so that we can detect a possible change in future.
const AutoDiffXd v1{1.0, Vector1<double>(3.)};
const AutoDiffXd v2{1.0, Vector1<double>(2.)};
EXPECT_EQ(min(v1, v2).derivatives()[0], 3.0); // Returns v1, not v2.
}
TEST_F(AutoDiffXdTest, TieBreakingCheckMinOneNonEmpty) {
// Given `min(v1, v2)`, our overload returns whichever argument has non-empty
// derivatives in the case where only one has non-empty derivatives. In
// Drake, we rely on this implementation-detail. This test checks if the
// property holds so that we can detect a possible change in future.
const AutoDiffXd v1{1.0};
const AutoDiffXd v2{1.0, Vector1<double>(2.)};
EXPECT_TRUE(CompareMatrices(min(v1, v2).derivatives(),
Vector1<double>(2.))); // Returns v2, not v1.
EXPECT_TRUE(CompareMatrices(min(v2, v1).derivatives(),
Vector1<double>(2.))); // Returns v2, not v1.
}
} // namespace
} // namespace test
} // namespace drake