1 //===- llvm/unittest/XRay/FDRRecordPrinterTest.cpp --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 #include "llvm/Support/raw_ostream.h"
9 #include "llvm/XRay/FDRRecords.h"
10 #include "llvm/XRay/RecordPrinter.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
21 template <class RecordType
> struct Helper
{};
23 template <> struct Helper
<BufferExtents
> {
24 static std::unique_ptr
<Record
> construct() {
25 return std::make_unique
<BufferExtents
>(1);
28 static const char *expected() { return "<Buffer: size = 1 bytes>"; }
31 template <> struct Helper
<WallclockRecord
> {
32 static std::unique_ptr
<Record
> construct() {
33 return std::make_unique
<WallclockRecord
>(1, 2);
36 static const char *expected() { return "<Wall Time: seconds = 1.000002>"; }
39 template <> struct Helper
<NewCPUIDRecord
> {
40 static std::unique_ptr
<Record
> construct() {
41 return std::make_unique
<NewCPUIDRecord
>(1, 2);
44 static const char *expected() { return "<CPU: id = 1, tsc = 2>"; }
47 template <> struct Helper
<TSCWrapRecord
> {
48 static std::unique_ptr
<Record
> construct() {
49 return std::make_unique
<TSCWrapRecord
>(1);
52 static const char *expected() { return "<TSC Wrap: base = 1>"; }
55 template <> struct Helper
<CustomEventRecord
> {
56 static std::unique_ptr
<Record
> construct() {
57 return std::make_unique
<CustomEventRecord
>(4, 1, 2, "data");
60 static const char *expected() {
61 return "<Custom Event: tsc = 1, cpu = 2, size = 4, data = 'data'>";
65 template <> struct Helper
<CallArgRecord
> {
66 static std::unique_ptr
<Record
> construct() {
67 return std::make_unique
<CallArgRecord
>(1);
70 static const char *expected() {
71 return "<Call Argument: data = 1 (hex = 0x1)>";
75 template <> struct Helper
<PIDRecord
> {
76 static std::unique_ptr
<Record
> construct() {
77 return std::make_unique
<PIDRecord
>(1);
80 static const char *expected() { return "<PID: 1>"; }
83 template <> struct Helper
<NewBufferRecord
> {
84 static std::unique_ptr
<Record
> construct() {
85 return std::make_unique
<NewBufferRecord
>(1);
88 static const char *expected() { return "<Thread ID: 1>"; }
91 template <> struct Helper
<EndBufferRecord
> {
92 static std::unique_ptr
<Record
> construct() {
93 return std::make_unique
<EndBufferRecord
>();
96 static const char *expected() { return "<End of Buffer>"; }
99 template <class T
> class PrinterTest
: public ::testing::Test
{
102 raw_string_ostream OS
;
104 std::unique_ptr
<Record
> R
;
107 PrinterTest() : Data(), OS(Data
), P(OS
), R(Helper
<T
>::construct()) {}
110 TYPED_TEST_CASE_P(PrinterTest
);
112 TYPED_TEST_P(PrinterTest
, PrintsRecord
) {
113 ASSERT_NE(nullptr, this->R
);
114 ASSERT_FALSE(errorToBool(this->R
->apply(this->P
)));
116 EXPECT_THAT(this->Data
, Eq(Helper
<TypeParam
>::expected()));
119 REGISTER_TYPED_TEST_CASE_P(PrinterTest
, PrintsRecord
);
120 using FDRRecordTypes
=
121 ::testing::Types
<BufferExtents
, NewBufferRecord
, EndBufferRecord
,
122 NewCPUIDRecord
, TSCWrapRecord
, WallclockRecord
,
123 CustomEventRecord
, CallArgRecord
, BufferExtents
,
125 INSTANTIATE_TYPED_TEST_CASE_P(Records
, PrinterTest
, FDRRecordTypes
);
127 TEST(FDRRecordPrinterTest
, WriteFunctionRecordEnter
) {
129 raw_string_ostream
OS(Data
);
131 FunctionRecord
R(RecordTypes::ENTER
, 1, 2);
132 ASSERT_FALSE(errorToBool(R
.apply(P
)));
134 EXPECT_THAT(Data
, Eq("<Function Enter: #1 delta = +2>"));
137 TEST(FDRRecordPrinterTest
, WriteFunctionRecordExit
) {
139 raw_string_ostream
OS(Data
);
141 FunctionRecord
R(RecordTypes::EXIT
, 1, 2);
142 ASSERT_FALSE(errorToBool(R
.apply(P
)));
144 EXPECT_THAT(Data
, Eq("<Function Exit: #1 delta = +2>"));
147 TEST(FDRRecordPrinterTest
, WriteFunctionRecordTailExit
) {
149 raw_string_ostream
OS(Data
);
151 FunctionRecord
R(RecordTypes::TAIL_EXIT
, 1, 2);
152 ASSERT_FALSE(errorToBool(R
.apply(P
)));
154 EXPECT_THAT(Data
, Eq("<Function Tail Exit: #1 delta = +2>"));
157 TEST(FDRRecordPrinterTest
, WriteFunctionRecordEnterArg
) {
159 raw_string_ostream
OS(Data
);
161 FunctionRecord
R(RecordTypes::ENTER_ARG
, 1, 2);
162 ASSERT_FALSE(errorToBool(R
.apply(P
)));
164 EXPECT_THAT(Data
, Eq("<Function Enter With Arg: #1 delta = +2>"));