forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiffxd_max_test.cc
41 lines (37 loc) · 1.7 KB
/
autodiffxd_max_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
/* 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 */
namespace drake {
namespace test {
namespace {
TEST_F(AutoDiffXdTest, Max) {
CHECK_BINARY_FUNCTION_ADS_ADS(max, x, y, 0.5);
CHECK_BINARY_FUNCTION_ADS_ADS(max, x, y, -0.3);
CHECK_BINARY_FUNCTION_ADS_ADS(max, y, x, 0.4);
CHECK_BINARY_FUNCTION_ADS_ADS(max, y, x, -0.4);
}
TEST_F(AutoDiffXdTest, TieBreakingCheckMaxBothNonEmpty) {
// Given `max(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(max(v1, v2).derivatives()[0], 3.0); // Returns v1, not v2.
}
TEST_F(AutoDiffXdTest, TieBreakingCheckMaxOneNonEmpty) {
// Given `max(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