FDRRecordPrinterTest.cpp
4.7 KB
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
//===- llvm/unittest/XRay/FDRRecordPrinterTest.cpp --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/raw_ostream.h"
#include "llvm/XRay/FDRRecords.h"
#include "llvm/XRay/RecordPrinter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
namespace llvm {
namespace xray {
namespace {
using ::testing::Eq;
template <class RecordType> struct Helper {};
template <> struct Helper<BufferExtents> {
static std::unique_ptr<Record> construct() {
return std::make_unique<BufferExtents>(1);
}
static const char *expected() { return "<Buffer: size = 1 bytes>"; }
};
template <> struct Helper<WallclockRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<WallclockRecord>(1, 2);
}
static const char *expected() { return "<Wall Time: seconds = 1.000002>"; }
};
template <> struct Helper<NewCPUIDRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<NewCPUIDRecord>(1, 2);
}
static const char *expected() { return "<CPU: id = 1, tsc = 2>"; }
};
template <> struct Helper<TSCWrapRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<TSCWrapRecord>(1);
}
static const char *expected() { return "<TSC Wrap: base = 1>"; }
};
template <> struct Helper<CustomEventRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<CustomEventRecord>(4, 1, 2, "data");
}
static const char *expected() {
return "<Custom Event: tsc = 1, cpu = 2, size = 4, data = 'data'>";
}
};
template <> struct Helper<CallArgRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<CallArgRecord>(1);
}
static const char *expected() {
return "<Call Argument: data = 1 (hex = 0x1)>";
}
};
template <> struct Helper<PIDRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<PIDRecord>(1);
}
static const char *expected() { return "<PID: 1>"; }
};
template <> struct Helper<NewBufferRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<NewBufferRecord>(1);
}
static const char *expected() { return "<Thread ID: 1>"; }
};
template <> struct Helper<EndBufferRecord> {
static std::unique_ptr<Record> construct() {
return std::make_unique<EndBufferRecord>();
}
static const char *expected() { return "<End of Buffer>"; }
};
template <class T> class PrinterTest : public ::testing::Test {
protected:
std::string Data;
raw_string_ostream OS;
RecordPrinter P;
std::unique_ptr<Record> R;
public:
PrinterTest() : Data(), OS(Data), P(OS), R(Helper<T>::construct()) {}
};
TYPED_TEST_CASE_P(PrinterTest);
TYPED_TEST_P(PrinterTest, PrintsRecord) {
ASSERT_NE(nullptr, this->R);
ASSERT_FALSE(errorToBool(this->R->apply(this->P)));
this->OS.flush();
EXPECT_THAT(this->Data, Eq(Helper<TypeParam>::expected()));
}
REGISTER_TYPED_TEST_CASE_P(PrinterTest, PrintsRecord);
using FDRRecordTypes =
::testing::Types<BufferExtents, NewBufferRecord, EndBufferRecord,
NewCPUIDRecord, TSCWrapRecord, WallclockRecord,
CustomEventRecord, CallArgRecord, BufferExtents,
PIDRecord>;
INSTANTIATE_TYPED_TEST_CASE_P(Records, PrinterTest, FDRRecordTypes);
TEST(FDRRecordPrinterTest, WriteFunctionRecordEnter) {
std::string Data;
raw_string_ostream OS(Data);
RecordPrinter P(OS);
FunctionRecord R(RecordTypes::ENTER, 1, 2);
ASSERT_FALSE(errorToBool(R.apply(P)));
OS.flush();
EXPECT_THAT(Data, Eq("<Function Enter: #1 delta = +2>"));
}
TEST(FDRRecordPrinterTest, WriteFunctionRecordExit) {
std::string Data;
raw_string_ostream OS(Data);
RecordPrinter P(OS);
FunctionRecord R(RecordTypes::EXIT, 1, 2);
ASSERT_FALSE(errorToBool(R.apply(P)));
OS.flush();
EXPECT_THAT(Data, Eq("<Function Exit: #1 delta = +2>"));
}
TEST(FDRRecordPrinterTest, WriteFunctionRecordTailExit) {
std::string Data;
raw_string_ostream OS(Data);
RecordPrinter P(OS);
FunctionRecord R(RecordTypes::TAIL_EXIT, 1, 2);
ASSERT_FALSE(errorToBool(R.apply(P)));
OS.flush();
EXPECT_THAT(Data, Eq("<Function Tail Exit: #1 delta = +2>"));
}
TEST(FDRRecordPrinterTest, WriteFunctionRecordEnterArg) {
std::string Data;
raw_string_ostream OS(Data);
RecordPrinter P(OS);
FunctionRecord R(RecordTypes::ENTER_ARG, 1, 2);
ASSERT_FALSE(errorToBool(R.apply(P)));
OS.flush();
EXPECT_THAT(Data, Eq("<Function Enter With Arg: #1 delta = +2>"));
}
} // namespace
} // namespace xray
} // namespace llvm