forked from google/bloaty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.h
291 lines (253 loc) · 9.11 KB
/
test.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BLOATY_TESTS_TEST_H_
#define BLOATY_TESTS_TEST_H_
#include <fstream>
#include <memory>
#include <string>
#include <unordered_set>
#include <tuple>
#include <vector>
#include "absl/strings/numbers.h"
#include "absl/strings/str_split.h"
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "gtest/gtest.h"
#include "strarr.h"
#include "bloaty.h"
#include "bloaty.pb.h"
inline bool GetFileSize(const std::string& filename, uint64_t* size) {
FILE* file = fopen(filename.c_str(), "rb");
if (!file) {
std::cerr << "Couldn't get file size for: " << filename << "\n";
return false;
}
fseek(file, 0L, SEEK_END);
*size = ftell(file);
fclose(file);
return true;
}
inline std::string GetTestDirectory() {
char pathbuf[PATH_MAX];
if (!getcwd(pathbuf, sizeof(pathbuf))) {
return "";
}
std::string path(pathbuf);
size_t pos = path.rfind('/');
return path.substr(pos + 1);
}
inline std::string DebugString(const google::protobuf::Message& message) {
std::string ret;
google::protobuf::TextFormat::PrintToString(message, &ret);
return ret;
}
#define NONE_STRING "[None]"
// Testing Bloaty requires a delicate balance. Bloaty's output is by its
// nature very compiler and platform dependent. So we want to verify correct
// operation without overspecifying how the platform should behave.
class BloatyTest : public ::testing::Test {
protected:
void CheckConsistencyForRow(const bloaty::RollupRow& row, bool is_toplevel,
bool diff_mode, int* count) {
// If any children exist, they should sum up to this row's values.
// Also none of the children should have the same name.
std::unordered_set<std::string> names;
if (row.sorted_children.size() > 0) {
uint64_t vmtotal = 0;
uint64_t filetotal = 0;
for (const auto& child : row.sorted_children) {
vmtotal += child.vmsize;
filetotal += child.filesize;
CheckConsistencyForRow(child, false, diff_mode, count);
ASSERT_TRUE(names.insert(child.name).second);
ASSERT_FALSE(child.vmsize == 0 && child.filesize == 0);
}
if (!diff_mode) {
ASSERT_EQ(vmtotal, row.vmsize);
ASSERT_EQ(filetotal, row.filesize);
}
} else {
// Count leaf rows.
*count += 1;
}
if (!is_toplevel && row.sorted_children.size() == 1) {
ASSERT_NE(NONE_STRING, row.sorted_children[0].name);
}
}
void CheckCSVConsistency(int row_count) {
std::ostringstream stream;
bloaty::OutputOptions options;
options.output_format = bloaty::OutputFormat::kCSV;
output_->Print(options, &stream);
std::string csv_output = stream.str();
std::vector<std::string> rows = absl::StrSplit(csv_output, '\n');
// Output ends with a final '\n', trim this.
ASSERT_EQ("", rows[rows.size() - 1]);
rows.pop_back();
ASSERT_GT(rows.size(), 0); // There should be a header row.
ASSERT_EQ(rows.size() - 1, row_count);
bool first = true;
for (const auto& row : rows) {
std::vector<std::string> cols = absl::StrSplit(row, ',');
if (first) {
// header row should be: header1,header2,...,vmsize,filesize
std::vector<std::string> expected_headers(output_->source_names());
expected_headers.push_back("vmsize");
expected_headers.push_back("filesize");
ASSERT_EQ(cols, expected_headers);
first = false;
} else {
// Final two columns should parse as integer.
int out;
ASSERT_EQ(output_->source_names().size() + 2, cols.size());
ASSERT_TRUE(absl::SimpleAtoi(cols[cols.size() - 1], &out));
ASSERT_TRUE(absl::SimpleAtoi(cols[cols.size() - 2], &out));
}
}
}
void CheckConsistency(const bloaty::Options& options) {
ASSERT_EQ(options.base_filename_size() > 0, output_->diff_mode());
if (!output_->diff_mode()) {
size_t total_input_size = 0;
for (const auto& filename : options.filename()) {
uint64_t size;
ASSERT_TRUE(GetFileSize(filename, &size));
total_input_size += size;
}
ASSERT_EQ(top_row_->filesize, total_input_size);
}
int rows = 0;
CheckConsistencyForRow(*top_row_, true, output_->diff_mode(), &rows);
CheckCSVConsistency(rows);
ASSERT_EQ("TOTAL", top_row_->name);
}
std::string JoinStrings(const std::vector<std::string>& strings) {
std::string ret = strings[0];
for (size_t i = 1; i < strings.size(); i++) {
ret += " " + strings[i];
}
return ret;
}
bool TryRunBloatyWithOptions(const bloaty::Options& options,
const bloaty::OutputOptions& output_options) {
output_.reset(new bloaty::RollupOutput);
top_row_ = &output_->toplevel_row();
std::string error;
bloaty::MmapInputFileFactory factory;
if (bloaty::BloatyMain(options, factory, output_.get(), &error)) {
CheckConsistency(options);
output_->Print(output_options, &std::cerr);
return true;
} else {
std::cerr << "Bloaty returned error:" << error << "\n";
return false;
}
}
bool TryRunBloaty(const std::vector<std::string>& strings) {
bloaty::Options options;
bloaty::OutputOptions output_options;
std::string error;
StrArr str_arr(strings);
int argc = strings.size();
char** argv = str_arr.get();
bool ok = bloaty::ParseOptions(false, &argc, &argv, &options,
&output_options, &error);
if (!ok) {
std::cerr << "Error parsing options: " << error;
return false;
}
return TryRunBloatyWithOptions(options, output_options);
}
void RunBloaty(const std::vector<std::string>& strings) {
std::cerr << "Running bloaty: " << JoinStrings(strings) << "\n";
ASSERT_TRUE(TryRunBloaty(strings));
}
void RunBloatyWithOptions(const bloaty::Options& options,
const bloaty::OutputOptions& output_options) {
std::cerr << "Running bloaty, options: " << DebugString(options) << "\n";
ASSERT_TRUE(TryRunBloatyWithOptions(options, output_options));
}
void AssertBloatyFails(const std::vector<std::string>& strings,
const std::string& /*msg_regex*/) {
// TODO(haberman): verify msg_regex by making all errors logged to a
// standard place.
ASSERT_FALSE(TryRunBloaty(strings));
}
// Special constants for asserting of children.
static constexpr int kUnknown = -1;
static constexpr int kSameAsVM = -2; // Only for file size.
void AssertChildren(
const bloaty::RollupRow& row,
const std::vector<std::tuple<std::string, int, int>>& children) {
size_t i = 0;
for (const auto& child : row.sorted_children) {
std::string expected_name;
int expected_vm, expected_file;
std::tie(expected_name, expected_vm, expected_file) = children[i];
// Excluding leading '_' is kind of a hack to exclude symbols
// automatically inserted by the compiler, like __x86.get_pc_thunk.bx
// for 32-bit x86 builds or _IO_stdin_used in binaries.
//
// Excluding leading '[' is for things like this:
//
// [None]
// [ELF Headers]
// [AR Headers]
// etc.
if (child.name[0] == '[' || child.name[0] == '_') {
continue;
}
EXPECT_EQ(expected_name, child.name);
// <0 indicates that we don't know what the exact size should be (for
// example for functions).
if (expected_vm == kUnknown) {
// Always pass.
} else if (expected_vm > 0) {
EXPECT_GE(child.vmsize, expected_vm);
// Allow some overhead.
EXPECT_LE(child.vmsize, (expected_vm * 1.1) + 40);
} else {
ASSERT_TRUE(false);
}
if (expected_file == kSameAsVM) {
expected_file = child.vmsize;
}
if (expected_file != kUnknown) {
EXPECT_GE(child.filesize, expected_file);
// Allow some overhead.
EXPECT_LE(child.filesize, (expected_file * 1.2) + 70);
}
if (++i == children.size()) {
// We allow the actual data to have excess elements.
break;
}
}
// All expected elements must be present.
ASSERT_EQ(i, children.size());
}
const bloaty::RollupRow* FindRow(const std::string& name) {
for (const auto& child : top_row_->sorted_children) {
if (child.name == name) {
return &child;
}
}
EXPECT_TRUE(false) << name;
return nullptr;
}
std::unique_ptr<bloaty::RollupOutput> output_;
const bloaty::RollupRow* top_row_;
};
constexpr int BloatyTest::kUnknown;
constexpr int BloatyTest::kSameAsVM;
#endif // BLOATY_TESTS_TEST_H_