forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiffxd_heap_test.cc
132 lines (107 loc) · 3.98 KB
/
autodiffxd_heap_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <gtest/gtest.h>
#include "drake/common/autodiff.h"
#include "drake/common/eigen_types.h"
#include "drake/common/test_utilities/limit_malloc.h"
using Eigen::VectorXd;
namespace drake {
namespace test {
namespace {
// Provide some autodiff variables that don't expire at scope end for test
// cases.
class AutoDiffXdHeapTest : public ::testing::Test {
protected:
AutoDiffXd x_{0.4, Eigen::VectorXd::Ones(3)};
AutoDiffXd y_{0.3, Eigen::VectorXd::Ones(3)};
};
// @note The test cases use a sum in function arguments to induce a temporary
// that can potentially be consumed by pass-by-value optimizations. As of this
// writing, none of the tested functions uses the optimization, so current heap
// counts are a baseline for the existing implementations.
// @note The tests cases use `volatile` variables to prevent optimizers from
// removing the tested function calls entirely. As of this writing, there is no
// evidence that the technique is strictly necessary. However, future
// implementations may be vulnerable to dead-code elimination.
TEST_F(AutoDiffXdHeapTest, Abs) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = abs(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Abs2) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = abs2(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Acos) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = acos(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Asin) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = asin(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Atan) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = atan(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Atan2) {
{
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = atan2(x_ + y_, y_);
}
{
LimitMalloc guard({.max_num_allocations = 2, .min_num_allocations = 2});
// Right-hand parameter moves are blocked by code in Eigen; see #14039.
volatile auto v = atan2(y_, x_ + y_);
}
}
TEST_F(AutoDiffXdHeapTest, Cos) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = cos(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Cosh) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = cosh(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Exp) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = exp(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Log) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = log(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Min) {
LimitMalloc guard({.max_num_allocations = 4, .min_num_allocations = 4});
volatile auto v = min(x_ + y_, y_);
volatile auto w = min(x_, x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Max) {
LimitMalloc guard({.max_num_allocations = 4, .min_num_allocations = 4});
volatile auto v = max(x_ + y_, y_);
volatile auto w = max(x_, x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Pow) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = pow(x_ + y_, 2.0);
}
TEST_F(AutoDiffXdHeapTest, Sin) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = sin(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Sinh) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = sinh(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Sqrt) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = sqrt(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Tan) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = tan(x_ + y_);
}
TEST_F(AutoDiffXdHeapTest, Tanh) {
LimitMalloc guard({.max_num_allocations = 1, .min_num_allocations = 1});
volatile auto v = tanh(x_ + y_);
}
} // namespace
} // namespace test
} // namespace drake