-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathlbr_branch_aggregator_test.cc
149 lines (130 loc) · 5.54 KB
/
lbr_branch_aggregator_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "lbr_branch_aggregator.h"
#include <memory>
#include <utility>
#include "branch_aggregation.h"
#include "lbr_aggregation.h"
#include "lbr_aggregator.h"
#include "llvm_propeller_binary_address_mapper.h"
#include "llvm_propeller_binary_content.h"
#include "llvm_propeller_options.pb.h"
#include "llvm_propeller_statistics.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "util/testing/status_matchers.h"
#include "third_party/abseil/absl/status/status.h"
#include "third_party/abseil/absl/status/statusor.h"
namespace devtools_crosstool_autofdo {
namespace {
using ::testing::AllOf;
using ::testing::DoAll;
using ::testing::ElementsAre;
using ::testing::Field;
using ::testing::FieldsAre;
using ::testing::Pair;
using ::testing::Return;
using ::testing::SetArgReferee;
using ::testing::UnorderedElementsAre;
using ::testing::status::IsOk;
using ::testing::status::IsOkAndHolds;
using ::testing::status::StatusIs;
class MockLbrAggregator : public LbrAggregator {
public:
MOCK_METHOD(absl::StatusOr<LbrAggregation>, AggregateLbrData,
(const PropellerOptions& options,
const BinaryContent& binary_content, PropellerStats& stats));
};
TEST(LbrBranchAggregator, GetBranchEndpointAddressesPropagatesErrors) {
const PropellerOptions options;
const BinaryContent binary_content;
PropellerStats stats;
auto mock_aggregator = std::make_unique<MockLbrAggregator>();
EXPECT_CALL(*mock_aggregator, AggregateLbrData)
.WillOnce(Return(absl::InternalError("")));
EXPECT_THAT(
LbrBranchAggregator(std::move(mock_aggregator), options, binary_content)
.GetBranchEndpointAddresses(),
StatusIs(absl::StatusCode::kInternal));
}
TEST(LbrBranchAggregator, GetBranchEndpointAddresses) {
const PropellerOptions options;
const BinaryContent binary_content;
PropellerStats stats;
EXPECT_THAT(
LbrBranchAggregator({.branch_counters = {{{.from = 1, .to = 2}, 1},
{{.from = 3, .to = 3}, 1}},
.fallthrough_counters = {{{.from = 3, .to = 3}, 1},
{{.from = 4, .to = 5}, 1}}})
.GetBranchEndpointAddresses(),
IsOkAndHolds(UnorderedElementsAre(1, 2, 3, 4, 5)));
}
TEST(LbrBranchAggregator, AggregatePropagatesErrors) {
const PropellerOptions options;
const BinaryContent binary_content;
PropellerStats stats;
BinaryAddressMapper binary_address_mapper(
/*selected_functions=*/{}, /*bb_addr_map=*/{}, /*bb_handles=*/{},
/*symbol_info_map=*/{});
auto mock_aggregator = std::make_unique<MockLbrAggregator>();
EXPECT_CALL(*mock_aggregator, AggregateLbrData)
.WillOnce(Return(absl::InternalError("")));
EXPECT_THAT(
LbrBranchAggregator(std::move(mock_aggregator), options, binary_content)
.Aggregate(binary_address_mapper, stats),
StatusIs(absl::StatusCode::kInternal));
}
TEST(LbrBranchAggregator, ConvertsLbrAggregations) {
const PropellerOptions options;
const BinaryContent binary_content;
PropellerStats stats;
BinaryAddressMapper binary_address_mapper(
/*selected_functions=*/{}, /*bb_addr_map=*/{}, /*bb_handles=*/{},
/*symbol_info_map=*/{});
auto mock_aggregator = std::make_unique<MockLbrAggregator>();
EXPECT_CALL(*mock_aggregator, AggregateLbrData)
.WillOnce(Return(LbrAggregation{
.branch_counters = {{{.from = 1, .to = 2}, 3}},
.fallthrough_counters = {{{.from = 4, .to = 5}, 6}},
}));
EXPECT_THAT(
LbrBranchAggregator(std::move(mock_aggregator), options, binary_content)
.Aggregate(binary_address_mapper, stats),
IsOkAndHolds(
AllOf(Field("branch_counters", &BranchAggregation::branch_counters,
ElementsAre(Pair(FieldsAre(1, 2), 3))),
Field("fallthrough_counters",
&BranchAggregation::fallthrough_counters,
ElementsAre(Pair(FieldsAre(4, 5), 6))))));
}
TEST(LbrBranchAggregator, AggregatePropagatesStats) {
const PropellerOptions options;
const BinaryContent binary_content;
PropellerStats stats;
BinaryAddressMapper binary_address_mapper(
/*selected_functions=*/{}, /*bb_addr_map=*/{}, /*bb_handles=*/{},
/*symbol_info_map=*/{});
auto mock_aggregator = std::make_unique<MockLbrAggregator>();
EXPECT_CALL(*mock_aggregator, AggregateLbrData)
.WillOnce(
DoAll(SetArgReferee<2>(PropellerStats{
.profile_stats = {.binary_mmap_num = 1,
.perf_file_parsed = 2,
.br_counters_accumulated = 3},
.disassembly_stats = {.could_not_disassemble = {4, 5},
.may_affect_control_flow = {6, 7},
.cant_affect_control_flow = {8, 9}}}),
Return(LbrAggregation{})));
LbrBranchAggregator aggregator(std::move(mock_aggregator), options,
binary_content);
// Aggregate twice and check that the stats are doubled.
EXPECT_THAT(aggregator.Aggregate(binary_address_mapper, stats), IsOk());
EXPECT_THAT(aggregator.Aggregate(binary_address_mapper, stats), IsOk());
EXPECT_THAT(
stats,
AllOf(Field("profile_stats", &PropellerStats::profile_stats,
FieldsAre(2, 4, 6)),
Field("disassembly_stats", &PropellerStats::disassembly_stats,
FieldsAre(FieldsAre(8, 10), FieldsAre(12, 14),
FieldsAre(16, 18)))));
}
} // namespace
} // namespace devtools_crosstool_autofdo